finally move buildroot-ng to trunk
[openwrt.git] / target / linux / rb532-2.6 / patches / 130-custom_partitions.patch
1 diff -urN linux.old/fs/partitions/check.c linux.dev/fs/partitions/check.c
2 --- linux.old/fs/partitions/check.c     2006-05-31 02:31:44.000000000 +0200
3 +++ linux.dev/fs/partitions/check.c     2006-06-15 01:27:17.000000000 +0200
4 @@ -29,6 +29,7 @@
5  #include "ldm.h"
6  #include "mac.h"
7  #include "msdos.h"
8 +#include "openwrt.h"
9  #include "osf.h"
10  #include "sgi.h"
11  #include "sun.h"
12 @@ -48,6 +49,9 @@
13          * Probe partition formats with tables at disk address 0
14          * that also have an ADFS boot block at 0xdc0.
15          */
16 +#ifdef CONFIG_OPENWRT_PARTITION
17 +       openwrt_partition,
18 +#endif
19  #ifdef CONFIG_ACORN_PARTITION_ICS
20         adfspart_check_ICS,
21  #endif
22 diff -urN linux.old/fs/partitions/Kconfig linux.dev/fs/partitions/Kconfig
23 --- linux.old/fs/partitions/Kconfig     2006-05-31 02:31:44.000000000 +0200
24 +++ linux.dev/fs/partitions/Kconfig     2006-06-15 01:27:17.000000000 +0200
25 @@ -14,6 +14,12 @@
26  
27           If unsure, say N.
28  
29 +config OPENWRT_PARTITION
30 +       bool "OpenWrt partition support" if PARTITION_ADVANCED
31 +       default y
32 +       help
33 +         Support the custom OpenWrt partition map
34 +
35  config ACORN_PARTITION
36         bool "Acorn partition support" if PARTITION_ADVANCED
37         default y if ARCH_ACORN
38 diff -urN linux.old/fs/partitions/Makefile linux.dev/fs/partitions/Makefile
39 --- linux.old/fs/partitions/Makefile    2006-05-31 02:31:44.000000000 +0200
40 +++ linux.dev/fs/partitions/Makefile    2006-06-15 01:27:17.000000000 +0200
41 @@ -11,6 +11,7 @@
42  obj-$(CONFIG_MAC_PARTITION) += mac.o
43  obj-$(CONFIG_LDM_PARTITION) += ldm.o
44  obj-$(CONFIG_MSDOS_PARTITION) += msdos.o
45 +obj-$(CONFIG_OPENWRT_PARTITION) += openwrt.o
46  obj-$(CONFIG_OSF_PARTITION) += osf.o
47  obj-$(CONFIG_SGI_PARTITION) += sgi.o
48  obj-$(CONFIG_SUN_PARTITION) += sun.o
49 diff -urN linux.old/fs/partitions/openwrt.c linux.dev/fs/partitions/openwrt.c
50 --- linux.old/fs/partitions/openwrt.c   1970-01-01 01:00:00.000000000 +0100
51 +++ linux.dev/fs/partitions/openwrt.c   2006-06-15 01:27:17.000000000 +0200
52 @@ -0,0 +1,249 @@
53 +/*
54 + *  fs/partitions/openwrt.c
55 + *
56 + *  Code extracted from drivers/block/genhd.c
57 + *  and fs/partitions/msdos.c
58 + *
59 + *  Copyright (C) 2006       Felix Fietkau <nbd@openwrt.org>
60 + *  Copyright (C) 1991-1998  Linus Torvalds
61 + *
62 + *  Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
63 + *  in the early extended-partition checks and added DM partitions
64 + *
65 + *  Support for DiskManager v6.0x added by Mark Lord,
66 + *  with information provided by OnTrack.  This now works for linux fdisk
67 + *  and LILO, as well as loadlin and bootln.  Note that disks other than
68 + *  /dev/hda *must* have a "DOS" type 0x51 partition in the first slot (hda1).
69 + *
70 + *  More flexible handling of extended partitions - aeb, 950831
71 + *
72 + *  Check partition table on IDE disks for common CHS translations
73 + *
74 + *  Re-organised Feb 1998 Russell King
75 + */
76 +
77 +#include <linux/config.h>
78 +
79 +#include "check.h"
80 +#include "openwrt.h"
81 +
82 +/*
83 + * Many architectures don't like unaligned accesses, while
84 + * the nr_sects and start_sect partition table entries are
85 + * at a 2 (mod 4) address.
86 + */
87 +#include <asm/unaligned.h>
88 +
89 +#define SYS_IND(p)     (get_unaligned(&p->sys_ind))
90 +#define NR_SECTS(p)    ({ __typeof__(p->nr_sects) __a =        \
91 +                               get_unaligned(&p->nr_sects);    \
92 +                               le32_to_cpu(__a); \
93 +                       })
94 +
95 +#define START_SECT(p)  ({ __typeof__(p->start_sect) __a =      \
96 +                               get_unaligned(&p->start_sect);  \
97 +                               le32_to_cpu(__a); \
98 +                       })
99 +
100 +static inline int is_extended_partition(struct partition *p)
101 +{
102 +       return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
103 +               SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
104 +               SYS_IND(p) == LINUX_EXTENDED_PARTITION);
105 +}
106 +
107 +#define MSDOS_LABEL_MAGIC1     0x55
108 +#define MSDOS_LABEL_MAGIC2     0xAA
109 +
110 +static inline int
111 +msdos_magic_present(unsigned char *p)
112 +{
113 +       return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
114 +}
115 +
116 +static inline int
117 +openwrt_magic_present(unsigned char *p)
118 +{
119 +       return (p[0] == 'O' && 
120 +                       p[1] == 'W' &&
121 +                       p[2] == 'R' &&
122 +                       p[3] == 'T');
123 +}
124 +
125 +/*
126 + * Create devices for each logical partition in an extended partition.
127 + * The logical partitions form a linked list, with each entry being
128 + * a partition table with two entries.  The first entry
129 + * is the real data partition (with a start relative to the partition
130 + * table start).  The second is a pointer to the next logical partition
131 + * (with a start relative to the entire extended partition).
132 + * We do not create a Linux partition for the partition tables, but
133 + * only for the actual data partitions.
134 + */
135 +
136 +static void
137 +parse_extended(struct parsed_partitions *state, struct block_device *bdev,
138 +                       u32 first_sector, u32 first_size)
139 +{
140 +       struct partition *p;
141 +       Sector sect;
142 +       unsigned char *data;
143 +       u32 this_sector, this_size;
144 +       int sector_size = bdev_hardsect_size(bdev) / 512;
145 +       int loopct = 0;         /* number of links followed
146 +                                  without finding a data partition */
147 +       int i;
148 +
149 +       this_sector = first_sector;
150 +       this_size = first_size;
151 +
152 +       while (1) {
153 +               if (++loopct > 100)
154 +                       return;
155 +               if (state->next == state->limit)
156 +                       return;
157 +               data = read_dev_sector(bdev, this_sector, &sect);
158 +               if (!data)
159 +                       return;
160 +
161 +               if (!msdos_magic_present(data + 510))
162 +                       goto done; 
163 +
164 +               p = (struct partition *) (data + 0x1be);
165 +
166 +               /*
167 +                * Usually, the first entry is the real data partition,
168 +                * the 2nd entry is the next extended partition, or empty,
169 +                * and the 3rd and 4th entries are unused.
170 +                * However, DRDOS sometimes has the extended partition as
171 +                * the first entry (when the data partition is empty),
172 +                * and OS/2 seems to use all four entries.
173 +                */
174 +
175 +               /* 
176 +                * First process the data partition(s)
177 +                */
178 +               for (i=0; i<4; i++, p++) {
179 +                       u32 offs, size, next;
180 +                       if (!NR_SECTS(p) || is_extended_partition(p))
181 +                               continue;
182 +
183 +                       /* Check the 3rd and 4th entries -
184 +                          these sometimes contain random garbage */
185 +                       offs = START_SECT(p)*sector_size;
186 +                       size = NR_SECTS(p)*sector_size;
187 +                       next = this_sector + offs;
188 +                       if (i >= 2) {
189 +                               if (offs + size > this_size)
190 +                                       continue;
191 +                               if (next < first_sector)
192 +                                       continue;
193 +                               if (next + size > first_sector + first_size)
194 +                                       continue;
195 +                       }
196 +
197 +                       put_partition(state, state->next, next, size);
198 +                       if (SYS_IND(p) == LINUX_RAID_PARTITION)
199 +                               state->parts[state->next].flags = 1;
200 +                       loopct = 0;
201 +                       if (++state->next == state->limit)
202 +                               goto done;
203 +               }
204 +               /*
205 +                * Next, process the (first) extended partition, if present.
206 +                * (So far, there seems to be no reason to make
207 +                *  parse_extended()  recursive and allow a tree
208 +                *  of extended partitions.)
209 +                * It should be a link to the next logical partition.
210 +                */
211 +               p -= 4;
212 +               for (i=0; i<4; i++, p++)
213 +                       if (NR_SECTS(p) && is_extended_partition(p))
214 +                               break;
215 +               if (i == 4)
216 +                       goto done;       /* nothing left to do */
217 +
218 +               this_sector = first_sector + START_SECT(p) * sector_size;
219 +               this_size = NR_SECTS(p) * sector_size;
220 +               put_dev_sector(sect);
221 +       }
222 +done:
223 +       put_dev_sector(sect);
224 +}
225 +
226 +
227 +int openwrt_partition(struct parsed_partitions *state, struct block_device *bdev)
228 +{
229 +       int sector_size = bdev_hardsect_size(bdev) / 512;
230 +       Sector sect;
231 +       unsigned char *data;
232 +       struct partition *p;
233 +       int slot;
234 +       u32 last_block = 0;
235 +       u32 size = 0;
236 +       int firstfree = 5;
237 +
238 +       data = read_dev_sector(bdev, 0, &sect);
239 +       if (!data)
240 +               return -1;
241 +       if (!openwrt_magic_present(data)) {
242 +               printk("No OpenWrt partition table detected\n");        
243 +               put_dev_sector(sect);
244 +               return 0;
245 +       }
246 +
247 +       /*
248 +        * Now that the 55aa signature is present, this is probably
249 +        * either the boot sector of a FAT filesystem or a DOS-type
250 +        * partition table. Reject this in case the boot indicator
251 +        * is not 0 or 0x80.
252 +        */
253 +       p = (struct partition *) (data + 0x1be);
254 +       for (slot = 1; slot <= 4; slot++, p++) {
255 +               if (p->boot_ind != 0 && p->boot_ind != 0x80) {
256 +                       put_dev_sector(sect);
257 +                       return 0;
258 +               }
259 +       }
260 +
261 +       p = (struct partition *) (data + 0x1be);
262 +
263 +       /*
264 +        * Look for partitions in two passes:
265 +        * First find the primary and DOS-type extended partitions.
266 +        */
267 +
268 +       state->next = 6;
269 +       for (slot = 1 ; slot <= 4 ; slot++, p++) {
270 +               u32 start = START_SECT(p)*sector_size;
271 +               size = NR_SECTS(p)*sector_size;
272 +               if (!size) {
273 +                       if (firstfree > slot)
274 +                               firstfree = slot;
275 +
276 +                       continue;
277 +               }
278 +               if (is_extended_partition(p)) {
279 +                       /* prevent someone doing mkfs or mkswap on an
280 +                          extended partition, but leave room for LILO */
281 +                       last_block = start + size;
282 +                       put_partition(state, slot, start, size == 1 ? 1 : 2);
283 +                       printk(" <");
284 +                       parse_extended(state, bdev, start, size);
285 +                       printk(" >");
286 +                       continue;
287 +               }
288 +               if ((start + size) > get_capacity(bdev->bd_disk))
289 +                       size = get_capacity(bdev->bd_disk) - start;
290 +               last_block = start + size;
291 +               put_partition(state, slot, start, size);
292 +       }
293 +       if (last_block + 1024 < (size = get_capacity(bdev->bd_disk)))
294 +               put_partition(state, firstfree, last_block, size - last_block);
295 +
296 +       printk("\n");
297 +
298 +       put_dev_sector(sect);
299 +       return 1;
300 +}
301 +
302 diff -urN linux.old/fs/partitions/openwrt.h linux.dev/fs/partitions/openwrt.h
303 --- linux.old/fs/partitions/openwrt.h   1970-01-01 01:00:00.000000000 +0100
304 +++ linux.dev/fs/partitions/openwrt.h   2006-06-15 01:27:17.000000000 +0200
305 @@ -0,0 +1,6 @@
306 +/*
307 + *  fs/partitions/openwrt.h
308 + */
309 +
310 +int openwrt_partition(struct parsed_partitions *state, struct block_device *bdev);
311 +