68cba61c5a05a864b1bc03f44246c7f3cbe86b0e
[oweals/u-boot.git] / disk / part.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2001
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #include <common.h>
8 #include <command.h>
9 #include <env.h>
10 #include <errno.h>
11 #include <ide.h>
12 #include <malloc.h>
13 #include <part.h>
14 #include <ubifs_uboot.h>
15
16 #undef  PART_DEBUG
17
18 #ifdef  PART_DEBUG
19 #define PRINTF(fmt,args...)     printf (fmt ,##args)
20 #else
21 #define PRINTF(fmt,args...)
22 #endif
23
24 /* Check all partition types */
25 #define PART_TYPE_ALL           -1
26
27 static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc)
28 {
29         struct part_driver *drv =
30                 ll_entry_start(struct part_driver, part_driver);
31         const int n_ents = ll_entry_count(struct part_driver, part_driver);
32         struct part_driver *entry;
33
34         if (dev_desc->part_type == PART_TYPE_UNKNOWN) {
35                 for (entry = drv; entry != drv + n_ents; entry++) {
36                         int ret;
37
38                         ret = entry->test(dev_desc);
39                         if (!ret) {
40                                 dev_desc->part_type = entry->part_type;
41                                 return entry;
42                         }
43                 }
44         } else {
45                 for (entry = drv; entry != drv + n_ents; entry++) {
46                         if (dev_desc->part_type == entry->part_type)
47                                 return entry;
48                 }
49         }
50
51         /* Not found */
52         return NULL;
53 }
54
55 #ifdef CONFIG_HAVE_BLOCK_DEVICE
56 static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
57 {
58         struct blk_desc *dev_desc;
59         int ret;
60
61         dev_desc = blk_get_devnum_by_typename(ifname, dev);
62         if (!dev_desc) {
63                 debug("%s: No device for iface '%s', dev %d\n", __func__,
64                       ifname, dev);
65                 return NULL;
66         }
67         ret = blk_dselect_hwpart(dev_desc, hwpart);
68         if (ret) {
69                 debug("%s: Failed to select h/w partition: err-%d\n", __func__,
70                       ret);
71                 return NULL;
72         }
73
74         return dev_desc;
75 }
76
77 struct blk_desc *blk_get_dev(const char *ifname, int dev)
78 {
79         return get_dev_hwpart(ifname, dev, 0);
80 }
81 #else
82 struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
83 {
84         return NULL;
85 }
86
87 struct blk_desc *blk_get_dev(const char *ifname, int dev)
88 {
89         return NULL;
90 }
91 #endif
92
93 #ifdef CONFIG_HAVE_BLOCK_DEVICE
94
95 /* ------------------------------------------------------------------------- */
96 /*
97  * reports device info to the user
98  */
99
100 #ifdef CONFIG_LBA48
101 typedef uint64_t lba512_t;
102 #else
103 typedef lbaint_t lba512_t;
104 #endif
105
106 /*
107  * Overflowless variant of (block_count * mul_by / 2**right_shift)
108  * when 2**right_shift > mul_by
109  */
110 static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by,
111                               int right_shift)
112 {
113         lba512_t bc_quot, bc_rem;
114
115         /* x * m / d == x / d * m + (x % d) * m / d */
116         bc_quot = block_count >> right_shift;
117         bc_rem  = block_count - (bc_quot << right_shift);
118         return bc_quot * mul_by + ((bc_rem * mul_by) >> right_shift);
119 }
120
121 void dev_print (struct blk_desc *dev_desc)
122 {
123         lba512_t lba512; /* number of blocks if 512bytes block size */
124
125         if (dev_desc->type == DEV_TYPE_UNKNOWN) {
126                 puts ("not available\n");
127                 return;
128         }
129
130         switch (dev_desc->if_type) {
131         case IF_TYPE_SCSI:
132                 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
133                         dev_desc->target,dev_desc->lun,
134                         dev_desc->vendor,
135                         dev_desc->product,
136                         dev_desc->revision);
137                 break;
138         case IF_TYPE_ATAPI:
139         case IF_TYPE_IDE:
140         case IF_TYPE_SATA:
141                 printf ("Model: %s Firm: %s Ser#: %s\n",
142                         dev_desc->vendor,
143                         dev_desc->revision,
144                         dev_desc->product);
145                 break;
146         case IF_TYPE_SD:
147         case IF_TYPE_MMC:
148         case IF_TYPE_USB:
149         case IF_TYPE_NVME:
150                 printf ("Vendor: %s Rev: %s Prod: %s\n",
151                         dev_desc->vendor,
152                         dev_desc->revision,
153                         dev_desc->product);
154                 break;
155         case IF_TYPE_VIRTIO:
156                 printf("%s VirtIO Block Device\n", dev_desc->vendor);
157                 break;
158         case IF_TYPE_DOC:
159                 puts("device type DOC\n");
160                 return;
161         case IF_TYPE_UNKNOWN:
162                 puts("device type unknown\n");
163                 return;
164         default:
165                 printf("Unhandled device type: %i\n", dev_desc->if_type);
166                 return;
167         }
168         puts ("            Type: ");
169         if (dev_desc->removable)
170                 puts ("Removable ");
171         switch (dev_desc->type & 0x1F) {
172         case DEV_TYPE_HARDDISK:
173                 puts ("Hard Disk");
174                 break;
175         case DEV_TYPE_CDROM:
176                 puts ("CD ROM");
177                 break;
178         case DEV_TYPE_OPDISK:
179                 puts ("Optical Device");
180                 break;
181         case DEV_TYPE_TAPE:
182                 puts ("Tape");
183                 break;
184         default:
185                 printf ("# %02X #", dev_desc->type & 0x1F);
186                 break;
187         }
188         puts ("\n");
189         if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
190                 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
191                 lbaint_t lba;
192
193                 lba = dev_desc->lba;
194
195                 lba512 = (lba * (dev_desc->blksz/512));
196                 /* round to 1 digit */
197                 /* 2048 = (1024 * 1024) / 512 MB */
198                 mb = lba512_muldiv(lba512, 10, 11);
199
200                 mb_quot = mb / 10;
201                 mb_rem  = mb - (10 * mb_quot);
202
203                 gb = mb / 1024;
204                 gb_quot = gb / 10;
205                 gb_rem  = gb - (10 * gb_quot);
206 #ifdef CONFIG_LBA48
207                 if (dev_desc->lba48)
208                         printf ("            Supports 48-bit addressing\n");
209 #endif
210 #if defined(CONFIG_SYS_64BIT_LBA)
211                 printf ("            Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
212                         mb_quot, mb_rem,
213                         gb_quot, gb_rem,
214                         lba,
215                         dev_desc->blksz);
216 #else
217                 printf ("            Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n",
218                         mb_quot, mb_rem,
219                         gb_quot, gb_rem,
220                         (ulong)lba,
221                         dev_desc->blksz);
222 #endif
223         } else {
224                 puts ("            Capacity: not available\n");
225         }
226 }
227 #endif
228
229 #ifdef CONFIG_HAVE_BLOCK_DEVICE
230
231 void part_init(struct blk_desc *dev_desc)
232 {
233         struct part_driver *drv =
234                 ll_entry_start(struct part_driver, part_driver);
235         const int n_ents = ll_entry_count(struct part_driver, part_driver);
236         struct part_driver *entry;
237
238         blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
239
240         dev_desc->part_type = PART_TYPE_UNKNOWN;
241         for (entry = drv; entry != drv + n_ents; entry++) {
242                 int ret;
243
244                 ret = entry->test(dev_desc);
245                 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
246                 if (!ret) {
247                         dev_desc->part_type = entry->part_type;
248                         break;
249                 }
250         }
251 }
252
253 static void print_part_header(const char *type, struct blk_desc *dev_desc)
254 {
255 #if CONFIG_IS_ENABLED(MAC_PARTITION) || \
256         CONFIG_IS_ENABLED(DOS_PARTITION) || \
257         CONFIG_IS_ENABLED(ISO_PARTITION) || \
258         CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
259         CONFIG_IS_ENABLED(EFI_PARTITION)
260         puts ("\nPartition Map for ");
261         switch (dev_desc->if_type) {
262         case IF_TYPE_IDE:
263                 puts ("IDE");
264                 break;
265         case IF_TYPE_SATA:
266                 puts ("SATA");
267                 break;
268         case IF_TYPE_SCSI:
269                 puts ("SCSI");
270                 break;
271         case IF_TYPE_ATAPI:
272                 puts ("ATAPI");
273                 break;
274         case IF_TYPE_USB:
275                 puts ("USB");
276                 break;
277         case IF_TYPE_DOC:
278                 puts ("DOC");
279                 break;
280         case IF_TYPE_MMC:
281                 puts ("MMC");
282                 break;
283         case IF_TYPE_HOST:
284                 puts ("HOST");
285                 break;
286         case IF_TYPE_NVME:
287                 puts ("NVMe");
288                 break;
289         case IF_TYPE_VIRTIO:
290                 puts("VirtIO");
291                 break;
292         default:
293                 puts ("UNKNOWN");
294                 break;
295         }
296         printf (" device %d  --   Partition Type: %s\n\n",
297                         dev_desc->devnum, type);
298 #endif /* any CONFIG_..._PARTITION */
299 }
300
301 void part_print(struct blk_desc *dev_desc)
302 {
303         struct part_driver *drv;
304
305         drv = part_driver_lookup_type(dev_desc);
306         if (!drv) {
307                 printf("## Unknown partition table type %x\n",
308                        dev_desc->part_type);
309                 return;
310         }
311
312         PRINTF("## Testing for valid %s partition ##\n", drv->name);
313         print_part_header(drv->name, dev_desc);
314         if (drv->print)
315                 drv->print(dev_desc);
316 }
317
318 #endif /* CONFIG_HAVE_BLOCK_DEVICE */
319
320 int part_get_info(struct blk_desc *dev_desc, int part,
321                        struct disk_partition *info)
322 {
323 #ifdef CONFIG_HAVE_BLOCK_DEVICE
324         struct part_driver *drv;
325
326 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
327         /* The common case is no UUID support */
328         info->uuid[0] = 0;
329 #endif
330 #ifdef CONFIG_PARTITION_TYPE_GUID
331         info->type_guid[0] = 0;
332 #endif
333
334         drv = part_driver_lookup_type(dev_desc);
335         if (!drv) {
336                 debug("## Unknown partition table type %x\n",
337                       dev_desc->part_type);
338                 return -EPROTONOSUPPORT;
339         }
340         if (!drv->get_info) {
341                 PRINTF("## Driver %s does not have the get_info() method\n",
342                        drv->name);
343                 return -ENOSYS;
344         }
345         if (drv->get_info(dev_desc, part, info) == 0) {
346                 PRINTF("## Valid %s partition found ##\n", drv->name);
347                 return 0;
348         }
349 #endif /* CONFIG_HAVE_BLOCK_DEVICE */
350
351         return -1;
352 }
353
354 int part_get_info_whole_disk(struct blk_desc *dev_desc,
355                              struct disk_partition *info)
356 {
357         info->start = 0;
358         info->size = dev_desc->lba;
359         info->blksz = dev_desc->blksz;
360         info->bootable = 0;
361         strcpy((char *)info->type, BOOT_PART_TYPE);
362         strcpy((char *)info->name, "Whole Disk");
363 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
364         info->uuid[0] = 0;
365 #endif
366 #ifdef CONFIG_PARTITION_TYPE_GUID
367         info->type_guid[0] = 0;
368 #endif
369
370         return 0;
371 }
372
373 int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
374                           struct blk_desc **dev_desc)
375 {
376         char *ep;
377         char *dup_str = NULL;
378         const char *dev_str, *hwpart_str;
379         int dev, hwpart;
380
381         hwpart_str = strchr(dev_hwpart_str, '.');
382         if (hwpart_str) {
383                 dup_str = strdup(dev_hwpart_str);
384                 dup_str[hwpart_str - dev_hwpart_str] = 0;
385                 dev_str = dup_str;
386                 hwpart_str++;
387         } else {
388                 dev_str = dev_hwpart_str;
389                 hwpart = 0;
390         }
391
392         dev = simple_strtoul(dev_str, &ep, 16);
393         if (*ep) {
394                 printf("** Bad device specification %s %s **\n",
395                        ifname, dev_str);
396                 dev = -EINVAL;
397                 goto cleanup;
398         }
399
400         if (hwpart_str) {
401                 hwpart = simple_strtoul(hwpart_str, &ep, 16);
402                 if (*ep) {
403                         printf("** Bad HW partition specification %s %s **\n",
404                             ifname, hwpart_str);
405                         dev = -EINVAL;
406                         goto cleanup;
407                 }
408         }
409
410         *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
411         if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
412                 debug("** Bad device %s %s **\n", ifname, dev_hwpart_str);
413                 dev = -ENOENT;
414                 goto cleanup;
415         }
416
417 #ifdef CONFIG_HAVE_BLOCK_DEVICE
418         /*
419          * Updates the partition table for the specified hw partition.
420          * Always should be done, otherwise hw partition 0 will return stale
421          * data after displaying a non-zero hw partition.
422          */
423         part_init(*dev_desc);
424 #endif
425
426 cleanup:
427         free(dup_str);
428         return dev;
429 }
430
431 #define PART_UNSPECIFIED -2
432 #define PART_AUTO -1
433 int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
434                              struct blk_desc **dev_desc,
435                              struct disk_partition *info, int allow_whole_dev)
436 {
437         int ret = -1;
438         const char *part_str;
439         char *dup_str = NULL;
440         const char *dev_str;
441         int dev;
442         char *ep;
443         int p;
444         int part;
445         struct disk_partition tmpinfo;
446
447 #ifdef CONFIG_SANDBOX
448         /*
449          * Special-case a pseudo block device "hostfs", to allow access to the
450          * host's own filesystem.
451          */
452         if (0 == strcmp(ifname, "hostfs")) {
453                 *dev_desc = NULL;
454                 info->start = 0;
455                 info->size = 0;
456                 info->blksz = 0;
457                 info->bootable = 0;
458                 strcpy((char *)info->type, BOOT_PART_TYPE);
459                 strcpy((char *)info->name, "Sandbox host");
460 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
461                 info->uuid[0] = 0;
462 #endif
463 #ifdef CONFIG_PARTITION_TYPE_GUID
464                 info->type_guid[0] = 0;
465 #endif
466
467                 return 0;
468         }
469 #endif
470
471 #ifdef CONFIG_CMD_UBIFS
472         /*
473          * Special-case ubi, ubi goes through a mtd, rather than through
474          * a regular block device.
475          */
476         if (0 == strcmp(ifname, "ubi")) {
477                 if (!ubifs_is_mounted()) {
478                         printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
479                         return -1;
480                 }
481
482                 *dev_desc = NULL;
483                 memset(info, 0, sizeof(*info));
484                 strcpy((char *)info->type, BOOT_PART_TYPE);
485                 strcpy((char *)info->name, "UBI");
486 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
487                 info->uuid[0] = 0;
488 #endif
489                 return 0;
490         }
491 #endif
492
493         /* If no dev_part_str, use bootdevice environment variable */
494         if (!dev_part_str || !strlen(dev_part_str) ||
495             !strcmp(dev_part_str, "-"))
496                 dev_part_str = env_get("bootdevice");
497
498         /* If still no dev_part_str, it's an error */
499         if (!dev_part_str) {
500                 printf("** No device specified **\n");
501                 goto cleanup;
502         }
503
504         /* Separate device and partition ID specification */
505         part_str = strchr(dev_part_str, ':');
506         if (part_str) {
507                 dup_str = strdup(dev_part_str);
508                 dup_str[part_str - dev_part_str] = 0;
509                 dev_str = dup_str;
510                 part_str++;
511         } else {
512                 dev_str = dev_part_str;
513         }
514
515         /* Look up the device */
516         dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
517         if (dev < 0)
518                 goto cleanup;
519
520         /* Convert partition ID string to number */
521         if (!part_str || !*part_str) {
522                 part = PART_UNSPECIFIED;
523         } else if (!strcmp(part_str, "auto")) {
524                 part = PART_AUTO;
525         } else {
526                 /* Something specified -> use exactly that */
527                 part = (int)simple_strtoul(part_str, &ep, 16);
528                 /*
529                  * Less than whole string converted,
530                  * or request for whole device, but caller requires partition.
531                  */
532                 if (*ep || (part == 0 && !allow_whole_dev)) {
533                         printf("** Bad partition specification %s %s **\n",
534                             ifname, dev_part_str);
535                         goto cleanup;
536                 }
537         }
538
539         /*
540          * No partition table on device,
541          * or user requested partition 0 (entire device).
542          */
543         if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
544             (part == 0)) {
545                 if (!(*dev_desc)->lba) {
546                         printf("** Bad device size - %s %s **\n", ifname,
547                                dev_str);
548                         goto cleanup;
549                 }
550
551                 /*
552                  * If user specified a partition ID other than 0,
553                  * or the calling command only accepts partitions,
554                  * it's an error.
555                  */
556                 if ((part > 0) || (!allow_whole_dev)) {
557                         printf("** No partition table - %s %s **\n", ifname,
558                                dev_str);
559                         goto cleanup;
560                 }
561
562                 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
563
564                 part_get_info_whole_disk(*dev_desc, info);
565
566                 ret = 0;
567                 goto cleanup;
568         }
569
570         /*
571          * Now there's known to be a partition table,
572          * not specifying a partition means to pick partition 1.
573          */
574         if (part == PART_UNSPECIFIED)
575                 part = 1;
576
577         /*
578          * If user didn't specify a partition number, or did specify something
579          * other than "auto", use that partition number directly.
580          */
581         if (part != PART_AUTO) {
582                 ret = part_get_info(*dev_desc, part, info);
583                 if (ret) {
584                         printf("** Invalid partition %d **\n", part);
585                         goto cleanup;
586                 }
587         } else {
588                 /*
589                  * Find the first bootable partition.
590                  * If none are bootable, fall back to the first valid partition.
591                  */
592                 part = 0;
593                 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
594                         ret = part_get_info(*dev_desc, p, info);
595                         if (ret)
596                                 continue;
597
598                         /*
599                          * First valid partition, or new better partition?
600                          * If so, save partition ID.
601                          */
602                         if (!part || info->bootable)
603                                 part = p;
604
605                         /* Best possible partition? Stop searching. */
606                         if (info->bootable)
607                                 break;
608
609                         /*
610                          * We now need to search further for best possible.
611                          * If we what we just queried was the best so far,
612                          * save the info since we over-write it next loop.
613                          */
614                         if (part == p)
615                                 tmpinfo = *info;
616                 }
617                 /* If we found any acceptable partition */
618                 if (part) {
619                         /*
620                          * If we searched all possible partition IDs,
621                          * return the first valid partition we found.
622                          */
623                         if (p == MAX_SEARCH_PARTITIONS + 1)
624                                 *info = tmpinfo;
625                 } else {
626                         printf("** No valid partitions found **\n");
627                         ret = -1;
628                         goto cleanup;
629                 }
630         }
631         if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
632                 printf("** Invalid partition type \"%.32s\""
633                         " (expect \"" BOOT_PART_TYPE "\")\n",
634                         info->type);
635                 ret  = -1;
636                 goto cleanup;
637         }
638
639         (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
640
641         ret = part;
642         goto cleanup;
643
644 cleanup:
645         free(dup_str);
646         return ret;
647 }
648
649 int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
650                                struct disk_partition *info, int part_type)
651 {
652         struct part_driver *part_drv;
653         int ret;
654         int i;
655
656         part_drv = part_driver_lookup_type(dev_desc);
657         if (!part_drv)
658                 return -1;
659         for (i = 1; i < part_drv->max_entries; i++) {
660                 ret = part_drv->get_info(dev_desc, i, info);
661                 if (ret != 0) {
662                         /* no more entries in table */
663                         break;
664                 }
665                 if (strcmp(name, (const char *)info->name) == 0) {
666                         /* matched */
667                         return i;
668                 }
669         }
670
671         return -1;
672 }
673
674 int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
675                           struct disk_partition *info)
676 {
677         return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL);
678 }
679
680 /**
681  * Get partition info from device number and partition name.
682  *
683  * Parse a device number and partition name string in the form of
684  * "device_num#partition_name", for example "0#misc". If the partition
685  * is found, sets dev_desc and part_info accordingly with the information
686  * of the partition with the given partition_name.
687  *
688  * @param[in] dev_iface Device interface
689  * @param[in] dev_part_str Input string argument, like "0#misc"
690  * @param[out] dev_desc Place to store the device description pointer
691  * @param[out] part_info Place to store the partition information
692  * @return 0 on success, or a negative on error
693  */
694 static int part_get_info_by_dev_and_name(const char *dev_iface,
695                                          const char *dev_part_str,
696                                          struct blk_desc **dev_desc,
697                                          struct disk_partition *part_info)
698 {
699         char *ep;
700         const char *part_str;
701         int dev_num;
702
703         part_str = strchr(dev_part_str, '#');
704         if (!part_str || part_str == dev_part_str)
705                 return -EINVAL;
706
707         dev_num = simple_strtoul(dev_part_str, &ep, 16);
708         if (ep != part_str) {
709                 /* Not all the first part before the # was parsed. */
710                 return -EINVAL;
711         }
712         part_str++;
713
714         *dev_desc = blk_get_dev(dev_iface, dev_num);
715         if (!*dev_desc) {
716                 printf("Could not find %s %d\n", dev_iface, dev_num);
717                 return -EINVAL;
718         }
719         if (part_get_info_by_name(*dev_desc, part_str, part_info) < 0) {
720                 printf("Could not find \"%s\" partition\n", part_str);
721                 return -EINVAL;
722         }
723         return 0;
724 }
725
726 int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
727                                          const char *dev_part_str,
728                                          struct blk_desc **dev_desc,
729                                          struct disk_partition *part_info)
730 {
731         /* Split the part_name if passed as "$dev_num#part_name". */
732         if (!part_get_info_by_dev_and_name(dev_iface, dev_part_str,
733                                            dev_desc, part_info))
734                 return 0;
735         /*
736          * Couldn't lookup by name, try looking up the partition description
737          * directly.
738          */
739         if (blk_get_device_part_str(dev_iface, dev_part_str,
740                                     dev_desc, part_info, 1) < 0) {
741                 printf("Couldn't find partition %s %s\n",
742                        dev_iface, dev_part_str);
743                 return -EINVAL;
744         }
745         return 0;
746 }
747
748 void part_set_generic_name(const struct blk_desc *dev_desc,
749         int part_num, char *name)
750 {
751         char *devtype;
752
753         switch (dev_desc->if_type) {
754         case IF_TYPE_IDE:
755         case IF_TYPE_SATA:
756         case IF_TYPE_ATAPI:
757                 devtype = "hd";
758                 break;
759         case IF_TYPE_SCSI:
760                 devtype = "sd";
761                 break;
762         case IF_TYPE_USB:
763                 devtype = "usbd";
764                 break;
765         case IF_TYPE_DOC:
766                 devtype = "docd";
767                 break;
768         case IF_TYPE_MMC:
769         case IF_TYPE_SD:
770                 devtype = "mmcsd";
771                 break;
772         default:
773                 devtype = "xx";
774                 break;
775         }
776
777         sprintf(name, "%s%c%d", devtype, 'a' + dev_desc->devnum, part_num);
778 }