xref: /openbmc/u-boot/disk/part.c (revision a89c33db)
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_MG_DISK) || \
39      defined(CONFIG_CMD_SATA) || \
40      defined(CONFIG_CMD_SCSI) || \
41      defined(CONFIG_CMD_USB) || \
42      defined(CONFIG_MMC) || \
43      defined(CONFIG_SYSTEMACE) )
44 
45 struct block_drvr {
46 	char *name;
47 	block_dev_desc_t* (*get_dev)(int dev);
48 };
49 
50 static const struct block_drvr block_drvr[] = {
51 #if defined(CONFIG_CMD_IDE)
52 	{ .name = "ide", .get_dev = ide_get_dev, },
53 #endif
54 #if defined(CONFIG_CMD_SATA)
55 	{.name = "sata", .get_dev = sata_get_dev, },
56 #endif
57 #if defined(CONFIG_CMD_SCSI)
58 	{ .name = "scsi", .get_dev = scsi_get_dev, },
59 #endif
60 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
61 	{ .name = "usb", .get_dev = usb_stor_get_dev, },
62 #endif
63 #if defined(CONFIG_MMC)
64 	{ .name = "mmc", .get_dev = mmc_get_dev, },
65 #endif
66 #if defined(CONFIG_SYSTEMACE)
67 	{ .name = "ace", .get_dev = systemace_get_dev, },
68 #endif
69 #if defined(CONFIG_CMD_MG_DISK)
70 	{ .name = "mgd", .get_dev = mg_disk_get_dev, },
71 #endif
72 	{ },
73 };
74 
75 DECLARE_GLOBAL_DATA_PTR;
76 
77 block_dev_desc_t *get_dev(char* ifname, int dev)
78 {
79 	const struct block_drvr *drvr = block_drvr;
80 	block_dev_desc_t* (*reloc_get_dev)(int dev);
81 
82 	while (drvr->name) {
83 		reloc_get_dev = drvr->get_dev + gd->reloc_off;
84 		if (strncmp(ifname, drvr->name, strlen(drvr->name)) == 0)
85 			return reloc_get_dev(dev);
86 		drvr++;
87 	}
88 	return NULL;
89 }
90 #else
91 block_dev_desc_t *get_dev(char* ifname, int dev)
92 {
93 	return NULL;
94 }
95 #endif
96 
97 #if (defined(CONFIG_CMD_IDE) || \
98      defined(CONFIG_CMD_MG_DISK) || \
99      defined(CONFIG_CMD_SATA) || \
100      defined(CONFIG_CMD_SCSI) || \
101      defined(CONFIG_CMD_USB) || \
102      defined(CONFIG_MMC) || \
103      defined(CONFIG_SYSTEMACE) )
104 
105 /* ------------------------------------------------------------------------- */
106 /*
107  * reports device info to the user
108  */
109 void dev_print (block_dev_desc_t *dev_desc)
110 {
111 #ifdef CONFIG_LBA48
112 	uint64_t lba512; /* number of blocks if 512bytes block size */
113 #else
114 	lbaint_t lba512;
115 #endif
116 
117 	if (dev_desc->type == DEV_TYPE_UNKNOWN) {
118 		puts ("not available\n");
119 		return;
120 	}
121 
122 	switch (dev_desc->if_type) {
123 	case IF_TYPE_SCSI:
124 		printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
125 			dev_desc->target,dev_desc->lun,
126 			dev_desc->vendor,
127 			dev_desc->product,
128 			dev_desc->revision);
129 		break;
130 	case IF_TYPE_ATAPI:
131 	case IF_TYPE_IDE:
132 	case IF_TYPE_SATA:
133 		printf ("Model: %s Firm: %s Ser#: %s\n",
134 			dev_desc->vendor,
135 			dev_desc->revision,
136 			dev_desc->product);
137 		break;
138 	case IF_TYPE_SD:
139 	case IF_TYPE_MMC:
140 	case IF_TYPE_USB:
141 		printf ("Vendor: %s Rev: %s Prod: %s\n",
142 			dev_desc->vendor,
143 			dev_desc->revision,
144 			dev_desc->product);
145 		break;
146 	case IF_TYPE_DOC:
147 		puts("device type DOC\n");
148 		return;
149 	case IF_TYPE_UNKNOWN:
150 		puts("device type unknown\n");
151 		return;
152 	default:
153 		printf("Unhandled device type: %i\n", dev_desc->if_type);
154 		return;
155 	}
156 	puts ("            Type: ");
157 	if (dev_desc->removable)
158 		puts ("Removable ");
159 	switch (dev_desc->type & 0x1F) {
160 	case DEV_TYPE_HARDDISK:
161 		puts ("Hard Disk");
162 		break;
163 	case DEV_TYPE_CDROM:
164 		puts ("CD ROM");
165 		break;
166 	case DEV_TYPE_OPDISK:
167 		puts ("Optical Device");
168 		break;
169 	case DEV_TYPE_TAPE:
170 		puts ("Tape");
171 		break;
172 	default:
173 		printf ("# %02X #", dev_desc->type & 0x1F);
174 		break;
175 	}
176 	puts ("\n");
177 	if ((dev_desc->lba * dev_desc->blksz)>0L) {
178 		ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
179 		lbaint_t lba;
180 
181 		lba = dev_desc->lba;
182 
183 		lba512 = (lba * (dev_desc->blksz/512));
184 		mb = (10 * lba512) / 2048;	/* 2048 = (1024 * 1024) / 512 MB */
185 		/* round to 1 digit */
186 		mb_quot	= mb / 10;
187 		mb_rem	= mb - (10 * mb_quot);
188 
189 		gb = mb / 1024;
190 		gb_quot	= gb / 10;
191 		gb_rem	= gb - (10 * gb_quot);
192 #ifdef CONFIG_LBA48
193 		if (dev_desc->lba48)
194 			printf ("            Supports 48-bit addressing\n");
195 #endif
196 #if defined(CONFIG_SYS_64BIT_LBA) && defined(CONFIG_SYS_64BIT_VSPRINTF)
197 		printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
198 			mb_quot, mb_rem,
199 			gb_quot, gb_rem,
200 			lba,
201 			dev_desc->blksz);
202 #else
203 		printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
204 			mb_quot, mb_rem,
205 			gb_quot, gb_rem,
206 			(ulong)lba,
207 			dev_desc->blksz);
208 #endif
209 	} else {
210 		puts ("            Capacity: not available\n");
211 	}
212 }
213 #endif
214 
215 #if (defined(CONFIG_CMD_IDE) || \
216      defined(CONFIG_CMD_MG_DISK) || \
217      defined(CONFIG_CMD_SATA) || \
218      defined(CONFIG_CMD_SCSI) || \
219      defined(CONFIG_CMD_USB) || \
220      defined(CONFIG_MMC)		|| \
221      defined(CONFIG_SYSTEMACE) )
222 
223 #if defined(CONFIG_MAC_PARTITION) || \
224     defined(CONFIG_DOS_PARTITION) || \
225     defined(CONFIG_ISO_PARTITION) || \
226     defined(CONFIG_AMIGA_PARTITION) || \
227     defined(CONFIG_EFI_PARTITION)
228 
229 void init_part (block_dev_desc_t * dev_desc)
230 {
231 #ifdef CONFIG_ISO_PARTITION
232 	if (test_part_iso(dev_desc) == 0) {
233 		dev_desc->part_type = PART_TYPE_ISO;
234 		return;
235 	}
236 #endif
237 
238 #ifdef CONFIG_MAC_PARTITION
239 	if (test_part_mac(dev_desc) == 0) {
240 		dev_desc->part_type = PART_TYPE_MAC;
241 		return;
242 	}
243 #endif
244 
245 /* must be placed before DOS partition detection */
246 #ifdef CONFIG_EFI_PARTITION
247 	if (test_part_efi(dev_desc) == 0) {
248 		dev_desc->part_type = PART_TYPE_EFI;
249 		return;
250 	}
251 #endif
252 
253 #ifdef CONFIG_DOS_PARTITION
254 	if (test_part_dos(dev_desc) == 0) {
255 		dev_desc->part_type = PART_TYPE_DOS;
256 		return;
257 	}
258 #endif
259 
260 #ifdef CONFIG_AMIGA_PARTITION
261 	if (test_part_amiga(dev_desc) == 0) {
262 	    dev_desc->part_type = PART_TYPE_AMIGA;
263 	    return;
264 	}
265 #endif
266 }
267 
268 
269 int get_partition_info (block_dev_desc_t *dev_desc, int part
270 					, disk_partition_t *info)
271 {
272 	switch (dev_desc->part_type) {
273 #ifdef CONFIG_MAC_PARTITION
274 	case PART_TYPE_MAC:
275 		if (get_partition_info_mac(dev_desc,part,info) == 0) {
276 			PRINTF ("## Valid MAC partition found ##\n");
277 			return (0);
278 		}
279 		break;
280 #endif
281 
282 #ifdef CONFIG_DOS_PARTITION
283 	case PART_TYPE_DOS:
284 		if (get_partition_info_dos(dev_desc,part,info) == 0) {
285 			PRINTF ("## Valid DOS partition found ##\n");
286 			return (0);
287 		}
288 		break;
289 #endif
290 
291 #ifdef CONFIG_ISO_PARTITION
292 	case PART_TYPE_ISO:
293 		if (get_partition_info_iso(dev_desc,part,info) == 0) {
294 			PRINTF ("## Valid ISO boot partition found ##\n");
295 			return (0);
296 		}
297 		break;
298 #endif
299 
300 #ifdef CONFIG_AMIGA_PARTITION
301 	case PART_TYPE_AMIGA:
302 	    if (get_partition_info_amiga(dev_desc, part, info) == 0)
303 	    {
304 		PRINTF ("## Valid Amiga partition found ##\n");
305 		return (0);
306 	    }
307 	    break;
308 #endif
309 
310 #ifdef CONFIG_EFI_PARTITION
311 	case PART_TYPE_EFI:
312 		if (get_partition_info_efi(dev_desc,part,info) == 0) {
313 			PRINTF ("## Valid EFI partition found ##\n");
314 			return (0);
315 		}
316 		break;
317 #endif
318 	default:
319 		break;
320 	}
321 	return (-1);
322 }
323 
324 static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
325 {
326 	puts ("\nPartition Map for ");
327 	switch (dev_desc->if_type) {
328 	case IF_TYPE_IDE:
329 		puts ("IDE");
330 		break;
331 	case IF_TYPE_SATA:
332 		puts ("SATA");
333 		break;
334 	case IF_TYPE_SCSI:
335 		puts ("SCSI");
336 		break;
337 	case IF_TYPE_ATAPI:
338 		puts ("ATAPI");
339 		break;
340 	case IF_TYPE_USB:
341 		puts ("USB");
342 		break;
343 	case IF_TYPE_DOC:
344 		puts ("DOC");
345 		break;
346 	default:
347 		puts ("UNKNOWN");
348 		break;
349 	}
350 	printf (" device %d  --   Partition Type: %s\n\n",
351 			dev_desc->dev, type);
352 }
353 
354 void print_part (block_dev_desc_t * dev_desc)
355 {
356 
357 		switch (dev_desc->part_type) {
358 #ifdef CONFIG_MAC_PARTITION
359 	case PART_TYPE_MAC:
360 		PRINTF ("## Testing for valid MAC partition ##\n");
361 		print_part_header ("MAC", dev_desc);
362 		print_part_mac (dev_desc);
363 		return;
364 #endif
365 #ifdef CONFIG_DOS_PARTITION
366 	case PART_TYPE_DOS:
367 		PRINTF ("## Testing for valid DOS partition ##\n");
368 		print_part_header ("DOS", dev_desc);
369 		print_part_dos (dev_desc);
370 		return;
371 #endif
372 
373 #ifdef CONFIG_ISO_PARTITION
374 	case PART_TYPE_ISO:
375 		PRINTF ("## Testing for valid ISO Boot partition ##\n");
376 		print_part_header ("ISO", dev_desc);
377 		print_part_iso (dev_desc);
378 		return;
379 #endif
380 
381 #ifdef CONFIG_AMIGA_PARTITION
382 	case PART_TYPE_AMIGA:
383 	    PRINTF ("## Testing for a valid Amiga partition ##\n");
384 	    print_part_header ("AMIGA", dev_desc);
385 	    print_part_amiga (dev_desc);
386 	    return;
387 #endif
388 
389 #ifdef CONFIG_EFI_PARTITION
390 	case PART_TYPE_EFI:
391 		PRINTF ("## Testing for valid EFI partition ##\n");
392 		print_part_header ("EFI", dev_desc);
393 		print_part_efi (dev_desc);
394 		return;
395 #endif
396 	}
397 	puts ("## Unknown partition table\n");
398 }
399 
400 
401 #else	/* neither MAC nor DOS nor ISO nor AMIGA nor EFI partition configured */
402 # error neither CONFIG_MAC_PARTITION nor CONFIG_DOS_PARTITION
403 # error nor CONFIG_ISO_PARTITION nor CONFIG_AMIGA_PARTITION
404 # error nor CONFIG_EFI_PARTITION configured!
405 #endif
406 
407 #endif
408