blob: 4a249868a12d2cb0ad586024aef6aecde29714a9
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * Mini dumpkmap implementation for busybox |
4 | * |
5 | * Copyright (C) Arne Bernin <arne@matrix.loopback.org> |
6 | * |
7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
8 | * |
9 | */ |
10 | //config:config DUMPKMAP |
11 | //config: bool "dumpkmap" |
12 | //config: default y |
13 | //config: select PLATFORM_LINUX |
14 | //config: help |
15 | //config: This program dumps the kernel's keyboard translation table to |
16 | //config: stdout, in binary format. You can then use loadkmap to load it. |
17 | |
18 | //applet:IF_DUMPKMAP(APPLET(dumpkmap, BB_DIR_BIN, BB_SUID_DROP)) |
19 | |
20 | //kbuild:lib-$(CONFIG_DUMPKMAP) += dumpkmap.o |
21 | |
22 | //usage:#define dumpkmap_trivial_usage |
23 | //usage: "> keymap" |
24 | //usage:#define dumpkmap_full_usage "\n\n" |
25 | //usage: "Print a binary keyboard translation table to stdout" |
26 | //usage: |
27 | //usage:#define dumpkmap_example_usage |
28 | //usage: "$ dumpkmap > keymap\n" |
29 | |
30 | #include "libbb.h" |
31 | #include "common_bufsiz.h" |
32 | |
33 | /* From <linux/kd.h> */ |
34 | struct kbentry { |
35 | unsigned char kb_table; |
36 | unsigned char kb_index; |
37 | unsigned short kb_value; |
38 | }; |
39 | #define KDGKBENT 0x4B46 /* gets one entry in translation table */ |
40 | |
41 | /* From <linux/keyboard.h> */ |
42 | #define NR_KEYS 128 |
43 | #define MAX_NR_KEYMAPS 256 |
44 | |
45 | int dumpkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
46 | int dumpkmap_main(int argc UNUSED_PARAM, char **argv) |
47 | { |
48 | struct kbentry ke; |
49 | int i, j, fd; |
50 | #define flags bb_common_bufsiz1 |
51 | setup_common_bufsiz(); |
52 | |
53 | /* When user accidentally runs "dumpkmap FILE" |
54 | * instead of "dumpkmap >FILE", we'd dump binary stuff to tty. |
55 | * Let's prevent it: |
56 | */ |
57 | if (argv[1]) |
58 | bb_show_usage(); |
59 | /* bb_warn_ignoring_args(argv[1]);*/ |
60 | |
61 | fd = get_console_fd_or_die(); |
62 | |
63 | #if 0 |
64 | write(STDOUT_FILENO, "bkeymap", 7); |
65 | /* Here we want to set everything to 0 except for indexes: |
66 | * [0-2] [4-6] [8-10] [12] |
67 | */ |
68 | /*memset(flags, 0x00, MAX_NR_KEYMAPS); - already is */ |
69 | memset(flags, 0x01, 13); |
70 | flags[3] = flags[7] = flags[11] = 0; |
71 | /* dump flags */ |
72 | write(STDOUT_FILENO, flags, MAX_NR_KEYMAPS); |
73 | #define flags7 flags |
74 | #else |
75 | /* Same effect */ |
76 | /* 0 1 2 3 4 5 6 7 8 9 a b c=12 */ |
77 | memcpy(flags, "bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1", |
78 | /* Can use sizeof, or sizeof-1. sizeof is even, using that */ |
79 | /****/ sizeof("bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1") |
80 | ); |
81 | write(STDOUT_FILENO, flags, 7 + MAX_NR_KEYMAPS); |
82 | #define flags7 (flags + 7) |
83 | #endif |
84 | |
85 | for (i = 0; i < 13; i++) { |
86 | if (flags7[i]) { |
87 | for (j = 0; j < NR_KEYS; j++) { |
88 | ke.kb_index = j; |
89 | ke.kb_table = i; |
90 | if (!ioctl_or_perror(fd, KDGKBENT, &ke, |
91 | "ioctl(KDGKBENT{%d,%d}) failed", |
92 | j, i) |
93 | ) { |
94 | write(STDOUT_FILENO, &ke.kb_value, 2); |
95 | } |
96 | } |
97 | } |
98 | } |
99 | if (ENABLE_FEATURE_CLEAN_UP) { |
100 | close(fd); |
101 | } |
102 | return EXIT_SUCCESS; |
103 | } |
104 |