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