1 // SPDX-License-Identifier: GPL-2.0+
4 * Raymond Lo, lo@routefree.com
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * Support for harddisk partitions.
11 * To be compatible with LinuxPPC and Apple we use the standard Apple
12 * SCSI disk partitioning scheme. For more information see:
13 * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
24 #ifdef CONFIG_HAVE_BLOCK_DEVICE
26 #define DOS_PART_DEFAULT_SECTOR 512
28 /* should this be configurable? It looks like it's not very common at all
29 * to use large numbers of partitions */
30 #define MAX_EXT_PARTS 256
32 /* Convert char[4] in little endian format to the host format integer
34 static inline unsigned int le32_to_int(unsigned char *le32)
36 return ((le32[3] << 24) +
43 static inline int is_extended(int part_type)
45 return (part_type == 0x5 ||
50 static int get_bootable(dos_partition_t *p)
54 if (p->sys_ind == 0xef)
55 ret |= PART_EFI_SYSTEM_PARTITION;
56 if (p->boot_ind == 0x80)
61 static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
62 int part_num, unsigned int disksig)
64 lbaint_t lba_start = ext_part_sector + le32_to_int (p->start4);
65 lbaint_t lba_size = le32_to_int (p->size4);
67 printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
68 "u\t%08x-%02x\t%02x%s%s\n",
69 part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
70 (is_extended(p->sys_ind) ? " Extd" : ""),
71 (get_bootable(p) ? " Boot" : ""));
74 static int test_block_type(unsigned char *buffer)
77 struct dos_partition *p;
80 if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
81 (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
83 } /* no DOS Signature at all */
84 p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
86 /* Check that the boot indicators are valid and count the partitions. */
87 for (slot = 0; slot < 4; ++slot, ++p) {
88 if (p->boot_ind != 0 && p->boot_ind != 0x80)
95 * If the partition table is invalid or empty,
96 * check if this is a DOS PBR
98 if (slot != 4 || !part_count) {
99 if (!strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
101 !strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
103 return DOS_PBR; /* This is a DOS PBR and not an MBR */
106 return DOS_MBR; /* This is an DOS MBR */
108 /* This is neither a DOS MBR nor a DOS PBR */
112 static int part_test_dos(struct blk_desc *dev_desc)
114 #ifndef CONFIG_SPL_BUILD
115 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr,
116 DIV_ROUND_UP(dev_desc->blksz, sizeof(legacy_mbr)));
118 if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1)
121 if (test_block_type((unsigned char *)mbr) != DOS_MBR)
124 if (dev_desc->sig_type == SIG_TYPE_NONE &&
125 mbr->unique_mbr_signature != 0) {
126 dev_desc->sig_type = SIG_TYPE_MBR;
127 dev_desc->mbr_sig = mbr->unique_mbr_signature;
130 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
132 if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
135 if (test_block_type(buffer) != DOS_MBR)
142 /* Print a partition that is relative to its Extended partition table
144 static void print_partition_extended(struct blk_desc *dev_desc,
145 lbaint_t ext_part_sector,
147 int part_num, unsigned int disksig)
149 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
153 /* set a maximum recursion level */
154 if (part_num > MAX_EXT_PARTS)
156 printf("** Nested DOS partitions detected, stopping **\n");
160 if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
161 printf ("** Can't read partition table on %d:" LBAFU " **\n",
162 dev_desc->devnum, ext_part_sector);
165 i=test_block_type(buffer);
167 printf ("bad MBR sector signature 0x%02x%02x\n",
168 buffer[DOS_PART_MAGIC_OFFSET],
169 buffer[DOS_PART_MAGIC_OFFSET + 1]);
173 if (!ext_part_sector)
174 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
176 /* Print all primary/logical partitions */
177 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
178 for (i = 0; i < 4; i++, pt++) {
180 * fdisk does not show the extended partitions that
184 if ((pt->sys_ind != 0) &&
185 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
186 print_one_part(pt, ext_part_sector, part_num, disksig);
189 /* Reverse engr the fdisk part# assignment rule! */
190 if ((ext_part_sector == 0) ||
191 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
196 /* Follows the extended partitions */
197 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
198 for (i = 0; i < 4; i++, pt++) {
199 if (is_extended (pt->sys_ind)) {
201 = le32_to_int (pt->start4) + relative;
203 print_partition_extended(dev_desc, lba_start,
204 ext_part_sector == 0 ? lba_start : relative,
213 /* Print a partition that is relative to its Extended partition table
215 static int part_get_info_extended(struct blk_desc *dev_desc,
216 lbaint_t ext_part_sector, lbaint_t relative,
217 int part_num, int which_part,
218 struct disk_partition *info, uint disksig)
220 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
225 /* set a maximum recursion level */
226 if (part_num > MAX_EXT_PARTS)
228 printf("** Nested DOS partitions detected, stopping **\n");
232 if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
233 printf ("** Can't read partition table on %d:" LBAFU " **\n",
234 dev_desc->devnum, ext_part_sector);
237 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
238 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
239 printf ("bad MBR sector signature 0x%02x%02x\n",
240 buffer[DOS_PART_MAGIC_OFFSET],
241 buffer[DOS_PART_MAGIC_OFFSET + 1]);
245 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
246 if (!ext_part_sector)
247 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
250 /* Print all primary/logical partitions */
251 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
252 for (i = 0; i < 4; i++, pt++) {
254 * fdisk does not show the extended partitions that
257 if (((pt->boot_ind & ~0x80) == 0) &&
258 (pt->sys_ind != 0) &&
259 (part_num == which_part) &&
260 (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
261 info->blksz = DOS_PART_DEFAULT_SECTOR;
262 info->start = (lbaint_t)(ext_part_sector +
263 le32_to_int(pt->start4));
264 info->size = (lbaint_t)le32_to_int(pt->size4);
265 part_set_generic_name(dev_desc, part_num,
267 /* sprintf(info->type, "%d, pt->sys_ind); */
268 strcpy((char *)info->type, "U-Boot");
269 info->bootable = get_bootable(pt);
270 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
271 sprintf(info->uuid, "%08x-%02x", disksig, part_num);
273 info->sys_ind = pt->sys_ind;
277 /* Reverse engr the fdisk part# assignment rule! */
278 if ((ext_part_sector == 0) ||
279 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
284 /* Follows the extended partitions */
285 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
286 for (i = 0; i < 4; i++, pt++) {
287 if (is_extended (pt->sys_ind)) {
289 = le32_to_int (pt->start4) + relative;
291 return part_get_info_extended(dev_desc, lba_start,
292 ext_part_sector == 0 ? lba_start : relative,
293 part_num, which_part, info, disksig);
297 /* Check for DOS PBR if no partition is found */
298 dos_type = test_block_type(buffer);
300 if (dos_type == DOS_PBR) {
302 info->size = dev_desc->lba;
303 info->blksz = DOS_PART_DEFAULT_SECTOR;
305 strcpy((char *)info->type, "U-Boot");
306 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
315 void part_print_dos(struct blk_desc *dev_desc)
317 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
318 print_partition_extended(dev_desc, 0, 0, 1, 0);
321 int part_get_info_dos(struct blk_desc *dev_desc, int part,
322 struct disk_partition *info)
324 return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0);
327 int is_valid_dos_buf(void *buf)
329 return test_block_type(buf) == DOS_MBR ? 0 : -1;
332 int write_mbr_sector(struct blk_desc *dev_desc, void *buf)
334 if (is_valid_dos_buf(buf))
338 if (blk_dwrite(dev_desc, 0, 1, buf) != 1) {
339 printf("%s: failed writing '%s' (1 blks at 0x0)\n",
347 U_BOOT_PART_TYPE(dos) = {
349 .part_type = PART_TYPE_DOS,
350 .max_entries = DOS_ENTRY_NUMBERS,
351 .get_info = part_get_info_ptr(part_get_info_dos),
352 .print = part_print_ptr(part_print_dos),
353 .test = part_test_dos,