6efffce1965bfae291389a2dacf060f22c585df4
[openwrt.git] / target / linux / generic / files / drivers / mtd / mtdsplit / mtdsplit_trx.c
1 /*
2  *  Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
3  *  Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify it
6  *  under the terms of the GNU General Public License version 2 as published
7  *  by the Free Software Foundation.
8  *
9  */
10
11 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.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 #define TRX_MAGIC   0x30524448  /* "HDR0" */
24
25 struct trx_header {
26         __le32 magic;
27         __le32 len;
28         __le32 crc32;
29         __le32 flag_version;
30         __le32 offset[4];
31 };
32
33 static int
34 read_trx_header(struct mtd_info *mtd, size_t offset,
35                    struct trx_header *header)
36 {
37         size_t header_len;
38         size_t retlen;
39         int ret;
40
41         header_len = sizeof(*header);
42         ret = mtd_read(mtd, offset, header_len, &retlen,
43                        (unsigned char *) header);
44         if (ret) {
45                 pr_debug("read error in \"%s\"\n", mtd->name);
46                 return ret;
47         }
48
49         if (retlen != header_len) {
50                 pr_debug("short read in \"%s\"\n", mtd->name);
51                 return -EIO;
52         }
53
54         return 0;
55 }
56
57 static int
58 mtdsplit_parse_trx(struct mtd_info *master,
59                    struct mtd_partition **pparts,
60                    struct mtd_part_parser_data *data)
61 {
62         struct mtd_partition *parts;
63         struct trx_header hdr;
64         int nr_parts;
65         size_t offset;
66         size_t trx_offset;
67         size_t trx_size = 0;
68         size_t rootfs_offset;
69         size_t rootfs_size = 0;
70         int ret;
71
72         nr_parts = 2;
73         parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
74         if (!parts)
75                 return -ENOMEM;
76
77         /* find trx image on erase block boundaries */
78         for (offset = 0; offset < master->size; offset += master->erasesize) {
79                 trx_size = 0;
80
81                 ret = read_trx_header(master, offset, &hdr);
82                 if (ret)
83                         continue;
84
85                 if (hdr.magic != cpu_to_le32(TRX_MAGIC)) {
86                         pr_debug("no valid trx header found in \"%s\" at offset %llx\n",
87                                  master->name, (unsigned long long) offset);
88                         continue;
89                 }
90
91                 trx_size = le32_to_cpu(hdr.len);
92                 if ((offset + trx_size) > master->size) {
93                         pr_debug("trx image exceeds MTD device \"%s\"\n",
94                                  master->name);
95                         continue;
96                 }
97                 break;
98         }
99
100         if (trx_size == 0) {
101                 pr_debug("no trx header found in \"%s\"\n", master->name);
102                 ret = -ENODEV;
103                 goto err;
104         }
105
106         trx_offset = offset + hdr.offset[0];
107         rootfs_offset = offset + hdr.offset[1];
108         rootfs_size = master->size - rootfs_offset;
109         trx_size = rootfs_offset - trx_offset;
110
111         if (rootfs_size == 0) {
112                 pr_debug("no rootfs found in \"%s\"\n", master->name);
113                 ret = -ENODEV;
114                 goto err;
115         }
116
117         parts[0].name = KERNEL_PART_NAME;
118         parts[0].offset = trx_offset;
119         parts[0].size = trx_size;
120
121         parts[1].name = ROOTFS_PART_NAME;
122         parts[1].offset = rootfs_offset;
123         parts[1].size = rootfs_size;
124
125         *pparts = parts;
126         return nr_parts;
127
128 err:
129         kfree(parts);
130         return ret;
131 }
132
133 static struct mtd_part_parser trx_parser = {
134         .owner = THIS_MODULE,
135         .name = "trx-fw",
136         .parse_fn = mtdsplit_parse_trx,
137         .type = MTD_PARSER_TYPE_FIRMWARE,
138 };
139
140 static int __init mtdsplit_trx_init(void)
141 {
142         register_mtd_parser(&trx_parser);
143
144         return 0;
145 }
146
147 module_init(mtdsplit_trx_init);