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 | */ |
Frank Seidel | 0607fd0 | 2008-04-28 02:16:31 -0700 | [diff] [blame] | 21 | #include <linux/msdos_fs.h> |
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 | |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 34 | #define SYS_IND(p) get_unaligned(&p->sys_ind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 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 |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 67 | static int aix_magic_present(struct parsed_partitions *state, unsigned char *p) |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 68 | { |
Olaf Hering | 4419d1a | 2007-02-10 01:45:47 -0800 | [diff] [blame] | 69 | struct partition *pt = (struct partition *) (p + 0x1be); |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 70 | Sector sect; |
| 71 | unsigned char *d; |
Olaf Hering | 4419d1a | 2007-02-10 01:45:47 -0800 | [diff] [blame] | 72 | int slot, ret = 0; |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 73 | |
Olaf Hering | a470e18 | 2007-02-10 01:45:48 -0800 | [diff] [blame] | 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)) |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 78 | return 0; |
Olaf Hering | 4419d1a | 2007-02-10 01:45:47 -0800 | [diff] [blame] | 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 | } |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 88 | d = read_part_sector(state, 7, §); |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 108 | static void parse_extended(struct parsed_partitions *state, |
| 109 | sector_t first_sector, sector_t first_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | struct partition *p; |
| 112 | Sector sect; |
| 113 | unsigned char *data; |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 114 | sector_t this_sector, this_size; |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 115 | sector_t sector_size = bdev_logical_block_size(state->bdev) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | int loopct = 0; /* number of links followed |
| 117 | without finding a data partition */ |
| 118 | int i; |
| 119 | |
| 120 | this_sector = first_sector; |
| 121 | this_size = first_size; |
| 122 | |
| 123 | while (1) { |
| 124 | if (++loopct > 100) |
| 125 | return; |
| 126 | if (state->next == state->limit) |
| 127 | return; |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 128 | data = read_part_sector(state, this_sector, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | if (!data) |
| 130 | return; |
| 131 | |
| 132 | if (!msdos_magic_present(data + 510)) |
| 133 | goto done; |
| 134 | |
| 135 | p = (struct partition *) (data + 0x1be); |
| 136 | |
| 137 | /* |
| 138 | * Usually, the first entry is the real data partition, |
| 139 | * the 2nd entry is the next extended partition, or empty, |
| 140 | * and the 3rd and 4th entries are unused. |
| 141 | * However, DRDOS sometimes has the extended partition as |
| 142 | * the first entry (when the data partition is empty), |
| 143 | * and OS/2 seems to use all four entries. |
| 144 | */ |
| 145 | |
| 146 | /* |
| 147 | * First process the data partition(s) |
| 148 | */ |
| 149 | for (i=0; i<4; i++, p++) { |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 150 | sector_t offs, size, next; |
| 151 | if (!nr_sects(p) || is_extended_partition(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | continue; |
| 153 | |
| 154 | /* Check the 3rd and 4th entries - |
| 155 | these sometimes contain random garbage */ |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 156 | offs = start_sect(p)*sector_size; |
| 157 | size = nr_sects(p)*sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | next = this_sector + offs; |
| 159 | if (i >= 2) { |
| 160 | if (offs + size > this_size) |
| 161 | continue; |
| 162 | if (next < first_sector) |
| 163 | continue; |
| 164 | if (next + size > first_sector + first_size) |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | put_partition(state, state->next, next, size); |
| 169 | if (SYS_IND(p) == LINUX_RAID_PARTITION) |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 170 | state->parts[state->next].flags = ADDPART_FLAG_RAID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | loopct = 0; |
| 172 | if (++state->next == state->limit) |
| 173 | goto done; |
| 174 | } |
| 175 | /* |
| 176 | * Next, process the (first) extended partition, if present. |
| 177 | * (So far, there seems to be no reason to make |
| 178 | * parse_extended() recursive and allow a tree |
| 179 | * of extended partitions.) |
| 180 | * It should be a link to the next logical partition. |
| 181 | */ |
| 182 | p -= 4; |
| 183 | for (i=0; i<4; i++, p++) |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 184 | if (nr_sects(p) && is_extended_partition(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | break; |
| 186 | if (i == 4) |
| 187 | goto done; /* nothing left to do */ |
| 188 | |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 189 | this_sector = first_sector + start_sect(p) * sector_size; |
| 190 | this_size = nr_sects(p) * sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | put_dev_sector(sect); |
| 192 | } |
| 193 | done: |
| 194 | put_dev_sector(sect); |
| 195 | } |
| 196 | |
| 197 | /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also |
| 198 | indicates linux swap. Be careful before believing this is Solaris. */ |
| 199 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 200 | static void parse_solaris_x86(struct parsed_partitions *state, |
| 201 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 203 | #ifdef CONFIG_SOLARIS_X86_PARTITION |
| 204 | Sector sect; |
| 205 | struct solaris_x86_vtoc *v; |
| 206 | int i; |
Mark Fortescue | b84d879 | 2007-07-25 18:30:08 -0700 | [diff] [blame] | 207 | short max_nparts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 209 | v = read_part_sector(state, offset + 1, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | if (!v) |
| 211 | return; |
| 212 | if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) { |
| 213 | put_dev_sector(sect); |
| 214 | return; |
| 215 | } |
| 216 | printk(" %s%d: <solaris:", state->name, origin); |
| 217 | if (le32_to_cpu(v->v_version) != 1) { |
| 218 | printk(" cannot handle version %d vtoc>\n", |
| 219 | le32_to_cpu(v->v_version)); |
| 220 | put_dev_sector(sect); |
| 221 | return; |
| 222 | } |
Mark Fortescue | b84d879 | 2007-07-25 18:30:08 -0700 | [diff] [blame] | 223 | /* Ensure we can handle previous case of VTOC with 8 entries gracefully */ |
| 224 | max_nparts = le16_to_cpu (v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8; |
| 225 | for (i=0; i<max_nparts && state->next<state->limit; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | struct solaris_x86_slice *s = &v->v_slice[i]; |
| 227 | if (s->s_size == 0) |
| 228 | continue; |
| 229 | printk(" [s%d]", i); |
| 230 | /* solaris partitions are relative to current MS-DOS |
| 231 | * one; must add the offset of the current partition */ |
| 232 | put_partition(state, state->next++, |
| 233 | le32_to_cpu(s->s_start)+offset, |
| 234 | le32_to_cpu(s->s_size)); |
| 235 | } |
| 236 | put_dev_sector(sect); |
| 237 | printk(" >\n"); |
| 238 | #endif |
| 239 | } |
| 240 | |
Adrian Bunk | 486fd40 | 2005-06-25 14:58:47 -0700 | [diff] [blame] | 241 | #if defined(CONFIG_BSD_DISKLABEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /* |
| 243 | * Create devices for BSD partitions listed in a disklabel, under a |
| 244 | * dos-like partition. See parse_extended() for more information. |
| 245 | */ |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 246 | static void parse_bsd(struct parsed_partitions *state, |
| 247 | sector_t offset, sector_t size, int origin, char *flavour, |
| 248 | int max_partitions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | Sector sect; |
| 251 | struct bsd_disklabel *l; |
| 252 | struct bsd_partition *p; |
| 253 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 254 | l = read_part_sector(state, offset + 1, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | if (!l) |
| 256 | return; |
| 257 | if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) { |
| 258 | put_dev_sector(sect); |
| 259 | return; |
| 260 | } |
| 261 | printk(" %s%d: <%s:", state->name, origin, flavour); |
| 262 | |
| 263 | if (le16_to_cpu(l->d_npartitions) < max_partitions) |
| 264 | max_partitions = le16_to_cpu(l->d_npartitions); |
| 265 | for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) { |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 266 | sector_t bsd_start, bsd_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | if (state->next == state->limit) |
| 269 | break; |
| 270 | if (p->p_fstype == BSD_FS_UNUSED) |
| 271 | continue; |
| 272 | bsd_start = le32_to_cpu(p->p_offset); |
| 273 | bsd_size = le32_to_cpu(p->p_size); |
| 274 | if (offset == bsd_start && size == bsd_size) |
| 275 | /* full parent partition, we have it already */ |
| 276 | continue; |
| 277 | if (offset > bsd_start || offset+size < bsd_start+bsd_size) { |
| 278 | printk("bad subpartition - ignored\n"); |
| 279 | continue; |
| 280 | } |
| 281 | put_partition(state, state->next++, bsd_start, bsd_size); |
| 282 | } |
| 283 | put_dev_sector(sect); |
| 284 | if (le16_to_cpu(l->d_npartitions) > max_partitions) |
| 285 | printk(" (ignored %d more)", |
| 286 | le16_to_cpu(l->d_npartitions) - max_partitions); |
| 287 | printk(" >\n"); |
| 288 | } |
| 289 | #endif |
| 290 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 291 | static void parse_freebsd(struct parsed_partitions *state, |
| 292 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
| 294 | #ifdef CONFIG_BSD_DISKLABEL |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 295 | parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | #endif |
| 297 | } |
| 298 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 299 | static void parse_netbsd(struct parsed_partitions *state, |
| 300 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
| 302 | #ifdef CONFIG_BSD_DISKLABEL |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 303 | parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | #endif |
| 305 | } |
| 306 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 307 | static void parse_openbsd(struct parsed_partitions *state, |
| 308 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
| 310 | #ifdef CONFIG_BSD_DISKLABEL |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 311 | parse_bsd(state, offset, size, origin, "openbsd", |
| 312 | OPENBSD_MAXPARTITIONS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | #endif |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Create devices for Unixware partitions listed in a disklabel, under a |
| 318 | * dos-like partition. See parse_extended() for more information. |
| 319 | */ |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 320 | static void parse_unixware(struct parsed_partitions *state, |
| 321 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
| 323 | #ifdef CONFIG_UNIXWARE_DISKLABEL |
| 324 | Sector sect; |
| 325 | struct unixware_disklabel *l; |
| 326 | struct unixware_slice *p; |
| 327 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 328 | l = read_part_sector(state, offset + 29, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | if (!l) |
| 330 | return; |
| 331 | if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC || |
| 332 | le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) { |
| 333 | put_dev_sector(sect); |
| 334 | return; |
| 335 | } |
| 336 | printk(" %s%d: <unixware:", state->name, origin); |
| 337 | p = &l->vtoc.v_slice[1]; |
| 338 | /* I omit the 0th slice as it is the same as whole disk. */ |
| 339 | while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) { |
| 340 | if (state->next == state->limit) |
| 341 | break; |
| 342 | |
| 343 | if (p->s_label != UNIXWARE_FS_UNUSED) |
| 344 | put_partition(state, state->next++, |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 345 | le32_to_cpu(p->start_sect), |
| 346 | le32_to_cpu(p->nr_sects)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | p++; |
| 348 | } |
| 349 | put_dev_sector(sect); |
| 350 | printk(" >\n"); |
| 351 | #endif |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * Minix 2.0.0/2.0.2 subpartition support. |
| 356 | * Anand Krishnamurthy <anandk@wiproge.med.ge.com> |
| 357 | * Rajeev V. Pillai <rajeevvp@yahoo.com> |
| 358 | */ |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 359 | static void parse_minix(struct parsed_partitions *state, |
| 360 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
| 362 | #ifdef CONFIG_MINIX_SUBPARTITION |
| 363 | Sector sect; |
| 364 | unsigned char *data; |
| 365 | struct partition *p; |
| 366 | int i; |
| 367 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 368 | data = read_part_sector(state, offset, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | if (!data) |
| 370 | return; |
| 371 | |
| 372 | p = (struct partition *)(data + 0x1be); |
| 373 | |
| 374 | /* The first sector of a Minix partition can have either |
| 375 | * a secondary MBR describing its subpartitions, or |
| 376 | * the normal boot sector. */ |
| 377 | if (msdos_magic_present (data + 510) && |
| 378 | SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */ |
| 379 | |
| 380 | printk(" %s%d: <minix:", state->name, origin); |
| 381 | for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) { |
| 382 | if (state->next == state->limit) |
| 383 | break; |
| 384 | /* add each partition in use */ |
| 385 | if (SYS_IND(p) == MINIX_PARTITION) |
| 386 | put_partition(state, state->next++, |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 387 | start_sect(p), nr_sects(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | printk(" >\n"); |
| 390 | } |
| 391 | put_dev_sector(sect); |
| 392 | #endif /* CONFIG_MINIX_SUBPARTITION */ |
| 393 | } |
| 394 | |
| 395 | static struct { |
| 396 | unsigned char id; |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 397 | void (*parse)(struct parsed_partitions *, sector_t, sector_t, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } subtypes[] = { |
| 399 | {FREEBSD_PARTITION, parse_freebsd}, |
| 400 | {NETBSD_PARTITION, parse_netbsd}, |
| 401 | {OPENBSD_PARTITION, parse_openbsd}, |
| 402 | {MINIX_PARTITION, parse_minix}, |
| 403 | {UNIXWARE_PARTITION, parse_unixware}, |
| 404 | {SOLARIS_X86_PARTITION, parse_solaris_x86}, |
| 405 | {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86}, |
| 406 | {0, NULL}, |
| 407 | }; |
| 408 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 409 | int msdos_partition(struct parsed_partitions *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 411 | sector_t sector_size = bdev_logical_block_size(state->bdev) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | Sector sect; |
| 413 | unsigned char *data; |
| 414 | struct partition *p; |
Frank Seidel | 0607fd0 | 2008-04-28 02:16:31 -0700 | [diff] [blame] | 415 | struct fat_boot_sector *fb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | int slot; |
| 417 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 418 | data = read_part_sector(state, 0, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | if (!data) |
| 420 | return -1; |
| 421 | if (!msdos_magic_present(data + 510)) { |
| 422 | put_dev_sector(sect); |
| 423 | return 0; |
| 424 | } |
| 425 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 426 | if (aix_magic_present(state, data)) { |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 427 | put_dev_sector(sect); |
| 428 | printk( " [AIX]"); |
| 429 | return 0; |
| 430 | } |
| 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | /* |
| 433 | * Now that the 55aa signature is present, this is probably |
| 434 | * either the boot sector of a FAT filesystem or a DOS-type |
| 435 | * partition table. Reject this in case the boot indicator |
| 436 | * is not 0 or 0x80. |
| 437 | */ |
| 438 | p = (struct partition *) (data + 0x1be); |
| 439 | for (slot = 1; slot <= 4; slot++, p++) { |
| 440 | if (p->boot_ind != 0 && p->boot_ind != 0x80) { |
Frank Seidel | 0607fd0 | 2008-04-28 02:16:31 -0700 | [diff] [blame] | 441 | /* |
| 442 | * Even without a valid boot inidicator value |
| 443 | * its still possible this is valid FAT filesystem |
| 444 | * without a partition table. |
| 445 | */ |
| 446 | fb = (struct fat_boot_sector *) data; |
| 447 | if (slot == 1 && fb->reserved && fb->fats |
| 448 | && fat_valid_media(fb->media)) { |
| 449 | printk("\n"); |
| 450 | put_dev_sector(sect); |
| 451 | return 1; |
| 452 | } else { |
| 453 | put_dev_sector(sect); |
| 454 | return 0; |
| 455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
| 459 | #ifdef CONFIG_EFI_PARTITION |
| 460 | p = (struct partition *) (data + 0x1be); |
| 461 | for (slot = 1 ; slot <= 4 ; slot++, p++) { |
| 462 | /* If this is an EFI GPT disk, msdos should ignore it. */ |
| 463 | if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) { |
| 464 | put_dev_sector(sect); |
| 465 | return 0; |
| 466 | } |
| 467 | } |
| 468 | #endif |
| 469 | p = (struct partition *) (data + 0x1be); |
| 470 | |
| 471 | /* |
| 472 | * Look for partitions in two passes: |
| 473 | * First find the primary and DOS-type extended partitions. |
| 474 | * On the second pass look inside *BSD, Unixware and Solaris partitions. |
| 475 | */ |
| 476 | |
| 477 | state->next = 5; |
| 478 | for (slot = 1 ; slot <= 4 ; slot++, p++) { |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 479 | sector_t start = start_sect(p)*sector_size; |
| 480 | sector_t size = nr_sects(p)*sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | if (!size) |
| 482 | continue; |
| 483 | if (is_extended_partition(p)) { |
OGAWA Hirofumi | 8e0cc81 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 484 | /* |
| 485 | * prevent someone doing mkfs or mkswap on an |
| 486 | * extended partition, but leave room for LILO |
| 487 | * FIXME: this uses one logical sector for > 512b |
| 488 | * sector, although it may not be enough/proper. |
| 489 | */ |
| 490 | sector_t n = 2; |
| 491 | n = min(size, max(sector_size, n)); |
| 492 | put_partition(state, slot, start, n); |
| 493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | printk(" <"); |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 495 | parse_extended(state, start, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | printk(" >"); |
| 497 | continue; |
| 498 | } |
| 499 | put_partition(state, slot, start, size); |
| 500 | if (SYS_IND(p) == LINUX_RAID_PARTITION) |
Cesar Eduardo Barros | cc91062 | 2010-04-17 19:28:09 -0300 | [diff] [blame] | 501 | state->parts[slot].flags = ADDPART_FLAG_RAID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | if (SYS_IND(p) == DM6_PARTITION) |
| 503 | printk("[DM]"); |
| 504 | if (SYS_IND(p) == EZD_PARTITION) |
| 505 | printk("[EZD]"); |
| 506 | } |
| 507 | |
| 508 | printk("\n"); |
| 509 | |
| 510 | /* second pass - output for each on a separate line */ |
| 511 | p = (struct partition *) (0x1be + data); |
| 512 | for (slot = 1 ; slot <= 4 ; slot++, p++) { |
| 513 | unsigned char id = SYS_IND(p); |
| 514 | int n; |
| 515 | |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 516 | if (!nr_sects(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | continue; |
| 518 | |
| 519 | for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++) |
| 520 | ; |
| 521 | |
| 522 | if (!subtypes[n].parse) |
| 523 | continue; |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 524 | subtypes[n].parse(state, start_sect(p) * sector_size, |
| 525 | nr_sects(p) * sector_size, slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
| 527 | put_dev_sector(sect); |
| 528 | return 1; |
| 529 | } |