f8ddee7ce0bf141c5fbc48b6161e062afea934ec
[openwrt/openwrt.git] / target / linux / generic / files / drivers / mtd / mtdsplit / mtdsplit_seama.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 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/mtd/partitions.h>
16 #include <linux/byteorder/generic.h>
17
18 #include "mtdsplit.h"
19
20 #define SEAMA_MAGIC 0x5EA3A417
21 #define SEAMA_NR_PARTS 2
22 #define SEAMA_MIN_ROOTFS_OFFS 0x80000 /* 512KiB */
23
24 struct seama_header {
25 __be32 magic; /* should always be SEAMA_MAGIC. */
26 __be16 reserved; /* reserved for */
27 __be16 metasize; /* size of the META data */
28 __be32 size; /* size of the image */
29 };
30
31 static int mtdsplit_parse_seama(struct mtd_info *master,
32 struct mtd_partition **pparts,
33 struct mtd_part_parser_data *data)
34 {
35 struct seama_header hdr;
36 size_t hdr_len, retlen, kernel_ent_size;
37 size_t rootfs_offset;
38 struct mtd_partition *parts;
39 enum mtdsplit_part_type type;
40 int err;
41
42 hdr_len = sizeof(hdr);
43 err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
44 if (err)
45 return err;
46
47 if (retlen != hdr_len)
48 return -EIO;
49
50 /* sanity checks */
51 if (be32_to_cpu(hdr.magic) != SEAMA_MAGIC)
52 return -EINVAL;
53
54 kernel_ent_size = hdr_len + be32_to_cpu(hdr.size) +
55 be16_to_cpu(hdr.metasize);
56 if (kernel_ent_size > master->size)
57 return -EINVAL;
58
59 /* Check for the rootfs right after Seama entity with a kernel. */
60 err = mtd_check_rootfs_magic(master, kernel_ent_size, &type);
61 if (!err) {
62 rootfs_offset = kernel_ent_size;
63 } else {
64 /*
65 * On some devices firmware entity might contain both: kernel
66 * and rootfs. We can't determine kernel size so we just have to
67 * look for rootfs magic.
68 * Start the search from an arbitrary offset.
69 */
70 err = mtd_find_rootfs_from(master, SEAMA_MIN_ROOTFS_OFFS,
71 master->size, &rootfs_offset, &type);
72 if (err)
73 return err;
74 }
75
76 parts = kzalloc(SEAMA_NR_PARTS * sizeof(*parts), GFP_KERNEL);
77 if (!parts)
78 return -ENOMEM;
79
80 parts[0].name = KERNEL_PART_NAME;
81 parts[0].offset = 0;
82 parts[0].size = rootfs_offset;
83
84 if (type == MTDSPLIT_PART_TYPE_UBI)
85 parts[1].name = UBI_PART_NAME;
86 else
87 parts[1].name = ROOTFS_PART_NAME;
88 parts[1].offset = rootfs_offset;
89 parts[1].size = master->size - rootfs_offset;
90
91 *pparts = parts;
92 return SEAMA_NR_PARTS;
93 }
94
95 static struct mtd_part_parser mtdsplit_seama_parser = {
96 .owner = THIS_MODULE,
97 .name = "seama-fw",
98 .parse_fn = mtdsplit_parse_seama,
99 .type = MTD_PARSER_TYPE_FIRMWARE,
100 };
101
102 static int __init mtdsplit_seama_init(void)
103 {
104 register_mtd_parser(&mtdsplit_seama_parser);
105
106 return 0;
107 }
108
109 subsys_initcall(mtdsplit_seama_init);