90be97f1f5a8c8ce5cf737c3e52020e0e516a291
[pandora-kernel.git] / fs / partitions / msdos.c
1 /*
2  *  fs/partitions/msdos.c
3  *
4  *  Code extracted from drivers/block/genhd.c
5  *  Copyright (C) 1991-1998  Linus Torvalds
6  *
7  *  Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
8  *  in the early extended-partition checks and added DM partitions
9  *
10  *  Support for DiskManager v6.0x added by Mark Lord,
11  *  with information provided by OnTrack.  This now works for linux fdisk
12  *  and LILO, as well as loadlin and bootln.  Note that disks other than
13  *  /dev/hda *must* have a "DOS" type 0x51 partition in the first slot (hda1).
14  *
15  *  More flexible handling of extended partitions - aeb, 950831
16  *
17  *  Check partition table on IDE disks for common CHS translations
18  *
19  *  Re-organised Feb 1998 Russell King
20  */
21 #include <linux/msdos_fs.h>
22
23 #include "check.h"
24 #include "msdos.h"
25 #include "efi.h"
26
27 /*
28  * Many architectures don't like unaligned accesses, while
29  * the nr_sects and start_sect partition table entries are
30  * at a 2 (mod 4) address.
31  */
32 #include <asm/unaligned.h>
33
34 #define SYS_IND(p)      get_unaligned(&p->sys_ind)
35
36 static inline sector_t nr_sects(struct partition *p)
37 {
38         return (sector_t)get_unaligned_le32(&p->nr_sects);
39 }
40
41 static inline sector_t start_sect(struct partition *p)
42 {
43         return (sector_t)get_unaligned_le32(&p->start_sect);
44 }
45
46 static inline int is_extended_partition(struct partition *p)
47 {
48         return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
49                 SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
50                 SYS_IND(p) == LINUX_EXTENDED_PARTITION);
51 }
52
53 #define MSDOS_LABEL_MAGIC1      0x55
54 #define MSDOS_LABEL_MAGIC2      0xAA
55
56 static inline int
57 msdos_magic_present(unsigned char *p)
58 {
59         return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
60 }
61
62 /* Value is EBCDIC 'IBMA' */
63 #define AIX_LABEL_MAGIC1        0xC9
64 #define AIX_LABEL_MAGIC2        0xC2
65 #define AIX_LABEL_MAGIC3        0xD4
66 #define AIX_LABEL_MAGIC4        0xC1
67 static int aix_magic_present(unsigned char *p, struct block_device *bdev)
68 {
69         struct partition *pt = (struct partition *) (p + 0x1be);
70         Sector sect;
71         unsigned char *d;
72         int slot, ret = 0;
73
74         if (!(p[0] == AIX_LABEL_MAGIC1 &&
75                 p[1] == AIX_LABEL_MAGIC2 &&
76                 p[2] == AIX_LABEL_MAGIC3 &&
77                 p[3] == AIX_LABEL_MAGIC4))
78                 return 0;
79         /* Assume the partition table is valid if Linux partitions exists */
80         for (slot = 1; slot <= 4; slot++, pt++) {
81                 if (pt->sys_ind == LINUX_SWAP_PARTITION ||
82                         pt->sys_ind == LINUX_RAID_PARTITION ||
83                         pt->sys_ind == LINUX_DATA_PARTITION ||
84                         pt->sys_ind == LINUX_LVM_PARTITION ||
85                         is_extended_partition(pt))
86                         return 0;
87         }
88         d = read_dev_sector(bdev, 7, &sect);
89         if (d) {
90                 if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
91                         ret = 1;
92                 put_dev_sector(sect);
93         };
94         return ret;
95 }
96
97 /*
98  * Create devices for each logical partition in an extended partition.
99  * The logical partitions form a linked list, with each entry being
100  * a partition table with two entries.  The first entry
101  * is the real data partition (with a start relative to the partition
102  * table start).  The second is a pointer to the next logical partition
103  * (with a start relative to the entire extended partition).
104  * We do not create a Linux partition for the partition tables, but
105  * only for the actual data partitions.
106  */
107
108 static void
109 parse_extended(struct parsed_partitions *state, struct block_device *bdev,
110                         sector_t first_sector, sector_t first_size)
111 {
112         struct partition *p;
113         Sector sect;
114         unsigned char *data;
115         sector_t this_sector, this_size;
116         sector_t sector_size = bdev_logical_block_size(bdev) / 512;
117         int loopct = 0;         /* number of links followed
118                                    without finding a data partition */
119         int i;
120
121         this_sector = first_sector;
122         this_size = first_size;
123
124         while (1) {
125                 if (++loopct > 100)
126                         return;
127                 if (state->next == state->limit)
128                         return;
129                 data = read_dev_sector(bdev, this_sector, &sect);
130                 if (!data)
131                         return;
132
133                 if (!msdos_magic_present(data + 510))
134                         goto done; 
135
136                 p = (struct partition *) (data + 0x1be);
137
138                 /*
139                  * Usually, the first entry is the real data partition,
140                  * the 2nd entry is the next extended partition, or empty,
141                  * and the 3rd and 4th entries are unused.
142                  * However, DRDOS sometimes has the extended partition as
143                  * the first entry (when the data partition is empty),
144                  * and OS/2 seems to use all four entries.
145                  */
146
147                 /* 
148                  * First process the data partition(s)
149                  */
150                 for (i=0; i<4; i++, p++) {
151                         sector_t offs, size, next;
152                         if (!nr_sects(p) || is_extended_partition(p))
153                                 continue;
154
155                         /* Check the 3rd and 4th entries -
156                            these sometimes contain random garbage */
157                         offs = start_sect(p)*sector_size;
158                         size = nr_sects(p)*sector_size;
159                         next = this_sector + offs;
160                         if (i >= 2) {
161                                 if (offs + size > this_size)
162                                         continue;
163                                 if (next < first_sector)
164                                         continue;
165                                 if (next + size > first_sector + first_size)
166                                         continue;
167                         }
168
169                         put_partition(state, state->next, next, size);
170                         if (SYS_IND(p) == LINUX_RAID_PARTITION)
171                                 state->parts[state->next].flags = ADDPART_FLAG_RAID;
172                         loopct = 0;
173                         if (++state->next == state->limit)
174                                 goto done;
175                 }
176                 /*
177                  * Next, process the (first) extended partition, if present.
178                  * (So far, there seems to be no reason to make
179                  *  parse_extended()  recursive and allow a tree
180                  *  of extended partitions.)
181                  * It should be a link to the next logical partition.
182                  */
183                 p -= 4;
184                 for (i=0; i<4; i++, p++)
185                         if (nr_sects(p) && is_extended_partition(p))
186                                 break;
187                 if (i == 4)
188                         goto done;       /* nothing left to do */
189
190                 this_sector = first_sector + start_sect(p) * sector_size;
191                 this_size = nr_sects(p) * sector_size;
192                 put_dev_sector(sect);
193         }
194 done:
195         put_dev_sector(sect);
196 }
197
198 /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
199    indicates linux swap.  Be careful before believing this is Solaris. */
200
201 static void
202 parse_solaris_x86(struct parsed_partitions *state, struct block_device *bdev,
203                         sector_t offset, sector_t size, int origin)
204 {
205 #ifdef CONFIG_SOLARIS_X86_PARTITION
206         Sector sect;
207         struct solaris_x86_vtoc *v;
208         int i;
209         short max_nparts;
210
211         v = (struct solaris_x86_vtoc *)read_dev_sector(bdev, offset+1, &sect);
212         if (!v)
213                 return;
214         if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
215                 put_dev_sector(sect);
216                 return;
217         }
218         printk(" %s%d: <solaris:", state->name, origin);
219         if (le32_to_cpu(v->v_version) != 1) {
220                 printk("  cannot handle version %d vtoc>\n",
221                         le32_to_cpu(v->v_version));
222                 put_dev_sector(sect);
223                 return;
224         }
225         /* Ensure we can handle previous case of VTOC with 8 entries gracefully */
226         max_nparts = le16_to_cpu (v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
227         for (i=0; i<max_nparts && state->next<state->limit; i++) {
228                 struct solaris_x86_slice *s = &v->v_slice[i];
229                 if (s->s_size == 0)
230                         continue;
231                 printk(" [s%d]", i);
232                 /* solaris partitions are relative to current MS-DOS
233                  * one; must add the offset of the current partition */
234                 put_partition(state, state->next++,
235                                  le32_to_cpu(s->s_start)+offset,
236                                  le32_to_cpu(s->s_size));
237         }
238         put_dev_sector(sect);
239         printk(" >\n");
240 #endif
241 }
242
243 #if defined(CONFIG_BSD_DISKLABEL)
244 /* 
245  * Create devices for BSD partitions listed in a disklabel, under a
246  * dos-like partition. See parse_extended() for more information.
247  */
248 static void
249 parse_bsd(struct parsed_partitions *state, struct block_device *bdev,
250                 sector_t offset, sector_t size, int origin, char *flavour,
251                 int max_partitions)
252 {
253         Sector sect;
254         struct bsd_disklabel *l;
255         struct bsd_partition *p;
256
257         l = (struct bsd_disklabel *)read_dev_sector(bdev, offset+1, &sect);
258         if (!l)
259                 return;
260         if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
261                 put_dev_sector(sect);
262                 return;
263         }
264         printk(" %s%d: <%s:", state->name, origin, flavour);
265
266         if (le16_to_cpu(l->d_npartitions) < max_partitions)
267                 max_partitions = le16_to_cpu(l->d_npartitions);
268         for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
269                 sector_t bsd_start, bsd_size;
270
271                 if (state->next == state->limit)
272                         break;
273                 if (p->p_fstype == BSD_FS_UNUSED) 
274                         continue;
275                 bsd_start = le32_to_cpu(p->p_offset);
276                 bsd_size = le32_to_cpu(p->p_size);
277                 if (offset == bsd_start && size == bsd_size)
278                         /* full parent partition, we have it already */
279                         continue;
280                 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
281                         printk("bad subpartition - ignored\n");
282                         continue;
283                 }
284                 put_partition(state, state->next++, bsd_start, bsd_size);
285         }
286         put_dev_sector(sect);
287         if (le16_to_cpu(l->d_npartitions) > max_partitions)
288                 printk(" (ignored %d more)",
289                        le16_to_cpu(l->d_npartitions) - max_partitions);
290         printk(" >\n");
291 }
292 #endif
293
294 static void
295 parse_freebsd(struct parsed_partitions *state, struct block_device *bdev,
296                 sector_t offset, sector_t size, int origin)
297 {
298 #ifdef CONFIG_BSD_DISKLABEL
299         parse_bsd(state, bdev, offset, size, origin,
300                         "bsd", BSD_MAXPARTITIONS);
301 #endif
302 }
303
304 static void
305 parse_netbsd(struct parsed_partitions *state, struct block_device *bdev,
306                 sector_t offset, sector_t size, int origin)
307 {
308 #ifdef CONFIG_BSD_DISKLABEL
309         parse_bsd(state, bdev, offset, size, origin,
310                         "netbsd", BSD_MAXPARTITIONS);
311 #endif
312 }
313
314 static void
315 parse_openbsd(struct parsed_partitions *state, struct block_device *bdev,
316                 sector_t offset, sector_t size, int origin)
317 {
318 #ifdef CONFIG_BSD_DISKLABEL
319         parse_bsd(state, bdev, offset, size, origin,
320                         "openbsd", OPENBSD_MAXPARTITIONS);
321 #endif
322 }
323
324 /*
325  * Create devices for Unixware partitions listed in a disklabel, under a
326  * dos-like partition. See parse_extended() for more information.
327  */
328 static void
329 parse_unixware(struct parsed_partitions *state, struct block_device *bdev,
330                 sector_t offset, sector_t size, int origin)
331 {
332 #ifdef CONFIG_UNIXWARE_DISKLABEL
333         Sector sect;
334         struct unixware_disklabel *l;
335         struct unixware_slice *p;
336
337         l = (struct unixware_disklabel *)read_dev_sector(bdev, offset+29, &sect);
338         if (!l)
339                 return;
340         if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
341             le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
342                 put_dev_sector(sect);
343                 return;
344         }
345         printk(" %s%d: <unixware:", state->name, origin);
346         p = &l->vtoc.v_slice[1];
347         /* I omit the 0th slice as it is the same as whole disk. */
348         while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
349                 if (state->next == state->limit)
350                         break;
351
352                 if (p->s_label != UNIXWARE_FS_UNUSED)
353                         put_partition(state, state->next++,
354                                       le32_to_cpu(p->start_sect),
355                                       le32_to_cpu(p->nr_sects));
356                 p++;
357         }
358         put_dev_sector(sect);
359         printk(" >\n");
360 #endif
361 }
362
363 /*
364  * Minix 2.0.0/2.0.2 subpartition support.
365  * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
366  * Rajeev V. Pillai    <rajeevvp@yahoo.com>
367  */
368 static void
369 parse_minix(struct parsed_partitions *state, struct block_device *bdev,
370                 sector_t offset, sector_t size, int origin)
371 {
372 #ifdef CONFIG_MINIX_SUBPARTITION
373         Sector sect;
374         unsigned char *data;
375         struct partition *p;
376         int i;
377
378         data = read_dev_sector(bdev, offset, &sect);
379         if (!data)
380                 return;
381
382         p = (struct partition *)(data + 0x1be);
383
384         /* The first sector of a Minix partition can have either
385          * a secondary MBR describing its subpartitions, or
386          * the normal boot sector. */
387         if (msdos_magic_present (data + 510) &&
388             SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
389
390                 printk(" %s%d: <minix:", state->name, origin);
391                 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
392                         if (state->next == state->limit)
393                                 break;
394                         /* add each partition in use */
395                         if (SYS_IND(p) == MINIX_PARTITION)
396                                 put_partition(state, state->next++,
397                                               start_sect(p), nr_sects(p));
398                 }
399                 printk(" >\n");
400         }
401         put_dev_sector(sect);
402 #endif /* CONFIG_MINIX_SUBPARTITION */
403 }
404
405 static struct {
406         unsigned char id;
407         void (*parse)(struct parsed_partitions *, struct block_device *,
408                         sector_t, sector_t, int);
409 } subtypes[] = {
410         {FREEBSD_PARTITION, parse_freebsd},
411         {NETBSD_PARTITION, parse_netbsd},
412         {OPENBSD_PARTITION, parse_openbsd},
413         {MINIX_PARTITION, parse_minix},
414         {UNIXWARE_PARTITION, parse_unixware},
415         {SOLARIS_X86_PARTITION, parse_solaris_x86},
416         {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
417         {0, NULL},
418 };
419  
420 int msdos_partition(struct parsed_partitions *state, struct block_device *bdev)
421 {
422         sector_t sector_size = bdev_logical_block_size(bdev) / 512;
423         Sector sect;
424         unsigned char *data;
425         struct partition *p;
426         struct fat_boot_sector *fb;
427         int slot;
428
429         data = read_dev_sector(bdev, 0, &sect);
430         if (!data)
431                 return -1;
432         if (!msdos_magic_present(data + 510)) {
433                 put_dev_sector(sect);
434                 return 0;
435         }
436
437         if (aix_magic_present(data, bdev)) {
438                 put_dev_sector(sect);
439                 printk( " [AIX]");
440                 return 0;
441         }
442
443         /*
444          * Now that the 55aa signature is present, this is probably
445          * either the boot sector of a FAT filesystem or a DOS-type
446          * partition table. Reject this in case the boot indicator
447          * is not 0 or 0x80.
448          */
449         p = (struct partition *) (data + 0x1be);
450         for (slot = 1; slot <= 4; slot++, p++) {
451                 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
452                         /*
453                          * Even without a valid boot inidicator value
454                          * its still possible this is valid FAT filesystem
455                          * without a partition table.
456                          */
457                         fb = (struct fat_boot_sector *) data;
458                         if (slot == 1 && fb->reserved && fb->fats
459                                 && fat_valid_media(fb->media)) {
460                                 printk("\n");
461                                 put_dev_sector(sect);
462                                 return 1;
463                         } else {
464                                 put_dev_sector(sect);
465                                 return 0;
466                         }
467                 }
468         }
469
470 #ifdef CONFIG_EFI_PARTITION
471         p = (struct partition *) (data + 0x1be);
472         for (slot = 1 ; slot <= 4 ; slot++, p++) {
473                 /* If this is an EFI GPT disk, msdos should ignore it. */
474                 if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
475                         put_dev_sector(sect);
476                         return 0;
477                 }
478         }
479 #endif
480         p = (struct partition *) (data + 0x1be);
481
482         /*
483          * Look for partitions in two passes:
484          * First find the primary and DOS-type extended partitions.
485          * On the second pass look inside *BSD, Unixware and Solaris partitions.
486          */
487
488         state->next = 5;
489         for (slot = 1 ; slot <= 4 ; slot++, p++) {
490                 sector_t start = start_sect(p)*sector_size;
491                 sector_t size = nr_sects(p)*sector_size;
492                 if (!size)
493                         continue;
494                 if (is_extended_partition(p)) {
495                         /*
496                          * prevent someone doing mkfs or mkswap on an
497                          * extended partition, but leave room for LILO
498                          * FIXME: this uses one logical sector for > 512b
499                          * sector, although it may not be enough/proper.
500                          */
501                         sector_t n = 2;
502                         n = min(size, max(sector_size, n));
503                         put_partition(state, slot, start, n);
504
505                         printk(" <");
506                         parse_extended(state, bdev, start, size);
507                         printk(" >");
508                         continue;
509                 }
510                 put_partition(state, slot, start, size);
511                 if (SYS_IND(p) == LINUX_RAID_PARTITION)
512                         state->parts[slot].flags = 1;
513                 if (SYS_IND(p) == DM6_PARTITION)
514                         printk("[DM]");
515                 if (SYS_IND(p) == EZD_PARTITION)
516                         printk("[EZD]");
517         }
518
519         printk("\n");
520
521         /* second pass - output for each on a separate line */
522         p = (struct partition *) (0x1be + data);
523         for (slot = 1 ; slot <= 4 ; slot++, p++) {
524                 unsigned char id = SYS_IND(p);
525                 int n;
526
527                 if (!nr_sects(p))
528                         continue;
529
530                 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
531                         ;
532
533                 if (!subtypes[n].parse)
534                         continue;
535                 subtypes[n].parse(state, bdev, start_sect(p)*sector_size,
536                                                 nr_sects(p)*sector_size, slot);
537         }
538         put_dev_sector(sect);
539         return 1;
540 }