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