Tom Rini | 10e4779 | 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 | 655306c | 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 | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 20 | #include <memalign.h> |
Marek Szyprowski | 552f79c | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 21 | #include <asm/unaligned.h> |
Marek Szyprowski | cbea647 | 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 | 655306c | 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 | 2c1e1b6 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 26 | #define DOS_PART_DEFAULT_SECTOR 512 |
| 27 | |
Paul Emge | 6149412 | 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 | 654fa83 | 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 | 59a860d | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 39 | static int get_bootable(dos_partition_t *p) |
Rob Herring | eda5111 | 2012-08-23 11:31:43 +0000 | [diff] [blame] | 40 | { |
Heinrich Schuchardt | 59a860d | 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 | eda5111 | 2012-08-23 11:31:43 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Stefan Monnier | 4c6e960 | 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 | 37c88ea | 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 | 552f79c | 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 | 4c6e960 | 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 | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 58 | part_num, lba_start, lba_size, disksig, part_num, p->sys_ind, |
Rob Herring | eda5111 | 2012-08-23 11:31:43 +0000 | [diff] [blame] | 59 | (is_extended(p->sys_ind) ? " Extd" : ""), |
Heinrich Schuchardt | 59a860d | 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 | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 63 | static int test_block_type(unsigned char *buffer) |
| 64 | { |
Egbert Eich | 300494b | 2013-04-09 05:46:14 +0000 | [diff] [blame] | 65 | int slot; |
| 66 | struct dos_partition *p; |
Heinrich Schuchardt | 86c8dd4 | 2019-10-15 20:43:42 +0200 | [diff] [blame] | 67 | int part_count = 0; |
Egbert Eich | 300494b | 2013-04-09 05:46:14 +0000 | [diff] [blame] | 68 | |
wdenk | 2c9b05d | 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 | 300494b | 2013-04-09 05:46:14 +0000 | [diff] [blame] | 73 | p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET]; |
Heinrich Schuchardt | 86c8dd4 | 2019-10-15 20:43:42 +0200 | [diff] [blame] | 74 | |
| 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; |
Wolfgang Denk | 7b2290c | 2010-07-19 11:36:57 +0200 | [diff] [blame] | 81 | } |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 82 | |
Heinrich Schuchardt | 86c8dd4 | 2019-10-15 20:43:42 +0200 | [diff] [blame] | 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 | 0f1c952 | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 101 | static int part_test_dos(struct blk_desc *dev_desc) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 102 | { |
Fabio Estevam | f0c82a8 | 2017-10-04 13:29:57 -0300 | [diff] [blame] | 103 | #ifndef CONFIG_SPL_BUILD |
Faiz Abbas | 1bca8de | 2019-09-04 20:10:12 +0530 | [diff] [blame] | 104 | ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr, |
| 105 | DIV_ROUND_UP(dev_desc->blksz, sizeof(legacy_mbr))); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 106 | |
Peter Jones | c27e1c1 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 107 | if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1) |
Stephen Warren | 71b4e3b | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 108 | return -1; |
| 109 | |
Peter Jones | c27e1c1 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 110 | if (test_block_type((unsigned char *)mbr) != DOS_MBR) |
Stephen Warren | 71b4e3b | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 111 | return -1; |
| 112 | |
Peter Jones | c27e1c1 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 113 | if (dev_desc->sig_type == SIG_TYPE_NONE && |
| 114 | mbr->unique_mbr_signature != 0) { |
| 115 | dev_desc->sig_type = SIG_TYPE_MBR; |
| 116 | dev_desc->mbr_sig = mbr->unique_mbr_signature; |
| 117 | } |
Fabio Estevam | f0c82a8 | 2017-10-04 13:29:57 -0300 | [diff] [blame] | 118 | #else |
| 119 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
| 120 | |
| 121 | if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1) |
| 122 | return -1; |
| 123 | |
| 124 | if (test_block_type(buffer) != DOS_MBR) |
| 125 | return -1; |
| 126 | #endif |
Peter Jones | c27e1c1 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 127 | |
Stephen Warren | 71b4e3b | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 128 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* Print a partition that is relative to its Extended partition table |
| 132 | */ |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 133 | static void print_partition_extended(struct blk_desc *dev_desc, |
Stefan Monnier | 4c6e960 | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 134 | lbaint_t ext_part_sector, |
| 135 | lbaint_t relative, |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 136 | int part_num, unsigned int disksig) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 137 | { |
Eric Nelson | 193b260 | 2012-03-03 12:02:20 +0000 | [diff] [blame] | 138 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 139 | dos_partition_t *pt; |
| 140 | int i; |
| 141 | |
Paul Emge | 6149412 | 2019-07-08 16:37:03 -0700 | [diff] [blame] | 142 | /* set a maximum recursion level */ |
| 143 | if (part_num > MAX_EXT_PARTS) |
| 144 | { |
| 145 | printf("** Nested DOS partitions detected, stopping **\n"); |
| 146 | return; |
| 147 | } |
| 148 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 149 | if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) { |
Stefan Monnier | 4c6e960 | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 150 | printf ("** Can't read partition table on %d:" LBAFU " **\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 151 | dev_desc->devnum, ext_part_sector); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 152 | return; |
| 153 | } |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 154 | i=test_block_type(buffer); |
Stephen Warren | 71b4e3b | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 155 | if (i != DOS_MBR) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 156 | printf ("bad MBR sector signature 0x%02x%02x\n", |
| 157 | buffer[DOS_PART_MAGIC_OFFSET], |
| 158 | buffer[DOS_PART_MAGIC_OFFSET + 1]); |
| 159 | return; |
| 160 | } |
Stephen Warren | 71b4e3b | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 161 | |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 162 | if (!ext_part_sector) |
Marek Szyprowski | 552f79c | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 163 | disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]); |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 164 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 165 | /* Print all primary/logical partitions */ |
| 166 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 167 | for (i = 0; i < 4; i++, pt++) { |
| 168 | /* |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 169 | * fdisk does not show the extended partitions that |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 170 | * are not in the MBR |
| 171 | */ |
| 172 | |
| 173 | if ((pt->sys_ind != 0) && |
| 174 | (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) { |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 175 | print_one_part(pt, ext_part_sector, part_num, disksig); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* Reverse engr the fdisk part# assignment rule! */ |
| 179 | if ((ext_part_sector == 0) || |
| 180 | (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { |
| 181 | part_num++; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* Follows the extended partitions */ |
| 186 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 187 | for (i = 0; i < 4; i++, pt++) { |
| 188 | if (is_extended (pt->sys_ind)) { |
Stefan Monnier | 4c6e960 | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 189 | lbaint_t lba_start |
Marek Szyprowski | 552f79c | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 190 | = get_unaligned_le32 (pt->start4) + relative; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 191 | |
Stephen Warren | 61b80a5 | 2012-10-08 08:14:38 +0000 | [diff] [blame] | 192 | print_partition_extended(dev_desc, lba_start, |
| 193 | ext_part_sector == 0 ? lba_start : relative, |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 194 | part_num, disksig); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /* Print a partition that is relative to its Extended partition table |
| 203 | */ |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 204 | static int part_get_info_extended(struct blk_desc *dev_desc, |
| 205 | lbaint_t ext_part_sector, lbaint_t relative, |
| 206 | int part_num, int which_part, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 207 | struct disk_partition *info, uint disksig) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 208 | { |
Eric Nelson | 193b260 | 2012-03-03 12:02:20 +0000 | [diff] [blame] | 209 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
Marek Vasut | b393da5 | 2023-08-14 01:48:45 +0200 | [diff] [blame] | 210 | struct disk_partition wdinfo = { 0 }; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 211 | dos_partition_t *pt; |
Marek Vasut | b393da5 | 2023-08-14 01:48:45 +0200 | [diff] [blame] | 212 | int i, ret; |
Darwin Dingel | 2c1e1b6 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 213 | int dos_type; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 214 | |
Paul Emge | 6149412 | 2019-07-08 16:37:03 -0700 | [diff] [blame] | 215 | /* set a maximum recursion level */ |
| 216 | if (part_num > MAX_EXT_PARTS) |
| 217 | { |
| 218 | printf("** Nested DOS partitions detected, stopping **\n"); |
| 219 | return -1; |
| 220 | } |
| 221 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 222 | if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) { |
Stefan Monnier | 4c6e960 | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 223 | printf ("** Can't read partition table on %d:" LBAFU " **\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 224 | dev_desc->devnum, ext_part_sector); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 225 | return -1; |
| 226 | } |
| 227 | if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 || |
| 228 | buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) { |
| 229 | printf ("bad MBR sector signature 0x%02x%02x\n", |
| 230 | buffer[DOS_PART_MAGIC_OFFSET], |
| 231 | buffer[DOS_PART_MAGIC_OFFSET + 1]); |
| 232 | return -1; |
| 233 | } |
| 234 | |
Patrick Delaunay | 7328709 | 2017-01-27 11:00:42 +0100 | [diff] [blame] | 235 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
Stephen Warren | b287a87 | 2012-09-21 09:51:00 +0000 | [diff] [blame] | 236 | if (!ext_part_sector) |
Marek Szyprowski | 552f79c | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 237 | disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]); |
Stephen Warren | b287a87 | 2012-09-21 09:51:00 +0000 | [diff] [blame] | 238 | #endif |
| 239 | |
Marek Vasut | b393da5 | 2023-08-14 01:48:45 +0200 | [diff] [blame] | 240 | ret = part_get_info_whole_disk(dev_desc, &wdinfo); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 244 | /* Print all primary/logical partitions */ |
| 245 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 246 | for (i = 0; i < 4; i++, pt++) { |
| 247 | /* |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 248 | * fdisk does not show the extended partitions that |
| 249 | * are not in the MBR |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 250 | */ |
Daniel Mack | 9a2671f | 2009-09-28 11:40:38 +0200 | [diff] [blame] | 251 | if (((pt->boot_ind & ~0x80) == 0) && |
| 252 | (pt->sys_ind != 0) && |
wdenk | 0893c47 | 2003-05-20 14:25:27 +0000 | [diff] [blame] | 253 | (part_num == which_part) && |
Shawn Guo | 1c6d679 | 2017-11-02 16:46:34 +0800 | [diff] [blame] | 254 | (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) { |
Marek Vasut | b393da5 | 2023-08-14 01:48:45 +0200 | [diff] [blame] | 255 | if (wdinfo.blksz > DOS_PART_DEFAULT_SECTOR) |
| 256 | info->blksz = wdinfo.blksz; |
| 257 | else |
| 258 | info->blksz = DOS_PART_DEFAULT_SECTOR; |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 259 | info->start = (lbaint_t)(ext_part_sector + |
Marek Szyprowski | 552f79c | 2020-12-23 13:55:12 +0100 | [diff] [blame] | 260 | get_unaligned_le32(pt->start4)); |
| 261 | info->size = (lbaint_t)get_unaligned_le32(pt->size4); |
Petr Kulhavy | 2e790d5 | 2016-09-09 10:27:17 +0200 | [diff] [blame] | 262 | part_set_generic_name(dev_desc, part_num, |
| 263 | (char *)info->name); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 264 | /* sprintf(info->type, "%d, pt->sys_ind); */ |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 265 | strcpy((char *)info->type, "U-Boot"); |
Heinrich Schuchardt | 59a860d | 2020-03-19 13:49:34 +0100 | [diff] [blame] | 266 | info->bootable = get_bootable(pt); |
Patrick Delaunay | 7328709 | 2017-01-27 11:00:42 +0100 | [diff] [blame] | 267 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
Stephen Warren | b287a87 | 2012-09-21 09:51:00 +0000 | [diff] [blame] | 268 | sprintf(info->uuid, "%08x-%02x", disksig, part_num); |
| 269 | #endif |
Dalon Westergreen | 8d770f4 | 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 | 4c6e960 | 2015-08-25 15:24:13 -0400 | [diff] [blame] | 285 | lbaint_t lba_start |
Marek Szyprowski | 552f79c | 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 | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 288 | return part_get_info_extended(dev_desc, lba_start, |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 289 | ext_part_sector == 0 ? lba_start : relative, |
Stephen Warren | b287a87 | 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 | 2c1e1b6 | 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; |
| 299 | info->size = dev_desc->lba; |
Marek Vasut | b393da5 | 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 | 2c1e1b6 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 304 | info->bootable = 0; |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 305 | strcpy((char *)info->type, "U-Boot"); |
Patrick Delaunay | 7328709 | 2017-01-27 11:00:42 +0100 | [diff] [blame] | 306 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
Darwin Dingel | 2c1e1b6 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 307 | info->uuid[0] = 0; |
| 308 | #endif |
| 309 | return 0; |
| 310 | } |
| 311 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 312 | return -1; |
| 313 | } |
| 314 | |
Marek Szyprowski | cbea647 | 2020-12-23 13:55:13 +0100 | [diff] [blame] | 315 | static void __maybe_unused part_print_dos(struct blk_desc *dev_desc) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 316 | { |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 317 | printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n"); |
| 318 | print_partition_extended(dev_desc, 0, 0, 1, 0); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Marek Szyprowski | cbea647 | 2020-12-23 13:55:13 +0100 | [diff] [blame] | 321 | static int __maybe_unused part_get_info_dos(struct blk_desc *dev_desc, int part, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 322 | struct disk_partition *info) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 323 | { |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 324 | return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 327 | int is_valid_dos_buf(void *buf) |
| 328 | { |
| 329 | return test_block_type(buf) == DOS_MBR ? 0 : -1; |
| 330 | } |
| 331 | |
Simon Glass | c4001de | 2023-02-05 15:36:33 -0700 | [diff] [blame] | 332 | #if IS_ENABLED(CONFIG_CMD_MBR) |
Marek Szyprowski | 6a60b67 | 2020-12-23 13:55:14 +0100 | [diff] [blame] | 333 | static void lba_to_chs(lbaint_t lba, unsigned char *rc, unsigned char *rh, |
| 334 | unsigned char *rs) |
| 335 | { |
| 336 | unsigned int c, h, s; |
| 337 | /* use fixed CHS geometry */ |
| 338 | unsigned int sectpertrack = 63; |
| 339 | unsigned int heads = 255; |
| 340 | |
| 341 | c = (lba + 1) / sectpertrack / heads; |
| 342 | h = (lba + 1) / sectpertrack - c * heads; |
| 343 | s = (lba + 1) - (c * heads + h) * sectpertrack; |
| 344 | |
| 345 | if (c > 1023) { |
| 346 | c = 1023; |
| 347 | h = 254; |
| 348 | s = 63; |
| 349 | } |
| 350 | |
| 351 | *rc = c & 0xff; |
| 352 | *rh = h; |
| 353 | *rs = s + ((c & 0x300) >> 2); |
| 354 | } |
| 355 | |
| 356 | static void mbr_fill_pt_entry(dos_partition_t *pt, lbaint_t start, |
| 357 | lbaint_t relative, lbaint_t size, uchar sys_ind, bool bootable) |
| 358 | { |
| 359 | pt->boot_ind = bootable ? 0x80 : 0x00; |
| 360 | pt->sys_ind = sys_ind; |
| 361 | lba_to_chs(start, &pt->cyl, &pt->head, &pt->sector); |
| 362 | lba_to_chs(start + size - 1, &pt->end_cyl, &pt->end_head, &pt->end_sector); |
| 363 | put_unaligned_le32(relative, &pt->start4); |
| 364 | put_unaligned_le32(size, &pt->size4); |
| 365 | } |
| 366 | |
| 367 | int write_mbr_partitions(struct blk_desc *dev, |
| 368 | struct disk_partition *p, int count, unsigned int disksig) |
| 369 | { |
| 370 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev->blksz); |
| 371 | lbaint_t ext_part_start = 0, ext_part_size = 0, ext_part_sect = 0; |
| 372 | dos_partition_t *pt; |
| 373 | int i; |
| 374 | |
| 375 | memset(buffer, 0, dev->blksz); |
| 376 | buffer[DOS_PART_MAGIC_OFFSET] = 0x55; |
| 377 | buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa; |
| 378 | put_unaligned_le32(disksig, &buffer[DOS_PART_DISKSIG_OFFSET]); |
| 379 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 380 | |
| 381 | /* create all primary partitions */ |
| 382 | for (i = 0; i < 4 && i < count; i++, pt++) { |
| 383 | mbr_fill_pt_entry(pt, p[i].start, p[i].start, p[i].size, |
| 384 | p[i].sys_ind, p[i].bootable); |
| 385 | if (is_extended(p[i].sys_ind)) { |
| 386 | ext_part_start = p[i].start; |
| 387 | ext_part_size = p[i].size; |
| 388 | ext_part_sect = p[i].start; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | if (i < count && !ext_part_start) { |
| 393 | printf("%s: extended partition is needed for more than 4 partitions\n", |
| 394 | __func__); |
| 395 | return -1; |
| 396 | } |
| 397 | |
| 398 | /* write MBR */ |
| 399 | if (blk_dwrite(dev, 0, 1, buffer) != 1) { |
| 400 | printf("%s: failed writing 'MBR' (1 blks at 0x0)\n", |
| 401 | __func__); |
| 402 | return -1; |
| 403 | } |
| 404 | |
| 405 | /* create extended volumes */ |
| 406 | for (; i < count; i++) { |
| 407 | lbaint_t next_ebr = 0; |
| 408 | |
| 409 | memset(buffer, 0, dev->blksz); |
| 410 | buffer[DOS_PART_MAGIC_OFFSET] = 0x55; |
| 411 | buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa; |
| 412 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 413 | |
| 414 | mbr_fill_pt_entry(pt, p[i].start, p[i].start - ext_part_sect, |
| 415 | p[i].size, p[i].sys_ind, p[i].bootable); |
| 416 | |
| 417 | if (i + 1 < count) { |
| 418 | pt++; |
| 419 | next_ebr = p[i].start + p[i].size; |
| 420 | mbr_fill_pt_entry(pt, next_ebr, |
| 421 | next_ebr - ext_part_start, |
| 422 | p[i+1].start + p[i+1].size - next_ebr, |
| 423 | DOS_PART_TYPE_EXTENDED, 0); |
| 424 | } |
| 425 | |
| 426 | /* write EBR */ |
| 427 | if (blk_dwrite(dev, ext_part_sect, 1, buffer) != 1) { |
| 428 | printf("%s: failed writing 'EBR' (1 blks at 0x%lx)\n", |
| 429 | __func__, ext_part_sect); |
| 430 | return -1; |
| 431 | } |
| 432 | ext_part_sect = next_ebr; |
| 433 | } |
| 434 | |
Gary Bisson | 75b4b62 | 2021-01-28 09:10:07 +0100 | [diff] [blame] | 435 | /* Update the partition table entries*/ |
Christian Melki | 34be560 | 2021-06-07 11:21:15 +0200 | [diff] [blame] | 436 | part_init(dev); |
Gary Bisson | 75b4b62 | 2021-01-28 09:10:07 +0100 | [diff] [blame] | 437 | |
Marek Szyprowski | 6a60b67 | 2020-12-23 13:55:14 +0100 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | int layout_mbr_partitions(struct disk_partition *p, int count, |
| 442 | lbaint_t total_sectors) |
| 443 | { |
| 444 | struct disk_partition *ext = NULL; |
| 445 | int i, j; |
| 446 | lbaint_t ext_vol_start; |
| 447 | |
| 448 | /* calculate primary partitions start and size if needed */ |
| 449 | if (!p[0].start) |
| 450 | p[0].start = DOS_PART_DEFAULT_GAP; |
| 451 | for (i = 0; i < 4 && i < count; i++) { |
| 452 | if (!p[i].start) |
| 453 | p[i].start = p[i - 1].start + p[i - 1].size; |
| 454 | if (!p[i].size) { |
| 455 | lbaint_t end = total_sectors; |
| 456 | lbaint_t allocated = 0; |
| 457 | |
| 458 | for (j = i + 1; j < 4 && j < count; j++) { |
| 459 | if (p[j].start) { |
| 460 | end = p[j].start; |
| 461 | break; |
| 462 | } |
| 463 | allocated += p[j].size; |
| 464 | } |
| 465 | p[i].size = end - allocated - p[i].start; |
| 466 | } |
| 467 | if (p[i].sys_ind == 0x05) |
| 468 | ext = &p[i]; |
| 469 | } |
| 470 | |
Simon Glass | b94d204 | 2021-10-23 17:26:02 -0600 | [diff] [blame] | 471 | if (count < 4) |
| 472 | return 0; |
| 473 | |
| 474 | if (!ext) { |
| 475 | log_err("extended partition is needed for more than 4 partitions\n"); |
| 476 | return -EINVAL; |
Marek Szyprowski | 6a60b67 | 2020-12-23 13:55:14 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* calculate extended volumes start and size if needed */ |
| 480 | ext_vol_start = ext->start; |
| 481 | for (i = 4; i < count; i++) { |
| 482 | if (!p[i].start) |
| 483 | p[i].start = ext_vol_start + DOS_PART_DEFAULT_GAP; |
| 484 | if (!p[i].size) { |
| 485 | lbaint_t end = ext->start + ext->size; |
| 486 | lbaint_t allocated = 0; |
| 487 | |
| 488 | for (j = i + 1; j < count; j++) { |
| 489 | if (p[j].start) { |
| 490 | end = p[j].start - DOS_PART_DEFAULT_GAP; |
| 491 | break; |
| 492 | } |
| 493 | allocated += p[j].size + DOS_PART_DEFAULT_GAP; |
| 494 | } |
| 495 | p[i].size = end - allocated - p[i].start; |
| 496 | } |
| 497 | ext_vol_start = p[i].start + p[i].size; |
| 498 | } |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | #endif |
| 503 | |
Marek Szyprowski | 29ee06b | 2020-12-23 13:55:10 +0100 | [diff] [blame] | 504 | int write_mbr_sector(struct blk_desc *dev_desc, void *buf) |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 505 | { |
| 506 | if (is_valid_dos_buf(buf)) |
| 507 | return -1; |
| 508 | |
| 509 | /* write MBR */ |
| 510 | if (blk_dwrite(dev_desc, 0, 1, buf) != 1) { |
| 511 | printf("%s: failed writing '%s' (1 blks at 0x0)\n", |
| 512 | __func__, "MBR"); |
| 513 | return 1; |
| 514 | } |
| 515 | |
Gary Bisson | 75b4b62 | 2021-01-28 09:10:07 +0100 | [diff] [blame] | 516 | /* Update the partition table entries*/ |
| 517 | part_init(dev_desc); |
| 518 | |
Petr Kulhavy | 9f174c9 | 2016-09-09 10:27:16 +0200 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 522 | U_BOOT_PART_TYPE(dos) = { |
| 523 | .name = "DOS", |
| 524 | .part_type = PART_TYPE_DOS, |
Petr Kulhavy | 712257e | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 525 | .max_entries = DOS_ENTRY_NUMBERS, |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 526 | .get_info = part_get_info_ptr(part_get_info_dos), |
Simon Glass | 0f1c952 | 2016-02-29 15:26:04 -0700 | [diff] [blame] | 527 | .print = part_print_ptr(part_print_dos), |
| 528 | .test = part_test_dos, |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 529 | }; |