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