kernel: drop unneeded LINUX_VERSION_CODE checks
[openwrt/openwrt.git] / target / linux / generic / files / drivers / mtd / mtdsplit / mtdsplit_minor.c
1 /*
2 * MTD splitter for MikroTik NOR devices
3 *
4 * Copyright (C) 2017 Thibaut VARENE <varenet@parisc-linux.org>
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
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>
30
31 #include "mtdsplit.h"
32
33 #define YAFFS_OBJECT_TYPE_FILE 0x1
34 #define YAFFS_OBJECTID_ROOT 0x1
35 #define YAFFS_SUM_UNUSED 0xFFFF
36 #define YAFFS_NAME "kernel"
37
38 #define MINOR_NR_PARTS 2
39
40 /*
41 * This structure is based on yaffs_obj_hdr from yaffs_guts.h
42 * The weak types match upstream. The fields have cpu-endianness
43 */
44 struct minor_header {
45 int yaffs_type;
46 int yaffs_obj_id;
47 u16 yaffs_sum_unused;
48 char yaffs_name[sizeof(YAFFS_NAME)];
49 };
50
51 static int mtdsplit_parse_minor(struct mtd_info *master,
52 const struct mtd_partition **pparts,
53 struct mtd_part_parser_data *data)
54 {
55 struct minor_header hdr;
56 size_t hdr_len, retlen;
57 size_t rootfs_offset;
58 struct mtd_partition *parts;
59 int err;
60
61 hdr_len = sizeof(hdr);
62 err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
63 if (err)
64 return err;
65
66 if (retlen != hdr_len)
67 return -EIO;
68
69 /* match header */
70 if (hdr.yaffs_type != YAFFS_OBJECT_TYPE_FILE)
71 return -EINVAL;
72
73 if (hdr.yaffs_obj_id != YAFFS_OBJECTID_ROOT)
74 return -EINVAL;
75
76 if (hdr.yaffs_sum_unused != YAFFS_SUM_UNUSED)
77 return -EINVAL;
78
79 if (memcmp(hdr.yaffs_name, YAFFS_NAME, sizeof(YAFFS_NAME)))
80 return -EINVAL;
81
82 err = mtd_find_rootfs_from(master, master->erasesize, master->size,
83 &rootfs_offset, NULL);
84 if (err)
85 return err;
86
87 parts = kzalloc(MINOR_NR_PARTS * sizeof(*parts), GFP_KERNEL);
88 if (!parts)
89 return -ENOMEM;
90
91 parts[0].name = KERNEL_PART_NAME;
92 parts[0].offset = 0;
93 parts[0].size = rootfs_offset;
94
95 parts[1].name = ROOTFS_PART_NAME;
96 parts[1].offset = rootfs_offset;
97 parts[1].size = master->size - rootfs_offset;
98
99 *pparts = parts;
100 return MINOR_NR_PARTS;
101 }
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
109 static struct mtd_part_parser mtdsplit_minor_parser = {
110 .owner = THIS_MODULE,
111 .name = "minor-fw",
112 .of_match_table = mtdsplit_minor_of_match_table,
113 .parse_fn = mtdsplit_parse_minor,
114 .type = MTD_PARSER_TYPE_FIRMWARE,
115 };
116
117 static int __init mtdsplit_minor_init(void)
118 {
119 register_mtd_parser(&mtdsplit_minor_parser);
120
121 return 0;
122 }
123
124 subsys_initcall(mtdsplit_minor_init);