xref: /openbmc/u-boot/disk/part.c (revision b0d8f5bf)
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>
27735dd97bSGrant Likely #include <part.h>
28affae2bfSwdenk 
29affae2bfSwdenk #undef	PART_DEBUG
30affae2bfSwdenk 
31affae2bfSwdenk #ifdef	PART_DEBUG
32affae2bfSwdenk #define	PRINTF(fmt,args...)	printf (fmt ,##args)
33affae2bfSwdenk #else
34affae2bfSwdenk #define PRINTF(fmt,args...)
35affae2bfSwdenk #endif
36affae2bfSwdenk 
37149dded2Swdenk #if ((CONFIG_COMMANDS & CFG_CMD_IDE)	|| \
38149dded2Swdenk      (CONFIG_COMMANDS & CFG_CMD_SCSI)	|| \
39c935d3bdSwdenk      (CONFIG_COMMANDS & CFG_CMD_USB)	|| \
401968e615Swdenk      defined(CONFIG_MMC) || \
411968e615Swdenk      defined(CONFIG_SYSTEMACE) )
42affae2bfSwdenk 
43735dd97bSGrant Likely struct block_drvr {
44735dd97bSGrant Likely 	char *name;
45735dd97bSGrant Likely 	block_dev_desc_t* (*get_dev)(int dev);
46735dd97bSGrant Likely };
47735dd97bSGrant Likely 
48735dd97bSGrant Likely static const struct block_drvr block_drvr[] = {
49735dd97bSGrant Likely #if (CONFIG_COMMANDS & CFG_CMD_IDE)
50735dd97bSGrant Likely 	{ .name = "ide", .get_dev = ide_get_dev, },
51735dd97bSGrant Likely #endif
52735dd97bSGrant Likely #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
53735dd97bSGrant Likely 	{ .name = "scsi", .get_dev = scsi_get_dev, },
54735dd97bSGrant Likely #endif
55735dd97bSGrant Likely #if ((CONFIG_COMMANDS & CFG_CMD_USB) && defined(CONFIG_USB_STORAGE))
56735dd97bSGrant Likely 	{ .name = "usb", .get_dev = usb_stor_get_dev, },
57735dd97bSGrant Likely #endif
58735dd97bSGrant Likely #if defined(CONFIG_MMC)
59735dd97bSGrant Likely 	{ .name = "mmc", .get_dev = mmc_get_dev, },
60735dd97bSGrant Likely #endif
61735dd97bSGrant Likely #if defined(CONFIG_SYSTEMACE)
62735dd97bSGrant Likely 	{ .name = "ace", .get_dev = systemace_get_dev, },
63735dd97bSGrant Likely #endif
64735dd97bSGrant Likely 	{ },
65735dd97bSGrant Likely };
66735dd97bSGrant Likely 
67751bb571SStefan Roese DECLARE_GLOBAL_DATA_PTR;
68751bb571SStefan Roese 
69735dd97bSGrant Likely block_dev_desc_t *get_dev(char* ifname, int dev)
70735dd97bSGrant Likely {
71735dd97bSGrant Likely 	const struct block_drvr *drvr = block_drvr;
72751bb571SStefan Roese 	block_dev_desc_t* (*reloc_get_dev)(int dev);
73751bb571SStefan Roese 
746c7cac8cSStefan Roese 	while (drvr->name) {
75751bb571SStefan Roese 		reloc_get_dev = drvr->get_dev + gd->reloc_off;
76751bb571SStefan Roese 		if (strncmp(ifname, drvr->name, strlen(drvr->name)) == 0)
77751bb571SStefan Roese 			return reloc_get_dev(dev);
78735dd97bSGrant Likely 		drvr++;
79735dd97bSGrant Likely 	}
80735dd97bSGrant Likely 	return NULL;
81735dd97bSGrant Likely }
82735dd97bSGrant Likely #else
83735dd97bSGrant Likely block_dev_desc_t *get_dev(char* ifname, int dev)
84735dd97bSGrant Likely {
85735dd97bSGrant Likely 	return NULL;
86735dd97bSGrant Likely }
87735dd97bSGrant Likely #endif
88735dd97bSGrant Likely 
89735dd97bSGrant Likely #if ((CONFIG_COMMANDS & CFG_CMD_IDE)	|| \
90735dd97bSGrant Likely      (CONFIG_COMMANDS & CFG_CMD_SCSI)	|| \
91735dd97bSGrant Likely      (CONFIG_COMMANDS & CFG_CMD_USB)	|| \
92735dd97bSGrant Likely      defined(CONFIG_MMC) || \
93735dd97bSGrant Likely      defined(CONFIG_SYSTEMACE) )
94735dd97bSGrant Likely 
95affae2bfSwdenk /* ------------------------------------------------------------------------- */
96affae2bfSwdenk /*
97affae2bfSwdenk  * reports device info to the user
98affae2bfSwdenk  */
99affae2bfSwdenk void dev_print (block_dev_desc_t *dev_desc)
100affae2bfSwdenk {
10142dfe7a1Swdenk #ifdef CONFIG_LBA48
102c40b2956Swdenk 	uint64_t lba512; /* number of blocks if 512bytes block size */
103c40b2956Swdenk #else
104c40b2956Swdenk 	lbaint_t lba512;
105c40b2956Swdenk #endif
106affae2bfSwdenk 
107affae2bfSwdenk 	if (dev_desc->type==DEV_TYPE_UNKNOWN) {
108affae2bfSwdenk 		puts ("not available\n");
109affae2bfSwdenk 		return;
110affae2bfSwdenk 	}
111affae2bfSwdenk 	if (dev_desc->if_type==IF_TYPE_SCSI)  {
112affae2bfSwdenk 		printf ("(%d:%d) ", dev_desc->target,dev_desc->lun);
113affae2bfSwdenk 	}
114affae2bfSwdenk 	if (dev_desc->if_type==IF_TYPE_IDE) {
115affae2bfSwdenk 		printf ("Model: %s Firm: %s Ser#: %s\n",
116affae2bfSwdenk 			dev_desc->vendor,
117affae2bfSwdenk 			dev_desc->revision,
118affae2bfSwdenk 			dev_desc->product);
119affae2bfSwdenk 	} else {
120affae2bfSwdenk 		printf ("Vendor: %s Prod.: %s Rev: %s\n",
121affae2bfSwdenk 			dev_desc->vendor,
122affae2bfSwdenk 			dev_desc->product,
123affae2bfSwdenk 			dev_desc->revision);
124affae2bfSwdenk 	}
125affae2bfSwdenk 	puts ("            Type: ");
126affae2bfSwdenk 	if (dev_desc->removable)
127affae2bfSwdenk 		puts ("Removable ");
128affae2bfSwdenk 	switch (dev_desc->type & 0x1F) {
129affae2bfSwdenk 		case DEV_TYPE_HARDDISK: puts ("Hard Disk");
130affae2bfSwdenk 					break;
131affae2bfSwdenk 		case DEV_TYPE_CDROM: 	puts ("CD ROM");
132affae2bfSwdenk 					break;
133affae2bfSwdenk 		case DEV_TYPE_OPDISK: 	puts ("Optical Device");
134affae2bfSwdenk 					break;
135affae2bfSwdenk 		case DEV_TYPE_TAPE: 	puts ("Tape");
136affae2bfSwdenk 					break;
137affae2bfSwdenk 		default:		printf ("# %02X #", dev_desc->type & 0x1F);
138affae2bfSwdenk 					break;
139affae2bfSwdenk 	}
140affae2bfSwdenk 	puts ("\n");
141affae2bfSwdenk 	if ((dev_desc->lba * dev_desc->blksz)>0L) {
142affae2bfSwdenk 		ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
143c40b2956Swdenk 		lbaint_t lba;
1446e592385Swdenk 
145c40b2956Swdenk 		lba = dev_desc->lba;
146affae2bfSwdenk 
147c40b2956Swdenk 		lba512 = (lba * (dev_desc->blksz/512));
148affae2bfSwdenk 		mb = (10 * lba512) / 2048;	/* 2048 = (1024 * 1024) / 512 MB */
149affae2bfSwdenk 		/* round to 1 digit */
150affae2bfSwdenk 		mb_quot	= mb / 10;
151affae2bfSwdenk 		mb_rem	= mb - (10 * mb_quot);
152affae2bfSwdenk 
153affae2bfSwdenk 		gb = mb / 1024;
154affae2bfSwdenk 		gb_quot	= gb / 10;
155affae2bfSwdenk 		gb_rem	= gb - (10 * gb_quot);
15642dfe7a1Swdenk #ifdef CONFIG_LBA48
1576e592385Swdenk 		if (dev_desc->lba48)
158c40b2956Swdenk 			printf ("            Supports 48-bit addressing\n");
159c40b2956Swdenk #endif
16042dfe7a1Swdenk #if defined(CFG_64BIT_LBA) && defined(CFG_64BIT_VSPRINTF)
161c40b2956Swdenk 		printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%qd x %ld)\n",
162c40b2956Swdenk 			mb_quot, mb_rem,
163c40b2956Swdenk 			gb_quot, gb_rem,
164c40b2956Swdenk 			lba,
165c40b2956Swdenk 			dev_desc->blksz);
166c40b2956Swdenk #else
167affae2bfSwdenk 		printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
168affae2bfSwdenk 			mb_quot, mb_rem,
169affae2bfSwdenk 			gb_quot, gb_rem,
170c40b2956Swdenk 			(ulong)lba,
171affae2bfSwdenk 			dev_desc->blksz);
172c40b2956Swdenk #endif
173affae2bfSwdenk 	} else {
174affae2bfSwdenk 		puts ("            Capacity: not available\n");
175affae2bfSwdenk 	}
176affae2bfSwdenk }
177c935d3bdSwdenk #endif	/* CFG_CMD_IDE || CFG_CMD_SCSI || CFG_CMD_USB || CONFIG_MMC */
178affae2bfSwdenk 
179c935d3bdSwdenk #if ((CONFIG_COMMANDS & CFG_CMD_IDE)	|| \
180c935d3bdSwdenk      (CONFIG_COMMANDS & CFG_CMD_SCSI)	|| \
1813f85ce27Swdenk      (CONFIG_COMMANDS & CFG_CMD_USB)	|| \
1827fac3f69SHaavard Skinnemoen      defined(CONFIG_MMC)		|| \
183*b0d8f5bfSPeter Pearse      (defined(CONFIG_MMC) && defined(CONFIG_LPC2292)) || \
1843f85ce27Swdenk      defined(CONFIG_SYSTEMACE)          )
185affae2bfSwdenk 
186affae2bfSwdenk #if defined(CONFIG_MAC_PARTITION) || \
187affae2bfSwdenk     defined(CONFIG_DOS_PARTITION) || \
188c7de829cSwdenk     defined(CONFIG_ISO_PARTITION) || \
189c7de829cSwdenk     defined(CONFIG_AMIGA_PARTITION)
190affae2bfSwdenk 
191affae2bfSwdenk void init_part (block_dev_desc_t * dev_desc)
192affae2bfSwdenk {
193affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION
194affae2bfSwdenk 	if (test_part_iso(dev_desc) == 0) {
195affae2bfSwdenk 		dev_desc->part_type = PART_TYPE_ISO;
196affae2bfSwdenk 		return;
197affae2bfSwdenk 	}
198affae2bfSwdenk #endif
199affae2bfSwdenk 
200affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION
201affae2bfSwdenk 	if (test_part_mac(dev_desc) == 0) {
202affae2bfSwdenk 		dev_desc->part_type = PART_TYPE_MAC;
203affae2bfSwdenk 		return;
204affae2bfSwdenk 	}
205affae2bfSwdenk #endif
206affae2bfSwdenk 
207affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION
208affae2bfSwdenk 	if (test_part_dos(dev_desc) == 0) {
209affae2bfSwdenk 		dev_desc->part_type = PART_TYPE_DOS;
210affae2bfSwdenk 		return;
211affae2bfSwdenk 	}
212affae2bfSwdenk #endif
213c7de829cSwdenk 
214c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION
215c7de829cSwdenk 	if (test_part_amiga(dev_desc) == 0) {
216c7de829cSwdenk 	    dev_desc->part_type = PART_TYPE_AMIGA;
217c7de829cSwdenk 	    return;
218c7de829cSwdenk 	}
219c7de829cSwdenk #endif
220affae2bfSwdenk }
221affae2bfSwdenk 
222affae2bfSwdenk 
223affae2bfSwdenk int get_partition_info (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
224affae2bfSwdenk {
225affae2bfSwdenk 		switch (dev_desc->part_type) {
226affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION
227affae2bfSwdenk 	case PART_TYPE_MAC:
228affae2bfSwdenk 		if (get_partition_info_mac(dev_desc,part,info) == 0) {
229affae2bfSwdenk 			PRINTF ("## Valid MAC partition found ##\n");
230affae2bfSwdenk 			return (0);
231affae2bfSwdenk 		}
232affae2bfSwdenk 		break;
233affae2bfSwdenk #endif
234affae2bfSwdenk 
235affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION
236affae2bfSwdenk 	case PART_TYPE_DOS:
237affae2bfSwdenk 		if (get_partition_info_dos(dev_desc,part,info) == 0) {
238affae2bfSwdenk 			PRINTF ("## Valid DOS partition found ##\n");
239affae2bfSwdenk 			return (0);
240affae2bfSwdenk 		}
241affae2bfSwdenk 		break;
242affae2bfSwdenk #endif
243affae2bfSwdenk 
244affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION
245affae2bfSwdenk 	case PART_TYPE_ISO:
246affae2bfSwdenk 		if (get_partition_info_iso(dev_desc,part,info) == 0) {
247affae2bfSwdenk 			PRINTF ("## Valid ISO boot partition found ##\n");
248affae2bfSwdenk 			return (0);
249affae2bfSwdenk 		}
250affae2bfSwdenk 		break;
251affae2bfSwdenk #endif
252c7de829cSwdenk 
253c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION
254c7de829cSwdenk 	case PART_TYPE_AMIGA:
255c7de829cSwdenk 	    if (get_partition_info_amiga(dev_desc, part, info) == 0)
256c7de829cSwdenk 	    {
257c7de829cSwdenk 		PRINTF ("## Valid Amiga partition found ##\n");
258c7de829cSwdenk 		return (0);
259c7de829cSwdenk 	    }
260c7de829cSwdenk 	    break;
261c7de829cSwdenk #endif
262affae2bfSwdenk 	default:
263affae2bfSwdenk 		break;
264affae2bfSwdenk 	}
265affae2bfSwdenk 	return (-1);
266affae2bfSwdenk }
267affae2bfSwdenk 
268affae2bfSwdenk static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
269affae2bfSwdenk {
270affae2bfSwdenk 	puts ("\nPartition Map for ");
271affae2bfSwdenk 	switch (dev_desc->if_type) {
272affae2bfSwdenk 		case IF_TYPE_IDE:  	puts ("IDE");
273affae2bfSwdenk 					break;
274affae2bfSwdenk 		case IF_TYPE_SCSI: 	puts ("SCSI");
275affae2bfSwdenk 					break;
276affae2bfSwdenk 		case IF_TYPE_ATAPI:	puts ("ATAPI");
277affae2bfSwdenk 					break;
278affae2bfSwdenk 		case IF_TYPE_USB:	puts ("USB");
279affae2bfSwdenk 					break;
280affae2bfSwdenk 		case IF_TYPE_DOC:	puts ("DOC");
281affae2bfSwdenk 					break;
282affae2bfSwdenk 		default: 		puts ("UNKNOWN");
283affae2bfSwdenk 					break;
284affae2bfSwdenk 	}
285affae2bfSwdenk 	printf (" device %d  --   Partition Type: %s\n\n",
286affae2bfSwdenk 			dev_desc->dev, type);
287affae2bfSwdenk }
288affae2bfSwdenk 
289affae2bfSwdenk void print_part (block_dev_desc_t * dev_desc)
290affae2bfSwdenk {
291affae2bfSwdenk 
292affae2bfSwdenk 		switch (dev_desc->part_type) {
293affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION
294affae2bfSwdenk 	case PART_TYPE_MAC:
295affae2bfSwdenk 		PRINTF ("## Testing for valid MAC partition ##\n");
296affae2bfSwdenk 		print_part_header ("MAC", dev_desc);
297affae2bfSwdenk 		print_part_mac (dev_desc);
298affae2bfSwdenk 		return;
299affae2bfSwdenk #endif
300affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION
301affae2bfSwdenk 	case PART_TYPE_DOS:
302affae2bfSwdenk 		PRINTF ("## Testing for valid DOS partition ##\n");
303affae2bfSwdenk 		print_part_header ("DOS", dev_desc);
304affae2bfSwdenk 		print_part_dos (dev_desc);
305affae2bfSwdenk 		return;
306affae2bfSwdenk #endif
307affae2bfSwdenk 
308affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION
309affae2bfSwdenk 	case PART_TYPE_ISO:
310affae2bfSwdenk 		PRINTF ("## Testing for valid ISO Boot partition ##\n");
311affae2bfSwdenk 		print_part_header ("ISO", dev_desc);
312affae2bfSwdenk 		print_part_iso (dev_desc);
313affae2bfSwdenk 		return;
314affae2bfSwdenk #endif
315c7de829cSwdenk 
316c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION
317c7de829cSwdenk 	case PART_TYPE_AMIGA:
318c7de829cSwdenk 	    PRINTF ("## Testing for a valid Amiga partition ##\n");
319c7de829cSwdenk 	    print_part_header ("AMIGA", dev_desc);
320c7de829cSwdenk 	    print_part_amiga (dev_desc);
321c7de829cSwdenk 	    return;
322c7de829cSwdenk #endif
323affae2bfSwdenk 	}
324affae2bfSwdenk 	puts ("## Unknown partition table\n");
325affae2bfSwdenk }
326affae2bfSwdenk 
327affae2bfSwdenk 
328affae2bfSwdenk #else	/* neither MAC nor DOS nor ISO partition configured */
329affae2bfSwdenk # error neither CONFIG_MAC_PARTITION nor CONFIG_DOS_PARTITION nor CONFIG_ISO_PARTITION configured!
330affae2bfSwdenk #endif
331affae2bfSwdenk 
332affae2bfSwdenk #endif	/* (CONFIG_COMMANDS & CFG_CMD_IDE) || CONFIG_COMMANDS & CFG_CMD_SCSI) */
333