xref: /openbmc/u-boot/disk/part.c (revision 24113a44)
1 /*
2  * (C) Copyright 2001
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 
24 #include <common.h>
25 #include <command.h>
26 #include <ide.h>
27 #include <part.h>
28 
29 #undef	PART_DEBUG
30 
31 #ifdef	PART_DEBUG
32 #define	PRINTF(fmt,args...)	printf (fmt ,##args)
33 #else
34 #define PRINTF(fmt,args...)
35 #endif
36 
37 #if (defined(CONFIG_CMD_IDE) || \
38      defined(CONFIG_CMD_SATA) || \
39      defined(CONFIG_CMD_SCSI) || \
40      defined(CONFIG_CMD_USB) || \
41      defined(CONFIG_MMC) || \
42      defined(CONFIG_SYSTEMACE) )
43 
44 struct block_drvr {
45 	char *name;
46 	block_dev_desc_t* (*get_dev)(int dev);
47 };
48 
49 static const struct block_drvr block_drvr[] = {
50 #if defined(CONFIG_CMD_IDE)
51 	{ .name = "ide", .get_dev = ide_get_dev, },
52 #endif
53 #if defined(CONFIG_CMD_SATA)
54 	{.name = "sata", .get_dev = sata_get_dev, },
55 #endif
56 #if defined(CONFIG_CMD_SCSI)
57 	{ .name = "scsi", .get_dev = scsi_get_dev, },
58 #endif
59 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
60 	{ .name = "usb", .get_dev = usb_stor_get_dev, },
61 #endif
62 #if defined(CONFIG_MMC)
63 	{ .name = "mmc", .get_dev = mmc_get_dev, },
64 #endif
65 #if defined(CONFIG_SYSTEMACE)
66 	{ .name = "ace", .get_dev = systemace_get_dev, },
67 #endif
68 	{ },
69 };
70 
71 DECLARE_GLOBAL_DATA_PTR;
72 
73 block_dev_desc_t *get_dev(char* ifname, int dev)
74 {
75 	const struct block_drvr *drvr = block_drvr;
76 	block_dev_desc_t* (*reloc_get_dev)(int dev);
77 
78 	while (drvr->name) {
79 		reloc_get_dev = drvr->get_dev + gd->reloc_off;
80 		if (strncmp(ifname, drvr->name, strlen(drvr->name)) == 0)
81 			return reloc_get_dev(dev);
82 		drvr++;
83 	}
84 	return NULL;
85 }
86 #else
87 block_dev_desc_t *get_dev(char* ifname, int dev)
88 {
89 	return NULL;
90 }
91 #endif
92 
93 #if (defined(CONFIG_CMD_IDE) || \
94      defined(CONFIG_CMD_SATA) || \
95      defined(CONFIG_CMD_SCSI) || \
96      defined(CONFIG_CMD_USB) || \
97      defined(CONFIG_MMC) || \
98      defined(CONFIG_SYSTEMACE) )
99 
100 /* ------------------------------------------------------------------------- */
101 /*
102  * reports device info to the user
103  */
104 void dev_print (block_dev_desc_t *dev_desc)
105 {
106 #ifdef CONFIG_LBA48
107 	uint64_t lba512; /* number of blocks if 512bytes block size */
108 #else
109 	lbaint_t lba512;
110 #endif
111 
112 	switch (dev_desc->if_type) {
113 	case IF_TYPE_SCSI:
114 		printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
115 			dev_desc->target,dev_desc->lun,
116 			dev_desc->vendor,
117 			dev_desc->product,
118 			dev_desc->revision);
119 		break;
120 	case IF_TYPE_ATAPI:
121 	case IF_TYPE_IDE:
122 	case IF_TYPE_SATA:
123 		printf ("Model: %s Firm: %s Ser#: %s\n",
124 			dev_desc->vendor,
125 			dev_desc->revision,
126 			dev_desc->product);
127 		break;
128 	case IF_TYPE_SD:
129 	case IF_TYPE_MMC:
130 	case IF_TYPE_USB:
131 		printf ("Vendor: %s Rev: %s Prod: %s\n",
132 			dev_desc->vendor,
133 			dev_desc->revision,
134 			dev_desc->product);
135 		break;
136 	case IF_TYPE_DOC:
137 		puts("device type DOC\n");
138 		return;
139 	case IF_TYPE_UNKNOWN:
140 		puts("device type unknown\n");
141 		return;
142 	default:
143 		printf("Unhandled device type: %i\n", dev_desc->if_type);
144 		return;
145 	}
146 	puts ("            Type: ");
147 	if (dev_desc->removable)
148 		puts ("Removable ");
149 	switch (dev_desc->type & 0x1F) {
150 	case DEV_TYPE_HARDDISK:
151 		puts ("Hard Disk");
152 		break;
153 	case DEV_TYPE_CDROM:
154 		puts ("CD ROM");
155 		break;
156 	case DEV_TYPE_OPDISK:
157 		puts ("Optical Device");
158 		break;
159 	case DEV_TYPE_TAPE:
160 		puts ("Tape");
161 		break;
162 	default:
163 		printf ("# %02X #", dev_desc->type & 0x1F);
164 		break;
165 	}
166 	puts ("\n");
167 	if ((dev_desc->lba * dev_desc->blksz)>0L) {
168 		ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
169 		lbaint_t lba;
170 
171 		lba = dev_desc->lba;
172 
173 		lba512 = (lba * (dev_desc->blksz/512));
174 		mb = (10 * lba512) / 2048;	/* 2048 = (1024 * 1024) / 512 MB */
175 		/* round to 1 digit */
176 		mb_quot	= mb / 10;
177 		mb_rem	= mb - (10 * mb_quot);
178 
179 		gb = mb / 1024;
180 		gb_quot	= gb / 10;
181 		gb_rem	= gb - (10 * gb_quot);
182 #ifdef CONFIG_LBA48
183 		if (dev_desc->lba48)
184 			printf ("            Supports 48-bit addressing\n");
185 #endif
186 #if defined(CONFIG_SYS_64BIT_LBA) && defined(CONFIG_SYS_64BIT_VSPRINTF)
187 		printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%qd x %ld)\n",
188 			mb_quot, mb_rem,
189 			gb_quot, gb_rem,
190 			lba,
191 			dev_desc->blksz);
192 #else
193 		printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
194 			mb_quot, mb_rem,
195 			gb_quot, gb_rem,
196 			(ulong)lba,
197 			dev_desc->blksz);
198 #endif
199 	} else {
200 		puts ("            Capacity: not available\n");
201 	}
202 }
203 #endif
204 
205 #if (defined(CONFIG_CMD_IDE) || \
206      defined(CONFIG_CMD_SATA) || \
207      defined(CONFIG_CMD_SCSI) || \
208      defined(CONFIG_CMD_USB) || \
209      defined(CONFIG_MMC)		|| \
210      defined(CONFIG_SYSTEMACE)          )
211 
212 #if defined(CONFIG_MAC_PARTITION) || \
213     defined(CONFIG_DOS_PARTITION) || \
214     defined(CONFIG_ISO_PARTITION) || \
215     defined(CONFIG_AMIGA_PARTITION) || \
216     defined(CONFIG_EFI_PARTITION)
217 
218 void init_part (block_dev_desc_t * dev_desc)
219 {
220 #ifdef CONFIG_ISO_PARTITION
221 	if (test_part_iso(dev_desc) == 0) {
222 		dev_desc->part_type = PART_TYPE_ISO;
223 		return;
224 	}
225 #endif
226 
227 #ifdef CONFIG_MAC_PARTITION
228 	if (test_part_mac(dev_desc) == 0) {
229 		dev_desc->part_type = PART_TYPE_MAC;
230 		return;
231 	}
232 #endif
233 
234 /* must be placed before DOS partition detection */
235 #ifdef CONFIG_EFI_PARTITION
236 	if (test_part_efi(dev_desc) == 0) {
237 		dev_desc->part_type = PART_TYPE_EFI;
238 		return;
239 	}
240 #endif
241 
242 #ifdef CONFIG_DOS_PARTITION
243 	if (test_part_dos(dev_desc) == 0) {
244 		dev_desc->part_type = PART_TYPE_DOS;
245 		return;
246 	}
247 #endif
248 
249 #ifdef CONFIG_AMIGA_PARTITION
250 	if (test_part_amiga(dev_desc) == 0) {
251 	    dev_desc->part_type = PART_TYPE_AMIGA;
252 	    return;
253 	}
254 #endif
255 }
256 
257 
258 int get_partition_info (block_dev_desc_t *dev_desc, int part
259 					, disk_partition_t *info)
260 {
261 	switch (dev_desc->part_type) {
262 #ifdef CONFIG_MAC_PARTITION
263 	case PART_TYPE_MAC:
264 		if (get_partition_info_mac(dev_desc,part,info) == 0) {
265 			PRINTF ("## Valid MAC partition found ##\n");
266 			return (0);
267 		}
268 		break;
269 #endif
270 
271 #ifdef CONFIG_DOS_PARTITION
272 	case PART_TYPE_DOS:
273 		if (get_partition_info_dos(dev_desc,part,info) == 0) {
274 			PRINTF ("## Valid DOS partition found ##\n");
275 			return (0);
276 		}
277 		break;
278 #endif
279 
280 #ifdef CONFIG_ISO_PARTITION
281 	case PART_TYPE_ISO:
282 		if (get_partition_info_iso(dev_desc,part,info) == 0) {
283 			PRINTF ("## Valid ISO boot partition found ##\n");
284 			return (0);
285 		}
286 		break;
287 #endif
288 
289 #ifdef CONFIG_AMIGA_PARTITION
290 	case PART_TYPE_AMIGA:
291 	    if (get_partition_info_amiga(dev_desc, part, info) == 0)
292 	    {
293 		PRINTF ("## Valid Amiga partition found ##\n");
294 		return (0);
295 	    }
296 	    break;
297 #endif
298 
299 #ifdef CONFIG_EFI_PARTITION
300 	case PART_TYPE_EFI:
301 		if (get_partition_info_efi(dev_desc,part,info) == 0) {
302 			PRINTF ("## Valid EFI partition found ##\n");
303 			return (0);
304 		}
305 		break;
306 #endif
307 	default:
308 		break;
309 	}
310 	return (-1);
311 }
312 
313 static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
314 {
315 	puts ("\nPartition Map for ");
316 	switch (dev_desc->if_type) {
317 	case IF_TYPE_IDE:
318 		puts ("IDE");
319 		break;
320 	case IF_TYPE_SATA:
321 		puts ("SATA");
322 		break;
323 	case IF_TYPE_SCSI:
324 		puts ("SCSI");
325 		break;
326 	case IF_TYPE_ATAPI:
327 		puts ("ATAPI");
328 		break;
329 	case IF_TYPE_USB:
330 		puts ("USB");
331 		break;
332 	case IF_TYPE_DOC:
333 		puts ("DOC");
334 		break;
335 	default:
336 		puts ("UNKNOWN");
337 		break;
338 	}
339 	printf (" device %d  --   Partition Type: %s\n\n",
340 			dev_desc->dev, type);
341 }
342 
343 void print_part (block_dev_desc_t * dev_desc)
344 {
345 
346 		switch (dev_desc->part_type) {
347 #ifdef CONFIG_MAC_PARTITION
348 	case PART_TYPE_MAC:
349 		PRINTF ("## Testing for valid MAC partition ##\n");
350 		print_part_header ("MAC", dev_desc);
351 		print_part_mac (dev_desc);
352 		return;
353 #endif
354 #ifdef CONFIG_DOS_PARTITION
355 	case PART_TYPE_DOS:
356 		PRINTF ("## Testing for valid DOS partition ##\n");
357 		print_part_header ("DOS", dev_desc);
358 		print_part_dos (dev_desc);
359 		return;
360 #endif
361 
362 #ifdef CONFIG_ISO_PARTITION
363 	case PART_TYPE_ISO:
364 		PRINTF ("## Testing for valid ISO Boot partition ##\n");
365 		print_part_header ("ISO", dev_desc);
366 		print_part_iso (dev_desc);
367 		return;
368 #endif
369 
370 #ifdef CONFIG_AMIGA_PARTITION
371 	case PART_TYPE_AMIGA:
372 	    PRINTF ("## Testing for a valid Amiga partition ##\n");
373 	    print_part_header ("AMIGA", dev_desc);
374 	    print_part_amiga (dev_desc);
375 	    return;
376 #endif
377 
378 #ifdef CONFIG_EFI_PARTITION
379 	case PART_TYPE_EFI:
380 		PRINTF ("## Testing for valid EFI partition ##\n");
381 		print_part_header ("EFI", dev_desc);
382 		print_part_efi (dev_desc);
383 		return;
384 #endif
385 	}
386 	puts ("## Unknown partition table\n");
387 }
388 
389 
390 #else	/* neither MAC nor DOS nor ISO nor AMIGA nor EFI partition configured */
391 # error neither CONFIG_MAC_PARTITION nor CONFIG_DOS_PARTITION
392 # error nor CONFIG_ISO_PARTITION nor CONFIG_AMIGA_PARTITION
393 # error nor CONFIG_EFI_PARTITION configured!
394 #endif
395 
396 #endif
397