Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / probe.c
blobc389f5dcf2295810f8108973c3b05909d114dda4
1 /* grub-probe.c - probe device information for a given path */
2 /*
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/>.
20 #include <config.h>
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>
27 #include <grub/fs.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>
34 #include <grub/env.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>
42 #include <string.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. */
47 static char *
48 escape_of_path (const char *orig_path)
50 char *new_path, *d, c;
51 const char *p;
53 if (!strchr (orig_path, ','))
54 return (char *) xstrdup (orig_path);
56 new_path = xmalloc (strlen (orig_path) * 2 + 1);
58 p = orig_path;
59 d = new_path;
60 while ((c = *p++) != '\0')
62 if (c == ',')
63 *d++ = '\\';
64 *d++ = c;
66 *d = 0;
68 return new_path;
71 char *
72 grub_util_guess_bios_drive (const char *orig_path)
74 char *canon;
75 char *ptr;
76 canon = canonicalize_file_name (orig_path);
77 if (!canon)
78 return NULL;
79 ptr = strrchr (orig_path, '/');
80 if (ptr)
81 ptr++;
82 else
83 ptr = canon;
84 if ((ptr[0] == 's' || ptr[0] == 'h') && ptr[1] == 'd')
86 int num = ptr[2] - 'a';
87 free (canon);
88 return xasprintf ("hd%d", num);
90 if (ptr[0] == 'f' && ptr[1] == 'd')
92 int num = atoi (ptr + 2);
93 free (canon);
94 return xasprintf ("fd%d", num);
96 free (canon);
97 return NULL;
100 char *
101 grub_util_guess_efi_drive (const char *orig_path)
103 char *canon;
104 char *ptr;
105 canon = canonicalize_file_name (orig_path);
106 if (!canon)
107 return NULL;
108 ptr = strrchr (orig_path, '/');
109 if (ptr)
110 ptr++;
111 else
112 ptr = canon;
113 if ((ptr[0] == 's' || ptr[0] == 'h') && ptr[1] == 'd')
115 int num = ptr[2] - 'a';
116 free (canon);
117 return xasprintf ("hd%d", num);
119 if (ptr[0] == 'f' && ptr[1] == 'd')
121 int num = atoi (ptr + 2);
122 free (canon);
123 return xasprintf ("fd%d", num);
125 free (canon);
126 return NULL;
129 char *
130 grub_util_guess_baremetal_drive (const char *orig_path)
132 char *canon;
133 char *ptr;
134 canon = canonicalize_file_name (orig_path);
135 if (!canon)
136 return NULL;
137 ptr = strrchr (orig_path, '/');
138 if (ptr)
139 ptr++;
140 else
141 ptr = canon;
142 if (ptr[0] == 'h' && ptr[1] == 'd')
144 int num = ptr[2] - 'a';
145 free (canon);
146 return xasprintf ("ata%d", num);
148 if (ptr[0] == 's' && ptr[1] == 'd')
150 int num = ptr[2] - 'a';
151 free (canon);
152 return xasprintf ("ahci%d", num);
154 free (canon);
155 return NULL;
158 void
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);
167 free (pname);
169 else
170 fprintf (f, "%s", dname);
171 free (dname);