kernel: mtdsplit_uimage: add "openwrt, ih-magic" device-tree property
[openwrt/openwrt.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 #include <dt-bindings/mtd/partitions/uimage.h>
23
24 #include "mtdsplit.h"
25
26 /*
27 * uimage_header itself is only 64B, but it may be prepended with another data.
28 * Currently the biggest size is for Fon(Foxconn) devices: 64B + 32B
29 */
30 #define MAX_HEADER_LEN 96
31
32 /*
33 * Legacy format image header,
34 * all data in network byte order (aka natural aka bigendian).
35 */
36 struct uimage_header {
37 uint32_t ih_magic; /* Image Header Magic Number */
38 uint32_t ih_hcrc; /* Image Header CRC Checksum */
39 uint32_t ih_time; /* Image Creation Timestamp */
40 uint32_t ih_size; /* Image Data Size */
41 uint32_t ih_load; /* Data Load Address */
42 uint32_t ih_ep; /* Entry Point Address */
43 uint32_t ih_dcrc; /* Image Data CRC Checksum */
44 uint8_t ih_os; /* Operating System */
45 uint8_t ih_arch; /* CPU architecture */
46 uint8_t ih_type; /* Image Type */
47 uint8_t ih_comp; /* Compression Type */
48 uint8_t ih_name[IH_NMLEN]; /* Image Name */
49 };
50
51 static int
52 read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
53 size_t header_len)
54 {
55 size_t retlen;
56 int ret;
57
58 ret = mtd_read(mtd, offset, header_len, &retlen, buf);
59 if (ret) {
60 pr_debug("read error in \"%s\"\n", mtd->name);
61 return ret;
62 }
63
64 if (retlen != header_len) {
65 pr_debug("short read in \"%s\"\n", mtd->name);
66 return -EIO;
67 }
68
69 return 0;
70 }
71
72 static void uimage_parse_dt(struct mtd_info *master, int *extralen, u32 *ih_magic)
73 {
74 struct device_node *np = mtd_get_of_node(master);
75
76 if (!np || !of_device_is_compatible(np, "openwrt,uimage"))
77 return;
78
79 if (!of_property_read_u32(np, "openwrt,padding", extralen))
80 pr_debug("got openwrt,padding=%d from device-tree\n", *extralen);
81 if (!of_property_read_u32(np, "openwrt,ih-magic", ih_magic))
82 pr_debug("got openwrt,ih-magic=%08x from device-tree\n", *ih_magic);
83 }
84
85 /**
86 * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
87 *
88 * @find_header: function to call for a block of data that will return offset
89 * and tail padding length of a valid uImage header if found
90 */
91 static int __mtdsplit_parse_uimage(struct mtd_info *master,
92 const struct mtd_partition **pparts,
93 struct mtd_part_parser_data *data,
94 ssize_t (*find_header)(u_char *buf, size_t len, u32 ih_magic))
95 {
96 struct mtd_partition *parts;
97 u_char *buf;
98 int nr_parts;
99 size_t offset;
100 size_t uimage_offset;
101 size_t uimage_size = 0;
102 size_t rootfs_offset;
103 size_t rootfs_size = 0;
104 int uimage_part, rf_part;
105 int ret;
106 int extralen = 0;
107 u32 ih_magic = IH_MAGIC;
108 enum mtdsplit_part_type type;
109
110 nr_parts = 2;
111 parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
112 if (!parts)
113 return -ENOMEM;
114
115 buf = vmalloc(MAX_HEADER_LEN);
116 if (!buf) {
117 ret = -ENOMEM;
118 goto err_free_parts;
119 }
120
121 uimage_parse_dt(master, &extralen, &ih_magic);
122
123 /* find uImage on erase block boundaries */
124 for (offset = 0; offset < master->size; offset += master->erasesize) {
125 struct uimage_header *header;
126
127 uimage_size = 0;
128
129 ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN);
130 if (ret)
131 continue;
132
133 ret = find_header(buf, MAX_HEADER_LEN, ih_magic);
134 if (ret < 0) {
135 pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
136 master->name, (unsigned long long) offset);
137 continue;
138 }
139 header = (struct uimage_header *)(buf + ret);
140
141 uimage_size = sizeof(*header) +
142 be32_to_cpu(header->ih_size) + ret + extralen;
143
144 if ((offset + uimage_size) > master->size) {
145 pr_debug("uImage exceeds MTD device \"%s\"\n",
146 master->name);
147 continue;
148 }
149 break;
150 }
151
152 if (uimage_size == 0) {
153 pr_debug("no uImage found in \"%s\"\n", master->name);
154 ret = -ENODEV;
155 goto err_free_buf;
156 }
157
158 uimage_offset = offset;
159
160 if (uimage_offset == 0) {
161 uimage_part = 0;
162 rf_part = 1;
163
164 /* find the roots after the uImage */
165 ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
166 master->size, &rootfs_offset, &type);
167 if (ret) {
168 pr_debug("no rootfs after uImage in \"%s\"\n",
169 master->name);
170 goto err_free_buf;
171 }
172
173 rootfs_size = master->size - rootfs_offset;
174 uimage_size = rootfs_offset - uimage_offset;
175 } else {
176 rf_part = 0;
177 uimage_part = 1;
178
179 /* check rootfs presence at offset 0 */
180 ret = mtd_check_rootfs_magic(master, 0, &type);
181 if (ret) {
182 pr_debug("no rootfs before uImage in \"%s\"\n",
183 master->name);
184 goto err_free_buf;
185 }
186
187 rootfs_offset = 0;
188 rootfs_size = uimage_offset;
189 }
190
191 if (rootfs_size == 0) {
192 pr_debug("no rootfs found in \"%s\"\n", master->name);
193 ret = -ENODEV;
194 goto err_free_buf;
195 }
196
197 parts[uimage_part].name = KERNEL_PART_NAME;
198 parts[uimage_part].offset = uimage_offset;
199 parts[uimage_part].size = uimage_size;
200
201 if (type == MTDSPLIT_PART_TYPE_UBI)
202 parts[rf_part].name = UBI_PART_NAME;
203 else
204 parts[rf_part].name = ROOTFS_PART_NAME;
205 parts[rf_part].offset = rootfs_offset;
206 parts[rf_part].size = rootfs_size;
207
208 vfree(buf);
209
210 *pparts = parts;
211 return nr_parts;
212
213 err_free_buf:
214 vfree(buf);
215
216 err_free_parts:
217 kfree(parts);
218 return ret;
219 }
220
221 static ssize_t uimage_verify_default(u_char *buf, size_t len, u32 ih_magic)
222 {
223 struct uimage_header *header = (struct uimage_header *)buf;
224
225 /* default sanity checks */
226 if (be32_to_cpu(header->ih_magic) != ih_magic) {
227 pr_debug("invalid uImage magic: %08x != %08x\n",
228 be32_to_cpu(header->ih_magic), ih_magic);
229 return -EINVAL;
230 }
231
232 if (header->ih_os != IH_OS_LINUX) {
233 pr_debug("invalid uImage OS: %08x != %08x\n",
234 be32_to_cpu(header->ih_os), IH_OS_LINUX);
235 return -EINVAL;
236 }
237
238 if (header->ih_type != IH_TYPE_KERNEL) {
239 pr_debug("invalid uImage type: %08x != %08x\n",
240 be32_to_cpu(header->ih_type), IH_TYPE_KERNEL);
241 return -EINVAL;
242 }
243
244 return 0;
245 }
246
247 static int
248 mtdsplit_uimage_parse_generic(struct mtd_info *master,
249 const struct mtd_partition **pparts,
250 struct mtd_part_parser_data *data)
251 {
252 return __mtdsplit_parse_uimage(master, pparts, data,
253 uimage_verify_default);
254 }
255
256 static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
257 { .compatible = "denx,uimage" },
258 { .compatible = "openwrt,uimage" },
259 {},
260 };
261
262 static struct mtd_part_parser uimage_generic_parser = {
263 .owner = THIS_MODULE,
264 .name = "uimage-fw",
265 .of_match_table = mtdsplit_uimage_of_match_table,
266 .parse_fn = mtdsplit_uimage_parse_generic,
267 .type = MTD_PARSER_TYPE_FIRMWARE,
268 };
269
270 #define FW_MAGIC_GS110TPPV1 0x4e474520
271 #define FW_MAGIC_WNR2000V1 0x32303031
272 #define FW_MAGIC_WNR2000V3 0x32303033
273 #define FW_MAGIC_WNR2000V4 0x32303034
274 #define FW_MAGIC_WNR2200 0x32323030
275 #define FW_MAGIC_WNR612V2 0x32303631
276 #define FW_MAGIC_WNR1000V2 0x31303031
277 #define FW_MAGIC_WNR1000V2_VC 0x31303030
278 #define FW_MAGIC_WNDR3700 0x33373030
279 #define FW_MAGIC_WNDR3700V2 0x33373031
280 #define FW_MAGIC_WPN824N 0x31313030
281
282 static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len, u32 ih_magic)
283 {
284 struct uimage_header *header = (struct uimage_header *)buf;
285 uint8_t expected_type = IH_TYPE_FILESYSTEM;
286
287 switch (be32_to_cpu(header->ih_magic)) {
288 case FW_MAGIC_GS110TPPV1:
289 case FW_MAGIC_WNR2000V4:
290 expected_type = IH_TYPE_KERNEL;
291 break;
292 case FW_MAGIC_WNR612V2:
293 case FW_MAGIC_WNR1000V2:
294 case FW_MAGIC_WNR1000V2_VC:
295 case FW_MAGIC_WNR2000V1:
296 case FW_MAGIC_WNR2000V3:
297 case FW_MAGIC_WNR2200:
298 case FW_MAGIC_WNDR3700:
299 case FW_MAGIC_WNDR3700V2:
300 case FW_MAGIC_WPN824N:
301 break;
302 default:
303 return -EINVAL;
304 }
305
306 if (header->ih_os != IH_OS_LINUX ||
307 header->ih_type != expected_type)
308 return -EINVAL;
309
310 return 0;
311 }
312
313 static int
314 mtdsplit_uimage_parse_netgear(struct mtd_info *master,
315 const struct mtd_partition **pparts,
316 struct mtd_part_parser_data *data)
317 {
318 return __mtdsplit_parse_uimage(master, pparts, data,
319 uimage_verify_wndr3700);
320 }
321
322 static const struct of_device_id mtdsplit_uimage_netgear_of_match_table[] = {
323 { .compatible = "netgear,uimage" },
324 {},
325 };
326
327 static struct mtd_part_parser uimage_netgear_parser = {
328 .owner = THIS_MODULE,
329 .name = "netgear-fw",
330 .of_match_table = mtdsplit_uimage_netgear_of_match_table,
331 .parse_fn = mtdsplit_uimage_parse_netgear,
332 .type = MTD_PARSER_TYPE_FIRMWARE,
333
334 };
335
336
337 /**************************************************
338 * ALLNET
339 **************************************************/
340
341 #define FW_MAGIC_SG8208M 0x00000006
342 #define FW_MAGIC_SG8310PM 0x83000006
343
344 static ssize_t uimage_verify_allnet(u_char *buf, size_t len, u32 ih_magic)
345 {
346 struct uimage_header *header = (struct uimage_header *)buf;
347
348 switch (be32_to_cpu(header->ih_magic)) {
349 case FW_MAGIC_SG8208M:
350 case FW_MAGIC_SG8310PM:
351 break;
352 default:
353 return -EINVAL;
354 }
355
356 if (header->ih_os != IH_OS_LINUX)
357 return -EINVAL;
358
359 return 0;
360 }
361
362 static int
363 mtdsplit_uimage_parse_allnet(struct mtd_info *master,
364 const struct mtd_partition **pparts,
365 struct mtd_part_parser_data *data)
366 {
367 return __mtdsplit_parse_uimage(master, pparts, data,
368 uimage_verify_allnet);
369 }
370
371 static const struct of_device_id mtdsplit_uimage_allnet_of_match_table[] = {
372 { .compatible = "allnet,uimage" },
373 {},
374 };
375
376 static struct mtd_part_parser uimage_allnet_parser = {
377 .owner = THIS_MODULE,
378 .name = "allnet-fw",
379 .of_match_table = mtdsplit_uimage_allnet_of_match_table,
380 .parse_fn = mtdsplit_uimage_parse_allnet,
381 };
382
383
384 /**************************************************
385 * Edimax
386 **************************************************/
387
388 #define FW_EDIMAX_OFFSET 20
389 #define FW_MAGIC_EDIMAX 0x43535953
390
391 static ssize_t uimage_find_edimax(u_char *buf, size_t len, u32 ih_magic)
392 {
393 u32 *magic;
394
395 if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
396 pr_err("Buffer too small for checking Edimax header\n");
397 return -ENOSPC;
398 }
399
400 magic = (u32 *)buf;
401 if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
402 return -EINVAL;
403
404 if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len, ih_magic))
405 return FW_EDIMAX_OFFSET;
406
407 return -EINVAL;
408 }
409
410 static int
411 mtdsplit_uimage_parse_edimax(struct mtd_info *master,
412 const struct mtd_partition **pparts,
413 struct mtd_part_parser_data *data)
414 {
415 return __mtdsplit_parse_uimage(master, pparts, data,
416 uimage_find_edimax);
417 }
418
419 static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
420 { .compatible = "edimax,uimage" },
421 {},
422 };
423
424 static struct mtd_part_parser uimage_edimax_parser = {
425 .owner = THIS_MODULE,
426 .name = "edimax-fw",
427 .of_match_table = mtdsplit_uimage_edimax_of_match_table,
428 .parse_fn = mtdsplit_uimage_parse_edimax,
429 .type = MTD_PARSER_TYPE_FIRMWARE,
430 };
431
432 /**************************************************
433 * OKLI (OpenWrt Kernel Loader Image)
434 **************************************************/
435
436 #define IH_MAGIC_OKLI 0x4f4b4c49
437
438 static ssize_t uimage_verify_okli(u_char *buf, size_t len, u32 ih_magic)
439 {
440 struct uimage_header *header = (struct uimage_header *)buf;
441
442 /* default sanity checks */
443 if (be32_to_cpu(header->ih_magic) != IH_MAGIC_OKLI) {
444 pr_debug("invalid uImage magic: %08x\n",
445 be32_to_cpu(header->ih_magic));
446 return -EINVAL;
447 }
448
449 if (header->ih_os != IH_OS_LINUX) {
450 pr_debug("invalid uImage OS: %08x\n",
451 be32_to_cpu(header->ih_os));
452 return -EINVAL;
453 }
454
455 if (header->ih_type != IH_TYPE_KERNEL) {
456 pr_debug("invalid uImage type: %08x\n",
457 be32_to_cpu(header->ih_type));
458 return -EINVAL;
459 }
460
461 return 0;
462 }
463
464 static int
465 mtdsplit_uimage_parse_okli(struct mtd_info *master,
466 const struct mtd_partition **pparts,
467 struct mtd_part_parser_data *data)
468 {
469 return __mtdsplit_parse_uimage(master, pparts, data,
470 uimage_verify_okli);
471 }
472
473 static const struct of_device_id mtdsplit_uimage_okli_of_match_table[] = {
474 { .compatible = "openwrt,okli" },
475 {},
476 };
477
478 static struct mtd_part_parser uimage_okli_parser = {
479 .owner = THIS_MODULE,
480 .name = "okli-fw",
481 .of_match_table = mtdsplit_uimage_okli_of_match_table,
482 .parse_fn = mtdsplit_uimage_parse_okli,
483 };
484
485 /**************************************************
486 * Init
487 **************************************************/
488
489 static int __init mtdsplit_uimage_init(void)
490 {
491 register_mtd_parser(&uimage_generic_parser);
492 register_mtd_parser(&uimage_netgear_parser);
493 register_mtd_parser(&uimage_allnet_parser);
494 register_mtd_parser(&uimage_edimax_parser);
495 register_mtd_parser(&uimage_okli_parser);
496
497 return 0;
498 }
499
500 module_init(mtdsplit_uimage_init);