1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * efi_selftest_util
4  *
5  * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
6  *
7  * Utility functions
8  */
9 
10 #include <efi_selftest.h>
11 
12 struct efi_st_translate {
13 	u16 code;
14 	u16 *text;
15 };
16 
17 static struct efi_st_translate efi_st_control_characters[] = {
18 	{0, u"Null"},
19 	{8, u"BS"},
20 	{9, u"TAB"},
21 	{10, u"LF"},
22 	{13, u"CR"},
23 	{0, NULL},
24 };
25 
26 static u16 efi_st_ch[] = u"' '";
27 static u16 efi_st_unknown[] = u"unknown";
28 
29 static struct efi_st_translate efi_st_scan_codes[] = {
30 	{0x00, u"Null"},
31 	{0x01, u"Up"},
32 	{0x02, u"Down"},
33 	{0x03, u"Right"},
34 	{0x04, u"Left"},
35 	{0x05, u"Home"},
36 	{0x06, u"End"},
37 	{0x07, u"Insert"},
38 	{0x08, u"Delete"},
39 	{0x09, u"Page Up"},
40 	{0x0a, u"Page Down"},
41 	{0x0b, u"FN 1"},
42 	{0x0c, u"FN 2"},
43 	{0x0d, u"FN 3"},
44 	{0x0e, u"FN 4"},
45 	{0x0f, u"FN 5"},
46 	{0x10, u"FN 6"},
47 	{0x11, u"FN 7"},
48 	{0x12, u"FN 8"},
49 	{0x13, u"FN 9"},
50 	{0x14, u"FN 10"},
51 	{0x15, u"FN 11"},
52 	{0x16, u"FN 12"},
53 	{0x17, u"Escape"},
54 	{0x68, u"FN 13"},
55 	{0x69, u"FN 14"},
56 	{0x6a, u"FN 15"},
57 	{0x6b, u"FN 16"},
58 	{0x6c, u"FN 17"},
59 	{0x6d, u"FN 18"},
60 	{0x6e, u"FN 19"},
61 	{0x6f, u"FN 20"},
62 	{0x70, u"FN 21"},
63 	{0x71, u"FN 22"},
64 	{0x72, u"FN 23"},
65 	{0x73, u"FN 24"},
66 	{0x7f, u"Mute"},
67 	{0x80, u"Volume Up"},
68 	{0x81, u"Volume Down"},
69 	{0x100, u"Brightness Up"},
70 	{0x101, u"Brightness Down"},
71 	{0x102, u"Suspend"},
72 	{0x103, u"Hibernate"},
73 	{0x104, u"Toggle Display"},
74 	{0x105, u"Recovery"},
75 	{0x106, u"Reject"},
76 	{0x0, NULL},
77 };
78 
efi_st_translate_char(u16 code)79 u16 *efi_st_translate_char(u16 code)
80 {
81 	struct efi_st_translate *tr;
82 
83 	if (code >= ' ') {
84 		efi_st_ch[1] = code;
85 		return efi_st_ch;
86 	}
87 	for (tr = efi_st_control_characters; tr->text; ++tr) {
88 		if (tr->code == code)
89 			return tr->text;
90 	}
91 	return efi_st_unknown;
92 }
93 
efi_st_translate_code(u16 code)94 u16 *efi_st_translate_code(u16 code)
95 {
96 	struct efi_st_translate *tr;
97 
98 	for (tr = efi_st_scan_codes; tr->text; ++tr) {
99 		if (tr->code == code)
100 			return tr->text;
101 	}
102 	return efi_st_unknown;
103 }
104 
efi_st_strcmp_16_8(const u16 * buf1,const unsigned char * buf2)105 int efi_st_strcmp_16_8(const u16 *buf1, const unsigned char *buf2)
106 {
107 	for (; *buf1 || *buf2; ++buf1, ++buf2) {
108 		if (*buf1 != *buf2)
109 			return *buf1 - *buf2;
110 	}
111 	return 0;
112 }
113 
efi_st_get_config_table(const efi_guid_t * guid)114 void *efi_st_get_config_table(const efi_guid_t *guid)
115 {
116 	size_t i;
117 
118 	for (i = 0; i < st_systable->nr_tables; i++) {
119 		if (!guidcmp(guid, &st_systable->tables[i].guid))
120 			return st_systable->tables[i].table;
121 	}
122 	return NULL;
123 }
124