2 * MTD splitter for MikroTik NOR devices
4 * Copyright (C) 2017 Thibaut VARENE <varenet@parisc-linux.org>
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.
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).
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/partitions.h>
29 #include <linux/string.h>
33 #define YAFFS_OBJECT_TYPE_FILE 0x1
34 #define YAFFS_OBJECTID_ROOT 0x1
35 #define YAFFS_SUM_UNUSED 0xFFFF
36 #define YAFFS_NAME "kernel"
38 #define MINOR_NR_PARTS 2
41 * This structure is based on yaffs_obj_hdr from yaffs_guts.h
42 * The weak types match upstream. The fields have cpu-endianness
48 char yaffs_name
[sizeof(YAFFS_NAME
)];
51 static int mtdsplit_parse_minor(struct mtd_info
*master
,
52 const struct mtd_partition
**pparts
,
53 struct mtd_part_parser_data
*data
)
55 struct minor_header hdr
;
56 size_t hdr_len
, retlen
;
58 struct mtd_partition
*parts
;
61 hdr_len
= sizeof(hdr
);
62 err
= mtd_read(master
, 0, hdr_len
, &retlen
, (void *) &hdr
);
66 if (retlen
!= hdr_len
)
70 if (hdr
.yaffs_type
!= YAFFS_OBJECT_TYPE_FILE
)
73 if (hdr
.yaffs_obj_id
!= YAFFS_OBJECTID_ROOT
)
76 if (hdr
.yaffs_sum_unused
!= YAFFS_SUM_UNUSED
)
79 if (memcmp(hdr
.yaffs_name
, YAFFS_NAME
, sizeof(YAFFS_NAME
)))
82 err
= mtd_find_rootfs_from(master
, master
->erasesize
, master
->size
,
83 &rootfs_offset
, NULL
);
87 parts
= kzalloc(MINOR_NR_PARTS
* sizeof(*parts
), GFP_KERNEL
);
91 parts
[0].name
= KERNEL_PART_NAME
;
93 parts
[0].size
= rootfs_offset
;
95 parts
[1].name
= ROOTFS_PART_NAME
;
96 parts
[1].offset
= rootfs_offset
;
97 parts
[1].size
= master
->size
- rootfs_offset
;
100 return MINOR_NR_PARTS
;
103 static struct mtd_part_parser mtdsplit_minor_parser
= {
104 .owner
= THIS_MODULE
,
106 .parse_fn
= mtdsplit_parse_minor
,
107 .type
= MTD_PARSER_TYPE_FIRMWARE
,
110 static int __init
mtdsplit_minor_init(void)
112 register_mtd_parser(&mtdsplit_minor_parser
);
117 subsys_initcall(mtdsplit_minor_init
);