-
Notifications
You must be signed in to change notification settings - Fork 0
/
std.c
235 lines (204 loc) · 4.89 KB
/
std.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "uefi.h"
int putchar(char c)
{
if (c == '\n') putchar('\r');
UINT16 String[2] = {c, 0};
gST->ConOut->OutputString(gST->ConOut, String);
return c;
}
int getchar()
{
UINTN index;
gBS->WaitForEvent(1, &gST->ConIn->WaitForKey, &index);
EFI_INPUT_KEY key = {0, 0};
EFI_STATUS status = gST->ConIn->ReadKeyStroke(gST->ConIn, &key);
return EFI_ERROR(status) ? -1 : key.UnicodeChar & 0xff;
}
void puts(char *s)
{
for (; *s; s++) putchar(*s);
}
void *memcpy(void *dest, const void *src, int size)
{
UINT8 *dst = (UINT8 *) dest;
UINT8 *source = (UINT8 *) src;
while (size-- > 0) *dst++ = *source++;
return dest;
}
void memset(void *dest, UINT8 value, int size)
{
UINT8 *dst = (UINT8 *) dest;
while (size-- > 0) *dst++ = value;
}
int memcmp(const void *a, const void *b, size_t size)
{
const UINT8 *a_ = a;
const UINT8 *b_ = b;
while (size-- > 0) {
if (*a_ != *b_) return *a_ > *b_ ? 1 : -1;
a_++;
b_++;
}
return 0;
}
char *strcpy(char *dest, const char *src)
{
char *r = dest;
while ((*dest++ = *src++));
return r;
}
int strlen(const char *str)
{
const char *p = str;
while (*p++);
return p - str - 1;
}
char *strchr(const char *str, const uint8_t ch)
{
while (*str) {
if (*str == ch) return (char *) str;
str++;
}
return NULL;
}
char *strstr(char *buf, char *sub)
{
if (!*sub) return (char *) buf;
char *bp, *sp;
while (*buf)
{
bp = buf;
sp = sub;
do {
if (!*sp) return buf;
} while ((*bp++ == *sp++));
buf++;
}
return 0;
}
void itoa(UINT32 value, char **buf_ptr_addr, UINT8 base)
{
UINT32 m = value % base;
UINT32 i = value / base;
if (i) itoa(i, buf_ptr_addr, base);
if (m < 10) {
*((*buf_ptr_addr)++) = m + '0';
} else {
*((*buf_ptr_addr)++) = m - 10 + 'A';
}
}
int vsprintf(char *str, const char *format, va_list ap)
{
char *buf_ptr = str;
const char *index_ptr = format;
char index_char = *index_ptr;
int arg_int;
char *arg_str;
while (index_char) {
if (index_char != '%') {
*(buf_ptr++) = index_char;
index_char = *(++index_ptr);
continue;
}
index_char = *(++index_ptr);
switch (index_char) {
case 'x':
arg_int = va_arg(ap, int);
itoa(arg_int, &buf_ptr, 16);
index_char = *(++index_ptr);
break;
case 'c':
*(buf_ptr++) = va_arg(ap, int);
index_char = *(++index_ptr);
break;
case 'd':
arg_int = va_arg(ap, int);
if (arg_int < 0) {
arg_int = -arg_int;
*buf_ptr++ = '-';
}
itoa(arg_int, &buf_ptr, 10);
index_char = *(++index_ptr);
break;
case 's':
arg_str = va_arg(ap, char*);
strcpy(buf_ptr, arg_str);
buf_ptr += strlen(arg_str);
index_char = *(++index_ptr);
break;
}
}
return strlen(str);
}
int sprintf(char *s, const char *format, ...)
{
va_list args;
va_start(args, format);
int i = vsprintf(s, format, args);
va_end(args);
return i;
}
int vprintf(const char *format, va_list args)
{
char buf[1024] = {0};
int ret = vsprintf(buf, format, args);
puts(buf);
return ret;
}
int printf(const char *format, ...)
{
va_list args;
va_start(args, format);
char buf[1024] = {0};
int ret = vsprintf(buf, format, args);
va_end(args);
puts(buf);
return ret;
}
void *malloc(size_t size)
{
void *ret = NULL;
EFI_STATUS status;
status = gBS->AllocatePool(EfiLoaderData, size, &ret);
if (EFI_ERROR(status) || !ret) ret = NULL;
return ret;
}
void *calloc(size_t nmemb, size_t size)
{
void *ret = malloc(nmemb * size);
if (ret) memset(ret, 0, nmemb * size);
return ret;
}
void *realloc(void *ptr, size_t size)
{
void *ret = NULL;
if (!ptr) return malloc(size);
if (!size) {
free(ptr);
return NULL;
}
ret = malloc(size);
if (!ret) return NULL;
memcpy(ret, ptr, size);
free(ptr);
return ret;
}
void free(void *buffer)
{
gBS->FreePool(buffer);
}
void exit(int status)
{
gBS->Exit(IM, !status ? 0 : (status < 0 ? EFIERR(-status) : EFIERR(status)), 0, NULL);
}
int usleep(uint32_t microseconds)
{
gBS->Stall(microseconds);
return 0;
}
int sleep(uint32_t seconds)
{
gBS->Stall(seconds * 1000000);
return 0;
}
void ___chkstk_ms() {}