blob: 5c77225cef93ca82b2f8e14897a80eeb427cb04e [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
Simon Glass655306c2020-05-10 11:39:58 -060016#include <blk.h>
wdenkfe8c2802002-11-03 00:38:21 +000017#include <command.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060018#include <memalign.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060019#include <vsprintf.h>
Marek Szyprowski552f79c2020-12-23 13:55:12 +010020#include <asm/unaligned.h>
Marek Szyprowskicbea6472020-12-23 13:55:13 +010021#include <linux/compiler.h>
wdenkfe8c2802002-11-03 00:38:21 +000022#include "part_dos.h"
Simon Glass655306c2020-05-10 11:39:58 -060023#include <part.h>
wdenkfe8c2802002-11-03 00:38:21 +000024
Darwin Dingel2c1e1b62014-06-06 15:48:26 +120025#define DOS_PART_DEFAULT_SECTOR 512
26
Paul Emge61494122019-07-08 16:37:03 -070027/* should this be configurable? It looks like it's not very common at all
28 * to use large numbers of partitions */
29#define MAX_EXT_PARTS 256
30
wdenkfe8c2802002-11-03 00:38:21 +000031static inline int is_extended(int part_type)
32{
Marek Szyprowski654fa832020-12-23 13:55:11 +010033 return (part_type == DOS_PART_TYPE_EXTENDED ||
34 part_type == DOS_PART_TYPE_EXTENDED_LBA ||
35 part_type == DOS_PART_TYPE_EXTENDED_LINUX);
wdenkfe8c2802002-11-03 00:38:21 +000036}
37
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010038static int get_bootable(dos_partition_t *p)
Rob Herringeda51112012-08-23 11:31:43 +000039{
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010040 int ret = 0;
41
42 if (p->sys_ind == 0xef)
43 ret |= PART_EFI_SYSTEM_PARTITION;
44 if (p->boot_ind == 0x80)
45 ret |= PART_BOOTABLE;
46 return ret;
Rob Herringeda51112012-08-23 11:31:43 +000047}
48
Stefan Monnier4c6e9602015-08-25 15:24:13 -040049static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
Stephen Warren37c88ea2012-10-08 08:14:40 +000050 int part_num, unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +000051{
Marek Szyprowski552f79c2020-12-23 13:55:12 +010052 lbaint_t lba_start = ext_part_sector + get_unaligned_le32(p->start4);
53 lbaint_t lba_size = get_unaligned_le32(p->size4);
wdenkfe8c2802002-11-03 00:38:21 +000054
Stefan Monnier4c6e9602015-08-25 15:24:13 -040055 printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
56 "u\t%08x-%02x\t%02x%s%s\n",
Stephen Warren37c88ea2012-10-08 08:14:40 +000057 part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
Rob Herringeda51112012-08-23 11:31:43 +000058 (is_extended(p->sys_ind) ? " Extd" : ""),
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +010059 (get_bootable(p) ? " Boot" : ""));
wdenkfe8c2802002-11-03 00:38:21 +000060}
61
wdenk2c9b05d2003-09-10 22:30:53 +000062static int test_block_type(unsigned char *buffer)
63{
Egbert Eich300494b2013-04-09 05:46:14 +000064 int slot;
65 struct dos_partition *p;
Heinrich Schuchardt86c8dd42019-10-15 20:43:42 +020066 int part_count = 0;
Egbert Eich300494b2013-04-09 05:46:14 +000067
wdenk2c9b05d2003-09-10 22:30:53 +000068 if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
69 (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
70 return (-1);
71 } /* no DOS Signature at all */
Egbert Eich300494b2013-04-09 05:46:14 +000072 p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
Heinrich Schuchardt86c8dd42019-10-15 20:43:42 +020073
74 /* Check that the boot indicators are valid and count the partitions. */
75 for (slot = 0; slot < 4; ++slot, ++p) {
76 if (p->boot_ind != 0 && p->boot_ind != 0x80)
77 break;
78 if (p->sys_ind)
79 ++part_count;
Wolfgang Denk7b2290c2010-07-19 11:36:57 +020080 }
wdenk2c9b05d2003-09-10 22:30:53 +000081
Heinrich Schuchardt86c8dd42019-10-15 20:43:42 +020082 /*
83 * If the partition table is invalid or empty,
84 * check if this is a DOS PBR
85 */
86 if (slot != 4 || !part_count) {
87 if (!strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
88 "FAT", 3) ||
89 !strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
90 "FAT32", 5))
91 return DOS_PBR; /* This is a DOS PBR and not an MBR */
92 }
93 if (slot == 4)
94 return DOS_MBR; /* This is an DOS MBR */
95
96 /* This is neither a DOS MBR nor a DOS PBR */
97 return -1;
98}
wdenkfe8c2802002-11-03 00:38:21 +000099
Simon Glassd0633952023-08-24 13:55:26 -0600100static int part_test_dos(struct blk_desc *desc)
wdenkfe8c2802002-11-03 00:38:21 +0000101{
Simon Glass0e84d962024-09-29 19:49:50 -0600102#ifndef CONFIG_XPL_BUILD
Faiz Abbas1bca8de2019-09-04 20:10:12 +0530103 ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr,
Simon Glassd0633952023-08-24 13:55:26 -0600104 DIV_ROUND_UP(desc->blksz, sizeof(legacy_mbr)));
wdenkfe8c2802002-11-03 00:38:21 +0000105
Simon Glassd0633952023-08-24 13:55:26 -0600106 if (blk_dread(desc, 0, 1, (ulong *)mbr) != 1)
Stephen Warren71b4e3b2012-10-05 13:17:40 +0000107 return -1;
108
Peter Jonesc27e1c12017-09-13 18:05:25 -0400109 if (test_block_type((unsigned char *)mbr) != DOS_MBR)
Stephen Warren71b4e3b2012-10-05 13:17:40 +0000110 return -1;
111
Simon Glassd0633952023-08-24 13:55:26 -0600112 if (desc->sig_type == SIG_TYPE_NONE && mbr->unique_mbr_signature) {
113 desc->sig_type = SIG_TYPE_MBR;
114 desc->mbr_sig = mbr->unique_mbr_signature;
Peter Jonesc27e1c12017-09-13 18:05:25 -0400115 }
Fabio Estevamf0c82a82017-10-04 13:29:57 -0300116#else
Simon Glassd0633952023-08-24 13:55:26 -0600117 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz);
Fabio Estevamf0c82a82017-10-04 13:29:57 -0300118
Simon Glassd0633952023-08-24 13:55:26 -0600119 if (blk_dread(desc, 0, 1, (ulong *)buffer) != 1)
Fabio Estevamf0c82a82017-10-04 13:29:57 -0300120 return -1;
121
122 if (test_block_type(buffer) != DOS_MBR)
123 return -1;
124#endif
Peter Jonesc27e1c12017-09-13 18:05:25 -0400125
Stephen Warren71b4e3b2012-10-05 13:17:40 +0000126 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000127}
128
129/* Print a partition that is relative to its Extended partition table
130 */
Simon Glassd0633952023-08-24 13:55:26 -0600131static void print_partition_extended(struct blk_desc *desc,
Stefan Monnier4c6e9602015-08-25 15:24:13 -0400132 lbaint_t ext_part_sector,
133 lbaint_t relative,
Stephen Warren37c88ea2012-10-08 08:14:40 +0000134 int part_num, unsigned int disksig)
wdenkfe8c2802002-11-03 00:38:21 +0000135{
Simon Glassd0633952023-08-24 13:55:26 -0600136 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz);
wdenkfe8c2802002-11-03 00:38:21 +0000137 dos_partition_t *pt;
138 int i;
139
Paul Emge61494122019-07-08 16:37:03 -0700140 /* set a maximum recursion level */
141 if (part_num > MAX_EXT_PARTS)
142 {
143 printf("** Nested DOS partitions detected, stopping **\n");
144 return;
145 }
146
Simon Glassd0633952023-08-24 13:55:26 -0600147 if (blk_dread(desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
Stefan Monnier4c6e9602015-08-25 15:24:13 -0400148 printf ("** Can't read partition table on %d:" LBAFU " **\n",
Simon Glassd0633952023-08-24 13:55:26 -0600149 desc->devnum, ext_part_sector);
wdenkfe8c2802002-11-03 00:38:21 +0000150 return;
151 }
wdenk2c9b05d2003-09-10 22:30:53 +0000152 i=test_block_type(buffer);
Stephen Warren71b4e3b2012-10-05 13:17:40 +0000153 if (i != DOS_MBR) {
wdenkfe8c2802002-11-03 00:38:21 +0000154 printf ("bad MBR sector signature 0x%02x%02x\n",
155 buffer[DOS_PART_MAGIC_OFFSET],
156 buffer[DOS_PART_MAGIC_OFFSET + 1]);
157 return;
158 }
Stephen Warren71b4e3b2012-10-05 13:17:40 +0000159
Stephen Warren37c88ea2012-10-08 08:14:40 +0000160 if (!ext_part_sector)
Marek Szyprowski552f79c2020-12-23 13:55:12 +0100161 disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
Stephen Warren37c88ea2012-10-08 08:14:40 +0000162
wdenkfe8c2802002-11-03 00:38:21 +0000163 /* Print all primary/logical partitions */
164 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
165 for (i = 0; i < 4; i++, pt++) {
166 /*
wdenk57b2d802003-06-27 21:31:46 +0000167 * fdisk does not show the extended partitions that
wdenkfe8c2802002-11-03 00:38:21 +0000168 * are not in the MBR
169 */
170
171 if ((pt->sys_ind != 0) &&
172 (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
Stephen Warren37c88ea2012-10-08 08:14:40 +0000173 print_one_part(pt, ext_part_sector, part_num, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000174 }
175
176 /* Reverse engr the fdisk part# assignment rule! */
177 if ((ext_part_sector == 0) ||
178 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
179 part_num++;
180 }
181 }
182
183 /* Follows the extended partitions */
184 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
185 for (i = 0; i < 4; i++, pt++) {
186 if (is_extended (pt->sys_ind)) {
Stefan Monnier4c6e9602015-08-25 15:24:13 -0400187 lbaint_t lba_start
Marek Szyprowski552f79c2020-12-23 13:55:12 +0100188 = get_unaligned_le32 (pt->start4) + relative;
wdenkfe8c2802002-11-03 00:38:21 +0000189
Simon Glassd0633952023-08-24 13:55:26 -0600190 print_partition_extended(desc, lba_start,
191 !ext_part_sector ? lba_start :
192 relative, part_num, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000193 }
194 }
195
196 return;
197}
198
wdenkfe8c2802002-11-03 00:38:21 +0000199/* Print a partition that is relative to its Extended partition table
200 */
Simon Glassd0633952023-08-24 13:55:26 -0600201static int part_get_info_extended(struct blk_desc *desc,
Simon Glassb89a8442016-02-29 15:25:48 -0700202 lbaint_t ext_part_sector, lbaint_t relative,
203 int part_num, int which_part,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600204 struct disk_partition *info, uint disksig)
wdenkfe8c2802002-11-03 00:38:21 +0000205{
Simon Glassd0633952023-08-24 13:55:26 -0600206 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, desc->blksz);
Marek Vasutb393da52023-08-14 01:48:45 +0200207 struct disk_partition wdinfo = { 0 };
wdenkfe8c2802002-11-03 00:38:21 +0000208 dos_partition_t *pt;
Marek Vasutb393da52023-08-14 01:48:45 +0200209 int i, ret;
Darwin Dingel2c1e1b62014-06-06 15:48:26 +1200210 int dos_type;
wdenkfe8c2802002-11-03 00:38:21 +0000211
Paul Emge61494122019-07-08 16:37:03 -0700212 /* set a maximum recursion level */
213 if (part_num > MAX_EXT_PARTS)
214 {
215 printf("** Nested DOS partitions detected, stopping **\n");
216 return -1;
217 }
218
Simon Glassd0633952023-08-24 13:55:26 -0600219 if (blk_dread(desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
Stefan Monnier4c6e9602015-08-25 15:24:13 -0400220 printf ("** Can't read partition table on %d:" LBAFU " **\n",
Simon Glassd0633952023-08-24 13:55:26 -0600221 desc->devnum, ext_part_sector);
wdenkfe8c2802002-11-03 00:38:21 +0000222 return -1;
223 }
224 if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
225 buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
226 printf ("bad MBR sector signature 0x%02x%02x\n",
227 buffer[DOS_PART_MAGIC_OFFSET],
228 buffer[DOS_PART_MAGIC_OFFSET + 1]);
229 return -1;
230 }
231
Simon Glass23e34532023-08-24 13:55:31 -0600232 if (CONFIG_IS_ENABLED(PARTITION_UUIDS) && !ext_part_sector)
Marek Szyprowski552f79c2020-12-23 13:55:12 +0100233 disksig = get_unaligned_le32(&buffer[DOS_PART_DISKSIG_OFFSET]);
Stephen Warrenb287a872012-09-21 09:51:00 +0000234
Simon Glassd0633952023-08-24 13:55:26 -0600235 ret = part_get_info_whole_disk(desc, &wdinfo);
Marek Vasutb393da52023-08-14 01:48:45 +0200236 if (ret)
237 return ret;
238
wdenkfe8c2802002-11-03 00:38:21 +0000239 /* Print all primary/logical partitions */
240 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
241 for (i = 0; i < 4; i++, pt++) {
242 /*
wdenk57b2d802003-06-27 21:31:46 +0000243 * fdisk does not show the extended partitions that
244 * are not in the MBR
wdenkfe8c2802002-11-03 00:38:21 +0000245 */
Daniel Mack9a2671f2009-09-28 11:40:38 +0200246 if (((pt->boot_ind & ~0x80) == 0) &&
247 (pt->sys_ind != 0) &&
wdenk0893c472003-05-20 14:25:27 +0000248 (part_num == which_part) &&
Shawn Guo1c6d6792017-11-02 16:46:34 +0800249 (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
Marek Vasutb393da52023-08-14 01:48:45 +0200250 if (wdinfo.blksz > DOS_PART_DEFAULT_SECTOR)
251 info->blksz = wdinfo.blksz;
252 else
253 info->blksz = DOS_PART_DEFAULT_SECTOR;
Steve Raed30cbae2014-05-26 11:52:23 -0700254 info->start = (lbaint_t)(ext_part_sector +
Marek Szyprowski552f79c2020-12-23 13:55:12 +0100255 get_unaligned_le32(pt->start4));
256 info->size = (lbaint_t)get_unaligned_le32(pt->size4);
Simon Glassd0633952023-08-24 13:55:26 -0600257 part_set_generic_name(desc, part_num,
Petr Kulhavy2e790d52016-09-09 10:27:17 +0200258 (char *)info->name);
wdenkfe8c2802002-11-03 00:38:21 +0000259 /* sprintf(info->type, "%d, pt->sys_ind); */
Ben Whitten34fd6c92015-12-30 13:05:58 +0000260 strcpy((char *)info->type, "U-Boot");
Heinrich Schuchardt59a860d2020-03-19 13:49:34 +0100261 info->bootable = get_bootable(pt);
Simon Glass23e34532023-08-24 13:55:31 -0600262 if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) {
263 char str[12];
264
265 sprintf(str, "%08x-%02x", disksig, part_num);
266 disk_partition_set_uuid(info, str);
267 }
Dalon Westergreen8d770f42017-02-10 17:15:34 -0800268 info->sys_ind = pt->sys_ind;
wdenkfe8c2802002-11-03 00:38:21 +0000269 return 0;
270 }
271
272 /* Reverse engr the fdisk part# assignment rule! */
273 if ((ext_part_sector == 0) ||
274 (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
275 part_num++;
276 }
277 }
278
279 /* Follows the extended partitions */
280 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
281 for (i = 0; i < 4; i++, pt++) {
282 if (is_extended (pt->sys_ind)) {
Stefan Monnier4c6e9602015-08-25 15:24:13 -0400283 lbaint_t lba_start
Marek Szyprowski552f79c2020-12-23 13:55:12 +0100284 = get_unaligned_le32 (pt->start4) + relative;
wdenkfe8c2802002-11-03 00:38:21 +0000285
Simon Glassd0633952023-08-24 13:55:26 -0600286 return part_get_info_extended(desc, lba_start,
wdenkfe8c2802002-11-03 00:38:21 +0000287 ext_part_sector == 0 ? lba_start : relative,
Stephen Warrenb287a872012-09-21 09:51:00 +0000288 part_num, which_part, info, disksig);
wdenkfe8c2802002-11-03 00:38:21 +0000289 }
290 }
Darwin Dingel2c1e1b62014-06-06 15:48:26 +1200291
292 /* Check for DOS PBR if no partition is found */
293 dos_type = test_block_type(buffer);
294
295 if (dos_type == DOS_PBR) {
296 info->start = 0;
Simon Glassd0633952023-08-24 13:55:26 -0600297 info->size = desc->lba;
Marek Vasutb393da52023-08-14 01:48:45 +0200298 if (wdinfo.blksz > DOS_PART_DEFAULT_SECTOR)
299 info->blksz = wdinfo.blksz;
300 else
301 info->blksz = DOS_PART_DEFAULT_SECTOR;
Darwin Dingel2c1e1b62014-06-06 15:48:26 +1200302 info->bootable = 0;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000303 strcpy((char *)info->type, "U-Boot");
Simon Glass23e34532023-08-24 13:55:31 -0600304 disk_partition_clr_uuid(info);
Darwin Dingel2c1e1b62014-06-06 15:48:26 +1200305 return 0;
306 }
307
wdenkfe8c2802002-11-03 00:38:21 +0000308 return -1;
309}
310
Simon Glassd0633952023-08-24 13:55:26 -0600311static void __maybe_unused part_print_dos(struct blk_desc *desc)
wdenkfe8c2802002-11-03 00:38:21 +0000312{
Stephen Warren37c88ea2012-10-08 08:14:40 +0000313 printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
Simon Glassd0633952023-08-24 13:55:26 -0600314 print_partition_extended(desc, 0, 0, 1, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000315}
316
Simon Glassd0633952023-08-24 13:55:26 -0600317static int __maybe_unused part_get_info_dos(struct blk_desc *desc, int part,
318 struct disk_partition *info)
wdenkfe8c2802002-11-03 00:38:21 +0000319{
Simon Glassd0633952023-08-24 13:55:26 -0600320 return part_get_info_extended(desc, 0, 0, 1, part, info, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000321}
322
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200323int is_valid_dos_buf(void *buf)
324{
325 return test_block_type(buf) == DOS_MBR ? 0 : -1;
326}
327
Simon Glassc4001de2023-02-05 15:36:33 -0700328#if IS_ENABLED(CONFIG_CMD_MBR)
Marek Szyprowski6a60b672020-12-23 13:55:14 +0100329static void lba_to_chs(lbaint_t lba, unsigned char *rc, unsigned char *rh,
330 unsigned char *rs)
331{
332 unsigned int c, h, s;
333 /* use fixed CHS geometry */
334 unsigned int sectpertrack = 63;
335 unsigned int heads = 255;
336
337 c = (lba + 1) / sectpertrack / heads;
338 h = (lba + 1) / sectpertrack - c * heads;
339 s = (lba + 1) - (c * heads + h) * sectpertrack;
340
341 if (c > 1023) {
342 c = 1023;
343 h = 254;
344 s = 63;
345 }
346
347 *rc = c & 0xff;
348 *rh = h;
349 *rs = s + ((c & 0x300) >> 2);
350}
351
352static void mbr_fill_pt_entry(dos_partition_t *pt, lbaint_t start,
353 lbaint_t relative, lbaint_t size, uchar sys_ind, bool bootable)
354{
355 pt->boot_ind = bootable ? 0x80 : 0x00;
356 pt->sys_ind = sys_ind;
357 lba_to_chs(start, &pt->cyl, &pt->head, &pt->sector);
358 lba_to_chs(start + size - 1, &pt->end_cyl, &pt->end_head, &pt->end_sector);
359 put_unaligned_le32(relative, &pt->start4);
360 put_unaligned_le32(size, &pt->size4);
361}
362
363int write_mbr_partitions(struct blk_desc *dev,
364 struct disk_partition *p, int count, unsigned int disksig)
365{
366 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev->blksz);
367 lbaint_t ext_part_start = 0, ext_part_size = 0, ext_part_sect = 0;
368 dos_partition_t *pt;
369 int i;
370
371 memset(buffer, 0, dev->blksz);
372 buffer[DOS_PART_MAGIC_OFFSET] = 0x55;
373 buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa;
374 put_unaligned_le32(disksig, &buffer[DOS_PART_DISKSIG_OFFSET]);
375 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
376
377 /* create all primary partitions */
378 for (i = 0; i < 4 && i < count; i++, pt++) {
379 mbr_fill_pt_entry(pt, p[i].start, p[i].start, p[i].size,
380 p[i].sys_ind, p[i].bootable);
381 if (is_extended(p[i].sys_ind)) {
382 ext_part_start = p[i].start;
383 ext_part_size = p[i].size;
384 ext_part_sect = p[i].start;
385 }
386 }
387
388 if (i < count && !ext_part_start) {
389 printf("%s: extended partition is needed for more than 4 partitions\n",
390 __func__);
391 return -1;
392 }
393
394 /* write MBR */
395 if (blk_dwrite(dev, 0, 1, buffer) != 1) {
396 printf("%s: failed writing 'MBR' (1 blks at 0x0)\n",
397 __func__);
398 return -1;
399 }
400
401 /* create extended volumes */
402 for (; i < count; i++) {
403 lbaint_t next_ebr = 0;
404
405 memset(buffer, 0, dev->blksz);
406 buffer[DOS_PART_MAGIC_OFFSET] = 0x55;
407 buffer[DOS_PART_MAGIC_OFFSET + 1] = 0xaa;
408 pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
409
410 mbr_fill_pt_entry(pt, p[i].start, p[i].start - ext_part_sect,
411 p[i].size, p[i].sys_ind, p[i].bootable);
412
413 if (i + 1 < count) {
414 pt++;
415 next_ebr = p[i].start + p[i].size;
416 mbr_fill_pt_entry(pt, next_ebr,
417 next_ebr - ext_part_start,
418 p[i+1].start + p[i+1].size - next_ebr,
419 DOS_PART_TYPE_EXTENDED, 0);
420 }
421
422 /* write EBR */
423 if (blk_dwrite(dev, ext_part_sect, 1, buffer) != 1) {
424 printf("%s: failed writing 'EBR' (1 blks at 0x%lx)\n",
425 __func__, ext_part_sect);
426 return -1;
427 }
428 ext_part_sect = next_ebr;
429 }
430
Gary Bisson75b4b622021-01-28 09:10:07 +0100431 /* Update the partition table entries*/
Christian Melki34be5602021-06-07 11:21:15 +0200432 part_init(dev);
Gary Bisson75b4b622021-01-28 09:10:07 +0100433
Marek Szyprowski6a60b672020-12-23 13:55:14 +0100434 return 0;
435}
436
437int layout_mbr_partitions(struct disk_partition *p, int count,
438 lbaint_t total_sectors)
439{
440 struct disk_partition *ext = NULL;
441 int i, j;
442 lbaint_t ext_vol_start;
443
444 /* calculate primary partitions start and size if needed */
445 if (!p[0].start)
446 p[0].start = DOS_PART_DEFAULT_GAP;
447 for (i = 0; i < 4 && i < count; i++) {
448 if (!p[i].start)
449 p[i].start = p[i - 1].start + p[i - 1].size;
450 if (!p[i].size) {
451 lbaint_t end = total_sectors;
452 lbaint_t allocated = 0;
453
454 for (j = i + 1; j < 4 && j < count; j++) {
455 if (p[j].start) {
456 end = p[j].start;
457 break;
458 }
459 allocated += p[j].size;
460 }
461 p[i].size = end - allocated - p[i].start;
462 }
463 if (p[i].sys_ind == 0x05)
464 ext = &p[i];
465 }
466
Alexander Gendin038cb022023-10-09 01:24:36 +0000467 if (count <= 4)
Simon Glassb94d2042021-10-23 17:26:02 -0600468 return 0;
469
470 if (!ext) {
471 log_err("extended partition is needed for more than 4 partitions\n");
472 return -EINVAL;
Marek Szyprowski6a60b672020-12-23 13:55:14 +0100473 }
474
475 /* calculate extended volumes start and size if needed */
476 ext_vol_start = ext->start;
477 for (i = 4; i < count; i++) {
478 if (!p[i].start)
479 p[i].start = ext_vol_start + DOS_PART_DEFAULT_GAP;
480 if (!p[i].size) {
481 lbaint_t end = ext->start + ext->size;
482 lbaint_t allocated = 0;
483
484 for (j = i + 1; j < count; j++) {
485 if (p[j].start) {
486 end = p[j].start - DOS_PART_DEFAULT_GAP;
487 break;
488 }
489 allocated += p[j].size + DOS_PART_DEFAULT_GAP;
490 }
491 p[i].size = end - allocated - p[i].start;
492 }
493 ext_vol_start = p[i].start + p[i].size;
494 }
495
496 return 0;
497}
498#endif
499
Simon Glassd0633952023-08-24 13:55:26 -0600500int write_mbr_sector(struct blk_desc *desc, void *buf)
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200501{
502 if (is_valid_dos_buf(buf))
503 return -1;
504
505 /* write MBR */
Simon Glassd0633952023-08-24 13:55:26 -0600506 if (blk_dwrite(desc, 0, 1, buf) != 1) {
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200507 printf("%s: failed writing '%s' (1 blks at 0x0)\n",
508 __func__, "MBR");
509 return 1;
510 }
511
Gary Bisson75b4b622021-01-28 09:10:07 +0100512 /* Update the partition table entries*/
Simon Glassd0633952023-08-24 13:55:26 -0600513 part_init(desc);
Gary Bisson75b4b622021-01-28 09:10:07 +0100514
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200515 return 0;
516}
517
Simon Glass83ce5632016-02-29 15:25:47 -0700518U_BOOT_PART_TYPE(dos) = {
519 .name = "DOS",
520 .part_type = PART_TYPE_DOS,
Petr Kulhavy712257e2016-09-09 10:27:15 +0200521 .max_entries = DOS_ENTRY_NUMBERS,
Simon Glassb89a8442016-02-29 15:25:48 -0700522 .get_info = part_get_info_ptr(part_get_info_dos),
Simon Glass0f1c9522016-02-29 15:26:04 -0700523 .print = part_print_ptr(part_print_dos),
524 .test = part_test_dos,
Simon Glass83ce5632016-02-29 15:25:47 -0700525};