Skip to content

Commit f60bc7b

Browse files
committed
Impl PMEM experimental code
1 parent 291ee20 commit f60bc7b

13 files changed

+441
-196
lines changed

.clang-format

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BasedOnStyle: Chromium

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ run_pmem : src/BOOTX64.EFI pmem.img
4242

4343
clean :
4444
make -C src clean
45+
46+
format :
47+
make -C src format

src/Makefile

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
OBJS=efimain.o
2-
HEADERS=efi.h
1+
OBJS=efi.o guid.o liumos.o static.o
2+
HEADERS=efi.h acpi.h liumos.h
33
OVMF=ovmf/bios64.bin
44
QEMU=qemu-system-x86_64
55

@@ -26,7 +26,7 @@ default: BOOTX64.EFI
2626

2727
BOOTX64.EFI : $(OBJS) $(HEADERS) Makefile
2828
$(LD) \
29-
-subsystem:efi_application -nodefaultlib -dll \
29+
-subsystem:efi_application -nodefaultlib \
3030
-entry:efi_main $(OBJS) -out:$@
3131

3232
run:
@@ -36,3 +36,6 @@ clean :
3636
-rm *.EFI
3737
-rm *.lib
3838
-rm *.o
39+
40+
format :
41+
clang-format -i *.c *.h

src/acpi.h

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#pragma once
2+
3+
#include "generic.h"
4+
#include "guid.h"
5+
6+
#define packed_struct struct __attribute__((__packed__))
7+
#define ACPI_DESCRIPTION_HEADER_SIZE 36
8+
9+
typedef struct ACPI_ROOT_SYSTEM_DESCRIPTION_POINTER ACPI_RSDP;
10+
typedef struct ACPI_EXTENDED_SYSTEM_DESCRIPTION_TABLE ACPI_XSDT;
11+
typedef struct ACPI_NVDIMM_FIRMWARE_INTERFACE_TABLE ACPI_NFIT;
12+
typedef struct ACPI_NFIT_SYSTEM_PHYSICAL_ADDRESS_RANGE_STRUCTURE
13+
ACPI_NFIT_SPARange;
14+
15+
packed_struct ACPI_ROOT_SYSTEM_DESCRIPTION_POINTER {
16+
char signature[8];
17+
uint8_t checksum;
18+
uint8_t oem_id[6];
19+
uint8_t revision;
20+
uint32_t rsdt_address;
21+
uint32_t length;
22+
ACPI_XSDT* xsdt;
23+
uint8_t extended_checksum;
24+
uint8_t reserved;
25+
};
26+
27+
packed_struct ACPI_EXTENDED_SYSTEM_DESCRIPTION_TABLE {
28+
char signature[4];
29+
uint32_t length;
30+
uint8_t revision;
31+
uint8_t checksum;
32+
uint8_t oem_id[6];
33+
uint64_t oem_table_id;
34+
uint32_t oem_revision;
35+
uint32_t creator_id;
36+
uint32_t creator_revision;
37+
void* entry[];
38+
};
39+
40+
enum ACPI_NFITStructureType {
41+
kSystemPhysicalAddressRangeStructure,
42+
kMemoryDeviceToSystemAddressRangeMapStructure,
43+
kInterleaveStructure,
44+
kSMBIOSManagementInformationStructure,
45+
kNVDIMMControlRegionStructure,
46+
kNVDIMMBlockDataWindowRegionStructure,
47+
kFlushHintAddressStructure,
48+
};
49+
50+
packed_struct ACPI_NVDIMM_FIRMWARE_INTERFACE_TABLE {
51+
char signature[4];
52+
uint32_t length;
53+
uint8_t revision;
54+
uint8_t checksum;
55+
uint8_t oem_id[6];
56+
uint64_t oem_table_id;
57+
uint32_t oem_revision;
58+
uint32_t creator_id;
59+
uint32_t creator_revision;
60+
uint32_t reserved;
61+
uint16_t entry[];
62+
};
63+
64+
packed_struct ACPI_NFIT_SYSTEM_PHYSICAL_ADDRESS_RANGE_STRUCTURE {
65+
uint16_t type;
66+
uint16_t length;
67+
uint16_t spa_range_structure_index;
68+
uint16_t flags;
69+
uint32_t reserved;
70+
uint32_t proximity_domain;
71+
uint64_t address_range_type_guid[2];
72+
uint64_t system_physical_address_range_base;
73+
uint64_t system_physical_address_range_length;
74+
uint64_t address_range_memory_mapping_attribute;
75+
};

src/efi.c

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include "efi.h"
2+
3+
#define MEMORY_MAP_ENTRIES 64
4+
5+
EFISystemTable* _system_table;
6+
EFIMemoryDescriptor memory_map[MEMORY_MAP_ENTRIES];
7+
EFI_UINTN memory_map_used;
8+
9+
bool IsEqualStringWithSize(const char* s1, const char* s2, int n) {
10+
for (int i = 0; i < n; i++) {
11+
if (s1[i] != s2[i])
12+
return false;
13+
}
14+
return true;
15+
}
16+
17+
void EFIInit(struct EFI_SYSTEM_TABLE* system_table) {
18+
_system_table = system_table;
19+
}
20+
21+
void EFIClearScreen() {
22+
_system_table->con_out->clear_screen(_system_table->con_out);
23+
}
24+
25+
void EFIPutString(wchar_t* s) {
26+
_system_table->con_out->output_string(_system_table->con_out, s);
27+
}
28+
29+
void EFIPutCString(char* s) {
30+
wchar_t buf[2];
31+
buf[1] = 0;
32+
while (*s) {
33+
buf[0] = *s;
34+
_system_table->con_out->output_string(_system_table->con_out, buf);
35+
s++;
36+
}
37+
}
38+
39+
void EFIPutnCString(char* s, int n) {
40+
wchar_t buf[2];
41+
buf[1] = 0;
42+
for (int i = 0; i < n; i++) {
43+
buf[0] = s[i];
44+
_system_table->con_out->output_string(_system_table->con_out, buf);
45+
}
46+
}
47+
48+
void EFIGetMemoryMap() {
49+
EFI_UINTN map_key;
50+
EFI_UINTN desc_size;
51+
uint32_t desc_version;
52+
EFI_UINTN memory_map_byte_size =
53+
sizeof(EFIMemoryDescriptor) * MEMORY_MAP_ENTRIES;
54+
_system_table->boot_services->GetMemoryMap(
55+
&memory_map_byte_size, memory_map, &map_key, &desc_size, &desc_version);
56+
memory_map_used = memory_map_byte_size / desc_size;
57+
}
58+
59+
void EFIPrintHex64(uint64_t value) {
60+
int i;
61+
wchar_t s[2];
62+
s[1] = 0;
63+
for (i = 15; i > 0; i--) {
64+
if ((value >> (4 * i)) & 0xF)
65+
break;
66+
}
67+
for (; i >= 0; i--) {
68+
s[0] = (value >> (4 * i)) & 0xF;
69+
if (s[0] < 10)
70+
s[0] += '0';
71+
else
72+
s[0] += 'A' - 10;
73+
EFIPutString(s);
74+
}
75+
}
76+
77+
void EFIPrintStringAndHex(wchar_t* s, uint64_t value) {
78+
EFIPutString(s);
79+
EFIPutString(L": 0x");
80+
EFIPrintHex64(value);
81+
EFIPutString(L"\r\n");
82+
}
83+
84+
void EFIPrintMemoryDescriptor(EFIMemoryDescriptor* desc) {
85+
EFIPutString(L" Type ");
86+
EFIPrintHex64(desc->type);
87+
EFIPutString(L" Phys ");
88+
EFIPrintHex64(desc->physical_start);
89+
EFIPutString(L" Virt ");
90+
EFIPrintHex64(desc->virtual_start);
91+
EFIPutString(L" NumOfPages ");
92+
EFIPrintHex64(desc->number_of_pages);
93+
EFIPutString(L" Attr ");
94+
EFIPrintHex64(desc->attribute);
95+
}
96+
97+
void EFIPrintMemoryMap() {
98+
EFIPrintStringAndHex(L"Map entries: ", memory_map_used);
99+
for (int i = 0; i < memory_map_used; i++) {
100+
EFIPutString(L"#");
101+
EFIPrintHex64(i);
102+
EFIPrintMemoryDescriptor(&memory_map[i]);
103+
EFIPutString(L"\r\n");
104+
}
105+
EFIPutString(L"NV Map entries: \r\n");
106+
for (int i = 0; i < memory_map_used; i++) {
107+
if (!(memory_map[i].attribute & EFI_MEMORY_NV))
108+
continue;
109+
EFIPutString(L"#");
110+
EFIPrintHex64(i);
111+
EFIPrintMemoryDescriptor(&memory_map[i]);
112+
EFIPutString(L"\r\n");
113+
}
114+
}
115+
116+
void* EFIGetConfigurationTableByUUID(const GUID* guid) {
117+
for (int i = 0; i < _system_table->number_of_table_entries; i++) {
118+
if (IsEqualGUID(guid, &_system_table->configuration_table[i].vendor_guid))
119+
return _system_table->configuration_table[i].vendor_table;
120+
}
121+
return NULL;
122+
}

0 commit comments

Comments
 (0)