2 * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
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.
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>
20 #define SEAMA_MAGIC 0x5EA3A417
21 #define SEAMA_NR_PARTS 2
22 #define SEAMA_MIN_ROOTFS_OFFS 0x80000 /* 512KiB */
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 */
31 static int mtdsplit_parse_seama(struct mtd_info *master,
32 struct mtd_partition **pparts,
33 struct mtd_part_parser_data *data)
35 struct seama_header hdr;
36 size_t hdr_len, retlen, kernel_size;
38 struct mtd_partition *parts;
41 hdr_len = sizeof(hdr);
42 err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
46 if (retlen != hdr_len)
50 if (be32_to_cpu(hdr.magic) != SEAMA_MAGIC)
53 kernel_size = hdr_len + be32_to_cpu(hdr.size) +
54 be16_to_cpu(hdr.metasize);
55 if (kernel_size > master->size)
58 /* Find the rootfs after the kernel. */
59 err = mtd_check_rootfs_magic(master, kernel_size);
61 rootfs_offset = kernel_size;
64 * The size in the header might cover the rootfs as well.
65 * Start the search from an arbitrary offset.
67 err = mtd_find_rootfs_from(master, SEAMA_MIN_ROOTFS_OFFS,
68 master->size, &rootfs_offset);
73 parts = kzalloc(SEAMA_NR_PARTS * sizeof(*parts), GFP_KERNEL);
77 parts[0].name = KERNEL_PART_NAME;
79 parts[0].size = rootfs_offset;
81 parts[1].name = ROOTFS_PART_NAME;
82 parts[1].offset = rootfs_offset;
83 parts[1].size = master->size - rootfs_offset;
86 return SEAMA_NR_PARTS;
89 static struct mtd_part_parser mtdsplit_seama_parser = {
92 .parse_fn = mtdsplit_parse_seama,
93 .type = MTD_PARSER_TYPE_FIRMWARE,
97 mtdsplit_seama_init(void)
99 return register_mtd_parser(&mtdsplit_seama_parser);
102 subsys_initcall(mtdsplit_seama_init);