Skip to content

Commit dfb0bdc

Browse files
committed
Clean Up
Signed-off-by: Jiaxun Yang <[email protected]>
1 parent 7ec97a9 commit dfb0bdc

File tree

9 files changed

+57
-42
lines changed

9 files changed

+57
-42
lines changed

GNUmakefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ NASMFLAGS := -F dwarf -g
3535
# User controllable linker flags. We set none by default.
3636
LDFLAGS :=
3737

38+
# User controllable version string.
39+
BUILD_VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "Unknown")
40+
3841
# Check if CC is Clang.
3942
override CC_IS_CLANG := $(shell ! $(CC) --version 2>/dev/null | grep 'clang' >/dev/null 2>&1; echo $$?)
4043

@@ -65,6 +68,7 @@ endif
6568
override CPPFLAGS := \
6669
-I src \
6770
-I nyu-efi/inc \
71+
-DBUILD_VERSION=\"$(BUILD_VERSION)\" \
6872
-isystem freestnd-c-hdrs \
6973
$(CPPFLAGS) \
7074
-MMD \

src/acpi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ int copy_rsdt(struct csmwrap_priv *priv)
2727
void *table_target = priv->csm_bin + (priv->csm_efi_table->AcpiRsdPtrPointer - priv->csm_bin_base);
2828

2929

30-
for (i = 0; i < ST->NumberOfTableEntries; i++) {
30+
for (i = 0; i < gST->NumberOfTableEntries; i++) {
3131
EFI_CONFIGURATION_TABLE *table;
32-
table = ST->ConfigurationTable + i;
32+
table = gST->ConfigurationTable + i;
3333

3434
if (!efi_guidcmp(table->VendorGuid, acpi2Guid)) {
3535
printf("Found ACPI 2.0 RSDT at %x, copied to %x\n", (uintptr_t)table->VendorTable, (uintptr_t)table_target);

src/csmwrap.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
// Generated by: xxd -i vgabios.bin >> vgabios.h
1111
#include "bins/vgabios.h"
1212

13-
const char *banner = "CSMWrap\n"
13+
#ifndef BUILD_VERSION
14+
#define BUILD_VERSION "Unknown"
15+
#endif
16+
17+
const char *banner = "CSMWrap Version " BUILD_VERSION "\n"
1418
"https://github.com/flygoat/csmwrap\n"
1519
"By: Jiaxun Yang <[email protected]>\n";
1620

17-
EFI_SYSTEM_TABLE *ST;
18-
EFI_BOOT_SERVICES *BS;
19-
20-
#define BIOS_ROM_BASE 0xc0000
21+
EFI_SYSTEM_TABLE *gST;
22+
EFI_BOOT_SERVICES *gBS;
2123

2224
struct csmwrap_priv priv = {
2325
.csm_bin = Csm16_bin,
@@ -72,9 +74,9 @@ int set_smbios_table()
7274
bool found = FALSE;
7375
uintptr_t table_addr = 0;
7476

75-
for (i = 0; i < ST->NumberOfTableEntries; i++) {
77+
for (i = 0; i < gST->NumberOfTableEntries; i++) {
7678
EFI_CONFIGURATION_TABLE *table;
77-
table = ST->ConfigurationTable + i;
79+
table = gST->ConfigurationTable + i;
7880

7981
if (!efi_guidcmp(table->VendorGuid, smbiosGuid)) {
8082
printf("Found SMBIOS Table at %x\n", (uintptr_t)table->VendorTable);
@@ -107,18 +109,18 @@ int set_smbios_table()
107109

108110
EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
109111
{
110-
ST = SystemTable;
111-
BS = SystemTable->BootServices;
112-
113112
EFI_PHYSICAL_ADDRESS HiPmm;
114113
uintptr_t csm_bin_base;
115114
EFI_STATUS Status;
116115
EFI_IA32_REGISTER_SET Regs;
117116

117+
gST = SystemTable;
118+
gBS = SystemTable->BootServices;
119+
118120
printf("%s", banner);
119121

120-
BS->RaiseTPL(TPL_NOTIFY);
121-
BS->SetWatchdogTimer(0, 0, 0, NULL);
122+
gBS->RaiseTPL(TPL_NOTIFY);
123+
gBS->SetWatchdogTimer(0, 0, 0, NULL);
122124

123125
if (unlock_bios_region()) {
124126
printf("Unable to unlock BIOS region\n");
@@ -153,7 +155,7 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
153155
}
154156

155157
HiPmm = 0xffffffff;
156-
if (BS->AllocatePages(AllocateMaxAddress, EfiRuntimeServicesData, HIPMM_SIZE / EFI_PAGE_SIZE, &HiPmm) != EFI_SUCCESS) {
158+
if (gBS->AllocatePages(AllocateMaxAddress, EfiRuntimeServicesData, HIPMM_SIZE / EFI_PAGE_SIZE, &HiPmm) != EFI_SUCCESS) {
157159
printf("Unable to alloc HiPmm!!!\n");
158160
return -1;
159161
}
@@ -197,15 +199,15 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
197199
EFI_MEMORY_DESCRIPTOR *efi_mmap;
198200
EFI_MEMORY_DESCRIPTOR tmp_mmap[1];
199201
efi_mmap_size = sizeof(tmp_mmap);
200-
BS->GetMemoryMap(&efi_mmap_size, tmp_mmap, &efi_mmap_key, &efi_desc_size, &efi_desc_ver);
202+
gBS->GetMemoryMap(&efi_mmap_size, tmp_mmap, &efi_mmap_key, &efi_desc_size, &efi_desc_ver);
201203
efi_mmap_size += 4096;
202-
BS->AllocatePool(EfiLoaderData, efi_mmap_size, (void **)&efi_mmap);
203-
BS->GetMemoryMap(&efi_mmap_size, efi_mmap, &efi_mmap_key, &efi_desc_size, &efi_desc_ver);
204+
gBS->AllocatePool(EfiLoaderData, efi_mmap_size, (void **)&efi_mmap);
205+
gBS->GetMemoryMap(&efi_mmap_size, efi_mmap, &efi_mmap_key, &efi_desc_size, &efi_desc_ver);
204206

205207
// It may take N amounts of ExitBootServices() calls to complete...
206208
// Cap at 128.
207209
for (size_t i = 0; i < 128; i++) {
208-
Status = BS->ExitBootServices(ImageHandle, efi_mmap_key);
210+
Status = gBS->ExitBootServices(ImageHandle, efi_mmap_key);
209211
if (Status == EFI_SUCCESS) {
210212
break;
211213
}

src/csmwrap.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#include <libc.h>
99
#include "x86thunk.h"
1010

11-
extern EFI_SYSTEM_TABLE *ST;
12-
extern EFI_BOOT_SERVICES *BS;
11+
extern EFI_SYSTEM_TABLE *gST;
12+
extern EFI_BOOT_SERVICES *gBS;
1313

1414
struct csmwrap_priv {
1515
uint8_t *csm_bin;
@@ -87,4 +87,14 @@ struct low_stub {
8787
#define ALIGN_DOWN(x, a) ((x) & ~((__typeof__(x))(a)-1UL))
8888
#define IS_ALIGNED(x, a) (((x) & ((__typeof__(x))(a)-1UL)) == 0)
8989

90+
#ifdef ACCESS_PAGE0_CODE
91+
# undef ACCESS_PAGE0_CODE
92+
#endif
93+
94+
#define ACCESS_PAGE0_CODE(statements) \
95+
do { \
96+
statements; \
97+
\
98+
} while (FALSE)
99+
90100
#endif

src/e820.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int build_e820_map(struct csmwrap_priv *priv)
5454
uint32_t e820_entries = 0;
5555

5656
/* First call to get the buffer size */
57-
status = BS->GetMemoryMap(&memory_map_size, NULL, &map_key, &descriptor_size, &descriptor_version);
57+
status = gBS->GetMemoryMap(&memory_map_size, NULL, &map_key, &descriptor_size, &descriptor_version);
5858
if (status != EFI_BUFFER_TOO_SMALL) {
5959
printf("Unexpected GetMemoryMap status: %lx\n", status);
6060
return -1;
@@ -64,17 +64,17 @@ int build_e820_map(struct csmwrap_priv *priv)
6464
memory_map_size += descriptor_size * 10;
6565

6666
/* Allocate memory for the UEFI memory map */
67-
status = BS->AllocatePool(EfiLoaderData, memory_map_size, (void **)&memory_map);
67+
status = gBS->AllocatePool(EfiLoaderData, memory_map_size, (void **)&memory_map);
6868
if (EFI_ERROR(status)) {
6969
printf("Failed to allocate memory for memory map: %lx\n", status);
7070
return -1;
7171
}
7272

7373
/* Get the actual memory map */
74-
status = BS->GetMemoryMap(&memory_map_size, memory_map, &map_key, &descriptor_size, &descriptor_version);
74+
status = gBS->GetMemoryMap(&memory_map_size, memory_map, &map_key, &descriptor_size, &descriptor_version);
7575
if (EFI_ERROR(status)) {
7676
printf("Failed to get memory map: %lx\n", status);
77-
BS->FreePool(memory_map);
77+
gBS->FreePool(memory_map);
7878
return -1;
7979
}
8080

@@ -115,7 +115,7 @@ int build_e820_map(struct csmwrap_priv *priv)
115115
}
116116

117117
/* Free the UEFI memory map */
118-
BS->FreePool(memory_map);
118+
gBS->FreePool(memory_map);
119119

120120
/* Save the number of entries in the low_stub */
121121
priv->low_stub->e820_entries = e820_entries;

src/printf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static void _putchar(int character, void *extra_arg) {
2323
CHAR16 string[2];
2424
string[0] = character;
2525
string[1] = 0;
26-
ST->ConOut->OutputString(ST->ConOut, string);
26+
gST->ConOut->OutputString(gST->ConOut, string);
2727
}
2828

2929
int printf(const char *restrict fmt, ...) {

src/unlock_region.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
uint32_t granularity;
4141

4242
/* Look for the Legacy Region 2 Protocol */
43-
status = BS->LocateProtocol(
43+
status = gBS->LocateProtocol(
4444
&gEfiLegacyRegion2ProtocolGuid,
4545
NULL,
4646
(void **)&legacy_region
@@ -233,7 +233,7 @@
233233
EFI_STATUS status;
234234

235235
/* First, try to use the Legacy Region 2 Protocol */
236-
status = BS->LocateProtocol(
236+
status = gBS->LocateProtocol(
237237
&gEfiLegacyRegion2ProtocolGuid,
238238
NULL,
239239
(void **)&legacy_region

src/video.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ EFI_STATUS FindGopPciDevice(struct csmwrap_priv *priv)
1717
EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop;
1818

1919
// Get all handles that support GOP
20-
Status = BS->LocateHandleBuffer(
20+
Status = gBS->LocateHandleBuffer(
2121
ByProtocol,
2222
&gopGuid,
2323
NULL,
@@ -32,7 +32,7 @@ EFI_STATUS FindGopPciDevice(struct csmwrap_priv *priv)
3232
// Iterate through each GOP handle
3333
for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
3434
// Get the GOP protocol
35-
Status = BS->HandleProtocol(
35+
Status = gBS->HandleProtocol(
3636
HandleBuffer[HandleIndex],
3737
&gopGuid,
3838
(VOID**)&Gop
@@ -48,23 +48,23 @@ EFI_STATUS FindGopPciDevice(struct csmwrap_priv *priv)
4848
if (priv->gop == NULL) {
4949
printf("No GOP handle found\n");
5050
// Free the handle buffer
51-
BS->FreePool(HandleBuffer);
51+
gBS->FreePool(HandleBuffer);
5252
goto Out;
5353
}
5454

55-
Status = BS->HandleProtocol(
55+
Status = gBS->HandleProtocol(
5656
HandleBuffer[HandleIndex],
5757
&DevicePathGuid,
5858
(VOID**)&DevicePath
5959
);
6060
// We are done with previous handle buffer atm
61-
BS->FreePool(HandleBuffer);
61+
gBS->FreePool(HandleBuffer);
6262
if (EFI_ERROR(Status)) {
6363
printf("Failed to get Device Path protocol: %d\n", Status);
6464
goto Out;
6565
}
6666

67-
Status = BS->LocateDevicePath(
67+
Status = gBS->LocateDevicePath(
6868
&PciIoGuid,
6969
&DevicePath,
7070
&Handle
@@ -75,7 +75,7 @@ EFI_STATUS FindGopPciDevice(struct csmwrap_priv *priv)
7575
goto Out;
7676
}
7777

78-
Status = BS->HandleProtocol(
78+
Status = gBS->HandleProtocol(
7979
Handle,
8080
&PciIoGuid,
8181
(VOID**)&PciIo

src/x86thunk.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include <libc.h>
1212
#include <printf.h>
13-
#include "x86thunk.h"
13+
#include "csmwrap.h"
1414

1515
// FIXME: Are we going to implement it?
1616
#define ASSERT(x)
@@ -252,7 +252,7 @@ THUNK_CONTEXT mThunkContext;
252252

253253
bool InternalLegacyBiosFarCall (uint16_t Segment, uint16_t Offset, EFI_IA32_REGISTER_SET *Regs, void *Stack, uintptr_t StackSize)
254254
{
255-
uintptr_t Status;
255+
// uintptr_t Status;
256256
uint16_t *Stack16;
257257
// EFI_TPL OriginalTpl;
258258
IA32_REGISTER_SET ThunkRegSet;
@@ -300,7 +300,7 @@ bool InternalLegacyBiosFarCall (uint16_t Segment, uint16_t Offset, EFI_IA32_REGI
300300
//
301301
// The call to Legacy16 is a critical section to EFI
302302
//
303-
// OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
303+
// OriginalTpl = ggBS->RaiseTPL (TPL_HIGH_LEVEL);
304304

305305
//
306306
// Check to see if there is more than one HW interrupt registers with the CPU AP.
@@ -389,7 +389,7 @@ bool InternalLegacyBiosFarCall (uint16_t Segment, uint16_t Offset, EFI_IA32_REGI
389389
//
390390
// End critical section
391391
//
392-
// gBS->RestoreTPL (OriginalTpl);
392+
// ggBS->RestoreTPL (OriginalTpl);
393393

394394
//
395395
// OPROM may allocate EBDA range by itself and change EBDA base and EBDA size.
@@ -469,7 +469,7 @@ bool LegacyBiosInt86(uint8_t BiosInt, EFI_IA32_REGISTER_SET *Regs)
469469
Regs->X.Flags.TF = 0;
470470
Regs->X.Flags.CF = 0;
471471

472-
#if 0
472+
473473
//
474474
// The base address of legacy interrupt vector table is 0.
475475
// We use this base address to get the legacy interrupt handler.
@@ -478,7 +478,6 @@ bool LegacyBiosInt86(uint8_t BiosInt, EFI_IA32_REGISTER_SET *Regs)
478478
Segment = (UINT16)(((UINT32 *)0)[BiosInt] >> 16);
479479
Offset = (UINT16)((UINT32 *)0)[BiosInt];
480480
);
481-
#endif
482481

483482
return InternalLegacyBiosFarCall (
484483
Segment,

0 commit comments

Comments
 (0)