mtdsplit_trx.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2013 Gabor Juhos <[email protected]>
  3. * Copyright (C) 2014 Felix Fietkau <[email protected]>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/partitions.h>
  17. #include <linux/byteorder/generic.h>
  18. #include <linux/of.h>
  19. #include "mtdsplit.h"
  20. #define TRX_MAGIC 0x30524448 /* "HDR0" */
  21. struct trx_header {
  22. __le32 magic;
  23. __le32 len;
  24. __le32 crc32;
  25. __le32 flag_version;
  26. __le32 offset[4];
  27. };
  28. static int
  29. read_trx_header(struct mtd_info *mtd, size_t offset,
  30. struct trx_header *header)
  31. {
  32. size_t header_len;
  33. size_t retlen;
  34. int ret;
  35. header_len = sizeof(*header);
  36. ret = mtd_read(mtd, offset, header_len, &retlen,
  37. (unsigned char *) header);
  38. if (ret) {
  39. pr_debug("read error in \"%s\"\n", mtd->name);
  40. return ret;
  41. }
  42. if (retlen != header_len) {
  43. pr_debug("short read in \"%s\"\n", mtd->name);
  44. return -EIO;
  45. }
  46. return 0;
  47. }
  48. static int
  49. mtdsplit_parse_trx(struct mtd_info *master,
  50. const struct mtd_partition **pparts,
  51. struct mtd_part_parser_data *data)
  52. {
  53. struct mtd_partition *parts;
  54. struct trx_header hdr;
  55. int nr_parts;
  56. size_t offset;
  57. size_t trx_offset;
  58. size_t trx_size = 0;
  59. size_t rootfs_offset;
  60. size_t rootfs_size = 0;
  61. int ret;
  62. nr_parts = 2;
  63. parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  64. if (!parts)
  65. return -ENOMEM;
  66. /* find trx image on erase block boundaries */
  67. for (offset = 0; offset < master->size; offset += master->erasesize) {
  68. trx_size = 0;
  69. ret = read_trx_header(master, offset, &hdr);
  70. if (ret)
  71. continue;
  72. if (hdr.magic != cpu_to_le32(TRX_MAGIC)) {
  73. pr_debug("no valid trx header found in \"%s\" at offset %llx\n",
  74. master->name, (unsigned long long) offset);
  75. continue;
  76. }
  77. trx_size = le32_to_cpu(hdr.len);
  78. if ((offset + trx_size) > master->size) {
  79. pr_debug("trx image exceeds MTD device \"%s\"\n",
  80. master->name);
  81. continue;
  82. }
  83. break;
  84. }
  85. if (trx_size == 0) {
  86. pr_debug("no trx header found in \"%s\"\n", master->name);
  87. ret = -ENODEV;
  88. goto err;
  89. }
  90. trx_offset = offset + hdr.offset[0];
  91. rootfs_offset = offset + hdr.offset[1];
  92. rootfs_size = master->size - rootfs_offset;
  93. trx_size = rootfs_offset - trx_offset;
  94. if (rootfs_size == 0) {
  95. pr_debug("no rootfs found in \"%s\"\n", master->name);
  96. ret = -ENODEV;
  97. goto err;
  98. }
  99. parts[0].name = KERNEL_PART_NAME;
  100. parts[0].offset = trx_offset;
  101. parts[0].size = trx_size;
  102. parts[1].name = ROOTFS_PART_NAME;
  103. parts[1].offset = rootfs_offset;
  104. parts[1].size = rootfs_size;
  105. *pparts = parts;
  106. return nr_parts;
  107. err:
  108. kfree(parts);
  109. return ret;
  110. }
  111. static const struct of_device_id trx_parser_of_match_table[] = {
  112. { .compatible = "openwrt,trx" },
  113. {},
  114. };
  115. MODULE_DEVICE_TABLE(of, trx_parser_of_match_table);
  116. static struct mtd_part_parser trx_parser = {
  117. .owner = THIS_MODULE,
  118. .name = "trx-fw",
  119. .of_match_table = trx_parser_of_match_table,
  120. .parse_fn = mtdsplit_parse_trx,
  121. .type = MTD_PARSER_TYPE_FIRMWARE,
  122. };
  123. static int __init mtdsplit_trx_init(void)
  124. {
  125. register_mtd_parser(&trx_parser);
  126. return 0;
  127. }
  128. module_init(mtdsplit_trx_init);