blob: cf1a36ebb826699e9b391f6d68a9fad0c5806430 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
2 * (C) Copyright 2001
3 * Raymond Lo, lo@routefree.com
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenkfe8c2802002-11-03 00:38:21 +00007 */
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>
wdenkfe8c2802002-11-03 00:38:21 +000020#include "part_dos.h"
21
Stephen Warren2c1af9d2013-02-28 15:03:46 +000022#ifdef HAVE_BLOCK_DEVICE
wdenkfe8c2802002-11-03 00:38:21 +000023
Darwin Dingel4a36be92014-06-06 15:48:26 +120024#define DOS_PART_DEFAULT_SECTOR 512
25
wdenkfe8c2802002-11-03 00:38:21 +000026/* Convert char[4] in little endian format to the host format integer
27 */
28static 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
37static inline int is_extended(int part_type)
38{
39 return (part_type == 0x5 ||
40 part_type == 0xf ||
41 part_type == 0x85);
42}
43
Rob Herring40e0e562012-08-23 11:31:43 +000044static inline int is_bootable(dos_partition_t *p)
45{
46 return p->boot_ind == 0x80;
47}
48
Stephen Warren304b5712012-10-08 08:14:38 +000049static void print_one_part(dos_partition_t *p, int ext_part_sector,
Stephen Warrene2e9b372012-10-08 08:14:40 +000050 int part_num, unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +000051{
52 int lba_start = ext_part_sector + le32_to_int (p->start4);
53 int lba_size = le32_to_int (p->size4);
54
Stephen Warrene2e9b372012-10-08 08:14:40 +000055 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 Herring40e0e562012-08-23 11:31:43 +000057 (is_extended(p->sys_ind) ? " Extd" : ""),
58 (is_bootable(p) ? " Boot" : ""));
wdenkfe8c2802002-11-03 00:38:21 +000059}
60
wdenk7205e402003-09-10 22:30:53 +000061static int test_block_type(unsigned char *buffer)
62{
Egbert Eich9d956e02013-04-09 05:46:14 +000063 int slot;
64 struct dos_partition *p;
65
wdenk7205e402003-09-10 22:30:53 +000066 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 Eich9d956e02013-04-09 05:46:14 +000070 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 Denk66c2d732010-07-19 11:36:57 +020083 }
wdenk7205e402003-09-10 22:30:53 +000084 return DOS_MBR; /* Is MBR */
85}
86
wdenkfe8c2802002-11-03 00:38:21 +000087
wdenkfe8c2802002-11-03 00:38:21 +000088int test_part_dos (block_dev_desc_t *dev_desc)
89{
Eric Nelsondec049d2012-03-03 12:02:20 +000090 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +000091
Stephen Warrend1efb642012-10-05 13:17:40 +000092 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;
wdenkfe8c2802002-11-03 00:38:21 +000099}
100
101/* Print a partition that is relative to its Extended partition table
102 */
Stephen Warren304b5712012-10-08 08:14:38 +0000103static void print_partition_extended(block_dev_desc_t *dev_desc,
104 int ext_part_sector, int relative,
Stephen Warrene2e9b372012-10-08 08:14:40 +0000105 int part_num, unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +0000106{
Eric Nelsondec049d2012-03-03 12:02:20 +0000107 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +0000108 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 }
wdenk7205e402003-09-10 22:30:53 +0000116 i=test_block_type(buffer);
Stephen Warrend1efb642012-10-05 13:17:40 +0000117 if (i != DOS_MBR) {
wdenkfe8c2802002-11-03 00:38:21 +0000118 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 Warrend1efb642012-10-05 13:17:40 +0000123
Stephen Warrene2e9b372012-10-08 08:14:40 +0000124 if (!ext_part_sector)
125 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
126
wdenkfe8c2802002-11-03 00:38:21 +0000127 /* Print all primary/logical partitions */
128 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
129 for (i = 0; i < 4; i++, pt++) {
130 /*
wdenk8bde7f72003-06-27 21:31:46 +0000131 * fdisk does not show the extended partitions that
wdenkfe8c2802002-11-03 00:38:21 +0000132 * are not in the MBR
133 */
134
135 if ((pt->sys_ind != 0) &&
136 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
Stephen Warrene2e9b372012-10-08 08:14:40 +0000137 print_one_part(pt, ext_part_sector, part_num, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000138 }
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 Warren304b5712012-10-08 08:14:38 +0000153 print_partition_extended(dev_desc, lba_start,
154 ext_part_sector == 0 ? lba_start : relative,
Stephen Warrene2e9b372012-10-08 08:14:40 +0000155 part_num, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000156 }
157 }
158
159 return;
160}
161
162
163/* Print a partition that is relative to its Extended partition table
164 */
165static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector,
166 int relative, int part_num,
Stephen Warrend27b5f92012-09-21 09:51:00 +0000167 int which_part, disk_partition_t *info,
168 unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +0000169{
Eric Nelsondec049d2012-03-03 12:02:20 +0000170 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +0000171 dos_partition_t *pt;
172 int i;
Darwin Dingel4a36be92014-06-06 15:48:26 +1200173 int dos_type;
wdenkfe8c2802002-11-03 00:38:21 +0000174
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 Warrend27b5f92012-09-21 09:51:00 +0000188#ifdef CONFIG_PARTITION_UUIDS
189 if (!ext_part_sector)
190 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
191#endif
192
wdenkfe8c2802002-11-03 00:38:21 +0000193 /* Print all primary/logical partitions */
194 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
195 for (i = 0; i < 4; i++, pt++) {
196 /*
wdenk8bde7f72003-06-27 21:31:46 +0000197 * fdisk does not show the extended partitions that
198 * are not in the MBR
wdenkfe8c2802002-11-03 00:38:21 +0000199 */
Daniel Mack78f4ca72009-09-28 11:40:38 +0200200 if (((pt->boot_ind & ~0x80) == 0) &&
201 (pt->sys_ind != 0) &&
wdenk7f70e852003-05-20 14:25:27 +0000202 (part_num == which_part) &&
203 (is_extended(pt->sys_ind) == 0)) {
Darwin Dingel4a36be92014-06-06 15:48:26 +1200204 info->blksz = DOS_PART_DEFAULT_SECTOR;
Steve Raee04350d2014-05-26 11:52:23 -0700205 info->start = (lbaint_t)(ext_part_sector +
206 le32_to_int(pt->start4));
207 info->size = (lbaint_t)le32_to_int(pt->size4);
wdenkfe8c2802002-11-03 00:38:21 +0000208 switch(dev_desc->if_type) {
209 case IF_TYPE_IDE:
Dave Liuc7057b52008-03-26 22:49:44 +0800210 case IF_TYPE_SATA:
wdenkfe8c2802002-11-03 00:38:21 +0000211 case IF_TYPE_ATAPI:
Wolfgang Denk71665522009-07-28 22:35:39 +0200212 sprintf ((char *)info->name, "hd%c%d",
213 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000214 break;
215 case IF_TYPE_SCSI:
Wolfgang Denk71665522009-07-28 22:35:39 +0200216 sprintf ((char *)info->name, "sd%c%d",
217 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000218 break;
219 case IF_TYPE_USB:
Wolfgang Denk71665522009-07-28 22:35:39 +0200220 sprintf ((char *)info->name, "usbd%c%d",
221 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000222 break;
223 case IF_TYPE_DOC:
Wolfgang Denk71665522009-07-28 22:35:39 +0200224 sprintf ((char *)info->name, "docd%c%d",
225 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000226 break;
227 default:
Wolfgang Denk71665522009-07-28 22:35:39 +0200228 sprintf ((char *)info->name, "xx%c%d",
229 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000230 break;
231 }
232 /* sprintf(info->type, "%d, pt->sys_ind); */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200233 sprintf ((char *)info->type, "U-Boot");
Rob Herring40e0e562012-08-23 11:31:43 +0000234 info->bootable = is_bootable(pt);
Stephen Warrend27b5f92012-09-21 09:51:00 +0000235#ifdef CONFIG_PARTITION_UUIDS
236 sprintf(info->uuid, "%08x-%02x", disksig, part_num);
237#endif
wdenkfe8c2802002-11-03 00:38:21 +0000238 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 Warrend27b5f92012-09-21 09:51:00 +0000256 part_num, which_part, info, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000257 }
258 }
Darwin Dingel4a36be92014-06-06 15:48:26 +1200259
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
wdenkfe8c2802002-11-03 00:38:21 +0000275 return -1;
276}
277
278void print_part_dos (block_dev_desc_t *dev_desc)
279{
Stephen Warrene2e9b372012-10-08 08:14:40 +0000280 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
281 print_partition_extended(dev_desc, 0, 0, 1, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000282}
283
284int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info)
285{
Stephen Warrend27b5f92012-09-21 09:51:00 +0000286 return get_partition_info_extended(dev_desc, 0, 0, 1, part, info, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000287}
288
289
Jon Loeligercde5c642007-07-09 17:22:37 -0500290#endif