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/memory.h>
28
#include <grub/machine/biosnum.h>
29
#include <grub/i18n.h>
32
/* Real mode IVT slot (seg:off far pointer) for interrupt 0x13. */
33
static grub_uint32_t *const int13slot = UINT_TO_PTR (4 * 0x13);
35
/* Remember to update enum opt_idxs accordingly. */
36
static const struct grub_arg_option options[] = {
37
{"list", 'l', 0, N_("Show the current mappings."), 0, 0},
38
{"reset", 'r', 0, N_("Reset all mappings to the default values."), 0, 0},
39
{"swap", 's', 0, N_("Perform both direct and reverse mappings."), 0, 0},
43
/* Remember to update options[] accordingly. */
51
/* Realmode far ptr (2 * 16b) to the previous INT13h handler. */
52
extern grub_uint32_t grub_drivemap_oldhandler;
54
/* The type "void" is used for imported assembly labels, takes no storage and
55
serves just to take the address with &label. */
57
/* The assembly function to replace the old INT13h handler. It does not follow
58
any C callspecs and returns with IRET. */
59
extern const void grub_drivemap_handler;
61
/* Start of the drive mappings area (space reserved at runtime). */
62
extern const void grub_drivemap_mapstart;
64
typedef struct drivemap_node
66
struct drivemap_node *next;
67
grub_uint8_t newdrive;
71
typedef struct __attribute__ ((packed)) int13map_node
77
#define INT13H_OFFSET(x) \
78
(((grub_uint8_t *)(x)) - ((grub_uint8_t *)&grub_drivemap_handler))
80
static drivemap_node_t *map_head;
81
static void *drivemap_hook;
82
static int drivemap_mmap;
84
/* Puts the specified mapping into the table, replacing an existing mapping
85
for newdrive or adding a new one if required. */
87
drivemap_set (grub_uint8_t newdrive, grub_uint8_t redirto)
89
drivemap_node_t *mapping = 0;
90
drivemap_node_t *search = map_head;
93
if (search->newdrive == newdrive)
98
search = search->next;
101
/* Check for pre-existing mappings to modify before creating a new one. */
103
mapping->redirto = redirto;
106
mapping = grub_malloc (sizeof (drivemap_node_t));
108
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
109
"cannot allocate map entry, not enough memory");
110
mapping->newdrive = newdrive;
111
mapping->redirto = redirto;
112
mapping->next = map_head;
115
return GRUB_ERR_NONE;
118
/* Removes the mapping for newdrive from the table. If there is no mapping,
119
then this function behaves like a no-op on the map. */
121
drivemap_remove (grub_uint8_t newdrive)
123
drivemap_node_t *mapping = 0;
124
drivemap_node_t *search = map_head;
125
drivemap_node_t *previous = 0;
129
if (search->newdrive == newdrive)
135
search = search->next;
141
previous->next = mapping->next;
143
map_head = mapping->next;
148
/* Given a GRUB-like device name and a convenient location, stores the
149
related BIOS disk number. Accepts devices like \((f|h)dN\), with
152
tryparse_diskstring (const char *str, grub_uint8_t *output)
154
/* Skip opening paren in order to allow both (hd0) and hd0. */
157
if ((str[0] == 'f' || str[0] == 'h') && str[1] == 'd')
159
grub_uint8_t bios_num = (str[0] == 'h') ? 0x80 : 0x00;
160
unsigned long drivenum = grub_strtoul (str + 2, 0, 0);
161
if (grub_errno == GRUB_ERR_NONE && drivenum < 128)
163
bios_num |= drivenum;
166
return GRUB_ERR_NONE;
169
return grub_error (GRUB_ERR_BAD_ARGUMENT, "device format \"%s\" "
170
"invalid: must be (f|h)dN, with 0 <= N < 128", str);
176
/* Show: list mappings. */
179
grub_printf ("No drives have been remapped\n");
180
return GRUB_ERR_NONE;
183
grub_printf ("OS disk #num ------> GRUB/BIOS device\n");
184
drivemap_node_t *curnode = map_head;
187
grub_printf ("%cD #%-3u (0x%02x) %cd%d\n",
188
(curnode->newdrive & 0x80) ? 'H' : 'F',
189
curnode->newdrive & 0x7F, curnode->newdrive,
190
(curnode->redirto & 0x80) ? 'h' : 'f',
191
curnode->redirto & 0x7F
193
curnode = curnode->next;
195
return GRUB_ERR_NONE;
199
grub_cmd_drivemap (struct grub_extcmd *cmd, int argc, char **args)
201
if (cmd->state[OPTIDX_LIST].set)
203
return list_mappings ();
205
else if (cmd->state[OPTIDX_RESET].set)
207
/* Reset: just delete all mappings, freeing their memory. */
208
drivemap_node_t *curnode = map_head;
209
drivemap_node_t *prevnode = 0;
213
curnode = curnode->next;
214
grub_free (prevnode);
217
return GRUB_ERR_NONE;
219
else if (!cmd->state[OPTIDX_SWAP].set && argc == 0)
222
return list_mappings ();
225
/* Neither flag: put mapping. */
226
grub_uint8_t mapfrom = 0;
227
grub_uint8_t mapto = 0xFF;
231
return grub_error (GRUB_ERR_BAD_ARGUMENT, "two arguments required");
233
err = tryparse_diskstring (args[0], &mapfrom);
234
if (err != GRUB_ERR_NONE)
237
err = tryparse_diskstring (args[1], &mapto);
238
if (err != GRUB_ERR_NONE)
241
if (mapto == mapfrom)
243
/* Reset to default. */
244
grub_dprintf ("drivemap", "Removing mapping for %s (%02x)\n",
246
drivemap_remove (mapfrom);
247
return GRUB_ERR_NONE;
249
/* Set the mapping for the disk (overwrites any existing mapping). */
250
grub_dprintf ("drivemap", "%s %s (%02x) = %s (%02x)\n",
251
cmd->state[OPTIDX_SWAP].set ? "Swapping" : "Mapping",
252
args[1], mapto, args[0], mapfrom);
253
err = drivemap_set (mapto, mapfrom);
254
/* If -s, perform the reverse mapping too (only if the first was OK). */
255
if (cmd->state[OPTIDX_SWAP].set && err == GRUB_ERR_NONE)
256
err = drivemap_set (mapfrom, mapto);
260
/* Int13h handler installer - reserves conventional memory for the handler,
261
copies it over and sets the IVT entry for int13h.
262
This code rests on the assumption that GRUB does not activate any kind
263
of memory mapping apart from identity paging, since it accesses
264
realmode structures by their absolute addresses, like the IVT at 0;
265
and transforms a pmode pointer into a rmode seg:off far ptr. */
267
install_int13_handler (int noret __attribute__ ((unused)))
269
/* Size of the full int13 handler "bundle", including code and map. */
270
grub_uint32_t total_size;
271
/* Base address of the space reserved for the handler bundle. */
272
grub_uint8_t *handler_base = 0;
273
/* Address of the map within the deployed bundle. */
274
int13map_node_t *handler_map;
278
drivemap_node_t *curentry = map_head;
280
/* Count entries to prepare a contiguous map block. */
284
curentry = curentry->next;
288
/* No need to install the int13h handler. */
289
grub_dprintf ("drivemap", "No drives marked as remapped, not installing "
290
"our int13h handler.\n");
291
return GRUB_ERR_NONE;
294
grub_dprintf ("drivemap", "Installing our int13h handler\n");
296
/* Save the pointer to the old handler. */
297
grub_drivemap_oldhandler = *int13slot;
298
grub_dprintf ("drivemap", "Original int13 handler: %04x:%04x\n",
299
(grub_drivemap_oldhandler >> 16) & 0x0ffff,
300
grub_drivemap_oldhandler & 0x0ffff);
302
/* Find a rmode-segment-aligned zone in conventional memory big
303
enough to hold the handler and its data. */
304
total_size = INT13H_OFFSET (&grub_drivemap_mapstart)
305
+ (entries + 1) * sizeof (int13map_node_t);
306
grub_dprintf ("drivemap", "Payload is %u bytes long\n", total_size);
307
handler_base = grub_mmap_malign_and_register (16, total_size,
309
GRUB_MACHINE_MEMORY_RESERVED,
310
GRUB_MMAP_MALLOC_LOW);
312
return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't reserve "
313
"memory for the int13h handler");
315
/* Copy int13h handler bundle to reserved area. */
316
grub_dprintf ("drivemap", "Reserved memory at %p, copying handler\n",
318
grub_memcpy (handler_base, &grub_drivemap_handler,
319
INT13H_OFFSET (&grub_drivemap_mapstart));
321
/* Copy the mappings to the reserved area. */
323
handler_map = (int13map_node_t *) (handler_base +
324
INT13H_OFFSET (&grub_drivemap_mapstart));
325
grub_dprintf ("drivemap", "Target map at %p, copying mappings\n", handler_map);
326
for (i = 0; i < entries; ++i, curentry = curentry->next)
328
handler_map[i].disknum = curentry->newdrive;
329
handler_map[i].mapto = curentry->redirto;
330
grub_dprintf ("drivemap", "\t#%d: 0x%02x <- 0x%02x\n", i,
331
handler_map[i].disknum, handler_map[i].mapto);
333
/* Signal end-of-map. */
334
handler_map[i].disknum = 0;
335
handler_map[i].mapto = 0;
336
grub_dprintf ("drivemap", "\t#%d: 0x00 <- 0x00 (end)\n", i);
338
/* Install our function as the int13h handler in the IVT. */
339
*int13slot = ((grub_uint32_t) handler_base) << 12; /* Segment address. */
340
grub_dprintf ("drivemap", "New int13 handler: %04x:%04x\n",
341
(*int13slot >> 16) & 0x0ffff, *int13slot & 0x0ffff);
343
return GRUB_ERR_NONE;
347
uninstall_int13_handler (void)
349
if (! grub_drivemap_oldhandler)
350
return GRUB_ERR_NONE;
352
*int13slot = grub_drivemap_oldhandler;
353
grub_mmap_free_and_unregister (drivemap_mmap);
354
grub_drivemap_oldhandler = 0;
355
grub_dprintf ("drivemap", "Restored int13 handler: %04x:%04x\n",
356
(*int13slot >> 16) & 0x0ffff, *int13slot & 0x0ffff);
358
return GRUB_ERR_NONE;
362
grub_get_root_biosnumber_drivemap (void)
368
biosnum = grub_env_get ("biosnum");
371
return grub_strtoul (biosnum, 0, 0);
373
dev = grub_device_open (0);
374
if (dev && dev->disk && dev->disk->dev
375
&& dev->disk->dev->id == GRUB_DISK_DEVICE_BIOSDISK_ID)
377
drivemap_node_t *curnode = map_head;
378
ret = (int) dev->disk->id;
381
if (curnode->redirto == ret)
383
ret = curnode->newdrive;
386
curnode = curnode->next;
392
grub_device_close (dev);
397
static grub_extcmd_t cmd;
398
static int (*grub_get_root_biosnumber_saved) (void);
400
GRUB_MOD_INIT (drivemap)
402
grub_get_root_biosnumber_saved = grub_get_root_biosnumber;
403
grub_get_root_biosnumber = grub_get_root_biosnumber_drivemap;
404
cmd = grub_register_extcmd ("drivemap", grub_cmd_drivemap,
405
GRUB_COMMAND_FLAG_BOTH,
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);