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, L"Null"},
19 	{8, L"BS"},
20 	{9, L"TAB"},
21 	{10, L"LF"},
22 	{13, L"CR"},
23 	{0, NULL},
24 };
25 
26 static u16 efi_st_ch[] = L"' '";
27 static u16 efi_st_unknown[] = L"unknown";
28 
29 static struct efi_st_translate efi_st_scan_codes[] = {
30 	{0x00, L"Null"},
31 	{0x01, L"Up"},
32 	{0x02, L"Down"},
33 	{0x03, L"Right"},
34 	{0x04, L"Left"},
35 	{0x05, L"Home"},
36 	{0x06, L"End"},
37 	{0x07, L"Insert"},
38 	{0x08, L"Delete"},
39 	{0x09, L"Page Up"},
40 	{0x0a, L"Page Down"},
41 	{0x0b, L"FN 1"},
42 	{0x0c, L"FN 2"},
43 	{0x0d, L"FN 3"},
44 	{0x0e, L"FN 4"},
45 	{0x0f, L"FN 5"},
46 	{0x10, L"FN 6"},
47 	{0x11, L"FN 7"},
48 	{0x12, L"FN 8"},
49 	{0x13, L"FN 9"},
50 	{0x14, L"FN 10"},
51 	{0x15, L"FN 11"},
52 	{0x16, L"FN 12"},
53 	{0x17, L"Escape"},
54 	{0x68, L"FN 13"},
55 	{0x69, L"FN 14"},
56 	{0x6a, L"FN 15"},
57 	{0x6b, L"FN 16"},
58 	{0x6c, L"FN 17"},
59 	{0x6d, L"FN 18"},
60 	{0x6e, L"FN 19"},
61 	{0x6f, L"FN 20"},
62 	{0x70, L"FN 21"},
63 	{0x71, L"FN 22"},
64 	{0x72, L"FN 23"},
65 	{0x73, L"FN 24"},
66 	{0x7f, L"Mute"},
67 	{0x80, L"Volume Up"},
68 	{0x81, L"Volume Down"},
69 	{0x100, L"Brightness Up"},
70 	{0x101, L"Brightness Down"},
71 	{0x102, L"Suspend"},
72 	{0x103, L"Hibernate"},
73 	{0x104, L"Toggle Display"},
74 	{0x105, L"Recovery"},
75 	{0x106, L"Reject"},
76 	{0x0, NULL},
77 };
78 
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 
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 
105 int efi_st_memcmp(const void *buf1, const void *buf2, size_t length)
106 {
107 	const u8 *pos1 = buf1;
108 	const u8 *pos2 = buf2;
109 
110 	for (; length; --length) {
111 		if (*pos1 != *pos2)
112 			return *pos1 - *pos2;
113 		++pos1;
114 		++pos2;
115 	}
116 	return 0;
117 }
118 
119 int efi_st_strcmp_16_8(const u16 *buf1, const char *buf2)
120 {
121 	for (; *buf1 || *buf2; ++buf1, ++buf2) {
122 		if (*buf1 != *buf2)
123 			return *buf1 - *buf2;
124 	}
125 	return 0;
126 }
127