Debian Codegraph @

grub-core/commands/i386/pc/drivemap.c

raw
1 /* drivemap.c - command to manage the BIOS drive mappings. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 #include <grub/extcmd.h>
21 #include <grub/dl.h>
22 #include <grub/mm.h>
23 #include <grub/misc.h>
24 #include <grub/disk.h>
25 #include <grub/loader.h>
26 #include <grub/env.h>
27 #include <grub/machine/biosnum.h>
28 #include <grub/i18n.h>
29 #include <grub/memory.h>
30 #include <grub/machine/memory.h>
31
32 GRUB_MOD_LICENSE ("GPLv3+");
33
34 /* Remember to update enum opt_idxs accordingly. */
35 static const struct grub_arg_option options[] = {
36 /* TRANSLATORS: In this file "mapping" refers to a change GRUB makes so if
37 your language doesn't have an equivalent of "mapping" you can
38 use the word like "rerouting".
39 */
40 {"list", 'l', 0, N_("Show the current mappings."), 0, 0},
41 {"reset", 'r', 0, N_("Reset all mappings to the default values."), 0, 0},
42 {"swap", 's', 0, N_("Perform both direct and reverse mappings."), 0, 0},
43 {0, 0, 0, 0, 0, 0}
44 };
45
46 /* Remember to update options[] accordingly. */
47 enum opt_idxs
48 {
49 OPTIDX_LIST = 0,
50 OPTIDX_RESET,
51 OPTIDX_SWAP,
52 };
53
54 /* Realmode far ptr (2 * 16b) to the previous INT13h handler. */
55 extern grub_uint32_t grub_drivemap_oldhandler;
56
57 /* The type "void" is used for imported assembly labels, takes no storage and
58 serves just to take the address with &label. */
59
60 /* The assembly function to replace the old INT13h handler. It does not follow
61 any C callspecs and returns with IRET. */
62 extern const void grub_drivemap_handler;
63
64 /* Start of the drive mappings area (space reserved at runtime). */
65 extern const void grub_drivemap_mapstart;
66
67 typedef struct drivemap_node
68 {
69 struct drivemap_node *next;
70 grub_uint8_t newdrive;
71 grub_uint8_t redirto;
72 } drivemap_node_t;
73
74 typedef struct GRUB_PACKED int13map_node
75 {
76 grub_uint8_t disknum;
77 grub_uint8_t mapto;
78 } int13map_node_t;
79
80 #define INT13H_OFFSET(x) \
81 (((grub_uint8_t *)(x)) - ((grub_uint8_t *)&grub_drivemap_handler))
82
83 static drivemap_node_t *map_head;
84 static void *drivemap_hook;
85 static int drivemap_mmap;
86
87 /* Puts the specified mapping into the table, replacing an existing mapping
88 for newdrive or adding a new one if required. */
89 static grub_err_t
90 drivemap_set (grub_uint8_t newdrive, grub_uint8_t redirto)
91 {
92 drivemap_node_t *mapping = 0;
93 drivemap_node_t *search = map_head;
94 while (search)
95 {
96 if (search->newdrive == newdrive)
97 {
98 mapping = search;
99 break;
100 }
101 search = search->next;
102 }
103
104 /* Check for pre-existing mappings to modify before creating a new one. */
105 if (mapping)
106 mapping->redirto = redirto;
107 else
108 {
109 mapping = grub_malloc (sizeof (drivemap_node_t));
110 if (! mapping)
111 return grub_errno;
112 mapping->newdrive = newdrive;
113 mapping->redirto = redirto;
114 mapping->next = map_head;
115 map_head = mapping;
116 }
117 return GRUB_ERR_NONE;
118 }
119
120 /* Removes the mapping for newdrive from the table. If there is no mapping,
121 then this function behaves like a no-op on the map. */
122 static void
123 drivemap_remove (grub_uint8_t newdrive)
124 {
125 drivemap_node_t *mapping = 0;
126 drivemap_node_t *search = map_head;
127 drivemap_node_t *previous = 0;
128
129 while (search)
130 {
131 if (search->newdrive == newdrive)
132 {
133 mapping = search;
134 break;
135 }
136 previous = search;
137 search = search->next;
138 }
139
140 if (mapping)
141 {
142 if (previous)
143 previous->next = mapping->next;
144 else
145 map_head = mapping->next;
146 grub_free (mapping);
147 }
148 }
149
150 /* Given a GRUB-like device name and a convenient location, stores the
151 related BIOS disk number. Accepts devices like \((f|h)dN\), with
152 0 <= N < 128. */
153 static grub_err_t
154 tryparse_diskstring (const char *str, grub_uint8_t *output)
155 {
156 /* Skip opening paren in order to allow both (hd0) and hd0. */
157 if (*str == '(')
158 str++;
159 if ((str[0] == 'f' || str[0] == 'h') && str[1] == 'd')
160 {
161 grub_uint8_t bios_num = (str[0] == 'h') ? 0x80 : 0x00;
162 unsigned long drivenum = grub_strtoul (str + 2, 0, 0);
163 if (grub_errno == GRUB_ERR_NONE && drivenum < 128)
164 {
165 bios_num |= drivenum;
166 if (output)
167 *output = bios_num;
168 return GRUB_ERR_NONE;
169 }
170 }
171 return grub_error (GRUB_ERR_BAD_ARGUMENT, "device format \"%s\" "
172 "invalid: must be (f|h)dN, with 0 <= N < 128", str);
173 }
174
175 static grub_err_t
176 list_mappings (void)
177 {
178 /* Show: list mappings. */
179 if (! map_head)
180 {
181 grub_puts_ (N_("No drives have been remapped"));
182 return GRUB_ERR_NONE;
183 }
184
185 /* TRANSLATORS: This is the header of mapping list.
186 On the left is how OS will see the disks and
187 on the right current GRUB vision. */
188 grub_puts_ (N_("OS disk #num ------> GRUB/BIOS device"));
189 drivemap_node_t *curnode = map_head;
190 while (curnode)
191 {
192 grub_printf ("%cD #%-3u (0x%02x) %cd%d\n",
193 (curnode->newdrive & 0x80) ? 'H' : 'F',
194 curnode->newdrive & 0x7F, curnode->newdrive,
195 (curnode->redirto & 0x80) ? 'h' : 'f',
196 curnode->redirto & 0x7F
197 );
198 curnode = curnode->next;
199 }
200 return GRUB_ERR_NONE;
201 }
202
203 static grub_err_t
204 grub_cmd_drivemap (struct grub_extcmd_context *ctxt, int argc, char **args)
205 {
206 if (ctxt->state[OPTIDX_LIST].set)
207 {
208 return list_mappings ();
209 }
210 else if (ctxt->state[OPTIDX_RESET].set)
211 {
212 /* Reset: just delete all mappings, freeing their memory. */
213 drivemap_node_t *curnode = map_head;
214 drivemap_node_t *prevnode = 0;
215 while (curnode)
216 {
217 prevnode = curnode;
218 curnode = curnode->next;
219 grub_free (prevnode);
220 }
221 map_head = 0;
222 return GRUB_ERR_NONE;
223 }
224 else if (!ctxt->state[OPTIDX_SWAP].set && argc == 0)
225 {
226 /* No arguments */
227 return list_mappings ();
228 }
229
230 /* Neither flag: put mapping. */
231 grub_uint8_t mapfrom = 0;
232 grub_uint8_t mapto = 0xFF;
233 grub_err_t err;
234
235 if (argc != 2)
236 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
237
238 err = tryparse_diskstring (args[0], &mapfrom);
239 if (err != GRUB_ERR_NONE)
240 return err;
241
242 err = tryparse_diskstring (args[1], &mapto);
243 if (err != GRUB_ERR_NONE)
244 return err;
245
246 if (mapto == mapfrom)
247 {
248 /* Reset to default. */
249 grub_dprintf ("drivemap", "Removing mapping for %s (%02x)\n",
250 args[0], mapfrom);
251 drivemap_remove (mapfrom);
252 return GRUB_ERR_NONE;
253 }
254 /* Set the mapping for the disk (overwrites any existing mapping). */
255 grub_dprintf ("drivemap", "%s %s (%02x) = %s (%02x)\n",
256 ctxt->state[OPTIDX_SWAP].set ? "Swapping" : "Mapping",
257 args[1], mapto, args[0], mapfrom);
258 err = drivemap_set (mapto, mapfrom);
259 /* If -s, perform the reverse mapping too (only if the first was OK). */
260 if (ctxt->state[OPTIDX_SWAP].set && err == GRUB_ERR_NONE)
261 err = drivemap_set (mapfrom, mapto);
262 return err;
263 }
264
265 /* Int13h handler installer - reserves conventional memory for the handler,
266 copies it over and sets the IVT entry for int13h.
267 This code rests on the assumption that GRUB does not activate any kind
268 of memory mapping apart from identity paging, since it accesses
269 realmode structures by their absolute addresses, like the IVT at 0;
270 and transforms a pmode pointer into a rmode seg:off far ptr. */
271 static grub_err_t
272 install_int13_handler (int noret __attribute__ ((unused)))
273 {
274 /* Size of the full int13 handler "bundle", including code and map. */
275 grub_uint32_t total_size;
276 /* Base address of the space reserved for the handler bundle. */
277 grub_uint8_t *handler_base = 0;
278 /* Address of the map within the deployed bundle. */
279 int13map_node_t *handler_map;
280 /* Real mode IVT slot (seg:off far pointer) for interrupt 0x13. */
281 grub_uint32_t *int13slot = (grub_uint32_t *) grub_absolute_pointer (4 * 0x13);
282
283 int i;
284 int entries = 0;
285 drivemap_node_t *curentry = map_head;
286
287 /* Count entries to prepare a contiguous map block. */
288 while (curentry)
289 {
290 entries++;
291 curentry = curentry->next;
292 }
293 if (entries == 0)
294 {
295 /* No need to install the int13h handler. */
296 grub_dprintf ("drivemap", "No drives marked as remapped, not installing "
297 "our int13h handler.\n");
298 return GRUB_ERR_NONE;
299 }
300
301 grub_dprintf ("drivemap", "Installing our int13h handler\n");
302
303 /* Save the pointer to the old handler. */
304 grub_drivemap_oldhandler = *int13slot;
305 grub_dprintf ("drivemap", "Original int13 handler: %04x:%04x\n",
306 (grub_drivemap_oldhandler >> 16) & 0x0ffff,
307 grub_drivemap_oldhandler & 0x0ffff);
308
309 /* Find a rmode-segment-aligned zone in conventional memory big
310 enough to hold the handler and its data. */
311 total_size = INT13H_OFFSET (&grub_drivemap_mapstart)
312 + (entries + 1) * sizeof (int13map_node_t);
313 grub_dprintf ("drivemap", "Payload is %u bytes long\n", total_size);
314 handler_base = grub_mmap_malign_and_register (16, ALIGN_UP (total_size, 16),
315 &drivemap_mmap,
316 GRUB_MEMORY_RESERVED,
317 GRUB_MMAP_MALLOC_LOW);
318 if (! handler_base)
319 return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't reserve "
320 "memory for the int13h handler");
321
322 /* Copy int13h handler bundle to reserved area. */
323 grub_dprintf ("drivemap", "Reserved memory at %p, copying handler\n",
324 handler_base);
325 grub_memcpy (handler_base, &grub_drivemap_handler,
326 INT13H_OFFSET (&grub_drivemap_mapstart));
327
328 /* Copy the mappings to the reserved area. */
329 curentry = map_head;
330 handler_map = (int13map_node_t *) (handler_base +
331 INT13H_OFFSET (&grub_drivemap_mapstart));
332 grub_dprintf ("drivemap", "Target map at %p, copying mappings\n", handler_map);
333 for (i = 0; i < entries; ++i, curentry = curentry->next)
334 {
335 handler_map[i].disknum = curentry->newdrive;
336 handler_map[i].mapto = curentry->redirto;
337 grub_dprintf ("drivemap", "\t#%d: 0x%02x <- 0x%02x\n", i,
338 handler_map[i].disknum, handler_map[i].mapto);
339 }
340 /* Signal end-of-map. */
341 handler_map[i].disknum = 0;
342 handler_map[i].mapto = 0;
343 grub_dprintf ("drivemap", "\t#%d: 0x00 <- 0x00 (end)\n", i);
344
345 /* Install our function as the int13h handler in the IVT. */
346 *int13slot = ((grub_uint32_t) handler_base) << 12; /* Segment address. */
347 grub_dprintf ("drivemap", "New int13 handler: %04x:%04x\n",
348 (*int13slot >> 16) & 0x0ffff, *int13slot & 0x0ffff);
349
350 return GRUB_ERR_NONE;
351 }
352
353 static grub_err_t
354 uninstall_int13_handler (void)
355 {
356 /* Real mode IVT slot (seg:off far pointer) for interrupt 0x13. */
357 grub_uint32_t *int13slot = (grub_uint32_t *) grub_absolute_pointer (4 * 0x13);
358
359 if (! grub_drivemap_oldhandler)
360 return GRUB_ERR_NONE;
361
362 *int13slot = grub_drivemap_oldhandler;
363 grub_mmap_free_and_unregister (drivemap_mmap);
364 grub_drivemap_oldhandler = 0;
365 grub_dprintf ("drivemap", "Restored int13 handler: %04x:%04x\n",
366 (*int13slot >> 16) & 0x0ffff, *int13slot & 0x0ffff);
367
368 return GRUB_ERR_NONE;
369 }
370
371 static int
372 grub_get_root_biosnumber_drivemap (void)
373 {
374 const char *biosnum;
375 int ret = -1;
376 grub_device_t dev;
377
378 biosnum = grub_env_get ("biosnum");
379
380 if (biosnum)
381 return grub_strtoul (biosnum, 0, 0);
382
383 dev = grub_device_open (0);
384 if (dev && dev->disk && dev->disk->dev
385 && dev->disk->dev->id == GRUB_DISK_DEVICE_BIOSDISK_ID)
386 {
387 drivemap_node_t *curnode = map_head;
388 ret = (int) dev->disk->id;
389 while (curnode)
390 {
391 if (curnode->redirto == ret)
392 {
393 ret = curnode->newdrive;
394 break;
395 }
396 curnode = curnode->next;
397 }
398
399 }
400
401 if (dev)
402 grub_device_close (dev);
403
404 return ret;
405 }
406
407 static grub_extcmd_t cmd;
408 static int (*grub_get_root_biosnumber_saved) (void);
409
410 GRUB_MOD_INIT (drivemap)
411 {
412 grub_get_root_biosnumber_saved = grub_get_root_biosnumber;
413 grub_get_root_biosnumber = grub_get_root_biosnumber_drivemap;
414 cmd = grub_register_extcmd ("drivemap", grub_cmd_drivemap, 0,
415 N_("-l | -r | [-s] grubdev osdisk."),
416 N_("Manage the BIOS drive mappings."),
417 options);
418 drivemap_hook =
419 grub_loader_register_preboot_hook (&install_int13_handler,
420 &uninstall_int13_handler,
421 GRUB_LOADER_PREBOOT_HOOK_PRIO_NORMAL);
422 }
423
424 GRUB_MOD_FINI (drivemap)
425 {
426 grub_get_root_biosnumber = grub_get_root_biosnumber_saved;
427 grub_loader_unregister_preboot_hook (drivemap_hook);
428 drivemap_hook = 0;
429 grub_unregister_extcmd (cmd);
430 }