mtdsplit_minor.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * MTD splitter for MikroTik NOR devices
  3. *
  4. * Copyright (C) 2017 Thibaut VARENE <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * The rootfs is expected at erase-block boundary due to the use of
  11. * mtd_find_rootfs_from(). We use a trimmed down version of the yaffs header
  12. * for two main reasons:
  13. * - the original header uses weakly defined types (int, enum...) which can
  14. * vary in length depending on build host (and the struct is not packed),
  15. * and the name field can have a different total length depending on
  16. * whether or not the yaffs code was _built_ with unicode support.
  17. * - the only field that could be of real use here (file_size_low) contains
  18. * invalid data in the header generated by kernel2minor, so we cannot use
  19. * it to infer the exact position of the rootfs and do away with
  20. * mtd_find_rootfs_from() (and thus have non-EB-aligned rootfs).
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/partitions.h>
  28. #include <linux/string.h>
  29. #include <linux/of.h>
  30. #include "mtdsplit.h"
  31. #define YAFFS_OBJECT_TYPE_FILE 0x1
  32. #define YAFFS_OBJECTID_ROOT 0x1
  33. #define YAFFS_SUM_UNUSED 0xFFFF
  34. #define YAFFS_NAME "kernel"
  35. #define MINOR_NR_PARTS 2
  36. /*
  37. * This structure is based on yaffs_obj_hdr from yaffs_guts.h
  38. * The weak types match upstream. The fields have cpu-endianness
  39. */
  40. struct minor_header {
  41. int yaffs_type;
  42. int yaffs_obj_id;
  43. u16 yaffs_sum_unused;
  44. char yaffs_name[sizeof(YAFFS_NAME)];
  45. };
  46. static int mtdsplit_parse_minor(struct mtd_info *master,
  47. const struct mtd_partition **pparts,
  48. struct mtd_part_parser_data *data)
  49. {
  50. struct minor_header hdr;
  51. size_t hdr_len, retlen;
  52. size_t rootfs_offset;
  53. struct mtd_partition *parts;
  54. int err;
  55. hdr_len = sizeof(hdr);
  56. err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
  57. if (err)
  58. return err;
  59. if (retlen != hdr_len)
  60. return -EIO;
  61. /* match header */
  62. if (hdr.yaffs_type != YAFFS_OBJECT_TYPE_FILE)
  63. return -EINVAL;
  64. if (hdr.yaffs_obj_id != YAFFS_OBJECTID_ROOT)
  65. return -EINVAL;
  66. if (hdr.yaffs_sum_unused != YAFFS_SUM_UNUSED)
  67. return -EINVAL;
  68. if (memcmp(hdr.yaffs_name, YAFFS_NAME, sizeof(YAFFS_NAME)))
  69. return -EINVAL;
  70. err = mtd_find_rootfs_from(master, master->erasesize, master->size,
  71. &rootfs_offset, NULL);
  72. if (err)
  73. return err;
  74. parts = kzalloc(MINOR_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  75. if (!parts)
  76. return -ENOMEM;
  77. parts[0].name = KERNEL_PART_NAME;
  78. parts[0].offset = 0;
  79. parts[0].size = rootfs_offset;
  80. parts[1].name = ROOTFS_PART_NAME;
  81. parts[1].offset = rootfs_offset;
  82. parts[1].size = master->size - rootfs_offset;
  83. *pparts = parts;
  84. return MINOR_NR_PARTS;
  85. }
  86. static const struct of_device_id mtdsplit_minor_of_match_table[] = {
  87. { .compatible = "mikrotik,minor" },
  88. {},
  89. };
  90. MODULE_DEVICE_TABLE(of, mtdsplit_minor_of_match_table);
  91. static struct mtd_part_parser mtdsplit_minor_parser = {
  92. .owner = THIS_MODULE,
  93. .name = "minor-fw",
  94. .of_match_table = mtdsplit_minor_of_match_table,
  95. .parse_fn = mtdsplit_parse_minor,
  96. .type = MTD_PARSER_TYPE_FIRMWARE,
  97. };
  98. static int __init mtdsplit_minor_init(void)
  99. {
  100. register_mtd_parser(&mtdsplit_minor_parser);
  101. return 0;
  102. }
  103. subsys_initcall(mtdsplit_minor_init);