1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <efi.h>
26 #include <efilib.h>
27 #include <efistdarg.h>
28
29 #include <libavb/libavb.h>
30
31 #include "uefi_avb_util.h"
32
avb_memcmp(const void * src1,const void * src2,size_t n)33 int avb_memcmp(const void* src1, const void* src2, size_t n) {
34 return (int)CompareMem((VOID*)src1, (VOID*)src2, (UINTN)n);
35 }
36
avb_strcmp(const char * s1,const char * s2)37 int avb_strcmp(const char* s1, const char* s2) {
38 return (int)strcmpa((CHAR8*)s1, (CHAR8*)s2);
39 }
40
avb_memcpy(void * dest,const void * src,size_t n)41 void* avb_memcpy(void* dest, const void* src, size_t n) {
42 CopyMem(dest, (VOID*)src, (UINTN)n);
43 return dest;
44 }
45
avb_memset(void * dest,const int c,size_t n)46 void* avb_memset(void* dest, const int c, size_t n) {
47 SetMem(dest, (UINTN)n, (UINT8)c);
48 return dest;
49 }
50
avb_print(const char * message)51 void avb_print(const char* message) {
52 size_t utf8_num_bytes = avb_strlen(message) + 1;
53 size_t max_ucs2_bytes = utf8_num_bytes * 2;
54 uint16_t* message_ucs2 = (uint16_t*)avb_calloc(max_ucs2_bytes);
55 if (message_ucs2 == NULL) {
56 return;
57 }
58 if (uefi_avb_utf8_to_ucs2((const uint8_t*)message,
59 utf8_num_bytes,
60 message_ucs2,
61 max_ucs2_bytes,
62 NULL)) {
63 Print(message_ucs2);
64 }
65 avb_free(message_ucs2);
66 }
67
avb_printv(const char * message,...)68 void avb_printv(const char* message, ...) {
69 va_list ap;
70
71 va_start(ap, message);
72 do {
73 avb_print(message);
74 message = va_arg(ap, const char*);
75 } while (message != NULL);
76 va_end(ap);
77 }
78
avb_abort(void)79 void avb_abort(void) {
80 avb_print("\nABORTING...\n");
81 uefi_call_wrapper(BS->Stall, 1, 5 * 1000 * 1000);
82 uefi_call_wrapper(BS->Exit, 4, NULL, EFI_NOT_FOUND, 0, NULL);
83 while (true) {
84 ;
85 }
86 }
87
avb_malloc_(size_t size)88 void* avb_malloc_(size_t size) {
89 EFI_STATUS err;
90 void* x;
91
92 err = uefi_call_wrapper(
93 BS->AllocatePool, 3, EfiBootServicesData, (UINTN)size, &x);
94 if (EFI_ERROR(err)) {
95 return NULL;
96 }
97
98 return x;
99 }
100
avb_free(void * ptr)101 void avb_free(void* ptr) {
102 EFI_STATUS err;
103 err = uefi_call_wrapper(BS->FreePool, 1, ptr);
104
105 if (EFI_ERROR(err)) {
106 Print(L"Warning: Bad avb_free: %r\n", err);
107 uefi_call_wrapper(BS->Stall, 1, 3 * 1000 * 1000);
108 }
109 }
110
avb_strlen(const char * str)111 size_t avb_strlen(const char* str) {
112 return strlena((CHAR8*)str);
113 }
114