tplinkpart.c 4.4 KB

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