2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010 Free Software Foundation, Inc.
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.
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.
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/>.
21 #include <grub/util/misc.h>
22 #include <grub/i18n.h>
23 #include <grub/term.h>
24 #include <grub/keyboard_layouts.h>
35 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
36 #pragma GCC diagnostic ignored "-Wmissing-declarations"
39 #pragma GCC diagnostic error "-Wmissing-prototypes"
40 #pragma GCC diagnostic error "-Wmissing-declarations"
51 static struct argp_option options
[] = {
52 {"input", 'i', N_("FILE"), 0,
53 N_("set input filename. Default is STDIN"), 0},
54 {"output", 'o', N_("FILE"), 0,
55 N_("set output filename. Default is STDOUT"), 0},
56 {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
60 struct console_grub_equivalence
66 static struct console_grub_equivalence console_grub_equivalences_shift
[] = {
82 static struct console_grub_equivalence console_grub_equivalences_unshift
[] = {
83 {"KP_0", GRUB_TERM_KEY_INSERT
},
84 {"KP_1", GRUB_TERM_KEY_END
},
85 {"KP_2", GRUB_TERM_KEY_DOWN
},
86 {"KP_3", GRUB_TERM_KEY_NPAGE
},
87 {"KP_4", GRUB_TERM_KEY_LEFT
},
88 {"KP_5", GRUB_TERM_KEY_CENTER
},
89 {"KP_6", GRUB_TERM_KEY_RIGHT
},
90 {"KP_7", GRUB_TERM_KEY_HOME
},
91 {"KP_8", GRUB_TERM_KEY_UP
},
92 {"KP_9", GRUB_TERM_KEY_PPAGE
},
93 {"KP_Period", GRUB_TERM_KEY_DC
},
98 static struct console_grub_equivalence console_grub_equivalences_common
[] = {
99 {"Escape", GRUB_TERM_ESC
},
100 {"Tab", GRUB_TERM_TAB
},
101 {"Delete", GRUB_TERM_BACKSPACE
},
106 {"KP_Multiply", '*'},
107 {"KP_Subtract", '-'},
111 {"F1", GRUB_TERM_KEY_F1
},
112 {"F2", GRUB_TERM_KEY_F2
},
113 {"F3", GRUB_TERM_KEY_F3
},
114 {"F4", GRUB_TERM_KEY_F4
},
115 {"F5", GRUB_TERM_KEY_F5
},
116 {"F6", GRUB_TERM_KEY_F6
},
117 {"F7", GRUB_TERM_KEY_F7
},
118 {"F8", GRUB_TERM_KEY_F8
},
119 {"F9", GRUB_TERM_KEY_F9
},
120 {"F10", GRUB_TERM_KEY_F10
},
121 {"F11", GRUB_TERM_KEY_F11
},
122 {"F12", GRUB_TERM_KEY_F12
},
123 {"F13", GRUB_TERM_KEY_F1
| GRUB_TERM_SHIFT
},
124 {"F14", GRUB_TERM_KEY_F2
| GRUB_TERM_SHIFT
},
125 {"F15", GRUB_TERM_KEY_F3
| GRUB_TERM_SHIFT
},
126 {"F16", GRUB_TERM_KEY_F4
| GRUB_TERM_SHIFT
},
127 {"F17", GRUB_TERM_KEY_F5
| GRUB_TERM_SHIFT
},
128 {"F18", GRUB_TERM_KEY_F6
| GRUB_TERM_SHIFT
},
129 {"F19", GRUB_TERM_KEY_F7
| GRUB_TERM_SHIFT
},
130 {"F20", GRUB_TERM_KEY_F8
| GRUB_TERM_SHIFT
},
131 {"F21", GRUB_TERM_KEY_F9
| GRUB_TERM_SHIFT
},
132 {"F22", GRUB_TERM_KEY_F10
| GRUB_TERM_SHIFT
},
133 {"F23", GRUB_TERM_KEY_F11
| GRUB_TERM_SHIFT
},
134 {"F24", GRUB_TERM_KEY_F12
| GRUB_TERM_SHIFT
},
135 {"Console_13", GRUB_TERM_KEY_F1
| GRUB_TERM_ALT
},
136 {"Console_14", GRUB_TERM_KEY_F2
| GRUB_TERM_ALT
},
137 {"Console_15", GRUB_TERM_KEY_F3
| GRUB_TERM_ALT
},
138 {"Console_16", GRUB_TERM_KEY_F4
| GRUB_TERM_ALT
},
139 {"Console_17", GRUB_TERM_KEY_F5
| GRUB_TERM_ALT
},
140 {"Console_18", GRUB_TERM_KEY_F6
| GRUB_TERM_ALT
},
141 {"Console_19", GRUB_TERM_KEY_F7
| GRUB_TERM_ALT
},
142 {"Console_20", GRUB_TERM_KEY_F8
| GRUB_TERM_ALT
},
143 {"Console_21", GRUB_TERM_KEY_F9
| GRUB_TERM_ALT
},
144 {"Console_22", GRUB_TERM_KEY_F10
| GRUB_TERM_ALT
},
145 {"Console_23", GRUB_TERM_KEY_F11
| GRUB_TERM_ALT
},
146 {"Console_24", GRUB_TERM_KEY_F12
| GRUB_TERM_ALT
},
147 {"Console_25", GRUB_TERM_KEY_F1
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
148 {"Console_26", GRUB_TERM_KEY_F2
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
149 {"Console_27", GRUB_TERM_KEY_F3
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
150 {"Console_28", GRUB_TERM_KEY_F4
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
151 {"Console_29", GRUB_TERM_KEY_F5
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
152 {"Console_30", GRUB_TERM_KEY_F6
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
153 {"Console_31", GRUB_TERM_KEY_F7
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
154 {"Console_32", GRUB_TERM_KEY_F8
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
155 {"Console_33", GRUB_TERM_KEY_F9
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
156 {"Console_34", GRUB_TERM_KEY_F10
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
157 {"Console_35", GRUB_TERM_KEY_F11
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
158 {"Console_36", GRUB_TERM_KEY_F12
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
160 {"Insert", GRUB_TERM_KEY_INSERT
},
161 {"Down", GRUB_TERM_KEY_DOWN
},
162 {"Up", GRUB_TERM_KEY_UP
},
163 {"Home", GRUB_TERM_KEY_HOME
},
164 {"End", GRUB_TERM_KEY_END
},
165 {"Right", GRUB_TERM_KEY_RIGHT
},
166 {"Left", GRUB_TERM_KEY_LEFT
},
167 {"Next", GRUB_TERM_KEY_NPAGE
},
168 {"Prior", GRUB_TERM_KEY_PPAGE
},
169 {"Remove", GRUB_TERM_KEY_DC
},
172 /* "Undead" keys since no dead key support in GRUB. */
173 {"dead_acute", '\''},
174 {"dead_circumflex", '^'},
177 {"dead_diaeresis", '"'},
179 /* Following ones don't provide any useful symbols for shell. */
184 {"dead_doubleacute", 0},
186 /* Unused in GRUB. */
188 {"Scroll_Forward", 0},
189 {"Scroll_Backward", 0},
208 {"Show_Registers", 0},
209 {"Control_backslash", 0},
215 static grub_uint8_t linux_to_usb_map
[128] = {
216 /* 0x00 */ 0 /* Unused */, GRUB_KEYBOARD_KEY_ESCAPE
,
217 /* 0x02 */ GRUB_KEYBOARD_KEY_1
, GRUB_KEYBOARD_KEY_2
,
218 /* 0x04 */ GRUB_KEYBOARD_KEY_3
, GRUB_KEYBOARD_KEY_4
,
219 /* 0x06 */ GRUB_KEYBOARD_KEY_5
, GRUB_KEYBOARD_KEY_6
,
220 /* 0x08 */ GRUB_KEYBOARD_KEY_7
, GRUB_KEYBOARD_KEY_8
,
221 /* 0x0a */ GRUB_KEYBOARD_KEY_9
, GRUB_KEYBOARD_KEY_0
,
222 /* 0x0c */ GRUB_KEYBOARD_KEY_DASH
, GRUB_KEYBOARD_KEY_EQUAL
,
223 /* 0x0e */ GRUB_KEYBOARD_KEY_BACKSPACE
, GRUB_KEYBOARD_KEY_TAB
,
224 /* 0x10 */ GRUB_KEYBOARD_KEY_Q
, GRUB_KEYBOARD_KEY_W
,
225 /* 0x12 */ GRUB_KEYBOARD_KEY_E
, GRUB_KEYBOARD_KEY_R
,
226 /* 0x14 */ GRUB_KEYBOARD_KEY_T
, GRUB_KEYBOARD_KEY_Y
,
227 /* 0x16 */ GRUB_KEYBOARD_KEY_U
, GRUB_KEYBOARD_KEY_I
,
228 /* 0x18 */ GRUB_KEYBOARD_KEY_O
, GRUB_KEYBOARD_KEY_P
,
229 /* 0x1a */ GRUB_KEYBOARD_KEY_LBRACKET
, GRUB_KEYBOARD_KEY_RBRACKET
,
230 /* 0x1c */ GRUB_KEYBOARD_KEY_ENTER
, GRUB_KEYBOARD_KEY_LEFT_CTRL
,
231 /* 0x1e */ GRUB_KEYBOARD_KEY_A
, GRUB_KEYBOARD_KEY_S
,
232 /* 0x20 */ GRUB_KEYBOARD_KEY_D
, GRUB_KEYBOARD_KEY_F
,
233 /* 0x22 */ GRUB_KEYBOARD_KEY_G
, GRUB_KEYBOARD_KEY_H
,
234 /* 0x24 */ GRUB_KEYBOARD_KEY_J
, GRUB_KEYBOARD_KEY_K
,
235 /* 0x26 */ GRUB_KEYBOARD_KEY_L
, GRUB_KEYBOARD_KEY_SEMICOLON
,
236 /* 0x28 */ GRUB_KEYBOARD_KEY_DQUOTE
, GRUB_KEYBOARD_KEY_RQUOTE
,
237 /* 0x2a */ GRUB_KEYBOARD_KEY_LEFT_SHIFT
, GRUB_KEYBOARD_KEY_BACKSLASH
,
238 /* 0x2c */ GRUB_KEYBOARD_KEY_Z
, GRUB_KEYBOARD_KEY_X
,
239 /* 0x2e */ GRUB_KEYBOARD_KEY_C
, GRUB_KEYBOARD_KEY_V
,
240 /* 0x30 */ GRUB_KEYBOARD_KEY_B
, GRUB_KEYBOARD_KEY_N
,
241 /* 0x32 */ GRUB_KEYBOARD_KEY_M
, GRUB_KEYBOARD_KEY_COMMA
,
242 /* 0x34 */ GRUB_KEYBOARD_KEY_DOT
, GRUB_KEYBOARD_KEY_SLASH
,
243 /* 0x36 */ GRUB_KEYBOARD_KEY_RIGHT_SHIFT
, GRUB_KEYBOARD_KEY_NUMMUL
,
244 /* 0x38 */ GRUB_KEYBOARD_KEY_LEFT_ALT
, GRUB_KEYBOARD_KEY_SPACE
,
245 /* 0x3a */ GRUB_KEYBOARD_KEY_CAPS_LOCK
, GRUB_KEYBOARD_KEY_F1
,
246 /* 0x3c */ GRUB_KEYBOARD_KEY_F2
, GRUB_KEYBOARD_KEY_F3
,
247 /* 0x3e */ GRUB_KEYBOARD_KEY_F4
, GRUB_KEYBOARD_KEY_F5
,
248 /* 0x40 */ GRUB_KEYBOARD_KEY_F6
, GRUB_KEYBOARD_KEY_F7
,
249 /* 0x42 */ GRUB_KEYBOARD_KEY_F8
, GRUB_KEYBOARD_KEY_F9
,
250 /* 0x44 */ GRUB_KEYBOARD_KEY_F10
, GRUB_KEYBOARD_KEY_NUM_LOCK
,
251 /* 0x46 */ GRUB_KEYBOARD_KEY_SCROLL_LOCK
, GRUB_KEYBOARD_KEY_NUM7
,
252 /* 0x48 */ GRUB_KEYBOARD_KEY_NUM8
, GRUB_KEYBOARD_KEY_NUM9
,
253 /* 0x4a */ GRUB_KEYBOARD_KEY_NUMMINUS
, GRUB_KEYBOARD_KEY_NUM4
,
254 /* 0x4c */ GRUB_KEYBOARD_KEY_NUM5
, GRUB_KEYBOARD_KEY_NUM6
,
255 /* 0x4e */ GRUB_KEYBOARD_KEY_NUMPLUS
, GRUB_KEYBOARD_KEY_NUM1
,
256 /* 0x50 */ GRUB_KEYBOARD_KEY_NUM2
, GRUB_KEYBOARD_KEY_NUM3
,
257 /* 0x52 */ GRUB_KEYBOARD_KEY_NUMDOT
, GRUB_KEYBOARD_KEY_NUMDOT
,
259 /* 0x56 */ GRUB_KEYBOARD_KEY_102ND
, GRUB_KEYBOARD_KEY_F11
,
260 /* 0x58 */ GRUB_KEYBOARD_KEY_F12
, GRUB_KEYBOARD_KEY_JP_RO
,
264 /* 0x60 */ GRUB_KEYBOARD_KEY_NUMENTER
, GRUB_KEYBOARD_KEY_RIGHT_CTRL
,
265 /* 0x62 */ GRUB_KEYBOARD_KEY_NUMSLASH
, 0,
266 /* 0x64 */ GRUB_KEYBOARD_KEY_RIGHT_ALT
, 0,
267 /* 0x66 */ GRUB_KEYBOARD_KEY_HOME
, GRUB_KEYBOARD_KEY_UP
,
268 /* 0x68 */ GRUB_KEYBOARD_KEY_PPAGE
, GRUB_KEYBOARD_KEY_LEFT
,
269 /* 0x6a */ GRUB_KEYBOARD_KEY_RIGHT
, GRUB_KEYBOARD_KEY_END
,
270 /* 0x6c */ GRUB_KEYBOARD_KEY_DOWN
, GRUB_KEYBOARD_KEY_NPAGE
,
271 /* 0x6e */ GRUB_KEYBOARD_KEY_INSERT
, GRUB_KEYBOARD_KEY_DELETE
,
273 /* 0x72 */ 0, GRUB_KEYBOARD_KEY_JP_RO
,
276 /* 0x78 */ 0, GRUB_KEYBOARD_KEY_KPCOMMA
,
278 /* 0x7c */ GRUB_KEYBOARD_KEY_JP_YEN
,
282 add_special_keys (struct grub_keyboard_layout
*layout
)
288 lookup (char *code
, int shift
)
291 struct console_grub_equivalence
*pr
;
294 pr
= console_grub_equivalences_shift
;
296 pr
= console_grub_equivalences_unshift
;
298 for (i
= 0; pr
[i
].layout
!= NULL
; i
++)
299 if (strcmp (code
, pr
[i
].layout
) == 0)
302 for (i
= 0; console_grub_equivalences_common
[i
].layout
!= NULL
; i
++)
303 if (strcmp (code
, console_grub_equivalences_common
[i
].layout
) == 0)
304 return console_grub_equivalences_common
[i
].grub
;
306 /* TRANSLATORS: scan identifier is keyboard key symbolic name. */
307 fprintf (stderr
, _("Unknown keyboard scan identifier %s\n"), code
);
313 get_grub_code (char *layout_code
, int shift
)
317 if (strncmp (layout_code
, "U+", sizeof ("U+") - 1) == 0)
318 sscanf (layout_code
, "U+%x", &code
);
319 else if (strncmp (layout_code
, "+U+", sizeof ("+U+") - 1) == 0)
320 sscanf (layout_code
, "+U+%x", &code
);
322 code
= lookup (layout_code
, shift
);
327 write_file (FILE *out
, const char *fname
, struct grub_keyboard_layout
*layout
)
329 grub_uint32_t version
;
332 version
= grub_cpu_to_le32_compile_time (GRUB_KEYBOARD_LAYOUTS_VERSION
);
334 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map
); i
++)
335 layout
->keyboard_map
[i
] = grub_cpu_to_le32(layout
->keyboard_map
[i
]);
337 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map_shift
); i
++)
338 layout
->keyboard_map_shift
[i
]
339 = grub_cpu_to_le32(layout
->keyboard_map_shift
[i
]);
341 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map_l3
); i
++)
342 layout
->keyboard_map_l3
[i
]
343 = grub_cpu_to_le32(layout
->keyboard_map_l3
[i
]);
345 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map_shift_l3
); i
++)
346 layout
->keyboard_map_shift_l3
[i
]
347 = grub_cpu_to_le32(layout
->keyboard_map_shift_l3
[i
]);
349 if (fwrite (GRUB_KEYBOARD_LAYOUTS_FILEMAGIC
, 1,
350 GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE
, out
)
351 != GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE
352 || fwrite (&version
, sizeof (version
), 1, out
) != 1
353 || fwrite (layout
, 1, sizeof (*layout
), out
) != sizeof (*layout
))
356 grub_util_error ("cannot write to `%s': %s", fname
, strerror (errno
));
358 grub_util_error ("cannot write to the stdout: %s", strerror (errno
));
363 write_keymaps (FILE *in
, FILE *out
, const char *out_filename
)
365 struct grub_keyboard_layout layout
;
369 memset (&layout
, 0, sizeof (layout
));
371 /* Process the ckbcomp output and prepare the layouts. */
373 while (fgets (line
, sizeof (line
), in
))
375 if (strncmp (line
, "keycode", sizeof ("keycode") - 1) == 0)
377 unsigned keycode_linux
;
378 unsigned keycode_usb
;
384 sscanf (line
, "keycode %u = %60s %60s %60s %60s", &keycode_linux
,
385 normal
, shift
, normalalt
, shiftalt
);
388 if (keycode_linux
== 0x77 /* Pause */
389 /* Some obscure keys */
390 || keycode_linux
== 0x63 || keycode_linux
== 0x7d
391 || keycode_linux
== 0x7e)
394 /* Not remappable. */
395 if (keycode_linux
== 0x1d /* Left CTRL */
396 || keycode_linux
== 0x61 /* Right CTRL */
397 || keycode_linux
== 0x2a /* Left Shift. */
398 || keycode_linux
== 0x36 /* Right Shift. */
399 || keycode_linux
== 0x38 /* Left ALT. */
400 || keycode_linux
== 0x64 /* Right ALT. */
401 || keycode_linux
== 0x3a /* CapsLock. */
402 || keycode_linux
== 0x45 /* NumLock. */
403 || keycode_linux
== 0x46 /* ScrollLock. */)
406 keycode_usb
= linux_to_usb_map
[keycode_linux
];
408 || keycode_usb
>= GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE
)
410 /* TRANSLATORS: scan code is keyboard key numeric identifier. */
411 fprintf (stderr
, _("Unknown keyboard scan code 0x%02x\n"), keycode_linux
);
414 if (keycode_usb
< GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE
)
416 layout
.keyboard_map
[keycode_usb
] = get_grub_code (normal
, 0);
417 layout
.keyboard_map_shift
[keycode_usb
] = get_grub_code (shift
, 1);
418 layout
.keyboard_map_l3
[keycode_usb
]
419 = get_grub_code (normalalt
, 0);
420 layout
.keyboard_map_shift_l3
[keycode_usb
]
421 = get_grub_code (shiftalt
, 1);
429 /* TRANSLATORS: this error is triggered when input doesn't contain any
431 fprintf (stderr
, "%s", _("ERROR: no valid keyboard layout found. Check the input.\n"));
435 add_special_keys (&layout
);
437 write_file (out
, out_filename
, &layout
);
441 argp_parser (int key
, char *arg
, struct argp_state
*state
)
443 /* Get the input argument from argp_parse, which we
444 know is a pointer to our arguments structure. */
445 struct arguments
*arguments
= state
->input
;
450 arguments
->input
= xstrdup (arg
);
454 arguments
->output
= xstrdup (arg
);
458 arguments
->verbosity
++;
462 return ARGP_ERR_UNKNOWN
;
468 static struct argp argp
= {
469 options
, argp_parser
, N_("[OPTIONS]"),
470 /* TRANSLATORS: "one" is a shortcut for "keyboard layout". */
471 N_("Generate GRUB keyboard layout from Linux console one."),
476 main (int argc
, char *argv
[])
479 struct arguments arguments
;
481 grub_util_host_init (&argc
, &argv
);
483 /* Check for options. */
484 memset (&arguments
, 0, sizeof (struct arguments
));
485 if (argp_parse (&argp
, argc
, argv
, 0, 0, &arguments
) != 0)
487 fprintf (stderr
, "%s", _("Error in parsing command line arguments\n"));
492 in
= grub_util_fopen (arguments
.input
, "r");
497 grub_util_error (_("cannot open `%s': %s"), arguments
.input
? : "stdin",
500 if (arguments
.output
)
501 out
= grub_util_fopen (arguments
.output
, "wb");
509 grub_util_error (_("cannot open `%s': %s"), arguments
.output
? : "stdout",
513 write_keymaps (in
, out
, arguments
.output
);