2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published by
4 * the Free Software Foundation; either version 2.1 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * Copyright (C) 2012-2013 Kay Sievers <kay@vrfy.org>
13 * Copyright (C) 2012 Harald Hoyer <harald@redhat.com>
22 * Allocated random UUID, intended to be shared across tools that implement
23 * the (ESP)\loader\entries\<vendor>-<revision>.conf convention and the
24 * associated EFI variables.
26 static const EFI_GUID loader_guid
= { 0x4a67b082, 0x0a4c, 0x41cf, {0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f} };
29 UINT64
ticks_read(VOID
) {
31 __asm__
volatile ("rdtsc" : "=a" (a
), "=d" (d
));
34 #elif defined(__i386__)
35 UINT64
ticks_read(VOID
) {
37 __asm__
volatile ("rdtsc" : "=A" (val
));
41 UINT64
ticks_read(VOID
) {
47 /* count TSC ticks during a millisecond delay */
48 UINT64
ticks_freq(VOID
) {
49 UINT64 ticks_start
, ticks_end
;
51 ticks_start
= ticks_read();
52 uefi_call_wrapper(BS
->Stall
, 1, 1000);
53 ticks_end
= ticks_read();
55 return (ticks_end
- ticks_start
) * 1000;
58 UINT64
time_usec(VOID
) {
72 return 1000 * 1000 * ticks
/ freq
;
75 EFI_STATUS
parse_boolean(CHAR8
*v
, BOOLEAN
*b
) {
76 if (strcmpa(v
, (CHAR8
*)"1") == 0 ||
77 strcmpa(v
, (CHAR8
*)"yes") == 0 ||
78 strcmpa(v
, (CHAR8
*)"y") == 0 ||
79 strcmpa(v
, (CHAR8
*)"true") == 0) {
84 if (strcmpa(v
, (CHAR8
*)"0") == 0 ||
85 strcmpa(v
, (CHAR8
*)"no") == 0 ||
86 strcmpa(v
, (CHAR8
*)"n") == 0 ||
87 strcmpa(v
, (CHAR8
*)"false") == 0) {
92 return EFI_INVALID_PARAMETER
;
95 EFI_STATUS
efivar_set_raw(const EFI_GUID
*vendor
, CHAR16
*name
, CHAR8
*buf
, UINTN size
, BOOLEAN persistent
) {
98 flags
= EFI_VARIABLE_BOOTSERVICE_ACCESS
|EFI_VARIABLE_RUNTIME_ACCESS
;
100 flags
|= EFI_VARIABLE_NON_VOLATILE
;
102 return uefi_call_wrapper(RT
->SetVariable
, 5, name
, (EFI_GUID
*)vendor
, flags
, size
, buf
);
105 EFI_STATUS
efivar_set(CHAR16
*name
, CHAR16
*value
, BOOLEAN persistent
) {
106 return efivar_set_raw(&loader_guid
, name
, (CHAR8
*)value
, value
? (StrLen(value
)+1) * sizeof(CHAR16
) : 0, persistent
);
109 EFI_STATUS
efivar_set_int(CHAR16
*name
, UINTN i
, BOOLEAN persistent
) {
112 SPrint(str
, 32, L
"%d", i
);
113 return efivar_set(name
, str
, persistent
);
116 EFI_STATUS
efivar_get(CHAR16
*name
, CHAR16
**value
) {
122 err
= efivar_get_raw(&loader_guid
, name
, &buf
, &size
);
126 val
= StrDuplicate((CHAR16
*)buf
);
129 return EFI_OUT_OF_RESOURCES
;
136 EFI_STATUS
efivar_get_int(CHAR16
*name
, UINTN
*i
) {
140 err
= efivar_get(name
, &val
);
141 if (!EFI_ERROR(err
)) {
148 EFI_STATUS
efivar_get_raw(const EFI_GUID
*vendor
, CHAR16
*name
, CHAR8
**buffer
, UINTN
*size
) {
153 l
= sizeof(CHAR16
*) * EFI_MAXIMUM_VARIABLE_SIZE
;
154 buf
= AllocatePool(l
);
156 return EFI_OUT_OF_RESOURCES
;
158 err
= uefi_call_wrapper(RT
->GetVariable
, 5, name
, (EFI_GUID
*)vendor
, NULL
, &l
, buf
);
159 if (!EFI_ERROR(err
)) {
169 VOID
efivar_set_time_usec(CHAR16
*name
, UINT64 usec
) {
177 SPrint(str
, 32, L
"%ld", usec
);
178 efivar_set(name
, str
, FALSE
);
181 static INTN
utf8_to_16(CHAR8
*stra
, CHAR16
*c
) {
188 else if ((stra
[0] & 0xe0) == 0xc0)
190 else if ((stra
[0] & 0xf0) == 0xe0)
192 else if ((stra
[0] & 0xf8) == 0xf0)
194 else if ((stra
[0] & 0xfc) == 0xf8)
196 else if ((stra
[0] & 0xfe) == 0xfc)
206 unichar
= stra
[0] & 0x1f;
209 unichar
= stra
[0] & 0x0f;
212 unichar
= stra
[0] & 0x07;
215 unichar
= stra
[0] & 0x03;
218 unichar
= stra
[0] & 0x01;
222 for (i
= 1; i
< len
; i
++) {
223 if ((stra
[i
] & 0xc0) != 0x80)
226 unichar
|= stra
[i
] & 0x3f;
233 CHAR16
*stra_to_str(CHAR8
*stra
) {
240 str
= AllocatePool((len
+ 1) * sizeof(CHAR16
));
247 utf8len
= utf8_to_16(stra
+ i
, str
+ strlen
);
249 /* invalid utf8 sequence, skip the garbage */
261 CHAR16
*stra_to_path(CHAR8
*stra
) {
268 str
= AllocatePool((len
+ 2) * sizeof(CHAR16
));
276 utf8len
= utf8_to_16(stra
+ i
, str
+ strlen
);
278 /* invalid utf8 sequence, skip the garbage */
283 if (str
[strlen
] == '/')
285 if (str
[strlen
] == '\\' && str
[strlen
-1] == '\\') {
286 /* skip double slashes */
298 CHAR8
*strchra(CHAR8
*s
, CHAR8 c
) {
306 INTN
file_read(EFI_FILE_HANDLE dir
, CHAR16
*name
, UINTN off
, UINTN size
, CHAR8
**content
) {
307 EFI_FILE_HANDLE handle
;
313 err
= uefi_call_wrapper(dir
->Open
, 5, dir
, &handle
, name
, EFI_FILE_MODE_READ
, 0ULL);
320 info
= LibFileInfo(handle
);
321 buflen
= info
->FileSize
+1;
327 err
= uefi_call_wrapper(handle
->SetPosition
, 2, handle
, off
);
332 buf
= AllocatePool(buflen
);
333 err
= uefi_call_wrapper(handle
->Read
, 3, handle
, &buflen
, buf
);
334 if (!EFI_ERROR(err
)) {
343 uefi_call_wrapper(handle
->Close
, 1, handle
);