Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini loadkmap implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 1998 Enrique Zanardi <ezanardi@ull.es> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <unistd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | #include <sys/ioctl.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 30 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | |
Matt Kraai | 9133c98 | 2000-10-25 16:48:15 +0000 | [diff] [blame] | 32 | #define BINARY_KEYMAP_MAGIC "bkeymap" |
| 33 | |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 34 | /* From <linux/kd.h> */ |
| 35 | struct kbentry { |
| 36 | unsigned char kb_table; |
| 37 | unsigned char kb_index; |
| 38 | unsigned short kb_value; |
| 39 | }; |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 40 | static const int KDSKBENT = 0x4B47; /* sets one entry in translation table */ |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 41 | |
| 42 | /* From <linux/keyboard.h> */ |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 43 | static const int NR_KEYS = 128; |
| 44 | static const int MAX_NR_KEYMAPS = 256; |
Eric Andersen | bd22ed8 | 2000-07-08 18:55:24 +0000 | [diff] [blame] | 45 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 46 | int loadkmap_main(int argc, char **argv) |
| 47 | { |
| 48 | struct kbentry ke; |
| 49 | u_short *ibuff; |
| 50 | int i, j, fd, readsz, pos, ibuffsz = NR_KEYS * sizeof(u_short); |
Matt Kraai | 9133c98 | 2000-10-25 16:48:15 +0000 | [diff] [blame] | 51 | char flags[MAX_NR_KEYMAPS], buff[7]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 52 | |
Matt Kraai | 9133c98 | 2000-10-25 16:48:15 +0000 | [diff] [blame] | 53 | if (argc != 1) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 54 | show_usage(); |
Erik Andersen | 3fe7f9f | 2000-04-19 03:59:10 +0000 | [diff] [blame] | 55 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | fd = open("/dev/tty0", O_RDWR); |
Matt Kraai | 9133c98 | 2000-10-25 16:48:15 +0000 | [diff] [blame] | 57 | if (fd < 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 58 | perror_msg_and_die("Error opening /dev/tty0"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | |
| 60 | read(0, buff, 7); |
Matt Kraai | 9133c98 | 2000-10-25 16:48:15 +0000 | [diff] [blame] | 61 | if (0 != strncmp(buff, BINARY_KEYMAP_MAGIC, 7)) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 62 | error_msg_and_die("This is not a valid binary keymap."); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | |
Matt Kraai | 9133c98 | 2000-10-25 16:48:15 +0000 | [diff] [blame] | 64 | if (MAX_NR_KEYMAPS != read(0, flags, MAX_NR_KEYMAPS)) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 65 | perror_msg_and_die("Error reading keymap flags"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 67 | ibuff = (u_short *) xmalloc(ibuffsz); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 68 | |
| 69 | for (i = 0; i < MAX_NR_KEYMAPS; i++) { |
| 70 | if (flags[i] == 1) { |
| 71 | pos = 0; |
| 72 | while (pos < ibuffsz) { |
Matt Kraai | 9133c98 | 2000-10-25 16:48:15 +0000 | [diff] [blame] | 73 | if ((readsz = read(0, (char *) ibuff + pos, ibuffsz - pos)) < 0) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 74 | perror_msg_and_die("Error reading keymap"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | pos += readsz; |
| 76 | } |
| 77 | for (j = 0; j < NR_KEYS; j++) { |
| 78 | ke.kb_index = j; |
| 79 | ke.kb_table = i; |
| 80 | ke.kb_value = ibuff[j]; |
| 81 | ioctl(fd, KDSKBENT, &ke); |
| 82 | } |
| 83 | } |
| 84 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 85 | /* Don't bother to close files. Exit does that |
| 86 | * automagically, so we can save a few bytes */ |
| 87 | /* close(fd); */ |
Matt Kraai | 9133c98 | 2000-10-25 16:48:15 +0000 | [diff] [blame] | 88 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 89 | } |