mtdsplit_trx.c 3.1 KB

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