diff --git a/src/.gitignore b/src/.gitignore index 29a60a9..398f942 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -3,3 +3,4 @@ efibootmgr eficonman efibootnext efibootdump +efibootkey diff --git a/src/Makefile b/src/Makefile index 683f220..1b14b48 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,12 +7,13 @@ include $(TOPDIR)/Make.defaults SUBDIR_CFLAGS = -I$(SRCDIR)/include -BINTARGETS=efibootmgr efibootdump +BINTARGETS=efibootmgr efibootdump efibootkey TARGETS=$(BINTARGETS) efibootmgr.8 efibootdump.8 all : deps $(TARGETS) EFIBOOTMGR_SOURCES = efibootmgr.c efi.c parse_loader_data.c +EFIBOOTKEY_SOURCES = efibootkey.c efi.c crc32.c EFICONMAN_SOURCES = eficonman.c EFIBOOTDUMP_SOURCES = efibootdump.c parse_loader_data.c EFIBOOTNEXT_SOURCES = efibootnext.c @@ -22,6 +23,9 @@ ALL_SOURCES=$(EFIBOOTMGR_SOURCES) efibootmgr : $(call objects-of,$(EFIBOOTMGR_SOURCES)) efibootmgr : PKGS=efivar efiboot +efibootkey : $(call objects-of,$(EFIBOOTKEY_SOURCES)) +efibootkey : PKGS=efivar efiboot popt + eficonman : $(call objects-of,$(EFICONMAN_SOURCES)) eficonman : PKGS=efivar efiboot popt diff --git a/src/crc32.c b/src/crc32.c new file mode 100644 index 0000000..4fa4bf4 --- /dev/null +++ b/src/crc32.c @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* + * Dec 5, 2000 Matt Domsch + * - Copied crc32.c from the linux/drivers/net/cipe directory. + * - Now pass seed as an arg + * - changed len to be an unsigned long + * - changed crc32val to be a register + * - License remains unchanged! It's still GPL-compatable! + */ + +/* ============================================================= */ +/* COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or */ +/* code or tables extracted from it, as desired without restriction. */ +/* */ +/* First, the polynomial itself and its table of feedback terms. The */ +/* polynomial is */ +/* X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 */ +/* */ +/* Note that we take it "backwards" and put the highest-order term in */ +/* the lowest-order bit. The X^32 term is "implied"; the LSB is the */ +/* X^31 term, etc. The X^0 term (usually shown as "+1") results in */ +/* the MSB being 1. */ +/* */ +/* Note that the usual hardware shift register implementation, which */ +/* is what we're using (we're merely optimizing it by doing eight-bit */ +/* chunks at a time) shifts bits into the lowest-order term. In our */ +/* implementation, that means shifting towards the right. Why do we */ +/* do it this way? Because the calculated CRC must be transmitted in */ +/* order from highest-order term to lowest-order term. UARTs transmit */ +/* characters in order from LSB to MSB. By storing the CRC this way, */ +/* we hand it to the UART in the order low-byte to high-byte; the UART */ +/* sends each low-bit to hight-bit; and the result is transmission bit */ +/* by bit from highest- to lowest-order term without requiring any bit */ +/* shuffling on our part. Reception works similarly. */ +/* */ +/* The feedback terms table consists of 256, 32-bit entries. Notes: */ +/* */ +/* The table can be generated at runtime if desired; code to do so */ +/* is shown later. It might not be obvious, but the feedback */ +/* terms simply represent the results of eight shift/xor opera- */ +/* tions for all combinations of data and CRC register values. */ +/* */ +/* The values must be right-shifted by eight bits by the "updcrc" */ +/* logic; the shift must be unsigned (bring in zeroes). On some */ +/* hardware you could probably optimize the shift in assembler by */ +/* using byte-swap instructions. */ +/* polynomial $edb88320 */ +/* */ +/* -------------------------------------------------------------------- */ + +#include + +static uint32_t crc32_tab[] = { + 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, + 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L, + 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, + 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, + 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, + 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, + 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, + 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL, + 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, + 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, + 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, + 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, + 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L, + 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, + 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, + 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L, + 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, + 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, + 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, + 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, + 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL, + 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, + 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, + 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL, + 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, + 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L, + 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, + 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, + 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L, + 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, + 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, + 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L, + 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, + 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, + 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, + 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, + 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL, + 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, + 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, + 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L, + 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, + 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, + 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, + 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, + 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L, + 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, + 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, + 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, + 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, + 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L, + 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, + 0x2d02ef8dL +}; + +/* Return a 32-bit CRC of the contents of the buffer. */ + +uint32_t +crc32(const void *buf, unsigned long len, uint32_t seed) +{ + unsigned long i; + register uint32_t val; + const unsigned char *s = buf; + + val = seed; + for (i = 0; i < len; i ++) + val = crc32_tab[(val ^ s[i]) & 0xff] ^ (val >> 8); + + return val; +} + +// vim:fenc=utf-8:tw=75:noet diff --git a/src/crc32.h b/src/crc32.h new file mode 100644 index 0000000..1027d2b --- /dev/null +++ b/src/crc32.h @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* + * crc32.h - headers for crc32 + * + */ + +#ifndef _CRC32_H +#define _CRC32_H + +#include + +/* + * This computes a 32 bit CRC of the data in the buffer, and returns the CRC. + * The polynomial used is 0xedb88320. + */ + +extern uint32_t crc32 (const void *buf, unsigned long len, uint32_t seed); + +/** + * efi_crc32() - EFI version of crc32 function + * @buf: buffer to calculate crc32 of + * @len - length of buf + * + * Description: Returns EFI-style CRC32 value for @buf + * + * This function uses the little endian Ethernet polynomial + * but seeds the function with ~0, and xor's with ~0 at the end. + * Note, the EFI Specification, v1.02, has a reference to + * Dr. Dobbs Journal, May 1994 (actually it's in May 1992). + */ +static inline uint32_t +efi_crc32(const void *buf, unsigned long len) +{ + return (crc32(buf, len, ~0L) ^ ~0L); +} + + +#endif /* _CRC32_H */ + +// vim:fenc=utf-8:tw=75:noet diff --git a/src/efibootkey.c b/src/efibootkey.c new file mode 100644 index 0000000..1eb89f2 --- /dev/null +++ b/src/efibootkey.c @@ -0,0 +1,371 @@ +/* + * efibootkey.c - create hotkeys for boot options. + * + * Copyright 2023 Demitrius Belai + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this library; if not, see . + * + */ + + +#include +#include +#include +#include +#include "efi.h" +#include "error.h" +#include "efibootkey.h" +#include "crc32.h" + +int verbose = 0; + +int +calc_crc_boot(uint16_t boot_num, uint32_t *crc) +{ + char boot_name[9]; + uint32_t attributes; + uint8_t *data = NULL; + size_t data_size = 0; + int rc; + sprintf(boot_name, "Boot%04d", boot_num); + rc = efi_get_variable(EFI_GLOBAL_GUID, boot_name, &data, + &data_size, &attributes); + if (rc < 0) { + return rc; + } + *crc = efi_crc32(data, data_size); + return 0; +} + +static void +show_keys(int check_crc) +{ + char **names = NULL; + uint8_t *data = NULL; + size_t data_size = 0; + efi_key_option_t *key_option; + uint32_t attributes, crc; + int i, j, rc; + rc = read_var_names("Key", &names); + if (rc < 0 || !names) { + return; + } + for (i=0; names[i] != NULL; i++) { + rc = efi_get_variable(EFI_GLOBAL_GUID, names[i], &data, + &data_size, &attributes); + if (rc < 0) { + warning("Skipping unreadable variable \"%s\"", + names[i]); + goto skipvar; + } + key_option = (efi_key_option_t *) data; + printf("%s: for=Boot%04X ", names[i], key_option->boot_option); + printf(" key="); + if (key_option->key_data.options.shift_pressed) { + printf("Shift+"); + } + if (key_option->key_data.options.control_pressed) { + printf("Ctrl+"); + } + if (key_option->key_data.options.alt_pressed) { + printf("Alt+"); + } + if (key_option->key_data.options.logo_pressed) { + printf("Logo+"); + } + if (key_option->key_data.options.menu_pressed) { + printf("Menu+"); + } + if (key_option->key_data.options.sysrq_pressed) { + printf("SysRq+"); + } + for (j = 0; j < key_option->key_data.options.input_key_count; j++) { + if (j > 0) { + printf("+"); + } + if (key_option->keys[j].scan_code) { + printf("%s", get_scan_code(key_option->keys[j].scan_code)); + } else { + char *s = ucs2_to_utf8_string(key_option->keys[j].unicode_char); + if (!s) { + error(8, "Could not allocate memory"); + } + printf("%s", s); + free(s); + } + } + if (check_crc) { + rc = calc_crc_boot(key_option->boot_option, &crc); + if (rc < 0) { + printf(" crc=unreadable"); + } else if (crc == key_option->boot_option_crc) { + printf(" crc=ok"); + } else { + printf(" crc=invalid"); + } + } + printf("\n"); + skipvar: + free(data); + } + free(names); +} + +int +verify_support() +{ + uint8_t *data = NULL; + size_t data_size = 0; + uint32_t attributes; + int rc; + rc = efi_get_variable(EFI_GLOBAL_GUID, "BootOptionSupport", &data, + &data_size, &attributes); + if (rc < 0) { + warning("Skipping unreadable variable \"BootOptionSupport\""); + return -1; + } + if (data_size > 0 && data[0] & 0x01) { + printf("Supported\n"); + if (data_size > 1) { + printf("Maximum number keys: %d\n", data[1] & 0x3); + } + return 0; + } + return -1; +} + +int +get_free_num() +{ + char **names = NULL; + int i, num, rc; + rc = read_var_names("Key", &names); + if (rc < 0 || !names) { + return 0; + } + for (i=0; names[i] != NULL; i++) { + sscanf(names[i], "Key%x", &num); + if (num != i) { + break; + } + } + free(names); + return i; +} + +int +create_key(int bootnum, char **keycode_v, efi_boot_key_data_t keydata) +{ + size_t size = sizeof(efi_key_option_t) + + sizeof(efi_input_key_t) * keydata.options.input_key_count; + uint32_t crc; + efi_key_option_t *key = malloc(size); + if (!key) { + error(8, "Could not allocate memory"); + } + key->boot_option = bootnum; + key->key_data = keydata; + if (calc_crc_boot(bootnum, &crc) < 0) { + error(10, "Could not gerate CRC"); + } + key->boot_option_crc = crc; + for (int i = 0; i < keydata.options.input_key_count; i++) { + if (utf8len(keycode_v[i]) == 1) { + char32_t c = utf8charat(keycode_v[i], 0); + key->keys[i].unicode_char = utf8_to_ucs2(c); + } else { + key->keys[i].scan_code = scan_code_from_name(keycode_v[i]); + if (key->keys[i].scan_code == SCAN_NULL) { + errorx(10, "Invalid key name: %s\n", keycode_v[i]); + } + } + } + int num = get_free_num(); + char name[8]; + int rc; + sprintf(name, "Key%04X", num); + uint32_t attributes = EFI_VARIABLE_NON_VOLATILE | + EFI_VARIABLE_BOOTSERVICE_ACCESS | + EFI_VARIABLE_RUNTIME_ACCESS; + rc = efi_set_variable(EFI_GLOBAL_GUID, name, (uint8_t *) key, size, + attributes, 0644); + if (rc < 0) { + error(10, "Error writing variable"); + } + free(key); + return 0; +} + +int +delete_key(int keynum) +{ + char name[8]; + int r; + sprintf(name, "Key%04X", keynum); + r = efi_del_variable(EFI_GLOBAL_GUID, name); + if (r < 0) { + error(10, "Error deleting variable"); + } + return 0; +} + +int +main(int argc, char **argv) +{ + int bootnum = -1; + int keynum = -1; + char *hexnum; + char *keycode; + char *keycode_v[3]; + int keycode_c = 0; + int alt = 0, shift = 0, ctrl = 0, logo = 0, menu = 0, sysrq = 0; + poptContext poptcon; + int verify = 0, list = 0, create = 0, delete = 0; + int check_crc = 0; + char rc; + + struct poptOption options[] = { + {.argInfo = POPT_ARG_INTL_DOMAIN, + .arg = "efibootkey"}, + {.argInfo = POPT_ARG_STRING, + .longName = "bootnum", + .shortName = 'b', + .arg = &hexnum, + .val = 'b', + .descrip = "Hot key for BootXXXX"}, + {.argInfo = POPT_ARG_STRING, + .longName = "keynum", + .shortName = 'n', + .arg = &hexnum, + .val = 'n', + .descrip = "Modify KeyXXXX"}, + {.argInfo = POPT_ARG_STRING, + .longName = "key", + .shortName = 'k', + .arg = &keycode, + .val = 'k', + .descrip = "Code of Key, can repeat up to three times"}, + {.argInfo = POPT_ARG_NONE, + .longName = "alt", + .arg = &alt, + .descrip = "Alt must be pressed"}, + {.argInfo = POPT_ARG_NONE, + .longName = "shift", + .arg = &shift, + .descrip = "Shift must be pressed"}, + {.argInfo = POPT_ARG_NONE, + .longName = "ctrl", + .arg = &ctrl, + .descrip = "Control must be pressed"}, + {.argInfo = POPT_ARG_NONE, + .longName = "logo", + .arg = &logo, + .descrip = "Logo must be pressed"}, + {.argInfo = POPT_ARG_NONE, + .longName = "menu", + .arg = &menu, + .descrip = "Menu must be pressed"}, + {.argInfo = POPT_ARG_NONE, + .longName = "sysrq", + .arg = &sysrq, + .descrip = "SysReq must be pressed"}, + {.argInfo = POPT_ARG_NONE, + .longName = "support", + .arg = &verify, + .descrip = "Verify if boot manager support hot key"}, + {.argInfo = POPT_ARG_NONE, + .longName = "list", + .shortName = 'l', + .arg = &list, + .descrip = "List hot keys"}, + {.argInfo = POPT_ARG_NONE, + .longName = "checkcrc", + .arg = &check_crc, + .descrip = "Check CRC"}, + {.argInfo = POPT_ARG_NONE, + .longName = "delete-keynum", + .shortName = 'B', + .arg = &delete, + .descrip = "Delete hot key"}, + POPT_AUTOHELP + POPT_TABLEEND + }; + + poptcon = poptGetContext(NULL, argc, (const char **)argv, options, 0); + if (argc < 2) { + poptPrintUsage(poptcon, stderr, 0); + exit(1); + } + + while ((rc = poptGetNextOpt(poptcon)) >=0) { + switch (rc) { + case 'k': + if (keycode_c >= 3) { + poptPrintUsage(poptcon, stderr, 0); + exit(4); + } + create = 1; + keycode_v[keycode_c++] = keycode; + break; + case 'b': + case 'n': + if (!hexnum) { + errorx(4, "Invalid hexdecimal number\n"); + } + char *endptr; + unsigned long num; + num = strtoul(hexnum, &endptr, 16); + if ((num == ULONG_MAX && errno == ERANGE) + || *endptr != '\0') { + error(4, "Invalid hexdecimal number"); + } + if (rc == 'b') + bootnum = num; + else + keynum = num; + break; + } + } + + if (rc < -1) + errorx(2, "Invalid argument: \"%s\": %s", + poptBadOption(poptcon, 0), poptStrerror(rc)); + + if (verify) { + return verify_support(); + } else if (list) { + show_keys(check_crc); + return 0; + } else if (create) { + if (bootnum < 0 || keycode_c == 0) { + poptPrintUsage(poptcon, stderr, 0); + exit(4); + } + efi_boot_key_data_t keydata = { + .options = { + .shift_pressed = shift, + .control_pressed = ctrl, + .alt_pressed = alt, + .logo_pressed = logo, + .menu_pressed = menu, + .sysrq_pressed = sysrq, + .input_key_count = keycode_c, + } + }; + create_key(bootnum, keycode_v, keydata); + } else if (delete) { + delete_key(keynum); + } + return 0; +} diff --git a/src/include/efibootkey.h b/src/include/efibootkey.h new file mode 100644 index 0000000..255f0ac --- /dev/null +++ b/src/include/efibootkey.h @@ -0,0 +1,269 @@ +/* + * efibootkey.h - create hotkeys for boot options. + * + * Copyright 2023 Demitrius Belai + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this library; if not, see . + * + */ + +#ifndef _EFIBOOTKEY_H +#define _EFIBOOTKEY_H + +#include +#include +#include +#include + +typedef struct { + uint16_t scan_code; + char16_t unicode_char; +} __attribute__ ((packed)) efi_input_key_t; + +typedef union { + struct { + uint32_t revision: 8; + uint32_t shift_pressed: 1; + uint32_t control_pressed: 1; + uint32_t alt_pressed: 1; + uint32_t logo_pressed: 1; + uint32_t menu_pressed: 1; + uint32_t sysrq_pressed: 1; + uint32_t reserved: 16; + uint32_t input_key_count: 2; + } options; + uint32_t packed_value; +} __attribute__ ((packed)) efi_boot_key_data_t; + +typedef struct { + efi_boot_key_data_t key_data; + uint32_t boot_option_crc; + uint16_t boot_option; + efi_input_key_t keys[]; +} __attribute__ ((packed)) efi_key_option_t; + +/* + * utf8_to_ucs2(): extracted from efivar/ucs2.h + */ +static inline char16_t +utf8_to_ucs2(const char32_t c) +{ + const char *utf8 = (char *) &c; + if ((utf8[0] & 0xe0) == 0xe0 && !(utf8[0] & 0x10)) { + return ((utf8[0] & 0x0f) << 12) + |((utf8[1] & 0x3f) << 6) + |((utf8[2] & 0x3f) << 0); + } else if ((utf8[0] & 0xc0) == 0xc0 && !(utf8[0] & 0x20)) { + return ((utf8[0] & 0x1f) << 6) + |((utf8[1] & 0x3f) << 0); + } else { + return utf8[0] & 0x7f; + } +} + +#define ev_bits(val, mask, shift) \ + (((val) & ((mask) << (shift))) >> (shift)) + +/* + * ucs2_to_utf8(): extracted from efivar/ucs2.h + */ +static inline +char32_t ucs2_to_utf8(const char16_t c) +{ + char ret[sizeof(char32_t)]; + memset(&ret, 0, sizeof(char32_t)); + if (c <= 0x7f) { + ret[0] = c; + } else if (c > 0x7f && c <= 0x7ff) { + ret[0] = 0xc0 | ev_bits(c, 0x1f, 6); + ret[1] = 0x80 | ev_bits(c, 0x3f, 0); + } else if (c > 0x7ff) { + ret[0] = 0xe0 | ev_bits(c, 0xf, 12); + ret[1] = 0x80 | ev_bits(c, 0x3f, 6); + ret[2] = 0x80| ev_bits(c, 0x3f, 0); + } + return *((char32_t*) &ret); +} + +static inline +char * ucs2_to_utf8_string(const char16_t c) +{ + void *s = malloc(sizeof(char32_t) + 1); + if (!s) { + return NULL; + } + ((char *)s)[sizeof(char32_t)] = '\0'; + char32_t val = ucs2_to_utf8(c); + *((char32_t *)s) = val; + return s; +} + +static inline +size_t utf8len(const char *s) +{ + size_t len = 0; + for (; *s; s++) + if ((*s & 0xc0) != 0x80) + len++; + return len; +} + +static inline +char32_t utf8charat(const char *s, size_t at) +{ + size_t len = 0; + char32_t c = 0; + char *b = (char *)&c; + for (; *s; s++) { + if ((*s & 0xc0) != 0x80) { + if (++len > at) + break; + } + } + *b = *s; + if ((*s & 0xc0) == 0xc0) + for (s++, b++; (*s & 0xc0) == 0x80; s++, b++) + *b = *s; + return c; +} + +// EFI Scan codes +#define SCAN_NULL 0x0000 +#define SCAN_UP 0x0001 +#define SCAN_DOWN 0x0002 +#define SCAN_RIGHT 0x0003 +#define SCAN_LEFT 0x0004 +#define SCAN_HOME 0x0005 +#define SCAN_END 0x0006 +#define SCAN_INSERT 0x0007 +#define SCAN_DELETE 0x0008 +#define SCAN_PAGE_UP 0x0009 +#define SCAN_PAGE_DOWN 0x000A +#define SCAN_F1 0x000B +#define SCAN_F2 0x000C +#define SCAN_F3 0x000D +#define SCAN_F4 0x000E +#define SCAN_F5 0x000F +#define SCAN_F6 0x0010 +#define SCAN_F7 0x0011 +#define SCAN_F8 0x0012 +#define SCAN_F9 0x0013 +#define SCAN_F10 0x0014 +#define SCAN_F11 0x0015 +#define SCAN_F12 0x0016 +#define SCAN_ESC 0x0017 +#define SCAN_PAUSE 0x0048 +#define SCAN_F13 0x0068 +#define SCAN_F14 0x0069 +#define SCAN_F15 0x006A +#define SCAN_F16 0x006B +#define SCAN_F17 0x006C +#define SCAN_F18 0x006D +#define SCAN_F19 0x006E +#define SCAN_F20 0x006F +#define SCAN_F21 0x0070 +#define SCAN_F22 0x0071 +#define SCAN_F23 0x0072 +#define SCAN_F24 0x0073 +#define SCAN_MUTE 0x007F +#define SCAN_VOLUME_UP 0x0080 +#define SCAN_VOLUME_DOWN 0x0081 +#define SCAN_BRIGHTNESS_UP 0x0100 +#define SCAN_BRIGHTNESS_DOWN 0x0101 +#define SCAN_SUSPEND 0x0102 +#define SCAN_HIBERNATE 0x0103 +#define SCAN_TOGGLE_DISPLAY 0x0104 +#define SCAN_RECOVERY 0x0105 +#define SCAN_EJECT 0x0106 + +typedef struct { + uint16_t scan_code; + char *name; +} scan_code_t; + +//const int scan_codes_size = 47; + +const scan_code_t scan_codes[] = { + {SCAN_UP, "Up"}, + {SCAN_DOWN, "Down"}, + {SCAN_RIGHT, "Right"}, + {SCAN_LEFT, "Left"}, + {SCAN_HOME, "Home"}, + {SCAN_END, "End"}, + {SCAN_INSERT, "Insert"}, + {SCAN_DELETE, "Delete"}, + {SCAN_PAGE_UP, "PgUP"}, + {SCAN_PAGE_DOWN, "PgDown"}, + {SCAN_F1, "F1"}, + {SCAN_F2, "F2"}, + {SCAN_F3, "F3"}, + {SCAN_F4, "F4"}, + {SCAN_F5, "F5"}, + {SCAN_F6, "F6"}, + {SCAN_F7, "F7"}, + {SCAN_F8, "F8"}, + {SCAN_F9, "F9"}, + {SCAN_F10, "F10"}, + {SCAN_F11, "F11"}, + {SCAN_F12, "F12"}, + {SCAN_ESC, "Esc"}, + {SCAN_PAUSE, "Pause"}, + {SCAN_F13, "F13"}, + {SCAN_F14, "F14"}, + {SCAN_F15, "F15"}, + {SCAN_F16, "F16"}, + {SCAN_F17, "F17"}, + {SCAN_F18, "F18"}, + {SCAN_F19, "F19"}, + {SCAN_F20, "F20"}, + {SCAN_F21, "F21"}, + {SCAN_F22, "F22"}, + {SCAN_F23, "F23"}, + {SCAN_F24, "F24"}, + {SCAN_MUTE, "Mute"}, + {SCAN_VOLUME_UP, "VolUp"}, + {SCAN_VOLUME_DOWN, "VolDown"}, + {SCAN_BRIGHTNESS_UP, "BrtUp"}, + {SCAN_BRIGHTNESS_DOWN, "BrtDown"}, + {SCAN_SUSPEND, "Suspend"}, + {SCAN_HIBERNATE, "Hibernate"}, + {SCAN_TOGGLE_DISPLAY, "Display"}, + {SCAN_RECOVERY, "Recovery"}, + {SCAN_EJECT, "Eject"}, + {SCAN_NULL, "NULL"}, +}; + +const char * +get_scan_code(uint16_t scan_code) +{ + int i; + for (i = 0; scan_codes[i].scan_code != SCAN_NULL; i++) { + if (scan_code == scan_codes[i].scan_code) + return scan_codes[i].name; + } + return "Unknown"; +} + +uint16_t +scan_code_from_name(const char *s) +{ + int i; + for (i = 0; scan_codes[i].scan_code != SCAN_NULL; i++) { + if (strcasecmp(scan_codes[i].name, s) == 0) + return scan_codes[i].scan_code; + } + return SCAN_NULL; +} + +#endif