xref: /openbmc/u-boot/disk/part.c (revision 751bb571)
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 
67*751bb571SStefan Roese #ifndef CFG_FIXUP_RELOCATION
68*751bb571SStefan Roese DECLARE_GLOBAL_DATA_PTR;
69*751bb571SStefan Roese #endif
70*751bb571SStefan Roese 
71735dd97bSGrant Likely block_dev_desc_t *get_dev(char* ifname, int dev)
72735dd97bSGrant Likely {
73735dd97bSGrant Likely 	const struct block_drvr *drvr = block_drvr;
74735dd97bSGrant Likely 
75735dd97bSGrant Likely 	while (drvr->name) {
76*751bb571SStefan Roese #ifndef CFG_FIXUP_RELOCATION
77*751bb571SStefan Roese 		block_dev_desc_t* (*reloc_get_dev)(int dev);
78*751bb571SStefan Roese 
79*751bb571SStefan Roese 		reloc_get_dev = drvr->get_dev + gd->reloc_off;
80*751bb571SStefan Roese 		if (strncmp(ifname, drvr->name, strlen(drvr->name)) == 0)
81*751bb571SStefan Roese 			return reloc_get_dev(dev);
82*751bb571SStefan Roese #else
83735dd97bSGrant Likely 		if (strncmp(ifname, drvr->name, strlen(drvr->name)) == 0)
84735dd97bSGrant Likely 			return drvr->get_dev(dev);
85*751bb571SStefan Roese #endif
86735dd97bSGrant Likely 		drvr++;
87735dd97bSGrant Likely 	}
88735dd97bSGrant Likely 	return NULL;
89735dd97bSGrant Likely }
90735dd97bSGrant Likely #else
91735dd97bSGrant Likely block_dev_desc_t *get_dev(char* ifname, int dev)
92735dd97bSGrant Likely {
93735dd97bSGrant Likely 	return NULL;
94735dd97bSGrant Likely }
95735dd97bSGrant Likely #endif
96735dd97bSGrant Likely 
97735dd97bSGrant Likely #if ((CONFIG_COMMANDS & CFG_CMD_IDE)	|| \
98735dd97bSGrant Likely      (CONFIG_COMMANDS & CFG_CMD_SCSI)	|| \
99735dd97bSGrant Likely      (CONFIG_COMMANDS & CFG_CMD_USB)	|| \
100735dd97bSGrant Likely      defined(CONFIG_MMC) || \
101735dd97bSGrant Likely      defined(CONFIG_SYSTEMACE) )
102735dd97bSGrant Likely 
103affae2bfSwdenk /* ------------------------------------------------------------------------- */
104affae2bfSwdenk /*
105affae2bfSwdenk  * reports device info to the user
106affae2bfSwdenk  */
107affae2bfSwdenk void dev_print (block_dev_desc_t *dev_desc)
108affae2bfSwdenk {
10942dfe7a1Swdenk #ifdef CONFIG_LBA48
110c40b2956Swdenk 	uint64_t lba512; /* number of blocks if 512bytes block size */
111c40b2956Swdenk #else
112c40b2956Swdenk 	lbaint_t lba512;
113c40b2956Swdenk #endif
114affae2bfSwdenk 
115affae2bfSwdenk 	if (dev_desc->type==DEV_TYPE_UNKNOWN) {
116affae2bfSwdenk 		puts ("not available\n");
117affae2bfSwdenk 		return;
118affae2bfSwdenk 	}
119affae2bfSwdenk 	if (dev_desc->if_type==IF_TYPE_SCSI)  {
120affae2bfSwdenk 		printf ("(%d:%d) ", dev_desc->target,dev_desc->lun);
121affae2bfSwdenk 	}
122affae2bfSwdenk 	if (dev_desc->if_type==IF_TYPE_IDE) {
123affae2bfSwdenk 		printf ("Model: %s Firm: %s Ser#: %s\n",
124affae2bfSwdenk 			dev_desc->vendor,
125affae2bfSwdenk 			dev_desc->revision,
126affae2bfSwdenk 			dev_desc->product);
127affae2bfSwdenk 	} else {
128affae2bfSwdenk 		printf ("Vendor: %s Prod.: %s Rev: %s\n",
129affae2bfSwdenk 			dev_desc->vendor,
130affae2bfSwdenk 			dev_desc->product,
131affae2bfSwdenk 			dev_desc->revision);
132affae2bfSwdenk 	}
133affae2bfSwdenk 	puts ("            Type: ");
134affae2bfSwdenk 	if (dev_desc->removable)
135affae2bfSwdenk 		puts ("Removable ");
136affae2bfSwdenk 	switch (dev_desc->type & 0x1F) {
137affae2bfSwdenk 		case DEV_TYPE_HARDDISK: puts ("Hard Disk");
138affae2bfSwdenk 					break;
139affae2bfSwdenk 		case DEV_TYPE_CDROM: 	puts ("CD ROM");
140affae2bfSwdenk 					break;
141affae2bfSwdenk 		case DEV_TYPE_OPDISK: 	puts ("Optical Device");
142affae2bfSwdenk 					break;
143affae2bfSwdenk 		case DEV_TYPE_TAPE: 	puts ("Tape");
144affae2bfSwdenk 					break;
145affae2bfSwdenk 		default:		printf ("# %02X #", dev_desc->type & 0x1F);
146affae2bfSwdenk 					break;
147affae2bfSwdenk 	}
148affae2bfSwdenk 	puts ("\n");
149affae2bfSwdenk 	if ((dev_desc->lba * dev_desc->blksz)>0L) {
150affae2bfSwdenk 		ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
151c40b2956Swdenk 		lbaint_t lba;
1526e592385Swdenk 
153c40b2956Swdenk 		lba = dev_desc->lba;
154affae2bfSwdenk 
155c40b2956Swdenk 		lba512 = (lba * (dev_desc->blksz/512));
156affae2bfSwdenk 		mb = (10 * lba512) / 2048;	/* 2048 = (1024 * 1024) / 512 MB */
157affae2bfSwdenk 		/* round to 1 digit */
158affae2bfSwdenk 		mb_quot	= mb / 10;
159affae2bfSwdenk 		mb_rem	= mb - (10 * mb_quot);
160affae2bfSwdenk 
161affae2bfSwdenk 		gb = mb / 1024;
162affae2bfSwdenk 		gb_quot	= gb / 10;
163affae2bfSwdenk 		gb_rem	= gb - (10 * gb_quot);
16442dfe7a1Swdenk #ifdef CONFIG_LBA48
1656e592385Swdenk 		if (dev_desc->lba48)
166c40b2956Swdenk 			printf ("            Supports 48-bit addressing\n");
167c40b2956Swdenk #endif
16842dfe7a1Swdenk #if defined(CFG_64BIT_LBA) && defined(CFG_64BIT_VSPRINTF)
169c40b2956Swdenk 		printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%qd x %ld)\n",
170c40b2956Swdenk 			mb_quot, mb_rem,
171c40b2956Swdenk 			gb_quot, gb_rem,
172c40b2956Swdenk 			lba,
173c40b2956Swdenk 			dev_desc->blksz);
174c40b2956Swdenk #else
175affae2bfSwdenk 		printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
176affae2bfSwdenk 			mb_quot, mb_rem,
177affae2bfSwdenk 			gb_quot, gb_rem,
178c40b2956Swdenk 			(ulong)lba,
179affae2bfSwdenk 			dev_desc->blksz);
180c40b2956Swdenk #endif
181affae2bfSwdenk 	} else {
182affae2bfSwdenk 		puts ("            Capacity: not available\n");
183affae2bfSwdenk 	}
184affae2bfSwdenk }
185c935d3bdSwdenk #endif	/* CFG_CMD_IDE || CFG_CMD_SCSI || CFG_CMD_USB || CONFIG_MMC */
186affae2bfSwdenk 
187c935d3bdSwdenk #if ((CONFIG_COMMANDS & CFG_CMD_IDE)	|| \
188c935d3bdSwdenk      (CONFIG_COMMANDS & CFG_CMD_SCSI)	|| \
1893f85ce27Swdenk      (CONFIG_COMMANDS & CFG_CMD_USB)	|| \
1903f85ce27Swdenk      defined(CONFIG_SYSTEMACE)          )
191affae2bfSwdenk 
192affae2bfSwdenk #if defined(CONFIG_MAC_PARTITION) || \
193affae2bfSwdenk     defined(CONFIG_DOS_PARTITION) || \
194c7de829cSwdenk     defined(CONFIG_ISO_PARTITION) || \
195c7de829cSwdenk     defined(CONFIG_AMIGA_PARTITION)
196affae2bfSwdenk 
197affae2bfSwdenk void init_part (block_dev_desc_t * dev_desc)
198affae2bfSwdenk {
199affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION
200affae2bfSwdenk 	if (test_part_iso(dev_desc) == 0) {
201affae2bfSwdenk 		dev_desc->part_type = PART_TYPE_ISO;
202affae2bfSwdenk 		return;
203affae2bfSwdenk 	}
204affae2bfSwdenk #endif
205affae2bfSwdenk 
206affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION
207affae2bfSwdenk 	if (test_part_mac(dev_desc) == 0) {
208affae2bfSwdenk 		dev_desc->part_type = PART_TYPE_MAC;
209affae2bfSwdenk 		return;
210affae2bfSwdenk 	}
211affae2bfSwdenk #endif
212affae2bfSwdenk 
213affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION
214affae2bfSwdenk 	if (test_part_dos(dev_desc) == 0) {
215affae2bfSwdenk 		dev_desc->part_type = PART_TYPE_DOS;
216affae2bfSwdenk 		return;
217affae2bfSwdenk 	}
218affae2bfSwdenk #endif
219c7de829cSwdenk 
220c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION
221c7de829cSwdenk 	if (test_part_amiga(dev_desc) == 0) {
222c7de829cSwdenk 	    dev_desc->part_type = PART_TYPE_AMIGA;
223c7de829cSwdenk 	    return;
224c7de829cSwdenk 	}
225c7de829cSwdenk #endif
226affae2bfSwdenk }
227affae2bfSwdenk 
228affae2bfSwdenk 
229affae2bfSwdenk int get_partition_info (block_dev_desc_t *dev_desc, int part, disk_partition_t *info)
230affae2bfSwdenk {
231affae2bfSwdenk 		switch (dev_desc->part_type) {
232affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION
233affae2bfSwdenk 	case PART_TYPE_MAC:
234affae2bfSwdenk 		if (get_partition_info_mac(dev_desc,part,info) == 0) {
235affae2bfSwdenk 			PRINTF ("## Valid MAC partition found ##\n");
236affae2bfSwdenk 			return (0);
237affae2bfSwdenk 		}
238affae2bfSwdenk 		break;
239affae2bfSwdenk #endif
240affae2bfSwdenk 
241affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION
242affae2bfSwdenk 	case PART_TYPE_DOS:
243affae2bfSwdenk 		if (get_partition_info_dos(dev_desc,part,info) == 0) {
244affae2bfSwdenk 			PRINTF ("## Valid DOS partition found ##\n");
245affae2bfSwdenk 			return (0);
246affae2bfSwdenk 		}
247affae2bfSwdenk 		break;
248affae2bfSwdenk #endif
249affae2bfSwdenk 
250affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION
251affae2bfSwdenk 	case PART_TYPE_ISO:
252affae2bfSwdenk 		if (get_partition_info_iso(dev_desc,part,info) == 0) {
253affae2bfSwdenk 			PRINTF ("## Valid ISO boot partition found ##\n");
254affae2bfSwdenk 			return (0);
255affae2bfSwdenk 		}
256affae2bfSwdenk 		break;
257affae2bfSwdenk #endif
258c7de829cSwdenk 
259c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION
260c7de829cSwdenk 	case PART_TYPE_AMIGA:
261c7de829cSwdenk 	    if (get_partition_info_amiga(dev_desc, part, info) == 0)
262c7de829cSwdenk 	    {
263c7de829cSwdenk 		PRINTF ("## Valid Amiga partition found ##\n");
264c7de829cSwdenk 		return (0);
265c7de829cSwdenk 	    }
266c7de829cSwdenk 	    break;
267c7de829cSwdenk #endif
268affae2bfSwdenk 	default:
269affae2bfSwdenk 		break;
270affae2bfSwdenk 	}
271affae2bfSwdenk 	return (-1);
272affae2bfSwdenk }
273affae2bfSwdenk 
274affae2bfSwdenk static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
275affae2bfSwdenk {
276affae2bfSwdenk 	puts ("\nPartition Map for ");
277affae2bfSwdenk 	switch (dev_desc->if_type) {
278affae2bfSwdenk 		case IF_TYPE_IDE:  	puts ("IDE");
279affae2bfSwdenk 					break;
280affae2bfSwdenk 		case IF_TYPE_SCSI: 	puts ("SCSI");
281affae2bfSwdenk 					break;
282affae2bfSwdenk 		case IF_TYPE_ATAPI:	puts ("ATAPI");
283affae2bfSwdenk 					break;
284affae2bfSwdenk 		case IF_TYPE_USB:	puts ("USB");
285affae2bfSwdenk 					break;
286affae2bfSwdenk 		case IF_TYPE_DOC:	puts ("DOC");
287affae2bfSwdenk 					break;
288affae2bfSwdenk 		default: 		puts ("UNKNOWN");
289affae2bfSwdenk 					break;
290affae2bfSwdenk 	}
291affae2bfSwdenk 	printf (" device %d  --   Partition Type: %s\n\n",
292affae2bfSwdenk 			dev_desc->dev, type);
293affae2bfSwdenk }
294affae2bfSwdenk 
295affae2bfSwdenk void print_part (block_dev_desc_t * dev_desc)
296affae2bfSwdenk {
297affae2bfSwdenk 
298affae2bfSwdenk 		switch (dev_desc->part_type) {
299affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION
300affae2bfSwdenk 	case PART_TYPE_MAC:
301affae2bfSwdenk 		PRINTF ("## Testing for valid MAC partition ##\n");
302affae2bfSwdenk 		print_part_header ("MAC", dev_desc);
303affae2bfSwdenk 		print_part_mac (dev_desc);
304affae2bfSwdenk 		return;
305affae2bfSwdenk #endif
306affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION
307affae2bfSwdenk 	case PART_TYPE_DOS:
308affae2bfSwdenk 		PRINTF ("## Testing for valid DOS partition ##\n");
309affae2bfSwdenk 		print_part_header ("DOS", dev_desc);
310affae2bfSwdenk 		print_part_dos (dev_desc);
311affae2bfSwdenk 		return;
312affae2bfSwdenk #endif
313affae2bfSwdenk 
314affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION
315affae2bfSwdenk 	case PART_TYPE_ISO:
316affae2bfSwdenk 		PRINTF ("## Testing for valid ISO Boot partition ##\n");
317affae2bfSwdenk 		print_part_header ("ISO", dev_desc);
318affae2bfSwdenk 		print_part_iso (dev_desc);
319affae2bfSwdenk 		return;
320affae2bfSwdenk #endif
321c7de829cSwdenk 
322c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION
323c7de829cSwdenk 	case PART_TYPE_AMIGA:
324c7de829cSwdenk 	    PRINTF ("## Testing for a valid Amiga partition ##\n");
325c7de829cSwdenk 	    print_part_header ("AMIGA", dev_desc);
326c7de829cSwdenk 	    print_part_amiga (dev_desc);
327c7de829cSwdenk 	    return;
328c7de829cSwdenk #endif
329affae2bfSwdenk 	}
330affae2bfSwdenk 	puts ("## Unknown partition table\n");
331affae2bfSwdenk }
332affae2bfSwdenk 
333affae2bfSwdenk 
334affae2bfSwdenk #else	/* neither MAC nor DOS nor ISO partition configured */
335affae2bfSwdenk # error neither CONFIG_MAC_PARTITION nor CONFIG_DOS_PARTITION nor CONFIG_ISO_PARTITION configured!
336affae2bfSwdenk #endif
337affae2bfSwdenk 
338affae2bfSwdenk #endif	/* (CONFIG_COMMANDS & CFG_CMD_IDE) || CONFIG_COMMANDS & CFG_CMD_SCSI) */
339