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