mtdsplit_fit.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2015 The Linux Foundation
  3. * Copyright (C) 2014 Gabor Juhos <[email protected]>
  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,
  43. const struct mtd_partition **pparts,
  44. struct mtd_part_parser_data *data)
  45. {
  46. struct fdt_header hdr;
  47. size_t hdr_len, retlen;
  48. size_t offset;
  49. size_t fit_offset, fit_size;
  50. size_t rootfs_offset, rootfs_size;
  51. struct mtd_partition *parts;
  52. int ret;
  53. hdr_len = sizeof(struct fdt_header);
  54. /* Parse the MTD device & search for the FIT image location */
  55. for(offset = 0; offset < mtd->size; offset += mtd->erasesize) {
  56. ret = mtd_read(mtd, 0, hdr_len, &retlen, (void*) &hdr);
  57. if (ret) {
  58. pr_err("read error in \"%s\" at offset 0x%llx\n",
  59. mtd->name, (unsigned long long) offset);
  60. return ret;
  61. }
  62. if (retlen != hdr_len) {
  63. pr_err("short read in \"%s\"\n", mtd->name);
  64. return -EIO;
  65. }
  66. /* Check the magic - see if this is a FIT image */
  67. if (be32_to_cpu(hdr.magic) != OF_DT_HEADER) {
  68. pr_debug("no valid FIT image found in \"%s\" at offset %llx\n",
  69. mtd->name, (unsigned long long) offset);
  70. continue;
  71. }
  72. /* We found a FIT image. Let's keep going */
  73. break;
  74. }
  75. fit_offset = offset;
  76. fit_size = be32_to_cpu(hdr.totalsize);
  77. if (fit_size == 0) {
  78. pr_err("FIT image in \"%s\" at offset %llx has null size\n",
  79. mtd->name, (unsigned long long) fit_offset);
  80. return -ENODEV;
  81. }
  82. /* Search for the rootfs partition after the FIT image */
  83. ret = mtd_find_rootfs_from(mtd, fit_offset + fit_size, mtd->size,
  84. &rootfs_offset, NULL);
  85. if (ret) {
  86. pr_info("no rootfs found after FIT image in \"%s\"\n",
  87. mtd->name);
  88. return ret;
  89. }
  90. rootfs_size = mtd->size - rootfs_offset;
  91. parts = kzalloc(2 * sizeof(*parts), GFP_KERNEL);
  92. if (!parts)
  93. return -ENOMEM;
  94. parts[0].name = KERNEL_PART_NAME;
  95. parts[0].offset = fit_offset;
  96. parts[0].size = mtd_rounddown_to_eb(fit_size, mtd) + mtd->erasesize;
  97. parts[1].name = ROOTFS_PART_NAME;
  98. parts[1].offset = rootfs_offset;
  99. parts[1].size = rootfs_size;
  100. *pparts = parts;
  101. return 2;
  102. }
  103. static const struct of_device_id mtdsplit_fit_of_match_table[] = {
  104. { .compatible = "denx,fit" },
  105. {},
  106. };
  107. static struct mtd_part_parser uimage_parser = {
  108. .owner = THIS_MODULE,
  109. .name = "fit-fw",
  110. .of_match_table = mtdsplit_fit_of_match_table,
  111. .parse_fn = mtdsplit_fit_parse,
  112. .type = MTD_PARSER_TYPE_FIRMWARE,
  113. };
  114. /**************************************************
  115. * Init
  116. **************************************************/
  117. static int __init mtdsplit_fit_init(void)
  118. {
  119. register_mtd_parser(&uimage_parser);
  120. return 0;
  121. }
  122. module_init(mtdsplit_fit_init);