1 /* grub-probe.c - probe device information for a given path */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2006,2007,2008,2009,2010,2013 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 #include <grub/types.h>
22 #include <grub/emu/misc.h>
23 #include <grub/util/misc.h>
24 #include <grub/device.h>
25 #include <grub/disk.h>
26 #include <grub/file.h>
28 #include <grub/partition.h>
29 #include <grub/msdos_partition.h>
30 #include <grub/gpt_partition.h>
31 #include <grub/emu/hostdisk.h>
32 #include <grub/emu/getroot.h>
33 #include <grub/term.h>
35 #include <grub/diskfilter.h>
36 #include <grub/i18n.h>
37 #include <grub/emu/misc.h>
38 #include <grub/util/ofpath.h>
39 #include <grub/crypto.h>
40 #include <grub/cryptodisk.h>
44 /* Since OF path names can have "," characters in them, and GRUB
45 internally uses "," to indicate partitions (unlike OF which uses
46 ":" for this purpose) we escape such commas. */
48 escape_of_path (const char *orig_path
)
50 char *new_path
, *d
, c
;
53 if (!strchr (orig_path
, ','))
54 return (char *) xstrdup (orig_path
);
56 new_path
= xmalloc (strlen (orig_path
) * 2 + 1);
60 while ((c
= *p
++) != '\0')
72 grub_util_guess_bios_drive (const char *orig_path
)
76 canon
= canonicalize_file_name (orig_path
);
79 ptr
= strrchr (orig_path
, '/');
84 if ((ptr
[0] == 's' || ptr
[0] == 'h') && ptr
[1] == 'd')
86 int num
= ptr
[2] - 'a';
88 return xasprintf ("hd%d", num
);
90 if (ptr
[0] == 'f' && ptr
[1] == 'd')
92 int num
= atoi (ptr
+ 2);
94 return xasprintf ("fd%d", num
);
101 grub_util_guess_efi_drive (const char *orig_path
)
105 canon
= canonicalize_file_name (orig_path
);
108 ptr
= strrchr (orig_path
, '/');
113 if ((ptr
[0] == 's' || ptr
[0] == 'h') && ptr
[1] == 'd')
115 int num
= ptr
[2] - 'a';
117 return xasprintf ("hd%d", num
);
119 if (ptr
[0] == 'f' && ptr
[1] == 'd')
121 int num
= atoi (ptr
+ 2);
123 return xasprintf ("fd%d", num
);
130 grub_util_guess_baremetal_drive (const char *orig_path
)
134 canon
= canonicalize_file_name (orig_path
);
137 ptr
= strrchr (orig_path
, '/');
142 if (ptr
[0] == 'h' && ptr
[1] == 'd')
144 int num
= ptr
[2] - 'a';
146 return xasprintf ("ata%d", num
);
148 if (ptr
[0] == 's' && ptr
[1] == 'd')
150 int num
= ptr
[2] - 'a';
152 return xasprintf ("ahci%d", num
);
159 grub_util_fprint_full_disk_name (FILE *f
,
160 const char *drive
, grub_device_t dev
)
162 char *dname
= escape_of_path (drive
);
163 if (dev
->disk
->partition
)
165 char *pname
= grub_partition_get_name (dev
->disk
->partition
);
166 fprintf (f
, "%s,%s", dname
, pname
);
170 fprintf (f
, "%s", dname
);