]> git.ipfire.org Git - thirdparty/u-boot.git/blob - disk/part_dos.c
part: nac: Use desc instead of dev_desc
[thirdparty/u-boot.git] / disk / part_dos.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2001
4 * Raymond Lo, lo@routefree.com
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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>
17 #include <blk.h>
18 #include <command.h>
19 #include <ide.h>
20 #include <memalign.h>
21 #include <asm/unaligned.h>
22 #include <linux/compiler.h>
23 #include "part_dos.h"
24 #include <part.h>
25
26 #define DOS_PART_DEFAULT_SECTOR 512
27
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
32 static inline int is_extended(int part_type)
33 {
34 return (part_type == DOS_PART_TYPE_EXTENDED ||
35 part_type == DOS_PART_TYPE_EXTENDED_LBA ||
36 part_type == DOS_PART_TYPE_EXTENDED_LINUX);
37 }
38
39 static int get_bootable(dos_partition_t *p)
40 {
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;
48 }
49
50 static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
51 int part_num, unsigned int disksig)
52 {
53 lbaint_t lba_start = ext_part_sector + get_unaligned_le32(p->start4);
54 lbaint_t lba_size = get_unaligned_le32(p->size4);
55
56 printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
57 "u\t%08x-%02x\t%02x%s%s\n",
58 part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
59 (is_extended(p->sys_ind) ? " Extd" : ""),
60 (get_bootable(p) ? " Boot" : ""));
61 }
62
63 static int test_block_type(unsigned char *buffer)
64 {
65 int slot;
66 struct dos_partition *p;
67 int part_count = 0;
68
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 */
73 p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
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;
81 }
82
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 }
100
101 static int part_test_dos(struct blk_desc *desc)
102 {
103 #ifndef CONFIG_SPL_BUILD
104 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr,
105 DIV_ROUND_UP(desc->blksz, sizeof(legacy_mbr)));
106
107 if (blk_dread(desc, 0, 1, (ulong *)mbr) != 1)
108 return -1;
109
110 if (test_block_type((unsigned char *)mbr) != DOS_MBR)
111 return -1;
112
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;
116 }
117 #else
118 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz);
119
120 if (blk_dread(desc, 0, 1, (ulong *)buffer) != 1)
121 return -1;
122
123 if (test_block_type(buffer) != DOS_MBR)
124 return -1;
125 #endif
126
127 return 0;
128 }
129
130 /* Print a partition that is relative to its Extended partition table
131 */
132 static void print_partition_extended(struct blk_desc *desc,
133 lbaint_t ext_part_sector,
134 lbaint_t relative,
135 int part_num, unsigned int disksig)
136 {
137 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz);
138 dos_partition_t *pt;
139 int i;
140
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
148 if (blk_dread(desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
149 printf ("** Can't read partition table on %d:" LBAFU " **\n",
150 desc->devnum, ext_part_sector);
151 return;
152 }
153 i=test_block_type(buffer);
154 if (i != DOS_MBR) {
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 }
160
161 if (!ext_part_sector)
162 disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
163
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 /*
168 * fdisk does not show the extended partitions that
169 * are not in the MBR
170 */
171
172 if ((pt->sys_ind != 0) &&
173 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
174 print_one_part(pt, ext_part_sector, part_num, disksig);
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)) {
188 lbaint_t lba_start
189 = get_unaligned_le32 (pt->start4) + relative;
190
191 print_partition_extended(desc, lba_start,
192 !ext_part_sector ? lba_start :
193 relative, part_num, disksig);
194 }
195 }
196
197 return;
198 }
199
200
201 /* Print a partition that is relative to its Extended partition table
202 */
203 static int part_get_info_extended(struct blk_desc *desc,
204 lbaint_t ext_part_sector, lbaint_t relative,
205 int part_num, int which_part,
206 struct disk_partition *info, uint disksig)
207 {
208 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz);
209 struct disk_partition wdinfo = { 0 };
210 dos_partition_t *pt;
211 int i, ret;
212 int dos_type;
213
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
221 if (blk_dread(desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
222 printf ("** Can't read partition table on %d:" LBAFU " **\n",
223 desc->devnum, ext_part_sector);
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
234 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
235 if (!ext_part_sector)
236 disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
237 #endif
238
239 ret = part_get_info_whole_disk(desc, &wdinfo);
240 if (ret)
241 return ret;
242
243 /* Print all primary/logical partitions */
244 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
245 for (i = 0; i < 4; i++, pt++) {
246 /*
247 * fdisk does not show the extended partitions that
248 * are not in the MBR
249 */
250 if (((pt->boot_ind & ~0x80) == 0) &&
251 (pt->sys_ind != 0) &&
252 (part_num == which_part) &&
253 (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
254 if (wdinfo.blksz > DOS_PART_DEFAULT_SECTOR)
255 info->blksz = wdinfo.blksz;
256 else
257 info->blksz = DOS_PART_DEFAULT_SECTOR;
258 info->start = (lbaint_t)(ext_part_sector +
259 get_unaligned_le32(pt->start4));
260 info->size = (lbaint_t)get_unaligned_le32(pt->size4);
261 part_set_generic_name(desc, part_num,
262 (char *)info->name);
263 /* sprintf(info->type, "%d, pt->sys_ind); */
264 strcpy((char *)info->type, "U-Boot");
265 info->bootable = get_bootable(pt);
266 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
267 sprintf(info->uuid, "%08x-%02x", disksig, part_num);
268 #endif
269 info->sys_ind = pt->sys_ind;
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)) {
284 lbaint_t lba_start
285 = get_unaligned_le32 (pt->start4) + relative;
286
287 return part_get_info_extended(desc, lba_start,
288 ext_part_sector == 0 ? lba_start : relative,
289 part_num, which_part, info, disksig);
290 }
291 }
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;
298 info->size = desc->lba;
299 if (wdinfo.blksz > DOS_PART_DEFAULT_SECTOR)
300 info->blksz = wdinfo.blksz;
301 else
302 info->blksz = DOS_PART_DEFAULT_SECTOR;
303 info->bootable = 0;
304 strcpy((char *)info->type, "U-Boot");
305 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
306 info->uuid[0] = 0;
307 #endif
308 return 0;
309 }
310
311 return -1;
312 }
313
314 static void __maybe_unused part_print_dos(struct blk_desc *desc)
315 {
316 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
317 print_partition_extended(desc, 0, 0, 1, 0);
318 }
319
320 static int __maybe_unused part_get_info_dos(struct blk_desc *desc, int part,
321 struct disk_partition *info)
322 {
323 return part_get_info_extended(desc, 0, 0, 1, part, info, 0);
324 }
325
326 int is_valid_dos_buf(void *buf)
327 {
328 return test_block_type(buf) == DOS_MBR ? 0 : -1;
329 }
330
331 #if IS_ENABLED(CONFIG_CMD_MBR)
332 static void lba_to_chs(lbaint_t lba, unsigned char *rc, unsigned char *rh,
333 unsigned char *rs)
334 {
335 unsigned int c, h, s;
336 /* use fixed CHS geometry */
337 unsigned int sectpertrack = 63;
338 unsigned int heads = 255;
339
340 c = (lba + 1) / sectpertrack / heads;
341 h = (lba + 1) / sectpertrack - c * heads;
342 s = (lba + 1) - (c * heads + h) * sectpertrack;
343
344 if (c > 1023) {
345 c = 1023;
346 h = 254;
347 s = 63;
348 }
349
350 *rc = c & 0xff;
351 *rh = h;
352 *rs = s + ((c & 0x300) >> 2);
353 }
354
355 static void mbr_fill_pt_entry(dos_partition_t *pt, lbaint_t start,
356 lbaint_t relative, lbaint_t size, uchar sys_ind, bool bootable)
357 {
358 pt->boot_ind = bootable ? 0x80 : 0x00;
359 pt->sys_ind = sys_ind;
360 lba_to_chs(start, &pt->cyl, &pt->head, &pt->sector);
361 lba_to_chs(start + size - 1, &pt->end_cyl, &pt->end_head, &pt->end_sector);
362 put_unaligned_le32(relative, &pt->start4);
363 put_unaligned_le32(size, &pt->size4);
364 }
365
366 int write_mbr_partitions(struct blk_desc *dev,
367 struct disk_partition *p, int count, unsigned int disksig)
368 {
369 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev->blksz);
370 lbaint_t ext_part_start = 0, ext_part_size = 0, ext_part_sect = 0;
371 dos_partition_t *pt;
372 int i;
373
374 memset(buffer, 0, dev->blksz);
375 buffer[DOS_PART_MAGIC_OFFSET] = 0x55;
376 buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa;
377 put_unaligned_le32(disksig, &buffer[DOS_PART_DISKSIG_OFFSET]);
378 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
379
380 /* create all primary partitions */
381 for (i = 0; i < 4 && i < count; i++, pt++) {
382 mbr_fill_pt_entry(pt, p[i].start, p[i].start, p[i].size,
383 p[i].sys_ind, p[i].bootable);
384 if (is_extended(p[i].sys_ind)) {
385 ext_part_start = p[i].start;
386 ext_part_size = p[i].size;
387 ext_part_sect = p[i].start;
388 }
389 }
390
391 if (i < count && !ext_part_start) {
392 printf("%s: extended partition is needed for more than 4 partitions\n",
393 __func__);
394 return -1;
395 }
396
397 /* write MBR */
398 if (blk_dwrite(dev, 0, 1, buffer) != 1) {
399 printf("%s: failed writing 'MBR' (1 blks at 0x0)\n",
400 __func__);
401 return -1;
402 }
403
404 /* create extended volumes */
405 for (; i < count; i++) {
406 lbaint_t next_ebr = 0;
407
408 memset(buffer, 0, dev->blksz);
409 buffer[DOS_PART_MAGIC_OFFSET] = 0x55;
410 buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa;
411 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
412
413 mbr_fill_pt_entry(pt, p[i].start, p[i].start - ext_part_sect,
414 p[i].size, p[i].sys_ind, p[i].bootable);
415
416 if (i + 1 < count) {
417 pt++;
418 next_ebr = p[i].start + p[i].size;
419 mbr_fill_pt_entry(pt, next_ebr,
420 next_ebr - ext_part_start,
421 p[i+1].start + p[i+1].size - next_ebr,
422 DOS_PART_TYPE_EXTENDED, 0);
423 }
424
425 /* write EBR */
426 if (blk_dwrite(dev, ext_part_sect, 1, buffer) != 1) {
427 printf("%s: failed writing 'EBR' (1 blks at 0x%lx)\n",
428 __func__, ext_part_sect);
429 return -1;
430 }
431 ext_part_sect = next_ebr;
432 }
433
434 /* Update the partition table entries*/
435 part_init(dev);
436
437 return 0;
438 }
439
440 int layout_mbr_partitions(struct disk_partition *p, int count,
441 lbaint_t total_sectors)
442 {
443 struct disk_partition *ext = NULL;
444 int i, j;
445 lbaint_t ext_vol_start;
446
447 /* calculate primary partitions start and size if needed */
448 if (!p[0].start)
449 p[0].start = DOS_PART_DEFAULT_GAP;
450 for (i = 0; i < 4 && i < count; i++) {
451 if (!p[i].start)
452 p[i].start = p[i - 1].start + p[i - 1].size;
453 if (!p[i].size) {
454 lbaint_t end = total_sectors;
455 lbaint_t allocated = 0;
456
457 for (j = i + 1; j < 4 && j < count; j++) {
458 if (p[j].start) {
459 end = p[j].start;
460 break;
461 }
462 allocated += p[j].size;
463 }
464 p[i].size = end - allocated - p[i].start;
465 }
466 if (p[i].sys_ind == 0x05)
467 ext = &p[i];
468 }
469
470 if (count < 4)
471 return 0;
472
473 if (!ext) {
474 log_err("extended partition is needed for more than 4 partitions\n");
475 return -EINVAL;
476 }
477
478 /* calculate extended volumes start and size if needed */
479 ext_vol_start = ext->start;
480 for (i = 4; i < count; i++) {
481 if (!p[i].start)
482 p[i].start = ext_vol_start + DOS_PART_DEFAULT_GAP;
483 if (!p[i].size) {
484 lbaint_t end = ext->start + ext->size;
485 lbaint_t allocated = 0;
486
487 for (j = i + 1; j < count; j++) {
488 if (p[j].start) {
489 end = p[j].start - DOS_PART_DEFAULT_GAP;
490 break;
491 }
492 allocated += p[j].size + DOS_PART_DEFAULT_GAP;
493 }
494 p[i].size = end - allocated - p[i].start;
495 }
496 ext_vol_start = p[i].start + p[i].size;
497 }
498
499 return 0;
500 }
501 #endif
502
503 int write_mbr_sector(struct blk_desc *desc, void *buf)
504 {
505 if (is_valid_dos_buf(buf))
506 return -1;
507
508 /* write MBR */
509 if (blk_dwrite(desc, 0, 1, buf) != 1) {
510 printf("%s: failed writing '%s' (1 blks at 0x0)\n",
511 __func__, "MBR");
512 return 1;
513 }
514
515 /* Update the partition table entries*/
516 part_init(desc);
517
518 return 0;
519 }
520
521 U_BOOT_PART_TYPE(dos) = {
522 .name = "DOS",
523 .part_type = PART_TYPE_DOS,
524 .max_entries = DOS_ENTRY_NUMBERS,
525 .get_info = part_get_info_ptr(part_get_info_dos),
526 .print = part_print_ptr(part_print_dos),
527 .test = part_test_dos,
528 };