mtdsplit_minor.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_MAX_NAME_LENGTH 127
  35. #define YAFFS_NAME_KERNEL "kernel"
  36. #define YAFFS_NAME_BOOTIMAGE "bootimage"
  37. #define MINOR_NR_PARTS 2
  38. /*
  39. * This structure is based on yaffs_obj_hdr from yaffs_guts.h
  40. * The weak types match upstream. The fields have cpu-endianness
  41. */
  42. struct minor_header {
  43. int yaffs_type;
  44. int yaffs_obj_id;
  45. u16 yaffs_sum_unused;
  46. char yaffs_name[YAFFS_MAX_NAME_LENGTH];
  47. };
  48. static int mtdsplit_parse_minor(struct mtd_info *master,
  49. const struct mtd_partition **pparts,
  50. struct mtd_part_parser_data *data)
  51. {
  52. struct minor_header hdr;
  53. size_t hdr_len, retlen;
  54. size_t rootfs_offset;
  55. struct mtd_partition *parts;
  56. int err;
  57. hdr_len = sizeof(hdr);
  58. err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
  59. if (err)
  60. return err;
  61. if (retlen != hdr_len)
  62. return -EIO;
  63. /* match header */
  64. if (hdr.yaffs_type != YAFFS_OBJECT_TYPE_FILE)
  65. return -ENOENT;
  66. if (hdr.yaffs_obj_id != YAFFS_OBJECTID_ROOT)
  67. return -ENOENT;
  68. if (hdr.yaffs_sum_unused != YAFFS_SUM_UNUSED)
  69. return -ENOENT;
  70. if ((memcmp(hdr.yaffs_name, YAFFS_NAME_KERNEL, sizeof(YAFFS_NAME_KERNEL))) &&
  71. (memcmp(hdr.yaffs_name, YAFFS_NAME_BOOTIMAGE, sizeof(YAFFS_NAME_BOOTIMAGE))))
  72. return -ENOENT;
  73. err = mtd_find_rootfs_from(master, master->erasesize, master->size,
  74. &rootfs_offset, NULL);
  75. if (err)
  76. return err;
  77. parts = kzalloc(MINOR_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  78. if (!parts)
  79. return -ENOMEM;
  80. parts[0].name = KERNEL_PART_NAME;
  81. parts[0].offset = 0;
  82. parts[0].size = rootfs_offset;
  83. parts[1].name = ROOTFS_PART_NAME;
  84. parts[1].offset = rootfs_offset;
  85. parts[1].size = master->size - rootfs_offset;
  86. *pparts = parts;
  87. return MINOR_NR_PARTS;
  88. }
  89. static const struct of_device_id mtdsplit_minor_of_match_table[] = {
  90. { .compatible = "mikrotik,minor" },
  91. {},
  92. };
  93. MODULE_DEVICE_TABLE(of, mtdsplit_minor_of_match_table);
  94. static struct mtd_part_parser mtdsplit_minor_parser = {
  95. .owner = THIS_MODULE,
  96. .name = "minor-fw",
  97. .of_match_table = mtdsplit_minor_of_match_table,
  98. .parse_fn = mtdsplit_parse_minor,
  99. .type = MTD_PARSER_TYPE_FIRMWARE,
  100. };
  101. static int __init mtdsplit_minor_init(void)
  102. {
  103. register_mtd_parser(&mtdsplit_minor_parser);
  104. return 0;
  105. }
  106. subsys_initcall(mtdsplit_minor_init);