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