1
0

tplinkpart.c 5.2 KB

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