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