misc: Compile stage 2 with -Os and stage 3 with -O3
diff --git a/stage23/Makefile b/stage23/Makefile
index 5f928a0a..f0dd8422 100644
--- a/stage23/Makefile
+++ b/stage23/Makefile
@@ -20,7 +20,8 @@ E9_OUTPUT = false
BUILD_ID := $(shell dd if=/dev/urandom count=8 bs=1 | od -An -t x8 | sed 's/^ /0x/')
LIMINE_VERSION := $(shell git describe --exact-match --tags `git log -n1 --pretty='%h'` || git log -n1 --pretty='%h')
WERROR = -Werror
-CFLAGS = -Os -g -pipe -Wall -Wextra $(WERROR)
+CFLAGS = -O3 -g -pipe -Wall -Wextra $(WERROR)
+S2CFLAGS = -Os -g -pipe -Wall -Wextra $(WERROR)
INTERNAL_CFLAGS := \
-std=gnu11 \
@@ -54,7 +55,7 @@ ifeq ($(TARGET), uefi)
-mno-red-zone
endif
-LDFLAGS = -Os -g
+LDFLAGS = -O3 -g
INTERNAL_LDFLAGS := \
-fno-lto \
@@ -133,6 +134,11 @@ limine_efi.elf: $(OBJ) font.o sys/smp_trampoline.o
%.o: %.c
$(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
+ifeq ($(TARGET), bios)
+%.s2.o: %.s2.c
+ $(CC) $(S2CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
+endif
+
%.o: %.asm
nasm $< -F dwarf -g -f elf32 -o $@
diff --git a/stage23/fs/fat32.s2.c b/stage23/fs/fat32.s2.c
index 06ffe1d2..d17c8bf4 100644
--- a/stage23/fs/fat32.s2.c
+++ b/stage23/fs/fat32.s2.c
@@ -191,6 +191,9 @@ static int fat32_open_in(struct fat32_context* context, struct fat32_directory_e
size_t dir_chain_len;
uint32_t *directory_cluster_chain = cache_cluster_chain(context, current_cluster_number, &dir_chain_len);
+ if (directory_cluster_chain == NULL)
+ return -1;
+
struct fat32_directory_entry *directory_entries = ext_mem_alloc(dir_chain_len * block_size);
if (!read_cluster_chain(context, directory_cluster_chain, directory_entries, 0, dir_chain_len * block_size))
return -1;
diff --git a/stage23/protos/chainload.c b/stage23/protos/chainload.c
index f407e35d..5663f9e1 100644
--- a/stage23/protos/chainload.c
+++ b/stage23/protos/chainload.c
@@ -26,7 +26,10 @@ static void spinup(uint8_t drive) {
"mov eax, cr0\n\t"
"and al, 0xfe\n\t"
"mov cr0, eax\n\t"
- "jmp 0x0000:1f\n\t"
+ "mov eax, OFFSET 1f\n\t"
+ "push eax\n\t"
+ "push 0\n\t"
+ "retf\n\t"
"1:\n\t"
"mov ax, 0\n\t"
"mov ds, ax\n\t"
diff --git a/stage23/protos/linux.c b/stage23/protos/linux.c
index 5803942e..a54a7672 100644
--- a/stage23/protos/linux.c
+++ b/stage23/protos/linux.c
@@ -34,7 +34,10 @@ static void spinup(uint16_t real_mode_code_seg, uint16_t kernel_entry_seg,
"mov eax, cr0\n\t"
"and al, 0xfe\n\t"
"mov cr0, eax\n\t"
- "jmp 0x0000:1f\n\t"
+ "mov eax, OFFSET 1f\n\t"
+ "push eax\n\t"
+ "push 0\n\t"
+ "retf\n\t"
"1:\n\t"
"mov ds, bx\n\t"
"mov es, bx\n\t"
