blob: 05c3933c322ea4e9ae15e6a08d8b4922f84d5a94 [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
24/* Convert char[4] in little endian format to the host format integer
25 */
26static inline int le32_to_int(unsigned char *le32)
27{
28 return ((le32[3] << 24) +
29 (le32[2] << 16) +
30 (le32[1] << 8) +
31 le32[0]
32 );
33}
34
35static inline int is_extended(int part_type)
36{
37 return (part_type == 0x5 ||
38 part_type == 0xf ||
39 part_type == 0x85);
40}
41
Rob Herring40e0e562012-08-23 11:31:43 +000042static inline int is_bootable(dos_partition_t *p)
43{
44 return p->boot_ind == 0x80;
45}
46
Stephen Warren304b5712012-10-08 08:14:38 +000047static void print_one_part(dos_partition_t *p, int ext_part_sector,
Stephen Warrene2e9b372012-10-08 08:14:40 +000048 int part_num, unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +000049{
50 int lba_start = ext_part_sector + le32_to_int (p->start4);
51 int lba_size = le32_to_int (p->size4);
52
Stephen Warrene2e9b372012-10-08 08:14:40 +000053 printf("%3d\t%-10d\t%-10d\t%08x-%02x\t%02x%s%s\n",
54 part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
Rob Herring40e0e562012-08-23 11:31:43 +000055 (is_extended(p->sys_ind) ? " Extd" : ""),
56 (is_bootable(p) ? " Boot" : ""));
wdenkfe8c2802002-11-03 00:38:21 +000057}
58
wdenk7205e402003-09-10 22:30:53 +000059static int test_block_type(unsigned char *buffer)
60{
Egbert Eich9d956e02013-04-09 05:46:14 +000061 int slot;
62 struct dos_partition *p;
63
wdenk7205e402003-09-10 22:30:53 +000064 if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
65 (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
66 return (-1);
67 } /* no DOS Signature at all */
Egbert Eich9d956e02013-04-09 05:46:14 +000068 p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
69 for (slot = 0; slot < 3; slot++) {
70 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
71 if (!slot &&
72 (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
73 "FAT", 3) == 0 ||
74 strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
75 "FAT32", 5) == 0)) {
76 return DOS_PBR; /* is PBR */
77 } else {
78 return -1;
79 }
80 }
Wolfgang Denk66c2d732010-07-19 11:36:57 +020081 }
wdenk7205e402003-09-10 22:30:53 +000082 return DOS_MBR; /* Is MBR */
83}
84
wdenkfe8c2802002-11-03 00:38:21 +000085
wdenkfe8c2802002-11-03 00:38:21 +000086int test_part_dos (block_dev_desc_t *dev_desc)
87{
Eric Nelsondec049d2012-03-03 12:02:20 +000088 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +000089
Stephen Warrend1efb642012-10-05 13:17:40 +000090 if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1)
91 return -1;
92
93 if (test_block_type(buffer) != DOS_MBR)
94 return -1;
95
96 return 0;
wdenkfe8c2802002-11-03 00:38:21 +000097}
98
99/* Print a partition that is relative to its Extended partition table
100 */
Stephen Warren304b5712012-10-08 08:14:38 +0000101static void print_partition_extended(block_dev_desc_t *dev_desc,
102 int ext_part_sector, int relative,
Stephen Warrene2e9b372012-10-08 08:14:40 +0000103 int part_num, unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +0000104{
Eric Nelsondec049d2012-03-03 12:02:20 +0000105 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +0000106 dos_partition_t *pt;
107 int i;
108
109 if (dev_desc->block_read(dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) {
110 printf ("** Can't read partition table on %d:%d **\n",
111 dev_desc->dev, ext_part_sector);
112 return;
113 }
wdenk7205e402003-09-10 22:30:53 +0000114 i=test_block_type(buffer);
Stephen Warrend1efb642012-10-05 13:17:40 +0000115 if (i != DOS_MBR) {
wdenkfe8c2802002-11-03 00:38:21 +0000116 printf ("bad MBR sector signature 0x%02x%02x\n",
117 buffer[DOS_PART_MAGIC_OFFSET],
118 buffer[DOS_PART_MAGIC_OFFSET + 1]);
119 return;
120 }
Stephen Warrend1efb642012-10-05 13:17:40 +0000121
Stephen Warrene2e9b372012-10-08 08:14:40 +0000122 if (!ext_part_sector)
123 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
124
wdenkfe8c2802002-11-03 00:38:21 +0000125 /* Print all primary/logical partitions */
126 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
127 for (i = 0; i < 4; i++, pt++) {
128 /*
wdenk8bde7f72003-06-27 21:31:46 +0000129 * fdisk does not show the extended partitions that
wdenkfe8c2802002-11-03 00:38:21 +0000130 * are not in the MBR
131 */
132
133 if ((pt->sys_ind != 0) &&
134 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
Stephen Warrene2e9b372012-10-08 08:14:40 +0000135 print_one_part(pt, ext_part_sector, part_num, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000136 }
137
138 /* Reverse engr the fdisk part# assignment rule! */
139 if ((ext_part_sector == 0) ||
140 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
141 part_num++;
142 }
143 }
144
145 /* Follows the extended partitions */
146 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
147 for (i = 0; i < 4; i++, pt++) {
148 if (is_extended (pt->sys_ind)) {
149 int lba_start = le32_to_int (pt->start4) + relative;
150
Stephen Warren304b5712012-10-08 08:14:38 +0000151 print_partition_extended(dev_desc, lba_start,
152 ext_part_sector == 0 ? lba_start : relative,
Stephen Warrene2e9b372012-10-08 08:14:40 +0000153 part_num, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000154 }
155 }
156
157 return;
158}
159
160
161/* Print a partition that is relative to its Extended partition table
162 */
163static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector,
164 int relative, int part_num,
Stephen Warrend27b5f92012-09-21 09:51:00 +0000165 int which_part, disk_partition_t *info,
166 unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +0000167{
Eric Nelsondec049d2012-03-03 12:02:20 +0000168 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +0000169 dos_partition_t *pt;
170 int i;
171
172 if (dev_desc->block_read (dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) {
173 printf ("** Can't read partition table on %d:%d **\n",
174 dev_desc->dev, ext_part_sector);
175 return -1;
176 }
177 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
178 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
179 printf ("bad MBR sector signature 0x%02x%02x\n",
180 buffer[DOS_PART_MAGIC_OFFSET],
181 buffer[DOS_PART_MAGIC_OFFSET + 1]);
182 return -1;
183 }
184
Stephen Warrend27b5f92012-09-21 09:51:00 +0000185#ifdef CONFIG_PARTITION_UUIDS
186 if (!ext_part_sector)
187 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
188#endif
189
wdenkfe8c2802002-11-03 00:38:21 +0000190 /* Print all primary/logical partitions */
191 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
192 for (i = 0; i < 4; i++, pt++) {
193 /*
wdenk8bde7f72003-06-27 21:31:46 +0000194 * fdisk does not show the extended partitions that
195 * are not in the MBR
wdenkfe8c2802002-11-03 00:38:21 +0000196 */
Daniel Mack78f4ca72009-09-28 11:40:38 +0200197 if (((pt->boot_ind & ~0x80) == 0) &&
198 (pt->sys_ind != 0) &&
wdenk7f70e852003-05-20 14:25:27 +0000199 (part_num == which_part) &&
200 (is_extended(pt->sys_ind) == 0)) {
wdenkfe8c2802002-11-03 00:38:21 +0000201 info->blksz = 512;
202 info->start = ext_part_sector + le32_to_int (pt->start4);
203 info->size = le32_to_int (pt->size4);
204 switch(dev_desc->if_type) {
205 case IF_TYPE_IDE:
Dave Liuc7057b52008-03-26 22:49:44 +0800206 case IF_TYPE_SATA:
wdenkfe8c2802002-11-03 00:38:21 +0000207 case IF_TYPE_ATAPI:
Wolfgang Denk71665522009-07-28 22:35:39 +0200208 sprintf ((char *)info->name, "hd%c%d",
209 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000210 break;
211 case IF_TYPE_SCSI:
Wolfgang Denk71665522009-07-28 22:35:39 +0200212 sprintf ((char *)info->name, "sd%c%d",
213 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000214 break;
215 case IF_TYPE_USB:
Wolfgang Denk71665522009-07-28 22:35:39 +0200216 sprintf ((char *)info->name, "usbd%c%d",
217 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000218 break;
219 case IF_TYPE_DOC:
Wolfgang Denk71665522009-07-28 22:35:39 +0200220 sprintf ((char *)info->name, "docd%c%d",
221 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000222 break;
223 default:
Wolfgang Denk71665522009-07-28 22:35:39 +0200224 sprintf ((char *)info->name, "xx%c%d",
225 'a' + dev_desc->dev, part_num);
wdenkfe8c2802002-11-03 00:38:21 +0000226 break;
227 }
228 /* sprintf(info->type, "%d, pt->sys_ind); */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200229 sprintf ((char *)info->type, "U-Boot");
Rob Herring40e0e562012-08-23 11:31:43 +0000230 info->bootable = is_bootable(pt);
Stephen Warrend27b5f92012-09-21 09:51:00 +0000231#ifdef CONFIG_PARTITION_UUIDS
232 sprintf(info->uuid, "%08x-%02x", disksig, part_num);
233#endif
wdenkfe8c2802002-11-03 00:38:21 +0000234 return 0;
235 }
236
237 /* Reverse engr the fdisk part# assignment rule! */
238 if ((ext_part_sector == 0) ||
239 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
240 part_num++;
241 }
242 }
243
244 /* Follows the extended partitions */
245 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
246 for (i = 0; i < 4; i++, pt++) {
247 if (is_extended (pt->sys_ind)) {
248 int lba_start = le32_to_int (pt->start4) + relative;
249
250 return get_partition_info_extended (dev_desc, lba_start,
251 ext_part_sector == 0 ? lba_start : relative,
Stephen Warrend27b5f92012-09-21 09:51:00 +0000252 part_num, which_part, info, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000253 }
254 }
255 return -1;
256}
257
258void print_part_dos (block_dev_desc_t *dev_desc)
259{
Stephen Warrene2e9b372012-10-08 08:14:40 +0000260 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
261 print_partition_extended(dev_desc, 0, 0, 1, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000262}
263
264int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info)
265{
Stephen Warrend27b5f92012-09-21 09:51:00 +0000266 return get_partition_info_extended(dev_desc, 0, 0, 1, part, info, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000267}
268
269
Jon Loeligercde5c642007-07-09 17:22:37 -0500270#endif