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>
44 static struct argp_option options
[] = {
45 {"input", 'i', N_("FILE"), 0,
46 N_("set input filename. Default is STDIN"), 0},
47 {"output", 'o', N_("FILE"), 0,
48 N_("set output filename. Default is STDOUT"), 0},
49 {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
53 struct console_grub_equivalence
59 static struct console_grub_equivalence console_grub_equivalences_shift
[] = {
75 static struct console_grub_equivalence console_grub_equivalences_unshift
[] = {
76 {"KP_0", GRUB_TERM_KEY_INSERT
},
77 {"KP_1", GRUB_TERM_KEY_END
},
78 {"KP_2", GRUB_TERM_KEY_DOWN
},
79 {"KP_3", GRUB_TERM_KEY_NPAGE
},
80 {"KP_4", GRUB_TERM_KEY_LEFT
},
81 {"KP_5", GRUB_TERM_KEY_CENTER
},
82 {"KP_6", GRUB_TERM_KEY_RIGHT
},
83 {"KP_7", GRUB_TERM_KEY_HOME
},
84 {"KP_8", GRUB_TERM_KEY_UP
},
85 {"KP_9", GRUB_TERM_KEY_PPAGE
},
86 {"KP_Period", GRUB_TERM_KEY_DC
},
91 static struct console_grub_equivalence console_grub_equivalences_common
[] = {
92 {"Escape", GRUB_TERM_ESC
},
93 {"Tab", GRUB_TERM_TAB
},
94 {"Delete", GRUB_TERM_BACKSPACE
},
100 {"KP_Subtract", '-'},
104 {"F1", GRUB_TERM_KEY_F1
},
105 {"F2", GRUB_TERM_KEY_F2
},
106 {"F3", GRUB_TERM_KEY_F3
},
107 {"F4", GRUB_TERM_KEY_F4
},
108 {"F5", GRUB_TERM_KEY_F5
},
109 {"F6", GRUB_TERM_KEY_F6
},
110 {"F7", GRUB_TERM_KEY_F7
},
111 {"F8", GRUB_TERM_KEY_F8
},
112 {"F9", GRUB_TERM_KEY_F9
},
113 {"F10", GRUB_TERM_KEY_F10
},
114 {"F11", GRUB_TERM_KEY_F11
},
115 {"F12", GRUB_TERM_KEY_F12
},
116 {"F13", GRUB_TERM_KEY_F1
| GRUB_TERM_SHIFT
},
117 {"F14", GRUB_TERM_KEY_F2
| GRUB_TERM_SHIFT
},
118 {"F15", GRUB_TERM_KEY_F3
| GRUB_TERM_SHIFT
},
119 {"F16", GRUB_TERM_KEY_F4
| GRUB_TERM_SHIFT
},
120 {"F17", GRUB_TERM_KEY_F5
| GRUB_TERM_SHIFT
},
121 {"F18", GRUB_TERM_KEY_F6
| GRUB_TERM_SHIFT
},
122 {"F19", GRUB_TERM_KEY_F7
| GRUB_TERM_SHIFT
},
123 {"F20", GRUB_TERM_KEY_F8
| GRUB_TERM_SHIFT
},
124 {"F21", GRUB_TERM_KEY_F9
| GRUB_TERM_SHIFT
},
125 {"F22", GRUB_TERM_KEY_F10
| GRUB_TERM_SHIFT
},
126 {"F23", GRUB_TERM_KEY_F11
| GRUB_TERM_SHIFT
},
127 {"F24", GRUB_TERM_KEY_F12
| GRUB_TERM_SHIFT
},
128 {"Console_13", GRUB_TERM_KEY_F1
| GRUB_TERM_ALT
},
129 {"Console_14", GRUB_TERM_KEY_F2
| GRUB_TERM_ALT
},
130 {"Console_15", GRUB_TERM_KEY_F3
| GRUB_TERM_ALT
},
131 {"Console_16", GRUB_TERM_KEY_F4
| GRUB_TERM_ALT
},
132 {"Console_17", GRUB_TERM_KEY_F5
| GRUB_TERM_ALT
},
133 {"Console_18", GRUB_TERM_KEY_F6
| GRUB_TERM_ALT
},
134 {"Console_19", GRUB_TERM_KEY_F7
| GRUB_TERM_ALT
},
135 {"Console_20", GRUB_TERM_KEY_F8
| GRUB_TERM_ALT
},
136 {"Console_21", GRUB_TERM_KEY_F9
| GRUB_TERM_ALT
},
137 {"Console_22", GRUB_TERM_KEY_F10
| GRUB_TERM_ALT
},
138 {"Console_23", GRUB_TERM_KEY_F11
| GRUB_TERM_ALT
},
139 {"Console_24", GRUB_TERM_KEY_F12
| GRUB_TERM_ALT
},
140 {"Console_25", GRUB_TERM_KEY_F1
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
141 {"Console_26", GRUB_TERM_KEY_F2
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
142 {"Console_27", GRUB_TERM_KEY_F3
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
143 {"Console_28", GRUB_TERM_KEY_F4
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
144 {"Console_29", GRUB_TERM_KEY_F5
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
145 {"Console_30", GRUB_TERM_KEY_F6
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
146 {"Console_31", GRUB_TERM_KEY_F7
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
147 {"Console_32", GRUB_TERM_KEY_F8
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
148 {"Console_33", GRUB_TERM_KEY_F9
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
149 {"Console_34", GRUB_TERM_KEY_F10
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
150 {"Console_35", GRUB_TERM_KEY_F11
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
151 {"Console_36", GRUB_TERM_KEY_F12
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
153 {"Insert", GRUB_TERM_KEY_INSERT
},
154 {"Down", GRUB_TERM_KEY_DOWN
},
155 {"Up", GRUB_TERM_KEY_UP
},
156 {"Home", GRUB_TERM_KEY_HOME
},
157 {"End", GRUB_TERM_KEY_END
},
158 {"Right", GRUB_TERM_KEY_RIGHT
},
159 {"Left", GRUB_TERM_KEY_LEFT
},
160 {"Next", GRUB_TERM_KEY_NPAGE
},
161 {"Prior", GRUB_TERM_KEY_PPAGE
},
162 {"Remove", GRUB_TERM_KEY_DC
},
165 /* "Undead" keys since no dead key support in GRUB. */
166 {"dead_acute", '\''},
167 {"dead_circumflex", '^'},
170 {"dead_diaeresis", '"'},
172 /* Following ones don't provide any useful symbols for shell. */
177 {"dead_doubleacute", 0},
179 /* Unused in GRUB. */
181 {"Scroll_Forward", 0},
182 {"Scroll_Backward", 0},
201 {"Show_Registers", 0},
202 {"Control_backslash", 0},
208 static grub_uint8_t linux_to_usb_map
[128] = {
209 /* 0x00 */ 0 /* Unused */, GRUB_KEYBOARD_KEY_ESCAPE
,
210 /* 0x02 */ GRUB_KEYBOARD_KEY_1
, GRUB_KEYBOARD_KEY_2
,
211 /* 0x04 */ GRUB_KEYBOARD_KEY_3
, GRUB_KEYBOARD_KEY_4
,
212 /* 0x06 */ GRUB_KEYBOARD_KEY_5
, GRUB_KEYBOARD_KEY_6
,
213 /* 0x08 */ GRUB_KEYBOARD_KEY_7
, GRUB_KEYBOARD_KEY_8
,
214 /* 0x0a */ GRUB_KEYBOARD_KEY_9
, GRUB_KEYBOARD_KEY_0
,
215 /* 0x0c */ GRUB_KEYBOARD_KEY_DASH
, GRUB_KEYBOARD_KEY_EQUAL
,
216 /* 0x0e */ GRUB_KEYBOARD_KEY_BACKSPACE
, GRUB_KEYBOARD_KEY_TAB
,
217 /* 0x10 */ GRUB_KEYBOARD_KEY_Q
, GRUB_KEYBOARD_KEY_W
,
218 /* 0x12 */ GRUB_KEYBOARD_KEY_E
, GRUB_KEYBOARD_KEY_R
,
219 /* 0x14 */ GRUB_KEYBOARD_KEY_T
, GRUB_KEYBOARD_KEY_Y
,
220 /* 0x16 */ GRUB_KEYBOARD_KEY_U
, GRUB_KEYBOARD_KEY_I
,
221 /* 0x18 */ GRUB_KEYBOARD_KEY_O
, GRUB_KEYBOARD_KEY_P
,
222 /* 0x1a */ GRUB_KEYBOARD_KEY_LBRACKET
, GRUB_KEYBOARD_KEY_RBRACKET
,
223 /* 0x1c */ GRUB_KEYBOARD_KEY_ENTER
, GRUB_KEYBOARD_KEY_LEFT_CTRL
,
224 /* 0x1e */ GRUB_KEYBOARD_KEY_A
, GRUB_KEYBOARD_KEY_S
,
225 /* 0x20 */ GRUB_KEYBOARD_KEY_D
, GRUB_KEYBOARD_KEY_F
,
226 /* 0x22 */ GRUB_KEYBOARD_KEY_G
, GRUB_KEYBOARD_KEY_H
,
227 /* 0x24 */ GRUB_KEYBOARD_KEY_J
, GRUB_KEYBOARD_KEY_K
,
228 /* 0x26 */ GRUB_KEYBOARD_KEY_L
, GRUB_KEYBOARD_KEY_SEMICOLON
,
229 /* 0x28 */ GRUB_KEYBOARD_KEY_DQUOTE
, GRUB_KEYBOARD_KEY_RQUOTE
,
230 /* 0x2a */ GRUB_KEYBOARD_KEY_LEFT_SHIFT
, GRUB_KEYBOARD_KEY_BACKSLASH
,
231 /* 0x2c */ GRUB_KEYBOARD_KEY_Z
, GRUB_KEYBOARD_KEY_X
,
232 /* 0x2e */ GRUB_KEYBOARD_KEY_C
, GRUB_KEYBOARD_KEY_V
,
233 /* 0x30 */ GRUB_KEYBOARD_KEY_B
, GRUB_KEYBOARD_KEY_N
,
234 /* 0x32 */ GRUB_KEYBOARD_KEY_M
, GRUB_KEYBOARD_KEY_COMMA
,
235 /* 0x34 */ GRUB_KEYBOARD_KEY_DOT
, GRUB_KEYBOARD_KEY_SLASH
,
236 /* 0x36 */ GRUB_KEYBOARD_KEY_RIGHT_SHIFT
, GRUB_KEYBOARD_KEY_NUMMUL
,
237 /* 0x38 */ GRUB_KEYBOARD_KEY_LEFT_ALT
, GRUB_KEYBOARD_KEY_SPACE
,
238 /* 0x3a */ GRUB_KEYBOARD_KEY_CAPS_LOCK
, GRUB_KEYBOARD_KEY_F1
,
239 /* 0x3c */ GRUB_KEYBOARD_KEY_F2
, GRUB_KEYBOARD_KEY_F3
,
240 /* 0x3e */ GRUB_KEYBOARD_KEY_F4
, GRUB_KEYBOARD_KEY_F5
,
241 /* 0x40 */ GRUB_KEYBOARD_KEY_F6
, GRUB_KEYBOARD_KEY_F7
,
242 /* 0x42 */ GRUB_KEYBOARD_KEY_F8
, GRUB_KEYBOARD_KEY_F9
,
243 /* 0x44 */ GRUB_KEYBOARD_KEY_F10
, GRUB_KEYBOARD_KEY_NUM_LOCK
,
244 /* 0x46 */ GRUB_KEYBOARD_KEY_SCROLL_LOCK
, GRUB_KEYBOARD_KEY_NUM7
,
245 /* 0x48 */ GRUB_KEYBOARD_KEY_NUM8
, GRUB_KEYBOARD_KEY_NUM9
,
246 /* 0x4a */ GRUB_KEYBOARD_KEY_NUMMINUS
, GRUB_KEYBOARD_KEY_NUM4
,
247 /* 0x4c */ GRUB_KEYBOARD_KEY_NUM5
, GRUB_KEYBOARD_KEY_NUM6
,
248 /* 0x4e */ GRUB_KEYBOARD_KEY_NUMPLUS
, GRUB_KEYBOARD_KEY_NUM1
,
249 /* 0x50 */ GRUB_KEYBOARD_KEY_NUM2
, GRUB_KEYBOARD_KEY_NUM3
,
250 /* 0x52 */ GRUB_KEYBOARD_KEY_NUMDOT
, GRUB_KEYBOARD_KEY_NUMDOT
,
252 /* 0x56 */ GRUB_KEYBOARD_KEY_102ND
, GRUB_KEYBOARD_KEY_F11
,
253 /* 0x58 */ GRUB_KEYBOARD_KEY_F12
, GRUB_KEYBOARD_KEY_JP_RO
,
257 /* 0x60 */ GRUB_KEYBOARD_KEY_NUMENTER
, GRUB_KEYBOARD_KEY_RIGHT_CTRL
,
258 /* 0x62 */ GRUB_KEYBOARD_KEY_NUMSLASH
, 0,
259 /* 0x64 */ GRUB_KEYBOARD_KEY_RIGHT_ALT
, 0,
260 /* 0x66 */ GRUB_KEYBOARD_KEY_HOME
, GRUB_KEYBOARD_KEY_UP
,
261 /* 0x68 */ GRUB_KEYBOARD_KEY_PPAGE
, GRUB_KEYBOARD_KEY_LEFT
,
262 /* 0x6a */ GRUB_KEYBOARD_KEY_RIGHT
, GRUB_KEYBOARD_KEY_END
,
263 /* 0x6c */ GRUB_KEYBOARD_KEY_DOWN
, GRUB_KEYBOARD_KEY_NPAGE
,
264 /* 0x6e */ GRUB_KEYBOARD_KEY_INSERT
, GRUB_KEYBOARD_KEY_DELETE
,
266 /* 0x72 */ 0, GRUB_KEYBOARD_KEY_JP_RO
,
271 /* 0x7c */ GRUB_KEYBOARD_KEY_JP_YEN
,
275 add_special_keys (struct grub_keyboard_layout
*layout
)
281 lookup (char *code
, int shift
)
284 struct console_grub_equivalence
*pr
;
287 pr
= console_grub_equivalences_shift
;
289 pr
= console_grub_equivalences_unshift
;
291 for (i
= 0; pr
[i
].layout
!= NULL
; i
++)
292 if (strcmp (code
, pr
[i
].layout
) == 0)
295 for (i
= 0; console_grub_equivalences_common
[i
].layout
!= NULL
; i
++)
296 if (strcmp (code
, console_grub_equivalences_common
[i
].layout
) == 0)
297 return console_grub_equivalences_common
[i
].grub
;
299 /* TRANSLATORS: scan identifier is keyboard key symbolic name. */
300 fprintf (stderr
, _("Unknown keyboard scan identifier %s\n"), code
);
306 get_grub_code (char *layout_code
, int shift
)
310 if (strncmp (layout_code
, "U+", sizeof ("U+") - 1) == 0)
311 sscanf (layout_code
, "U+%x", &code
);
312 else if (strncmp (layout_code
, "+U+", sizeof ("+U+") - 1) == 0)
313 sscanf (layout_code
, "+U+%x", &code
);
315 code
= lookup (layout_code
, shift
);
320 write_file (FILE *out
, const char *fname
, struct grub_keyboard_layout
*layout
)
322 grub_uint32_t version
;
325 version
= grub_cpu_to_le32 (GRUB_KEYBOARD_LAYOUTS_VERSION
);
327 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map
); i
++)
328 layout
->keyboard_map
[i
] = grub_cpu_to_le32(layout
->keyboard_map
[i
]);
330 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map_shift
); i
++)
331 layout
->keyboard_map_shift
[i
]
332 = grub_cpu_to_le32(layout
->keyboard_map_shift
[i
]);
334 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map_l3
); i
++)
335 layout
->keyboard_map_l3
[i
]
336 = grub_cpu_to_le32(layout
->keyboard_map_l3
[i
]);
338 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map_shift_l3
); i
++)
339 layout
->keyboard_map_shift_l3
[i
]
340 = grub_cpu_to_le32(layout
->keyboard_map_shift_l3
[i
]);
342 if (fwrite (GRUB_KEYBOARD_LAYOUTS_FILEMAGIC
, 1,
343 GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE
, out
)
344 != GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE
345 || fwrite (&version
, sizeof (version
), 1, out
) != 1
346 || fwrite (layout
, 1, sizeof (*layout
), out
) != sizeof (*layout
))
349 grub_util_error ("cannot write to `%s': %s", fname
, strerror (errno
));
351 grub_util_error ("cannot write to the stdout: %s", strerror (errno
));
356 write_keymaps (FILE *in
, FILE *out
, const char *out_filename
)
358 struct grub_keyboard_layout layout
;
362 memset (&layout
, 0, sizeof (layout
));
364 /* Process the ckbcomp output and prepare the layouts. */
366 while (fgets (line
, sizeof (line
), in
))
368 if (strncmp (line
, "keycode", sizeof ("keycode") - 1) == 0)
370 unsigned keycode_linux
;
371 unsigned keycode_usb
;
377 sscanf (line
, "keycode %u = %60s %60s %60s %60s", &keycode_linux
,
378 normal
, shift
, normalalt
, shiftalt
);
381 if (keycode_linux
== 0x77 /* Pause */
382 /* Some obscure keys */
383 || keycode_linux
== 0x63 || keycode_linux
== 0x7d
384 || keycode_linux
== 0x7e)
387 /* Not remappable. */
388 if (keycode_linux
== 0x1d /* Left CTRL */
389 || keycode_linux
== 0x61 /* Right CTRL */
390 || keycode_linux
== 0x2a /* Left Shift. */
391 || keycode_linux
== 0x36 /* Right Shift. */
392 || keycode_linux
== 0x38 /* Left ALT. */
393 || keycode_linux
== 0x64 /* Right ALT. */
394 || keycode_linux
== 0x3a /* CapsLock. */
395 || keycode_linux
== 0x45 /* NumLock. */
396 || keycode_linux
== 0x46 /* ScrollLock. */)
399 keycode_usb
= linux_to_usb_map
[keycode_linux
];
401 || keycode_usb
>= GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE
)
403 /* TRANSLATORS: scan code is keyboard key numeric identifier. */
404 fprintf (stderr
, _("Unknown keyboard scan code 0x%02x\n"), keycode_linux
);
407 if (keycode_usb
< GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE
)
409 layout
.keyboard_map
[keycode_usb
] = get_grub_code (normal
, 0);
410 layout
.keyboard_map_shift
[keycode_usb
] = get_grub_code (shift
, 1);
411 layout
.keyboard_map_l3
[keycode_usb
]
412 = get_grub_code (normalalt
, 0);
413 layout
.keyboard_map_shift_l3
[keycode_usb
]
414 = get_grub_code (shiftalt
, 1);
422 /* TRANSLATORS: this error is triggered when input doesn't contain any
424 fprintf (stderr
, "%s", _("ERROR: no valid keyboard layout found. Check the input.\n"));
428 add_special_keys (&layout
);
430 write_file (out
, out_filename
, &layout
);
434 argp_parser (int key
, char *arg
, struct argp_state
*state
)
436 /* Get the input argument from argp_parse, which we
437 know is a pointer to our arguments structure. */
438 struct arguments
*arguments
= state
->input
;
443 arguments
->input
= xstrdup (arg
);
447 arguments
->output
= xstrdup (arg
);
451 arguments
->verbosity
++;
455 return ARGP_ERR_UNKNOWN
;
461 static struct argp argp
= {
462 options
, argp_parser
, N_("[OPTIONS]"),
463 /* TRANSLATORS: "one" is a shortcut for "keyboard layout". */
464 N_("Generate GRUB keyboard layout from Linux console one."),
469 main (int argc
, char *argv
[])
472 struct arguments arguments
;
474 set_program_name (argv
[0]);
476 /* Check for options. */
477 memset (&arguments
, 0, sizeof (struct arguments
));
478 if (argp_parse (&argp
, argc
, argv
, 0, 0, &arguments
) != 0)
480 fprintf (stderr
, "%s", _("Error in parsing command line arguments\n"));
485 in
= fopen (arguments
.input
, "r");
490 grub_util_error (_("cannot open `%s': %s"), arguments
.input
? : "stdin",
493 if (arguments
.output
)
494 out
= fopen (arguments
.output
, "wb");
502 grub_util_error (_("cannot open `%s': %s"), arguments
.output
? : "stdout",
506 write_keymaps (in
, out
, arguments
.output
);