xref: /openbmc/u-boot/disk/part.c (revision 92ccc96b)
1affae2bfSwdenk /*
2affae2bfSwdenk  * (C) Copyright 2001
3affae2bfSwdenk  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4affae2bfSwdenk  *
5affae2bfSwdenk  * See file CREDITS for list of people who contributed to this
6affae2bfSwdenk  * project.
7affae2bfSwdenk  *
8affae2bfSwdenk  * This program is free software; you can redistribute it and/or
9affae2bfSwdenk  * modify it under the terms of the GNU General Public License as
10affae2bfSwdenk  * published by the Free Software Foundation; either version 2 of
11affae2bfSwdenk  * the License, or (at your option) any later version.
12affae2bfSwdenk  *
13affae2bfSwdenk  * This program is distributed in the hope that it will be useful,
14affae2bfSwdenk  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15affae2bfSwdenk  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16affae2bfSwdenk  * GNU General Public License for more details.
17affae2bfSwdenk  *
18affae2bfSwdenk  * You should have received a copy of the GNU General Public License
19affae2bfSwdenk  * along with this program; if not, write to the Free Software
20affae2bfSwdenk  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21affae2bfSwdenk  * MA 02111-1307 USA
22affae2bfSwdenk  */
23affae2bfSwdenk 
24affae2bfSwdenk #include <common.h>
25affae2bfSwdenk #include <command.h>
26affae2bfSwdenk #include <ide.h>
2710a37fd7SStephen Warren #include <malloc.h>
28735dd97bSGrant Likely #include <part.h>
29affae2bfSwdenk 
30affae2bfSwdenk #undef	PART_DEBUG
31affae2bfSwdenk 
32affae2bfSwdenk #ifdef	PART_DEBUG
33affae2bfSwdenk #define	PRINTF(fmt,args...)	printf (fmt ,##args)
34affae2bfSwdenk #else
35affae2bfSwdenk #define PRINTF(fmt,args...)
36affae2bfSwdenk #endif
37affae2bfSwdenk 
380c9c8fb5SGabe Black /* Rather than repeat this expression each time, add a define for it */
39cde5c64dSJon Loeliger #if (defined(CONFIG_CMD_IDE) || \
40c7057b52SDave Liu      defined(CONFIG_CMD_SATA) || \
41cde5c64dSJon Loeliger      defined(CONFIG_CMD_SCSI) || \
42cde5c64dSJon Loeliger      defined(CONFIG_CMD_USB) || \
431968e615Swdenk      defined(CONFIG_MMC) || \
441968e615Swdenk      defined(CONFIG_SYSTEMACE) )
450c9c8fb5SGabe Black #define HAVE_BLOCK_DEVICE
460c9c8fb5SGabe Black #endif
47affae2bfSwdenk 
48735dd97bSGrant Likely struct block_drvr {
49735dd97bSGrant Likely 	char *name;
50735dd97bSGrant Likely 	block_dev_desc_t* (*get_dev)(int dev);
51735dd97bSGrant Likely };
52735dd97bSGrant Likely 
53735dd97bSGrant Likely static const struct block_drvr block_drvr[] = {
54cde5c64dSJon Loeliger #if defined(CONFIG_CMD_IDE)
55735dd97bSGrant Likely 	{ .name = "ide", .get_dev = ide_get_dev, },
56735dd97bSGrant Likely #endif
57c7057b52SDave Liu #if defined(CONFIG_CMD_SATA)
58c7057b52SDave Liu 	{.name = "sata", .get_dev = sata_get_dev, },
59c7057b52SDave Liu #endif
60cde5c64dSJon Loeliger #if defined(CONFIG_CMD_SCSI)
61735dd97bSGrant Likely 	{ .name = "scsi", .get_dev = scsi_get_dev, },
62735dd97bSGrant Likely #endif
63cde5c64dSJon Loeliger #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
64735dd97bSGrant Likely 	{ .name = "usb", .get_dev = usb_stor_get_dev, },
65735dd97bSGrant Likely #endif
66735dd97bSGrant Likely #if defined(CONFIG_MMC)
67735dd97bSGrant Likely 	{ .name = "mmc", .get_dev = mmc_get_dev, },
68735dd97bSGrant Likely #endif
69735dd97bSGrant Likely #if defined(CONFIG_SYSTEMACE)
70735dd97bSGrant Likely 	{ .name = "ace", .get_dev = systemace_get_dev, },
71735dd97bSGrant Likely #endif
72735dd97bSGrant Likely 	{ },
73735dd97bSGrant Likely };
74735dd97bSGrant Likely 
75751bb571SStefan Roese DECLARE_GLOBAL_DATA_PTR;
76751bb571SStefan Roese 
770c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE
7899d2c205SRob Herring block_dev_desc_t *get_dev(const char *ifname, int dev)
79735dd97bSGrant Likely {
80735dd97bSGrant Likely 	const struct block_drvr *drvr = block_drvr;
81751bb571SStefan Roese 	block_dev_desc_t* (*reloc_get_dev)(int dev);
8223090dacSHeiko Schocher 	char *name;
83751bb571SStefan Roese 
847e71dc68STim Kientzle 	if (!ifname)
857e71dc68STim Kientzle 		return NULL;
867e71dc68STim Kientzle 
8723090dacSHeiko Schocher 	name = drvr->name;
882e5167ccSWolfgang Denk #ifdef CONFIG_NEEDS_MANUAL_RELOC
8923090dacSHeiko Schocher 	name += gd->reloc_off;
9023090dacSHeiko Schocher #endif
91b16aadf4SLei Wen 	while (drvr->name) {
9223090dacSHeiko Schocher 		name = drvr->name;
93521af04dSPeter Tyser 		reloc_get_dev = drvr->get_dev;
942e5167ccSWolfgang Denk #ifdef CONFIG_NEEDS_MANUAL_RELOC
9523090dacSHeiko Schocher 		name += gd->reloc_off;
96521af04dSPeter Tyser 		reloc_get_dev += gd->reloc_off;
97521af04dSPeter Tyser #endif
9823090dacSHeiko Schocher 		if (strncmp(ifname, name, strlen(name)) == 0)
99751bb571SStefan Roese 			return reloc_get_dev(dev);
100735dd97bSGrant Likely 		drvr++;
101735dd97bSGrant Likely 	}
102735dd97bSGrant Likely 	return NULL;
103735dd97bSGrant Likely }
104735dd97bSGrant Likely #else
10599d2c205SRob Herring block_dev_desc_t *get_dev(const char *ifname, int dev)
106735dd97bSGrant Likely {
107735dd97bSGrant Likely 	return NULL;
108735dd97bSGrant Likely }
109735dd97bSGrant Likely #endif
110735dd97bSGrant Likely 
1110c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE
112735dd97bSGrant Likely 
113affae2bfSwdenk /* ------------------------------------------------------------------------- */
114affae2bfSwdenk /*
115affae2bfSwdenk  * reports device info to the user
116affae2bfSwdenk  */
11769a2a4d9SSergei Trofimovich 
11869a2a4d9SSergei Trofimovich #ifdef CONFIG_LBA48
11969a2a4d9SSergei Trofimovich typedef uint64_t lba512_t;
12069a2a4d9SSergei Trofimovich #else
12169a2a4d9SSergei Trofimovich typedef lbaint_t lba512_t;
12269a2a4d9SSergei Trofimovich #endif
12369a2a4d9SSergei Trofimovich 
12469a2a4d9SSergei Trofimovich /*
12569a2a4d9SSergei Trofimovich  * Overflowless variant of (block_count * mul_by / div_by)
12669a2a4d9SSergei Trofimovich  * when div_by > mul_by
12769a2a4d9SSergei Trofimovich  */
12869a2a4d9SSergei Trofimovich static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by)
12969a2a4d9SSergei Trofimovich {
13069a2a4d9SSergei Trofimovich 	lba512_t bc_quot, bc_rem;
13169a2a4d9SSergei Trofimovich 
13269a2a4d9SSergei Trofimovich 	/* x * m / d == x / d * m + (x % d) * m / d */
13369a2a4d9SSergei Trofimovich 	bc_quot = block_count / div_by;
13469a2a4d9SSergei Trofimovich 	bc_rem  = block_count - div_by * bc_quot;
13569a2a4d9SSergei Trofimovich 	return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
13669a2a4d9SSergei Trofimovich }
13769a2a4d9SSergei Trofimovich 
138affae2bfSwdenk void dev_print (block_dev_desc_t *dev_desc)
139affae2bfSwdenk {
14069a2a4d9SSergei Trofimovich 	lba512_t lba512; /* number of blocks if 512bytes block size */
141affae2bfSwdenk 
142af75a45dSWolfgang Denk 	if (dev_desc->type == DEV_TYPE_UNKNOWN) {
143af75a45dSWolfgang Denk 		puts ("not available\n");
144af75a45dSWolfgang Denk 		return;
145af75a45dSWolfgang Denk 	}
146af75a45dSWolfgang Denk 
1478ec6e332STor Krill 	switch (dev_desc->if_type) {
148574b3195SDetlev Zundel 	case IF_TYPE_SCSI:
149574b3195SDetlev Zundel 		printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
150574b3195SDetlev Zundel 			dev_desc->target,dev_desc->lun,
151affae2bfSwdenk 			dev_desc->vendor,
152affae2bfSwdenk 			dev_desc->product,
153affae2bfSwdenk 			dev_desc->revision);
154574b3195SDetlev Zundel 		break;
1556e24a1ebSRemy Bohmer 	case IF_TYPE_ATAPI:
156574b3195SDetlev Zundel 	case IF_TYPE_IDE:
157574b3195SDetlev Zundel 	case IF_TYPE_SATA:
158574b3195SDetlev Zundel 		printf ("Model: %s Firm: %s Ser#: %s\n",
159574b3195SDetlev Zundel 			dev_desc->vendor,
160574b3195SDetlev Zundel 			dev_desc->revision,
161574b3195SDetlev Zundel 			dev_desc->product);
162574b3195SDetlev Zundel 		break;
1636e24a1ebSRemy Bohmer 	case IF_TYPE_SD:
1646e24a1ebSRemy Bohmer 	case IF_TYPE_MMC:
16547bebe34SNícolas Carneiro Lebedenco 	case IF_TYPE_USB:
16647bebe34SNícolas Carneiro Lebedenco 		printf ("Vendor: %s Rev: %s Prod: %s\n",
16747bebe34SNícolas Carneiro Lebedenco 			dev_desc->vendor,
16847bebe34SNícolas Carneiro Lebedenco 			dev_desc->revision,
16947bebe34SNícolas Carneiro Lebedenco 			dev_desc->product);
17047bebe34SNícolas Carneiro Lebedenco 		break;
1716e24a1ebSRemy Bohmer 	case IF_TYPE_DOC:
1726e24a1ebSRemy Bohmer 		puts("device type DOC\n");
1736e24a1ebSRemy Bohmer 		return;
1748ec6e332STor Krill 	case IF_TYPE_UNKNOWN:
1756e24a1ebSRemy Bohmer 		puts("device type unknown\n");
1766e24a1ebSRemy Bohmer 		return;
177574b3195SDetlev Zundel 	default:
1786e24a1ebSRemy Bohmer 		printf("Unhandled device type: %i\n", dev_desc->if_type);
179574b3195SDetlev Zundel 		return;
180affae2bfSwdenk 	}
181affae2bfSwdenk 	puts ("            Type: ");
182affae2bfSwdenk 	if (dev_desc->removable)
183affae2bfSwdenk 		puts ("Removable ");
184affae2bfSwdenk 	switch (dev_desc->type & 0x1F) {
185726c0f1eSDetlev Zundel 	case DEV_TYPE_HARDDISK:
186726c0f1eSDetlev Zundel 		puts ("Hard Disk");
187affae2bfSwdenk 		break;
188726c0f1eSDetlev Zundel 	case DEV_TYPE_CDROM:
189726c0f1eSDetlev Zundel 		puts ("CD ROM");
190affae2bfSwdenk 		break;
191726c0f1eSDetlev Zundel 	case DEV_TYPE_OPDISK:
192726c0f1eSDetlev Zundel 		puts ("Optical Device");
193affae2bfSwdenk 		break;
194726c0f1eSDetlev Zundel 	case DEV_TYPE_TAPE:
195726c0f1eSDetlev Zundel 		puts ("Tape");
196affae2bfSwdenk 		break;
197726c0f1eSDetlev Zundel 	default:
198726c0f1eSDetlev Zundel 		printf ("# %02X #", dev_desc->type & 0x1F);
199affae2bfSwdenk 		break;
200affae2bfSwdenk 	}
201affae2bfSwdenk 	puts ("\n");
20233699df1SJerry Huang 	if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
203affae2bfSwdenk 		ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
204c40b2956Swdenk 		lbaint_t lba;
2056e592385Swdenk 
206c40b2956Swdenk 		lba = dev_desc->lba;
207affae2bfSwdenk 
208c40b2956Swdenk 		lba512 = (lba * (dev_desc->blksz/512));
209affae2bfSwdenk 		/* round to 1 digit */
21069a2a4d9SSergei Trofimovich 		mb = lba512_muldiv(lba512, 10, 2048);	/* 2048 = (1024 * 1024) / 512 MB */
21169a2a4d9SSergei Trofimovich 
212affae2bfSwdenk 		mb_quot	= mb / 10;
213affae2bfSwdenk 		mb_rem	= mb - (10 * mb_quot);
214affae2bfSwdenk 
215affae2bfSwdenk 		gb = mb / 1024;
216affae2bfSwdenk 		gb_quot	= gb / 10;
217affae2bfSwdenk 		gb_rem	= gb - (10 * gb_quot);
21842dfe7a1Swdenk #ifdef CONFIG_LBA48
2196e592385Swdenk 		if (dev_desc->lba48)
220c40b2956Swdenk 			printf ("            Supports 48-bit addressing\n");
221c40b2956Swdenk #endif
2224b142febSHeiko Schocher #if defined(CONFIG_SYS_64BIT_LBA)
2236c6166f5SMike Frysinger 		printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
224c40b2956Swdenk 			mb_quot, mb_rem,
225c40b2956Swdenk 			gb_quot, gb_rem,
226c40b2956Swdenk 			lba,
227c40b2956Swdenk 			dev_desc->blksz);
228c40b2956Swdenk #else
229affae2bfSwdenk 		printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
230affae2bfSwdenk 			mb_quot, mb_rem,
231affae2bfSwdenk 			gb_quot, gb_rem,
232c40b2956Swdenk 			(ulong)lba,
233affae2bfSwdenk 			dev_desc->blksz);
234c40b2956Swdenk #endif
235affae2bfSwdenk 	} else {
236affae2bfSwdenk 		puts ("            Capacity: not available\n");
237affae2bfSwdenk 	}
238affae2bfSwdenk }
239b3aff0cbSJon Loeliger #endif
240affae2bfSwdenk 
2410c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE
242affae2bfSwdenk 
243affae2bfSwdenk void init_part (block_dev_desc_t * dev_desc)
244affae2bfSwdenk {
245affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION
246affae2bfSwdenk 	if (test_part_iso(dev_desc) == 0) {
247affae2bfSwdenk 		dev_desc->part_type = PART_TYPE_ISO;
248affae2bfSwdenk 		return;
249affae2bfSwdenk 	}
250affae2bfSwdenk #endif
251affae2bfSwdenk 
252affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION
253affae2bfSwdenk 	if (test_part_mac(dev_desc) == 0) {
254affae2bfSwdenk 		dev_desc->part_type = PART_TYPE_MAC;
255affae2bfSwdenk 		return;
256affae2bfSwdenk 	}
257affae2bfSwdenk #endif
258affae2bfSwdenk 
25907f3d789Srichardretanubun /* must be placed before DOS partition detection */
26007f3d789Srichardretanubun #ifdef CONFIG_EFI_PARTITION
26107f3d789Srichardretanubun 	if (test_part_efi(dev_desc) == 0) {
26207f3d789Srichardretanubun 		dev_desc->part_type = PART_TYPE_EFI;
26307f3d789Srichardretanubun 		return;
26407f3d789Srichardretanubun 	}
26507f3d789Srichardretanubun #endif
26607f3d789Srichardretanubun 
267affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION
268affae2bfSwdenk 	if (test_part_dos(dev_desc) == 0) {
269affae2bfSwdenk 		dev_desc->part_type = PART_TYPE_DOS;
270affae2bfSwdenk 		return;
271affae2bfSwdenk 	}
272affae2bfSwdenk #endif
273c7de829cSwdenk 
274c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION
275c7de829cSwdenk 	if (test_part_amiga(dev_desc) == 0) {
276c7de829cSwdenk 	    dev_desc->part_type = PART_TYPE_AMIGA;
277c7de829cSwdenk 	    return;
278c7de829cSwdenk 	}
279c7de829cSwdenk #endif
28099d2c205SRob Herring 	dev_desc->part_type = PART_TYPE_UNKNOWN;
281affae2bfSwdenk }
282affae2bfSwdenk 
283affae2bfSwdenk 
2840c9c8fb5SGabe Black #if defined(CONFIG_MAC_PARTITION) || \
2850c9c8fb5SGabe Black 	defined(CONFIG_DOS_PARTITION) || \
2860c9c8fb5SGabe Black 	defined(CONFIG_ISO_PARTITION) || \
2870c9c8fb5SGabe Black 	defined(CONFIG_AMIGA_PARTITION) || \
2880c9c8fb5SGabe Black 	defined(CONFIG_EFI_PARTITION)
2890c9c8fb5SGabe Black 
290affae2bfSwdenk static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
291affae2bfSwdenk {
292affae2bfSwdenk 	puts ("\nPartition Map for ");
293affae2bfSwdenk 	switch (dev_desc->if_type) {
294726c0f1eSDetlev Zundel 	case IF_TYPE_IDE:
295726c0f1eSDetlev Zundel 		puts ("IDE");
296affae2bfSwdenk 		break;
297726c0f1eSDetlev Zundel 	case IF_TYPE_SATA:
298726c0f1eSDetlev Zundel 		puts ("SATA");
299c7057b52SDave Liu 		break;
300726c0f1eSDetlev Zundel 	case IF_TYPE_SCSI:
301726c0f1eSDetlev Zundel 		puts ("SCSI");
302affae2bfSwdenk 		break;
303726c0f1eSDetlev Zundel 	case IF_TYPE_ATAPI:
304726c0f1eSDetlev Zundel 		puts ("ATAPI");
305affae2bfSwdenk 		break;
306726c0f1eSDetlev Zundel 	case IF_TYPE_USB:
307726c0f1eSDetlev Zundel 		puts ("USB");
308affae2bfSwdenk 		break;
309726c0f1eSDetlev Zundel 	case IF_TYPE_DOC:
310726c0f1eSDetlev Zundel 		puts ("DOC");
311affae2bfSwdenk 		break;
3128f3b9642SLei Wen 	case IF_TYPE_MMC:
3138f3b9642SLei Wen 		puts ("MMC");
3148f3b9642SLei Wen 		break;
315726c0f1eSDetlev Zundel 	default:
316726c0f1eSDetlev Zundel 		puts ("UNKNOWN");
317affae2bfSwdenk 		break;
318affae2bfSwdenk 	}
319affae2bfSwdenk 	printf (" device %d  --   Partition Type: %s\n\n",
320affae2bfSwdenk 			dev_desc->dev, type);
321affae2bfSwdenk }
322affae2bfSwdenk 
3230c9c8fb5SGabe Black #endif /* any CONFIG_..._PARTITION */
3240c9c8fb5SGabe Black 
325affae2bfSwdenk void print_part (block_dev_desc_t * dev_desc)
326affae2bfSwdenk {
327affae2bfSwdenk 
328affae2bfSwdenk 		switch (dev_desc->part_type) {
329affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION
330affae2bfSwdenk 	case PART_TYPE_MAC:
331affae2bfSwdenk 		PRINTF ("## Testing for valid MAC partition ##\n");
332affae2bfSwdenk 		print_part_header ("MAC", dev_desc);
333affae2bfSwdenk 		print_part_mac (dev_desc);
334affae2bfSwdenk 		return;
335affae2bfSwdenk #endif
336affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION
337affae2bfSwdenk 	case PART_TYPE_DOS:
338affae2bfSwdenk 		PRINTF ("## Testing for valid DOS partition ##\n");
339affae2bfSwdenk 		print_part_header ("DOS", dev_desc);
340affae2bfSwdenk 		print_part_dos (dev_desc);
341affae2bfSwdenk 		return;
342affae2bfSwdenk #endif
343affae2bfSwdenk 
344affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION
345affae2bfSwdenk 	case PART_TYPE_ISO:
346affae2bfSwdenk 		PRINTF ("## Testing for valid ISO Boot partition ##\n");
347affae2bfSwdenk 		print_part_header ("ISO", dev_desc);
348affae2bfSwdenk 		print_part_iso (dev_desc);
349affae2bfSwdenk 		return;
350affae2bfSwdenk #endif
351c7de829cSwdenk 
352c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION
353c7de829cSwdenk 	case PART_TYPE_AMIGA:
354c7de829cSwdenk 	    PRINTF ("## Testing for a valid Amiga partition ##\n");
355c7de829cSwdenk 	    print_part_header ("AMIGA", dev_desc);
356c7de829cSwdenk 	    print_part_amiga (dev_desc);
357c7de829cSwdenk 	    return;
358c7de829cSwdenk #endif
35907f3d789Srichardretanubun 
36007f3d789Srichardretanubun #ifdef CONFIG_EFI_PARTITION
36107f3d789Srichardretanubun 	case PART_TYPE_EFI:
36207f3d789Srichardretanubun 		PRINTF ("## Testing for valid EFI partition ##\n");
36307f3d789Srichardretanubun 		print_part_header ("EFI", dev_desc);
36407f3d789Srichardretanubun 		print_part_efi (dev_desc);
36507f3d789Srichardretanubun 		return;
36607f3d789Srichardretanubun #endif
367affae2bfSwdenk 	}
368affae2bfSwdenk 	puts ("## Unknown partition table\n");
369affae2bfSwdenk }
370affae2bfSwdenk 
3710c9c8fb5SGabe Black #endif /* HAVE_BLOCK_DEVICE */
3722f501646SStephen Warren 
3732f501646SStephen Warren int get_partition_info(block_dev_desc_t *dev_desc, int part
3742f501646SStephen Warren 					, disk_partition_t *info)
3752f501646SStephen Warren {
3760c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE
3772f501646SStephen Warren 
378894bfbbfSStephen Warren #ifdef CONFIG_PARTITION_UUIDS
379894bfbbfSStephen Warren 	/* The common case is no UUID support */
380894bfbbfSStephen Warren 	info->uuid[0] = 0;
381894bfbbfSStephen Warren #endif
382894bfbbfSStephen Warren 
3832f501646SStephen Warren 	switch (dev_desc->part_type) {
3842f501646SStephen Warren #ifdef CONFIG_MAC_PARTITION
3852f501646SStephen Warren 	case PART_TYPE_MAC:
3862f501646SStephen Warren 		if (get_partition_info_mac(dev_desc, part, info) == 0) {
3872f501646SStephen Warren 			PRINTF("## Valid MAC partition found ##\n");
3882f501646SStephen Warren 			return 0;
3892f501646SStephen Warren 		}
3902f501646SStephen Warren 		break;
3912f501646SStephen Warren #endif
3922f501646SStephen Warren 
3932f501646SStephen Warren #ifdef CONFIG_DOS_PARTITION
3942f501646SStephen Warren 	case PART_TYPE_DOS:
3952f501646SStephen Warren 		if (get_partition_info_dos(dev_desc, part, info) == 0) {
3962f501646SStephen Warren 			PRINTF("## Valid DOS partition found ##\n");
3972f501646SStephen Warren 			return 0;
3982f501646SStephen Warren 		}
3992f501646SStephen Warren 		break;
4002f501646SStephen Warren #endif
4012f501646SStephen Warren 
4022f501646SStephen Warren #ifdef CONFIG_ISO_PARTITION
4032f501646SStephen Warren 	case PART_TYPE_ISO:
4042f501646SStephen Warren 		if (get_partition_info_iso(dev_desc, part, info) == 0) {
4052f501646SStephen Warren 			PRINTF("## Valid ISO boot partition found ##\n");
4062f501646SStephen Warren 			return 0;
4072f501646SStephen Warren 		}
4082f501646SStephen Warren 		break;
4092f501646SStephen Warren #endif
4102f501646SStephen Warren 
4112f501646SStephen Warren #ifdef CONFIG_AMIGA_PARTITION
4122f501646SStephen Warren 	case PART_TYPE_AMIGA:
4132f501646SStephen Warren 		if (get_partition_info_amiga(dev_desc, part, info) == 0) {
4142f501646SStephen Warren 			PRINTF("## Valid Amiga partition found ##\n");
4152f501646SStephen Warren 			return 0;
4162f501646SStephen Warren 		}
4172f501646SStephen Warren 		break;
4182f501646SStephen Warren #endif
4192f501646SStephen Warren 
4202f501646SStephen Warren #ifdef CONFIG_EFI_PARTITION
4212f501646SStephen Warren 	case PART_TYPE_EFI:
4222f501646SStephen Warren 		if (get_partition_info_efi(dev_desc, part, info) == 0) {
4232f501646SStephen Warren 			PRINTF("## Valid EFI partition found ##\n");
4242f501646SStephen Warren 			return 0;
4252f501646SStephen Warren 		}
4262f501646SStephen Warren 		break;
4272f501646SStephen Warren #endif
4282f501646SStephen Warren 	default:
4292f501646SStephen Warren 		break;
4302f501646SStephen Warren 	}
4310c9c8fb5SGabe Black #endif /* HAVE_BLOCK_DEVICE */
4322f501646SStephen Warren 
4332f501646SStephen Warren 	return -1;
4342f501646SStephen Warren }
43599d2c205SRob Herring 
4362023e608SStephen Warren int get_device(const char *ifname, const char *dev_str,
4372023e608SStephen Warren 	       block_dev_desc_t **dev_desc)
4382023e608SStephen Warren {
4392023e608SStephen Warren 	char *ep;
4402023e608SStephen Warren 	int dev;
4412023e608SStephen Warren 
4422023e608SStephen Warren 	dev = simple_strtoul(dev_str, &ep, 16);
4432023e608SStephen Warren 	if (*ep) {
4442023e608SStephen Warren 		printf("** Bad device specification %s %s **\n",
4452023e608SStephen Warren 		       ifname, dev_str);
4462023e608SStephen Warren 		return -1;
4472023e608SStephen Warren 	}
4482023e608SStephen Warren 
4492023e608SStephen Warren 	*dev_desc = get_dev(ifname, dev);
4502023e608SStephen Warren 	if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
4512023e608SStephen Warren 		printf("** Bad device %s %s **\n", ifname, dev_str);
4522023e608SStephen Warren 		return -1;
4532023e608SStephen Warren 	}
4542023e608SStephen Warren 
4552023e608SStephen Warren 	return dev;
4562023e608SStephen Warren }
4572023e608SStephen Warren 
45810a37fd7SStephen Warren #define PART_UNSPECIFIED -2
45910a37fd7SStephen Warren #define PART_AUTO -1
46010a37fd7SStephen Warren #define MAX_SEARCH_PARTITIONS 16
46110a37fd7SStephen Warren int get_device_and_partition(const char *ifname, const char *dev_part_str,
46299d2c205SRob Herring 			     block_dev_desc_t **dev_desc,
46310a37fd7SStephen Warren 			     disk_partition_t *info, int allow_whole_dev)
46499d2c205SRob Herring {
46510a37fd7SStephen Warren 	int ret = -1;
46610a37fd7SStephen Warren 	const char *part_str;
46710a37fd7SStephen Warren 	char *dup_str = NULL;
46810a37fd7SStephen Warren 	const char *dev_str;
46999d2c205SRob Herring 	int dev;
47010a37fd7SStephen Warren 	char *ep;
47110a37fd7SStephen Warren 	int p;
47210a37fd7SStephen Warren 	int part;
47310a37fd7SStephen Warren 	disk_partition_t tmpinfo;
47499d2c205SRob Herring 
475*92ccc96bSSimon Glass 	/*
476*92ccc96bSSimon Glass 	 * For now, we have a special case for sandbox, since there is no
477*92ccc96bSSimon Glass 	 * real block device support.
478*92ccc96bSSimon Glass 	 */
479*92ccc96bSSimon Glass 	if (0 == strcmp(ifname, "host")) {
480*92ccc96bSSimon Glass 		*dev_desc = NULL;
481*92ccc96bSSimon Glass 		info->start = info->size =  info->blksz = 0;
482*92ccc96bSSimon Glass 		info->bootable = 0;
483*92ccc96bSSimon Glass 		strcpy((char *)info->type, BOOT_PART_TYPE);
484*92ccc96bSSimon Glass 		strcpy((char *)info->name, "Sandbox host");
485*92ccc96bSSimon Glass #ifdef CONFIG_PARTITION_UUIDS
486*92ccc96bSSimon Glass 		info->uuid[0] = 0;
487*92ccc96bSSimon Glass #endif
488*92ccc96bSSimon Glass 
489*92ccc96bSSimon Glass 		return 0;
490*92ccc96bSSimon Glass 	}
491*92ccc96bSSimon Glass 
49210a37fd7SStephen Warren 	/* If no dev_part_str, use bootdevice environment variable */
493a10973e7SStephen Warren 	if (!dev_part_str || !strlen(dev_part_str) ||
494a10973e7SStephen Warren 	    !strcmp(dev_part_str, "-"))
49510a37fd7SStephen Warren 		dev_part_str = getenv("bootdevice");
49699d2c205SRob Herring 
49710a37fd7SStephen Warren 	/* If still no dev_part_str, it's an error */
49810a37fd7SStephen Warren 	if (!dev_part_str) {
49910a37fd7SStephen Warren 		printf("** No device specified **\n");
50010a37fd7SStephen Warren 		goto cleanup;
50199d2c205SRob Herring 	}
50299d2c205SRob Herring 
50310a37fd7SStephen Warren 	/* Separate device and partition ID specification */
50410a37fd7SStephen Warren 	part_str = strchr(dev_part_str, ':');
50510a37fd7SStephen Warren 	if (part_str) {
50610a37fd7SStephen Warren 		dup_str = strdup(dev_part_str);
50710a37fd7SStephen Warren 		dup_str[part_str - dev_part_str] = 0;
50810a37fd7SStephen Warren 		dev_str = dup_str;
50910a37fd7SStephen Warren 		part_str++;
51010a37fd7SStephen Warren 	} else {
51110a37fd7SStephen Warren 		dev_str = dev_part_str;
51299d2c205SRob Herring 	}
51310a37fd7SStephen Warren 
51410a37fd7SStephen Warren 	/* Look up the device */
51510a37fd7SStephen Warren 	dev = get_device(ifname, dev_str, dev_desc);
51610a37fd7SStephen Warren 	if (dev < 0)
51710a37fd7SStephen Warren 		goto cleanup;
51810a37fd7SStephen Warren 
51910a37fd7SStephen Warren 	/* Convert partition ID string to number */
52010a37fd7SStephen Warren 	if (!part_str || !*part_str) {
52110a37fd7SStephen Warren 		part = PART_UNSPECIFIED;
52210a37fd7SStephen Warren 	} else if (!strcmp(part_str, "auto")) {
52310a37fd7SStephen Warren 		part = PART_AUTO;
52410a37fd7SStephen Warren 	} else {
52510a37fd7SStephen Warren 		/* Something specified -> use exactly that */
52610a37fd7SStephen Warren 		part = (int)simple_strtoul(part_str, &ep, 16);
52710a37fd7SStephen Warren 		/*
52810a37fd7SStephen Warren 		 * Less than whole string converted,
52910a37fd7SStephen Warren 		 * or request for whole device, but caller requires partition.
53010a37fd7SStephen Warren 		 */
53110a37fd7SStephen Warren 		if (*ep || (part == 0 && !allow_whole_dev)) {
53210a37fd7SStephen Warren 			printf("** Bad partition specification %s %s **\n",
53310a37fd7SStephen Warren 			    ifname, dev_part_str);
53410a37fd7SStephen Warren 			goto cleanup;
53510a37fd7SStephen Warren 		}
53610a37fd7SStephen Warren 	}
53710a37fd7SStephen Warren 
53810a37fd7SStephen Warren 	/*
53910a37fd7SStephen Warren 	 * No partition table on device,
54010a37fd7SStephen Warren 	 * or user requested partition 0 (entire device).
54110a37fd7SStephen Warren 	 */
54210a37fd7SStephen Warren 	if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
54310a37fd7SStephen Warren 	    (part == 0)) {
54410a37fd7SStephen Warren 		if (!(*dev_desc)->lba) {
54510a37fd7SStephen Warren 			printf("** Bad device size - %s %s **\n", ifname,
54610a37fd7SStephen Warren 			       dev_str);
54710a37fd7SStephen Warren 			goto cleanup;
54810a37fd7SStephen Warren 		}
54910a37fd7SStephen Warren 
55010a37fd7SStephen Warren 		/*
55110a37fd7SStephen Warren 		 * If user specified a partition ID other than 0,
55210a37fd7SStephen Warren 		 * or the calling command only accepts partitions,
55310a37fd7SStephen Warren 		 * it's an error.
55410a37fd7SStephen Warren 		 */
55510a37fd7SStephen Warren 		if ((part > 0) || (!allow_whole_dev)) {
55610a37fd7SStephen Warren 			printf("** No partition table - %s %s **\n", ifname,
55710a37fd7SStephen Warren 			       dev_str);
55810a37fd7SStephen Warren 			goto cleanup;
55910a37fd7SStephen Warren 		}
56010a37fd7SStephen Warren 
56199d2c205SRob Herring 		info->start = 0;
56210a37fd7SStephen Warren 		info->size = (*dev_desc)->lba;
56310a37fd7SStephen Warren 		info->blksz = (*dev_desc)->blksz;
56410a37fd7SStephen Warren 		info->bootable = 0;
5656ab6a650SStephen Warren 		strcpy((char *)info->type, BOOT_PART_TYPE);
5666ab6a650SStephen Warren 		strcpy((char *)info->name, "Whole Disk");
56710a37fd7SStephen Warren #ifdef CONFIG_PARTITION_UUIDS
56810a37fd7SStephen Warren 		info->uuid[0] = 0;
56910a37fd7SStephen Warren #endif
57099d2c205SRob Herring 
57110a37fd7SStephen Warren 		ret = 0;
57210a37fd7SStephen Warren 		goto cleanup;
57399d2c205SRob Herring 	}
57499d2c205SRob Herring 
57510a37fd7SStephen Warren 	/*
57610a37fd7SStephen Warren 	 * Now there's known to be a partition table,
57710a37fd7SStephen Warren 	 * not specifying a partition means to pick partition 1.
57810a37fd7SStephen Warren 	 */
57910a37fd7SStephen Warren 	if (part == PART_UNSPECIFIED)
58010a37fd7SStephen Warren 		part = 1;
58199d2c205SRob Herring 
58210a37fd7SStephen Warren 	/*
58310a37fd7SStephen Warren 	 * If user didn't specify a partition number, or did specify something
58410a37fd7SStephen Warren 	 * other than "auto", use that partition number directly.
58510a37fd7SStephen Warren 	 */
58610a37fd7SStephen Warren 	if (part != PART_AUTO) {
58710a37fd7SStephen Warren 		ret = get_partition_info(*dev_desc, part, info);
58899d2c205SRob Herring 		if (ret) {
58910a37fd7SStephen Warren 			printf("** Invalid partition %d **\n", part);
59010a37fd7SStephen Warren 			goto cleanup;
59110a37fd7SStephen Warren 		}
59210a37fd7SStephen Warren 	} else {
59310a37fd7SStephen Warren 		/*
59410a37fd7SStephen Warren 		 * Find the first bootable partition.
59510a37fd7SStephen Warren 		 * If none are bootable, fall back to the first valid partition.
59610a37fd7SStephen Warren 		 */
59710a37fd7SStephen Warren 		part = 0;
59810a37fd7SStephen Warren 		for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
59910a37fd7SStephen Warren 			ret = get_partition_info(*dev_desc, p, info);
60010a37fd7SStephen Warren 			if (ret)
60110a37fd7SStephen Warren 				continue;
60210a37fd7SStephen Warren 
60310a37fd7SStephen Warren 			/*
60410a37fd7SStephen Warren 			 * First valid partition, or new better partition?
60510a37fd7SStephen Warren 			 * If so, save partition ID.
60610a37fd7SStephen Warren 			 */
60710a37fd7SStephen Warren 			if (!part || info->bootable)
60810a37fd7SStephen Warren 				part = p;
60910a37fd7SStephen Warren 
61010a37fd7SStephen Warren 			/* Best possible partition? Stop searching. */
61110a37fd7SStephen Warren 			if (info->bootable)
61210a37fd7SStephen Warren 				break;
61310a37fd7SStephen Warren 
61410a37fd7SStephen Warren 			/*
61510a37fd7SStephen Warren 			 * We now need to search further for best possible.
61610a37fd7SStephen Warren 			 * If we what we just queried was the best so far,
61710a37fd7SStephen Warren 			 * save the info since we over-write it next loop.
61810a37fd7SStephen Warren 			 */
61910a37fd7SStephen Warren 			if (part == p)
62010a37fd7SStephen Warren 				tmpinfo = *info;
62110a37fd7SStephen Warren 		}
62210a37fd7SStephen Warren 		/* If we found any acceptable partition */
62310a37fd7SStephen Warren 		if (part) {
62410a37fd7SStephen Warren 			/*
62510a37fd7SStephen Warren 			 * If we searched all possible partition IDs,
62610a37fd7SStephen Warren 			 * return the first valid partition we found.
62710a37fd7SStephen Warren 			 */
62810a37fd7SStephen Warren 			if (p == MAX_SEARCH_PARTITIONS + 1)
62910a37fd7SStephen Warren 				*info = tmpinfo;
63010a37fd7SStephen Warren 		} else {
63110a37fd7SStephen Warren 			printf("** No valid partitions found **\n");
63271bba424SStephen Warren 			ret = -1;
63310a37fd7SStephen Warren 			goto cleanup;
63410a37fd7SStephen Warren 		}
63599d2c205SRob Herring 	}
63699d2c205SRob Herring 	if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
63799d2c205SRob Herring 		printf("** Invalid partition type \"%.32s\""
63899d2c205SRob Herring 			" (expect \"" BOOT_PART_TYPE "\")\n",
63999d2c205SRob Herring 			info->type);
64010a37fd7SStephen Warren 		ret  = -1;
64110a37fd7SStephen Warren 		goto cleanup;
64299d2c205SRob Herring 	}
64399d2c205SRob Herring 
64410a37fd7SStephen Warren 	ret = part;
64510a37fd7SStephen Warren 	goto cleanup;
64699d2c205SRob Herring 
64710a37fd7SStephen Warren cleanup:
64810a37fd7SStephen Warren 	free(dup_str);
64910a37fd7SStephen Warren 	return ret;
65099d2c205SRob Herring }
651