blob: 04f53106f7ce8762aa89468397c969bb0fd4ae65 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkfe8c2802002-11-03 00:38:21 +00002/*
3 * (C) Copyright 2001
4 * Raymond Lo, lo@routefree.com
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkfe8c2802002-11-03 00:38:21 +00006 */
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>
Simon Glass655306c2020-05-10 11:39:58 -060017#include <blk.h>
wdenkfe8c2802002-11-03 00:38:21 +000018#include <command.h>
19#include <ide.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060020#include <memalign.h>
wdenkfe8c2802002-11-03 00:38:21 +000021#include "part_dos.h"
Simon Glass655306c2020-05-10 11:39:58 -060022#include <part.h>
wdenkfe8c2802002-11-03 00:38:21 +000023
Adam Fordb10ba902018-02-06 12:43:56 -060024#ifdef CONFIG_HAVE_BLOCK_DEVICE
wdenkfe8c2802002-11-03 00:38:21 +000025
Darwin Dingel2c1e1b62014-06-06 15:48:26 +120026#define DOS_PART_DEFAULT_SECTOR 512
27
Paul Emge61494122019-07-08 16:37:03 -070028/* 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
wdenkfe8c2802002-11-03 00:38:21 +000032/* Convert char[4] in little endian format to the host format integer
33 */
Stefan Monnier4c6e9602015-08-25 15:24:13 -040034static inline unsigned int le32_to_int(unsigned char *le32)
wdenkfe8c2802002-11-03 00:38:21 +000035{
36 return ((le32[3] << 24) +
37 (le32[2] << 16) +
38 (le32[1] << 8) +
39 le32[0]
40 );
41}
42
43static inline int is_extended(int part_type)
44{
45 return (part_type == 0x5 ||
46 part_type == 0xf ||
47 part_type == 0x85);
48}
49
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010050static int get_bootable(dos_partition_t *p)
Rob Herringeda51112012-08-23 11:31:43 +000051{
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010052 int ret = 0;
53
54 if (p->sys_ind == 0xef)
55 ret |= PART_EFI_SYSTEM_PARTITION;
56 if (p->boot_ind == 0x80)
57 ret |= PART_BOOTABLE;
58 return ret;
Rob Herringeda51112012-08-23 11:31:43 +000059}
60
Stefan Monnier4c6e9602015-08-25 15:24:13 -040061static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
Stephen Warren37c88ea2012-10-08 08:14:40 +000062 int part_num, unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +000063{
Stefan Monnier4c6e9602015-08-25 15:24:13 -040064 lbaint_t lba_start = ext_part_sector + le32_to_int (p->start4);
65 lbaint_t lba_size = le32_to_int (p->size4);
wdenkfe8c2802002-11-03 00:38:21 +000066
Stefan Monnier4c6e9602015-08-25 15:24:13 -040067 printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
68 "u\t%08x-%02x\t%02x%s%s\n",
Stephen Warren37c88ea2012-10-08 08:14:40 +000069 part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
Rob Herringeda51112012-08-23 11:31:43 +000070 (is_extended(p->sys_ind) ? " Extd" : ""),
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010071 (get_bootable(p) ? " Boot" : ""));
wdenkfe8c2802002-11-03 00:38:21 +000072}
73
wdenk2c9b05d2003-09-10 22:30:53 +000074static int test_block_type(unsigned char *buffer)
75{
Egbert Eich300494b2013-04-09 05:46:14 +000076 int slot;
77 struct dos_partition *p;
Heinrich Schuchardt86c8dd42019-10-15 20:43:42 +020078 int part_count = 0;
Egbert Eich300494b2013-04-09 05:46:14 +000079
wdenk2c9b05d2003-09-10 22:30:53 +000080 if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
81 (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
82 return (-1);
83 } /* no DOS Signature at all */
Egbert Eich300494b2013-04-09 05:46:14 +000084 p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
Heinrich Schuchardt86c8dd42019-10-15 20:43:42 +020085
86 /* Check that the boot indicators are valid and count the partitions. */
87 for (slot = 0; slot < 4; ++slot, ++p) {
88 if (p->boot_ind != 0 && p->boot_ind != 0x80)
89 break;
90 if (p->sys_ind)
91 ++part_count;
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020092 }
wdenk2c9b05d2003-09-10 22:30:53 +000093
Heinrich Schuchardt86c8dd42019-10-15 20:43:42 +020094 /*
95 * If the partition table is invalid or empty,
96 * check if this is a DOS PBR
97 */
98 if (slot != 4 || !part_count) {
99 if (!strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
100 "FAT", 3) ||
101 !strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
102 "FAT32", 5))
103 return DOS_PBR; /* This is a DOS PBR and not an MBR */
104 }
105 if (slot == 4)
106 return DOS_MBR; /* This is an DOS MBR */
107
108 /* This is neither a DOS MBR nor a DOS PBR */
109 return -1;
110}
wdenkfe8c2802002-11-03 00:38:21 +0000111
Simon Glass0f1c9522016-02-29 15:26:04 -0700112static int part_test_dos(struct blk_desc *dev_desc)
wdenkfe8c2802002-11-03 00:38:21 +0000113{
Fabio Estevamf0c82a82017-10-04 13:29:57 -0300114#ifndef CONFIG_SPL_BUILD
Faiz Abbas1bca8de2019-09-04 20:10:12 +0530115 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr,
116 DIV_ROUND_UP(dev_desc->blksz, sizeof(legacy_mbr)));
wdenkfe8c2802002-11-03 00:38:21 +0000117
Peter Jonesc27e1c12017-09-13 18:05:25 -0400118 if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1)
Stephen Warren71b4e3b2012-10-05 13:17:40 +0000119 return -1;
120
Peter Jonesc27e1c12017-09-13 18:05:25 -0400121 if (test_block_type((unsigned char *)mbr) != DOS_MBR)
Stephen Warren71b4e3b2012-10-05 13:17:40 +0000122 return -1;
123
Peter Jonesc27e1c12017-09-13 18:05:25 -0400124 if (dev_desc->sig_type == SIG_TYPE_NONE &&
125 mbr->unique_mbr_signature != 0) {
126 dev_desc->sig_type = SIG_TYPE_MBR;
127 dev_desc->mbr_sig = mbr->unique_mbr_signature;
128 }
Fabio Estevamf0c82a82017-10-04 13:29:57 -0300129#else
130 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
131
132 if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
133 return -1;
134
135 if (test_block_type(buffer) != DOS_MBR)
136 return -1;
137#endif
Peter Jonesc27e1c12017-09-13 18:05:25 -0400138
Stephen Warren71b4e3b2012-10-05 13:17:40 +0000139 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000140}
141
142/* Print a partition that is relative to its Extended partition table
143 */
Simon Glasse3394752016-02-29 15:25:34 -0700144static void print_partition_extended(struct blk_desc *dev_desc,
Stefan Monnier4c6e9602015-08-25 15:24:13 -0400145 lbaint_t ext_part_sector,
146 lbaint_t relative,
Stephen Warren37c88ea2012-10-08 08:14:40 +0000147 int part_num, unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +0000148{
Eric Nelson193b2602012-03-03 12:02:20 +0000149 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +0000150 dos_partition_t *pt;
151 int i;
152
Paul Emge61494122019-07-08 16:37:03 -0700153 /* set a maximum recursion level */
154 if (part_num > MAX_EXT_PARTS)
155 {
156 printf("** Nested DOS partitions detected, stopping **\n");
157 return;
158 }
159
Simon Glass2ee8ada2016-02-29 15:25:52 -0700160 if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
Stefan Monnier4c6e9602015-08-25 15:24:13 -0400161 printf ("** Can't read partition table on %d:" LBAFU " **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700162 dev_desc->devnum, ext_part_sector);
wdenkfe8c2802002-11-03 00:38:21 +0000163 return;
164 }
wdenk2c9b05d2003-09-10 22:30:53 +0000165 i=test_block_type(buffer);
Stephen Warren71b4e3b2012-10-05 13:17:40 +0000166 if (i != DOS_MBR) {
wdenkfe8c2802002-11-03 00:38:21 +0000167 printf ("bad MBR sector signature 0x%02x%02x\n",
168 buffer[DOS_PART_MAGIC_OFFSET],
169 buffer[DOS_PART_MAGIC_OFFSET + 1]);
170 return;
171 }
Stephen Warren71b4e3b2012-10-05 13:17:40 +0000172
Stephen Warren37c88ea2012-10-08 08:14:40 +0000173 if (!ext_part_sector)
174 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
175
wdenkfe8c2802002-11-03 00:38:21 +0000176 /* Print all primary/logical partitions */
177 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
178 for (i = 0; i < 4; i++, pt++) {
179 /*
wdenk57b2d802003-06-27 21:31:46 +0000180 * fdisk does not show the extended partitions that
wdenkfe8c2802002-11-03 00:38:21 +0000181 * are not in the MBR
182 */
183
184 if ((pt->sys_ind != 0) &&
185 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
Stephen Warren37c88ea2012-10-08 08:14:40 +0000186 print_one_part(pt, ext_part_sector, part_num, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000187 }
188
189 /* Reverse engr the fdisk part# assignment rule! */
190 if ((ext_part_sector == 0) ||
191 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
192 part_num++;
193 }
194 }
195
196 /* Follows the extended partitions */
197 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
198 for (i = 0; i < 4; i++, pt++) {
199 if (is_extended (pt->sys_ind)) {
Stefan Monnier4c6e9602015-08-25 15:24:13 -0400200 lbaint_t lba_start
201 = le32_to_int (pt->start4) + relative;
wdenkfe8c2802002-11-03 00:38:21 +0000202
Stephen Warren61b80a52012-10-08 08:14:38 +0000203 print_partition_extended(dev_desc, lba_start,
204 ext_part_sector == 0 ? lba_start : relative,
Stephen Warren37c88ea2012-10-08 08:14:40 +0000205 part_num, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000206 }
207 }
208
209 return;
210}
211
212
213/* Print a partition that is relative to its Extended partition table
214 */
Simon Glassb89a8442016-02-29 15:25:48 -0700215static int part_get_info_extended(struct blk_desc *dev_desc,
216 lbaint_t ext_part_sector, lbaint_t relative,
217 int part_num, int which_part,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600218 struct disk_partition *info, uint disksig)
wdenkfe8c2802002-11-03 00:38:21 +0000219{
Eric Nelson193b2602012-03-03 12:02:20 +0000220 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +0000221 dos_partition_t *pt;
222 int i;
Darwin Dingel2c1e1b62014-06-06 15:48:26 +1200223 int dos_type;
wdenkfe8c2802002-11-03 00:38:21 +0000224
Paul Emge61494122019-07-08 16:37:03 -0700225 /* set a maximum recursion level */
226 if (part_num > MAX_EXT_PARTS)
227 {
228 printf("** Nested DOS partitions detected, stopping **\n");
229 return -1;
230 }
231
Simon Glass2ee8ada2016-02-29 15:25:52 -0700232 if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
Stefan Monnier4c6e9602015-08-25 15:24:13 -0400233 printf ("** Can't read partition table on %d:" LBAFU " **\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700234 dev_desc->devnum, ext_part_sector);
wdenkfe8c2802002-11-03 00:38:21 +0000235 return -1;
236 }
237 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
238 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
239 printf ("bad MBR sector signature 0x%02x%02x\n",
240 buffer[DOS_PART_MAGIC_OFFSET],
241 buffer[DOS_PART_MAGIC_OFFSET + 1]);
242 return -1;
243 }
244
Patrick Delaunay73287092017-01-27 11:00:42 +0100245#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warrenb287a872012-09-21 09:51:00 +0000246 if (!ext_part_sector)
247 disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
248#endif
249
wdenkfe8c2802002-11-03 00:38:21 +0000250 /* Print all primary/logical partitions */
251 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
252 for (i = 0; i < 4; i++, pt++) {
253 /*
wdenk57b2d802003-06-27 21:31:46 +0000254 * fdisk does not show the extended partitions that
255 * are not in the MBR
wdenkfe8c2802002-11-03 00:38:21 +0000256 */
Daniel Mack9a2671f2009-09-28 11:40:38 +0200257 if (((pt->boot_ind & ~0x80) == 0) &&
258 (pt->sys_ind != 0) &&
wdenk0893c472003-05-20 14:25:27 +0000259 (part_num == which_part) &&
Shawn Guo1c6d6792017-11-02 16:46:34 +0800260 (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
Darwin Dingel2c1e1b62014-06-06 15:48:26 +1200261 info->blksz = DOS_PART_DEFAULT_SECTOR;
Steve Raed30cbae2014-05-26 11:52:23 -0700262 info->start = (lbaint_t)(ext_part_sector +
263 le32_to_int(pt->start4));
264 info->size = (lbaint_t)le32_to_int(pt->size4);
Petr Kulhavy2e790d52016-09-09 10:27:17 +0200265 part_set_generic_name(dev_desc, part_num,
266 (char *)info->name);
wdenkfe8c2802002-11-03 00:38:21 +0000267 /* sprintf(info->type, "%d, pt->sys_ind); */
Ben Whitten34fd6c92015-12-30 13:05:58 +0000268 strcpy((char *)info->type, "U-Boot");
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100269 info->bootable = get_bootable(pt);
Patrick Delaunay73287092017-01-27 11:00:42 +0100270#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warrenb287a872012-09-21 09:51:00 +0000271 sprintf(info->uuid, "%08x-%02x", disksig, part_num);
272#endif
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800273 info->sys_ind = pt->sys_ind;
wdenkfe8c2802002-11-03 00:38:21 +0000274 return 0;
275 }
276
277 /* Reverse engr the fdisk part# assignment rule! */
278 if ((ext_part_sector == 0) ||
279 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
280 part_num++;
281 }
282 }
283
284 /* Follows the extended partitions */
285 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
286 for (i = 0; i < 4; i++, pt++) {
287 if (is_extended (pt->sys_ind)) {
Stefan Monnier4c6e9602015-08-25 15:24:13 -0400288 lbaint_t lba_start
289 = le32_to_int (pt->start4) + relative;
wdenkfe8c2802002-11-03 00:38:21 +0000290
Simon Glassb89a8442016-02-29 15:25:48 -0700291 return part_get_info_extended(dev_desc, lba_start,
wdenkfe8c2802002-11-03 00:38:21 +0000292 ext_part_sector == 0 ? lba_start : relative,
Stephen Warrenb287a872012-09-21 09:51:00 +0000293 part_num, which_part, info, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000294 }
295 }
Darwin Dingel2c1e1b62014-06-06 15:48:26 +1200296
297 /* Check for DOS PBR if no partition is found */
298 dos_type = test_block_type(buffer);
299
300 if (dos_type == DOS_PBR) {
301 info->start = 0;
302 info->size = dev_desc->lba;
303 info->blksz = DOS_PART_DEFAULT_SECTOR;
304 info->bootable = 0;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000305 strcpy((char *)info->type, "U-Boot");
Patrick Delaunay73287092017-01-27 11:00:42 +0100306#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Darwin Dingel2c1e1b62014-06-06 15:48:26 +1200307 info->uuid[0] = 0;
308#endif
309 return 0;
310 }
311
wdenkfe8c2802002-11-03 00:38:21 +0000312 return -1;
313}
314
Simon Glass0f1c9522016-02-29 15:26:04 -0700315void part_print_dos(struct blk_desc *dev_desc)
wdenkfe8c2802002-11-03 00:38:21 +0000316{
Stephen Warren37c88ea2012-10-08 08:14:40 +0000317 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
318 print_partition_extended(dev_desc, 0, 0, 1, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000319}
320
Simon Glassb89a8442016-02-29 15:25:48 -0700321int part_get_info_dos(struct blk_desc *dev_desc, int part,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600322 struct disk_partition *info)
wdenkfe8c2802002-11-03 00:38:21 +0000323{
Simon Glassb89a8442016-02-29 15:25:48 -0700324 return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000325}
326
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200327int is_valid_dos_buf(void *buf)
328{
329 return test_block_type(buf) == DOS_MBR ? 0 : -1;
330}
331
332int write_mbr_partition(struct blk_desc *dev_desc, void *buf)
333{
334 if (is_valid_dos_buf(buf))
335 return -1;
336
337 /* write MBR */
338 if (blk_dwrite(dev_desc, 0, 1, buf) != 1) {
339 printf("%s: failed writing '%s' (1 blks at 0x0)\n",
340 __func__, "MBR");
341 return 1;
342 }
343
344 return 0;
345}
346
Simon Glass83ce5632016-02-29 15:25:47 -0700347U_BOOT_PART_TYPE(dos) = {
348 .name = "DOS",
349 .part_type = PART_TYPE_DOS,
Petr Kulhavy712257e2016-09-09 10:27:15 +0200350 .max_entries = DOS_ENTRY_NUMBERS,
Simon Glassb89a8442016-02-29 15:25:48 -0700351 .get_info = part_get_info_ptr(part_get_info_dos),
Simon Glass0f1c9522016-02-29 15:26:04 -0700352 .print = part_print_ptr(part_print_dos),
353 .test = part_test_dos,
Simon Glass83ce5632016-02-29 15:25:47 -0700354};
wdenkfe8c2802002-11-03 00:38:21 +0000355
Jon Loeligerf7f54a82007-07-09 17:22:37 -0500356#endif