Merge pull request #42 from ethan4984/master

protocol code clean up
This commit is contained in:
mint 2020-10-01 01:32:07 +02:00 committed by GitHub
commit 53339c806b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 104 deletions

View File

@ -1,6 +1,9 @@
#include <stdint.h>
#include <stddef.h>
#include <lib/image.h>
#include <lib/config.h>
#include <lib/blib.h>
#include <mm/pmm.h>
#include <lib/bmp.h>
int open_image(struct image *image, struct file_handle *file) {
@ -11,3 +14,35 @@ int open_image(struct image *image, struct file_handle *file) {
return -1;
}
struct kernel_loc get_kernel_loc(int boot_drive) {
int kernel_drive; {
char buf[32];
if (!config_get_value(buf, 0, 32, "KERNEL_DRIVE")) {
kernel_drive = boot_drive;
} else {
kernel_drive = (int)strtoui(buf);
}
}
int kernel_part; {
char buf[32];
if (!config_get_value(buf, 0, 32, "KERNEL_PARTITION")) {
panic("KERNEL_PARTITION not specified");
} else {
kernel_part = (int)strtoui(buf);
}
}
char *kernel_path = conv_mem_alloc(128);
if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
panic("KERNEL_PATH not specified");
}
struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
panic("Could not open kernel file");
}
return (struct kernel_loc) { kernel_drive, kernel_part, kernel_path, fd };
}

View File

@ -12,6 +12,15 @@ struct image {
void *local;
};
struct kernel_loc {
int kernel_drive;
int kernel_part;
char *kernel_path;
struct file_handle *fd;
};
int open_image(struct image *image, struct file_handle *file);
struct kernel_loc get_kernel_loc(int boot_drive);
#endif

View File

@ -52,36 +52,10 @@ static void spinup(uint16_t real_mode_code_seg, uint16_t kernel_entry_seg) {
}
void linux_load(char *cmdline, int boot_drive) {
int kernel_drive; {
char buf[32];
if (!config_get_value(buf, 0, 32, "KERNEL_DRIVE")) {
kernel_drive = boot_drive;
} else {
kernel_drive = (int)strtoui(buf);
}
}
int kernel_part; {
char buf[32];
if (!config_get_value(buf, 0, 32, "KERNEL_PARTITION")) {
panic("KERNEL_PARTITION not specified");
} else {
kernel_part = (int)strtoui(buf);
}
}
char *kernel_path = conv_mem_alloc(128);
if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
panic("KERNEL_PATH not specified");
}
struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
panic("Could not open kernel file");
}
struct kernel_loc kernel = get_kernel_loc(boot_drive);
uint32_t signature;
fread(fd, &signature, 0x202, sizeof(uint32_t));
fread(kernel.fd, &signature, 0x202, sizeof(uint32_t));
// validate signature
if (signature != 0x53726448) {
@ -89,7 +63,7 @@ void linux_load(char *cmdline, int boot_drive) {
}
size_t setup_code_size = 0;
fread(fd, &setup_code_size, 0x1f1, 1);
fread(kernel.fd, &setup_code_size, 0x1f1, 1);
if (setup_code_size == 0)
setup_code_size = 4;
@ -104,7 +78,7 @@ void linux_load(char *cmdline, int boot_drive) {
void *real_mode_code = conv_mem_alloc_aligned(real_mode_code_size, 0x1000);
fread(fd, real_mode_code, 0, real_mode_code_size);
fread(kernel.fd, real_mode_code, 0, real_mode_code_size);
size_t heap_end_ptr = ((real_mode_code_size & 0x0f) + 0x10) - 0x200;
*((uint16_t *)(real_mode_code + 0x224)) = (uint16_t)heap_end_ptr;
@ -142,8 +116,8 @@ void linux_load(char *cmdline, int boot_drive) {
// load kernel
print("Loading kernel...\n");
memmap_alloc_range(KERNEL_LOAD_ADDR, fd->size - real_mode_code_size, 0);
fread(fd, (void *)KERNEL_LOAD_ADDR, real_mode_code_size, fd->size - real_mode_code_size);
memmap_alloc_range(KERNEL_LOAD_ADDR, kernel.fd->size - real_mode_code_size, 0);
fread(kernel.fd, (void *)KERNEL_LOAD_ADDR, real_mode_code_size, kernel.fd->size - real_mode_code_size);
char initrd_path[64];
if (!config_get_value(initrd_path, 0, 64, "INITRD_PATH"))
@ -152,14 +126,14 @@ void linux_load(char *cmdline, int boot_drive) {
int initrd_part; {
char buf[32];
if (!config_get_value(buf, 0, 32, "INITRD_PARTITION")) {
initrd_part = fd->partition;
initrd_part = kernel.fd->partition;
} else {
initrd_part = (int)strtoui(buf);
}
}
struct file_handle initrd;
if (fopen(&initrd, fd->disk, initrd_part, initrd_path)) {
if (fopen(&initrd, kernel.fd->disk, initrd_part, initrd_path)) {
panic("Failed to open initrd");
}

View File

@ -26,37 +26,11 @@ struct stivale_struct stivale_struct = {0};
void stivale_load(char *cmdline, int boot_drive) {
stivale_struct.flags |= (1 << 0); // set bit 0 since we are BIOS and not UEFI
int kernel_drive; {
char buf[32];
if (!config_get_value(buf, 0, 32, "KERNEL_DRIVE")) {
kernel_drive = boot_drive;
} else {
kernel_drive = (int)strtoui(buf);
}
}
int kernel_part; {
char buf[32];
if (!config_get_value(buf, 0, 32, "KERNEL_PARTITION")) {
panic("KERNEL_PARTITION not specified");
} else {
kernel_part = (int)strtoui(buf);
}
}
char *kernel_path = conv_mem_alloc(128);
if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
panic("KERNEL_PATH not specified");
}
struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
panic("Could not open kernel file");
}
struct kernel_loc kernel = get_kernel_loc(boot_drive);
struct stivale_header stivale_hdr;
int bits = elf_bits(fd);
int bits = elf_bits(kernel.fd);
int ret;
@ -78,20 +52,20 @@ void stivale_load(char *cmdline, int boot_drive) {
level5pg = true;
}
ret = elf64_load_section(fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header), slide);
ret = elf64_load_section(kernel.fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header), slide);
if (!ret && ((stivale_hdr.flags >> 2) & 1)) {
// KASLR is enabled, set the slide
slide = rand64() & KASLR_SLIDE_BITMASK;
// Re-read the .stivalehdr with slid relocations
ret = elf64_load_section(fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header), slide);
ret = elf64_load_section(kernel.fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header), slide);
}
break;
}
case 32:
ret = elf32_load_section(fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header));
ret = elf32_load_section(kernel.fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header));
break;
default:
panic("stivale: Not 32 nor 64 bit x86 ELF file.");
@ -117,10 +91,10 @@ void stivale_load(char *cmdline, int boot_drive) {
switch (bits) {
case 64:
elf64_load(fd, &entry_point, &top_used_addr, slide, 10);
elf64_load(kernel.fd, &entry_point, &top_used_addr, slide, 10);
break;
case 32:
elf32_load(fd, (uint32_t *)&entry_point, (uint32_t *)&top_used_addr, 10);
elf32_load(kernel.fd, (uint32_t *)&entry_point, (uint32_t *)&top_used_addr, 10);
break;
}
@ -149,14 +123,14 @@ void stivale_load(char *cmdline, int boot_drive) {
int part; {
char buf[32];
if (!config_get_value(buf, i, 32, "MODULE_PARTITION")) {
part = kernel_part;
part = kernel.kernel_part;
} else {
part = (int)strtoui(buf);
}
}
struct file_handle f;
if (fopen(&f, fd->disk, part, module_file)) {
if (fopen(&f, kernel.fd->disk, part, module_file)) {
panic("Requested module with path \"%s\" not found!\n", module_file);
}

View File

@ -7,6 +7,7 @@
#include <lib/elf.h>
#include <lib/blib.h>
#include <lib/acpi.h>
#include <lib/image.h>
#include <lib/config.h>
#include <lib/time.h>
#include <lib/print.h>
@ -49,37 +50,11 @@ static void append_tag(struct stivale2_struct *s, struct stivale2_tag *tag) {
}
void stivale2_load(char *cmdline, int boot_drive) {
int kernel_drive; {
char buf[32];
if (!config_get_value(buf, 0, 32, "KERNEL_DRIVE")) {
kernel_drive = boot_drive;
} else {
kernel_drive = (int)strtoui(buf);
}
}
int kernel_part; {
char buf[32];
if (!config_get_value(buf, 0, 32, "KERNEL_PARTITION")) {
panic("KERNEL_PARTITION not specified");
} else {
kernel_part = (int)strtoui(buf);
}
}
char *kernel_path = conv_mem_alloc(128);
if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
panic("KERNEL_PATH not specified");
}
struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
panic("Could not open kernel file");
}
struct kernel_loc kernel = get_kernel_loc(boot_drive);
struct stivale2_header stivale2_hdr;
int bits = elf_bits(fd);
int bits = elf_bits(kernel.fd);
int ret;
@ -101,20 +76,20 @@ void stivale2_load(char *cmdline, int boot_drive) {
level5pg = true;
}
ret = elf64_load_section(fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header), slide);
ret = elf64_load_section(kernel.fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header), slide);
if (!ret && (stivale2_hdr.flags & 1)) {
// KASLR is enabled, set the slide
slide = rand64() & KASLR_SLIDE_BITMASK;
// Re-read the .stivale2hdr with slid relocations
ret = elf64_load_section(fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header), slide);
ret = elf64_load_section(kernel.fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header), slide);
}
break;
}
case 32:
ret = elf32_load_section(fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header));
ret = elf32_load_section(kernel.fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header));
break;
default:
panic("stivale2: Not 32 nor 64 bit x86 ELF file.");
@ -140,10 +115,10 @@ void stivale2_load(char *cmdline, int boot_drive) {
switch (bits) {
case 64:
elf64_load(fd, &entry_point, &top_used_addr, slide, 0x1001);
elf64_load(kernel.fd, &entry_point, &top_used_addr, slide, 0x1001);
break;
case 32:
elf32_load(fd, (uint32_t *)&entry_point, (uint32_t *)&top_used_addr, 0x1001);
elf32_load(kernel.fd, (uint32_t *)&entry_point, (uint32_t *)&top_used_addr, 0x1001);
break;
}
@ -194,14 +169,14 @@ void stivale2_load(char *cmdline, int boot_drive) {
int part; {
char buf[32];
if (!config_get_value(buf, i, 32, "MODULE_PARTITION")) {
part = kernel_part;
part = kernel.kernel_part;
} else {
part = (int)strtoui(buf);
}
}
struct file_handle f;
if (fopen(&f, fd->disk, part, module_file)) {
if (fopen(&f, kernel.fd->disk, part, module_file)) {
panic("Requested module with path \"%s\" not found!\n", module_file);
}