blob: 43c1cc795ef813472f5135ce697793c67c04b715 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
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 Andersen3570a342000-09-25 21:45:58 +000023#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include <errno.h>
25#include <fcntl.h>
26#include <stdio.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000027#include <sys/ioctl.h>
28
Matt Kraai9133c982000-10-25 16:48:15 +000029#define BINARY_KEYMAP_MAGIC "bkeymap"
30
Eric Andersenbd22ed82000-07-08 18:55:24 +000031/* From <linux/kd.h> */
32struct kbentry {
33 unsigned char kb_table;
34 unsigned char kb_index;
35 unsigned short kb_value;
36};
Mark Whitley59ab0252001-01-23 22:30:04 +000037static const int KDSKBENT = 0x4B47; /* sets one entry in translation table */
Eric Andersenbd22ed82000-07-08 18:55:24 +000038
39/* From <linux/keyboard.h> */
Mark Whitley59ab0252001-01-23 22:30:04 +000040static const int NR_KEYS = 128;
41static const int MAX_NR_KEYMAPS = 256;
Eric Andersenbd22ed82000-07-08 18:55:24 +000042
Erik Andersene49d5ec2000-02-08 19:58:47 +000043int loadkmap_main(int argc, char **argv)
44{
45 struct kbentry ke;
46 u_short *ibuff;
47 int i, j, fd, readsz, pos, ibuffsz = NR_KEYS * sizeof(u_short);
Matt Kraai9133c982000-10-25 16:48:15 +000048 char flags[MAX_NR_KEYMAPS], buff[7];
Eric Andersencc8ed391999-10-05 16:24:54 +000049
Matt Kraai9133c982000-10-25 16:48:15 +000050 if (argc != 1)
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000051 usage(loadkmap_usage);
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000052
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 fd = open("/dev/tty0", O_RDWR);
Matt Kraai9133c982000-10-25 16:48:15 +000054 if (fd < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +000055 perror_msg_and_die("Error opening /dev/tty0");
Erik Andersene49d5ec2000-02-08 19:58:47 +000056
57 read(0, buff, 7);
Matt Kraai9133c982000-10-25 16:48:15 +000058 if (0 != strncmp(buff, BINARY_KEYMAP_MAGIC, 7))
Mark Whitleyf57c9442000-12-07 19:56:48 +000059 error_msg_and_die("This is not a valid binary keymap.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +000060
Matt Kraai9133c982000-10-25 16:48:15 +000061 if (MAX_NR_KEYMAPS != read(0, flags, MAX_NR_KEYMAPS))
Mark Whitleyf57c9442000-12-07 19:56:48 +000062 perror_msg_and_die("Error reading keymap flags");
Erik Andersene49d5ec2000-02-08 19:58:47 +000063
Matt Kraai322ae932000-09-13 02:46:14 +000064 ibuff = (u_short *) xmalloc(ibuffsz);
Erik Andersene49d5ec2000-02-08 19:58:47 +000065
66 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
67 if (flags[i] == 1) {
68 pos = 0;
69 while (pos < ibuffsz) {
Matt Kraai9133c982000-10-25 16:48:15 +000070 if ((readsz = read(0, (char *) ibuff + pos, ibuffsz - pos)) < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +000071 perror_msg_and_die("Error reading keymap");
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 pos += readsz;
73 }
74 for (j = 0; j < NR_KEYS; j++) {
75 ke.kb_index = j;
76 ke.kb_table = i;
77 ke.kb_value = ibuff[j];
78 ioctl(fd, KDSKBENT, &ke);
79 }
80 }
81 }
Erik Andersen298854f2000-03-23 01:09:18 +000082 /* Don't bother to close files. Exit does that
83 * automagically, so we can save a few bytes */
84 /* close(fd); */
Matt Kraai9133c982000-10-25 16:48:15 +000085 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000086}