kernel: Add missing includes mtdsplit_*.c
[openwrt/staging/blogic.git] / target / linux / generic / files / drivers / mtd / mtdsplit / mtdsplit_uimage.c
1 /*
2 * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
7 *
8 */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/version.h>
20 #include <linux/byteorder/generic.h>
21 #include <linux/of.h>
22
23 #include "mtdsplit.h"
24
25 /*
26 * uimage_header itself is only 64B, but it may be prepended with another data.
27 * Currently the biggest size is for Edimax devices: 20B + 64B
28 */
29 #define MAX_HEADER_LEN 84
30
31 #define IH_MAGIC 0x27051956 /* Image Magic Number */
32 #define IH_NMLEN 32 /* Image Name Length */
33
34 #define IH_OS_LINUX 5 /* Linux */
35
36 #define IH_TYPE_KERNEL 2 /* OS Kernel Image */
37 #define IH_TYPE_FILESYSTEM 7 /* Filesystem Image */
38
39 /*
40 * Legacy format image header,
41 * all data in network byte order (aka natural aka bigendian).
42 */
43 struct uimage_header {
44 uint32_t ih_magic; /* Image Header Magic Number */
45 uint32_t ih_hcrc; /* Image Header CRC Checksum */
46 uint32_t ih_time; /* Image Creation Timestamp */
47 uint32_t ih_size; /* Image Data Size */
48 uint32_t ih_load; /* Data Load Address */
49 uint32_t ih_ep; /* Entry Point Address */
50 uint32_t ih_dcrc; /* Image Data CRC Checksum */
51 uint8_t ih_os; /* Operating System */
52 uint8_t ih_arch; /* CPU architecture */
53 uint8_t ih_type; /* Image Type */
54 uint8_t ih_comp; /* Compression Type */
55 uint8_t ih_name[IH_NMLEN]; /* Image Name */
56 };
57
58 static int
59 read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
60 size_t header_len)
61 {
62 size_t retlen;
63 int ret;
64
65 ret = mtd_read(mtd, offset, header_len, &retlen, buf);
66 if (ret) {
67 pr_debug("read error in \"%s\"\n", mtd->name);
68 return ret;
69 }
70
71 if (retlen != header_len) {
72 pr_debug("short read in \"%s\"\n", mtd->name);
73 return -EIO;
74 }
75
76 return 0;
77 }
78
79 /**
80 * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
81 *
82 * @find_header: function to call for a block of data that will return offset
83 * of a valid uImage header if found
84 */
85 static int __mtdsplit_parse_uimage(struct mtd_info *master,
86 const struct mtd_partition **pparts,
87 struct mtd_part_parser_data *data,
88 ssize_t (*find_header)(u_char *buf, size_t len))
89 {
90 struct mtd_partition *parts;
91 u_char *buf;
92 int nr_parts;
93 size_t offset;
94 size_t uimage_offset;
95 size_t uimage_size = 0;
96 size_t rootfs_offset;
97 size_t rootfs_size = 0;
98 int uimage_part, rf_part;
99 int ret;
100 enum mtdsplit_part_type type;
101
102 nr_parts = 2;
103 parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
104 if (!parts)
105 return -ENOMEM;
106
107 buf = vmalloc(MAX_HEADER_LEN);
108 if (!buf) {
109 ret = -ENOMEM;
110 goto err_free_parts;
111 }
112
113 /* find uImage on erase block boundaries */
114 for (offset = 0; offset < master->size; offset += master->erasesize) {
115 struct uimage_header *header;
116
117 uimage_size = 0;
118
119 ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN);
120 if (ret)
121 continue;
122
123 ret = find_header(buf, MAX_HEADER_LEN);
124 if (ret < 0) {
125 pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
126 master->name, (unsigned long long) offset);
127 continue;
128 }
129 header = (struct uimage_header *)(buf + ret);
130
131 uimage_size = sizeof(*header) + be32_to_cpu(header->ih_size) + ret;
132 if ((offset + uimage_size) > master->size) {
133 pr_debug("uImage exceeds MTD device \"%s\"\n",
134 master->name);
135 continue;
136 }
137 break;
138 }
139
140 if (uimage_size == 0) {
141 pr_debug("no uImage found in \"%s\"\n", master->name);
142 ret = -ENODEV;
143 goto err_free_buf;
144 }
145
146 uimage_offset = offset;
147
148 if (uimage_offset == 0) {
149 uimage_part = 0;
150 rf_part = 1;
151
152 /* find the roots after the uImage */
153 ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
154 master->size, &rootfs_offset, &type);
155 if (ret) {
156 pr_debug("no rootfs after uImage in \"%s\"\n",
157 master->name);
158 goto err_free_buf;
159 }
160
161 rootfs_size = master->size - rootfs_offset;
162 uimage_size = rootfs_offset - uimage_offset;
163 } else {
164 rf_part = 0;
165 uimage_part = 1;
166
167 /* check rootfs presence at offset 0 */
168 ret = mtd_check_rootfs_magic(master, 0, &type);
169 if (ret) {
170 pr_debug("no rootfs before uImage in \"%s\"\n",
171 master->name);
172 goto err_free_buf;
173 }
174
175 rootfs_offset = 0;
176 rootfs_size = uimage_offset;
177 }
178
179 if (rootfs_size == 0) {
180 pr_debug("no rootfs found in \"%s\"\n", master->name);
181 ret = -ENODEV;
182 goto err_free_buf;
183 }
184
185 parts[uimage_part].name = KERNEL_PART_NAME;
186 parts[uimage_part].offset = uimage_offset;
187 parts[uimage_part].size = uimage_size;
188
189 if (type == MTDSPLIT_PART_TYPE_UBI)
190 parts[rf_part].name = UBI_PART_NAME;
191 else
192 parts[rf_part].name = ROOTFS_PART_NAME;
193 parts[rf_part].offset = rootfs_offset;
194 parts[rf_part].size = rootfs_size;
195
196 vfree(buf);
197
198 *pparts = parts;
199 return nr_parts;
200
201 err_free_buf:
202 vfree(buf);
203
204 err_free_parts:
205 kfree(parts);
206 return ret;
207 }
208
209 static ssize_t uimage_verify_default(u_char *buf, size_t len)
210 {
211 struct uimage_header *header = (struct uimage_header *)buf;
212
213 /* default sanity checks */
214 if (be32_to_cpu(header->ih_magic) != IH_MAGIC) {
215 pr_debug("invalid uImage magic: %08x\n",
216 be32_to_cpu(header->ih_magic));
217 return -EINVAL;
218 }
219
220 if (header->ih_os != IH_OS_LINUX) {
221 pr_debug("invalid uImage OS: %08x\n",
222 be32_to_cpu(header->ih_os));
223 return -EINVAL;
224 }
225
226 if (header->ih_type != IH_TYPE_KERNEL) {
227 pr_debug("invalid uImage type: %08x\n",
228 be32_to_cpu(header->ih_type));
229 return -EINVAL;
230 }
231
232 return 0;
233 }
234
235 static int
236 mtdsplit_uimage_parse_generic(struct mtd_info *master,
237 const struct mtd_partition **pparts,
238 struct mtd_part_parser_data *data)
239 {
240 return __mtdsplit_parse_uimage(master, pparts, data,
241 uimage_verify_default);
242 }
243
244 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
245 static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
246 { .compatible = "denx,uimage" },
247 {},
248 };
249 #endif
250
251 static struct mtd_part_parser uimage_generic_parser = {
252 .owner = THIS_MODULE,
253 .name = "uimage-fw",
254 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
255 .of_match_table = mtdsplit_uimage_of_match_table,
256 #endif
257 .parse_fn = mtdsplit_uimage_parse_generic,
258 .type = MTD_PARSER_TYPE_FIRMWARE,
259 };
260
261 #define FW_MAGIC_WNR2000V1 0x32303031
262 #define FW_MAGIC_WNR2000V3 0x32303033
263 #define FW_MAGIC_WNR2000V4 0x32303034
264 #define FW_MAGIC_WNR2200 0x32323030
265 #define FW_MAGIC_WNR612V2 0x32303631
266 #define FW_MAGIC_WNR1000V2 0x31303031
267 #define FW_MAGIC_WNR1000V2_VC 0x31303030
268 #define FW_MAGIC_WNDR3700 0x33373030
269 #define FW_MAGIC_WNDR3700V2 0x33373031
270 #define FW_MAGIC_WPN824N 0x31313030
271
272 static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len)
273 {
274 struct uimage_header *header = (struct uimage_header *)buf;
275 uint8_t expected_type = IH_TYPE_FILESYSTEM;
276
277 switch (be32_to_cpu(header->ih_magic)) {
278 case FW_MAGIC_WNR612V2:
279 case FW_MAGIC_WNR1000V2:
280 case FW_MAGIC_WNR1000V2_VC:
281 case FW_MAGIC_WNR2000V1:
282 case FW_MAGIC_WNR2000V3:
283 case FW_MAGIC_WNR2200:
284 case FW_MAGIC_WNDR3700:
285 case FW_MAGIC_WNDR3700V2:
286 case FW_MAGIC_WPN824N:
287 break;
288 case FW_MAGIC_WNR2000V4:
289 expected_type = IH_TYPE_KERNEL;
290 break;
291 default:
292 return -EINVAL;
293 }
294
295 if (header->ih_os != IH_OS_LINUX ||
296 header->ih_type != expected_type)
297 return -EINVAL;
298
299 return 0;
300 }
301
302 static int
303 mtdsplit_uimage_parse_netgear(struct mtd_info *master,
304 const struct mtd_partition **pparts,
305 struct mtd_part_parser_data *data)
306 {
307 return __mtdsplit_parse_uimage(master, pparts, data,
308 uimage_verify_wndr3700);
309 }
310
311 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
312 static const struct of_device_id mtdsplit_uimage_netgear_of_match_table[] = {
313 { .compatible = "netgear,uimage" },
314 {},
315 };
316 #endif
317
318 static struct mtd_part_parser uimage_netgear_parser = {
319 .owner = THIS_MODULE,
320 .name = "netgear-fw",
321 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
322 .of_match_table = mtdsplit_uimage_netgear_of_match_table,
323 #endif
324 .parse_fn = mtdsplit_uimage_parse_netgear,
325 .type = MTD_PARSER_TYPE_FIRMWARE,
326 };
327
328 /**************************************************
329 * Edimax
330 **************************************************/
331
332 #define FW_EDIMAX_OFFSET 20
333 #define FW_MAGIC_EDIMAX 0x43535953
334
335 static ssize_t uimage_find_edimax(u_char *buf, size_t len)
336 {
337 u32 *magic;
338
339 if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
340 pr_err("Buffer too small for checking Edimax header\n");
341 return -ENOSPC;
342 }
343
344 magic = (u32 *)buf;
345 if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
346 return -EINVAL;
347
348 if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len))
349 return FW_EDIMAX_OFFSET;
350
351 return -EINVAL;
352 }
353
354 static int
355 mtdsplit_uimage_parse_edimax(struct mtd_info *master,
356 const struct mtd_partition **pparts,
357 struct mtd_part_parser_data *data)
358 {
359 return __mtdsplit_parse_uimage(master, pparts, data,
360 uimage_find_edimax);
361 }
362
363 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
364 static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
365 { .compatible = "edimax,uimage" },
366 {},
367 };
368 #endif
369
370 static struct mtd_part_parser uimage_edimax_parser = {
371 .owner = THIS_MODULE,
372 .name = "edimax-fw",
373 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
374 .of_match_table = mtdsplit_uimage_edimax_of_match_table,
375 #endif
376 .parse_fn = mtdsplit_uimage_parse_edimax,
377 .type = MTD_PARSER_TYPE_FIRMWARE,
378 };
379
380 /**************************************************
381 * Init
382 **************************************************/
383
384 static int __init mtdsplit_uimage_init(void)
385 {
386 register_mtd_parser(&uimage_generic_parser);
387 register_mtd_parser(&uimage_netgear_parser);
388 register_mtd_parser(&uimage_edimax_parser);
389
390 return 0;
391 }
392
393 module_init(mtdsplit_uimage_init);