blob: 76f393900a50b452b9f7afa59a43f3e896fa90cc [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
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>
Grant Likelyffc2dd72007-02-20 09:04:34 +010027#include <part.h>
wdenkaffae2b2002-08-17 09:36:01 +000028
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
Jon Loeligerf7f54a82007-07-09 17:22:37 -050037#if (defined(CONFIG_CMD_IDE) || \
Dave Liu76596ba2008-03-26 22:49:44 +080038 defined(CONFIG_CMD_SATA) || \
Jon Loeligerf7f54a82007-07-09 17:22:37 -050039 defined(CONFIG_CMD_SCSI) || \
40 defined(CONFIG_CMD_USB) || \
wdenkacd05362005-02-24 23:23:29 +000041 defined(CONFIG_MMC) || \
42 defined(CONFIG_SYSTEMACE) )
wdenkaffae2b2002-08-17 09:36:01 +000043
Grant Likelyffc2dd72007-02-20 09:04:34 +010044struct block_drvr {
45 char *name;
46 block_dev_desc_t* (*get_dev)(int dev);
47};
48
49static const struct block_drvr block_drvr[] = {
Jon Loeligerf7f54a82007-07-09 17:22:37 -050050#if defined(CONFIG_CMD_IDE)
Grant Likelyffc2dd72007-02-20 09:04:34 +010051 { .name = "ide", .get_dev = ide_get_dev, },
52#endif
Dave Liu76596ba2008-03-26 22:49:44 +080053#if defined(CONFIG_CMD_SATA)
54 {.name = "sata", .get_dev = sata_get_dev, },
55#endif
Jon Loeligerf7f54a82007-07-09 17:22:37 -050056#if defined(CONFIG_CMD_SCSI)
Grant Likelyffc2dd72007-02-20 09:04:34 +010057 { .name = "scsi", .get_dev = scsi_get_dev, },
58#endif
Jon Loeligerf7f54a82007-07-09 17:22:37 -050059#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
Grant Likelyffc2dd72007-02-20 09:04:34 +010060 { .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
Stefan Roese61ddee42007-02-20 13:21:57 +010071DECLARE_GLOBAL_DATA_PTR;
Stefan Roese61ddee42007-02-20 13:21:57 +010072
Grant Likelyffc2dd72007-02-20 09:04:34 +010073block_dev_desc_t *get_dev(char* ifname, int dev)
74{
75 const struct block_drvr *drvr = block_drvr;
Stefan Roese5b084e12007-02-22 07:43:34 +010076 block_dev_desc_t* (*reloc_get_dev)(int dev);
Heiko Schocher9b987232010-09-17 13:10:36 +020077 char *name;
Grant Likelyffc2dd72007-02-20 09:04:34 +010078
Tim Kientzle47e444c2012-03-27 11:43:25 +020079 if (!ifname)
80 return NULL;
81
Heiko Schocher9b987232010-09-17 13:10:36 +020082 name = drvr->name;
Wolfgang Denkd0813e52010-10-28 20:00:11 +020083#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher9b987232010-09-17 13:10:36 +020084 name += gd->reloc_off;
85#endif
Lei Wen2a010672011-02-15 16:56:40 +080086 while (drvr->name) {
Heiko Schocher9b987232010-09-17 13:10:36 +020087 name = drvr->name;
Peter Tyser9057cbf2009-09-21 11:20:36 -050088 reloc_get_dev = drvr->get_dev;
Wolfgang Denkd0813e52010-10-28 20:00:11 +020089#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher9b987232010-09-17 13:10:36 +020090 name += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -050091 reloc_get_dev += gd->reloc_off;
92#endif
Heiko Schocher9b987232010-09-17 13:10:36 +020093 if (strncmp(ifname, name, strlen(name)) == 0)
Stefan Roese61ddee42007-02-20 13:21:57 +010094 return reloc_get_dev(dev);
Grant Likelyffc2dd72007-02-20 09:04:34 +010095 drvr++;
96 }
97 return NULL;
98}
99#else
100block_dev_desc_t *get_dev(char* ifname, int dev)
101{
102 return NULL;
103}
104#endif
105
Jon Loeligerf7f54a82007-07-09 17:22:37 -0500106#if (defined(CONFIG_CMD_IDE) || \
Dave Liu76596ba2008-03-26 22:49:44 +0800107 defined(CONFIG_CMD_SATA) || \
Jon Loeligerf7f54a82007-07-09 17:22:37 -0500108 defined(CONFIG_CMD_SCSI) || \
109 defined(CONFIG_CMD_USB) || \
Grant Likelyffc2dd72007-02-20 09:04:34 +0100110 defined(CONFIG_MMC) || \
111 defined(CONFIG_SYSTEMACE) )
112
wdenkaffae2b2002-08-17 09:36:01 +0000113/* ------------------------------------------------------------------------- */
114/*
115 * reports device info to the user
116 */
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300117
wdenkc35ba4e2004-03-14 22:25:36 +0000118#ifdef CONFIG_LBA48
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300119typedef uint64_t lba512_t;
wdenkf602aa02004-03-13 23:29:43 +0000120#else
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300121typedef lbaint_t lba512_t;
wdenkf602aa02004-03-13 23:29:43 +0000122#endif
wdenkaffae2b2002-08-17 09:36:01 +0000123
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300124/*
125 * Overflowless variant of (block_count * mul_by / div_by)
126 * when div_by > mul_by
127 */
128static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by)
129{
130 lba512_t bc_quot, bc_rem;
131
132 /* x * m / d == x / d * m + (x % d) * m / d */
133 bc_quot = block_count / div_by;
134 bc_rem = block_count - div_by * bc_quot;
135 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
136}
137
138void dev_print (block_dev_desc_t *dev_desc)
139{
140 lba512_t lba512; /* number of blocks if 512bytes block size */
141
Wolfgang Denk8561e712009-05-15 09:27:58 +0200142 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
143 puts ("not available\n");
144 return;
145 }
146
Tor Krill03ff3302008-05-29 11:10:30 +0200147 switch (dev_desc->if_type) {
Detlev Zundelbec6b322008-05-05 16:11:21 +0200148 case IF_TYPE_SCSI:
149 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
150 dev_desc->target,dev_desc->lun,
wdenkaffae2b2002-08-17 09:36:01 +0000151 dev_desc->vendor,
Detlev Zundelbec6b322008-05-05 16:11:21 +0200152 dev_desc->product,
153 dev_desc->revision);
154 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200155 case IF_TYPE_ATAPI:
Detlev Zundelbec6b322008-05-05 16:11:21 +0200156 case IF_TYPE_IDE:
157 case IF_TYPE_SATA:
Dave Liu76596ba2008-03-26 22:49:44 +0800158 printf ("Model: %s Firm: %s Ser#: %s\n",
159 dev_desc->vendor,
160 dev_desc->revision,
161 dev_desc->product);
Detlev Zundelbec6b322008-05-05 16:11:21 +0200162 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200163 case IF_TYPE_SD:
164 case IF_TYPE_MMC:
Nícolas Carneiro Lebedencoef3edd42008-09-04 15:35:46 -0300165 case IF_TYPE_USB:
166 printf ("Vendor: %s Rev: %s Prod: %s\n",
167 dev_desc->vendor,
168 dev_desc->revision,
169 dev_desc->product);
170 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200171 case IF_TYPE_DOC:
172 puts("device type DOC\n");
173 return;
Tor Krill03ff3302008-05-29 11:10:30 +0200174 case IF_TYPE_UNKNOWN:
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200175 puts("device type unknown\n");
176 return;
Detlev Zundelbec6b322008-05-05 16:11:21 +0200177 default:
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200178 printf("Unhandled device type: %i\n", dev_desc->if_type);
Detlev Zundelbec6b322008-05-05 16:11:21 +0200179 return;
wdenkaffae2b2002-08-17 09:36:01 +0000180 }
181 puts (" Type: ");
182 if (dev_desc->removable)
183 puts ("Removable ");
184 switch (dev_desc->type & 0x1F) {
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200185 case DEV_TYPE_HARDDISK:
186 puts ("Hard Disk");
187 break;
188 case DEV_TYPE_CDROM:
189 puts ("CD ROM");
190 break;
191 case DEV_TYPE_OPDISK:
192 puts ("Optical Device");
193 break;
194 case DEV_TYPE_TAPE:
195 puts ("Tape");
196 break;
197 default:
198 printf ("# %02X #", dev_desc->type & 0x1F);
199 break;
wdenkaffae2b2002-08-17 09:36:01 +0000200 }
201 puts ("\n");
202 if ((dev_desc->lba * dev_desc->blksz)>0L) {
203 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
wdenkf602aa02004-03-13 23:29:43 +0000204 lbaint_t lba;
wdenk05939202004-04-18 17:39:38 +0000205
206 lba = dev_desc->lba;
wdenkaffae2b2002-08-17 09:36:01 +0000207
wdenkf602aa02004-03-13 23:29:43 +0000208 lba512 = (lba * (dev_desc->blksz/512));
wdenkaffae2b2002-08-17 09:36:01 +0000209 /* round to 1 digit */
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300210 mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
211
wdenkaffae2b2002-08-17 09:36:01 +0000212 mb_quot = mb / 10;
213 mb_rem = mb - (10 * mb_quot);
214
215 gb = mb / 1024;
216 gb_quot = gb / 10;
217 gb_rem = gb - (10 * gb_quot);
wdenkc35ba4e2004-03-14 22:25:36 +0000218#ifdef CONFIG_LBA48
wdenk05939202004-04-18 17:39:38 +0000219 if (dev_desc->lba48)
wdenkf602aa02004-03-13 23:29:43 +0000220 printf (" Supports 48-bit addressing\n");
221#endif
Heiko Schocher0f602e12009-12-03 11:21:21 +0100222#if defined(CONFIG_SYS_64BIT_LBA)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500223 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
wdenkf602aa02004-03-13 23:29:43 +0000224 mb_quot, mb_rem,
225 gb_quot, gb_rem,
226 lba,
227 dev_desc->blksz);
228#else
wdenkaffae2b2002-08-17 09:36:01 +0000229 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
230 mb_quot, mb_rem,
231 gb_quot, gb_rem,
wdenkf602aa02004-03-13 23:29:43 +0000232 (ulong)lba,
wdenkaffae2b2002-08-17 09:36:01 +0000233 dev_desc->blksz);
wdenkf602aa02004-03-13 23:29:43 +0000234#endif
wdenkaffae2b2002-08-17 09:36:01 +0000235 } else {
236 puts (" Capacity: not available\n");
237 }
238}
Jon Loeliger1670a5b2007-07-10 11:19:50 -0500239#endif
wdenkaffae2b2002-08-17 09:36:01 +0000240
Jon Loeligerf7f54a82007-07-09 17:22:37 -0500241#if (defined(CONFIG_CMD_IDE) || \
Dave Liu76596ba2008-03-26 22:49:44 +0800242 defined(CONFIG_CMD_SATA) || \
Jon Loeligerf7f54a82007-07-09 17:22:37 -0500243 defined(CONFIG_CMD_SCSI) || \
244 defined(CONFIG_CMD_USB) || \
Haavard Skinnemoen01d354e2006-12-17 18:53:56 +0100245 defined(CONFIG_MMC) || \
unsik Kimf0b1bdd2009-02-25 11:31:24 +0900246 defined(CONFIG_SYSTEMACE) )
wdenkaffae2b2002-08-17 09:36:01 +0000247
wdenkaffae2b2002-08-17 09:36:01 +0000248#if defined(CONFIG_MAC_PARTITION) || \
249 defined(CONFIG_DOS_PARTITION) || \
wdenk452cfd62002-11-19 11:04:11 +0000250 defined(CONFIG_ISO_PARTITION) || \
richardretanubune6745592008-09-26 11:13:22 -0400251 defined(CONFIG_AMIGA_PARTITION) || \
252 defined(CONFIG_EFI_PARTITION)
wdenkaffae2b2002-08-17 09:36:01 +0000253
254void init_part (block_dev_desc_t * dev_desc)
255{
256#ifdef CONFIG_ISO_PARTITION
257 if (test_part_iso(dev_desc) == 0) {
258 dev_desc->part_type = PART_TYPE_ISO;
259 return;
260 }
261#endif
262
263#ifdef CONFIG_MAC_PARTITION
264 if (test_part_mac(dev_desc) == 0) {
265 dev_desc->part_type = PART_TYPE_MAC;
266 return;
267 }
268#endif
269
richardretanubune6745592008-09-26 11:13:22 -0400270/* must be placed before DOS partition detection */
271#ifdef CONFIG_EFI_PARTITION
272 if (test_part_efi(dev_desc) == 0) {
273 dev_desc->part_type = PART_TYPE_EFI;
274 return;
275 }
276#endif
277
wdenkaffae2b2002-08-17 09:36:01 +0000278#ifdef CONFIG_DOS_PARTITION
279 if (test_part_dos(dev_desc) == 0) {
280 dev_desc->part_type = PART_TYPE_DOS;
281 return;
282 }
283#endif
wdenk452cfd62002-11-19 11:04:11 +0000284
285#ifdef CONFIG_AMIGA_PARTITION
286 if (test_part_amiga(dev_desc) == 0) {
287 dev_desc->part_type = PART_TYPE_AMIGA;
288 return;
289 }
290#endif
wdenkaffae2b2002-08-17 09:36:01 +0000291}
292
293
Peter Pearsefb312c82007-05-18 16:47:03 +0100294int get_partition_info (block_dev_desc_t *dev_desc, int part
295 , disk_partition_t *info)
wdenkaffae2b2002-08-17 09:36:01 +0000296{
Heiko Schocher633e03a2007-06-22 19:11:54 +0200297 switch (dev_desc->part_type) {
wdenkaffae2b2002-08-17 09:36:01 +0000298#ifdef CONFIG_MAC_PARTITION
299 case PART_TYPE_MAC:
300 if (get_partition_info_mac(dev_desc,part,info) == 0) {
301 PRINTF ("## Valid MAC partition found ##\n");
302 return (0);
303 }
304 break;
305#endif
306
307#ifdef CONFIG_DOS_PARTITION
308 case PART_TYPE_DOS:
309 if (get_partition_info_dos(dev_desc,part,info) == 0) {
310 PRINTF ("## Valid DOS partition found ##\n");
311 return (0);
312 }
313 break;
314#endif
315
316#ifdef CONFIG_ISO_PARTITION
317 case PART_TYPE_ISO:
318 if (get_partition_info_iso(dev_desc,part,info) == 0) {
319 PRINTF ("## Valid ISO boot partition found ##\n");
320 return (0);
321 }
322 break;
323#endif
wdenk452cfd62002-11-19 11:04:11 +0000324
325#ifdef CONFIG_AMIGA_PARTITION
326 case PART_TYPE_AMIGA:
327 if (get_partition_info_amiga(dev_desc, part, info) == 0)
328 {
329 PRINTF ("## Valid Amiga partition found ##\n");
330 return (0);
331 }
332 break;
333#endif
richardretanubune6745592008-09-26 11:13:22 -0400334
335#ifdef CONFIG_EFI_PARTITION
336 case PART_TYPE_EFI:
337 if (get_partition_info_efi(dev_desc,part,info) == 0) {
338 PRINTF ("## Valid EFI partition found ##\n");
339 return (0);
340 }
341 break;
342#endif
wdenkaffae2b2002-08-17 09:36:01 +0000343 default:
344 break;
345 }
346 return (-1);
347}
348
349static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
350{
351 puts ("\nPartition Map for ");
352 switch (dev_desc->if_type) {
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200353 case IF_TYPE_IDE:
354 puts ("IDE");
355 break;
356 case IF_TYPE_SATA:
357 puts ("SATA");
358 break;
359 case IF_TYPE_SCSI:
360 puts ("SCSI");
361 break;
362 case IF_TYPE_ATAPI:
363 puts ("ATAPI");
364 break;
365 case IF_TYPE_USB:
366 puts ("USB");
367 break;
368 case IF_TYPE_DOC:
369 puts ("DOC");
370 break;
Lei Wen61dba932010-09-13 22:07:28 +0800371 case IF_TYPE_MMC:
372 puts ("MMC");
373 break;
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200374 default:
375 puts ("UNKNOWN");
376 break;
wdenkaffae2b2002-08-17 09:36:01 +0000377 }
378 printf (" device %d -- Partition Type: %s\n\n",
379 dev_desc->dev, type);
380}
381
382void print_part (block_dev_desc_t * dev_desc)
383{
384
385 switch (dev_desc->part_type) {
386#ifdef CONFIG_MAC_PARTITION
387 case PART_TYPE_MAC:
388 PRINTF ("## Testing for valid MAC partition ##\n");
389 print_part_header ("MAC", dev_desc);
390 print_part_mac (dev_desc);
391 return;
392#endif
393#ifdef CONFIG_DOS_PARTITION
394 case PART_TYPE_DOS:
395 PRINTF ("## Testing for valid DOS partition ##\n");
396 print_part_header ("DOS", dev_desc);
397 print_part_dos (dev_desc);
398 return;
399#endif
400
401#ifdef CONFIG_ISO_PARTITION
402 case PART_TYPE_ISO:
403 PRINTF ("## Testing for valid ISO Boot partition ##\n");
404 print_part_header ("ISO", dev_desc);
405 print_part_iso (dev_desc);
406 return;
407#endif
wdenk452cfd62002-11-19 11:04:11 +0000408
409#ifdef CONFIG_AMIGA_PARTITION
410 case PART_TYPE_AMIGA:
411 PRINTF ("## Testing for a valid Amiga partition ##\n");
412 print_part_header ("AMIGA", dev_desc);
413 print_part_amiga (dev_desc);
414 return;
415#endif
richardretanubune6745592008-09-26 11:13:22 -0400416
417#ifdef CONFIG_EFI_PARTITION
418 case PART_TYPE_EFI:
419 PRINTF ("## Testing for valid EFI partition ##\n");
420 print_part_header ("EFI", dev_desc);
421 print_part_efi (dev_desc);
422 return;
423#endif
wdenkaffae2b2002-08-17 09:36:01 +0000424 }
425 puts ("## Unknown partition table\n");
426}
427
428
richardretanubune6745592008-09-26 11:13:22 -0400429#else /* neither MAC nor DOS nor ISO nor AMIGA nor EFI partition configured */
Wolfgang Denka9d7acb2007-06-06 16:26:56 +0200430# error neither CONFIG_MAC_PARTITION nor CONFIG_DOS_PARTITION
richardretanubune6745592008-09-26 11:13:22 -0400431# error nor CONFIG_ISO_PARTITION nor CONFIG_AMIGA_PARTITION
432# error nor CONFIG_EFI_PARTITION configured!
wdenkaffae2b2002-08-17 09:36:01 +0000433#endif
434
Jon Loeligerf7f54a82007-07-09 17:22:37 -0500435#endif