From 0f00932955521c3c2fb8140176ab72f22172c298 Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Fri, 26 Feb 2021 14:51:13 +0100
Subject: Started UBSan and stack-smashing check support

---
 boot/Makefile |  2 +-
 boot/load.c   | 17 +++++++----------
 2 files changed, 8 insertions(+), 11 deletions(-)

(limited to 'boot')

diff --git a/boot/Makefile b/boot/Makefile
index 6d38ce5..714ec22 100644
--- a/boot/Makefile
+++ b/boot/Makefile
@@ -5,7 +5,7 @@ LD = ccache ../cross/opt/bin/i686-elf-ld
 OC = ccache ../cross/opt/bin/i686-elf-objcopy
 AS = ccache nasm
 
-CFLAGS = $(CFLAGS_DEFAULT) -ffreestanding
+CFLAGS = $(CFLAGS_DEFAULT) -ffreestanding -fno-stack-protector -fno-sanitize=undefined
 
 ASFLAGS = -f elf32
 
diff --git a/boot/load.c b/boot/load.c
index 69024c0..b243629 100644
--- a/boot/load.c
+++ b/boot/load.c
@@ -136,7 +136,8 @@ void serial_print(const char *data);
 int main(void *data)
 {
 	serial_install();
-	heap = 0xf000;
+	serial_print("Loaded bootloader!\n");
+	heap = 0xf00000;
 	void (*entry)(void *);
 	*(void **)(&entry) = read_inode(get_inode(find_inode("kernel.bin", 2)));
 	if (entry) {
@@ -329,7 +330,7 @@ void *read_inode(struct inode *in)
 	if (!in)
 		return 0;
 
-	int num_blocks = in->blocks / (BLOCK_SIZE / SECTOR_SIZE);
+	u32 num_blocks = in->blocks / (BLOCK_SIZE / SECTOR_SIZE);
 
 	//assert(num_blocks != 0);
 	if (!num_blocks)
@@ -341,24 +342,20 @@ void *read_inode(struct inode *in)
 	int indirect;
 
 	int blocknum;
-	char *data;
-	for (int i = 0; i < num_blocks; i++) {
+	for (u32 i = 0; i < num_blocks; i++) {
 		if (i < 12) {
 			blocknum = in->block[i];
-			data = buffer_read(blocknum);
-			memcpy((u32 *)((u32)buf + i * BLOCK_SIZE), data, BLOCK_SIZE);
 		} else if (i < BLOCK_COUNT + 12) {
 			indirect = in->block[12];
 			blocknum = read_indirect(indirect, i - 12);
-			data = buffer_read(blocknum);
-			memcpy((u32 *)((u32)buf + i * BLOCK_SIZE), data, BLOCK_SIZE);
 		} else {
 			indirect = in->block[13];
 			blocknum = read_indirect(indirect, (i - (BLOCK_COUNT + 12)) / BLOCK_COUNT);
 			blocknum = read_indirect(blocknum, (i - (BLOCK_COUNT + 12)) % BLOCK_COUNT);
-			data = buffer_read(blocknum);
-			memcpy((u32 *)((u32)buf + i * BLOCK_SIZE), data, BLOCK_SIZE);
 		}
+
+		char *data = buffer_read(blocknum);
+		memcpy((u32 *)((u32)buf + i * BLOCK_SIZE), data, BLOCK_SIZE);
 	}
 
 	return buf;
-- 
cgit v1.2.3