commit 23cba4228f0a1a489116595eb1204c19d2f4f3c4
parent d172aebbf8014790fc55784d0ac29d6162d2a129
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 17 Mar 2024 13:51:03 +0900
fix compile options, use printf to print memory map
Diffstat:
4 files changed, 43 insertions(+), 22 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,9 +1,10 @@
CC = x86_64-w64-mingw32-gcc
-CFLAGS = -shared -nostdlib -mno-red-zone -fno-stack-protector \
+CFLAGS = -nostdlib -fpic -shared -mno-red-zone -fno-stack-protector \
+ -fno-builtin \
-mno-stack-arg-probe -Wall -e EfiMain
OBJCOPY = objcopy
SRC = hello.c utils.c
-HDR = uefi.h
+HDR = uefi.h stdarg.h
all: hello.efi
diff --git a/hello.c b/hello.c
@@ -10,12 +10,11 @@ EfiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *systab)
SystemTable = systab;
EFI_STATUS stat;
- CHAR16 s[19];
+ char s8[32];
// Output firmware bender.
SystemTable->ConOut->ClearScreen(SystemTable->ConOut);
- SystemTable->ConOut->OutputString(SystemTable->ConOut, SystemTable->FirmwareVendor);
- printf("--%s--%d--\n==%x==\n", "char *", 1259320, 0xdeadbeef);
+ printf("vendor: %s\n", wstr2str(SystemTable->FirmwareVendor, s8));
// Output memory map information.
UINTN mmsize = 8196;
@@ -24,7 +23,6 @@ EfiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *systab)
UINTN mkey;
UINTN dsize;
UINT32 dver;
-
mmap = (EFI_MEMORY_DESCRIPTOR *) mmbuf;
stat = SystemTable->BootServices->GetMemoryMap(&mmsize, mmap, &mkey, &dsize, &dver);
if (stat != EFI_SUCCESS) {
@@ -37,18 +35,10 @@ EfiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *systab)
continue;
}
printf("%d\t", mmap->Type);
- SystemTable->ConOut->OutputString(SystemTable->ConOut, sprinth(mmap->VirtualStart, s));
- s[0] = '\t'; s[1] = '\0';
- SystemTable->ConOut->OutputString(SystemTable->ConOut, s);
- SystemTable->ConOut->OutputString(SystemTable->ConOut, sprinth(mmap->PhysicalStart, s));
- s[0] = '\t'; s[1] = '\0';
- SystemTable->ConOut->OutputString(SystemTable->ConOut, s);
- SystemTable->ConOut->OutputString(SystemTable->ConOut, sprinth(mmap->NumberOfPages, s));
- s[0] = '\t'; s[1] = '\0';
- SystemTable->ConOut->OutputString(SystemTable->ConOut, s);
- SystemTable->ConOut->OutputString(SystemTable->ConOut, sprinth(mmap->Attribute, s));
- s[0] = '\n'; s[1] = '\r'; s[2] = '\0';
- SystemTable->ConOut->OutputString(SystemTable->ConOut, s);
+ printf("%x\t", mmap->VirtualStart);
+ printf("%x\t", mmap->PhysicalStart);
+ printf("%x\t", mmap->NumberOfPages);
+ printf("%x\n", mmap->Attribute);
}
// Echo back user input.
diff --git a/utils.c b/utils.c
@@ -1,5 +1,4 @@
#include <stdarg.h>
-
#include "uefi.h"
#include "utils.h"
@@ -21,6 +20,17 @@ sprinth(UINT64 n, CHAR16 s[19])
return s;
}
+char *
+wstr2str(CHAR16 *s16, char *s8)
+{
+ char *s = s8;
+ for (;*s16;) {
+ *s8++ = (char) (*s16++ & 0xff);
+ }
+ *s8 = '\0';
+ return s;
+}
+
int
printf(char *fmt, ...)
{
@@ -29,6 +39,7 @@ printf(char *fmt, ...)
EFI_STATUS stat;
char *s;
+ CHAR16 *w;
long long d, e;
unsigned long long f, g, h;
@@ -56,9 +67,18 @@ printf(char *fmt, ...)
*buf++ = (CHAR16) *s++;
}
break;
+ case 'w':
+ w = va_arg(ap, CHAR16 *);
+ for (;*s;) {
+ *buf++ = *w++;
+ }
+ break;
case 'd':
d = va_arg(ap, int);
- if (d < 0) {
+ if (d == 0) {
+ *buf++ = '0';
+ break;
+ } else if (d < 0) {
*buf++ = '-';
d = -d;
}
@@ -73,6 +93,10 @@ printf(char *fmt, ...)
break;
case 'x':
f = va_arg(ap, unsigned int);
+ if (f == 0) {
+ *buf++ = '0';
+ break;
+ }
for (g = 1; g <= f; g *= 0x10) {
if (g > g * 0x10) { // integer too big.
return -1;
@@ -80,7 +104,7 @@ printf(char *fmt, ...)
}
for (g /= 0x10; g >= 1; g /= 0x10) {
h = ((f / g) % 0x10);
- if (f < 0xa) {
+ if (h < 0xa) {
*buf++ = (CHAR16) ('0' + h);
} else {
*buf++ = (CHAR16) ('a' + h - 0xa);
@@ -88,10 +112,10 @@ printf(char *fmt, ...)
}
break;
}
-
}
va_end(ap);
*buf = L'\0';
+
stat = SystemTable->ConOut->OutputString(SystemTable->ConOut, _buf);
if (stat != EFI_SUCCESS) {
return -1;
diff --git a/utils.h b/utils.h
@@ -7,6 +7,12 @@ extern EFI_SYSTEM_TABLE *SystemTable;
// The second argument should have length of 19 ("0x" + "0123456789abcdef" + "\0").
CHAR16 *sprinth(UINT64, CHAR16[19]);
+// Wstr2str converts UTF16-encoded string s16 to string.
+// It ignores the high byte.
+// caller must ensure that s has enough space.
+// It returns s.
+char *wstr2str(CHAR16 *s16, char *s8);
+
// Printf prints formatted string to ConOut.
// Can't format a string longer than 1024 CHAR16s.
// It returns number of CHAR16s written excluding the final NULL.