]> Git Repo - u-boot.git/blame - disk/part_dos.c
Merge branch 'master' of https://source.denx.de/u-boot/custodians/u-boot-sh
[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, [email protected]
5 * Wolfgang Denk, DENX Software Engineering, [email protected].
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
e6f6f9e6 16#include <blk.h>
fe8c2806
WD
17#include <command.h>
18#include <ide.h>
cf92e05c 19#include <memalign.h>
03de305e 20#include <vsprintf.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
cfdcd41e 101static int part_test_dos(struct blk_desc *desc)
fe8c2806 102{
1d6132e2 103#ifndef CONFIG_XPL_BUILD
7aed3d38 104 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr,
cfdcd41e 105 DIV_ROUND_UP(desc->blksz, sizeof(legacy_mbr)));
fe8c2806 106
cfdcd41e 107 if (blk_dread(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
cfdcd41e
SG
113 if (desc->sig_type == SIG_TYPE_NONE && mbr->unique_mbr_signature) {
114 desc->sig_type = SIG_TYPE_MBR;
115 desc->mbr_sig = mbr->unique_mbr_signature;
ff98cb90 116 }
3ea05205 117#else
cfdcd41e 118 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz);
3ea05205 119
cfdcd41e 120 if (blk_dread(desc, 0, 1, (ulong *)buffer) != 1)
3ea05205
FE
121 return -1;
122
123 if (test_block_type(buffer) != DOS_MBR)
124 return -1;
125#endif
ff98cb90 126
d1efb644 127 return 0;
fe8c2806
WD
128}
129
130/* Print a partition that is relative to its Extended partition table
131 */
cfdcd41e 132static void print_partition_extended(struct blk_desc *desc,
d29892ba
SM
133 lbaint_t ext_part_sector,
134 lbaint_t relative,
e2e9b378 135 int part_num, unsigned int disksig)
fe8c2806 136{
cfdcd41e 137 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz);
fe8c2806
WD
138 dos_partition_t *pt;
139 int i;
140
232e2f4f
PE
141 /* set a maximum recursion level */
142 if (part_num > MAX_EXT_PARTS)
143 {
144 printf("** Nested DOS partitions detected, stopping **\n");
145 return;
146 }
147
cfdcd41e 148 if (blk_dread(desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
d29892ba 149 printf ("** Can't read partition table on %d:" LBAFU " **\n",
cfdcd41e 150 desc->devnum, ext_part_sector);
fe8c2806
WD
151 return;
152 }
7205e407 153 i=test_block_type(buffer);
d1efb644 154 if (i != DOS_MBR) {
fe8c2806
WD
155 printf ("bad MBR sector signature 0x%02x%02x\n",
156 buffer[DOS_PART_MAGIC_OFFSET],
157 buffer[DOS_PART_MAGIC_OFFSET + 1]);
158 return;
159 }
d1efb644 160
e2e9b378 161 if (!ext_part_sector)
97163dd1 162 disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
e2e9b378 163
fe8c2806
WD
164 /* Print all primary/logical partitions */
165 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
166 for (i = 0; i < 4; i++, pt++) {
167 /*
8bde7f77 168 * fdisk does not show the extended partitions that
fe8c2806
WD
169 * are not in the MBR
170 */
171
172 if ((pt->sys_ind != 0) &&
173 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
e2e9b378 174 print_one_part(pt, ext_part_sector, part_num, disksig);
fe8c2806
WD
175 }
176
177 /* Reverse engr the fdisk part# assignment rule! */
178 if ((ext_part_sector == 0) ||
179 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
180 part_num++;
181 }
182 }
183
184 /* Follows the extended partitions */
185 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
186 for (i = 0; i < 4; i++, pt++) {
187 if (is_extended (pt->sys_ind)) {
d29892ba 188 lbaint_t lba_start
97163dd1 189 = get_unaligned_le32 (pt->start4) + relative;
fe8c2806 190
cfdcd41e
SG
191 print_partition_extended(desc, lba_start,
192 !ext_part_sector ? lba_start :
193 relative, part_num, disksig);
fe8c2806
WD
194 }
195 }
196
197 return;
198}
199
fe8c2806
WD
200/* Print a partition that is relative to its Extended partition table
201 */
cfdcd41e 202static int part_get_info_extended(struct blk_desc *desc,
3e8bd469
SG
203 lbaint_t ext_part_sector, lbaint_t relative,
204 int part_num, int which_part,
0528979f 205 struct disk_partition *info, uint disksig)
fe8c2806 206{
cfdcd41e 207 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz);
e2b5cc60 208 struct disk_partition wdinfo = { 0 };
fe8c2806 209 dos_partition_t *pt;
e2b5cc60 210 int i, ret;
4a36be9b 211 int dos_type;
fe8c2806 212
232e2f4f
PE
213 /* set a maximum recursion level */
214 if (part_num > MAX_EXT_PARTS)
215 {
216 printf("** Nested DOS partitions detected, stopping **\n");
217 return -1;
218 }
219
cfdcd41e 220 if (blk_dread(desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
d29892ba 221 printf ("** Can't read partition table on %d:" LBAFU " **\n",
cfdcd41e 222 desc->devnum, ext_part_sector);
fe8c2806
WD
223 return -1;
224 }
225 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
226 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
227 printf ("bad MBR sector signature 0x%02x%02x\n",
228 buffer[DOS_PART_MAGIC_OFFSET],
229 buffer[DOS_PART_MAGIC_OFFSET + 1]);
230 return -1;
231 }
232
c5f1d005 233 if (CONFIG_IS_ENABLED(PARTITION_UUIDS) && !ext_part_sector)
97163dd1 234 disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
d27b5f93 235
cfdcd41e 236 ret = part_get_info_whole_disk(desc, &wdinfo);
e2b5cc60
MV
237 if (ret)
238 return ret;
239
fe8c2806
WD
240 /* Print all primary/logical partitions */
241 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
242 for (i = 0; i < 4; i++, pt++) {
243 /*
8bde7f77
WD
244 * fdisk does not show the extended partitions that
245 * are not in the MBR
fe8c2806 246 */
78f4ca79
DM
247 if (((pt->boot_ind & ~0x80) == 0) &&
248 (pt->sys_ind != 0) &&
7f70e853 249 (part_num == which_part) &&
8f7102cf 250 (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
e2b5cc60
MV
251 if (wdinfo.blksz > DOS_PART_DEFAULT_SECTOR)
252 info->blksz = wdinfo.blksz;
253 else
254 info->blksz = DOS_PART_DEFAULT_SECTOR;
e04350d2 255 info->start = (lbaint_t)(ext_part_sector +
97163dd1
MS
256 get_unaligned_le32(pt->start4));
257 info->size = (lbaint_t)get_unaligned_le32(pt->size4);
cfdcd41e 258 part_set_generic_name(desc, part_num,
da2ee24d 259 (char *)info->name);
fe8c2806 260 /* sprintf(info->type, "%d, pt->sys_ind); */
192bc694 261 strcpy((char *)info->type, "U-Boot");
25801acc 262 info->bootable = get_bootable(pt);
c5f1d005
SG
263 if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) {
264 char str[12];
265
266 sprintf(str, "%08x-%02x", disksig, part_num);
267 disk_partition_set_uuid(info, str);
268 }
f0fb4fa7 269 info->sys_ind = pt->sys_ind;
fe8c2806
WD
270 return 0;
271 }
272
273 /* Reverse engr the fdisk part# assignment rule! */
274 if ((ext_part_sector == 0) ||
275 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
276 part_num++;
277 }
278 }
279
280 /* Follows the extended partitions */
281 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
282 for (i = 0; i < 4; i++, pt++) {
283 if (is_extended (pt->sys_ind)) {
d29892ba 284 lbaint_t lba_start
97163dd1 285 = get_unaligned_le32 (pt->start4) + relative;
fe8c2806 286
cfdcd41e 287 return part_get_info_extended(desc, lba_start,
fe8c2806 288 ext_part_sector == 0 ? lba_start : relative,
d27b5f93 289 part_num, which_part, info, disksig);
fe8c2806
WD
290 }
291 }
4a36be9b
DD
292
293 /* Check for DOS PBR if no partition is found */
294 dos_type = test_block_type(buffer);
295
296 if (dos_type == DOS_PBR) {
297 info->start = 0;
cfdcd41e 298 info->size = desc->lba;
e2b5cc60
MV
299 if (wdinfo.blksz > DOS_PART_DEFAULT_SECTOR)
300 info->blksz = wdinfo.blksz;
301 else
302 info->blksz = DOS_PART_DEFAULT_SECTOR;
4a36be9b 303 info->bootable = 0;
192bc694 304 strcpy((char *)info->type, "U-Boot");
c5f1d005 305 disk_partition_clr_uuid(info);
4a36be9b
DD
306 return 0;
307 }
308
fe8c2806
WD
309 return -1;
310}
311
cfdcd41e 312static void __maybe_unused part_print_dos(struct blk_desc *desc)
fe8c2806 313{
e2e9b378 314 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
cfdcd41e 315 print_partition_extended(desc, 0, 0, 1, 0);
fe8c2806
WD
316}
317
cfdcd41e
SG
318static int __maybe_unused part_get_info_dos(struct blk_desc *desc, int part,
319 struct disk_partition *info)
fe8c2806 320{
cfdcd41e 321 return part_get_info_extended(desc, 0, 0, 1, part, info, 0);
fe8c2806
WD
322}
323
b6dd69a4
PK
324int is_valid_dos_buf(void *buf)
325{
326 return test_block_type(buf) == DOS_MBR ? 0 : -1;
327}
328
d01bf20e 329#if IS_ENABLED(CONFIG_CMD_MBR)
20bd5ac6
MS
330static void lba_to_chs(lbaint_t lba, unsigned char *rc, unsigned char *rh,
331 unsigned char *rs)
332{
333 unsigned int c, h, s;
334 /* use fixed CHS geometry */
335 unsigned int sectpertrack = 63;
336 unsigned int heads = 255;
337
338 c = (lba + 1) / sectpertrack / heads;
339 h = (lba + 1) / sectpertrack - c * heads;
340 s = (lba + 1) - (c * heads + h) * sectpertrack;
341
342 if (c > 1023) {
343 c = 1023;
344 h = 254;
345 s = 63;
346 }
347
348 *rc = c & 0xff;
349 *rh = h;
350 *rs = s + ((c & 0x300) >> 2);
351}
352
353static void mbr_fill_pt_entry(dos_partition_t *pt, lbaint_t start,
354 lbaint_t relative, lbaint_t size, uchar sys_ind, bool bootable)
355{
356 pt->boot_ind = bootable ? 0x80 : 0x00;
357 pt->sys_ind = sys_ind;
358 lba_to_chs(start, &pt->cyl, &pt->head, &pt->sector);
359 lba_to_chs(start + size - 1, &pt->end_cyl, &pt->end_head, &pt->end_sector);
360 put_unaligned_le32(relative, &pt->start4);
361 put_unaligned_le32(size, &pt->size4);
362}
363
364int write_mbr_partitions(struct blk_desc *dev,
365 struct disk_partition *p, int count, unsigned int disksig)
366{
367 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev->blksz);
368 lbaint_t ext_part_start = 0, ext_part_size = 0, ext_part_sect = 0;
369 dos_partition_t *pt;
370 int i;
371
372 memset(buffer, 0, dev->blksz);
373 buffer[DOS_PART_MAGIC_OFFSET] = 0x55;
374 buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa;
375 put_unaligned_le32(disksig, &buffer[DOS_PART_DISKSIG_OFFSET]);
376 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
377
378 /* create all primary partitions */
379 for (i = 0; i < 4 && i < count; i++, pt++) {
380 mbr_fill_pt_entry(pt, p[i].start, p[i].start, p[i].size,
381 p[i].sys_ind, p[i].bootable);
382 if (is_extended(p[i].sys_ind)) {
383 ext_part_start = p[i].start;
384 ext_part_size = p[i].size;
385 ext_part_sect = p[i].start;
386 }
387 }
388
389 if (i < count && !ext_part_start) {
390 printf("%s: extended partition is needed for more than 4 partitions\n",
391 __func__);
392 return -1;
393 }
394
395 /* write MBR */
396 if (blk_dwrite(dev, 0, 1, buffer) != 1) {
397 printf("%s: failed writing 'MBR' (1 blks at 0x0)\n",
398 __func__);
399 return -1;
400 }
401
402 /* create extended volumes */
403 for (; i < count; i++) {
404 lbaint_t next_ebr = 0;
405
406 memset(buffer, 0, dev->blksz);
407 buffer[DOS_PART_MAGIC_OFFSET] = 0x55;
408 buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa;
409 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
410
411 mbr_fill_pt_entry(pt, p[i].start, p[i].start - ext_part_sect,
412 p[i].size, p[i].sys_ind, p[i].bootable);
413
414 if (i + 1 < count) {
415 pt++;
416 next_ebr = p[i].start + p[i].size;
417 mbr_fill_pt_entry(pt, next_ebr,
418 next_ebr - ext_part_start,
419 p[i+1].start + p[i+1].size - next_ebr,
420 DOS_PART_TYPE_EXTENDED, 0);
421 }
422
423 /* write EBR */
424 if (blk_dwrite(dev, ext_part_sect, 1, buffer) != 1) {
425 printf("%s: failed writing 'EBR' (1 blks at 0x%lx)\n",
426 __func__, ext_part_sect);
427 return -1;
428 }
429 ext_part_sect = next_ebr;
430 }
431
f14c5ee5 432 /* Update the partition table entries*/
c7d1b189 433 part_init(dev);
f14c5ee5 434
20bd5ac6
MS
435 return 0;
436}
437
438int layout_mbr_partitions(struct disk_partition *p, int count,
439 lbaint_t total_sectors)
440{
441 struct disk_partition *ext = NULL;
442 int i, j;
443 lbaint_t ext_vol_start;
444
445 /* calculate primary partitions start and size if needed */
446 if (!p[0].start)
447 p[0].start = DOS_PART_DEFAULT_GAP;
448 for (i = 0; i < 4 && i < count; i++) {
449 if (!p[i].start)
450 p[i].start = p[i - 1].start + p[i - 1].size;
451 if (!p[i].size) {
452 lbaint_t end = total_sectors;
453 lbaint_t allocated = 0;
454
455 for (j = i + 1; j < 4 && j < count; j++) {
456 if (p[j].start) {
457 end = p[j].start;
458 break;
459 }
460 allocated += p[j].size;
461 }
462 p[i].size = end - allocated - p[i].start;
463 }
464 if (p[i].sys_ind == 0x05)
465 ext = &p[i];
466 }
467
04291ee0 468 if (count <= 4)
b7c2cc49
SG
469 return 0;
470
471 if (!ext) {
472 log_err("extended partition is needed for more than 4 partitions\n");
473 return -EINVAL;
20bd5ac6
MS
474 }
475
476 /* calculate extended volumes start and size if needed */
477 ext_vol_start = ext->start;
478 for (i = 4; i < count; i++) {
479 if (!p[i].start)
480 p[i].start = ext_vol_start + DOS_PART_DEFAULT_GAP;
481 if (!p[i].size) {
482 lbaint_t end = ext->start + ext->size;
483 lbaint_t allocated = 0;
484
485 for (j = i + 1; j < count; j++) {
486 if (p[j].start) {
487 end = p[j].start - DOS_PART_DEFAULT_GAP;
488 break;
489 }
490 allocated += p[j].size + DOS_PART_DEFAULT_GAP;
491 }
492 p[i].size = end - allocated - p[i].start;
493 }
494 ext_vol_start = p[i].start + p[i].size;
495 }
496
497 return 0;
498}
499#endif
500
cfdcd41e 501int write_mbr_sector(struct blk_desc *desc, void *buf)
b6dd69a4
PK
502{
503 if (is_valid_dos_buf(buf))
504 return -1;
505
506 /* write MBR */
cfdcd41e 507 if (blk_dwrite(desc, 0, 1, buf) != 1) {
b6dd69a4
PK
508 printf("%s: failed writing '%s' (1 blks at 0x0)\n",
509 __func__, "MBR");
510 return 1;
511 }
512
f14c5ee5 513 /* Update the partition table entries*/
cfdcd41e 514 part_init(desc);
f14c5ee5 515
b6dd69a4
PK
516 return 0;
517}
518
96e5b03c
SG
519U_BOOT_PART_TYPE(dos) = {
520 .name = "DOS",
521 .part_type = PART_TYPE_DOS,
87b8530f 522 .max_entries = DOS_ENTRY_NUMBERS,
3e8bd469 523 .get_info = part_get_info_ptr(part_get_info_dos),
084bf4c2
SG
524 .print = part_print_ptr(part_print_dos),
525 .test = part_test_dos,
96e5b03c 526};
This page took 0.45967 seconds and 4 git commands to generate.