[ar71xx] WRT160NL: more mtd parser cleanup
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.21 / 060-rootfs_split.patch
1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -49,6 +49,16 @@ config MTD_PARTITIONS
4 devices. Partitioning on NFTL 'devices' is a different - that's the
5 'normal' form of partitioning used on a block device.
6
7 +config MTD_ROOTFS_ROOT_DEV
8 + bool "Automatically set 'rootfs' partition to be root filesystem"
9 + depends on MTD_PARTITIONS
10 + default y
11 +
12 +config MTD_ROOTFS_SPLIT
13 + bool "Automatically split 'rootfs' partition for squashfs"
14 + depends on MTD_PARTITIONS
15 + default y
16 +
17 config MTD_REDBOOT_PARTS
18 tristate "RedBoot partition table parsing"
19 depends on MTD_PARTITIONS
20 --- a/drivers/mtd/mtdpart.c
21 +++ b/drivers/mtd/mtdpart.c
22 @@ -20,6 +20,8 @@
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/partitions.h>
25 #include <linux/mtd/compatmac.h>
26 +#include <linux/squashfs_fs.h>
27 +#include <linux/root_dev.h>
28
29 /* Our partition linked list */
30 static LIST_HEAD(mtd_partitions);
31 @@ -308,6 +310,266 @@ int del_mtd_partitions(struct mtd_info *
32 return 0;
33 }
34
35 +static u_int32_t cur_offset = 0;
36 +static int add_one_partition(struct mtd_info *master, const struct mtd_partition *part,
37 + int i, struct mtd_part **slp)
38 +{
39 + struct mtd_part *slave;
40 +
41 + /* allocate the partition structure */
42 + slave = kzalloc (sizeof(*slave), GFP_KERNEL);
43 + if (!slave) {
44 + printk ("memory allocation error while creating partitions for \"%s\"\n",
45 + master->name);
46 + del_mtd_partitions(master);
47 + return -ENOMEM;
48 + }
49 + list_add(&slave->list, &mtd_partitions);
50 +
51 + /* set up the MTD object for this partition */
52 + slave->mtd.type = master->type;
53 + slave->mtd.flags = master->flags & ~part->mask_flags;
54 + slave->mtd.size = part->size;
55 + slave->mtd.writesize = master->writesize;
56 + slave->mtd.oobsize = master->oobsize;
57 + slave->mtd.oobavail = master->oobavail;
58 + slave->mtd.subpage_sft = master->subpage_sft;
59 +
60 + slave->mtd.name = part->name;
61 + slave->mtd.bank_size = master->bank_size;
62 + slave->mtd.owner = master->owner;
63 +
64 + slave->mtd.read = part_read;
65 + slave->mtd.write = part_write;
66 +
67 + if(master->point && master->unpoint){
68 + slave->mtd.point = part_point;
69 + slave->mtd.unpoint = part_unpoint;
70 + }
71 +
72 + if (master->read_oob)
73 + slave->mtd.read_oob = part_read_oob;
74 + if (master->write_oob)
75 + slave->mtd.write_oob = part_write_oob;
76 + if(master->read_user_prot_reg)
77 + slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
78 + if(master->read_fact_prot_reg)
79 + slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
80 + if(master->write_user_prot_reg)
81 + slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
82 + if(master->lock_user_prot_reg)
83 + slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
84 + if(master->get_user_prot_info)
85 + slave->mtd.get_user_prot_info = part_get_user_prot_info;
86 + if(master->get_fact_prot_info)
87 + slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
88 + if (master->sync)
89 + slave->mtd.sync = part_sync;
90 + if (!i && master->suspend && master->resume) {
91 + slave->mtd.suspend = part_suspend;
92 + slave->mtd.resume = part_resume;
93 + }
94 + if (master->writev)
95 + slave->mtd.writev = part_writev;
96 + if (master->lock)
97 + slave->mtd.lock = part_lock;
98 + if (master->unlock)
99 + slave->mtd.unlock = part_unlock;
100 + if (master->block_isbad)
101 + slave->mtd.block_isbad = part_block_isbad;
102 + if (master->block_markbad)
103 + slave->mtd.block_markbad = part_block_markbad;
104 + slave->mtd.erase = part_erase;
105 + slave->master = master;
106 + slave->offset = part->offset;
107 + slave->index = i;
108 +
109 + if (slave->offset == MTDPART_OFS_APPEND)
110 + slave->offset = cur_offset;
111 + if (slave->offset == MTDPART_OFS_NXTBLK) {
112 + slave->offset = cur_offset;
113 + if ((cur_offset % master->erasesize) != 0) {
114 + /* Round up to next erasesize */
115 + slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
116 + printk(KERN_NOTICE "Moving partition %d: "
117 + "0x%08x -> 0x%08x\n", i,
118 + cur_offset, slave->offset);
119 + }
120 + }
121 + if (slave->mtd.size == MTDPART_SIZ_FULL)
122 + slave->mtd.size = master->size - slave->offset;
123 + cur_offset = slave->offset + slave->mtd.size;
124 +
125 + printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
126 + slave->offset + slave->mtd.size, slave->mtd.name);
127 +
128 + /* let's do some sanity checks */
129 + if (slave->offset >= master->size) {
130 + /* let's register it anyway to preserve ordering */
131 + slave->offset = 0;
132 + slave->mtd.size = 0;
133 + printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
134 + part->name);
135 + }
136 + if (slave->offset + slave->mtd.size > master->size) {
137 + slave->mtd.size = master->size - slave->offset;
138 + printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
139 + part->name, master->name, slave->mtd.size);
140 + }
141 + if (master->numeraseregions>1) {
142 + /* Deal with variable erase size stuff */
143 + int i;
144 + struct mtd_erase_region_info *regions = master->eraseregions;
145 +
146 + /* Find the first erase regions which is part of this partition. */
147 + for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
148 + ;
149 +
150 + for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
151 + if (slave->mtd.erasesize < regions[i].erasesize) {
152 + slave->mtd.erasesize = regions[i].erasesize;
153 + }
154 + }
155 + } else {
156 + /* Single erase size */
157 + slave->mtd.erasesize = master->erasesize;
158 + }
159 +
160 + if ((slave->mtd.flags & MTD_WRITEABLE) &&
161 + (slave->offset % slave->mtd.erasesize)) {
162 + /* Doesn't start on a boundary of major erase size */
163 + /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
164 + slave->mtd.flags &= ~MTD_WRITEABLE;
165 + printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
166 + part->name);
167 + }
168 + if ((slave->mtd.flags & MTD_WRITEABLE) &&
169 + (slave->mtd.size % slave->mtd.erasesize)) {
170 + slave->mtd.flags &= ~MTD_WRITEABLE;
171 + printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
172 + part->name);
173 + }
174 +
175 + slave->mtd.ecclayout = master->ecclayout;
176 + if (master->block_isbad) {
177 + uint32_t offs = 0;
178 +
179 + while(offs < slave->mtd.size) {
180 + if (master->block_isbad(master,
181 + offs + slave->offset))
182 + slave->mtd.ecc_stats.badblocks++;
183 + offs += slave->mtd.erasesize;
184 + }
185 + }
186 +
187 + if(part->mtdp)
188 + { /* store the object pointer (caller may or may not register it */
189 + *part->mtdp = &slave->mtd;
190 + slave->registered = 0;
191 + }
192 + else
193 + {
194 + /* register our partition */
195 + add_mtd_device(&slave->mtd);
196 + slave->registered = 1;
197 + }
198 +
199 + if (slp)
200 + *slp = slave;
201 +
202 + return 0;
203 +}
204 +
205 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
206 +#define ROOTFS_SPLIT_NAME "rootfs_data"
207 +static int split_squashfs(struct mtd_info *master, struct mtd_partition *old,
208 + struct mtd_partition **new)
209 +{
210 + struct mtd_partition *part = NULL;
211 + int len;
212 + char buf[512];
213 + struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
214 + int ret;
215 +
216 + ret = master->read(master, old->offset, sizeof(*sb), &len, buf);
217 + if (ret) {
218 + printk(KERN_ALERT "split_squashfs: error occured while reading "
219 + "from \"%s\"\n", master->name);
220 + goto out;
221 + }
222 +
223 + if (len != sizeof(*sb)) {
224 + printk(KERN_ALERT "split_squashfs: unable to read superblock "
225 + "from \"%s\"\n", master->name);
226 + ret=-1;
227 + goto out;
228 + }
229 +
230 + if (*((u32 *) buf) != SQUASHFS_MAGIC) {
231 + printk(KERN_ALERT "split_squasfs: no squashfs found in \"%s\"\n",
232 + master->name);
233 + ret=0;
234 + goto out;
235 + }
236 +
237 + if (sb->bytes_used <= 0) {
238 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
239 + master->name);
240 + ret=0;
241 + goto out;
242 + }
243 +
244 + part = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
245 + if (part == NULL) {
246 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
247 + ROOTFS_SPLIT_NAME);
248 + ret = -ENOMEM;
249 + goto out;
250 + }
251 +
252 + memcpy(part, old, sizeof(*part));
253 + part->name = (unsigned char *)&part[1];
254 + strcpy(part->name, ROOTFS_SPLIT_NAME);
255 +
256 + len = (u32) sb->bytes_used;
257 + len += (part->offset & 0x000fffff);
258 + len += (master->erasesize - 1);
259 + len &= ~(master->erasesize - 1);
260 + len -= (part->offset & 0x000fffff);
261 + part->offset += len;
262 + part->size -= len;
263 +
264 + ret = 0;
265 +
266 +out:
267 + *new = part;
268 + return ret;
269 +}
270 +
271 +static int split_rootfs_data(struct mtd_info *master, struct mtd_partition *part,
272 + int index)
273 +{
274 + struct mtd_partition *dpart;
275 + int ret;
276 +
277 + ret = split_squashfs(master, part, &dpart);
278 + if (ret)
279 + return ret;
280 +
281 + if (dpart == NULL)
282 + return 1;
283 +
284 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%X, len=%X \n",
285 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
286 +
287 + ret = add_one_partition(master, dpart, index, NULL);
288 + if (ret)
289 + kfree(dpart);
290 +
291 + return ret;
292 +}
293 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
294 +
295 /*
296 * This function, given a master MTD object and a partition table, creates
297 * and registers slave MTD objects which are bound to the master according to
298 @@ -320,169 +582,31 @@ int add_mtd_partitions(struct mtd_info *
299 int nbparts)
300 {
301 struct mtd_part *slave;
302 - u_int32_t cur_offset = 0;
303 - int i;
304 + struct mtd_partition *part;
305 + int i, j, ret = 0;
306
307 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
308
309 - for (i = 0; i < nbparts; i++) {
310 -
311 - /* allocate the partition structure */
312 - slave = kzalloc (sizeof(*slave), GFP_KERNEL);
313 - if (!slave) {
314 - printk ("memory allocation error while creating partitions for \"%s\"\n",
315 - master->name);
316 - del_mtd_partitions(master);
317 - return -ENOMEM;
318 - }
319 - list_add(&slave->list, &mtd_partitions);
320 -
321 - /* set up the MTD object for this partition */
322 - slave->mtd.type = master->type;
323 - slave->mtd.flags = master->flags & ~parts[i].mask_flags;
324 - slave->mtd.size = parts[i].size;
325 - slave->mtd.writesize = master->writesize;
326 - slave->mtd.oobsize = master->oobsize;
327 - slave->mtd.oobavail = master->oobavail;
328 - slave->mtd.subpage_sft = master->subpage_sft;
329 -
330 - slave->mtd.name = parts[i].name;
331 - slave->mtd.bank_size = master->bank_size;
332 - slave->mtd.owner = master->owner;
333 -
334 - slave->mtd.read = part_read;
335 - slave->mtd.write = part_write;
336 -
337 - if(master->point && master->unpoint){
338 - slave->mtd.point = part_point;
339 - slave->mtd.unpoint = part_unpoint;
340 - }
341 -
342 - if (master->read_oob)
343 - slave->mtd.read_oob = part_read_oob;
344 - if (master->write_oob)
345 - slave->mtd.write_oob = part_write_oob;
346 - if(master->read_user_prot_reg)
347 - slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
348 - if(master->read_fact_prot_reg)
349 - slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
350 - if(master->write_user_prot_reg)
351 - slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
352 - if(master->lock_user_prot_reg)
353 - slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
354 - if(master->get_user_prot_info)
355 - slave->mtd.get_user_prot_info = part_get_user_prot_info;
356 - if(master->get_fact_prot_info)
357 - slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
358 - if (master->sync)
359 - slave->mtd.sync = part_sync;
360 - if (!i && master->suspend && master->resume) {
361 - slave->mtd.suspend = part_suspend;
362 - slave->mtd.resume = part_resume;
363 - }
364 - if (master->writev)
365 - slave->mtd.writev = part_writev;
366 - if (master->lock)
367 - slave->mtd.lock = part_lock;
368 - if (master->unlock)
369 - slave->mtd.unlock = part_unlock;
370 - if (master->block_isbad)
371 - slave->mtd.block_isbad = part_block_isbad;
372 - if (master->block_markbad)
373 - slave->mtd.block_markbad = part_block_markbad;
374 - slave->mtd.erase = part_erase;
375 - slave->master = master;
376 - slave->offset = parts[i].offset;
377 - slave->index = i;
378 -
379 - if (slave->offset == MTDPART_OFS_APPEND)
380 - slave->offset = cur_offset;
381 - if (slave->offset == MTDPART_OFS_NXTBLK) {
382 - slave->offset = cur_offset;
383 - if ((cur_offset % master->erasesize) != 0) {
384 - /* Round up to next erasesize */
385 - slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
386 - printk(KERN_NOTICE "Moving partition %d: "
387 - "0x%08x -> 0x%08x\n", i,
388 - cur_offset, slave->offset);
389 - }
390 - }
391 - if (slave->mtd.size == MTDPART_SIZ_FULL)
392 - slave->mtd.size = master->size - slave->offset;
393 - cur_offset = slave->offset + slave->mtd.size;
394 -
395 - printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
396 - slave->offset + slave->mtd.size, slave->mtd.name);
397 -
398 - /* let's do some sanity checks */
399 - if (slave->offset >= master->size) {
400 - /* let's register it anyway to preserve ordering */
401 - slave->offset = 0;
402 - slave->mtd.size = 0;
403 - printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
404 - parts[i].name);
405 - }
406 - if (slave->offset + slave->mtd.size > master->size) {
407 - slave->mtd.size = master->size - slave->offset;
408 - printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
409 - parts[i].name, master->name, slave->mtd.size);
410 - }
411 - if (master->numeraseregions>1) {
412 - /* Deal with variable erase size stuff */
413 - int i;
414 - struct mtd_erase_region_info *regions = master->eraseregions;
415 -
416 - /* Find the first erase regions which is part of this partition. */
417 - for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
418 - ;
419 -
420 - for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
421 - if (slave->mtd.erasesize < regions[i].erasesize) {
422 - slave->mtd.erasesize = regions[i].erasesize;
423 - }
424 - }
425 - } else {
426 - /* Single erase size */
427 - slave->mtd.erasesize = master->erasesize;
428 - }
429 -
430 - if ((slave->mtd.flags & MTD_WRITEABLE) &&
431 - (slave->offset % slave->mtd.erasesize)) {
432 - /* Doesn't start on a boundary of major erase size */
433 - /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
434 - slave->mtd.flags &= ~MTD_WRITEABLE;
435 - printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
436 - parts[i].name);
437 - }
438 - if ((slave->mtd.flags & MTD_WRITEABLE) &&
439 - (slave->mtd.size % slave->mtd.erasesize)) {
440 - slave->mtd.flags &= ~MTD_WRITEABLE;
441 - printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
442 - parts[i].name);
443 - }
444 -
445 - slave->mtd.ecclayout = master->ecclayout;
446 - if (master->block_isbad) {
447 - uint32_t offs = 0;
448 -
449 - while(offs < slave->mtd.size) {
450 - if (master->block_isbad(master,
451 - offs + slave->offset))
452 - slave->mtd.ecc_stats.badblocks++;
453 - offs += slave->mtd.erasesize;
454 + for (i = 0, j = 0; i < nbparts; i++) {
455 + part = (struct mtd_partition *) &parts[i];
456 + ret = add_one_partition(master, part, j, &slave);
457 + if (ret)
458 + return ret;
459 + j++;
460 +
461 + if (strcmp(part->name, "rootfs") == 0 && slave->registered) {
462 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
463 + if (ROOT_DEV == 0) {
464 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
465 + "set to be root filesystem\n");
466 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
467 }
468 - }
469 -
470 - if(parts[i].mtdp)
471 - { /* store the object pointer (caller may or may not register it */
472 - *parts[i].mtdp = &slave->mtd;
473 - slave->registered = 0;
474 - }
475 - else
476 - {
477 - /* register our partition */
478 - add_mtd_device(&slave->mtd);
479 - slave->registered = 1;
480 +#endif
481 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
482 + ret = split_rootfs_data(master, part, j);
483 + if (ret == 0)
484 + j++;
485 +#endif
486 }
487 }
488
This page took 0.065975 seconds and 5 git commands to generate.