3
* Raymond Lo, lo@routefree.com
4
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6
* SPDX-License-Identifier: GPL-2.0+
10
* Support for harddisk partitions.
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
22
#ifdef HAVE_BLOCK_DEVICE
24
/* Convert char[4] in little endian format to the host format integer
26
static inline int le32_to_int(unsigned char *le32)
28
return ((le32[3] << 24) +
35
static inline int is_extended(int part_type)
37
return (part_type == 0x5 ||
42
static inline int is_bootable(dos_partition_t *p)
44
return p->boot_ind == 0x80;
47
static void print_one_part(dos_partition_t *p, int ext_part_sector,
48
int part_num, unsigned int disksig)
50
int lba_start = ext_part_sector + le32_to_int (p->start4);
51
int lba_size = le32_to_int (p->size4);
53
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,
55
(is_extended(p->sys_ind) ? " Extd" : ""),
56
(is_bootable(p) ? " Boot" : ""));
59
static int test_block_type(unsigned char *buffer)
62
struct dos_partition *p;
64
if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
65
(buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
67
} /* no DOS Signature at all */
68
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) {
72
(strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
74
strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
76
return DOS_PBR; /* is PBR */
82
return DOS_MBR; /* Is MBR */
86
int test_part_dos (block_dev_desc_t *dev_desc)
88
ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
90
if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1)
93
if (test_block_type(buffer) != DOS_MBR)
99
/* Print a partition that is relative to its Extended partition table
101
static void print_partition_extended(block_dev_desc_t *dev_desc,
102
int ext_part_sector, int relative,
103
int part_num, unsigned int disksig)
105
ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
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);
114
i=test_block_type(buffer);
116
printf ("bad MBR sector signature 0x%02x%02x\n",
117
buffer[DOS_PART_MAGIC_OFFSET],
118
buffer[DOS_PART_MAGIC_OFFSET + 1]);
122
if (!ext_part_sector)
123
disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
125
/* Print all primary/logical partitions */
126
pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
127
for (i = 0; i < 4; i++, pt++) {
129
* fdisk does not show the extended partitions that
133
if ((pt->sys_ind != 0) &&
134
(ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
135
print_one_part(pt, ext_part_sector, part_num, disksig);
138
/* Reverse engr the fdisk part# assignment rule! */
139
if ((ext_part_sector == 0) ||
140
(pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
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;
151
print_partition_extended(dev_desc, lba_start,
152
ext_part_sector == 0 ? lba_start : relative,
161
/* Print a partition that is relative to its Extended partition table
163
static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector,
164
int relative, int part_num,
165
int which_part, disk_partition_t *info,
166
unsigned int disksig)
168
ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
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);
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]);
185
#ifdef CONFIG_PARTITION_UUIDS
186
if (!ext_part_sector)
187
disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
190
/* Print all primary/logical partitions */
191
pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
192
for (i = 0; i < 4; i++, pt++) {
194
* fdisk does not show the extended partitions that
197
if (((pt->boot_ind & ~0x80) == 0) &&
198
(pt->sys_ind != 0) &&
199
(part_num == which_part) &&
200
(is_extended(pt->sys_ind) == 0)) {
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) {
208
sprintf ((char *)info->name, "hd%c%d",
209
'a' + dev_desc->dev, part_num);
212
sprintf ((char *)info->name, "sd%c%d",
213
'a' + dev_desc->dev, part_num);
216
sprintf ((char *)info->name, "usbd%c%d",
217
'a' + dev_desc->dev, part_num);
220
sprintf ((char *)info->name, "docd%c%d",
221
'a' + dev_desc->dev, part_num);
224
sprintf ((char *)info->name, "xx%c%d",
225
'a' + dev_desc->dev, part_num);
228
/* sprintf(info->type, "%d, pt->sys_ind); */
229
sprintf ((char *)info->type, "U-Boot");
230
info->bootable = is_bootable(pt);
231
#ifdef CONFIG_PARTITION_UUIDS
232
sprintf(info->uuid, "%08x-%02x", disksig, part_num);
237
/* Reverse engr the fdisk part# assignment rule! */
238
if ((ext_part_sector == 0) ||
239
(pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
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;
250
return get_partition_info_extended (dev_desc, lba_start,
251
ext_part_sector == 0 ? lba_start : relative,
252
part_num, which_part, info, disksig);
258
void print_part_dos (block_dev_desc_t *dev_desc)
260
printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
261
print_partition_extended(dev_desc, 0, 0, 1, 0);
264
int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info)
266
return get_partition_info_extended(dev_desc, 0, 0, 1, part, info, 0);