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/sun.c |
| 4 | * |
| 5 | * Code extracted from drivers/block/genhd.c |
| 6 | * |
| 7 | * Copyright (C) 1991-1998 Linus Torvalds |
| 8 | * Re-organised Feb 1998 Russell King |
| 9 | */ |
| 10 | |
| 11 | #include "check.h" |
Christoph Hellwig | cbb5cb3 | 2020-03-24 08:25:25 +0100 | [diff] [blame] | 12 | |
| 13 | #define SUN_LABEL_MAGIC 0xDABE |
| 14 | #define SUN_VTOC_SANITY 0x600DDEEE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Christoph Hellwig | 0226e9e | 2020-03-24 08:25:27 +0100 | [diff] [blame] | 16 | enum { |
| 17 | SUN_WHOLE_DISK = 5, |
| 18 | LINUX_RAID_PARTITION = 0xfd, /* autodetect RAID partition */ |
| 19 | }; |
| 20 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 21 | int sun_partition(struct parsed_partitions *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
| 23 | int i; |
| 24 | __be16 csum; |
| 25 | int slot = 1; |
| 26 | __be16 *ush; |
| 27 | Sector sect; |
| 28 | struct sun_disklabel { |
| 29 | unsigned char info[128]; /* Informative text string */ |
Mark Fortescue | 3961bae | 2007-07-26 00:17:22 -0700 | [diff] [blame] | 30 | struct sun_vtoc { |
| 31 | __be32 version; /* Layout version */ |
| 32 | char volume[8]; /* Volume name */ |
| 33 | __be16 nparts; /* Number of partitions */ |
| 34 | struct sun_info { /* Partition hdrs, sec 2 */ |
| 35 | __be16 id; |
| 36 | __be16 flags; |
| 37 | } infos[8]; |
| 38 | __be16 padding; /* Alignment padding */ |
| 39 | __be32 bootinfo[3]; /* Info needed by mboot */ |
| 40 | __be32 sanity; /* To verify vtoc sanity */ |
| 41 | __be32 reserved[10]; /* Free space */ |
| 42 | __be32 timestamp[8]; /* Partition timestamp */ |
| 43 | } vtoc; |
| 44 | __be32 write_reinstruct; /* sectors to skip, writes */ |
| 45 | __be32 read_reinstruct; /* sectors to skip, reads */ |
| 46 | unsigned char spare[148]; /* Padding */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | __be16 rspeed; /* Disk rotational speed */ |
| 48 | __be16 pcylcount; /* Physical cylinder count */ |
| 49 | __be16 sparecyl; /* extra sects per cylinder */ |
Mark Fortescue | 3961bae | 2007-07-26 00:17:22 -0700 | [diff] [blame] | 50 | __be16 obs1; /* gap1 */ |
| 51 | __be16 obs2; /* gap2 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | __be16 ilfact; /* Interleave factor */ |
| 53 | __be16 ncyl; /* Data cylinder count */ |
| 54 | __be16 nacyl; /* Alt. cylinder count */ |
| 55 | __be16 ntrks; /* Tracks per cylinder */ |
| 56 | __be16 nsect; /* Sectors per track */ |
Mark Fortescue | 3961bae | 2007-07-26 00:17:22 -0700 | [diff] [blame] | 57 | __be16 obs3; /* bhead - Label head offset */ |
| 58 | __be16 obs4; /* ppart - Physical Partition */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | struct sun_partition { |
| 60 | __be32 start_cylinder; |
| 61 | __be32 num_sectors; |
| 62 | } partitions[8]; |
| 63 | __be16 magic; /* Magic number */ |
| 64 | __be16 csum; /* Label xor'd checksum */ |
Mark Fortescue | 3961bae | 2007-07-26 00:17:22 -0700 | [diff] [blame] | 65 | } * label; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | struct sun_partition *p; |
| 67 | unsigned long spc; |
Mark Fortescue | 3961bae | 2007-07-26 00:17:22 -0700 | [diff] [blame] | 68 | int use_vtoc; |
| 69 | int nparts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Tejun Heo | 1493bf2 | 2010-05-15 20:09:30 +0200 | [diff] [blame] | 71 | label = read_part_sector(state, 0, §); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | if (!label) |
| 73 | return -1; |
| 74 | |
| 75 | p = label->partitions; |
| 76 | if (be16_to_cpu(label->magic) != SUN_LABEL_MAGIC) { |
| 77 | /* printk(KERN_INFO "Dev %s Sun disklabel: bad magic %04x\n", |
Christoph Hellwig | a08aa9b | 2021-08-10 17:45:09 +0200 | [diff] [blame] | 78 | state->disk->disk_name, be16_to_cpu(label->magic)); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | put_dev_sector(sect); |
| 80 | return 0; |
| 81 | } |
| 82 | /* Look at the checksum */ |
| 83 | ush = ((__be16 *) (label+1)) - 1; |
| 84 | for (csum = 0; ush >= ((__be16 *) label);) |
| 85 | csum ^= *ush--; |
| 86 | if (csum) { |
| 87 | printk("Dev %s Sun disklabel: Csum bad, label corrupted\n", |
Christoph Hellwig | a08aa9b | 2021-08-10 17:45:09 +0200 | [diff] [blame] | 88 | state->disk->disk_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | put_dev_sector(sect); |
| 90 | return 0; |
| 91 | } |
| 92 | |
Mark Fortescue | 3961bae | 2007-07-26 00:17:22 -0700 | [diff] [blame] | 93 | /* Check to see if we can use the VTOC table */ |
| 94 | use_vtoc = ((be32_to_cpu(label->vtoc.sanity) == SUN_VTOC_SANITY) && |
| 95 | (be32_to_cpu(label->vtoc.version) == 1) && |
| 96 | (be16_to_cpu(label->vtoc.nparts) <= 8)); |
| 97 | |
| 98 | /* Use 8 partition entries if not specified in validated VTOC */ |
| 99 | nparts = (use_vtoc) ? be16_to_cpu(label->vtoc.nparts) : 8; |
| 100 | |
| 101 | /* |
| 102 | * So that old Linux-Sun partitions continue to work, |
| 103 | * alow the VTOC to be used under the additional condition ... |
| 104 | */ |
Al Viro | b1519d0 | 2007-10-14 19:34:50 +0100 | [diff] [blame] | 105 | use_vtoc = use_vtoc || !(label->vtoc.sanity || |
| 106 | label->vtoc.version || label->vtoc.nparts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | spc = be16_to_cpu(label->ntrks) * be16_to_cpu(label->nsect); |
Mark Fortescue | 3961bae | 2007-07-26 00:17:22 -0700 | [diff] [blame] | 108 | for (i = 0; i < nparts; i++, p++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | unsigned long st_sector; |
Jeff Mahoney | 81a42d2 | 2006-08-25 15:58:57 -0700 | [diff] [blame] | 110 | unsigned int num_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | st_sector = be32_to_cpu(p->start_cylinder) * spc; |
| 113 | num_sectors = be32_to_cpu(p->num_sectors); |
| 114 | if (num_sectors) { |
| 115 | put_partition(state, slot, st_sector, num_sectors); |
Fabio Massimo Di Nitto | d18d768 | 2007-02-10 23:50:00 -0800 | [diff] [blame] | 116 | state->parts[slot].flags = 0; |
Mark Fortescue | 3961bae | 2007-07-26 00:17:22 -0700 | [diff] [blame] | 117 | if (use_vtoc) { |
| 118 | if (be16_to_cpu(label->vtoc.infos[i].id) == LINUX_RAID_PARTITION) |
| 119 | state->parts[slot].flags |= ADDPART_FLAG_RAID; |
| 120 | else if (be16_to_cpu(label->vtoc.infos[i].id) == SUN_WHOLE_DISK) |
| 121 | state->parts[slot].flags |= ADDPART_FLAG_WHOLEDISK; |
| 122 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | slot++; |
| 125 | } |
Alexey Dobriyan | 9c867fb | 2010-08-10 18:03:14 -0700 | [diff] [blame] | 126 | strlcat(state->pp_buf, "\n", PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | put_dev_sector(sect); |
| 128 | return 1; |
| 129 | } |