1
0

tplinkpart.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published
  6. * by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/magic.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/partitions.h>
  16. #include <linux/version.h>
  17. #define TPLINK_NUM_PARTS 5
  18. #define TPLINK_HEADER_V1 0x01000000
  19. #define TPLINK_HEADER_V2 0x02000000
  20. #define MD5SUM_LEN 16
  21. #define TPLINK_ART_LEN 0x10000
  22. #define TPLINK_KERNEL_OFFS 0x20000
  23. #define TPLINK_64K_KERNEL_OFFS 0x10000
  24. struct tplink_fw_header {
  25. uint32_t version; /* header version */
  26. char vendor_name[24];
  27. char fw_version[36];
  28. uint32_t hw_id; /* hardware id */
  29. uint32_t hw_rev; /* hardware revision */
  30. uint32_t unk1;
  31. uint8_t md5sum1[MD5SUM_LEN];
  32. uint32_t unk2;
  33. uint8_t md5sum2[MD5SUM_LEN];
  34. uint32_t unk3;
  35. uint32_t kernel_la; /* kernel load address */
  36. uint32_t kernel_ep; /* kernel entry point */
  37. uint32_t fw_length; /* total length of the firmware */
  38. uint32_t kernel_ofs; /* kernel data offset */
  39. uint32_t kernel_len; /* kernel data length */
  40. uint32_t rootfs_ofs; /* rootfs data offset */
  41. uint32_t rootfs_len; /* rootfs data length */
  42. uint32_t boot_ofs; /* bootloader data offset */
  43. uint32_t boot_len; /* bootloader data length */
  44. uint8_t pad[360];
  45. } __attribute__ ((packed));
  46. static struct tplink_fw_header *
  47. tplink_read_header(struct mtd_info *mtd, size_t offset)
  48. {
  49. struct tplink_fw_header *header;
  50. size_t header_len;
  51. size_t retlen;
  52. int ret;
  53. u32 t;
  54. header = vmalloc(sizeof(*header));
  55. if (!header)
  56. goto err;
  57. header_len = sizeof(struct tplink_fw_header);
  58. ret = mtd_read(mtd, offset, header_len, &retlen,
  59. (unsigned char *) header);
  60. if (ret)
  61. goto err_free_header;
  62. if (retlen != header_len)
  63. goto err_free_header;
  64. /* sanity checks */
  65. t = be32_to_cpu(header->version);
  66. if ((t != TPLINK_HEADER_V1) && (t != TPLINK_HEADER_V2))
  67. goto err_free_header;
  68. t = be32_to_cpu(header->kernel_ofs);
  69. if (t != header_len)
  70. goto err_free_header;
  71. return header;
  72. err_free_header:
  73. vfree(header);
  74. err:
  75. return NULL;
  76. }
  77. static int tplink_check_rootfs_magic(struct mtd_info *mtd, size_t offset)
  78. {
  79. u32 magic;
  80. size_t retlen;
  81. int ret;
  82. ret = mtd_read(mtd, offset, sizeof(magic), &retlen,
  83. (unsigned char *) &magic);
  84. if (ret)
  85. return ret;
  86. if (retlen != sizeof(magic))
  87. return -EIO;
  88. if (le32_to_cpu(magic) != SQUASHFS_MAGIC &&
  89. magic != 0x19852003)
  90. return -EINVAL;
  91. return 0;
  92. }
  93. static int tplink_parse_partitions_offset(struct mtd_info *master,
  94. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
  95. struct mtd_partition **pparts,
  96. #else
  97. const struct mtd_partition **pparts,
  98. #endif
  99. struct mtd_part_parser_data *data,
  100. size_t offset)
  101. {
  102. struct mtd_partition *parts;
  103. struct tplink_fw_header *header;
  104. int nr_parts;
  105. size_t art_offset;
  106. size_t rootfs_offset;
  107. size_t squashfs_offset;
  108. int ret;
  109. nr_parts = TPLINK_NUM_PARTS;
  110. parts = kzalloc(nr_parts * sizeof(struct mtd_partition), GFP_KERNEL);
  111. if (!parts) {
  112. ret = -ENOMEM;
  113. goto err;
  114. }
  115. header = tplink_read_header(master, offset);
  116. if (!header) {
  117. pr_notice("%s: no TP-Link header found\n", master->name);
  118. ret = -ENODEV;
  119. goto err_free_parts;
  120. }
  121. squashfs_offset = offset + sizeof(struct tplink_fw_header) +
  122. be32_to_cpu(header->kernel_len);
  123. ret = tplink_check_rootfs_magic(master, squashfs_offset);
  124. if (ret == 0)
  125. rootfs_offset = squashfs_offset;
  126. else
  127. rootfs_offset = offset + be32_to_cpu(header->rootfs_ofs);
  128. art_offset = master->size - TPLINK_ART_LEN;
  129. parts[0].name = "u-boot";
  130. parts[0].offset = 0;
  131. parts[0].size = offset;
  132. parts[0].mask_flags = MTD_WRITEABLE;
  133. parts[1].name = "kernel";
  134. parts[1].offset = offset;
  135. parts[1].size = rootfs_offset - offset;
  136. parts[2].name = "rootfs";
  137. parts[2].offset = rootfs_offset;
  138. parts[2].size = art_offset - rootfs_offset;
  139. parts[3].name = "art";
  140. parts[3].offset = art_offset;
  141. parts[3].size = TPLINK_ART_LEN;
  142. parts[3].mask_flags = MTD_WRITEABLE;
  143. parts[4].name = "firmware";
  144. parts[4].offset = offset;
  145. parts[4].size = art_offset - offset;
  146. vfree(header);
  147. *pparts = parts;
  148. return nr_parts;
  149. err_free_parts:
  150. kfree(parts);
  151. err:
  152. *pparts = NULL;
  153. return ret;
  154. }
  155. static int tplink_parse_partitions(struct mtd_info *master,
  156. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
  157. struct mtd_partition **pparts,
  158. #else
  159. const struct mtd_partition **pparts,
  160. #endif
  161. struct mtd_part_parser_data *data)
  162. {
  163. return tplink_parse_partitions_offset(master, pparts, data,
  164. TPLINK_KERNEL_OFFS);
  165. }
  166. static int tplink_parse_64k_partitions(struct mtd_info *master,
  167. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
  168. struct mtd_partition **pparts,
  169. #else
  170. const struct mtd_partition **pparts,
  171. #endif
  172. struct mtd_part_parser_data *data)
  173. {
  174. return tplink_parse_partitions_offset(master, pparts, data,
  175. TPLINK_64K_KERNEL_OFFS);
  176. }
  177. static struct mtd_part_parser tplink_parser = {
  178. .owner = THIS_MODULE,
  179. .parse_fn = tplink_parse_partitions,
  180. .name = "tp-link",
  181. };
  182. static struct mtd_part_parser tplink_64k_parser = {
  183. .owner = THIS_MODULE,
  184. .parse_fn = tplink_parse_64k_partitions,
  185. .name = "tp-link-64k",
  186. };
  187. static int __init tplink_parser_init(void)
  188. {
  189. register_mtd_parser(&tplink_parser);
  190. register_mtd_parser(&tplink_64k_parser);
  191. return 0;
  192. }
  193. module_init(tplink_parser_init);
  194. MODULE_LICENSE("GPL v2");
  195. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");