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