Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * fs/partitions/msdos.c |
| 4 | * |
| 5 | * Code extracted from drivers/block/genhd.c |
| 6 | * Copyright (C) 1991-1998 Linus Torvalds |
| 7 | * |
| 8 | * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug |
| 9 | * in the early extended-partition checks and added DM partitions |
| 10 | * |
| 11 | * Support for DiskManager v6.0x added by Mark Lord, |
| 12 | * with information provided by OnTrack. This now works for linux fdisk |
| 13 | * and LILO, as well as loadlin and bootln. Note that disks other than |
| 14 | * /dev/hda *must* have a "DOS" type 0x51 partition in the first slot (hda1). |
| 15 | * |
| 16 | * More flexible handling of extended partitions - aeb, 950831 |
| 17 | * |
| 18 | * Check partition table on IDE disks for common CHS translations |
| 19 | * |
| 20 | * Re-organised Feb 1998 Russell King |
Christoph Hellwig | 3f4fc59 | 2020-03-24 08:25:29 +0100 | [diff] [blame] | 21 | * |
| 22 | * BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il> |
| 23 | * updated by Marc Espie <Marc.Espie@openbsd.org> |
| 24 | * |
| 25 | * Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl> |
| 26 | * and Krzysztof G. Baranowski <kgb@knm.org.pl> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | */ |
Frank Seidel | 0607fd0 | 2008-04-28 02:16:31 -0700 | [diff] [blame] | 28 | #include <linux/msdos_fs.h> |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 29 | #include <linux/msdos_partition.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #include "check.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "efi.h" |
| 33 | |
| 34 | /* |
| 35 | * Many architectures don't like unaligned accesses, while |
| 36 | * the nr_sects and start_sect partition table entries are |
| 37 | * at a 2 (mod 4) address. |
| 38 | */ |
| 39 | #include <asm/unaligned.h> |
| 40 | |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 41 | static inline sector_t nr_sects(struct msdos_partition *p) |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 42 | { |
| 43 | return (sector_t)get_unaligned_le32(&p->nr_sects); |
| 44 | } |
| 45 | |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 46 | static inline sector_t start_sect(struct msdos_partition *p) |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 47 | { |
| 48 | return (sector_t)get_unaligned_le32(&p->start_sect); |
| 49 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 51 | static inline int is_extended_partition(struct msdos_partition *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Arnd Bergmann | 5f69841 | 2021-05-08 00:07:53 +0200 | [diff] [blame] | 53 | return (p->sys_ind == DOS_EXTENDED_PARTITION || |
| 54 | p->sys_ind == WIN98_EXTENDED_PARTITION || |
| 55 | p->sys_ind == LINUX_EXTENDED_PARTITION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | #define MSDOS_LABEL_MAGIC1 0x55 |
| 59 | #define MSDOS_LABEL_MAGIC2 0xAA |
| 60 | |
| 61 | static inline int |
| 62 | msdos_magic_present(unsigned char *p) |
| 63 | { |
| 64 | return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2); |
| 65 | } |
| 66 | |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 67 | /* Value is EBCDIC 'IBMA' */ |
| 68 | #define AIX_LABEL_MAGIC1 0xC9 |
| 69 | #define AIX_LABEL_MAGIC2 0xC2 |
| 70 | #define AIX_LABEL_MAGIC3 0xD4 |
| 71 | #define AIX_LABEL_MAGIC4 0xC1 |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 72 | static int aix_magic_present(struct parsed_partitions *state, unsigned char *p) |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 73 | { |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 74 | struct msdos_partition *pt = (struct msdos_partition *) (p + 0x1be); |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 75 | Sector sect; |
| 76 | unsigned char *d; |
Olaf Hering | 4419d1a | 2007-02-10 01:45:47 -0800 | [diff] [blame] | 77 | int slot, ret = 0; |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 78 | |
Olaf Hering | a470e18 | 2007-02-10 01:45:48 -0800 | [diff] [blame] | 79 | if (!(p[0] == AIX_LABEL_MAGIC1 && |
| 80 | p[1] == AIX_LABEL_MAGIC2 && |
| 81 | p[2] == AIX_LABEL_MAGIC3 && |
| 82 | p[3] == AIX_LABEL_MAGIC4)) |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 83 | return 0; |
Christoph Hellwig | cb0ab52 | 2020-03-24 08:25:28 +0100 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * Assume the partition table is valid if Linux partitions exists. |
| 87 | * Note that old Solaris/x86 partitions use the same indicator as |
| 88 | * Linux swap partitions, so we consider that a Linux partition as |
| 89 | * well. |
| 90 | */ |
Olaf Hering | 4419d1a | 2007-02-10 01:45:47 -0800 | [diff] [blame] | 91 | for (slot = 1; slot <= 4; slot++, pt++) { |
Christoph Hellwig | cb0ab52 | 2020-03-24 08:25:28 +0100 | [diff] [blame] | 92 | if (pt->sys_ind == SOLARIS_X86_PARTITION || |
| 93 | pt->sys_ind == LINUX_RAID_PARTITION || |
| 94 | pt->sys_ind == LINUX_DATA_PARTITION || |
| 95 | pt->sys_ind == LINUX_LVM_PARTITION || |
| 96 | is_extended_partition(pt)) |
Olaf Hering | 4419d1a | 2007-02-10 01:45:47 -0800 | [diff] [blame] | 97 | return 0; |
| 98 | } |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 99 | d = read_part_sector(state, 7, §); |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 100 | if (d) { |
| 101 | if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M') |
| 102 | ret = 1; |
| 103 | put_dev_sector(sect); |
Philippe De Muyter | 1d04f3c | 2013-07-08 16:01:28 -0700 | [diff] [blame] | 104 | } |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 105 | return ret; |
| 106 | } |
| 107 | |
Stephen Warren | d33b98f | 2012-11-08 16:12:28 -0800 | [diff] [blame] | 108 | static void set_info(struct parsed_partitions *state, int slot, |
| 109 | u32 disksig) |
| 110 | { |
| 111 | struct partition_meta_info *info = &state->parts[slot].info; |
| 112 | |
| 113 | snprintf(info->uuid, sizeof(info->uuid), "%08x-%02x", disksig, |
| 114 | slot); |
| 115 | info->volname[0] = 0; |
| 116 | state->parts[slot].has_info = true; |
| 117 | } |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | /* |
| 120 | * Create devices for each logical partition in an extended partition. |
| 121 | * The logical partitions form a linked list, with each entry being |
| 122 | * a partition table with two entries. The first entry |
| 123 | * is the real data partition (with a start relative to the partition |
| 124 | * table start). The second is a pointer to the next logical partition |
| 125 | * (with a start relative to the entire extended partition). |
| 126 | * We do not create a Linux partition for the partition tables, but |
| 127 | * only for the actual data partitions. |
| 128 | */ |
| 129 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 130 | static void parse_extended(struct parsed_partitions *state, |
Stephen Warren | d33b98f | 2012-11-08 16:12:28 -0800 | [diff] [blame] | 131 | sector_t first_sector, sector_t first_size, |
| 132 | u32 disksig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 134 | struct msdos_partition *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | Sector sect; |
| 136 | unsigned char *data; |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 137 | sector_t this_sector, this_size; |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 138 | sector_t sector_size = bdev_logical_block_size(state->bdev) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | int loopct = 0; /* number of links followed |
| 140 | without finding a data partition */ |
| 141 | int i; |
| 142 | |
| 143 | this_sector = first_sector; |
| 144 | this_size = first_size; |
| 145 | |
| 146 | while (1) { |
| 147 | if (++loopct > 100) |
| 148 | return; |
| 149 | if (state->next == state->limit) |
| 150 | return; |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 151 | data = read_part_sector(state, this_sector, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | if (!data) |
| 153 | return; |
| 154 | |
| 155 | if (!msdos_magic_present(data + 510)) |
Philippe De Muyter | 1d04f3c | 2013-07-08 16:01:28 -0700 | [diff] [blame] | 156 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 158 | p = (struct msdos_partition *) (data + 0x1be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * Usually, the first entry is the real data partition, |
| 162 | * the 2nd entry is the next extended partition, or empty, |
| 163 | * and the 3rd and 4th entries are unused. |
| 164 | * However, DRDOS sometimes has the extended partition as |
| 165 | * the first entry (when the data partition is empty), |
| 166 | * and OS/2 seems to use all four entries. |
| 167 | */ |
| 168 | |
Philippe De Muyter | 1d04f3c | 2013-07-08 16:01:28 -0700 | [diff] [blame] | 169 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | * First process the data partition(s) |
| 171 | */ |
Fabian Frederick | dce14c2 | 2014-06-12 20:16:57 +0200 | [diff] [blame] | 172 | for (i = 0; i < 4; i++, p++) { |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 173 | sector_t offs, size, next; |
Fabian Frederick | dce14c2 | 2014-06-12 20:16:57 +0200 | [diff] [blame] | 174 | |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 175 | if (!nr_sects(p) || is_extended_partition(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | continue; |
| 177 | |
| 178 | /* Check the 3rd and 4th entries - |
| 179 | these sometimes contain random garbage */ |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 180 | offs = start_sect(p)*sector_size; |
| 181 | size = nr_sects(p)*sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | next = this_sector + offs; |
| 183 | if (i >= 2) { |
| 184 | if (offs + size > this_size) |
| 185 | continue; |
| 186 | if (next < first_sector) |
| 187 | continue; |
| 188 | if (next + size > first_sector + first_size) |
| 189 | continue; |
| 190 | } |
| 191 | |
| 192 | put_partition(state, state->next, next, size); |
Stephen Warren | d33b98f | 2012-11-08 16:12:28 -0800 | [diff] [blame] | 193 | set_info(state, state->next, disksig); |
Arnd Bergmann | 5f69841 | 2021-05-08 00:07:53 +0200 | [diff] [blame] | 194 | if (p->sys_ind == LINUX_RAID_PARTITION) |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 195 | state->parts[state->next].flags = ADDPART_FLAG_RAID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | loopct = 0; |
| 197 | if (++state->next == state->limit) |
| 198 | goto done; |
| 199 | } |
| 200 | /* |
| 201 | * Next, process the (first) extended partition, if present. |
| 202 | * (So far, there seems to be no reason to make |
| 203 | * parse_extended() recursive and allow a tree |
| 204 | * of extended partitions.) |
| 205 | * It should be a link to the next logical partition. |
| 206 | */ |
| 207 | p -= 4; |
Fabian Frederick | dce14c2 | 2014-06-12 20:16:57 +0200 | [diff] [blame] | 208 | for (i = 0; i < 4; i++, p++) |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 209 | if (nr_sects(p) && is_extended_partition(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | break; |
| 211 | if (i == 4) |
| 212 | goto done; /* nothing left to do */ |
| 213 | |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 214 | this_sector = first_sector + start_sect(p) * sector_size; |
| 215 | this_size = nr_sects(p) * sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | put_dev_sector(sect); |
| 217 | } |
| 218 | done: |
| 219 | put_dev_sector(sect); |
| 220 | } |
| 221 | |
Christoph Hellwig | 3f4fc59 | 2020-03-24 08:25:29 +0100 | [diff] [blame] | 222 | #define SOLARIS_X86_NUMSLICE 16 |
| 223 | #define SOLARIS_X86_VTOC_SANE (0x600DDEEEUL) |
| 224 | |
| 225 | struct solaris_x86_slice { |
| 226 | __le16 s_tag; /* ID tag of partition */ |
| 227 | __le16 s_flag; /* permission flags */ |
| 228 | __le32 s_start; /* start sector no of partition */ |
| 229 | __le32 s_size; /* # of blocks in partition */ |
| 230 | }; |
| 231 | |
| 232 | struct solaris_x86_vtoc { |
| 233 | unsigned int v_bootinfo[3]; /* info needed by mboot */ |
| 234 | __le32 v_sanity; /* to verify vtoc sanity */ |
| 235 | __le32 v_version; /* layout version */ |
| 236 | char v_volume[8]; /* volume name */ |
| 237 | __le16 v_sectorsz; /* sector size in bytes */ |
| 238 | __le16 v_nparts; /* number of partitions */ |
| 239 | unsigned int v_reserved[10]; /* free space */ |
| 240 | struct solaris_x86_slice |
| 241 | v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */ |
| 242 | unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp */ |
| 243 | char v_asciilabel[128]; /* for compatibility */ |
| 244 | }; |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also |
| 247 | indicates linux swap. Be careful before believing this is Solaris. */ |
| 248 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 249 | static void parse_solaris_x86(struct parsed_partitions *state, |
| 250 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
| 252 | #ifdef CONFIG_SOLARIS_X86_PARTITION |
| 253 | Sector sect; |
| 254 | struct solaris_x86_vtoc *v; |
| 255 | int i; |
Mark Fortescue | b84d879 | 2007-07-25 18:30:08 -0700 | [diff] [blame] | 256 | short max_nparts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 258 | v = read_part_sector(state, offset + 1, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (!v) |
| 260 | return; |
| 261 | if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) { |
| 262 | put_dev_sector(sect); |
| 263 | return; |
| 264 | } |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 265 | { |
| 266 | char tmp[1 + BDEVNAME_SIZE + 10 + 11 + 1]; |
| 267 | |
| 268 | snprintf(tmp, sizeof(tmp), " %s%d: <solaris:", state->name, origin); |
| 269 | strlcat(state->pp_buf, tmp, PAGE_SIZE); |
| 270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | if (le32_to_cpu(v->v_version) != 1) { |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 272 | char tmp[64]; |
| 273 | |
| 274 | snprintf(tmp, sizeof(tmp), " cannot handle version %d vtoc>\n", |
| 275 | le32_to_cpu(v->v_version)); |
| 276 | strlcat(state->pp_buf, tmp, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | put_dev_sector(sect); |
| 278 | return; |
| 279 | } |
Mark Fortescue | b84d879 | 2007-07-25 18:30:08 -0700 | [diff] [blame] | 280 | /* Ensure we can handle previous case of VTOC with 8 entries gracefully */ |
Fabian Frederick | dce14c2 | 2014-06-12 20:16:57 +0200 | [diff] [blame] | 281 | max_nparts = le16_to_cpu(v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8; |
| 282 | for (i = 0; i < max_nparts && state->next < state->limit; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | struct solaris_x86_slice *s = &v->v_slice[i]; |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 284 | char tmp[3 + 10 + 1 + 1]; |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | if (s->s_size == 0) |
| 287 | continue; |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 288 | snprintf(tmp, sizeof(tmp), " [s%d]", i); |
| 289 | strlcat(state->pp_buf, tmp, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | /* solaris partitions are relative to current MS-DOS |
| 291 | * one; must add the offset of the current partition */ |
| 292 | put_partition(state, state->next++, |
| 293 | le32_to_cpu(s->s_start)+offset, |
| 294 | le32_to_cpu(s->s_size)); |
| 295 | } |
| 296 | put_dev_sector(sect); |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 297 | strlcat(state->pp_buf, " >\n", PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | #endif |
| 299 | } |
| 300 | |
Christoph Hellwig | 3f4fc59 | 2020-03-24 08:25:29 +0100 | [diff] [blame] | 301 | /* check against BSD src/sys/sys/disklabel.h for consistency */ |
| 302 | #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */ |
| 303 | #define BSD_MAXPARTITIONS 16 |
| 304 | #define OPENBSD_MAXPARTITIONS 16 |
| 305 | #define BSD_FS_UNUSED 0 /* disklabel unused partition entry ID */ |
| 306 | struct bsd_disklabel { |
| 307 | __le32 d_magic; /* the magic number */ |
| 308 | __s16 d_type; /* drive type */ |
| 309 | __s16 d_subtype; /* controller/d_type specific */ |
| 310 | char d_typename[16]; /* type name, e.g. "eagle" */ |
| 311 | char d_packname[16]; /* pack identifier */ |
| 312 | __u32 d_secsize; /* # of bytes per sector */ |
| 313 | __u32 d_nsectors; /* # of data sectors per track */ |
| 314 | __u32 d_ntracks; /* # of tracks per cylinder */ |
| 315 | __u32 d_ncylinders; /* # of data cylinders per unit */ |
| 316 | __u32 d_secpercyl; /* # of data sectors per cylinder */ |
| 317 | __u32 d_secperunit; /* # of data sectors per unit */ |
| 318 | __u16 d_sparespertrack; /* # of spare sectors per track */ |
| 319 | __u16 d_sparespercyl; /* # of spare sectors per cylinder */ |
| 320 | __u32 d_acylinders; /* # of alt. cylinders per unit */ |
| 321 | __u16 d_rpm; /* rotational speed */ |
| 322 | __u16 d_interleave; /* hardware sector interleave */ |
| 323 | __u16 d_trackskew; /* sector 0 skew, per track */ |
| 324 | __u16 d_cylskew; /* sector 0 skew, per cylinder */ |
| 325 | __u32 d_headswitch; /* head switch time, usec */ |
| 326 | __u32 d_trkseek; /* track-to-track seek, usec */ |
| 327 | __u32 d_flags; /* generic flags */ |
| 328 | #define NDDATA 5 |
| 329 | __u32 d_drivedata[NDDATA]; /* drive-type specific information */ |
| 330 | #define NSPARE 5 |
| 331 | __u32 d_spare[NSPARE]; /* reserved for future use */ |
| 332 | __le32 d_magic2; /* the magic number (again) */ |
| 333 | __le16 d_checksum; /* xor of data incl. partitions */ |
| 334 | |
| 335 | /* filesystem and partition information: */ |
| 336 | __le16 d_npartitions; /* number of partitions in following */ |
| 337 | __le32 d_bbsize; /* size of boot area at sn0, bytes */ |
| 338 | __le32 d_sbsize; /* max size of fs superblock, bytes */ |
| 339 | struct bsd_partition { /* the partition table */ |
| 340 | __le32 p_size; /* number of sectors in partition */ |
| 341 | __le32 p_offset; /* starting sector */ |
| 342 | __le32 p_fsize; /* filesystem basic fragment size */ |
| 343 | __u8 p_fstype; /* filesystem type, see below */ |
| 344 | __u8 p_frag; /* filesystem fragments per block */ |
| 345 | __le16 p_cpg; /* filesystem cylinders per group */ |
| 346 | } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */ |
| 347 | }; |
| 348 | |
Adrian Bunk | 486fd40 | 2005-06-25 14:58:47 -0700 | [diff] [blame] | 349 | #if defined(CONFIG_BSD_DISKLABEL) |
Philippe De Muyter | 1d04f3c | 2013-07-08 16:01:28 -0700 | [diff] [blame] | 350 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | * Create devices for BSD partitions listed in a disklabel, under a |
| 352 | * dos-like partition. See parse_extended() for more information. |
| 353 | */ |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 354 | static void parse_bsd(struct parsed_partitions *state, |
| 355 | sector_t offset, sector_t size, int origin, char *flavour, |
| 356 | int max_partitions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
| 358 | Sector sect; |
| 359 | struct bsd_disklabel *l; |
| 360 | struct bsd_partition *p; |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 361 | char tmp[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 363 | l = read_part_sector(state, offset + 1, §); |
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) != BSD_DISKMAGIC) { |
| 367 | put_dev_sector(sect); |
| 368 | return; |
| 369 | } |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 370 | |
| 371 | snprintf(tmp, sizeof(tmp), " %s%d: <%s:", state->name, origin, flavour); |
| 372 | strlcat(state->pp_buf, tmp, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | if (le16_to_cpu(l->d_npartitions) < max_partitions) |
| 375 | max_partitions = le16_to_cpu(l->d_npartitions); |
| 376 | for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) { |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 377 | sector_t bsd_start, bsd_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | if (state->next == state->limit) |
| 380 | break; |
Philippe De Muyter | 1d04f3c | 2013-07-08 16:01:28 -0700 | [diff] [blame] | 381 | if (p->p_fstype == BSD_FS_UNUSED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | continue; |
| 383 | bsd_start = le32_to_cpu(p->p_offset); |
| 384 | bsd_size = le32_to_cpu(p->p_size); |
Richard Narron | 5f15684 | 2018-01-10 09:12:16 -0700 | [diff] [blame] | 385 | /* FreeBSD has relative offset if C partition offset is zero */ |
| 386 | if (memcmp(flavour, "bsd\0", 4) == 0 && |
| 387 | le32_to_cpu(l->d_partitions[2].p_offset) == 0) |
Richard | 2232203 | 2017-05-21 12:27:00 -0700 | [diff] [blame] | 388 | bsd_start += offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | if (offset == bsd_start && size == bsd_size) |
| 390 | /* full parent partition, we have it already */ |
| 391 | continue; |
| 392 | if (offset > bsd_start || offset+size < bsd_start+bsd_size) { |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 393 | strlcat(state->pp_buf, "bad subpartition - ignored\n", PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | continue; |
| 395 | } |
| 396 | put_partition(state, state->next++, bsd_start, bsd_size); |
| 397 | } |
| 398 | put_dev_sector(sect); |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 399 | if (le16_to_cpu(l->d_npartitions) > max_partitions) { |
| 400 | snprintf(tmp, sizeof(tmp), " (ignored %d more)", |
| 401 | le16_to_cpu(l->d_npartitions) - max_partitions); |
| 402 | strlcat(state->pp_buf, tmp, PAGE_SIZE); |
| 403 | } |
| 404 | strlcat(state->pp_buf, " >\n", PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | #endif |
| 407 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 408 | static void parse_freebsd(struct parsed_partitions *state, |
| 409 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
| 411 | #ifdef CONFIG_BSD_DISKLABEL |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 412 | parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | #endif |
| 414 | } |
| 415 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 416 | static void parse_netbsd(struct parsed_partitions *state, |
| 417 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
| 419 | #ifdef CONFIG_BSD_DISKLABEL |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 420 | parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | #endif |
| 422 | } |
| 423 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 424 | static void parse_openbsd(struct parsed_partitions *state, |
| 425 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
| 427 | #ifdef CONFIG_BSD_DISKLABEL |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 428 | parse_bsd(state, offset, size, origin, "openbsd", |
| 429 | OPENBSD_MAXPARTITIONS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | #endif |
| 431 | } |
| 432 | |
Christoph Hellwig | 3f4fc59 | 2020-03-24 08:25:29 +0100 | [diff] [blame] | 433 | #define UNIXWARE_DISKMAGIC (0xCA5E600DUL) /* The disk magic number */ |
| 434 | #define UNIXWARE_DISKMAGIC2 (0x600DDEEEUL) /* The slice table magic nr */ |
| 435 | #define UNIXWARE_NUMSLICE 16 |
| 436 | #define UNIXWARE_FS_UNUSED 0 /* Unused slice entry ID */ |
| 437 | |
| 438 | struct unixware_slice { |
| 439 | __le16 s_label; /* label */ |
| 440 | __le16 s_flags; /* permission flags */ |
| 441 | __le32 start_sect; /* starting sector */ |
| 442 | __le32 nr_sects; /* number of sectors in slice */ |
| 443 | }; |
| 444 | |
| 445 | struct unixware_disklabel { |
| 446 | __le32 d_type; /* drive type */ |
| 447 | __le32 d_magic; /* the magic number */ |
| 448 | __le32 d_version; /* version number */ |
| 449 | char d_serial[12]; /* serial number of the device */ |
| 450 | __le32 d_ncylinders; /* # of data cylinders per device */ |
| 451 | __le32 d_ntracks; /* # of tracks per cylinder */ |
| 452 | __le32 d_nsectors; /* # of data sectors per track */ |
| 453 | __le32 d_secsize; /* # of bytes per sector */ |
| 454 | __le32 d_part_start; /* # of first sector of this partition*/ |
| 455 | __le32 d_unknown1[12]; /* ? */ |
| 456 | __le32 d_alt_tbl; /* byte offset of alternate table */ |
| 457 | __le32 d_alt_len; /* byte length of alternate table */ |
| 458 | __le32 d_phys_cyl; /* # of physical cylinders per device */ |
| 459 | __le32 d_phys_trk; /* # of physical tracks per cylinder */ |
| 460 | __le32 d_phys_sec; /* # of physical sectors per track */ |
| 461 | __le32 d_phys_bytes; /* # of physical bytes per sector */ |
| 462 | __le32 d_unknown2; /* ? */ |
| 463 | __le32 d_unknown3; /* ? */ |
| 464 | __le32 d_pad[8]; /* pad */ |
| 465 | |
| 466 | struct unixware_vtoc { |
| 467 | __le32 v_magic; /* the magic number */ |
| 468 | __le32 v_version; /* version number */ |
| 469 | char v_name[8]; /* volume name */ |
| 470 | __le16 v_nslices; /* # of slices */ |
| 471 | __le16 v_unknown1; /* ? */ |
| 472 | __le32 v_reserved[10]; /* reserved */ |
| 473 | struct unixware_slice |
| 474 | v_slice[UNIXWARE_NUMSLICE]; /* slice headers */ |
| 475 | } vtoc; |
| 476 | }; /* 408 */ |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | /* |
| 479 | * Create devices for Unixware partitions listed in a disklabel, under a |
| 480 | * dos-like partition. See parse_extended() for more information. |
| 481 | */ |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 482 | static void parse_unixware(struct parsed_partitions *state, |
| 483 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
| 485 | #ifdef CONFIG_UNIXWARE_DISKLABEL |
| 486 | Sector sect; |
| 487 | struct unixware_disklabel *l; |
| 488 | struct unixware_slice *p; |
| 489 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 490 | l = read_part_sector(state, offset + 29, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | if (!l) |
| 492 | return; |
| 493 | if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC || |
| 494 | le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) { |
| 495 | put_dev_sector(sect); |
| 496 | return; |
| 497 | } |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 498 | { |
| 499 | char tmp[1 + BDEVNAME_SIZE + 10 + 12 + 1]; |
| 500 | |
| 501 | snprintf(tmp, sizeof(tmp), " %s%d: <unixware:", state->name, origin); |
| 502 | strlcat(state->pp_buf, tmp, PAGE_SIZE); |
| 503 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | p = &l->vtoc.v_slice[1]; |
| 505 | /* I omit the 0th slice as it is the same as whole disk. */ |
| 506 | while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) { |
| 507 | if (state->next == state->limit) |
| 508 | break; |
| 509 | |
| 510 | if (p->s_label != UNIXWARE_FS_UNUSED) |
| 511 | put_partition(state, state->next++, |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 512 | le32_to_cpu(p->start_sect), |
| 513 | le32_to_cpu(p->nr_sects)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | p++; |
| 515 | } |
| 516 | put_dev_sector(sect); |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 517 | strlcat(state->pp_buf, " >\n", PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | #endif |
| 519 | } |
| 520 | |
Christoph Hellwig | 3f4fc59 | 2020-03-24 08:25:29 +0100 | [diff] [blame] | 521 | #define MINIX_NR_SUBPARTITIONS 4 |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | /* |
| 524 | * Minix 2.0.0/2.0.2 subpartition support. |
| 525 | * Anand Krishnamurthy <anandk@wiproge.med.ge.com> |
| 526 | * Rajeev V. Pillai <rajeevvp@yahoo.com> |
| 527 | */ |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 528 | static void parse_minix(struct parsed_partitions *state, |
| 529 | sector_t offset, sector_t size, int origin) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { |
| 531 | #ifdef CONFIG_MINIX_SUBPARTITION |
| 532 | Sector sect; |
| 533 | unsigned char *data; |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 534 | struct msdos_partition *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | int i; |
| 536 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 537 | data = read_part_sector(state, offset, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | if (!data) |
| 539 | return; |
| 540 | |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 541 | p = (struct msdos_partition *)(data + 0x1be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
| 543 | /* The first sector of a Minix partition can have either |
| 544 | * a secondary MBR describing its subpartitions, or |
| 545 | * the normal boot sector. */ |
Fabian Frederick | dce14c2 | 2014-06-12 20:16:57 +0200 | [diff] [blame] | 546 | if (msdos_magic_present(data + 510) && |
Arnd Bergmann | 5f69841 | 2021-05-08 00:07:53 +0200 | [diff] [blame] | 547 | p->sys_ind == MINIX_PARTITION) { /* subpartition table present */ |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 548 | char tmp[1 + BDEVNAME_SIZE + 10 + 9 + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 550 | snprintf(tmp, sizeof(tmp), " %s%d: <minix:", state->name, origin); |
| 551 | strlcat(state->pp_buf, tmp, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) { |
| 553 | if (state->next == state->limit) |
| 554 | break; |
| 555 | /* add each partition in use */ |
Arnd Bergmann | 5f69841 | 2021-05-08 00:07:53 +0200 | [diff] [blame] | 556 | if (p->sys_ind == MINIX_PARTITION) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | put_partition(state, state->next++, |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 558 | start_sect(p), nr_sects(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 560 | strlcat(state->pp_buf, " >\n", PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | } |
| 562 | put_dev_sector(sect); |
| 563 | #endif /* CONFIG_MINIX_SUBPARTITION */ |
| 564 | } |
| 565 | |
| 566 | static struct { |
| 567 | unsigned char id; |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 568 | void (*parse)(struct parsed_partitions *, sector_t, sector_t, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } subtypes[] = { |
| 570 | {FREEBSD_PARTITION, parse_freebsd}, |
| 571 | {NETBSD_PARTITION, parse_netbsd}, |
| 572 | {OPENBSD_PARTITION, parse_openbsd}, |
| 573 | {MINIX_PARTITION, parse_minix}, |
| 574 | {UNIXWARE_PARTITION, parse_unixware}, |
| 575 | {SOLARIS_X86_PARTITION, parse_solaris_x86}, |
| 576 | {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86}, |
| 577 | {0, NULL}, |
| 578 | }; |
Philippe De Muyter | 1d04f3c | 2013-07-08 16:01:28 -0700 | [diff] [blame] | 579 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 580 | int msdos_partition(struct parsed_partitions *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 582 | sector_t sector_size = bdev_logical_block_size(state->bdev) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | Sector sect; |
| 584 | unsigned char *data; |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 585 | struct msdos_partition *p; |
Frank Seidel | 0607fd0 | 2008-04-28 02:16:31 -0700 | [diff] [blame] | 586 | struct fat_boot_sector *fb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | int slot; |
Stephen Warren | d33b98f | 2012-11-08 16:12:28 -0800 | [diff] [blame] | 588 | u32 disksig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 590 | data = read_part_sector(state, 0, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | if (!data) |
| 592 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
Philippe De Muyter | 86ee8ba | 2013-02-27 17:05:16 -0800 | [diff] [blame] | 594 | /* |
| 595 | * Note order! (some AIX disks, e.g. unbootable kind, |
| 596 | * have no MSDOS 55aa) |
| 597 | */ |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 598 | if (aix_magic_present(state, data)) { |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 599 | put_dev_sector(sect); |
Philippe De Muyter | f8f0660 | 2013-07-08 16:01:30 -0700 | [diff] [blame] | 600 | #ifdef CONFIG_AIX_PARTITION |
| 601 | return aix_partition(state); |
| 602 | #else |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 603 | strlcat(state->pp_buf, " [AIX]", PAGE_SIZE); |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 604 | return 0; |
Philippe De Muyter | f8f0660 | 2013-07-08 16:01:30 -0700 | [diff] [blame] | 605 | #endif |
Olaf Hering | e1dfa92 | 2006-09-29 01:59:39 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Philippe De Muyter | 86ee8ba | 2013-02-27 17:05:16 -0800 | [diff] [blame] | 608 | if (!msdos_magic_present(data + 510)) { |
| 609 | put_dev_sector(sect); |
| 610 | return 0; |
| 611 | } |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | /* |
| 614 | * Now that the 55aa signature is present, this is probably |
| 615 | * either the boot sector of a FAT filesystem or a DOS-type |
| 616 | * partition table. Reject this in case the boot indicator |
| 617 | * is not 0 or 0x80. |
| 618 | */ |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 619 | p = (struct msdos_partition *) (data + 0x1be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | for (slot = 1; slot <= 4; slot++, p++) { |
| 621 | if (p->boot_ind != 0 && p->boot_ind != 0x80) { |
Frank Seidel | 0607fd0 | 2008-04-28 02:16:31 -0700 | [diff] [blame] | 622 | /* |
| 623 | * Even without a valid boot inidicator value |
| 624 | * its still possible this is valid FAT filesystem |
| 625 | * without a partition table. |
| 626 | */ |
| 627 | fb = (struct fat_boot_sector *) data; |
| 628 | if (slot == 1 && fb->reserved && fb->fats |
| 629 | && fat_valid_media(fb->media)) { |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 630 | strlcat(state->pp_buf, "\n", PAGE_SIZE); |
Frank Seidel | 0607fd0 | 2008-04-28 02:16:31 -0700 | [diff] [blame] | 631 | put_dev_sector(sect); |
| 632 | return 1; |
| 633 | } else { |
| 634 | put_dev_sector(sect); |
| 635 | return 0; |
| 636 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
| 640 | #ifdef CONFIG_EFI_PARTITION |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 641 | p = (struct msdos_partition *) (data + 0x1be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | for (slot = 1 ; slot <= 4 ; slot++, p++) { |
| 643 | /* If this is an EFI GPT disk, msdos should ignore it. */ |
Arnd Bergmann | 5f69841 | 2021-05-08 00:07:53 +0200 | [diff] [blame] | 644 | if (p->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | put_dev_sector(sect); |
| 646 | return 0; |
| 647 | } |
| 648 | } |
| 649 | #endif |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 650 | p = (struct msdos_partition *) (data + 0x1be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
Stephen Warren | d33b98f | 2012-11-08 16:12:28 -0800 | [diff] [blame] | 652 | disksig = le32_to_cpup((__le32 *)(data + 0x1b8)); |
| 653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | /* |
| 655 | * Look for partitions in two passes: |
| 656 | * First find the primary and DOS-type extended partitions. |
| 657 | * On the second pass look inside *BSD, Unixware and Solaris partitions. |
| 658 | */ |
| 659 | |
| 660 | state->next = 5; |
| 661 | for (slot = 1 ; slot <= 4 ; slot++, p++) { |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 662 | sector_t start = start_sect(p)*sector_size; |
| 663 | sector_t size = nr_sects(p)*sector_size; |
Fabian Frederick | dce14c2 | 2014-06-12 20:16:57 +0200 | [diff] [blame] | 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | if (!size) |
| 666 | continue; |
| 667 | if (is_extended_partition(p)) { |
OGAWA Hirofumi | 8e0cc81 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 668 | /* |
| 669 | * prevent someone doing mkfs or mkswap on an |
| 670 | * extended partition, but leave room for LILO |
| 671 | * FIXME: this uses one logical sector for > 512b |
| 672 | * sector, although it may not be enough/proper. |
| 673 | */ |
| 674 | sector_t n = 2; |
Fabian Frederick | dce14c2 | 2014-06-12 20:16:57 +0200 | [diff] [blame] | 675 | |
OGAWA Hirofumi | 8e0cc81 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 676 | n = min(size, max(sector_size, n)); |
| 677 | put_partition(state, slot, start, n); |
| 678 | |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 679 | strlcat(state->pp_buf, " <", PAGE_SIZE); |
Stephen Warren | d33b98f | 2012-11-08 16:12:28 -0800 | [diff] [blame] | 680 | parse_extended(state, start, size, disksig); |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 681 | strlcat(state->pp_buf, " >", PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | continue; |
| 683 | } |
| 684 | put_partition(state, slot, start, size); |
Stephen Warren | d33b98f | 2012-11-08 16:12:28 -0800 | [diff] [blame] | 685 | set_info(state, slot, disksig); |
Arnd Bergmann | 5f69841 | 2021-05-08 00:07:53 +0200 | [diff] [blame] | 686 | if (p->sys_ind == LINUX_RAID_PARTITION) |
Cesar Eduardo Barros | cc91062 | 2010-04-17 19:28:09 -0300 | [diff] [blame] | 687 | state->parts[slot].flags = ADDPART_FLAG_RAID; |
Arnd Bergmann | 5f69841 | 2021-05-08 00:07:53 +0200 | [diff] [blame] | 688 | if (p->sys_ind == DM6_PARTITION) |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 689 | strlcat(state->pp_buf, "[DM]", PAGE_SIZE); |
Arnd Bergmann | 5f69841 | 2021-05-08 00:07:53 +0200 | [diff] [blame] | 690 | if (p->sys_ind == EZD_PARTITION) |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 691 | strlcat(state->pp_buf, "[EZD]", PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 694 | strlcat(state->pp_buf, "\n", PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
| 696 | /* second pass - output for each on a separate line */ |
Christoph Hellwig | 1442f76 | 2020-03-24 08:25:26 +0100 | [diff] [blame] | 697 | p = (struct msdos_partition *) (0x1be + data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | for (slot = 1 ; slot <= 4 ; slot++, p++) { |
Arnd Bergmann | 5f69841 | 2021-05-08 00:07:53 +0200 | [diff] [blame] | 699 | unsigned char id = p->sys_ind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | int n; |
| 701 | |
Daniel Taylor | 3fbf586 | 2010-03-23 13:35:50 -0700 | [diff] [blame] | 702 | if (!nr_sects(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | continue; |
| 704 | |
| 705 | for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++) |
| 706 | ; |
| 707 | |
| 708 | if (!subtypes[n].parse) |
| 709 | continue; |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 710 | subtypes[n].parse(state, start_sect(p) * sector_size, |
| 711 | nr_sects(p) * sector_size, slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | put_dev_sector(sect); |
| 714 | return 1; |
| 715 | } |