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