~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to grub-core/commands/keylayouts.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
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2002,2003,2005,2007,2008,2009  Free Software Foundation, Inc.
 
4
 *
 
5
 *  GRUB is free software: you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation, either version 3 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  GRUB is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include <grub/term.h>
 
20
#include <grub/err.h>
 
21
#include <grub/mm.h>
 
22
#include <grub/misc.h>
 
23
#include <grub/env.h>
 
24
#include <grub/time.h>
 
25
#include <grub/dl.h>
 
26
#include <grub/keyboard_layouts.h>
 
27
#include <grub/command.h>
 
28
#include <grub/i18n.h>
 
29
#include <grub/file.h>
 
30
 
 
31
static struct grub_keyboard_layout layout_us = {
 
32
  .keyboard_map = {
 
33
    /* Keyboard errors. Handled by driver.  */
 
34
    /* 0x00 */   0,   0,   0,   0,
 
35
 
 
36
    /* 0x04 */ 'a',  'b',  'c',  'd', 
 
37
    /* 0x08 */ 'e',  'f',  'g',  'h',  'i', 'j', 'k', 'l',
 
38
    /* 0x10 */ 'm',  'n',  'o',  'p',  'q', 'r', 's', 't',
 
39
    /* 0x18 */ 'u',  'v',  'w',  'x',  'y', 'z', '1', '2',
 
40
    /* 0x20 */ '3',  '4',  '5',  '6',  '7', '8', '9', '0',
 
41
    /* 0x28 */ '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
 
42
    /* According to usage table 0x31 should be mapped to '/'
 
43
       but testing with real keyboard shows that 0x32 is remapped to '/'.
 
44
       Map 0x31 to 0. 
 
45
    */
 
46
    /* 0x30 */ ']',   0,   '\\', ';', '\'', '`', ',', '.',
 
47
    /* 0x39 is CapsLock. Handled by driver.  */
 
48
    /* 0x38 */ '/',   0,   GRUB_TERM_KEY_F1, GRUB_TERM_KEY_F2, 
 
49
    /* 0x3c */ GRUB_TERM_KEY_F3,     GRUB_TERM_KEY_F4,
 
50
    /* 0x3e */ GRUB_TERM_KEY_F5,     GRUB_TERM_KEY_F6,
 
51
    /* 0x40 */ GRUB_TERM_KEY_F7,     GRUB_TERM_KEY_F8,
 
52
    /* 0x42 */ GRUB_TERM_KEY_F9,     GRUB_TERM_KEY_F10,
 
53
    /* 0x44 */ GRUB_TERM_KEY_F11,    GRUB_TERM_KEY_F12,
 
54
    /* PrtScr and ScrollLock. Not handled yet.  */
 
55
    /* 0x46 */ 0,                    0,
 
56
    /* 0x48 is Pause. Not handled yet.  */
 
57
    /* 0x48 */ 0,                    GRUB_TERM_KEY_INSERT, 
 
58
    /* 0x4a */ GRUB_TERM_KEY_HOME,   GRUB_TERM_KEY_PPAGE,
 
59
    /* 0x4c */ GRUB_TERM_KEY_DC,     GRUB_TERM_KEY_END,
 
60
    /* 0x4e */ GRUB_TERM_KEY_NPAGE,  GRUB_TERM_KEY_RIGHT,
 
61
    /* 0x50 */ GRUB_TERM_KEY_LEFT,   GRUB_TERM_KEY_DOWN,
 
62
    /* 0x53 is NumLock. Handled by driver.  */
 
63
    /* 0x52 */ GRUB_TERM_KEY_UP,     0,
 
64
    /* 0x54 */ '/',                  '*', 
 
65
    /* 0x56 */ '-',                  '+',
 
66
    /* 0x58 */ '\n',                 GRUB_TERM_KEY_END, 
 
67
    /* 0x5a */ GRUB_TERM_KEY_DOWN,   GRUB_TERM_KEY_NPAGE,
 
68
    /* 0x5c */ GRUB_TERM_KEY_LEFT,   GRUB_TERM_KEY_CENTER,
 
69
    /* 0x5e */ GRUB_TERM_KEY_RIGHT,  GRUB_TERM_KEY_HOME,
 
70
    /* 0x60 */ GRUB_TERM_KEY_UP,     GRUB_TERM_KEY_PPAGE,
 
71
    /* 0x62 */ GRUB_TERM_KEY_INSERT, GRUB_TERM_KEY_DC,
 
72
    /* 0x64 */ '\\'
 
73
  },
 
74
  .keyboard_map_shift = {
 
75
    /* Keyboard errors. Handled by driver.  */
 
76
    /* 0x00 */   0,   0,   0,   0,
 
77
 
 
78
    /* 0x04 */ 'A',  'B',  'C',  'D', 
 
79
    /* 0x08 */ 'E',  'F',  'G',  'H',  'I', 'J', 'K', 'L',
 
80
    /* 0x10 */ 'M',  'N',  'O',  'P',  'Q', 'R', 'S', 'T',
 
81
    /* 0x18 */ 'U',  'V',  'W',  'X',  'Y', 'Z', '!', '@',
 
82
    /* 0x20 */ '#',  '$',  '%',  '^',  '&', '*', '(', ')',
 
83
    /* 0x28 */ '\n' | GRUB_TERM_SHIFT, '\e' | GRUB_TERM_SHIFT, 
 
84
    /* 0x2a */ '\b' | GRUB_TERM_SHIFT, '\t' | GRUB_TERM_SHIFT, 
 
85
    /* 0x2c */ ' '  | GRUB_TERM_SHIFT,  '_', '+', '{',
 
86
    /* According to usage table 0x31 should be mapped to '/'
 
87
       but testing with real keyboard shows that 0x32 is remapped to '/'.
 
88
       Map 0x31 to 0. 
 
89
    */
 
90
    /* 0x30 */ '}',  0,    '|',  ':',  '"', '~', '<', '>',
 
91
    /* 0x39 is CapsLock. Handled by driver.  */
 
92
    /* 0x38 */ '?',  0,
 
93
    /* 0x3a */ GRUB_TERM_KEY_F1 | GRUB_TERM_SHIFT,
 
94
    /* 0x3b */ GRUB_TERM_KEY_F2 | GRUB_TERM_SHIFT, 
 
95
    /* 0x3c */ GRUB_TERM_KEY_F3 | GRUB_TERM_SHIFT, 
 
96
    /* 0x3d */ GRUB_TERM_KEY_F4 | GRUB_TERM_SHIFT, 
 
97
    /* 0x3e */ GRUB_TERM_KEY_F5 | GRUB_TERM_SHIFT, 
 
98
    /* 0x3f */ GRUB_TERM_KEY_F6 | GRUB_TERM_SHIFT, 
 
99
    /* 0x40 */ GRUB_TERM_KEY_F7 | GRUB_TERM_SHIFT, 
 
100
    /* 0x41 */ GRUB_TERM_KEY_F8 | GRUB_TERM_SHIFT, 
 
101
    /* 0x42 */ GRUB_TERM_KEY_F9 | GRUB_TERM_SHIFT, 
 
102
    /* 0x43 */ GRUB_TERM_KEY_F10 | GRUB_TERM_SHIFT, 
 
103
    /* 0x44 */ GRUB_TERM_KEY_F11 | GRUB_TERM_SHIFT, 
 
104
    /* 0x45 */ GRUB_TERM_KEY_F12 | GRUB_TERM_SHIFT, 
 
105
    /* PrtScr and ScrollLock. Not handled yet.  */
 
106
    /* 0x46 */ 0,                    0,
 
107
    /* 0x48 is Pause. Not handled yet.  */
 
108
    /* 0x48 */ 0,                    GRUB_TERM_KEY_INSERT | GRUB_TERM_SHIFT, 
 
109
    /* 0x4a */ GRUB_TERM_KEY_HOME | GRUB_TERM_SHIFT,
 
110
    /* 0x4b */ GRUB_TERM_KEY_PPAGE | GRUB_TERM_SHIFT,
 
111
    /* 0x4c */ GRUB_TERM_KEY_DC | GRUB_TERM_SHIFT,
 
112
    /* 0x4d */ GRUB_TERM_KEY_END | GRUB_TERM_SHIFT,
 
113
    /* 0x4e */ GRUB_TERM_KEY_NPAGE | GRUB_TERM_SHIFT,
 
114
    /* 0x4f */ GRUB_TERM_KEY_RIGHT | GRUB_TERM_SHIFT,
 
115
    /* 0x50 */ GRUB_TERM_KEY_LEFT | GRUB_TERM_SHIFT,
 
116
    /* 0x51 */ GRUB_TERM_KEY_DOWN | GRUB_TERM_SHIFT,
 
117
    /* 0x53 is NumLock. Handled by driver.  */
 
118
    /* 0x52 */ GRUB_TERM_KEY_UP | GRUB_TERM_SHIFT,     0,
 
119
    /* 0x54 */ '/',                    '*', 
 
120
    /* 0x56 */ '-',                    '+',
 
121
    /* 0x58 */ '\n' | GRUB_TERM_SHIFT, '1', '2', '3', '4', '5','6', '7',
 
122
    /* 0x60 */ '8', '9', '0', '.', '|'
 
123
  }
 
124
};
 
125
 
 
126
static struct grub_keyboard_layout *grub_current_layout = &layout_us;
 
127
 
 
128
static int
 
129
map_key_core (int code, int status, int *alt_gr_consumed)
 
130
{
 
131
  *alt_gr_consumed = 0;
 
132
 
 
133
  if (status & GRUB_TERM_STATUS_RALT)
 
134
    {
 
135
      if (status & (GRUB_TERM_STATUS_LSHIFT | GRUB_TERM_STATUS_RSHIFT))
 
136
        {
 
137
          if (grub_current_layout->keyboard_map_shift_l3[code])
 
138
            {
 
139
              *alt_gr_consumed = 1;
 
140
              return grub_current_layout->keyboard_map_shift_l3[code];
 
141
            }
 
142
        }
 
143
      else if (grub_current_layout->keyboard_map_l3[code])
 
144
        {
 
145
          *alt_gr_consumed = 1;
 
146
          return grub_current_layout->keyboard_map_l3[code];  
 
147
        }
 
148
    }
 
149
  if (status & (GRUB_TERM_STATUS_LSHIFT | GRUB_TERM_STATUS_RSHIFT))
 
150
    return grub_current_layout->keyboard_map_shift[code];
 
151
  else
 
152
    return grub_current_layout->keyboard_map[code];
 
153
}
 
154
 
 
155
unsigned
 
156
grub_term_map_key (grub_keyboard_key_t code, int status)
 
157
{
 
158
  int alt_gr_consumed = 0;
 
159
  int key;
 
160
 
 
161
  if (code >= 0x59 && code <= 0x63 && (status & GRUB_TERM_STATUS_NUM))
 
162
    {
 
163
      if (status & (GRUB_TERM_STATUS_RSHIFT | GRUB_TERM_STATUS_LSHIFT))
 
164
        status &= ~(GRUB_TERM_STATUS_RSHIFT | GRUB_TERM_STATUS_LSHIFT);
 
165
      else
 
166
        status |= GRUB_TERM_STATUS_RSHIFT;
 
167
    }
 
168
 
 
169
  key = map_key_core (code, status, &alt_gr_consumed);
 
170
  
 
171
  if (key == 0 || key == GRUB_TERM_SHIFT)
 
172
    grub_printf ("Unknown key 0x%x detected\n", code);
 
173
  
 
174
  if (status & GRUB_TERM_STATUS_CAPS)
 
175
    {
 
176
      if ((key >= 'a') && (key <= 'z'))
 
177
        key += 'A' - 'a';
 
178
      else if ((key >= 'A') && (key <= 'Z'))
 
179
        key += 'a' - 'A';
 
180
    }
 
181
  
 
182
  if ((status & GRUB_TERM_STATUS_LALT) || 
 
183
      ((status & GRUB_TERM_STATUS_RALT) && !alt_gr_consumed))
 
184
    key |= GRUB_TERM_ALT;
 
185
  if (status & (GRUB_TERM_STATUS_LCTRL | GRUB_TERM_STATUS_RCTRL))
 
186
    key |= GRUB_TERM_CTRL;
 
187
 
 
188
  return key;
 
189
}
 
190
 
 
191
static grub_err_t
 
192
grub_cmd_keymap (struct grub_command *cmd __attribute__ ((unused)),
 
193
                 int argc, char *argv[])
 
194
{
 
195
  char *filename;
 
196
  grub_file_t file;
 
197
  grub_uint32_t version;
 
198
  grub_uint8_t magic[GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE];
 
199
  struct grub_keyboard_layout *newmap = NULL;
 
200
  unsigned i;
 
201
 
 
202
  if (argc < 1)
 
203
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "file or layout name required");
 
204
  if (argv[0][0] != '(' && argv[0][0] != '/' && argv[0][0] != '+')
 
205
    {
 
206
      const char *prefix = grub_env_get ("prefix");
 
207
      if (!prefix)
 
208
        return grub_error (GRUB_ERR_BAD_ARGUMENT, "No prefix set");     
 
209
      filename = grub_xasprintf ("%s/layouts/%s.gkb", prefix, argv[0]);
 
210
      if (!filename)
 
211
        return grub_errno;
 
212
    }
 
213
  else
 
214
    filename = argv[0];
 
215
 
 
216
  file = grub_file_open (filename);
 
217
  if (! file)
 
218
    goto fail;
 
219
 
 
220
  if (grub_file_read (file, magic, sizeof (magic)) != sizeof (magic))
 
221
    {
 
222
      if (!grub_errno)
 
223
        grub_error (GRUB_ERR_BAD_ARGUMENT, "file is too short");
 
224
      goto fail;
 
225
    }
 
226
 
 
227
  if (grub_memcmp (magic, GRUB_KEYBOARD_LAYOUTS_FILEMAGIC,
 
228
                   GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE) != 0)
 
229
    {
 
230
      grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid magic");
 
231
      goto fail;
 
232
    }
 
233
 
 
234
  if (grub_file_read (file, &version, sizeof (version)) != sizeof (version))
 
235
    {
 
236
      if (!grub_errno)
 
237
        grub_error (GRUB_ERR_BAD_ARGUMENT, "file is too short");
 
238
      goto fail;
 
239
    }
 
240
 
 
241
  if (grub_le_to_cpu32 (version) != GRUB_KEYBOARD_LAYOUTS_VERSION)
 
242
    {
 
243
      grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid version");
 
244
      goto fail;
 
245
    }
 
246
 
 
247
  newmap = grub_malloc (sizeof (*newmap));
 
248
  if (!newmap)
 
249
    goto fail;
 
250
 
 
251
  if (grub_file_read (file, newmap, sizeof (*newmap)) != sizeof (*newmap))
 
252
    {
 
253
      if (!grub_errno)
 
254
        grub_error (GRUB_ERR_BAD_ARGUMENT, "file is too short");
 
255
      goto fail;
 
256
    }
 
257
 
 
258
  for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map); i++)
 
259
    newmap->keyboard_map[i] = grub_le_to_cpu32(newmap->keyboard_map[i]);
 
260
 
 
261
  for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map_shift); i++)
 
262
    newmap->keyboard_map_shift[i]
 
263
      = grub_le_to_cpu32(newmap->keyboard_map_shift[i]);
 
264
 
 
265
  for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map_l3); i++)
 
266
    newmap->keyboard_map_l3[i]
 
267
      = grub_le_to_cpu32(newmap->keyboard_map_l3[i]);
 
268
 
 
269
  for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map_shift_l3); i++)
 
270
    newmap->keyboard_map_shift_l3[i]
 
271
      = grub_le_to_cpu32(newmap->keyboard_map_shift_l3[i]);
 
272
 
 
273
  grub_current_layout = newmap;
 
274
 
 
275
  return GRUB_ERR_NONE;
 
276
 
 
277
 fail:
 
278
  if (filename != argv[0])
 
279
    grub_free (filename);
 
280
  grub_free (newmap);
 
281
  if (file)
 
282
    grub_file_close (file);
 
283
  return grub_errno;
 
284
}
 
285
 
 
286
static grub_command_t cmd;
 
287
 
 
288
GRUB_MOD_INIT(keylayouts)
 
289
{
 
290
  cmd = grub_register_command ("keymap", grub_cmd_keymap,
 
291
                               0, N_("Load a keyboard layout."));
 
292
}
 
293
 
 
294
GRUB_MOD_FINI(keylayouts)
 
295
{
 
296
  grub_unregister_command (cmd);
 
297
}