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