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