]> git.ipfire.org Git - thirdparty/u-boot.git/blame - disk/part_dos.c
part: Add a function to find the first bootable partition
[thirdparty/u-boot.git] / disk / part_dos.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
fe8c2806
WD
2/*
3 * (C) Copyright 2001
4 * Raymond Lo, lo@routefree.com
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
fe8c2806
WD
6 */
7
8/*
9 * Support for harddisk partitions.
10 *
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
14 */
15
16#include <common.h>
e6f6f9e6 17#include <blk.h>
fe8c2806
WD
18#include <command.h>
19#include <ide.h>
cf92e05c 20#include <memalign.h>
97163dd1 21#include <asm/unaligned.h>
cb571f91 22#include <linux/compiler.h>
fe8c2806 23#include "part_dos.h"
e6f6f9e6 24#include <part.h>
fe8c2806 25
4a36be9b
DD
26#define DOS_PART_DEFAULT_SECTOR 512
27
232e2f4f
PE
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
31
fe8c2806
WD
32static inline int is_extended(int part_type)
33{
80bd05f2
MS
34 return (part_type == DOS_PART_TYPE_EXTENDED ||
35 part_type == DOS_PART_TYPE_EXTENDED_LBA ||
36 part_type == DOS_PART_TYPE_EXTENDED_LINUX);
fe8c2806
WD
37}
38
25801acc 39static int get_bootable(dos_partition_t *p)
40e0e568 40{
25801acc
HS
41 int ret = 0;
42
43 if (p->sys_ind == 0xef)
44 ret |= PART_EFI_SYSTEM_PARTITION;
45 if (p->boot_ind == 0x80)
46 ret |= PART_BOOTABLE;
47 return ret;
40e0e568
RH
48}
49
d29892ba 50static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
e2e9b378 51 int part_num, unsigned int disksig)
fe8c2806 52{
97163dd1
MS
53 lbaint_t lba_start = ext_part_sector + get_unaligned_le32(p->start4);
54 lbaint_t lba_size = get_unaligned_le32(p->size4);
fe8c2806 55
d29892ba
SM
56 printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
57 "u\t%08x-%02x\t%02x%s%s\n",
e2e9b378 58 part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
40e0e568 59 (is_extended(p->sys_ind) ? " Extd" : ""),
25801acc 60 (get_bootable(p) ? " Boot" : ""));
fe8c2806
WD
61}
62
7205e407
WD
63static int test_block_type(unsigned char *buffer)
64{
9d956e0f
EE
65 int slot;
66 struct dos_partition *p;
34856b0f 67 int part_count = 0;
9d956e0f 68
7205e407
WD
69 if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
70 (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
71 return (-1);
72 } /* no DOS Signature at all */
9d956e0f 73 p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
34856b0f
HS
74
75 /* Check that the boot indicators are valid and count the partitions. */
76 for (slot = 0; slot < 4; ++slot, ++p) {
77 if (p->boot_ind != 0 && p->boot_ind != 0x80)
78 break;
79 if (p->sys_ind)
80 ++part_count;
66c2d73c 81 }
7205e407 82
34856b0f
HS
83 /*
84 * If the partition table is invalid or empty,
85 * check if this is a DOS PBR
86 */
87 if (slot != 4 || !part_count) {
88 if (!strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
89 "FAT", 3) ||
90 !strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
91 "FAT32", 5))
92 return DOS_PBR; /* This is a DOS PBR and not an MBR */
93 }
94 if (slot == 4)
95 return DOS_MBR; /* This is an DOS MBR */
96
97 /* This is neither a DOS MBR nor a DOS PBR */
98 return -1;
99}
fe8c2806 100
084bf4c2 101static int part_test_dos(struct blk_desc *dev_desc)
fe8c2806 102{
3ea05205 103#ifndef CONFIG_SPL_BUILD
7aed3d38
FA
104 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr,
105 DIV_ROUND_UP(dev_desc->blksz, sizeof(legacy_mbr)));
fe8c2806 106
ff98cb90 107 if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1)
d1efb644
SW
108 return -1;
109
ff98cb90 110 if (test_block_type((unsigned char *)mbr) != DOS_MBR)
d1efb644
SW
111 return -1;
112
ff98cb90
PJ
113 if (dev_desc->sig_type == SIG_TYPE_NONE &&
114 mbr->unique_mbr_signature != 0) {
115 dev_desc->sig_type = SIG_TYPE_MBR;
116 dev_desc->mbr_sig = mbr->unique_mbr_signature;
117 }
3ea05205
FE
118#else
119 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
120
121 if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
122 return -1;
123
124 if (test_block_type(buffer) != DOS_MBR)
125 return -1;
126#endif
ff98cb90 127
d1efb644 128 return 0;
fe8c2806
WD
129}
130
131/* Print a partition that is relative to its Extended partition table
132 */
4101f687 133static void print_partition_extended(struct blk_desc *dev_desc,
d29892ba
SM
134 lbaint_t ext_part_sector,
135 lbaint_t relative,
e2e9b378 136 int part_num, unsigned int disksig)
fe8c2806 137{
dec049d9 138 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
fe8c2806
WD
139 dos_partition_t *pt;
140 int i;
141
232e2f4f
PE
142 /* set a maximum recursion level */
143 if (part_num > MAX_EXT_PARTS)
144 {
145 printf("** Nested DOS partitions detected, stopping **\n");
146 return;
147 }
148
2a981dc2 149 if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
d29892ba 150 printf ("** Can't read partition table on %d:" LBAFU " **\n",
bcce53d0 151 dev_desc->devnum, ext_part_sector);
fe8c2806
WD
152 return;
153 }
7205e407 154 i=test_block_type(buffer);
d1efb644 155 if (i != DOS_MBR) {
fe8c2806
WD
156 printf ("bad MBR sector signature 0x%02x%02x\n",
157 buffer[DOS_PART_MAGIC_OFFSET],
158 buffer[DOS_PART_MAGIC_OFFSET + 1]);
159 return;
160 }
d1efb644 161
e2e9b378 162 if (!ext_part_sector)
97163dd1 163 disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
e2e9b378 164
fe8c2806
WD
165 /* Print all primary/logical partitions */
166 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
167 for (i = 0; i < 4; i++, pt++) {
168 /*
8bde7f77 169 * fdisk does not show the extended partitions that
fe8c2806
WD
170 * are not in the MBR
171 */
172
173 if ((pt->sys_ind != 0) &&
174 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
e2e9b378 175 print_one_part(pt, ext_part_sector, part_num, disksig);
fe8c2806
WD
176 }
177
178 /* Reverse engr the fdisk part# assignment rule! */
179 if ((ext_part_sector == 0) ||
180 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
181 part_num++;
182 }
183 }
184
185 /* Follows the extended partitions */
186 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
187 for (i = 0; i < 4; i++, pt++) {
188 if (is_extended (pt->sys_ind)) {
d29892ba 189 lbaint_t lba_start
97163dd1 190 = get_unaligned_le32 (pt->start4) + relative;
fe8c2806 191
304b5711
SW
192 print_partition_extended(dev_desc, lba_start,
193 ext_part_sector == 0 ? lba_start : relative,
e2e9b378 194 part_num, disksig);
fe8c2806
WD
195 }
196 }
197
198 return;
199}
200
201
202/* Print a partition that is relative to its Extended partition table
203 */
3e8bd469
SG
204static int part_get_info_extended(struct blk_desc *dev_desc,
205 lbaint_t ext_part_sector, lbaint_t relative,
206 int part_num, int which_part,
0528979f 207 struct disk_partition *info, uint disksig)
fe8c2806 208{
dec049d9 209 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
fe8c2806
WD
210 dos_partition_t *pt;
211 int i;
4a36be9b 212 int dos_type;
fe8c2806 213
232e2f4f
PE
214 /* set a maximum recursion level */
215 if (part_num > MAX_EXT_PARTS)
216 {
217 printf("** Nested DOS partitions detected, stopping **\n");
218 return -1;
219 }
220
2a981dc2 221 if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
d29892ba 222 printf ("** Can't read partition table on %d:" LBAFU " **\n",
bcce53d0 223 dev_desc->devnum, ext_part_sector);
fe8c2806
WD
224 return -1;
225 }
226 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
227 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
228 printf ("bad MBR sector signature 0x%02x%02x\n",
229 buffer[DOS_PART_MAGIC_OFFSET],
230 buffer[DOS_PART_MAGIC_OFFSET + 1]);
231 return -1;
232 }
233
b331cd62 234#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
d27b5f93 235 if (!ext_part_sector)
97163dd1 236 disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
d27b5f93
SW
237#endif
238
fe8c2806
WD
239 /* Print all primary/logical partitions */
240 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
241 for (i = 0; i < 4; i++, pt++) {
242 /*
8bde7f77
WD
243 * fdisk does not show the extended partitions that
244 * are not in the MBR
fe8c2806 245 */
78f4ca79
DM
246 if (((pt->boot_ind & ~0x80) == 0) &&
247 (pt->sys_ind != 0) &&
7f70e853 248 (part_num == which_part) &&
8f7102cf 249 (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
4a36be9b 250 info->blksz = DOS_PART_DEFAULT_SECTOR;
e04350d2 251 info->start = (lbaint_t)(ext_part_sector +
97163dd1
MS
252 get_unaligned_le32(pt->start4));
253 info->size = (lbaint_t)get_unaligned_le32(pt->size4);
da2ee24d
PK
254 part_set_generic_name(dev_desc, part_num,
255 (char *)info->name);
fe8c2806 256 /* sprintf(info->type, "%d, pt->sys_ind); */
192bc694 257 strcpy((char *)info->type, "U-Boot");
25801acc 258 info->bootable = get_bootable(pt);
b331cd62 259#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
d27b5f93
SW
260 sprintf(info->uuid, "%08x-%02x", disksig, part_num);
261#endif
f0fb4fa7 262 info->sys_ind = pt->sys_ind;
fe8c2806
WD
263 return 0;
264 }
265
266 /* Reverse engr the fdisk part# assignment rule! */
267 if ((ext_part_sector == 0) ||
268 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
269 part_num++;
270 }
271 }
272
273 /* Follows the extended partitions */
274 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
275 for (i = 0; i < 4; i++, pt++) {
276 if (is_extended (pt->sys_ind)) {
d29892ba 277 lbaint_t lba_start
97163dd1 278 = get_unaligned_le32 (pt->start4) + relative;
fe8c2806 279
3e8bd469 280 return part_get_info_extended(dev_desc, lba_start,
fe8c2806 281 ext_part_sector == 0 ? lba_start : relative,
d27b5f93 282 part_num, which_part, info, disksig);
fe8c2806
WD
283 }
284 }
4a36be9b
DD
285
286 /* Check for DOS PBR if no partition is found */
287 dos_type = test_block_type(buffer);
288
289 if (dos_type == DOS_PBR) {
290 info->start = 0;
291 info->size = dev_desc->lba;
292 info->blksz = DOS_PART_DEFAULT_SECTOR;
293 info->bootable = 0;
192bc694 294 strcpy((char *)info->type, "U-Boot");
b331cd62 295#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
4a36be9b
DD
296 info->uuid[0] = 0;
297#endif
298 return 0;
299 }
300
fe8c2806
WD
301 return -1;
302}
303
cb571f91 304static void __maybe_unused part_print_dos(struct blk_desc *dev_desc)
fe8c2806 305{
e2e9b378
SW
306 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
307 print_partition_extended(dev_desc, 0, 0, 1, 0);
fe8c2806
WD
308}
309
cb571f91 310static int __maybe_unused part_get_info_dos(struct blk_desc *dev_desc, int part,
0528979f 311 struct disk_partition *info)
fe8c2806 312{
3e8bd469 313 return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0);
fe8c2806
WD
314}
315
b6dd69a4
PK
316int is_valid_dos_buf(void *buf)
317{
318 return test_block_type(buf) == DOS_MBR ? 0 : -1;
319}
320
20bd5ac6
MS
321#if CONFIG_IS_ENABLED(CMD_MBR)
322static void lba_to_chs(lbaint_t lba, unsigned char *rc, unsigned char *rh,
323 unsigned char *rs)
324{
325 unsigned int c, h, s;
326 /* use fixed CHS geometry */
327 unsigned int sectpertrack = 63;
328 unsigned int heads = 255;
329
330 c = (lba + 1) / sectpertrack / heads;
331 h = (lba + 1) / sectpertrack - c * heads;
332 s = (lba + 1) - (c * heads + h) * sectpertrack;
333
334 if (c > 1023) {
335 c = 1023;
336 h = 254;
337 s = 63;
338 }
339
340 *rc = c & 0xff;
341 *rh = h;
342 *rs = s + ((c & 0x300) >> 2);
343}
344
345static void mbr_fill_pt_entry(dos_partition_t *pt, lbaint_t start,
346 lbaint_t relative, lbaint_t size, uchar sys_ind, bool bootable)
347{
348 pt->boot_ind = bootable ? 0x80 : 0x00;
349 pt->sys_ind = sys_ind;
350 lba_to_chs(start, &pt->cyl, &pt->head, &pt->sector);
351 lba_to_chs(start + size - 1, &pt->end_cyl, &pt->end_head, &pt->end_sector);
352 put_unaligned_le32(relative, &pt->start4);
353 put_unaligned_le32(size, &pt->size4);
354}
355
356int write_mbr_partitions(struct blk_desc *dev,
357 struct disk_partition *p, int count, unsigned int disksig)
358{
359 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev->blksz);
360 lbaint_t ext_part_start = 0, ext_part_size = 0, ext_part_sect = 0;
361 dos_partition_t *pt;
362 int i;
363
364 memset(buffer, 0, dev->blksz);
365 buffer[DOS_PART_MAGIC_OFFSET] = 0x55;
366 buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa;
367 put_unaligned_le32(disksig, &buffer[DOS_PART_DISKSIG_OFFSET]);
368 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
369
370 /* create all primary partitions */
371 for (i = 0; i < 4 && i < count; i++, pt++) {
372 mbr_fill_pt_entry(pt, p[i].start, p[i].start, p[i].size,
373 p[i].sys_ind, p[i].bootable);
374 if (is_extended(p[i].sys_ind)) {
375 ext_part_start = p[i].start;
376 ext_part_size = p[i].size;
377 ext_part_sect = p[i].start;
378 }
379 }
380
381 if (i < count && !ext_part_start) {
382 printf("%s: extended partition is needed for more than 4 partitions\n",
383 __func__);
384 return -1;
385 }
386
387 /* write MBR */
388 if (blk_dwrite(dev, 0, 1, buffer) != 1) {
389 printf("%s: failed writing 'MBR' (1 blks at 0x0)\n",
390 __func__);
391 return -1;
392 }
393
394 /* create extended volumes */
395 for (; i < count; i++) {
396 lbaint_t next_ebr = 0;
397
398 memset(buffer, 0, dev->blksz);
399 buffer[DOS_PART_MAGIC_OFFSET] = 0x55;
400 buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa;
401 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
402
403 mbr_fill_pt_entry(pt, p[i].start, p[i].start - ext_part_sect,
404 p[i].size, p[i].sys_ind, p[i].bootable);
405
406 if (i + 1 < count) {
407 pt++;
408 next_ebr = p[i].start + p[i].size;
409 mbr_fill_pt_entry(pt, next_ebr,
410 next_ebr - ext_part_start,
411 p[i+1].start + p[i+1].size - next_ebr,
412 DOS_PART_TYPE_EXTENDED, 0);
413 }
414
415 /* write EBR */
416 if (blk_dwrite(dev, ext_part_sect, 1, buffer) != 1) {
417 printf("%s: failed writing 'EBR' (1 blks at 0x%lx)\n",
418 __func__, ext_part_sect);
419 return -1;
420 }
421 ext_part_sect = next_ebr;
422 }
423
f14c5ee5 424 /* Update the partition table entries*/
c7d1b189 425 part_init(dev);
f14c5ee5 426
20bd5ac6
MS
427 return 0;
428}
429
430int layout_mbr_partitions(struct disk_partition *p, int count,
431 lbaint_t total_sectors)
432{
433 struct disk_partition *ext = NULL;
434 int i, j;
435 lbaint_t ext_vol_start;
436
437 /* calculate primary partitions start and size if needed */
438 if (!p[0].start)
439 p[0].start = DOS_PART_DEFAULT_GAP;
440 for (i = 0; i < 4 && i < count; i++) {
441 if (!p[i].start)
442 p[i].start = p[i - 1].start + p[i - 1].size;
443 if (!p[i].size) {
444 lbaint_t end = total_sectors;
445 lbaint_t allocated = 0;
446
447 for (j = i + 1; j < 4 && j < count; j++) {
448 if (p[j].start) {
449 end = p[j].start;
450 break;
451 }
452 allocated += p[j].size;
453 }
454 p[i].size = end - allocated - p[i].start;
455 }
456 if (p[i].sys_ind == 0x05)
457 ext = &p[i];
458 }
459
b7c2cc49
SG
460 if (count < 4)
461 return 0;
462
463 if (!ext) {
464 log_err("extended partition is needed for more than 4 partitions\n");
465 return -EINVAL;
20bd5ac6
MS
466 }
467
468 /* calculate extended volumes start and size if needed */
469 ext_vol_start = ext->start;
470 for (i = 4; i < count; i++) {
471 if (!p[i].start)
472 p[i].start = ext_vol_start + DOS_PART_DEFAULT_GAP;
473 if (!p[i].size) {
474 lbaint_t end = ext->start + ext->size;
475 lbaint_t allocated = 0;
476
477 for (j = i + 1; j < count; j++) {
478 if (p[j].start) {
479 end = p[j].start - DOS_PART_DEFAULT_GAP;
480 break;
481 }
482 allocated += p[j].size + DOS_PART_DEFAULT_GAP;
483 }
484 p[i].size = end - allocated - p[i].start;
485 }
486 ext_vol_start = p[i].start + p[i].size;
487 }
488
489 return 0;
490}
491#endif
492
92f1c89d 493int write_mbr_sector(struct blk_desc *dev_desc, void *buf)
b6dd69a4
PK
494{
495 if (is_valid_dos_buf(buf))
496 return -1;
497
498 /* write MBR */
499 if (blk_dwrite(dev_desc, 0, 1, buf) != 1) {
500 printf("%s: failed writing '%s' (1 blks at 0x0)\n",
501 __func__, "MBR");
502 return 1;
503 }
504
f14c5ee5
GB
505 /* Update the partition table entries*/
506 part_init(dev_desc);
507
b6dd69a4
PK
508 return 0;
509}
510
96e5b03c
SG
511U_BOOT_PART_TYPE(dos) = {
512 .name = "DOS",
513 .part_type = PART_TYPE_DOS,
87b8530f 514 .max_entries = DOS_ENTRY_NUMBERS,
3e8bd469 515 .get_info = part_get_info_ptr(part_get_info_dos),
084bf4c2
SG
516 .print = part_print_ptr(part_print_dos),
517 .test = part_test_dos,
96e5b03c 518};