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 #define DOS_PART_DEFAULT_SECTOR 512
26 /* Convert char[4] in little endian format to the host format integer
28 static inline int le32_to_int(unsigned char *le32)
30 return ((le32[3] << 24) +
37 static inline int is_extended(int part_type)
39 return (part_type == 0x5 ||
44 static inline int is_bootable(dos_partition_t *p)
46 return p->boot_ind == 0x80;
49 static void print_one_part(dos_partition_t *p, int ext_part_sector,
50 int part_num, unsigned int disksig)
52 int lba_start = ext_part_sector + le32_to_int (p->start4);
53 int lba_size = le32_to_int (p->size4);
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,
57 (is_extended(p->sys_ind) ? " Extd" : ""),
58 (is_bootable(p) ? " Boot" : ""));
61 static int test_block_type(unsigned char *buffer)
64 struct dos_partition *p;
66 if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
67 (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
69 } /* no DOS Signature at all */
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) {
74 (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
76 strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
78 return DOS_PBR; /* is PBR */
84 return DOS_MBR; /* Is MBR */
88 int test_part_dos (block_dev_desc_t *dev_desc)
90 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
92 if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1)
95 if (test_block_type(buffer) != DOS_MBR)
101 /* Print a partition that is relative to its Extended partition table
103 static void print_partition_extended(block_dev_desc_t *dev_desc,
104 int ext_part_sector, int relative,
105 int part_num, unsigned int disksig)
107 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
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);
116 i=test_block_type(buffer);
118 printf ("bad MBR sector signature 0x%02x%02x\n",
119 buffer[DOS_PART_MAGIC_OFFSET],
120 buffer[DOS_PART_MAGIC_OFFSET + 1]);
124 if (!ext_part_sector)
125 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
127 /* Print all primary/logical partitions */
128 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
129 for (i = 0; i < 4; i++, pt++) {
131 * fdisk does not show the extended partitions that
135 if ((pt->sys_ind != 0) &&
136 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
137 print_one_part(pt, ext_part_sector, part_num, disksig);
140 /* Reverse engr the fdisk part# assignment rule! */
141 if ((ext_part_sector == 0) ||
142 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
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;
153 print_partition_extended(dev_desc, lba_start,
154 ext_part_sector == 0 ? lba_start : relative,
163 /* Print a partition that is relative to its Extended partition table
165 static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector,
166 int relative, int part_num,
167 int which_part, disk_partition_t *info,
168 unsigned int disksig)
170 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
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);
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]);
188 #ifdef CONFIG_PARTITION_UUIDS
189 if (!ext_part_sector)
190 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
193 /* Print all primary/logical partitions */
194 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
195 for (i = 0; i < 4; i++, pt++) {
197 * fdisk does not show the extended partitions that
200 if (((pt->boot_ind & ~0x80) == 0) &&
201 (pt->sys_ind != 0) &&
202 (part_num == which_part) &&
203 (is_extended(pt->sys_ind) == 0)) {
204 info->blksz = DOS_PART_DEFAULT_SECTOR;
205 info->start = (lbaint_t)(ext_part_sector +
206 le32_to_int(pt->start4));
207 info->size = (lbaint_t)le32_to_int(pt->size4);
208 switch(dev_desc->if_type) {
212 sprintf ((char *)info->name, "hd%c%d",
213 'a' + dev_desc->dev, part_num);
216 sprintf ((char *)info->name, "sd%c%d",
217 'a' + dev_desc->dev, part_num);
220 sprintf ((char *)info->name, "usbd%c%d",
221 'a' + dev_desc->dev, part_num);
224 sprintf ((char *)info->name, "docd%c%d",
225 'a' + dev_desc->dev, part_num);
228 sprintf ((char *)info->name, "xx%c%d",
229 'a' + dev_desc->dev, part_num);
232 /* sprintf(info->type, "%d, pt->sys_ind); */
233 sprintf ((char *)info->type, "U-Boot");
234 info->bootable = is_bootable(pt);
235 #ifdef CONFIG_PARTITION_UUIDS
236 sprintf(info->uuid, "%08x-%02x", disksig, part_num);
241 /* Reverse engr the fdisk part# assignment rule! */
242 if ((ext_part_sector == 0) ||
243 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
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;
254 return get_partition_info_extended (dev_desc, lba_start,
255 ext_part_sector == 0 ? lba_start : relative,
256 part_num, which_part, info, disksig);
260 /* Check for DOS PBR if no partition is found */
261 dos_type = test_block_type(buffer);
263 if (dos_type == DOS_PBR) {
265 info->size = dev_desc->lba;
266 info->blksz = DOS_PART_DEFAULT_SECTOR;
268 sprintf ((char *)info->type, "U-Boot");
269 #ifdef CONFIG_PARTITION_UUIDS
278 void print_part_dos (block_dev_desc_t *dev_desc)
280 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
281 print_partition_extended(dev_desc, 0, 0, 1, 0);
284 int get_partition_info_dos (block_dev_desc_t *dev_desc, int part, disk_partition_t * info)
286 return get_partition_info_extended(dev_desc, 0, 0, 1, part, info, 0);