1
/* drivemap.c - command to manage the BIOS drive mappings. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2008, 2009 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 <grub/extcmd.h>
23
#include <grub/misc.h>
24
#include <grub/disk.h>
25
#include <grub/loader.h>
27
#include <grub/machine/biosnum.h>
28
#include <grub/i18n.h>
29
#include <grub/memory.h>
30
#include <grub/machine/memory.h>
33
/* Real mode IVT slot (seg:off far pointer) for interrupt 0x13. */
34
static grub_uint32_t *const int13slot = UINT_TO_PTR (4 * 0x13);
36
/* Remember to update enum opt_idxs accordingly. */
37
static const struct grub_arg_option options[] = {
38
{"list", 'l', 0, N_("Show the current mappings."), 0, 0},
39
{"reset", 'r', 0, N_("Reset all mappings to the default values."), 0, 0},
40
{"swap", 's', 0, N_("Perform both direct and reverse mappings."), 0, 0},
44
/* Remember to update options[] accordingly. */
52
/* Realmode far ptr (2 * 16b) to the previous INT13h handler. */
53
extern grub_uint32_t grub_drivemap_oldhandler;
55
/* The type "void" is used for imported assembly labels, takes no storage and
56
serves just to take the address with &label. */
58
/* The assembly function to replace the old INT13h handler. It does not follow
59
any C callspecs and returns with IRET. */
60
extern const void grub_drivemap_handler;
62
/* Start of the drive mappings area (space reserved at runtime). */
63
extern const void grub_drivemap_mapstart;
65
typedef struct drivemap_node
67
struct drivemap_node *next;
68
grub_uint8_t newdrive;
72
typedef struct __attribute__ ((packed)) int13map_node
78
#define INT13H_OFFSET(x) \
79
(((grub_uint8_t *)(x)) - ((grub_uint8_t *)&grub_drivemap_handler))
81
static drivemap_node_t *map_head;
82
static void *drivemap_hook;
83
static int drivemap_mmap;
85
/* Puts the specified mapping into the table, replacing an existing mapping
86
for newdrive or adding a new one if required. */
88
drivemap_set (grub_uint8_t newdrive, grub_uint8_t redirto)
90
drivemap_node_t *mapping = 0;
91
drivemap_node_t *search = map_head;
94
if (search->newdrive == newdrive)
99
search = search->next;
102
/* Check for pre-existing mappings to modify before creating a new one. */
104
mapping->redirto = redirto;
107
mapping = grub_malloc (sizeof (drivemap_node_t));
109
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
110
"cannot allocate map entry, not enough memory");
111
mapping->newdrive = newdrive;
112
mapping->redirto = redirto;
113
mapping->next = map_head;
116
return GRUB_ERR_NONE;
119
/* Removes the mapping for newdrive from the table. If there is no mapping,
120
then this function behaves like a no-op on the map. */
122
drivemap_remove (grub_uint8_t newdrive)
124
drivemap_node_t *mapping = 0;
125
drivemap_node_t *search = map_head;
126
drivemap_node_t *previous = 0;
130
if (search->newdrive == newdrive)
136
search = search->next;
142
previous->next = mapping->next;
144
map_head = mapping->next;
149
/* Given a GRUB-like device name and a convenient location, stores the
150
related BIOS disk number. Accepts devices like \((f|h)dN\), with
153
tryparse_diskstring (const char *str, grub_uint8_t *output)
155
/* Skip opening paren in order to allow both (hd0) and hd0. */
158
if ((str[0] == 'f' || str[0] == 'h') && str[1] == 'd')
160
grub_uint8_t bios_num = (str[0] == 'h') ? 0x80 : 0x00;
161
unsigned long drivenum = grub_strtoul (str + 2, 0, 0);
162
if (grub_errno == GRUB_ERR_NONE && drivenum < 128)
164
bios_num |= drivenum;
167
return GRUB_ERR_NONE;
170
return grub_error (GRUB_ERR_BAD_ARGUMENT, "device format \"%s\" "
171
"invalid: must be (f|h)dN, with 0 <= N < 128", str);
177
/* Show: list mappings. */
180
grub_printf ("No drives have been remapped\n");
181
return GRUB_ERR_NONE;
184
grub_printf ("OS disk #num ------> GRUB/BIOS device\n");
185
drivemap_node_t *curnode = map_head;
188
grub_printf ("%cD #%-3u (0x%02x) %cd%d\n",
189
(curnode->newdrive & 0x80) ? 'H' : 'F',
190
curnode->newdrive & 0x7F, curnode->newdrive,
191
(curnode->redirto & 0x80) ? 'h' : 'f',
192
curnode->redirto & 0x7F
194
curnode = curnode->next;
196
return GRUB_ERR_NONE;
200
grub_cmd_drivemap (struct grub_extcmd_context *ctxt, int argc, char **args)
202
if (ctxt->state[OPTIDX_LIST].set)
204
return list_mappings ();
206
else if (ctxt->state[OPTIDX_RESET].set)
208
/* Reset: just delete all mappings, freeing their memory. */
209
drivemap_node_t *curnode = map_head;
210
drivemap_node_t *prevnode = 0;
214
curnode = curnode->next;
215
grub_free (prevnode);
218
return GRUB_ERR_NONE;
220
else if (!ctxt->state[OPTIDX_SWAP].set && argc == 0)
223
return list_mappings ();
226
/* Neither flag: put mapping. */
227
grub_uint8_t mapfrom = 0;
228
grub_uint8_t mapto = 0xFF;
232
return grub_error (GRUB_ERR_BAD_ARGUMENT, "two arguments required");
234
err = tryparse_diskstring (args[0], &mapfrom);
235
if (err != GRUB_ERR_NONE)
238
err = tryparse_diskstring (args[1], &mapto);
239
if (err != GRUB_ERR_NONE)
242
if (mapto == mapfrom)
244
/* Reset to default. */
245
grub_dprintf ("drivemap", "Removing mapping for %s (%02x)\n",
247
drivemap_remove (mapfrom);
248
return GRUB_ERR_NONE;
250
/* Set the mapping for the disk (overwrites any existing mapping). */
251
grub_dprintf ("drivemap", "%s %s (%02x) = %s (%02x)\n",
252
ctxt->state[OPTIDX_SWAP].set ? "Swapping" : "Mapping",
253
args[1], mapto, args[0], mapfrom);
254
err = drivemap_set (mapto, mapfrom);
255
/* If -s, perform the reverse mapping too (only if the first was OK). */
256
if (ctxt->state[OPTIDX_SWAP].set && err == GRUB_ERR_NONE)
257
err = drivemap_set (mapfrom, mapto);
261
/* Int13h handler installer - reserves conventional memory for the handler,
262
copies it over and sets the IVT entry for int13h.
263
This code rests on the assumption that GRUB does not activate any kind
264
of memory mapping apart from identity paging, since it accesses
265
realmode structures by their absolute addresses, like the IVT at 0;
266
and transforms a pmode pointer into a rmode seg:off far ptr. */
268
install_int13_handler (int noret __attribute__ ((unused)))
270
/* Size of the full int13 handler "bundle", including code and map. */
271
grub_uint32_t total_size;
272
/* Base address of the space reserved for the handler bundle. */
273
grub_uint8_t *handler_base = 0;
274
/* Address of the map within the deployed bundle. */
275
int13map_node_t *handler_map;
279
drivemap_node_t *curentry = map_head;
281
/* Count entries to prepare a contiguous map block. */
285
curentry = curentry->next;
289
/* No need to install the int13h handler. */
290
grub_dprintf ("drivemap", "No drives marked as remapped, not installing "
291
"our int13h handler.\n");
292
return GRUB_ERR_NONE;
295
grub_dprintf ("drivemap", "Installing our int13h handler\n");
297
/* Save the pointer to the old handler. */
298
grub_drivemap_oldhandler = *int13slot;
299
grub_dprintf ("drivemap", "Original int13 handler: %04x:%04x\n",
300
(grub_drivemap_oldhandler >> 16) & 0x0ffff,
301
grub_drivemap_oldhandler & 0x0ffff);
303
/* Find a rmode-segment-aligned zone in conventional memory big
304
enough to hold the handler and its data. */
305
total_size = INT13H_OFFSET (&grub_drivemap_mapstart)
306
+ (entries + 1) * sizeof (int13map_node_t);
307
grub_dprintf ("drivemap", "Payload is %u bytes long\n", total_size);
308
handler_base = grub_mmap_malign_and_register (16, total_size,
310
GRUB_MEMORY_RESERVED,
311
GRUB_MMAP_MALLOC_LOW);
313
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't reserve "
314
"memory for the int13h handler");
316
/* Copy int13h handler bundle to reserved area. */
317
grub_dprintf ("drivemap", "Reserved memory at %p, copying handler\n",
319
grub_memcpy (handler_base, &grub_drivemap_handler,
320
INT13H_OFFSET (&grub_drivemap_mapstart));
322
/* Copy the mappings to the reserved area. */
324
handler_map = (int13map_node_t *) (handler_base +
325
INT13H_OFFSET (&grub_drivemap_mapstart));
326
grub_dprintf ("drivemap", "Target map at %p, copying mappings\n", handler_map);
327
for (i = 0; i < entries; ++i, curentry = curentry->next)
329
handler_map[i].disknum = curentry->newdrive;
330
handler_map[i].mapto = curentry->redirto;
331
grub_dprintf ("drivemap", "\t#%d: 0x%02x <- 0x%02x\n", i,
332
handler_map[i].disknum, handler_map[i].mapto);
334
/* Signal end-of-map. */
335
handler_map[i].disknum = 0;
336
handler_map[i].mapto = 0;
337
grub_dprintf ("drivemap", "\t#%d: 0x00 <- 0x00 (end)\n", i);
339
/* Install our function as the int13h handler in the IVT. */
340
*int13slot = ((grub_uint32_t) handler_base) << 12; /* Segment address. */
341
grub_dprintf ("drivemap", "New int13 handler: %04x:%04x\n",
342
(*int13slot >> 16) & 0x0ffff, *int13slot & 0x0ffff);
344
return GRUB_ERR_NONE;
348
uninstall_int13_handler (void)
350
if (! grub_drivemap_oldhandler)
351
return GRUB_ERR_NONE;
353
*int13slot = grub_drivemap_oldhandler;
354
grub_mmap_free_and_unregister (drivemap_mmap);
355
grub_drivemap_oldhandler = 0;
356
grub_dprintf ("drivemap", "Restored int13 handler: %04x:%04x\n",
357
(*int13slot >> 16) & 0x0ffff, *int13slot & 0x0ffff);
359
return GRUB_ERR_NONE;
363
grub_get_root_biosnumber_drivemap (void)
369
biosnum = grub_env_get ("biosnum");
372
return grub_strtoul (biosnum, 0, 0);
374
dev = grub_device_open (0);
375
if (dev && dev->disk && dev->disk->dev
376
&& dev->disk->dev->id == GRUB_DISK_DEVICE_BIOSDISK_ID)
378
drivemap_node_t *curnode = map_head;
379
ret = (int) dev->disk->id;
382
if (curnode->redirto == ret)
384
ret = curnode->newdrive;
387
curnode = curnode->next;
393
grub_device_close (dev);
398
static grub_extcmd_t cmd;
399
static int (*grub_get_root_biosnumber_saved) (void);
401
GRUB_MOD_INIT (drivemap)
403
grub_get_root_biosnumber_saved = grub_get_root_biosnumber;
404
grub_get_root_biosnumber = grub_get_root_biosnumber_drivemap;
405
cmd = grub_register_extcmd ("drivemap", grub_cmd_drivemap, 0,
406
N_("-l | -r | [-s] grubdev osdisk."),
407
N_("Manage the BIOS drive mappings."),
410
grub_loader_register_preboot_hook (&install_int13_handler,
411
&uninstall_int13_handler,
412
GRUB_LOADER_PREBOOT_HOOK_PRIO_NORMAL);
415
GRUB_MOD_FINI (drivemap)
417
grub_get_root_biosnumber = grub_get_root_biosnumber_saved;
418
grub_loader_unregister_preboot_hook (drivemap_hook);
420
grub_unregister_extcmd (cmd);