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