wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Raymond Lo, lo@routefree.com |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * Support for harddisk partitions. |
| 11 | * |
| 12 | * To be compatible with LinuxPPC and Apple we use the standard Apple |
| 13 | * SCSI disk partitioning scheme. For more information see: |
| 14 | * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92 |
| 15 | */ |
| 16 | |
| 17 | #include <common.h> |
| 18 | #include <command.h> |
| 19 | #include <ide.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 20 | #include <memalign.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 21 | #include "part_dos.h" |
| 22 | |
Stephen Warren | 8ec730d | 2013-02-28 15:03:46 +0000 | [diff] [blame] | 23 | #ifdef HAVE_BLOCK_DEVICE |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 24 | |
Darwin Dingel | 2c1e1b6 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 25 | #define DOS_PART_DEFAULT_SECTOR 512 |
| 26 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 27 | /* Convert char[4] in little endian format to the host format integer |
| 28 | */ |
| 29 | static inline int le32_to_int(unsigned char *le32) |
| 30 | { |
| 31 | return ((le32[3] << 24) + |
| 32 | (le32[2] << 16) + |
| 33 | (le32[1] << 8) + |
| 34 | le32[0] |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | static inline int is_extended(int part_type) |
| 39 | { |
| 40 | return (part_type == 0x5 || |
| 41 | part_type == 0xf || |
| 42 | part_type == 0x85); |
| 43 | } |
| 44 | |
Rob Herring | eda5111 | 2012-08-23 11:31:43 +0000 | [diff] [blame] | 45 | static inline int is_bootable(dos_partition_t *p) |
| 46 | { |
| 47 | return p->boot_ind == 0x80; |
| 48 | } |
| 49 | |
Stephen Warren | 61b80a5 | 2012-10-08 08:14:38 +0000 | [diff] [blame] | 50 | static void print_one_part(dos_partition_t *p, int 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 | { |
| 53 | int lba_start = ext_part_sector + le32_to_int (p->start4); |
| 54 | int lba_size = le32_to_int (p->size4); |
| 55 | |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 56 | printf("%3d\t%-10d\t%-10d\t%08x-%02x\t%02x%s%s\n", |
| 57 | part_num, lba_start, lba_size, disksig, part_num, p->sys_ind, |
Rob Herring | eda5111 | 2012-08-23 11:31:43 +0000 | [diff] [blame] | 58 | (is_extended(p->sys_ind) ? " Extd" : ""), |
| 59 | (is_bootable(p) ? " Boot" : "")); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 60 | } |
| 61 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 62 | static int test_block_type(unsigned char *buffer) |
| 63 | { |
Egbert Eich | 300494b | 2013-04-09 05:46:14 +0000 | [diff] [blame] | 64 | int slot; |
| 65 | struct dos_partition *p; |
| 66 | |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 67 | if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) || |
| 68 | (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) { |
| 69 | return (-1); |
| 70 | } /* no DOS Signature at all */ |
Egbert Eich | 300494b | 2013-04-09 05:46:14 +0000 | [diff] [blame] | 71 | p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET]; |
| 72 | for (slot = 0; slot < 3; slot++) { |
| 73 | if (p->boot_ind != 0 && p->boot_ind != 0x80) { |
| 74 | if (!slot && |
| 75 | (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET], |
| 76 | "FAT", 3) == 0 || |
| 77 | strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET], |
| 78 | "FAT32", 5) == 0)) { |
| 79 | return DOS_PBR; /* is PBR */ |
| 80 | } else { |
| 81 | return -1; |
| 82 | } |
| 83 | } |
Wolfgang Denk | 7b2290c | 2010-07-19 11:36:57 +0200 | [diff] [blame] | 84 | } |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 85 | return DOS_MBR; /* Is MBR */ |
| 86 | } |
| 87 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 88 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 89 | int test_part_dos (block_dev_desc_t *dev_desc) |
| 90 | { |
Eric Nelson | 193b260 | 2012-03-03 12:02:20 +0000 | [diff] [blame] | 91 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 92 | |
Stephen Warren | 71b4e3b | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 93 | if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) |
| 94 | return -1; |
| 95 | |
| 96 | if (test_block_type(buffer) != DOS_MBR) |
| 97 | return -1; |
| 98 | |
| 99 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* Print a partition that is relative to its Extended partition table |
| 103 | */ |
Stephen Warren | 61b80a5 | 2012-10-08 08:14:38 +0000 | [diff] [blame] | 104 | static void print_partition_extended(block_dev_desc_t *dev_desc, |
| 105 | int ext_part_sector, int relative, |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 106 | int part_num, unsigned int disksig) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 107 | { |
Eric Nelson | 193b260 | 2012-03-03 12:02:20 +0000 | [diff] [blame] | 108 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 109 | dos_partition_t *pt; |
| 110 | int i; |
| 111 | |
| 112 | if (dev_desc->block_read(dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) { |
| 113 | printf ("** Can't read partition table on %d:%d **\n", |
| 114 | dev_desc->dev, ext_part_sector); |
| 115 | return; |
| 116 | } |
wdenk | 2c9b05d | 2003-09-10 22:30:53 +0000 | [diff] [blame] | 117 | i=test_block_type(buffer); |
Stephen Warren | 71b4e3b | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 118 | if (i != DOS_MBR) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 119 | printf ("bad MBR sector signature 0x%02x%02x\n", |
| 120 | buffer[DOS_PART_MAGIC_OFFSET], |
| 121 | buffer[DOS_PART_MAGIC_OFFSET + 1]); |
| 122 | return; |
| 123 | } |
Stephen Warren | 71b4e3b | 2012-10-05 13:17:40 +0000 | [diff] [blame] | 124 | |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 125 | if (!ext_part_sector) |
| 126 | disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]); |
| 127 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 128 | /* Print all primary/logical partitions */ |
| 129 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 130 | for (i = 0; i < 4; i++, pt++) { |
| 131 | /* |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 132 | * fdisk does not show the extended partitions that |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 133 | * are not in the MBR |
| 134 | */ |
| 135 | |
| 136 | if ((pt->sys_ind != 0) && |
| 137 | (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) { |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 138 | print_one_part(pt, ext_part_sector, part_num, disksig); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* Reverse engr the fdisk part# assignment rule! */ |
| 142 | if ((ext_part_sector == 0) || |
| 143 | (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { |
| 144 | part_num++; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /* Follows the extended partitions */ |
| 149 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 150 | for (i = 0; i < 4; i++, pt++) { |
| 151 | if (is_extended (pt->sys_ind)) { |
| 152 | int lba_start = le32_to_int (pt->start4) + relative; |
| 153 | |
Stephen Warren | 61b80a5 | 2012-10-08 08:14:38 +0000 | [diff] [blame] | 154 | print_partition_extended(dev_desc, lba_start, |
| 155 | ext_part_sector == 0 ? lba_start : relative, |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 156 | part_num, disksig); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /* Print a partition that is relative to its Extended partition table |
| 165 | */ |
| 166 | static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector, |
| 167 | int relative, int part_num, |
Stephen Warren | b287a87 | 2012-09-21 09:51:00 +0000 | [diff] [blame] | 168 | int which_part, disk_partition_t *info, |
| 169 | unsigned int disksig) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 170 | { |
Eric Nelson | 193b260 | 2012-03-03 12:02:20 +0000 | [diff] [blame] | 171 | ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 172 | dos_partition_t *pt; |
| 173 | int i; |
Darwin Dingel | 2c1e1b6 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 174 | int dos_type; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 175 | |
| 176 | if (dev_desc->block_read (dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) { |
| 177 | printf ("** Can't read partition table on %d:%d **\n", |
| 178 | dev_desc->dev, ext_part_sector); |
| 179 | return -1; |
| 180 | } |
| 181 | if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 || |
| 182 | buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) { |
| 183 | printf ("bad MBR sector signature 0x%02x%02x\n", |
| 184 | buffer[DOS_PART_MAGIC_OFFSET], |
| 185 | buffer[DOS_PART_MAGIC_OFFSET + 1]); |
| 186 | return -1; |
| 187 | } |
| 188 | |
Stephen Warren | b287a87 | 2012-09-21 09:51:00 +0000 | [diff] [blame] | 189 | #ifdef CONFIG_PARTITION_UUIDS |
| 190 | if (!ext_part_sector) |
| 191 | disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]); |
| 192 | #endif |
| 193 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 194 | /* Print all primary/logical partitions */ |
| 195 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 196 | for (i = 0; i < 4; i++, pt++) { |
| 197 | /* |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 198 | * fdisk does not show the extended partitions that |
| 199 | * are not in the MBR |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 200 | */ |
Daniel Mack | 9a2671f | 2009-09-28 11:40:38 +0200 | [diff] [blame] | 201 | if (((pt->boot_ind & ~0x80) == 0) && |
| 202 | (pt->sys_ind != 0) && |
wdenk | 0893c47 | 2003-05-20 14:25:27 +0000 | [diff] [blame] | 203 | (part_num == which_part) && |
| 204 | (is_extended(pt->sys_ind) == 0)) { |
Darwin Dingel | 2c1e1b6 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 205 | info->blksz = DOS_PART_DEFAULT_SECTOR; |
Steve Rae | d30cbae | 2014-05-26 11:52:23 -0700 | [diff] [blame] | 206 | info->start = (lbaint_t)(ext_part_sector + |
| 207 | le32_to_int(pt->start4)); |
| 208 | info->size = (lbaint_t)le32_to_int(pt->size4); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 209 | switch(dev_desc->if_type) { |
| 210 | case IF_TYPE_IDE: |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 211 | case IF_TYPE_SATA: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 212 | case IF_TYPE_ATAPI: |
Wolfgang Denk | 03e5ce4 | 2009-07-28 22:35:39 +0200 | [diff] [blame] | 213 | sprintf ((char *)info->name, "hd%c%d", |
| 214 | 'a' + dev_desc->dev, part_num); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 215 | break; |
| 216 | case IF_TYPE_SCSI: |
Wolfgang Denk | 03e5ce4 | 2009-07-28 22:35:39 +0200 | [diff] [blame] | 217 | sprintf ((char *)info->name, "sd%c%d", |
| 218 | 'a' + dev_desc->dev, part_num); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 219 | break; |
| 220 | case IF_TYPE_USB: |
Wolfgang Denk | 03e5ce4 | 2009-07-28 22:35:39 +0200 | [diff] [blame] | 221 | sprintf ((char *)info->name, "usbd%c%d", |
| 222 | 'a' + dev_desc->dev, part_num); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 223 | break; |
| 224 | case IF_TYPE_DOC: |
Wolfgang Denk | 03e5ce4 | 2009-07-28 22:35:39 +0200 | [diff] [blame] | 225 | sprintf ((char *)info->name, "docd%c%d", |
| 226 | 'a' + dev_desc->dev, part_num); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 227 | break; |
| 228 | default: |
Wolfgang Denk | 03e5ce4 | 2009-07-28 22:35:39 +0200 | [diff] [blame] | 229 | sprintf ((char *)info->name, "xx%c%d", |
| 230 | 'a' + dev_desc->dev, part_num); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | /* sprintf(info->type, "%d, pt->sys_ind); */ |
Wolfgang Denk | 7fb5266 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 234 | sprintf ((char *)info->type, "U-Boot"); |
Rob Herring | eda5111 | 2012-08-23 11:31:43 +0000 | [diff] [blame] | 235 | info->bootable = is_bootable(pt); |
Stephen Warren | b287a87 | 2012-09-21 09:51:00 +0000 | [diff] [blame] | 236 | #ifdef CONFIG_PARTITION_UUIDS |
| 237 | sprintf(info->uuid, "%08x-%02x", disksig, part_num); |
| 238 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | /* Reverse engr the fdisk part# assignment rule! */ |
| 243 | if ((ext_part_sector == 0) || |
| 244 | (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { |
| 245 | part_num++; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* Follows the extended partitions */ |
| 250 | pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); |
| 251 | for (i = 0; i < 4; i++, pt++) { |
| 252 | if (is_extended (pt->sys_ind)) { |
| 253 | int lba_start = le32_to_int (pt->start4) + relative; |
| 254 | |
| 255 | return get_partition_info_extended (dev_desc, lba_start, |
| 256 | ext_part_sector == 0 ? lba_start : relative, |
Stephen Warren | b287a87 | 2012-09-21 09:51:00 +0000 | [diff] [blame] | 257 | part_num, which_part, info, disksig); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
Darwin Dingel | 2c1e1b6 | 2014-06-06 15:48:26 +1200 | [diff] [blame] | 260 | |
| 261 | /* Check for DOS PBR if no partition is found */ |
| 262 | dos_type = test_block_type(buffer); |
| 263 | |
| 264 | if (dos_type == DOS_PBR) { |
| 265 | info->start = 0; |
| 266 | info->size = dev_desc->lba; |
| 267 | info->blksz = DOS_PART_DEFAULT_SECTOR; |
| 268 | info->bootable = 0; |
| 269 | sprintf ((char *)info->type, "U-Boot"); |
| 270 | #ifdef CONFIG_PARTITION_UUIDS |
| 271 | info->uuid[0] = 0; |
| 272 | #endif |
| 273 | return 0; |
| 274 | } |
| 275 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | void print_part_dos (block_dev_desc_t *dev_desc) |
| 280 | { |
Stephen Warren | 37c88ea | 2012-10-08 08:14:40 +0000 | [diff] [blame] | 281 | printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n"); |
| 282 | print_partition_extended(dev_desc, 0, 0, 1, 0); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info) |
| 286 | { |
Stephen Warren | b287a87 | 2012-09-21 09:51:00 +0000 | [diff] [blame] | 287 | return get_partition_info_extended(dev_desc, 0, 0, 1, part, info, 0); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | |
Jon Loeliger | f7f54a8 | 2007-07-09 17:22:37 -0500 | [diff] [blame] | 291 | #endif |