2
0

mtdsplit_tplink.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
  3. * Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/partitions.h>
  16. #include <linux/byteorder/generic.h>
  17. #include "mtdsplit.h"
  18. #define TPLINK_NR_PARTS 2
  19. #define TPLINK_MIN_ROOTFS_OFFS 0x80000 /* 512KiB */
  20. #define MD5SUM_LEN 16
  21. struct fw_v1 {
  22. char vendor_name[24];
  23. char fw_version[36];
  24. uint32_t hw_id; /* hardware id */
  25. uint32_t hw_rev; /* hardware revision */
  26. uint32_t unk1;
  27. uint8_t md5sum1[MD5SUM_LEN];
  28. uint32_t unk2;
  29. uint8_t md5sum2[MD5SUM_LEN];
  30. uint32_t unk3;
  31. uint32_t kernel_la; /* kernel load address */
  32. uint32_t kernel_ep; /* kernel entry point */
  33. uint32_t fw_length; /* total length of the firmware */
  34. uint32_t kernel_ofs; /* kernel data offset */
  35. uint32_t kernel_len; /* kernel data length */
  36. uint32_t rootfs_ofs; /* rootfs data offset */
  37. uint32_t rootfs_len; /* rootfs data length */
  38. uint32_t boot_ofs; /* bootloader data offset */
  39. uint32_t boot_len; /* bootloader data length */
  40. uint8_t pad[360];
  41. } __attribute__ ((packed));
  42. struct fw_v2 {
  43. char fw_version[48]; /* 0x04: fw version string */
  44. uint32_t hw_id; /* 0x34: hardware id */
  45. uint32_t hw_rev; /* 0x38: FIXME: hardware revision? */
  46. uint32_t unk1; /* 0x3c: 0x00000000 */
  47. uint8_t md5sum1[MD5SUM_LEN]; /* 0x40 */
  48. uint32_t unk2; /* 0x50: 0x00000000 */
  49. uint8_t md5sum2[MD5SUM_LEN]; /* 0x54 */
  50. uint32_t unk3; /* 0x64: 0xffffffff */
  51. uint32_t kernel_la; /* 0x68: kernel load address */
  52. uint32_t kernel_ep; /* 0x6c: kernel entry point */
  53. uint32_t fw_length; /* 0x70: total length of the image */
  54. uint32_t kernel_ofs; /* 0x74: kernel data offset */
  55. uint32_t kernel_len; /* 0x78: kernel data length */
  56. uint32_t rootfs_ofs; /* 0x7c: rootfs data offset */
  57. uint32_t rootfs_len; /* 0x80: rootfs data length */
  58. uint32_t boot_ofs; /* 0x84: FIXME: seems to be unused */
  59. uint32_t boot_len; /* 0x88: FIXME: seems to be unused */
  60. uint16_t unk4; /* 0x8c: 0x55aa */
  61. uint8_t sver_hi; /* 0x8e */
  62. uint8_t sver_lo; /* 0x8f */
  63. uint8_t unk5; /* 0x90: magic: 0xa5 */
  64. uint8_t ver_hi; /* 0x91 */
  65. uint8_t ver_mid; /* 0x92 */
  66. uint8_t ver_lo; /* 0x93 */
  67. uint8_t pad[364];
  68. } __attribute__ ((packed));
  69. struct tplink_fw_header {
  70. uint32_t version;
  71. union {
  72. struct fw_v1 v1;
  73. struct fw_v2 v2;
  74. };
  75. };
  76. static int mtdsplit_parse_tplink(struct mtd_info *master,
  77. struct mtd_partition **pparts,
  78. struct mtd_part_parser_data *data)
  79. {
  80. struct tplink_fw_header hdr;
  81. size_t hdr_len, retlen, kernel_size;
  82. size_t rootfs_offset;
  83. struct mtd_partition *parts;
  84. int err;
  85. hdr_len = sizeof(hdr);
  86. err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
  87. if (err)
  88. return err;
  89. if (retlen != hdr_len)
  90. return -EIO;
  91. switch (le32_to_cpu(hdr.version)) {
  92. case 1:
  93. if (be32_to_cpu(hdr.v1.kernel_ofs) != sizeof(hdr))
  94. return -EINVAL;
  95. kernel_size = sizeof(hdr) + be32_to_cpu(hdr.v1.kernel_len);
  96. break;
  97. case 2:
  98. case 3:
  99. if (be32_to_cpu(hdr.v2.kernel_ofs) != sizeof(hdr))
  100. return -EINVAL;
  101. kernel_size = sizeof(hdr) + be32_to_cpu(hdr.v2.kernel_len);
  102. break;
  103. default:
  104. return -EINVAL;
  105. }
  106. if (kernel_size > master->size)
  107. return -EINVAL;
  108. /* Find the rootfs after the kernel. */
  109. err = mtd_check_rootfs_magic(master, kernel_size, NULL);
  110. if (!err) {
  111. rootfs_offset = kernel_size;
  112. } else {
  113. /*
  114. * The size in the header might cover the rootfs as well.
  115. * Start the search from an arbitrary offset.
  116. */
  117. err = mtd_find_rootfs_from(master, TPLINK_MIN_ROOTFS_OFFS,
  118. master->size, &rootfs_offset, NULL);
  119. if (err)
  120. return err;
  121. }
  122. parts = kzalloc(TPLINK_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  123. if (!parts)
  124. return -ENOMEM;
  125. parts[0].name = KERNEL_PART_NAME;
  126. parts[0].offset = 0;
  127. parts[0].size = rootfs_offset;
  128. parts[1].name = ROOTFS_PART_NAME;
  129. parts[1].offset = rootfs_offset;
  130. parts[1].size = master->size - rootfs_offset;
  131. *pparts = parts;
  132. return TPLINK_NR_PARTS;
  133. }
  134. static struct mtd_part_parser mtdsplit_tplink_parser = {
  135. .owner = THIS_MODULE,
  136. .name = "tplink-fw",
  137. .parse_fn = mtdsplit_parse_tplink,
  138. .type = MTD_PARSER_TYPE_FIRMWARE,
  139. };
  140. static int __init mtdsplit_tplink_init(void)
  141. {
  142. register_mtd_parser(&mtdsplit_tplink_parser);
  143. return 0;
  144. }
  145. subsys_initcall(mtdsplit_tplink_init);