mtdsplit_minor.c 3.1 KB

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