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
21 #include <asm/unaligned.h>
25 #ifdef CONFIG_HAVE_BLOCK_DEVICE
27 #define DOS_PART_DEFAULT_SECTOR 512
29 /* should this be configurable? It looks like it's not very common at all
30 * to use large numbers of partitions */
31 #define MAX_EXT_PARTS 256
33 static inline int is_extended(int part_type)
35 return (part_type == DOS_PART_TYPE_EXTENDED ||
36 part_type == DOS_PART_TYPE_EXTENDED_LBA ||
37 part_type == DOS_PART_TYPE_EXTENDED_LINUX);
40 static int get_bootable(dos_partition_t *p)
44 if (p->sys_ind == 0xef)
45 ret |= PART_EFI_SYSTEM_PARTITION;
46 if (p->boot_ind == 0x80)
51 static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
52 int part_num, unsigned int disksig)
54 lbaint_t lba_start = ext_part_sector + get_unaligned_le32(p->start4);
55 lbaint_t lba_size = get_unaligned_le32(p->size4);
57 printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
58 "u\t%08x-%02x\t%02x%s%s\n",
59 part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
60 (is_extended(p->sys_ind) ? " Extd" : ""),
61 (get_bootable(p) ? " Boot" : ""));
64 static int test_block_type(unsigned char *buffer)
67 struct dos_partition *p;
70 if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
71 (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
73 } /* no DOS Signature at all */
74 p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
76 /* Check that the boot indicators are valid and count the partitions. */
77 for (slot = 0; slot < 4; ++slot, ++p) {
78 if (p->boot_ind != 0 && p->boot_ind != 0x80)
85 * If the partition table is invalid or empty,
86 * check if this is a DOS PBR
88 if (slot != 4 || !part_count) {
89 if (!strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
91 !strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
93 return DOS_PBR; /* This is a DOS PBR and not an MBR */
96 return DOS_MBR; /* This is an DOS MBR */
98 /* This is neither a DOS MBR nor a DOS PBR */
102 static int part_test_dos(struct blk_desc *dev_desc)
104 #ifndef CONFIG_SPL_BUILD
105 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr,
106 DIV_ROUND_UP(dev_desc->blksz, sizeof(legacy_mbr)));
108 if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1)
111 if (test_block_type((unsigned char *)mbr) != DOS_MBR)
114 if (dev_desc->sig_type == SIG_TYPE_NONE &&
115 mbr->unique_mbr_signature != 0) {
116 dev_desc->sig_type = SIG_TYPE_MBR;
117 dev_desc->mbr_sig = mbr->unique_mbr_signature;
120 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
122 if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
125 if (test_block_type(buffer) != DOS_MBR)
132 /* Print a partition that is relative to its Extended partition table
134 static void print_partition_extended(struct blk_desc *dev_desc,
135 lbaint_t ext_part_sector,
137 int part_num, unsigned int disksig)
139 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
143 /* set a maximum recursion level */
144 if (part_num > MAX_EXT_PARTS)
146 printf("** Nested DOS partitions detected, stopping **\n");
150 if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
151 printf ("** Can't read partition table on %d:" LBAFU " **\n",
152 dev_desc->devnum, ext_part_sector);
155 i=test_block_type(buffer);
157 printf ("bad MBR sector signature 0x%02x%02x\n",
158 buffer[DOS_PART_MAGIC_OFFSET],
159 buffer[DOS_PART_MAGIC_OFFSET + 1]);
163 if (!ext_part_sector)
164 disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
166 /* Print all primary/logical partitions */
167 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
168 for (i = 0; i < 4; i++, pt++) {
170 * fdisk does not show the extended partitions that
174 if ((pt->sys_ind != 0) &&
175 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
176 print_one_part(pt, ext_part_sector, part_num, disksig);
179 /* Reverse engr the fdisk part# assignment rule! */
180 if ((ext_part_sector == 0) ||
181 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
186 /* Follows the extended partitions */
187 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
188 for (i = 0; i < 4; i++, pt++) {
189 if (is_extended (pt->sys_ind)) {
191 = get_unaligned_le32 (pt->start4) + relative;
193 print_partition_extended(dev_desc, lba_start,
194 ext_part_sector == 0 ? lba_start : relative,
203 /* Print a partition that is relative to its Extended partition table
205 static int part_get_info_extended(struct blk_desc *dev_desc,
206 lbaint_t ext_part_sector, lbaint_t relative,
207 int part_num, int which_part,
208 struct disk_partition *info, uint disksig)
210 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
215 /* set a maximum recursion level */
216 if (part_num > MAX_EXT_PARTS)
218 printf("** Nested DOS partitions detected, stopping **\n");
222 if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
223 printf ("** Can't read partition table on %d:" LBAFU " **\n",
224 dev_desc->devnum, ext_part_sector);
227 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
228 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
229 printf ("bad MBR sector signature 0x%02x%02x\n",
230 buffer[DOS_PART_MAGIC_OFFSET],
231 buffer[DOS_PART_MAGIC_OFFSET + 1]);
235 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
236 if (!ext_part_sector)
237 disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
240 /* Print all primary/logical partitions */
241 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
242 for (i = 0; i < 4; i++, pt++) {
244 * fdisk does not show the extended partitions that
247 if (((pt->boot_ind & ~0x80) == 0) &&
248 (pt->sys_ind != 0) &&
249 (part_num == which_part) &&
250 (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
251 info->blksz = DOS_PART_DEFAULT_SECTOR;
252 info->start = (lbaint_t)(ext_part_sector +
253 get_unaligned_le32(pt->start4));
254 info->size = (lbaint_t)get_unaligned_le32(pt->size4);
255 part_set_generic_name(dev_desc, part_num,
257 /* sprintf(info->type, "%d, pt->sys_ind); */
258 strcpy((char *)info->type, "U-Boot");
259 info->bootable = get_bootable(pt);
260 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
261 sprintf(info->uuid, "%08x-%02x", disksig, part_num);
263 info->sys_ind = pt->sys_ind;
267 /* Reverse engr the fdisk part# assignment rule! */
268 if ((ext_part_sector == 0) ||
269 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
274 /* Follows the extended partitions */
275 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
276 for (i = 0; i < 4; i++, pt++) {
277 if (is_extended (pt->sys_ind)) {
279 = get_unaligned_le32 (pt->start4) + relative;
281 return part_get_info_extended(dev_desc, lba_start,
282 ext_part_sector == 0 ? lba_start : relative,
283 part_num, which_part, info, disksig);
287 /* Check for DOS PBR if no partition is found */
288 dos_type = test_block_type(buffer);
290 if (dos_type == DOS_PBR) {
292 info->size = dev_desc->lba;
293 info->blksz = DOS_PART_DEFAULT_SECTOR;
295 strcpy((char *)info->type, "U-Boot");
296 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
305 void part_print_dos(struct blk_desc *dev_desc)
307 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
308 print_partition_extended(dev_desc, 0, 0, 1, 0);
311 int part_get_info_dos(struct blk_desc *dev_desc, int part,
312 struct disk_partition *info)
314 return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0);
317 int is_valid_dos_buf(void *buf)
319 return test_block_type(buf) == DOS_MBR ? 0 : -1;
322 int write_mbr_sector(struct blk_desc *dev_desc, void *buf)
324 if (is_valid_dos_buf(buf))
328 if (blk_dwrite(dev_desc, 0, 1, buf) != 1) {
329 printf("%s: failed writing '%s' (1 blks at 0x0)\n",
337 U_BOOT_PART_TYPE(dos) = {
339 .part_type = PART_TYPE_DOS,
340 .max_entries = DOS_ENTRY_NUMBERS,
341 .get_info = part_get_info_ptr(part_get_info_dos),
342 .print = part_print_ptr(part_print_dos),
343 .test = part_test_dos,