mtdsplit_minor.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. pr_err("MiNOR mtd_read error: %d\n", err);
  61. return err;
  62. }
  63. if (retlen != hdr_len) {
  64. pr_err("MiNOR mtd_read too short\n");
  65. return -EIO;
  66. }
  67. /* match header */
  68. if (hdr.yaffs_type != YAFFS_OBJECT_TYPE_FILE) {
  69. pr_info("MiNOR YAFFS first type not matched\n");
  70. return 0;
  71. }
  72. if (hdr.yaffs_obj_id != YAFFS_OBJECTID_ROOT) {
  73. pr_info("MiNOR YAFFS first objectid not matched\n");
  74. return 0;
  75. }
  76. if (hdr.yaffs_sum_unused != YAFFS_SUM_UNUSED) {
  77. pr_info("MiNOR YAFFS first sum not matched\n");
  78. return 0;
  79. }
  80. if ((memcmp(hdr.yaffs_name, YAFFS_NAME_KERNEL, sizeof(YAFFS_NAME_KERNEL))) &&
  81. (memcmp(hdr.yaffs_name, YAFFS_NAME_BOOTIMAGE, sizeof(YAFFS_NAME_BOOTIMAGE)))) {
  82. pr_info("MiNOR YAFFS first name not matched\n");
  83. return 0;
  84. }
  85. err = mtd_find_rootfs_from(master, master->erasesize, master->size,
  86. &rootfs_offset, NULL);
  87. if (err) {
  88. pr_info("MiNOR mtd_find_rootfs_from error: %d\n", err);
  89. return 0;
  90. }
  91. parts = kzalloc(MINOR_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  92. if (!parts)
  93. return -ENOMEM;
  94. parts[0].name = KERNEL_PART_NAME;
  95. parts[0].offset = 0;
  96. parts[0].size = rootfs_offset;
  97. parts[1].name = ROOTFS_PART_NAME;
  98. parts[1].offset = rootfs_offset;
  99. parts[1].size = master->size - rootfs_offset;
  100. *pparts = parts;
  101. return MINOR_NR_PARTS;
  102. }
  103. static const struct of_device_id mtdsplit_minor_of_match_table[] = {
  104. { .compatible = "mikrotik,minor" },
  105. {},
  106. };
  107. MODULE_DEVICE_TABLE(of, mtdsplit_minor_of_match_table);
  108. static struct mtd_part_parser mtdsplit_minor_parser = {
  109. .owner = THIS_MODULE,
  110. .name = "minor-fw",
  111. .of_match_table = mtdsplit_minor_of_match_table,
  112. .parse_fn = mtdsplit_parse_minor,
  113. .type = MTD_PARSER_TYPE_FIRMWARE,
  114. };
  115. static int __init mtdsplit_minor_init(void)
  116. {
  117. register_mtd_parser(&mtdsplit_minor_parser);
  118. return 0;
  119. }
  120. subsys_initcall(mtdsplit_minor_init);