xref: /openbmc/u-boot/disk/part_dos.c (revision e87eb2705bcb5850b6af8ca79d3ba1d6a51f0a26)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2fe8c2806Swdenk /*
3fe8c2806Swdenk  * (C) Copyright 2001
4fe8c2806Swdenk  * Raymond Lo, lo@routefree.com
5fe8c2806Swdenk  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6fe8c2806Swdenk  */
7fe8c2806Swdenk 
8fe8c2806Swdenk /*
9fe8c2806Swdenk  * Support for harddisk partitions.
10fe8c2806Swdenk  *
11fe8c2806Swdenk  * To be compatible with LinuxPPC and Apple we use the standard Apple
12fe8c2806Swdenk  * SCSI disk partitioning scheme. For more information see:
13fe8c2806Swdenk  * http://developer.apple.com/techpubs/mac/Devices/Devices-126.html#MARKER-14-92
14fe8c2806Swdenk  */
15fe8c2806Swdenk 
16fe8c2806Swdenk #include <common.h>
17fe8c2806Swdenk #include <command.h>
18fe8c2806Swdenk #include <ide.h>
19cf92e05cSSimon Glass #include <memalign.h>
20fe8c2806Swdenk #include "part_dos.h"
21fe8c2806Swdenk 
221811a928SAdam Ford #ifdef CONFIG_HAVE_BLOCK_DEVICE
23fe8c2806Swdenk 
244a36be9bSDarwin Dingel #define DOS_PART_DEFAULT_SECTOR 512
254a36be9bSDarwin Dingel 
26*3b40245eSPaul Emge /* should this be configurable? It looks like it's not very common at all
27*3b40245eSPaul Emge  * to use large numbers of partitions */
28*3b40245eSPaul Emge #define MAX_EXT_PARTS 256
29*3b40245eSPaul Emge 
30fe8c2806Swdenk /* Convert char[4] in little endian format to the host format integer
31fe8c2806Swdenk  */
le32_to_int(unsigned char * le32)32d29892baSStefan Monnier static inline unsigned int le32_to_int(unsigned char *le32)
33fe8c2806Swdenk {
34fe8c2806Swdenk     return ((le32[3] << 24) +
35fe8c2806Swdenk 	    (le32[2] << 16) +
36fe8c2806Swdenk 	    (le32[1] << 8) +
37fe8c2806Swdenk 	     le32[0]
38fe8c2806Swdenk 	   );
39fe8c2806Swdenk }
40fe8c2806Swdenk 
is_extended(int part_type)41fe8c2806Swdenk static inline int is_extended(int part_type)
42fe8c2806Swdenk {
43fe8c2806Swdenk     return (part_type == 0x5 ||
44fe8c2806Swdenk 	    part_type == 0xf ||
45fe8c2806Swdenk 	    part_type == 0x85);
46fe8c2806Swdenk }
47fe8c2806Swdenk 
is_bootable(dos_partition_t * p)4840e0e568SRob Herring static inline int is_bootable(dos_partition_t *p)
4940e0e568SRob Herring {
50eefa0a64SAndre Przywara 	return (p->sys_ind == 0xef) || (p->boot_ind == 0x80);
5140e0e568SRob Herring }
5240e0e568SRob Herring 
print_one_part(dos_partition_t * p,lbaint_t ext_part_sector,int part_num,unsigned int disksig)53d29892baSStefan Monnier static void print_one_part(dos_partition_t *p, lbaint_t ext_part_sector,
54e2e9b378SStephen Warren 			   int part_num, unsigned int disksig)
55fe8c2806Swdenk {
56d29892baSStefan Monnier 	lbaint_t lba_start = ext_part_sector + le32_to_int (p->start4);
57d29892baSStefan Monnier 	lbaint_t lba_size  = le32_to_int (p->size4);
58fe8c2806Swdenk 
59d29892baSStefan Monnier 	printf("%3d\t%-10" LBAFlength "u\t%-10" LBAFlength
60d29892baSStefan Monnier 		"u\t%08x-%02x\t%02x%s%s\n",
61e2e9b378SStephen Warren 		part_num, lba_start, lba_size, disksig, part_num, p->sys_ind,
6240e0e568SRob Herring 		(is_extended(p->sys_ind) ? " Extd" : ""),
6340e0e568SRob Herring 		(is_bootable(p) ? " Boot" : ""));
64fe8c2806Swdenk }
65fe8c2806Swdenk 
test_block_type(unsigned char * buffer)667205e407Swdenk static int test_block_type(unsigned char *buffer)
677205e407Swdenk {
689d956e0fSEgbert Eich 	int slot;
699d956e0fSEgbert Eich 	struct dos_partition *p;
709d956e0fSEgbert Eich 
717205e407Swdenk 	if((buffer[DOS_PART_MAGIC_OFFSET + 0] != 0x55) ||
727205e407Swdenk 	    (buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) ) {
737205e407Swdenk 		return (-1);
747205e407Swdenk 	} /* no DOS Signature at all */
759d956e0fSEgbert Eich 	p = (struct dos_partition *)&buffer[DOS_PART_TBL_OFFSET];
769d956e0fSEgbert Eich 	for (slot = 0; slot < 3; slot++) {
779d956e0fSEgbert Eich 		if (p->boot_ind != 0 && p->boot_ind != 0x80) {
789d956e0fSEgbert Eich 			if (!slot &&
799d956e0fSEgbert Eich 			    (strncmp((char *)&buffer[DOS_PBR_FSTYPE_OFFSET],
809d956e0fSEgbert Eich 				     "FAT", 3) == 0 ||
819d956e0fSEgbert Eich 			     strncmp((char *)&buffer[DOS_PBR32_FSTYPE_OFFSET],
829d956e0fSEgbert Eich 				     "FAT32", 5) == 0)) {
837205e407Swdenk 				return DOS_PBR; /* is PBR */
849d956e0fSEgbert Eich 			} else {
859d956e0fSEgbert Eich 				return -1;
869d956e0fSEgbert Eich 			}
879d956e0fSEgbert Eich 		}
8866c2d73cSWolfgang Denk 	}
897205e407Swdenk 	return DOS_MBR;	    /* Is MBR */
907205e407Swdenk }
917205e407Swdenk 
92fe8c2806Swdenk 
part_test_dos(struct blk_desc * dev_desc)93084bf4c2SSimon Glass static int part_test_dos(struct blk_desc *dev_desc)
94fe8c2806Swdenk {
953ea05205SFabio Estevam #ifndef CONFIG_SPL_BUILD
968639e34dSAlexey Brodkin 	ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, mbr, 1);
97fe8c2806Swdenk 
98ff98cb90SPeter Jones 	if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1)
99d1efb644SStephen Warren 		return -1;
100d1efb644SStephen Warren 
101ff98cb90SPeter Jones 	if (test_block_type((unsigned char *)mbr) != DOS_MBR)
102d1efb644SStephen Warren 		return -1;
103d1efb644SStephen Warren 
104ff98cb90SPeter Jones 	if (dev_desc->sig_type == SIG_TYPE_NONE &&
105ff98cb90SPeter Jones 	    mbr->unique_mbr_signature != 0) {
106ff98cb90SPeter Jones 		dev_desc->sig_type = SIG_TYPE_MBR;
107ff98cb90SPeter Jones 		dev_desc->mbr_sig = mbr->unique_mbr_signature;
108ff98cb90SPeter Jones 	}
1093ea05205SFabio Estevam #else
1103ea05205SFabio Estevam 	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
1113ea05205SFabio Estevam 
1123ea05205SFabio Estevam 	if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
1133ea05205SFabio Estevam 		return -1;
1143ea05205SFabio Estevam 
1153ea05205SFabio Estevam 	if (test_block_type(buffer) != DOS_MBR)
1163ea05205SFabio Estevam 		return -1;
1173ea05205SFabio Estevam #endif
118ff98cb90SPeter Jones 
119d1efb644SStephen Warren 	return 0;
120fe8c2806Swdenk }
121fe8c2806Swdenk 
122fe8c2806Swdenk /*  Print a partition that is relative to its Extended partition table
123fe8c2806Swdenk  */
print_partition_extended(struct blk_desc * dev_desc,lbaint_t ext_part_sector,lbaint_t relative,int part_num,unsigned int disksig)1244101f687SSimon Glass static void print_partition_extended(struct blk_desc *dev_desc,
125d29892baSStefan Monnier 				     lbaint_t ext_part_sector,
126d29892baSStefan Monnier 				     lbaint_t relative,
127e2e9b378SStephen Warren 				     int part_num, unsigned int disksig)
128fe8c2806Swdenk {
129dec049d9SEric Nelson 	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
130fe8c2806Swdenk 	dos_partition_t *pt;
131fe8c2806Swdenk 	int i;
132fe8c2806Swdenk 
133*3b40245eSPaul Emge 	/* set a maximum recursion level */
134*3b40245eSPaul Emge 	if (part_num > MAX_EXT_PARTS)
135*3b40245eSPaul Emge 	{
136*3b40245eSPaul Emge 		printf("** Nested DOS partitions detected, stopping **\n");
137*3b40245eSPaul Emge 		return;
138*3b40245eSPaul Emge     }
139*3b40245eSPaul Emge 
1402a981dc2SSimon Glass 	if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
141d29892baSStefan Monnier 		printf ("** Can't read partition table on %d:" LBAFU " **\n",
142bcce53d0SSimon Glass 			dev_desc->devnum, ext_part_sector);
143fe8c2806Swdenk 		return;
144fe8c2806Swdenk 	}
1457205e407Swdenk 	i=test_block_type(buffer);
146d1efb644SStephen Warren 	if (i != DOS_MBR) {
147fe8c2806Swdenk 		printf ("bad MBR sector signature 0x%02x%02x\n",
148fe8c2806Swdenk 			buffer[DOS_PART_MAGIC_OFFSET],
149fe8c2806Swdenk 			buffer[DOS_PART_MAGIC_OFFSET + 1]);
150fe8c2806Swdenk 		return;
151fe8c2806Swdenk 	}
152d1efb644SStephen Warren 
153e2e9b378SStephen Warren 	if (!ext_part_sector)
154e2e9b378SStephen Warren 		disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
155e2e9b378SStephen Warren 
156fe8c2806Swdenk 	/* Print all primary/logical partitions */
157fe8c2806Swdenk 	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
158fe8c2806Swdenk 	for (i = 0; i < 4; i++, pt++) {
159fe8c2806Swdenk 		/*
160fe8c2806Swdenk 		 * fdisk does not show the extended partitions that
161fe8c2806Swdenk 		 * are not in the MBR
162fe8c2806Swdenk 		 */
163fe8c2806Swdenk 
164fe8c2806Swdenk 		if ((pt->sys_ind != 0) &&
165fe8c2806Swdenk 		    (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) {
166e2e9b378SStephen Warren 			print_one_part(pt, ext_part_sector, part_num, disksig);
167fe8c2806Swdenk 		}
168fe8c2806Swdenk 
169fe8c2806Swdenk 		/* Reverse engr the fdisk part# assignment rule! */
170fe8c2806Swdenk 		if ((ext_part_sector == 0) ||
171fe8c2806Swdenk 		    (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
172fe8c2806Swdenk 			part_num++;
173fe8c2806Swdenk 		}
174fe8c2806Swdenk 	}
175fe8c2806Swdenk 
176fe8c2806Swdenk 	/* Follows the extended partitions */
177fe8c2806Swdenk 	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
178fe8c2806Swdenk 	for (i = 0; i < 4; i++, pt++) {
179fe8c2806Swdenk 		if (is_extended (pt->sys_ind)) {
180d29892baSStefan Monnier 			lbaint_t lba_start
181d29892baSStefan Monnier 				= le32_to_int (pt->start4) + relative;
182fe8c2806Swdenk 
183fe8c2806Swdenk 			print_partition_extended(dev_desc, lba_start,
184304b5711SStephen Warren 				ext_part_sector == 0  ? lba_start : relative,
185e2e9b378SStephen Warren 				part_num, disksig);
186fe8c2806Swdenk 		}
187fe8c2806Swdenk 	}
188fe8c2806Swdenk 
189fe8c2806Swdenk 	return;
190fe8c2806Swdenk }
191fe8c2806Swdenk 
192fe8c2806Swdenk 
193fe8c2806Swdenk /*  Print a partition that is relative to its Extended partition table
194fe8c2806Swdenk  */
part_get_info_extended(struct blk_desc * dev_desc,lbaint_t ext_part_sector,lbaint_t relative,int part_num,int which_part,disk_partition_t * info,unsigned int disksig)1953e8bd469SSimon Glass static int part_get_info_extended(struct blk_desc *dev_desc,
1963e8bd469SSimon Glass 				  lbaint_t ext_part_sector, lbaint_t relative,
1973e8bd469SSimon Glass 				  int part_num, int which_part,
1983e8bd469SSimon Glass 				  disk_partition_t *info, unsigned int disksig)
199fe8c2806Swdenk {
200dec049d9SEric Nelson 	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
201fe8c2806Swdenk 	dos_partition_t *pt;
202fe8c2806Swdenk 	int i;
2034a36be9bSDarwin Dingel 	int dos_type;
204fe8c2806Swdenk 
205*3b40245eSPaul Emge 	/* set a maximum recursion level */
206*3b40245eSPaul Emge 	if (part_num > MAX_EXT_PARTS)
207*3b40245eSPaul Emge 	{
208*3b40245eSPaul Emge 		printf("** Nested DOS partitions detected, stopping **\n");
209*3b40245eSPaul Emge 		return -1;
210*3b40245eSPaul Emge     }
211*3b40245eSPaul Emge 
2122a981dc2SSimon Glass 	if (blk_dread(dev_desc, ext_part_sector, 1, (ulong *)buffer) != 1) {
213d29892baSStefan Monnier 		printf ("** Can't read partition table on %d:" LBAFU " **\n",
214bcce53d0SSimon Glass 			dev_desc->devnum, ext_part_sector);
215fe8c2806Swdenk 		return -1;
216fe8c2806Swdenk 	}
217fe8c2806Swdenk 	if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
218fe8c2806Swdenk 		buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
219fe8c2806Swdenk 		printf ("bad MBR sector signature 0x%02x%02x\n",
220fe8c2806Swdenk 			buffer[DOS_PART_MAGIC_OFFSET],
221fe8c2806Swdenk 			buffer[DOS_PART_MAGIC_OFFSET + 1]);
222fe8c2806Swdenk 		return -1;
223fe8c2806Swdenk 	}
224fe8c2806Swdenk 
225b331cd62SPatrick Delaunay #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
226d27b5f93SStephen Warren 	if (!ext_part_sector)
227d27b5f93SStephen Warren 		disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]);
228d27b5f93SStephen Warren #endif
229d27b5f93SStephen Warren 
230fe8c2806Swdenk 	/* Print all primary/logical partitions */
231fe8c2806Swdenk 	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
232fe8c2806Swdenk 	for (i = 0; i < 4; i++, pt++) {
233fe8c2806Swdenk 		/*
234fe8c2806Swdenk 		 * fdisk does not show the extended partitions that
235fe8c2806Swdenk 		 * are not in the MBR
236fe8c2806Swdenk 		 */
23778f4ca79SDaniel Mack 		if (((pt->boot_ind & ~0x80) == 0) &&
23878f4ca79SDaniel Mack 		    (pt->sys_ind != 0) &&
2397f70e853Swdenk 		    (part_num == which_part) &&
2408f7102cfSShawn Guo 		    (ext_part_sector == 0 || is_extended(pt->sys_ind) == 0)) {
2414a36be9bSDarwin Dingel 			info->blksz = DOS_PART_DEFAULT_SECTOR;
242e04350d2SSteve Rae 			info->start = (lbaint_t)(ext_part_sector +
243e04350d2SSteve Rae 					le32_to_int(pt->start4));
244e04350d2SSteve Rae 			info->size  = (lbaint_t)le32_to_int(pt->size4);
245da2ee24dSPetr Kulhavy 			part_set_generic_name(dev_desc, part_num,
246da2ee24dSPetr Kulhavy 					      (char *)info->name);
247fe8c2806Swdenk 			/* sprintf(info->type, "%d, pt->sys_ind); */
248192bc694SBen Whitten 			strcpy((char *)info->type, "U-Boot");
24940e0e568SRob Herring 			info->bootable = is_bootable(pt);
250b331cd62SPatrick Delaunay #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
251d27b5f93SStephen Warren 			sprintf(info->uuid, "%08x-%02x", disksig, part_num);
252d27b5f93SStephen Warren #endif
253f0fb4fa7SDalon Westergreen 			info->sys_ind = pt->sys_ind;
254fe8c2806Swdenk 			return 0;
255fe8c2806Swdenk 		}
256fe8c2806Swdenk 
257fe8c2806Swdenk 		/* Reverse engr the fdisk part# assignment rule! */
258fe8c2806Swdenk 		if ((ext_part_sector == 0) ||
259fe8c2806Swdenk 		    (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) {
260fe8c2806Swdenk 			part_num++;
261fe8c2806Swdenk 		}
262fe8c2806Swdenk 	}
263fe8c2806Swdenk 
264fe8c2806Swdenk 	/* Follows the extended partitions */
265fe8c2806Swdenk 	pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET);
266fe8c2806Swdenk 	for (i = 0; i < 4; i++, pt++) {
267fe8c2806Swdenk 		if (is_extended (pt->sys_ind)) {
268d29892baSStefan Monnier 			lbaint_t lba_start
269d29892baSStefan Monnier 				= le32_to_int (pt->start4) + relative;
270fe8c2806Swdenk 
2713e8bd469SSimon Glass 			return part_get_info_extended(dev_desc, lba_start,
272fe8c2806Swdenk 				 ext_part_sector == 0 ? lba_start : relative,
273d27b5f93SStephen Warren 				 part_num, which_part, info, disksig);
274fe8c2806Swdenk 		}
275fe8c2806Swdenk 	}
2764a36be9bSDarwin Dingel 
2774a36be9bSDarwin Dingel 	/* Check for DOS PBR if no partition is found */
2784a36be9bSDarwin Dingel 	dos_type = test_block_type(buffer);
2794a36be9bSDarwin Dingel 
2804a36be9bSDarwin Dingel 	if (dos_type == DOS_PBR) {
2814a36be9bSDarwin Dingel 		info->start = 0;
2824a36be9bSDarwin Dingel 		info->size = dev_desc->lba;
2834a36be9bSDarwin Dingel 		info->blksz = DOS_PART_DEFAULT_SECTOR;
2844a36be9bSDarwin Dingel 		info->bootable = 0;
285192bc694SBen Whitten 		strcpy((char *)info->type, "U-Boot");
286b331cd62SPatrick Delaunay #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
2874a36be9bSDarwin Dingel 		info->uuid[0] = 0;
2884a36be9bSDarwin Dingel #endif
2894a36be9bSDarwin Dingel 		return 0;
2904a36be9bSDarwin Dingel 	}
2914a36be9bSDarwin Dingel 
292fe8c2806Swdenk 	return -1;
293fe8c2806Swdenk }
294fe8c2806Swdenk 
part_print_dos(struct blk_desc * dev_desc)295084bf4c2SSimon Glass void part_print_dos(struct blk_desc *dev_desc)
296fe8c2806Swdenk {
297e2e9b378SStephen Warren 	printf("Part\tStart Sector\tNum Sectors\tUUID\t\tType\n");
298e2e9b378SStephen Warren 	print_partition_extended(dev_desc, 0, 0, 1, 0);
299fe8c2806Swdenk }
300fe8c2806Swdenk 
part_get_info_dos(struct blk_desc * dev_desc,int part,disk_partition_t * info)3013e8bd469SSimon Glass int part_get_info_dos(struct blk_desc *dev_desc, int part,
3024101f687SSimon Glass 		      disk_partition_t *info)
303fe8c2806Swdenk {
3043e8bd469SSimon Glass 	return part_get_info_extended(dev_desc, 0, 0, 1, part, info, 0);
305fe8c2806Swdenk }
306fe8c2806Swdenk 
is_valid_dos_buf(void * buf)307b6dd69a4SPetr Kulhavy int is_valid_dos_buf(void *buf)
308b6dd69a4SPetr Kulhavy {
309b6dd69a4SPetr Kulhavy 	return test_block_type(buf) == DOS_MBR ? 0 : -1;
310b6dd69a4SPetr Kulhavy }
311b6dd69a4SPetr Kulhavy 
write_mbr_partition(struct blk_desc * dev_desc,void * buf)312b6dd69a4SPetr Kulhavy int write_mbr_partition(struct blk_desc *dev_desc, void *buf)
313b6dd69a4SPetr Kulhavy {
314b6dd69a4SPetr Kulhavy 	if (is_valid_dos_buf(buf))
315b6dd69a4SPetr Kulhavy 		return -1;
316b6dd69a4SPetr Kulhavy 
317b6dd69a4SPetr Kulhavy 	/* write MBR */
318b6dd69a4SPetr Kulhavy 	if (blk_dwrite(dev_desc, 0, 1, buf) != 1) {
319b6dd69a4SPetr Kulhavy 		printf("%s: failed writing '%s' (1 blks at 0x0)\n",
320b6dd69a4SPetr Kulhavy 		       __func__, "MBR");
321b6dd69a4SPetr Kulhavy 		return 1;
322b6dd69a4SPetr Kulhavy 	}
323b6dd69a4SPetr Kulhavy 
324b6dd69a4SPetr Kulhavy 	return 0;
325b6dd69a4SPetr Kulhavy }
326b6dd69a4SPetr Kulhavy 
32796e5b03cSSimon Glass U_BOOT_PART_TYPE(dos) = {
32896e5b03cSSimon Glass 	.name		= "DOS",
32996e5b03cSSimon Glass 	.part_type	= PART_TYPE_DOS,
33087b8530fSPetr Kulhavy 	.max_entries	= DOS_ENTRY_NUMBERS,
3313e8bd469SSimon Glass 	.get_info	= part_get_info_ptr(part_get_info_dos),
332084bf4c2SSimon Glass 	.print		= part_print_ptr(part_print_dos),
333084bf4c2SSimon Glass 	.test		= part_test_dos,
33496e5b03cSSimon Glass };
335fe8c2806Swdenk 
336cde5c64dSJon Loeliger #endif
337