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