tl-wr1043nd: Register missing leds.
[openwrt.git] / target / linux / adm5120 / files / drivers / mtd / trxsplit.c
1 /*
2 * Copyright (C) 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/types.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/list.h>
15 #include <linux/kmod.h>
16 #include <linux/root_dev.h>
17
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/partitions.h>
20
21 #include <linux/byteorder/generic.h>
22
23 #define PFX "trxsplit: "
24
25 #define TRX_MAGIC 0x30524448 /* "HDR0" */
26 #define TRX_VERSION 1
27 #define TRX_MAX_LEN 0x3A0000
28 #define TRX_NO_HEADER 0x1 /* do not write TRX header */
29 #define TRX_GZ_FILES 0x2 /* contains individual gzip files */
30 #define TRX_MAX_OFFSET 3
31 #define TRX_MIN_KERNEL_SIZE 256*1024
32
33 struct trx_header {
34 u32 magic; /* "HDR0" */
35 u32 len; /* Length of file including header */
36 u32 crc32; /* 32-bit CRC from flag_version to end of file */
37 u32 flag_version; /* 0:15 flags, 16:31 version */
38 u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions */
39 };
40
41 #define TRX_ALIGN 0x1000
42
43 static int trx_nr_parts;
44 static unsigned long trx_offset;
45 static struct mtd_info *trx_mtd;
46 static struct mtd_partition trx_parts[TRX_MAX_OFFSET];
47 static struct trx_header trx_hdr;
48
49 static int trxsplit_refresh_partitions(struct mtd_info *mtd);
50
51 static int trxsplit_checktrx(struct mtd_info *mtd, unsigned long offset)
52 {
53 size_t retlen;
54 int err;
55
56 err = mtd->read(mtd, offset, sizeof(trx_hdr), &retlen,
57 (void *)&trx_hdr);
58 if (err) {
59 printk(KERN_ALERT PFX "unable to read from '%s'\n", mtd->name);
60 goto err_out;
61 }
62
63 if (retlen != sizeof(trx_hdr)) {
64 printk(KERN_ALERT PFX "reading failed on '%s'\n", mtd->name);
65 goto err_out;
66 }
67
68 trx_hdr.magic = le32_to_cpu(trx_hdr.magic);
69 trx_hdr.len = le32_to_cpu(trx_hdr.len);
70 trx_hdr.crc32 = le32_to_cpu(trx_hdr.crc32);
71 trx_hdr.flag_version = le32_to_cpu(trx_hdr.flag_version);
72 trx_hdr.offsets[0] = le32_to_cpu(trx_hdr.offsets[0]);
73 trx_hdr.offsets[1] = le32_to_cpu(trx_hdr.offsets[1]);
74 trx_hdr.offsets[2] = le32_to_cpu(trx_hdr.offsets[2]);
75
76 /* sanity checks */
77 if (trx_hdr.magic != TRX_MAGIC)
78 goto err_out;
79
80 if (trx_hdr.len > mtd->size - offset)
81 goto err_out;
82
83 /* TODO: add crc32 checking too? */
84
85 return 0;
86
87 err_out:
88 return -1;
89 }
90
91 static void trxsplit_findtrx(struct mtd_info *mtd)
92 {
93 unsigned long offset;
94 int err;
95
96 printk(KERN_INFO PFX "searching TRX header in '%s'\n", mtd->name);
97
98 err = 0;
99 for (offset = 0; offset < mtd->size; offset += TRX_ALIGN) {
100 err = trxsplit_checktrx(mtd, offset);
101 if (err == 0)
102 break;
103 }
104
105 if (err)
106 return;
107
108 printk(KERN_INFO PFX "TRX header found at 0x%lX\n", offset);
109
110 trx_mtd = mtd;
111 trx_offset = offset;
112 }
113
114 static void trxsplit_create_partitions(struct mtd_info *mtd)
115 {
116 struct mtd_partition *part = trx_parts;
117 int err;
118 int i;
119
120 for (i = 0; i < TRX_MAX_OFFSET; i++) {
121 part = &trx_parts[i];
122 if (trx_hdr.offsets[i] == 0)
123 continue;
124 part->offset = trx_offset + trx_hdr.offsets[i];
125 trx_nr_parts++;
126 }
127
128 for (i = 0; i < trx_nr_parts-1; i++)
129 trx_parts[i].size = trx_parts[i+1].offset - trx_parts[i].offset;
130
131 trx_parts[i].size = mtd->size - trx_parts[i].offset;
132
133 i = 0;
134 part = &trx_parts[i];
135 if (part->size < TRX_MIN_KERNEL_SIZE) {
136 part->name = "loader";
137 i++;
138 }
139
140 part = &trx_parts[i];
141 part->name = "kernel";
142 i++;
143
144 part = &trx_parts[i];
145 part->name = "rootfs";
146
147 err = add_mtd_partitions(mtd, trx_parts, trx_nr_parts);
148 if (err) {
149 printk(KERN_ALERT PFX "adding TRX partitions failed\n");
150 return;
151 }
152
153 mtd->refresh_device = trxsplit_refresh_partitions;
154 }
155
156 static int trxsplit_refresh_partitions(struct mtd_info *mtd)
157 {
158 printk(KERN_INFO PFX "refreshing TRX partitions in '%s' (%d,%d)\n",
159 mtd->name, MTD_BLOCK_MAJOR, mtd->index);
160
161 /* remove old partitions */
162 del_mtd_partitions(mtd);
163
164 trxsplit_findtrx(mtd);
165 if (!trx_mtd)
166 goto err;
167
168 trxsplit_create_partitions(trx_mtd);
169 return 1;
170
171 err:
172 return 0;
173 }
174
175 static void __init trxsplit_add_mtd(struct mtd_info *mtd)
176 {
177 if (mtd->type != MTD_NORFLASH) {
178 printk(KERN_INFO PFX "'%s' is not a NOR flash, skipped\n",
179 mtd->name);
180 return;
181 }
182
183 if (!trx_mtd)
184 trxsplit_findtrx(mtd);
185 }
186
187 static void __init trxsplit_remove_mtd(struct mtd_info *mtd)
188 {
189 /* nothing to do */
190 }
191
192 static struct mtd_notifier trxsplit_notifier __initdata = {
193 .add = trxsplit_add_mtd,
194 .remove = trxsplit_remove_mtd,
195 };
196
197 static void __init trxsplit_scan(void)
198 {
199 register_mtd_user(&trxsplit_notifier);
200 unregister_mtd_user(&trxsplit_notifier);
201 }
202
203 static int __init trxsplit_init(void)
204 {
205 trxsplit_scan();
206
207 if (trx_mtd) {
208 printk(KERN_INFO PFX "creating TRX partitions in '%s' "
209 "(%d,%d)\n", trx_mtd->name, MTD_BLOCK_MAJOR,
210 trx_mtd->index);
211 trxsplit_create_partitions(trx_mtd);
212 }
213
214 return 0;
215 }
216
217 late_initcall(trxsplit_init);
This page took 0.051575 seconds and 5 git commands to generate.