mtdsplit_fit.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2015 The Linux Foundation
  3. * Copyright (C) 2014 Gabor Juhos <juhosg@openwrt.org>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/partitions.h>
  20. #include <linux/types.h>
  21. #include <linux/byteorder/generic.h>
  22. #include <linux/slab.h>
  23. #include <linux/of_fdt.h>
  24. #include "mtdsplit.h"
  25. struct fdt_header {
  26. uint32_t magic; /* magic word FDT_MAGIC */
  27. uint32_t totalsize; /* total size of DT block */
  28. uint32_t off_dt_struct; /* offset to structure */
  29. uint32_t off_dt_strings; /* offset to strings */
  30. uint32_t off_mem_rsvmap; /* offset to memory reserve map */
  31. uint32_t version; /* format version */
  32. uint32_t last_comp_version; /* last compatible version */
  33. /* version 2 fields below */
  34. uint32_t boot_cpuid_phys; /* Which physical CPU id we're
  35. booting on */
  36. /* version 3 fields below */
  37. uint32_t size_dt_strings; /* size of the strings block */
  38. /* version 17 fields below */
  39. uint32_t size_dt_struct; /* size of the structure block */
  40. };
  41. static int
  42. mtdsplit_fit_parse(struct mtd_info *mtd, struct mtd_partition **pparts,
  43. struct mtd_part_parser_data *data)
  44. {
  45. struct fdt_header hdr;
  46. size_t hdr_len, retlen;
  47. size_t offset;
  48. size_t fit_offset, fit_size;
  49. size_t rootfs_offset, rootfs_size;
  50. struct mtd_partition *parts;
  51. int ret;
  52. hdr_len = sizeof(struct fdt_header);
  53. /* Parse the MTD device & search for the FIT image location */
  54. for(offset = 0; offset < mtd->size; offset += mtd->erasesize) {
  55. ret = mtd_read(mtd, 0, hdr_len, &retlen, (void*) &hdr);
  56. if (ret) {
  57. pr_err("read error in \"%s\" at offset 0x%llx\n",
  58. mtd->name, (unsigned long long) offset);
  59. return ret;
  60. }
  61. if (retlen != hdr_len) {
  62. pr_err("short read in \"%s\"\n", mtd->name);
  63. return -EIO;
  64. }
  65. /* Check the magic - see if this is a FIT image */
  66. if (be32_to_cpu(hdr.magic) != OF_DT_HEADER) {
  67. pr_debug("no valid FIT image found in \"%s\" at offset %llx\n",
  68. mtd->name, (unsigned long long) offset);
  69. continue;
  70. }
  71. /* We found a FIT image. Let's keep going */
  72. break;
  73. }
  74. fit_offset = offset;
  75. fit_size = be32_to_cpu(hdr.totalsize);
  76. if (fit_size == 0) {
  77. pr_err("FIT image in \"%s\" at offset %llx has null size\n",
  78. mtd->name, (unsigned long long) fit_offset);
  79. return -ENODEV;
  80. }
  81. /* Search for the rootfs partition after the FIT image */
  82. ret = mtd_find_rootfs_from(mtd, fit_offset + fit_size, mtd->size,
  83. &rootfs_offset, NULL);
  84. if (ret) {
  85. pr_info("no rootfs found after FIT image in \"%s\"\n",
  86. mtd->name);
  87. return ret;
  88. }
  89. rootfs_size = mtd->size - rootfs_offset;
  90. parts = kzalloc(2 * sizeof(*parts), GFP_KERNEL);
  91. if (!parts)
  92. return -ENOMEM;
  93. parts[0].name = KERNEL_PART_NAME;
  94. parts[0].offset = fit_offset;
  95. parts[0].size = mtd_rounddown_to_eb(fit_size, mtd) + mtd->erasesize;
  96. parts[1].name = ROOTFS_PART_NAME;
  97. parts[1].offset = rootfs_offset;
  98. parts[1].size = rootfs_size;
  99. *pparts = parts;
  100. return 2;
  101. }
  102. static struct mtd_part_parser uimage_parser = {
  103. .owner = THIS_MODULE,
  104. .name = "fit-fw",
  105. .parse_fn = mtdsplit_fit_parse,
  106. .type = MTD_PARSER_TYPE_FIRMWARE,
  107. };
  108. /**************************************************
  109. * Init
  110. **************************************************/
  111. static int __init mtdsplit_fit_init(void)
  112. {
  113. register_mtd_parser(&uimage_parser);
  114. return 0;
  115. }
  116. module_init(mtdsplit_fit_init);