some modify

This commit is contained in:
Chiharu Shirasaka 2021-04-09 18:13:55 +09:00
parent d6b01a0ac5
commit 1b4d5fb6a4
30 changed files with 569 additions and 55 deletions

4
.gitignore vendored Normal file → Executable file
View File

@ -1 +1,5 @@
.vscode/settings.json .vscode/settings.json
**/a.out
mkramfs/ramfs.img
mkramfs/mkramfs
FAT/

0
LICENSE Normal file → Executable file
View File

2
README.md Normal file → Executable file
View File

@ -18,4 +18,4 @@ Challenge of handmade OS.
Conf/target.txtのACTIVE_PLATFORMをsprinkleos/sprinkleos.dscに TARGET_ARCHをX64に TOOL_CHAIN_TAGをGCC5に Conf/target.txtのACTIVE_PLATFORMをsprinkleos/sprinkleos.dscに TARGET_ARCHをX64に TOOL_CHAIN_TAGをGCC5に
```build``` ```build```
出来上がったBuild/SprinkleOSX64/DEBUG_GCC5/X64/sprinkleos.efiを上手いこと起動 出来上がったBuild/SprinkleOSX64/DEBUG_GCC5/X64/sprinkleos.efiを上手いこと起動
--> -->

30
elf.c Executable file
View File

@ -0,0 +1,30 @@
#include "elf.h"
#include "text.h"
#include <Library/BaseMemoryLib.h>
void run(char *elfdata) {
if (elfdata[EI_CLASS] == ELFCLASS32) {
Elf32_Ehdr elf32header;
CopyMem(&elf32header, elfdata, sizeof(elf32header));
if (!IS_ELF(elf32header)) {
print("This is not ELF file!");
}
if (elf32header.e_ident[EI_OSABI] != ELFOSABI_LINUX &&
elf32header.e_ident[EI_OSABI] != ELFOSABI_NONE) {
print("This ELF file is not valid OS ABI!");
}
if (elf32header.e_machine != EM_386 &&
elf32header.e_machine != EM_486 &&
elf32header.e_machine != EM_X86_64) {
print("This ELF file is not for x86 or x86_64!");
}
if (elf32header.e_type == ET_REL) {
} else if (elf32header.e_type == ET_EXEC) {
} else if (elf32header.e_type == ET_DYN) {
}
} else if (elfdata[EI_CLASS] == ELFCLASS64) {
Elf64_Ehdr elf64header;
CopyMem(&elf64header, elfdata, sizeof(elf64header));
}
}

3
elf.h Executable file
View File

@ -0,0 +1,3 @@
#include <../BaseTools/Source/C/GenFw/elf_common.h>
#include <../BaseTools/Source/C/GenFw/elf32.h>
#include <../BaseTools/Source/C/GenFw/elf64.h>

0
fb.c Normal file → Executable file
View File

0
fb.h Normal file → Executable file
View File

0
font.h Normal file → Executable file
View File

0
gdt.c Normal file → Executable file
View File

0
gdt.h Normal file → Executable file
View File

0
int_handler.s Normal file → Executable file
View File

91
interrupt.c Normal file → Executable file
View File

@ -1,6 +1,7 @@
#include <Library/BaseLib.h> #include <Library/BaseLib.h>
#include "io.h" #include "io.h"
#include "keycode.h"
#include "text.h" #include "text.h"
#define SPECIAL_SHIFT_L 0b00000001 #define SPECIAL_SHIFT_L 0b00000001
#define SPECIAL_SHIFT_R 0b00000010 #define SPECIAL_SHIFT_R 0b00000010
@ -82,41 +83,81 @@ void interrupt_init() {
void keyboard_int_proc() { void keyboard_int_proc() {
UINT8 data; UINT8 data;
// static char keyboard_special_keys = 0; static BOOLEAN special_flag = FALSE;
static char keyboard_special_keys = 0;
data = mapped_io_read(0x64); data = mapped_io_read(0x64);
if ((data & 0b00000001) == 0) // output buffer is not full if ((data & 0b00000001) == 0) // output buffer is not full
goto eoi; goto eoi;
data = mapped_io_read(0x60); data = mapped_io_read(0x60);
/* if (data == 0xE0) {
if (data & 0b10000000){ // key is broken special_flag = TRUE;
goto eoi;
}
if (data & 0b10000000) { // key is broken
switch (data & 0b01111111) { switch (data & 0b01111111) {
case 44: case 0x2A:
keyboard_special_keys &= ~SPECIAL_SHIFT_L; keyboard_special_keys &= ~SPECIAL_SHIFT_L;
return; goto eoi;
case 57: case 0x36:
keyboard_special_keys &= ~SPECIAL_SHIFT_R; keyboard_special_keys &= ~SPECIAL_SHIFT_R;
return; goto eoi;
case 58: case 0x1D:
keyboard_special_keys &= ~SPECIAL_CTRL_L; if (!special_flag)
return; keyboard_special_keys &= ~SPECIAL_CTRL_L;
case 64: else
keyboard_special_keys &= ~SPECIAL_CTRL_R; keyboard_special_keys &= ~SPECIAL_CTRL_R;
return; goto eoi;
case 60: case 0x38:
keyboard_special_keys &= ~SPECIAL_ALT_L; if (!special_flag)
return; keyboard_special_keys &= ~SPECIAL_ALT_L;
case 62: else
keyboard_special_keys &= ~SPECIAL_ALT_R; keyboard_special_keys &= ~SPECIAL_ALT_R;
return; goto eoi;
case : case 0x5B:
keyboard_special_keys &= ~SPECIAL_CTRL_L; if (special_flag) keyboard_special_keys &= ~SPECIAL_SUPER_L;
return; goto eoi;
case 0x5C:
if (special_flag) keyboard_special_keys &= ~SPECIAL_SUPER_L;
goto eoi;
}
} else { // key is make
switch (data & 0b01111111) {
case 0x2A:
keyboard_special_keys |= SPECIAL_SHIFT_L;
goto eoi;
case 0x36:
keyboard_special_keys |= SPECIAL_SHIFT_R;
goto eoi;
case 0x1D:
if (!special_flag)
keyboard_special_keys |= SPECIAL_CTRL_L;
else
keyboard_special_keys |= SPECIAL_CTRL_R;
goto eoi;
case 0x38:
if (!special_flag)
keyboard_special_keys |= SPECIAL_ALT_L;
else
keyboard_special_keys |= SPECIAL_ALT_R;
goto eoi;
case 0x5B:
if (special_flag) keyboard_special_keys |= SPECIAL_SUPER_L;
goto eoi;
case 0x5C:
if (special_flag) keyboard_special_keys |= SPECIAL_SUPER_L;
goto eoi;
default: default:
return; if ((keyboard_special_keys & SPECIAL_SHIFT_L) ||
(keyboard_special_keys & SPECIAL_SHIFT_R)) {
if (keycode_to_ascii_shift[data])
textout(keycode_to_ascii_shift[data]);
} else if (keycode_to_ascii[data])
textout(keycode_to_ascii[data]);
goto eoi;
} }
} }
*/
textout('0');
eoi: eoi:
end_of_interrupt(33); end_of_interrupt(33);
} }

0
interrupt.h Normal file → Executable file
View File

30
io.c Normal file → Executable file
View File

@ -8,3 +8,33 @@ UINT8 mapped_io_read(UINT16 addr) {
void mapped_io_write(UINT16 addr, UINT8 data) { void mapped_io_write(UINT16 addr, UINT8 data) {
asm volatile("outb %0,%1" ::"a"(data), "d"(addr)); asm volatile("outb %0,%1" ::"a"(data), "d"(addr));
} }
UINT16 mapped_io_read_word(UINT16 addr) {
UINT16 data;
asm volatile("inw %1,%0" : "=a"(data) : "d"(addr));
return data;
}
void mapped_io_write_word(UINT16 addr, UINT16 data) {
asm volatile("outw %0,%1" ::"a"(data), "d"(addr));
}
UINT32 mapped_io_read_dword(UINT16 addr) {
UINT32 data;
asm volatile("inl %1,%0" : "=a"(data) : "d"(addr));
return data;
}
void mapped_io_write_dword(UINT16 addr, UINT32 data) {
asm volatile("outl %0,%1" ::"a"(data), "d"(addr));
}
UINT64 mapped_io_read_qword(UINT16 addr) {
UINT64 data;
asm volatile("inq %1,%0" : "=a"(data) : "d"(addr));
return data;
}
void mapped_io_write_qword(UINT16 addr, UINT64 data) {
asm volatile("outq %0,%1" ::"a"(data), "d"(addr));
}

0
io.h Normal file → Executable file
View File

40
keycode.h Executable file
View File

@ -0,0 +1,40 @@
char keycode_to_ascii[] = {
0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '^',
0, 0, 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '@', '[',
'\n', 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', ':', 0,
0, ']', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*',
0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, '\\', 0, 0, 0, 0, 0, 0, 0, 0, 0, '\\',
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0};
char keycode_to_ascii_shift[] = {
0, 0, '!', '"', '#', '$', '%', '&', '\'', '(', ')', 0, '=', '~', 0,
0, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '`', '{', 0, 0,
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', '+', '*', 0, 0, '}', 'Z',
'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', '8', '9', '-',
'4', '5', '6', '+', '1', '2', '3', '0', '.', 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '_', 0, 0, 0, 0,
0, 0, 0, 0, 0, '|', 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0};

174
main.c Normal file → Executable file
View File

@ -9,20 +9,68 @@
#include <Pi/PiMultiPhase.h> #include <Pi/PiMultiPhase.h>
#include <Protocol/MpService.h> #include <Protocol/MpService.h>
*/ */
#include <Guid/Acpi.h>
#include <Uefi.h> #include <Uefi.h>
#include <Uefi/UefiSpec.h> #include <Uefi/UefiSpec.h>
#include "fb.h" #include "fb.h"
#include "gdt.h" #include "gdt.h"
#include "interrupt.h" #include "interrupt.h"
#include "ramfs.h"
#include "text.h" #include "text.h"
#pragma pack(1)
typedef struct {
EFI_ACPI_DESCRIPTION_HEADER header;
EFI_ACPI_DESCRIPTION_HEADER *entry[0];
} XSDT;
typedef struct {
EFI_ACPI_DESCRIPTION_HEADER header;
UINT32 EventTimerBlockID;
EFI_ACPI_2_0_GENERIC_ADDRESS_STRUCTURE BaseAddress;
UINT8 HPETNumber;
UINT16 MinimumTick;
UINT8 Flags;
} HPET;
#pragma pack()
EFI_ACPI_DESCRIPTION_HEADER *Get_SDT(EFI_SYSTEM_TABLE *SystemTable,
UINT64 Signature) {
EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *ACPI_Table;
EFI_GUID gEfiAcpiTableGuid = EFI_ACPI_TABLE_GUID;
for (UINTN i = 0; i < SystemTable->NumberOfTableEntries; i++) {
EFI_CONFIGURATION_TABLE *table = SystemTable->ConfigurationTable + i;
if (table->VendorGuid.Data1 == gEfiAcpiTableGuid.Data1 &&
table->VendorGuid.Data2 == gEfiAcpiTableGuid.Data2 &&
table->VendorGuid.Data3 == gEfiAcpiTableGuid.Data3 &&
table->VendorGuid.Data4[0] == gEfiAcpiTableGuid.Data4[0] &&
table->VendorGuid.Data4[1] == gEfiAcpiTableGuid.Data4[1] &&
table->VendorGuid.Data4[2] == gEfiAcpiTableGuid.Data4[2] &&
table->VendorGuid.Data4[3] == gEfiAcpiTableGuid.Data4[3] &&
table->VendorGuid.Data4[4] == gEfiAcpiTableGuid.Data4[4] &&
table->VendorGuid.Data4[5] == gEfiAcpiTableGuid.Data4[5] &&
table->VendorGuid.Data4[6] == gEfiAcpiTableGuid.Data4[6] &&
table->VendorGuid.Data4[7] == gEfiAcpiTableGuid.Data4[7]) {
ACPI_Table = (EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *)
table->VendorTable;
}
}
XSDT *xsdt = (XSDT *)ACPI_Table->XsdtAddress;
for (int i = 0;
i < (xsdt->header.Length - sizeof(EFI_ACPI_DESCRIPTION_HEADER)) /
sizeof(EFI_ACPI_DESCRIPTION_HEADER *);
i++) {
if (xsdt->entry[i]->Signature == Signature) return xsdt->entry[i];
}
return NULL;
}
EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle, EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable) { IN EFI_SYSTEM_TABLE *SystemTable) {
EFI_STATUS Status; EFI_STATUS Status;
EFI_GRAPHICS_OUTPUT_PROTOCOL *gop; EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fsp;
// EFI_MP_SERVICES_PROTOCOL *mps; // EFI_MP_SERVICES_PROTOCOL *mps;
SystemTable->BootServices->SetWatchdogTimer( SystemTable->BootServices->SetWatchdogTimer(
@ -37,14 +85,6 @@ EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle,
return Status; return Status;
} }
Status = SystemTable->BootServices->LocateProtocol(
&gEfiSimpleFileSystemProtocolGuid, NULL, (VOID **)&fsp);
if (EFI_ERROR(Status)) {
Print(L"Error:Failed to locate EFI Simple FileSystem Protocol - %r",
Status);
return Status;
}
/* /*
Status = SystemTable->BootServices->LocateProtocol( Status = SystemTable->BootServices->LocateProtocol(
&gEfiMpServiceProtocolGuid, NULL, &gEfiMpServiceProtocolGuid, NULL,
@ -54,22 +94,119 @@ EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle,
Status); return Status; Status); return Status;
} }
*/ */
// HPET *hpet_table = (HPET *)Get_SDT(
// SystemTable, EFI_ACPI_3_0_HIGH_PRECISION_EVENT_TIMER_TABLE_SIGNATURE);
FrameBuffer_init(gop); FrameBuffer_init(gop);
ramfsinit(SystemTable);
UINTN MemoryMapSize = 1, MapKey, DescriptorSize; UINTN MemoryMapSize = 1, MapKey, DescriptorSize;
UINT32 DescriptorVersion; UINT32 DescriptorVersion;
EFI_MEMORY_DESCRIPTOR *mem_desc;
do { do {
EFI_MEMORY_DESCRIPTOR *mem_desc = Status = SystemTable->BootServices->AllocatePool(
(EFI_MEMORY_DESCRIPTOR *)AllocatePool(MemoryMapSize); EfiLoaderData, MemoryMapSize, (void **)&mem_desc);
if (EFI_ERROR(Status)) {
Print(L"Error:Failed to allocate memory map memory - %r\n", Status);
continue;
}
Status = SystemTable->BootServices->GetMemoryMap( Status = SystemTable->BootServices->GetMemoryMap(
&MemoryMapSize, mem_desc, &MapKey, &DescriptorSize, &MemoryMapSize, mem_desc, &MapKey, &DescriptorSize,
&DescriptorVersion); &DescriptorVersion);
if (EFI_ERROR(Status)) continue; if (EFI_ERROR(Status)) {
Print(L"Error:Failed to get memory map - %r\n", Status);
SystemTable->BootServices->FreePool(mem_desc);
continue;
}
Status = Status =
SystemTable->BootServices->ExitBootServices(ImageHandle, MapKey); SystemTable->BootServices->ExitBootServices(ImageHandle, MapKey);
} while (EFI_ERROR(Status)); if (EFI_ERROR(Status)) {
Print(L"Error:Failed to exit boot service - %r\n", Status);
SystemTable->BootServices->FreePool(mem_desc);
continue;
}
break;
} while (TRUE);
for (int y = 0; y < buffer.VerticalRes; y++) {
for (int x = 0; x < buffer.HorizontalRes; x++) {
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixel =
(EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)buffer.BaseAddress +
(buffer.HorizontalRes * y) + x;
RGBtoPixel(pixel, 0x000000);
}
}
for (UINTN i = (UINTN)mem_desc; i < ((UINTN)mem_desc + MemoryMapSize);
i += DescriptorSize) {
((EFI_MEMORY_DESCRIPTOR *)i)->VirtualStart =
((EFI_MEMORY_DESCRIPTOR *)i)->PhysicalStart;
}
SystemTable->RuntimeServices->SetVirtualAddressMap(
MemoryMapSize, DescriptorSize, DescriptorVersion, mem_desc);
for (UINTN i = (UINTN)mem_desc; i < ((UINTN)mem_desc + MemoryMapSize);
i += DescriptorSize) {
print("Type:");
switch (((EFI_MEMORY_DESCRIPTOR *)i)->Type) {
case EfiReservedMemoryType:
print("EfiReservedMemoryType");
break;
case EfiLoaderCode:
print("EfiLoaderCode");
break;
case EfiLoaderData:
print("EfiLoaderData");
break;
case EfiBootServicesCode:
print("EfiBootServicesCode");
break;
case EfiBootServicesData:
print("EfiBootServicesData");
break;
case EfiRuntimeServicesCode:
print("EfiRuntimeServicesCode");
break;
case EfiRuntimeServicesData:
print("EfiRuntimeServicesData");
break;
case EfiConventionalMemory:
print("EfiConventionalMemory");
break;
case EfiUnusableMemory:
print("EfiUnusableMemory");
break;
case EfiACPIReclaimMemory:
print("EfiACPIReclaimMemory");
break;
case EfiACPIMemoryNVS:
print("EfiACPIMemoryNVS");
break;
case EfiMemoryMappedIO:
print("EfiMemoryMappedIO");
break;
case EfiMemoryMappedIOPortSpace:
print("EfiMemoryMappedIOPortSpace");
break;
case EfiPalCode:
print("EfiPalCode");
break;
case EfiPersistentMemory:
print("EfiPersistentMemory");
break;
case EfiMaxMemoryType:
print("EfiMaxMemoryType");
break;
}
print(" Physical:");
printhex(((EFI_MEMORY_DESCRIPTOR *)i)->PhysicalStart, 0);
print(" Virtual:");
printhex(((EFI_MEMORY_DESCRIPTOR *)i)->VirtualStart, 0);
print(" Pages:");
printhex(((EFI_MEMORY_DESCRIPTOR *)i)->NumberOfPages, 0);
print(" Attribute:");
printhex(((EFI_MEMORY_DESCRIPTOR *)i)->Attribute, 0);
print("\n");
}
GDT_init(); GDT_init();
interrupt_init(); interrupt_init();
/*
for (int y = 0; y < buffer.VerticalRes; y++) { for (int y = 0; y < buffer.VerticalRes; y++) {
for (int x = 0; x < buffer.HorizontalRes; x++) { for (int x = 0; x < buffer.HorizontalRes; x++) {
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixel = EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixel =
@ -77,12 +214,7 @@ EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle,
(buffer.HorizontalRes * y) + x; (buffer.HorizontalRes * y) + x;
RGBtoPixel(pixel, 0xFF0000); RGBtoPixel(pixel, 0xFF0000);
} }
} }*/
textout('A');
textout('p');
textout('p');
textout('l');
textout('e');
while (1) asm volatile("hlt"); while (1) asm volatile("hlt");
return Status; return Status;
} }

5
memory.c Executable file
View File

@ -0,0 +1,5 @@
#include <Library/BaseLib.h>
#include <Uefi.h>
#include <Uefi/UefiSpec.h>
void VMemInit() { UINTN *PageTable = (UINTN *)AsmReadCr3(); }

8
mkramfs/Makefile Executable file
View File

@ -0,0 +1,8 @@
CC := g++
CFLAGS := -std=c++17
mkramfs: main.cpp
$(CC) $(CFLAGS) -o mkramfs main.cpp
clean: $(RM) mkramfs

37
mkramfs/main.cpp Executable file
View File

@ -0,0 +1,37 @@
#include <string.h>
#include <filesystem>
#include <fstream>
#include <iostream>
typedef struct {
int Start;
int Size;
char name[256];
} FILE_DESC;
int main(int argc, char **argv) {
if (argc == 1) {
std::cout << "Usage:mkramfs <file1> <file2> ..." << std::endl;
return -1;
}
FILE_DESC descriptor[256] = {0};
std::ofstream ramfs("ramfs.img", std::ios::binary | std::ios::ate);
for (int i = 1; i < argc; i++) {
if (!std::filesystem::exists(std::filesystem::path(argv[i]))) {
std::cout << "file does not exist! - " << argv[i] << std::endl;
continue;
}
descriptor[i].Start = descriptor[i - 1].Start + descriptor[i - 1].Size;
descriptor[i].Size =
std::filesystem::file_size(std::filesystem::path(argv[i]));
strncpy(descriptor[i].name,
std::filesystem::path(argv[i]).filename().c_str(), 255);
}
ramfs.write(reinterpret_cast<char *>(descriptor), sizeof(descriptor));
for (int i = 1; i < argc; i++) {
std::ifstream file(argv[i], std::ios::binary);
ramfs << file.rdbuf();
file.close();
}
ramfs.close();
}

0
note.md Normal file → Executable file
View File

51
pci.c Executable file
View File

@ -0,0 +1,51 @@
#include <IndustryStandard/Pci.h>
#include <Uefi.h>
#include <Uefi/UefiSpec.h>
typedef struct {
BOOLEAN isUsing;
BOOLEAN isIDE; // T:IDE F:AHCI
UINT8 bus;
UINT8 device;
UINT8 function;
} DiskController;
DiskController controllers[5];
void init_pci() {
for (UINT8 bus = 0; bus < 256; bus++) {
for (UINT8 device = 0; device < 32; device++) {
for (UINT8 function = 0; function < 8; function++) {
UINT32 ConfigData =
PciCf8Read32(PCI_CF8_LIB_ADDRESS(bus, device, function, 0));
if (ConfigData != 0xffffffff) {
ConfigData = PciCf8Read32(
PCI_CF8_LIB_ADDRESS(bus, device, function, 8));
if ((ConfigData >> 16) == 0x0101) { // IDE Controller
int n = 0;
while (controllers[n].isUsing) {
n++;
}
controllers[n].isUsing = TRUE;
controllers[n].isIDE = TRUE;
controllers[n].bus = bus;
controllers[n].device = device;
controllers[n].function = function;
return;
} else if ((ConfigData >> 8) ==
0x010601) { // AHCI Controller
int n = 0;
while (controllers[n].isUsing) {
n++;
}
controllers[n].isUsing = TRUE;
controllers[n].isIDE = FALSE;
controllers[n].bus = bus;
controllers[n].device = device;
controllers[n].function = function;
}
}
}
}
}
}

56
ramfs.c Executable file
View File

@ -0,0 +1,56 @@
#include "ramfs.h"
#include <Guid/FileInfo.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/PrintLib.h>
#include <Library/UefiLib.h>
#include <Uefi.h>
#include "text.h"
RAMFS_DATA *data;
void ramfsinit(
EFI_SYSTEM_TABLE *SystemTable) { // must call before ExitBootServices
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fsp;
EFI_FILE_PROTOCOL *Root, *ramfs;
EFI_FILE_INFO info;
UINTN infosize = sizeof(info), datasize;
EFI_STATUS Status = SystemTable->BootServices->LocateProtocol(
&gEfiSimpleFileSystemProtocolGuid, NULL, (VOID **)&fsp);
if (EFI_ERROR(Status)) {
Print(L"Error:Failed to locate EFI Simple FileSystem Protocol - %r",
Status);
return;
}
fsp->OpenVolume(fsp, &Root);
Status = Root->Open(Root, &ramfs, L"ramfs.img", EFI_FILE_MODE_READ, 0);
if (EFI_ERROR(Status)) {
Print(L"Error:Failed to open ramfs file - %r\n", Status);
return;
}
ramfs->GetInfo(ramfs, &gEfiFileInfoGuid, &infosize, &info);
Status = SystemTable->BootServices->AllocatePool(
EfiLoaderData, info.FileSize, (void **)&data);
if (EFI_ERROR(Status)) {
Print(L"Error:Failed to allocate ramfs memory - %r\n", Status);
return;
}
datasize = info.FileSize;
ramfs->Read(ramfs, &datasize, data);
}
void getfilesize(char *filename, UINTN *size) {
int index;
for (index = 0; index < 256; index++)
if (!AsciiStrCmp(data->descriptor[index].name, filename)) break;
*size = data->descriptor[index].Size;
}
void readfile(char *filename, UINTN buffersize, void *buffer) {
int index;
for (index = 0; index < 256; index++)
if (!AsciiStrCmp(data->descriptor[index].name, filename)) break;
CopyMem(buffer, data->data + data->descriptor[index].Start, buffersize);
}

15
ramfs.h Executable file
View File

@ -0,0 +1,15 @@
#include <Protocol/SimpleFileSystem.h>
typedef struct {
int Start;
int Size;
char name[256];
} FILE_DESC;
typedef struct {
FILE_DESC descriptor[256];
char data[];
} RAMFS_DATA;
void ramfsinit(EFI_SYSTEM_TABLE *SystemTable);
void getfilesize(char *filename, UINTN *size);
void readfile(char *filename, UINTN buffersize, void *buffer);

0
sprinkleos.dec Normal file → Executable file
View File

6
sprinkleos.dsc Normal file → Executable file
View File

@ -30,6 +30,8 @@
MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf
UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf UefiRuntimeServicesTableLib|MdePkg/Library/UefiRuntimeServicesTableLib/UefiRuntimeServicesTableLib.inf
DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf
PciCf8Lib|MdePkg/Library/BasePciCf8Lib/BasePciCf8Lib.inf
IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
RegisterFilterLib|MdePkg/Library/RegisterFilterLibNull/RegisterFilterLibNull.inf
[Components] [Components]
sprinkleos/sprinkleos.inf sprinkleos/sprinkleos.inf

8
sprinkleos.inf Normal file → Executable file
View File

@ -14,10 +14,16 @@
io.c io.c
text.c text.c
int_handler.s int_handler.s
ramfs.c
elf.c
[Packages] [Packages]
MdePkg/MdePkg.dec MdePkg/MdePkg.dec
[LibraryClasses] [LibraryClasses]
UefiLib UefiLib
UefiApplicationEntryPoint UefiApplicationEntryPoint
PciCf8Lib
[Guids]
gEfiFileInfoGuid

3
testapp/main.c Executable file
View File

@ -0,0 +1,3 @@
int main(){
return 0;
}

55
text.c Normal file → Executable file
View File

@ -57,6 +57,11 @@ void textinit(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL* fsp) {
void textout(UINT8 code) { void textout(UINT8 code) {
static UINTN x = 0, y = 0; static UINTN x = 0, y = 0;
if (code == '\n') {
x = 0;
y++;
return;
}
for (int row = 0; row < 13; row++) { for (int row = 0; row < 13; row++) {
for (int column = 0; column < 8; column++) { for (int column = 0; column < 8; column++) {
EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixel = EFI_GRAPHICS_OUTPUT_BLT_PIXEL *pixel =
@ -64,7 +69,7 @@ void textout(UINT8 code) {
y * 13 * buffer.HorizontalRes + row * buffer.HorizontalRes + y * 13 * buffer.HorizontalRes + row * buffer.HorizontalRes +
8 * x + column; 8 * x + column;
if ((font[code][row] >> (7 - column)) & 0x1) if ((font[code][row] >> (7 - column)) & 0x1)
RGBtoPixel(pixel, 0x00FF00); RGBtoPixel(pixel, 0xFFFFFF);
} }
} }
if (x < (buffer.HorizontalRes / 8) - 1) if (x < (buffer.HorizontalRes / 8) - 1)
@ -73,4 +78,50 @@ void textout(UINT8 code) {
x = 0; x = 0;
y++; y++;
} }
} }
void print(char *string) {
do {
textout(*string);
string++;
} while (*string != '\0');
}
UINT64 pow(UINT64 base, UINT64 exponent) {
UINT64 ans = 1;
for (UINT64 i = 0; i < exponent; i++) {
ans *= base;
}
return ans;
}
void printhex(UINT64 num, UINT8 digit) {
if (digit == 0) {
UINT8 maxdigit = 0;
while (num >= pow(16, maxdigit)) maxdigit++;
print("0x");
if (maxdigit == 0) textout('0' + num);
while (maxdigit) {
UINT64 weight = pow(16, maxdigit - 1);
UINT64 ans = num / weight;
if (ans < 10)
textout('0' + ans);
else
textout('A' + ans - 10);
num -= ans * weight;
maxdigit--;
}
} else {
print("0x");
while (digit) {
UINT64 weight = pow(16, digit - 1);
UINT64 ans = num / weight;
if (ans < 10)
textout('0' + ans);
else
textout('A' + ans - 10);
num -= ans * weight;
digit--;
}
}
}

6
text.h Normal file → Executable file
View File

@ -1,6 +1,6 @@
#include <Protocol/SimpleFileSystem.h>
#include <Uefi.h> #include <Uefi.h>
#include <Uefi/UefiSpec.h> #include <Uefi/UefiSpec.h>
// extern char keycode[];
void textout(UINT8 code); void textout(UINT8 code);
void print(char *string);
void printhex(UINT64 num, UINT8 digit);