0027-tools-firmware-utils-tplink-safeloader-fix-support-list-format-clean-up-vendor-information.patch 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. From: Matthias Schiffer <mschiffer@universe-factory.net>
  2. Date: Thu, 15 Oct 2015 21:01:25 +0200
  3. Subject: tools/firmware-utils: tplink-safeloader: fix support-list format, clean up vendor information
  4. The first 4 bytes of the support list and the vendor information are
  5. supposed to contain the length of these fields.
  6. Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
  7. diff --git a/tools/firmware-utils/src/tplink-safeloader.c b/tools/firmware-utils/src/tplink-safeloader.c
  8. index 4607a1d..66658aa 100644
  9. --- a/tools/firmware-utils/src/tplink-safeloader.c
  10. +++ b/tools/firmware-utils/src/tplink-safeloader.c
  11. @@ -103,7 +103,7 @@ static const uint8_t md5_salt[16] = {
  12. /** Vendor information for CPE210/220/510/520 */
  13. -static const unsigned char cpe510_vendor[] = "\x00\x00\x00\x1f""CPE510(TP-LINK|UN|N300-5):1.0\r\n";
  14. +static const char cpe510_vendor[] = "CPE510(TP-LINK|UN|N300-5):1.0\r\n";
  15. /**
  16. @@ -133,14 +133,13 @@ static const struct flash_partition_entry cpe510_partitions[] = {
  17. The stock images also contain strings for two more devices: BS510 and BS210.
  18. At the moment, there exists no public information about these devices.
  19. */
  20. -static const unsigned char cpe510_support_list[] =
  21. - "\x00\x00\x00\xc8\x00\x00\x00\x00"
  22. +static const char cpe510_support_list[] =
  23. "SupportList:\r\n"
  24. "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
  25. "CPE520(TP-LINK|UN|N300-5):1.0\r\n"
  26. "CPE210(TP-LINK|UN|N300-2):1.0\r\n"
  27. "CPE220(TP-LINK|UN|N300-2):1.0\r\n"
  28. - "\r\n\xff";
  29. + "\r\n";
  30. #define error(_ret, _errno, _str, ...) \
  31. do { \
  32. @@ -151,6 +150,14 @@ static const unsigned char cpe510_support_list[] =
  33. } while (0)
  34. +/** Stores a uint32 as big endian */
  35. +static inline void put32(uint8_t *buf, uint32_t val) {
  36. + buf[0] = val >> 24;
  37. + buf[1] = val >> 16;
  38. + buf[2] = val >> 8;
  39. + buf[3] = val;
  40. +}
  41. +
  42. /** Allocates a new image partition */
  43. static struct image_partition_entry alloc_image_partition(const char *name, size_t len) {
  44. struct image_partition_entry entry = {name, len, malloc(len)};
  45. @@ -233,9 +240,15 @@ static struct image_partition_entry make_soft_version(uint32_t rev) {
  46. }
  47. /** Generates the support-list partition */
  48. -static struct image_partition_entry make_support_list(const unsigned char *support_list, size_t len) {
  49. - struct image_partition_entry entry = alloc_image_partition("support-list", len);
  50. - memcpy(entry.data, support_list, len);
  51. +static struct image_partition_entry make_support_list(const char *support_list) {
  52. + size_t len = strlen(support_list);
  53. + struct image_partition_entry entry = alloc_image_partition("support-list", len + 9);
  54. +
  55. + put32(entry.data, len);
  56. + memset(entry.data+4, 0, 4);
  57. + memcpy(entry.data+8, support_list, len);
  58. + entry.data[len+8] = '\xff';
  59. +
  60. return entry;
  61. }
  62. @@ -344,12 +357,13 @@ static void put_md5(uint8_t *md5, uint8_t *buffer, unsigned int len) {
  63. ----------- -----
  64. 0000-0003 Image size (4 bytes, big endian)
  65. 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
  66. - 0014-1013 Vendor information (4096 bytes, padded with 0xff; there seem to be older
  67. + 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
  68. + 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
  69. (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
  70. 1014-1813 Image partition table (2048 bytes, padded with 0xff)
  71. 1814-xxxx Firmware partitions
  72. */
  73. -static void * generate_factory_image(const unsigned char *vendor, size_t vendor_len, const struct image_partition_entry *parts, size_t *len) {
  74. +static void * generate_factory_image(const char *vendor, const struct image_partition_entry *parts, size_t *len) {
  75. *len = 0x1814;
  76. size_t i;
  77. @@ -360,13 +374,12 @@ static void * generate_factory_image(const unsigned char *vendor, size_t vendor_
  78. if (!image)
  79. error(1, errno, "malloc");
  80. - image[0] = *len >> 24;
  81. - image[1] = *len >> 16;
  82. - image[2] = *len >> 8;
  83. - image[3] = *len;
  84. + put32(image, *len);
  85. - memcpy(image+0x14, vendor, vendor_len);
  86. - memset(image+0x14+vendor_len, 0xff, 4096-vendor_len);
  87. + size_t vendor_len = strlen(vendor);
  88. + put32(image+0x14, vendor_len);
  89. + memcpy(image+0x18, vendor, vendor_len);
  90. + memset(image+0x18+vendor_len, 0xff, 4092-vendor_len);
  91. put_partitions(image + 0x1014, parts);
  92. put_md5(image+0x04, image+0x14, *len-0x14);
  93. @@ -430,7 +443,7 @@ static void do_cpe510(const char *output, const char *kernel_image, const char *
  94. parts[0] = make_partition_table(cpe510_partitions);
  95. parts[1] = make_soft_version(rev);
  96. - parts[2] = make_support_list(cpe510_support_list, sizeof(cpe510_support_list)-1);
  97. + parts[2] = make_support_list(cpe510_support_list);
  98. parts[3] = read_file("os-image", kernel_image, false);
  99. parts[4] = read_file("file-system", rootfs_image, add_jffs2_eof);
  100. @@ -439,7 +452,7 @@ static void do_cpe510(const char *output, const char *kernel_image, const char *
  101. if (sysupgrade)
  102. image = generate_sysupgrade_image(cpe510_partitions, parts, &len);
  103. else
  104. - image = generate_factory_image(cpe510_vendor, sizeof(cpe510_vendor)-1, parts, &len);
  105. + image = generate_factory_image(cpe510_vendor, parts, &len);
  106. FILE *file = fopen(output, "wb");
  107. if (!file)