~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to grub-core/commands/i386/pc/drivemap.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
33
/* Real mode IVT slot (seg:off far pointer) for interrupt 0x13.  */
 
34
static grub_uint32_t *const int13slot = UINT_TO_PTR (4 * 0x13);
 
35
 
 
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},
 
41
  {0, 0, 0, 0, 0, 0}
 
42
};
 
43
 
 
44
/* Remember to update options[] accordingly.  */
 
45
enum opt_idxs
 
46
{
 
47
  OPTIDX_LIST = 0,
 
48
  OPTIDX_RESET,
 
49
  OPTIDX_SWAP,
 
50
};
 
51
 
 
52
/* Realmode far ptr (2 * 16b) to the previous INT13h handler.  */
 
53
extern grub_uint32_t grub_drivemap_oldhandler;
 
54
 
 
55
/* The type "void" is used for imported assembly labels, takes no storage and
 
56
   serves just to take the address with &label.  */
 
57
 
 
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;
 
61
 
 
62
/* Start of the drive mappings area (space reserved at runtime).  */
 
63
extern const void grub_drivemap_mapstart;
 
64
 
 
65
typedef struct drivemap_node
 
66
{
 
67
  struct drivemap_node *next;
 
68
  grub_uint8_t newdrive;
 
69
  grub_uint8_t redirto;
 
70
} drivemap_node_t;
 
71
 
 
72
typedef struct __attribute__ ((packed)) int13map_node
 
73
{
 
74
  grub_uint8_t disknum;
 
75
  grub_uint8_t mapto;
 
76
} int13map_node_t;
 
77
 
 
78
#define INT13H_OFFSET(x) \
 
79
        (((grub_uint8_t *)(x)) - ((grub_uint8_t *)&grub_drivemap_handler))
 
80
 
 
81
static drivemap_node_t *map_head;
 
82
static void *drivemap_hook;
 
83
static int drivemap_mmap;
 
84
 
 
85
/* Puts the specified mapping into the table, replacing an existing mapping
 
86
   for newdrive or adding a new one if required.  */
 
87
static grub_err_t
 
88
drivemap_set (grub_uint8_t newdrive, grub_uint8_t redirto)
 
89
{
 
90
  drivemap_node_t *mapping = 0;
 
91
  drivemap_node_t *search = map_head;
 
92
  while (search)
 
93
    {
 
94
      if (search->newdrive == newdrive)
 
95
        {
 
96
          mapping = search;
 
97
          break;
 
98
        }
 
99
      search = search->next;
 
100
    }
 
101
 
 
102
  /* Check for pre-existing mappings to modify before creating a new one.  */
 
103
  if (mapping)
 
104
    mapping->redirto = redirto;
 
105
  else
 
106
    {
 
107
      mapping = grub_malloc (sizeof (drivemap_node_t));
 
108
      if (! mapping)
 
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;
 
114
      map_head = mapping;
 
115
    }
 
116
  return GRUB_ERR_NONE;
 
117
}
 
118
 
 
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.  */
 
121
static void
 
122
drivemap_remove (grub_uint8_t newdrive)
 
123
{
 
124
  drivemap_node_t *mapping = 0;
 
125
  drivemap_node_t *search = map_head;
 
126
  drivemap_node_t *previous = 0;
 
127
 
 
128
  while (search)
 
129
    {
 
130
      if (search->newdrive == newdrive)
 
131
        {
 
132
          mapping = search;
 
133
          break;
 
134
        }
 
135
      previous = search;
 
136
      search = search->next;
 
137
    }
 
138
 
 
139
  if (mapping)
 
140
    {
 
141
      if (previous)
 
142
        previous->next = mapping->next;
 
143
      else
 
144
        map_head = mapping->next;
 
145
      grub_free (mapping);
 
146
    }
 
147
}
 
148
 
 
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
 
151
   0 <= N < 128.  */
 
152
static grub_err_t
 
153
tryparse_diskstring (const char *str, grub_uint8_t *output)
 
154
{
 
155
  /* Skip opening paren in order to allow both (hd0) and hd0.  */
 
156
  if (*str == '(')
 
157
    str++;
 
158
  if ((str[0] == 'f' || str[0] == 'h') && str[1] == 'd')
 
159
    {
 
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)
 
163
        {
 
164
          bios_num |= drivenum;
 
165
          if (output)
 
166
            *output = bios_num;
 
167
          return GRUB_ERR_NONE;
 
168
        }
 
169
    }
 
170
  return grub_error (GRUB_ERR_BAD_ARGUMENT, "device format \"%s\" "
 
171
                     "invalid: must be (f|h)dN, with 0 <= N < 128", str);
 
172
}
 
173
 
 
174
static grub_err_t
 
175
list_mappings (void)
 
176
{
 
177
  /* Show: list mappings.  */
 
178
  if (! map_head)
 
179
    {
 
180
      grub_printf ("No drives have been remapped\n");
 
181
      return GRUB_ERR_NONE;
 
182
    }
 
183
 
 
184
  grub_printf ("OS disk #num ------> GRUB/BIOS device\n");
 
185
  drivemap_node_t *curnode = map_head;
 
186
  while (curnode)
 
187
    {
 
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
 
193
                   );
 
194
      curnode = curnode->next;
 
195
    }
 
196
  return GRUB_ERR_NONE;
 
197
}
 
198
 
 
199
static grub_err_t
 
200
grub_cmd_drivemap (struct grub_extcmd_context *ctxt, int argc, char **args)
 
201
{
 
202
  if (ctxt->state[OPTIDX_LIST].set)
 
203
    {
 
204
      return list_mappings ();
 
205
    }
 
206
  else if (ctxt->state[OPTIDX_RESET].set)
 
207
    {
 
208
      /* Reset: just delete all mappings, freeing their memory.  */
 
209
      drivemap_node_t *curnode = map_head;
 
210
      drivemap_node_t *prevnode = 0;
 
211
      while (curnode)
 
212
        {
 
213
          prevnode = curnode;
 
214
          curnode = curnode->next;
 
215
          grub_free (prevnode);
 
216
        }
 
217
      map_head = 0;
 
218
      return GRUB_ERR_NONE;
 
219
    }
 
220
  else if (!ctxt->state[OPTIDX_SWAP].set && argc == 0)
 
221
    {
 
222
      /* No arguments */
 
223
      return list_mappings ();
 
224
    }
 
225
 
 
226
  /* Neither flag: put mapping.  */
 
227
  grub_uint8_t mapfrom = 0;
 
228
  grub_uint8_t mapto = 0xFF;
 
229
  grub_err_t err;
 
230
 
 
231
  if (argc != 2)
 
232
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "two arguments required");
 
233
 
 
234
  err = tryparse_diskstring (args[0], &mapfrom);
 
235
  if (err != GRUB_ERR_NONE)
 
236
    return err;
 
237
 
 
238
  err = tryparse_diskstring (args[1], &mapto);
 
239
  if (err != GRUB_ERR_NONE)
 
240
    return err;
 
241
 
 
242
  if (mapto == mapfrom)
 
243
    {
 
244
      /* Reset to default.  */
 
245
      grub_dprintf ("drivemap", "Removing mapping for %s (%02x)\n",
 
246
                    args[0], mapfrom);
 
247
      drivemap_remove (mapfrom);
 
248
      return GRUB_ERR_NONE;
 
249
    }
 
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);
 
258
  return err;
 
259
}
 
260
 
 
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.  */
 
267
static grub_err_t
 
268
install_int13_handler (int noret __attribute__ ((unused)))
 
269
{
 
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;
 
276
 
 
277
  int i;
 
278
  int entries = 0;
 
279
  drivemap_node_t *curentry = map_head;
 
280
 
 
281
  /* Count entries to prepare a contiguous map block.  */
 
282
  while (curentry)
 
283
    {
 
284
      entries++;
 
285
      curentry = curentry->next;
 
286
    }
 
287
  if (entries == 0)
 
288
    {
 
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;
 
293
    }
 
294
 
 
295
  grub_dprintf ("drivemap", "Installing our int13h handler\n");
 
296
 
 
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);
 
302
 
 
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,
 
309
                                                &drivemap_mmap,
 
310
                                                GRUB_MEMORY_RESERVED,
 
311
                                                GRUB_MMAP_MALLOC_LOW);
 
312
  if (! handler_base)
 
313
    return grub_error (GRUB_ERR_OUT_OF_MEMORY, "couldn't reserve "
 
314
                       "memory for the int13h handler");
 
315
 
 
316
  /* Copy int13h handler bundle to reserved area.  */
 
317
  grub_dprintf ("drivemap", "Reserved memory at %p, copying handler\n",
 
318
                handler_base);
 
319
  grub_memcpy (handler_base, &grub_drivemap_handler,
 
320
               INT13H_OFFSET (&grub_drivemap_mapstart));
 
321
 
 
322
  /* Copy the mappings to the reserved area.  */
 
323
  curentry = map_head;
 
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)
 
328
    {
 
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);
 
333
    }
 
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);
 
338
 
 
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);
 
343
 
 
344
  return GRUB_ERR_NONE;
 
345
}
 
346
 
 
347
static grub_err_t
 
348
uninstall_int13_handler (void)
 
349
{
 
350
  if (! grub_drivemap_oldhandler)
 
351
    return GRUB_ERR_NONE;
 
352
 
 
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);
 
358
 
 
359
  return GRUB_ERR_NONE;
 
360
}
 
361
 
 
362
static int
 
363
grub_get_root_biosnumber_drivemap (void)
 
364
{
 
365
  char *biosnum;
 
366
  int ret = -1;
 
367
  grub_device_t dev;
 
368
 
 
369
  biosnum = grub_env_get ("biosnum");
 
370
 
 
371
  if (biosnum)
 
372
    return grub_strtoul (biosnum, 0, 0);
 
373
 
 
374
  dev = grub_device_open (0);
 
375
  if (dev && dev->disk && dev->disk->dev
 
376
      && dev->disk->dev->id == GRUB_DISK_DEVICE_BIOSDISK_ID)
 
377
    {
 
378
      drivemap_node_t *curnode = map_head;
 
379
      ret = (int) dev->disk->id;
 
380
      while (curnode)
 
381
        {
 
382
          if (curnode->redirto == ret)
 
383
            {
 
384
              ret = curnode->newdrive;
 
385
              break;
 
386
            }
 
387
          curnode = curnode->next;
 
388
        }
 
389
 
 
390
    }
 
391
 
 
392
  if (dev)
 
393
    grub_device_close (dev);
 
394
 
 
395
  return ret;
 
396
}
 
397
 
 
398
static grub_extcmd_t cmd;
 
399
static int (*grub_get_root_biosnumber_saved) (void);
 
400
 
 
401
GRUB_MOD_INIT (drivemap)
 
402
{
 
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."),
 
408
                              options);
 
409
  drivemap_hook =
 
410
    grub_loader_register_preboot_hook (&install_int13_handler,
 
411
                                       &uninstall_int13_handler,
 
412
                                       GRUB_LOADER_PREBOOT_HOOK_PRIO_NORMAL);
 
413
}
 
414
 
 
415
GRUB_MOD_FINI (drivemap)
 
416
{
 
417
  grub_get_root_biosnumber = grub_get_root_biosnumber_saved;
 
418
  grub_loader_unregister_preboot_hook (drivemap_hook);
 
419
  drivemap_hook = 0;
 
420
  grub_unregister_extcmd (cmd);
 
421
}