1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU Lesser General Public License as published by
5 * the Free Software Foundation; either version 2.1 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * Copyright (C) 2012-2013 Kay Sievers <kay@vrfy.org>
14 * Copyright (C) 2012 Harald Hoyer <harald@redhat.com>
23 * Allocated random UUID, intended to be shared across tools that implement
24 * the (ESP)\loader\entries\<vendor>-<revision>.conf convention and the
25 * associated EFI variables.
27 static const EFI_GUID loader_guid
= { 0x4a67b082, 0x0a4c, 0x41cf, {0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f} };
30 UINT64
ticks_read(VOID
) {
32 __asm__
volatile ("rdtsc" : "=a" (a
), "=d" (d
));
35 #elif defined(__i386__)
36 UINT64
ticks_read(VOID
) {
38 __asm__
volatile ("rdtsc" : "=A" (val
));
42 UINT64
ticks_read(VOID
) {
48 /* count TSC ticks during a millisecond delay */
49 UINT64
ticks_freq(VOID
) {
50 UINT64 ticks_start
, ticks_end
;
52 ticks_start
= ticks_read();
53 uefi_call_wrapper(BS
->Stall
, 1, 1000);
54 ticks_end
= ticks_read();
56 return (ticks_end
- ticks_start
) * 1000;
59 UINT64
time_usec(VOID
) {
73 return 1000 * 1000 * ticks
/ freq
;
76 EFI_STATUS
parse_boolean(CHAR8
*v
, BOOLEAN
*b
) {
77 if (strcmpa(v
, (CHAR8
*)"1") == 0 ||
78 strcmpa(v
, (CHAR8
*)"yes") == 0 ||
79 strcmpa(v
, (CHAR8
*)"y") == 0 ||
80 strcmpa(v
, (CHAR8
*)"true") == 0) {
85 if (strcmpa(v
, (CHAR8
*)"0") == 0 ||
86 strcmpa(v
, (CHAR8
*)"no") == 0 ||
87 strcmpa(v
, (CHAR8
*)"n") == 0 ||
88 strcmpa(v
, (CHAR8
*)"false") == 0) {
93 return EFI_INVALID_PARAMETER
;
96 EFI_STATUS
efivar_set_raw(const EFI_GUID
*vendor
, CHAR16
*name
, CHAR8
*buf
, UINTN size
, BOOLEAN persistent
) {
99 flags
= EFI_VARIABLE_BOOTSERVICE_ACCESS
|EFI_VARIABLE_RUNTIME_ACCESS
;
101 flags
|= EFI_VARIABLE_NON_VOLATILE
;
103 return uefi_call_wrapper(RT
->SetVariable
, 5, name
, (EFI_GUID
*)vendor
, flags
, size
, buf
);
106 EFI_STATUS
efivar_set(CHAR16
*name
, CHAR16
*value
, BOOLEAN persistent
) {
107 return efivar_set_raw(&loader_guid
, name
, (CHAR8
*)value
, value
? (StrLen(value
)+1) * sizeof(CHAR16
) : 0, persistent
);
110 EFI_STATUS
efivar_set_int(CHAR16
*name
, UINTN i
, BOOLEAN persistent
) {
113 SPrint(str
, 32, L
"%d", i
);
114 return efivar_set(name
, str
, persistent
);
117 EFI_STATUS
efivar_get(CHAR16
*name
, CHAR16
**value
) {
118 _cleanup_freepool_ CHAR8
*buf
= NULL
;
123 err
= efivar_get_raw(&loader_guid
, name
, &buf
, &size
);
127 val
= StrDuplicate((CHAR16
*)buf
);
129 return EFI_OUT_OF_RESOURCES
;
135 EFI_STATUS
efivar_get_int(CHAR16
*name
, UINTN
*i
) {
136 _cleanup_freepool_ CHAR16
*val
= NULL
;
139 err
= efivar_get(name
, &val
);
146 EFI_STATUS
efivar_get_raw(const EFI_GUID
*vendor
, CHAR16
*name
, CHAR8
**buffer
, UINTN
*size
) {
147 _cleanup_freepool_ CHAR8
*buf
= NULL
;
151 l
= sizeof(CHAR16
*) * EFI_MAXIMUM_VARIABLE_SIZE
;
152 buf
= AllocatePool(l
);
154 return EFI_OUT_OF_RESOURCES
;
156 err
= uefi_call_wrapper(RT
->GetVariable
, 5, name
, (EFI_GUID
*)vendor
, NULL
, &l
, buf
);
157 if (!EFI_ERROR(err
)) {
167 VOID
efivar_set_time_usec(CHAR16
*name
, UINT64 usec
) {
175 SPrint(str
, 32, L
"%ld", usec
);
176 efivar_set(name
, str
, FALSE
);
179 static INTN
utf8_to_16(CHAR8
*stra
, CHAR16
*c
) {
186 else if ((stra
[0] & 0xe0) == 0xc0)
188 else if ((stra
[0] & 0xf0) == 0xe0)
190 else if ((stra
[0] & 0xf8) == 0xf0)
192 else if ((stra
[0] & 0xfc) == 0xf8)
194 else if ((stra
[0] & 0xfe) == 0xfc)
204 unichar
= stra
[0] & 0x1f;
207 unichar
= stra
[0] & 0x0f;
210 unichar
= stra
[0] & 0x07;
213 unichar
= stra
[0] & 0x03;
216 unichar
= stra
[0] & 0x01;
220 for (i
= 1; i
< len
; i
++) {
221 if ((stra
[i
] & 0xc0) != 0x80)
224 unichar
|= stra
[i
] & 0x3f;
231 CHAR16
*stra_to_str(CHAR8
*stra
) {
238 str
= AllocatePool((len
+ 1) * sizeof(CHAR16
));
245 utf8len
= utf8_to_16(stra
+ i
, str
+ strlen
);
247 /* invalid utf8 sequence, skip the garbage */
259 CHAR16
*stra_to_path(CHAR8
*stra
) {
266 str
= AllocatePool((len
+ 2) * sizeof(CHAR16
));
274 utf8len
= utf8_to_16(stra
+ i
, str
+ strlen
);
276 /* invalid utf8 sequence, skip the garbage */
281 if (str
[strlen
] == '/')
283 if (str
[strlen
] == '\\' && str
[strlen
-1] == '\\') {
284 /* skip double slashes */
296 CHAR8
*strchra(CHAR8
*s
, CHAR8 c
) {
304 EFI_STATUS
file_read(EFI_FILE_HANDLE dir
, CHAR16
*name
, UINTN off
, UINTN size
, CHAR8
**content
, UINTN
*content_size
) {
305 EFI_FILE_HANDLE handle
;
306 _cleanup_freepool_ CHAR8
*buf
= NULL
;
309 err
= uefi_call_wrapper(dir
->Open
, 5, dir
, &handle
, name
, EFI_FILE_MODE_READ
, 0ULL);
314 _cleanup_freepool_ EFI_FILE_INFO
*info
;
316 info
= LibFileInfo(handle
);
317 size
= info
->FileSize
+1;
321 err
= uefi_call_wrapper(handle
->SetPosition
, 2, handle
, off
);
326 buf
= AllocatePool(size
+ 1);
327 err
= uefi_call_wrapper(handle
->Read
, 3, handle
, &size
, buf
);
328 if (!EFI_ERROR(err
)) {
333 *content_size
= size
;
335 uefi_call_wrapper(handle
->Close
, 1, handle
);