1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * Copyright (C) 2012-2013 Kay Sievers <kay@vrfy.org>
15 * Copyright (C) 2012 Harald Hoyer <harald@redhat.com>
24 * Allocated random UUID, intended to be shared across tools that implement
25 * the (ESP)\loader\entries\<vendor>-<revision>.conf convention and the
26 * associated EFI variables.
28 static const EFI_GUID loader_guid
= { 0x4a67b082, 0x0a4c, 0x41cf, {0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f} };
31 UINT64
ticks_read(VOID
) {
33 __asm__
volatile ("rdtsc" : "=a" (a
), "=d" (d
));
37 UINT64
ticks_read(VOID
) {
39 __asm__
volatile ("rdtsc" : "=A" (val
));
44 /* count TSC ticks during a millisecond delay */
45 UINT64
ticks_freq(VOID
) {
46 UINT64 ticks_start
, ticks_end
;
48 ticks_start
= ticks_read();
49 uefi_call_wrapper(BS
->Stall
, 1, 1000);
50 ticks_end
= ticks_read();
52 return (ticks_end
- ticks_start
) * 1000;
55 UINT64
time_usec(VOID
) {
69 return 1000 * 1000 * ticks
/ freq
;
72 EFI_STATUS
parse_boolean(CHAR8
*v
, BOOLEAN
*b
) {
73 if (strcmpa(v
, (CHAR8
*)"1") == 0 ||
74 strcmpa(v
, (CHAR8
*)"yes") == 0 ||
75 strcmpa(v
, (CHAR8
*)"y") == 0 ||
76 strcmpa(v
, (CHAR8
*)"true") == 0) {
81 if (strcmpa(v
, (CHAR8
*)"0") == 0 ||
82 strcmpa(v
, (CHAR8
*)"no") == 0 ||
83 strcmpa(v
, (CHAR8
*)"n") == 0 ||
84 strcmpa(v
, (CHAR8
*)"false") == 0) {
89 return EFI_INVALID_PARAMETER
;
92 EFI_STATUS
efivar_set_raw(const EFI_GUID
*vendor
, CHAR16
*name
, CHAR8
*buf
, UINTN size
, BOOLEAN persistent
) {
95 flags
= EFI_VARIABLE_BOOTSERVICE_ACCESS
|EFI_VARIABLE_RUNTIME_ACCESS
;
97 flags
|= EFI_VARIABLE_NON_VOLATILE
;
99 return uefi_call_wrapper(RT
->SetVariable
, 5, name
, (EFI_GUID
*)vendor
, flags
, size
, buf
);
102 EFI_STATUS
efivar_set(CHAR16
*name
, CHAR16
*value
, BOOLEAN persistent
) {
103 return efivar_set_raw(&loader_guid
, name
, (CHAR8
*)value
, value
? (StrLen(value
)+1) * sizeof(CHAR16
) : 0, persistent
);
106 EFI_STATUS
efivar_set_int(CHAR16
*name
, UINTN i
, BOOLEAN persistent
) {
109 SPrint(str
, 32, L
"%d", i
);
110 return efivar_set(name
, str
, persistent
);
113 EFI_STATUS
efivar_get(CHAR16
*name
, CHAR16
**value
) {
119 err
= efivar_get_raw(&loader_guid
, name
, &buf
, &size
);
123 val
= StrDuplicate((CHAR16
*)buf
);
126 return EFI_OUT_OF_RESOURCES
;
133 EFI_STATUS
efivar_get_int(CHAR16
*name
, UINTN
*i
) {
137 err
= efivar_get(name
, &val
);
138 if (!EFI_ERROR(err
)) {
145 EFI_STATUS
efivar_get_raw(const EFI_GUID
*vendor
, CHAR16
*name
, CHAR8
**buffer
, UINTN
*size
) {
150 l
= sizeof(CHAR16
*) * EFI_MAXIMUM_VARIABLE_SIZE
;
151 buf
= AllocatePool(l
);
153 return EFI_OUT_OF_RESOURCES
;
155 err
= uefi_call_wrapper(RT
->GetVariable
, 5, name
, (EFI_GUID
*)vendor
, NULL
, &l
, buf
);
156 if (!EFI_ERROR(err
)) {
166 VOID
efivar_set_time_usec(CHAR16
*name
, UINT64 usec
) {
174 SPrint(str
, 32, L
"%ld", usec
);
175 efivar_set(name
, str
, FALSE
);
178 static INTN
utf8_to_16(CHAR8
*stra
, CHAR16
*c
) {
185 else if ((stra
[0] & 0xe0) == 0xc0)
187 else if ((stra
[0] & 0xf0) == 0xe0)
189 else if ((stra
[0] & 0xf8) == 0xf0)
191 else if ((stra
[0] & 0xfc) == 0xf8)
193 else if ((stra
[0] & 0xfe) == 0xfc)
203 unichar
= stra
[0] & 0x1f;
206 unichar
= stra
[0] & 0x0f;
209 unichar
= stra
[0] & 0x07;
212 unichar
= stra
[0] & 0x03;
215 unichar
= stra
[0] & 0x01;
219 for (i
= 1; i
< len
; i
++) {
220 if ((stra
[i
] & 0xc0) != 0x80)
223 unichar
|= stra
[i
] & 0x3f;
230 CHAR16
*stra_to_str(CHAR8
*stra
) {
237 str
= AllocatePool((len
+ 1) * sizeof(CHAR16
));
244 utf8len
= utf8_to_16(stra
+ i
, str
+ strlen
);
246 /* invalid utf8 sequence, skip the garbage */
258 CHAR16
*stra_to_path(CHAR8
*stra
) {
265 str
= AllocatePool((len
+ 2) * sizeof(CHAR16
));
273 utf8len
= utf8_to_16(stra
+ i
, str
+ strlen
);
275 /* invalid utf8 sequence, skip the garbage */
280 if (str
[strlen
] == '/')
282 if (str
[strlen
] == '\\' && str
[strlen
-1] == '\\') {
283 /* skip double slashes */
295 CHAR8
*strchra(CHAR8
*s
, CHAR8 c
) {
303 INTN
file_read(EFI_FILE_HANDLE dir
, CHAR16
*name
, UINTN off
, UINTN size
, CHAR8
**content
) {
304 EFI_FILE_HANDLE handle
;
310 err
= uefi_call_wrapper(dir
->Open
, 5, dir
, &handle
, name
, EFI_FILE_MODE_READ
, 0ULL);
317 info
= LibFileInfo(handle
);
318 buflen
= info
->FileSize
+1;
324 err
= uefi_call_wrapper(handle
->SetPosition
, 2, handle
, off
);
329 buf
= AllocatePool(buflen
);
330 err
= uefi_call_wrapper(handle
->Read
, 3, handle
, &buflen
, buf
);
331 if (!EFI_ERROR(err
)) {
340 uefi_call_wrapper(handle
->Close
, 1, handle
);