Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001 |
| 4 | * Raymond Lo, lo@routefree.com |
| 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Support for harddisk partitions. |
| 10 | * |
| 11 | * To be compatible with LinuxPPC and Apple we use the standard Apple |
| 12 | * SCSI disk partitioning scheme. For more information see: |
| 13 | * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92 |
| 14 | */ |
| 15 | |
| 16 | #include <common.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 17 | #include <blk.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 18 | #include <command.h> |
| 19 | #include <ide.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 20 | #include <memalign.h> |
Marek Szyprowski | 97163dd | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 21 | #include <asm/unaligned.h> |
Marek Szyprowski | cb571f9 | 2020-12-23 13:55:13 +0100 | [diff] [blame] | 22 | #include <linux/compiler.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 23 | #include "part_dos.h" |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 24 | #include <part.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 25 | |
Darwin Dingel | 4a36be9 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 26 | #define DOS_PART_DEFAULT_SECTOR 512 |
| 27 | |
Paul Emge | 232e2f4 | 2019-07-08 16:37:03 -0700 | [diff] [blame] | 28 | /* should this be configurable? It looks like it's not very common at all |
| 29 | * to use large numbers of partitions */ |
| 30 | #define MAX_EXT_PARTS 256 |
| 31 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 32 | static inline int is_extended(int part_type) |
| 33 | { |
Marek Szyprowski | 80bd05f | 2020-12-23 13:55:11 +0100 | [diff] [blame] | 34 | return (part_type == DOS_PART_TYPE_EXTENDED || |
| 35 | part_type == DOS_PART_TYPE_EXTENDED_LBA || |
| 36 | part_type == DOS_PART_TYPE_EXTENDED_LINUX); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Heinrich Schuchardt | 25801ac | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 39 | static int get_bootable(dos_partition_t *p) |
Rob Herring | 40e0e56 | 2012-08-23 11:31:43 +0000 | [diff] [blame] | 40 | { |
Heinrich Schuchardt | 25801ac | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 41 | int ret = 0; |
| 42 | |
| 43 | if (p->sys_ind == 0xef) |
| 44 | ret |= PART_EFI_SYSTEM_PARTITION; |
| 45 | if (p->boot_ind == 0x80) |
| 46 | ret |= PART_BOOTABLE; |
| 47 | return ret; |
Rob Herring | 40e0e56 | 2012-08-23 11:31:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Stefan Monnier | d29892b | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 50 | static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector, |
Stephen Warren | e2e9b37 | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 51 | int part_num, unsigned int disksig) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 52 | { |
Marek Szyprowski | 97163dd | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 53 | lbaint_t lba_start = ext_part_sector + get_unaligned_le32(p->start4); |
| 54 | lbaint_t lba_size = get_unaligned_le32(p->size4); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 55 | |
Stefan Monnier | d29892b | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 56 | printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength |
| 57 | "u\t%08x-%02x\t%02x%s%s\n", |
Stephen Warren | e2e9b37 | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 58 | part_num, lba_start, lba_size, disksig, part_num, p->sys_ind, |
Rob Herring | 40e0e56 | 2012-08-23 11:31:43 +0000 | [diff] [blame] | 59 | (is_extended(p->sys_ind) ? " Extd" : ""), |
Heinrich Schuchardt | 25801ac | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 60 | (get_bootable(p) ? " Boot" : "")); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 61 | } |
| 62 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 63 | static int test_block_type(unsigned char *buffer) |
| 64 | { |
Egbert Eich | 9d956e0 | 2013-04-09 05:46:14 +0000 | [diff] [blame] | 65 | int slot; |
| 66 | struct dos_partition *p; |
Heinrich Schuchardt | 34856b0 | 2019-10-15 20:43:42 +0200 | [diff] [blame] | 67 | int part_count = 0; |
Egbert Eich | 9d956e0 | 2013-04-09 05:46:14 +0000 | [diff] [blame] | 68 | |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 69 | if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) || |
| 70 | (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) { |
| 71 | return (-1); |
| 72 | } /* no DOS Signature at all */ |
Egbert Eich | 9d956e0 | 2013-04-09 05:46:14 +0000 | [diff] [blame] | 73 | p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET]; |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 74 | |
Heinrich Schuchardt | 34856b0 | 2019-10-15 20:43:42 +0200 | [diff] [blame] | 75 | /* Check that the boot indicators are valid and count the partitions. */ |
| 76 | for (slot = 0; slot < 4; ++slot, ++p) { |
| 77 | if (p->boot_ind != 0 && p->boot_ind != 0x80) |
| 78 | break; |
| 79 | if (p->sys_ind) |
| 80 | ++part_count; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * If the partition table is invalid or empty, |
| 85 | * check if this is a DOS PBR |
| 86 | */ |
| 87 | if (slot != 4 || !part_count) { |
| 88 | if (!strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET], |
| 89 | "FAT", 3) || |
| 90 | !strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET], |
| 91 | "FAT32", 5)) |
| 92 | return DOS_PBR; /* This is a DOS PBR and not an MBR */ |
| 93 | } |
| 94 | if (slot == 4) |
| 95 | return DOS_MBR; /* This is an DOS MBR */ |
| 96 | |
| 97 | /* This is neither a DOS MBR nor a DOS PBR */ |
| 98 | return -1; |
| 99 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 100 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 101 | static int part_test_dos(struct blk_desc *desc) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 102 | { |
Fabio Estevam | 3ea0520 | 2017-10-04 13:29:57 -0300 | [diff] [blame] | 103 | #ifndef CONFIG_SPL_BUILD |
Faiz Abbas | 7aed3d3 | 2019-09-04 20:10:12 +0530 | [diff] [blame] | 104 | ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr, |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 105 | DIV_ROUND_UP(desc->blksz, sizeof(legacy_mbr))); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 106 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 107 | if (blk_dread(desc, 0, 1, (ulong *)mbr) != 1) |
Stephen Warren | d1efb64 | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 108 | return -1; |
| 109 | |
Peter Jones | ff98cb9 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 110 | if (test_block_type((unsigned char *)mbr) != DOS_MBR) |
Stephen Warren | d1efb64 | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 111 | return -1; |
| 112 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 113 | if (desc->sig_type == SIG_TYPE_NONE && mbr->unique_mbr_signature) { |
| 114 | desc->sig_type = SIG_TYPE_MBR; |
| 115 | desc->mbr_sig = mbr->unique_mbr_signature; |
Peter Jones | ff98cb9 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 116 | } |
Fabio Estevam | 3ea0520 | 2017-10-04 13:29:57 -0300 | [diff] [blame] | 117 | #else |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 118 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz); |
Fabio Estevam | 3ea0520 | 2017-10-04 13:29:57 -0300 | [diff] [blame] | 119 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 120 | if (blk_dread(desc, 0, 1, (ulong *)buffer) != 1) |
Fabio Estevam | 3ea0520 | 2017-10-04 13:29:57 -0300 | [diff] [blame] | 121 | return -1; |
| 122 | |
| 123 | if (test_block_type(buffer) != DOS_MBR) |
| 124 | return -1; |
| 125 | #endif |
Peter Jones | ff98cb9 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 126 | |
Stephen Warren | d1efb64 | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 127 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* Print a partition that is relative to its Extended partition table |
| 131 | */ |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 132 | static void print_partition_extended(struct blk_desc *desc, |
Stefan Monnier | d29892b | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 133 | lbaint_t ext_part_sector, |
| 134 | lbaint_t relative, |
Stephen Warren | e2e9b37 | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 135 | int part_num, unsigned int disksig) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 136 | { |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 137 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 138 | dos_partition_t *pt; |
| 139 | int i; |
| 140 | |
Paul Emge | 232e2f4 | 2019-07-08 16:37:03 -0700 | [diff] [blame] | 141 | /* set a maximum recursion level */ |
| 142 | if (part_num > MAX_EXT_PARTS) |
| 143 | { |
| 144 | printf("** Nested DOS partitions detected, stopping **\n"); |
| 145 | return; |
| 146 | } |
| 147 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 148 | if (blk_dread(desc, ext_part_sector, 1, (ulong *)buffer) != 1) { |
Stefan Monnier | d29892b | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 149 | printf ("** Can't read partition table on %d:" LBAFU " **\n", |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 150 | desc->devnum, ext_part_sector); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 151 | return; |
| 152 | } |
wdenk | 7205e40 | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 153 | i=test_block_type(buffer); |
Stephen Warren | d1efb64 | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 154 | if (i != DOS_MBR) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 155 | printf ("bad MBR sector signature 0x%02x%02x\n", |
| 156 | buffer[DOS_PART_MAGIC_OFFSET], |
| 157 | buffer[DOS_PART_MAGIC_OFFSET + 1]); |
| 158 | return; |
| 159 | } |
Stephen Warren | d1efb64 | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 160 | |
Stephen Warren | e2e9b37 | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 161 | if (!ext_part_sector) |
Marek Szyprowski | 97163dd | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 162 | disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]); |
Stephen Warren | e2e9b37 | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 163 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 164 | /* Print all primary/logical partitions */ |
| 165 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 166 | for (i = 0; i < 4; i++, pt++) { |
| 167 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 168 | * fdisk does not show the extended partitions that |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 169 | * are not in the MBR |
| 170 | */ |
| 171 | |
| 172 | if ((pt->sys_ind != 0) && |
| 173 | (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) { |
Stephen Warren | e2e9b37 | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 174 | print_one_part(pt, ext_part_sector, part_num, disksig); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* Reverse engr the fdisk part# assignment rule! */ |
| 178 | if ((ext_part_sector == 0) || |
| 179 | (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { |
| 180 | part_num++; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* Follows the extended partitions */ |
| 185 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 186 | for (i = 0; i < 4; i++, pt++) { |
| 187 | if (is_extended (pt->sys_ind)) { |
Stefan Monnier | d29892b | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 188 | lbaint_t lba_start |
Marek Szyprowski | 97163dd | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 189 | = get_unaligned_le32 (pt->start4) + relative; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 190 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 191 | print_partition_extended(desc, lba_start, |
| 192 | !ext_part_sector ? lba_start : |
| 193 | relative, part_num, disksig); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /* Print a partition that is relative to its Extended partition table |
| 202 | */ |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 203 | static int part_get_info_extended(struct blk_desc *desc, |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 204 | lbaint_t ext_part_sector, lbaint_t relative, |
| 205 | int part_num, int which_part, |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 206 | struct disk_partition *info, uint disksig) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 207 | { |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 208 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz); |
Marek Vasut | e2b5cc6 | 2023-08-14 01:48:45 +0200 | [diff] [blame] | 209 | struct disk_partition wdinfo = { 0 }; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 210 | dos_partition_t *pt; |
Marek Vasut | e2b5cc6 | 2023-08-14 01:48:45 +0200 | [diff] [blame] | 211 | int i, ret; |
Darwin Dingel | 4a36be9 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 212 | int dos_type; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 213 | |
Paul Emge | 232e2f4 | 2019-07-08 16:37:03 -0700 | [diff] [blame] | 214 | /* set a maximum recursion level */ |
| 215 | if (part_num > MAX_EXT_PARTS) |
| 216 | { |
| 217 | printf("** Nested DOS partitions detected, stopping **\n"); |
| 218 | return -1; |
| 219 | } |
| 220 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 221 | if (blk_dread(desc, ext_part_sector, 1, (ulong *)buffer) != 1) { |
Stefan Monnier | d29892b | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 222 | printf ("** Can't read partition table on %d:" LBAFU " **\n", |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 223 | desc->devnum, ext_part_sector); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 224 | return -1; |
| 225 | } |
| 226 | if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 || |
| 227 | buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) { |
| 228 | printf ("bad MBR sector signature 0x%02x%02x\n", |
| 229 | buffer[DOS_PART_MAGIC_OFFSET], |
| 230 | buffer[DOS_PART_MAGIC_OFFSET + 1]); |
| 231 | return -1; |
| 232 | } |
| 233 | |
Simon Glass | c5f1d00 | 2023-08-24 13:55:31 -0600 | [diff] [blame] | 234 | if (CONFIG_IS_ENABLED(PARTITION_UUIDS) && !ext_part_sector) |
Marek Szyprowski | 97163dd | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 235 | disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]); |
Stephen Warren | d27b5f9 | 2012-09-21 09:51:00 +0000 | [diff] [blame] | 236 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 237 | ret = part_get_info_whole_disk(desc, &wdinfo); |
Marek Vasut | e2b5cc6 | 2023-08-14 01:48:45 +0200 | [diff] [blame] | 238 | if (ret) |
| 239 | return ret; |
| 240 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 241 | /* Print all primary/logical partitions */ |
| 242 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 243 | for (i = 0; i < 4; i++, pt++) { |
| 244 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 245 | * fdisk does not show the extended partitions that |
| 246 | * are not in the MBR |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 247 | */ |
Daniel Mack | 78f4ca7 | 2009-09-28 11:40:38 +0200 | [diff] [blame] | 248 | if (((pt->boot_ind & ~0x80) == 0) && |
| 249 | (pt->sys_ind != 0) && |
wdenk | 7f70e85 | 2003-05-20 14:25:27 +0000 | [diff] [blame] | 250 | (part_num == which_part) && |
Shawn Guo | 8f7102c | 2017-11-02 16:46:34 +0800 | [diff] [blame] | 251 | (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) { |
Marek Vasut | e2b5cc6 | 2023-08-14 01:48:45 +0200 | [diff] [blame] | 252 | if (wdinfo.blksz > DOS_PART_DEFAULT_SECTOR) |
| 253 | info->blksz = wdinfo.blksz; |
| 254 | else |
| 255 | info->blksz = DOS_PART_DEFAULT_SECTOR; |
Steve Rae | e04350d | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 256 | info->start = (lbaint_t)(ext_part_sector + |
Marek Szyprowski | 97163dd | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 257 | get_unaligned_le32(pt->start4)); |
| 258 | info->size = (lbaint_t)get_unaligned_le32(pt->size4); |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 259 | part_set_generic_name(desc, part_num, |
Petr Kulhavy | da2ee24 | 2016-09-09 10:27:17 +0200 | [diff] [blame] | 260 | (char *)info->name); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 261 | /* sprintf(info->type, "%d, pt->sys_ind); */ |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 262 | strcpy((char *)info->type, "U-Boot"); |
Heinrich Schuchardt | 25801ac | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 263 | info->bootable = get_bootable(pt); |
Simon Glass | c5f1d00 | 2023-08-24 13:55:31 -0600 | [diff] [blame] | 264 | if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) { |
| 265 | char str[12]; |
| 266 | |
| 267 | sprintf(str, "%08x-%02x", disksig, part_num); |
| 268 | disk_partition_set_uuid(info, str); |
| 269 | } |
Dalon Westergreen | f0fb4fa | 2017-02-10 17:15:34 -0800 | [diff] [blame] | 270 | info->sys_ind = pt->sys_ind; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | /* Reverse engr the fdisk part# assignment rule! */ |
| 275 | if ((ext_part_sector == 0) || |
| 276 | (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { |
| 277 | part_num++; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* Follows the extended partitions */ |
| 282 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 283 | for (i = 0; i < 4; i++, pt++) { |
| 284 | if (is_extended (pt->sys_ind)) { |
Stefan Monnier | d29892b | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 285 | lbaint_t lba_start |
Marek Szyprowski | 97163dd | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 286 | = get_unaligned_le32 (pt->start4) + relative; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 287 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 288 | return part_get_info_extended(desc, lba_start, |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 289 | ext_part_sector == 0 ? lba_start : relative, |
Stephen Warren | d27b5f9 | 2012-09-21 09:51:00 +0000 | [diff] [blame] | 290 | part_num, which_part, info, disksig); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
Darwin Dingel | 4a36be9 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 293 | |
| 294 | /* Check for DOS PBR if no partition is found */ |
| 295 | dos_type = test_block_type(buffer); |
| 296 | |
| 297 | if (dos_type == DOS_PBR) { |
| 298 | info->start = 0; |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 299 | info->size = desc->lba; |
Marek Vasut | e2b5cc6 | 2023-08-14 01:48:45 +0200 | [diff] [blame] | 300 | if (wdinfo.blksz > DOS_PART_DEFAULT_SECTOR) |
| 301 | info->blksz = wdinfo.blksz; |
| 302 | else |
| 303 | info->blksz = DOS_PART_DEFAULT_SECTOR; |
Darwin Dingel | 4a36be9 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 304 | info->bootable = 0; |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 305 | strcpy((char *)info->type, "U-Boot"); |
Simon Glass | c5f1d00 | 2023-08-24 13:55:31 -0600 | [diff] [blame] | 306 | disk_partition_clr_uuid(info); |
Darwin Dingel | 4a36be9 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 310 | return -1; |
| 311 | } |
| 312 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 313 | static void __maybe_unused part_print_dos(struct blk_desc *desc) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 314 | { |
Stephen Warren | e2e9b37 | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 315 | printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n"); |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 316 | print_partition_extended(desc, 0, 0, 1, 0); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 319 | static int __maybe_unused part_get_info_dos(struct blk_desc *desc, int part, |
| 320 | struct disk_partition *info) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 321 | { |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 322 | return part_get_info_extended(desc, 0, 0, 1, part, info, 0); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 325 | int is_valid_dos_buf(void *buf) |
| 326 | { |
| 327 | return test_block_type(buf) == DOS_MBR ? 0 : -1; |
| 328 | } |
| 329 | |
Simon Glass | d01bf20 | 2023-02-05 15:36:33 -0700 | [diff] [blame] | 330 | #if IS_ENABLED(CONFIG_CMD_MBR) |
Marek Szyprowski | 20bd5ac | 2020-12-23 13:55:14 +0100 | [diff] [blame] | 331 | static void lba_to_chs(lbaint_t lba, unsigned char *rc, unsigned char *rh, |
| 332 | unsigned char *rs) |
| 333 | { |
| 334 | unsigned int c, h, s; |
| 335 | /* use fixed CHS geometry */ |
| 336 | unsigned int sectpertrack = 63; |
| 337 | unsigned int heads = 255; |
| 338 | |
| 339 | c = (lba + 1) / sectpertrack / heads; |
| 340 | h = (lba + 1) / sectpertrack - c * heads; |
| 341 | s = (lba + 1) - (c * heads + h) * sectpertrack; |
| 342 | |
| 343 | if (c > 1023) { |
| 344 | c = 1023; |
| 345 | h = 254; |
| 346 | s = 63; |
| 347 | } |
| 348 | |
| 349 | *rc = c & 0xff; |
| 350 | *rh = h; |
| 351 | *rs = s + ((c & 0x300) >> 2); |
| 352 | } |
| 353 | |
| 354 | static void mbr_fill_pt_entry(dos_partition_t *pt, lbaint_t start, |
| 355 | lbaint_t relative, lbaint_t size, uchar sys_ind, bool bootable) |
| 356 | { |
| 357 | pt->boot_ind = bootable ? 0x80 : 0x00; |
| 358 | pt->sys_ind = sys_ind; |
| 359 | lba_to_chs(start, &pt->cyl, &pt->head, &pt->sector); |
| 360 | lba_to_chs(start + size - 1, &pt->end_cyl, &pt->end_head, &pt->end_sector); |
| 361 | put_unaligned_le32(relative, &pt->start4); |
| 362 | put_unaligned_le32(size, &pt->size4); |
| 363 | } |
| 364 | |
| 365 | int write_mbr_partitions(struct blk_desc *dev, |
| 366 | struct disk_partition *p, int count, unsigned int disksig) |
| 367 | { |
| 368 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev->blksz); |
| 369 | lbaint_t ext_part_start = 0, ext_part_size = 0, ext_part_sect = 0; |
| 370 | dos_partition_t *pt; |
| 371 | int i; |
| 372 | |
| 373 | memset(buffer, 0, dev->blksz); |
| 374 | buffer[DOS_PART_MAGIC_OFFSET] = 0x55; |
| 375 | buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa; |
| 376 | put_unaligned_le32(disksig, &buffer[DOS_PART_DISKSIG_OFFSET]); |
| 377 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 378 | |
| 379 | /* create all primary partitions */ |
| 380 | for (i = 0; i < 4 && i < count; i++, pt++) { |
| 381 | mbr_fill_pt_entry(pt, p[i].start, p[i].start, p[i].size, |
| 382 | p[i].sys_ind, p[i].bootable); |
| 383 | if (is_extended(p[i].sys_ind)) { |
| 384 | ext_part_start = p[i].start; |
| 385 | ext_part_size = p[i].size; |
| 386 | ext_part_sect = p[i].start; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if (i < count && !ext_part_start) { |
| 391 | printf("%s: extended partition is needed for more than 4 partitions\n", |
| 392 | __func__); |
| 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | /* write MBR */ |
| 397 | if (blk_dwrite(dev, 0, 1, buffer) != 1) { |
| 398 | printf("%s: failed writing 'MBR' (1 blks at 0x0)\n", |
| 399 | __func__); |
| 400 | return -1; |
| 401 | } |
| 402 | |
| 403 | /* create extended volumes */ |
| 404 | for (; i < count; i++) { |
| 405 | lbaint_t next_ebr = 0; |
| 406 | |
| 407 | memset(buffer, 0, dev->blksz); |
| 408 | buffer[DOS_PART_MAGIC_OFFSET] = 0x55; |
| 409 | buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa; |
| 410 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 411 | |
| 412 | mbr_fill_pt_entry(pt, p[i].start, p[i].start - ext_part_sect, |
| 413 | p[i].size, p[i].sys_ind, p[i].bootable); |
| 414 | |
| 415 | if (i + 1 < count) { |
| 416 | pt++; |
| 417 | next_ebr = p[i].start + p[i].size; |
| 418 | mbr_fill_pt_entry(pt, next_ebr, |
| 419 | next_ebr - ext_part_start, |
| 420 | p[i+1].start + p[i+1].size - next_ebr, |
| 421 | DOS_PART_TYPE_EXTENDED, 0); |
| 422 | } |
| 423 | |
| 424 | /* write EBR */ |
| 425 | if (blk_dwrite(dev, ext_part_sect, 1, buffer) != 1) { |
| 426 | printf("%s: failed writing 'EBR' (1 blks at 0x%lx)\n", |
| 427 | __func__, ext_part_sect); |
| 428 | return -1; |
| 429 | } |
| 430 | ext_part_sect = next_ebr; |
| 431 | } |
| 432 | |
Gary Bisson | f14c5ee | 2021-01-28 09:10:07 +0100 | [diff] [blame] | 433 | /* Update the partition table entries*/ |
Christian Melki | c7d1b18 | 2021-06-07 11:21:15 +0200 | [diff] [blame] | 434 | part_init(dev); |
Gary Bisson | f14c5ee | 2021-01-28 09:10:07 +0100 | [diff] [blame] | 435 | |
Marek Szyprowski | 20bd5ac | 2020-12-23 13:55:14 +0100 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | int layout_mbr_partitions(struct disk_partition *p, int count, |
| 440 | lbaint_t total_sectors) |
| 441 | { |
| 442 | struct disk_partition *ext = NULL; |
| 443 | int i, j; |
| 444 | lbaint_t ext_vol_start; |
| 445 | |
| 446 | /* calculate primary partitions start and size if needed */ |
| 447 | if (!p[0].start) |
| 448 | p[0].start = DOS_PART_DEFAULT_GAP; |
| 449 | for (i = 0; i < 4 && i < count; i++) { |
| 450 | if (!p[i].start) |
| 451 | p[i].start = p[i - 1].start + p[i - 1].size; |
| 452 | if (!p[i].size) { |
| 453 | lbaint_t end = total_sectors; |
| 454 | lbaint_t allocated = 0; |
| 455 | |
| 456 | for (j = i + 1; j < 4 && j < count; j++) { |
| 457 | if (p[j].start) { |
| 458 | end = p[j].start; |
| 459 | break; |
| 460 | } |
| 461 | allocated += p[j].size; |
| 462 | } |
| 463 | p[i].size = end - allocated - p[i].start; |
| 464 | } |
| 465 | if (p[i].sys_ind == 0x05) |
| 466 | ext = &p[i]; |
| 467 | } |
| 468 | |
Simon Glass | b7c2cc4 | 2021-10-23 17:26:02 -0600 | [diff] [blame] | 469 | if (count < 4) |
| 470 | return 0; |
| 471 | |
| 472 | if (!ext) { |
| 473 | log_err("extended partition is needed for more than 4 partitions\n"); |
| 474 | return -EINVAL; |
Marek Szyprowski | 20bd5ac | 2020-12-23 13:55:14 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* calculate extended volumes start and size if needed */ |
| 478 | ext_vol_start = ext->start; |
| 479 | for (i = 4; i < count; i++) { |
| 480 | if (!p[i].start) |
| 481 | p[i].start = ext_vol_start + DOS_PART_DEFAULT_GAP; |
| 482 | if (!p[i].size) { |
| 483 | lbaint_t end = ext->start + ext->size; |
| 484 | lbaint_t allocated = 0; |
| 485 | |
| 486 | for (j = i + 1; j < count; j++) { |
| 487 | if (p[j].start) { |
| 488 | end = p[j].start - DOS_PART_DEFAULT_GAP; |
| 489 | break; |
| 490 | } |
| 491 | allocated += p[j].size + DOS_PART_DEFAULT_GAP; |
| 492 | } |
| 493 | p[i].size = end - allocated - p[i].start; |
| 494 | } |
| 495 | ext_vol_start = p[i].start + p[i].size; |
| 496 | } |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | #endif |
| 501 | |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 502 | int write_mbr_sector(struct blk_desc *desc, void *buf) |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 503 | { |
| 504 | if (is_valid_dos_buf(buf)) |
| 505 | return -1; |
| 506 | |
| 507 | /* write MBR */ |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 508 | if (blk_dwrite(desc, 0, 1, buf) != 1) { |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 509 | printf("%s: failed writing '%s' (1 blks at 0x0)\n", |
| 510 | __func__, "MBR"); |
| 511 | return 1; |
| 512 | } |
| 513 | |
Gary Bisson | f14c5ee | 2021-01-28 09:10:07 +0100 | [diff] [blame] | 514 | /* Update the partition table entries*/ |
Simon Glass | cfdcd41 | 2023-08-24 13:55:26 -0600 | [diff] [blame] | 515 | part_init(desc); |
Gary Bisson | f14c5ee | 2021-01-28 09:10:07 +0100 | [diff] [blame] | 516 | |
Petr Kulhavy | b6dd69a | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
Simon Glass | 96e5b03 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 520 | U_BOOT_PART_TYPE(dos) = { |
| 521 | .name = "DOS", |
| 522 | .part_type = PART_TYPE_DOS, |
Petr Kulhavy | 87b8530 | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 523 | .max_entries = DOS_ENTRY_NUMBERS, |
Simon Glass | 3e8bd46 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 524 | .get_info = part_get_info_ptr(part_get_info_dos), |
Simon Glass | 084bf4c | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 525 | .print = part_print_ptr(part_print_dos), |
| 526 | .test = part_test_dos, |
Simon Glass | 96e5b03 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 527 | }; |