From a485d59fdaf7b768f95539991d72421fb0ec85de Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 17 Jul 2025 19:20:26 +0200 Subject: Restructure project folder Signed-off-by: Carlos Maiolino --- .gitignore | 2 +- Makefile | 6 ++-- boot.txt | 1 - bootloader.asm | 94 ------------------------------------------------- src/boot/bootloader.asm | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 100 deletions(-) delete mode 100644 boot.txt delete mode 100644 bootloader.asm create mode 100644 src/boot/bootloader.asm diff --git a/.gitignore b/.gitignore index ad0cdfc..a8a0dce 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -boot.img +*.bin diff --git a/Makefile b/Makefile index f44f839..4df65dd 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,7 @@ -BOOTLOADER=./bootloader.asm -TARGET=./boot.img +BOOTLOADER=./src/boot/bootloader.asm +TARGET=./bin/boot.bin all: nasm -f bin $(BOOTLOADER) -o $(TARGET) - dd if=./boot.txt >> $(TARGET) - dd if=/dev/zero bs=512 count=1 >> $(TARGET) clean: rm -f $(TARGET) diff --git a/boot.txt b/boot.txt deleted file mode 100644 index ebf67aa..0000000 --- a/boot.txt +++ /dev/null @@ -1 +0,0 @@ -Initializing CemOS... diff --git a/bootloader.asm b/bootloader.asm deleted file mode 100644 index f42e702..0000000 --- a/bootloader.asm +++ /dev/null @@ -1,94 +0,0 @@ -ORG 0x7c00 -BITS 16 - -CODE_SEG equ gdt_code - gdt_start ; 0x8 -DATA_SEG equ gdt_data - gdt_start ; 0x10 -_start: - jmp short start - nop - -; Setup dummy BIOS Parameter block so a dumb BIOS does not corrupt us -; if it starts messing up with the parameter block - times 33 db 0 - -start: - ; Set CS to 0 with label step2 as its beginning offset - jmp 0:step2 - -step2: - ; Setup segments - - cli ; Disable interrupts - mov ax, 0 - mov ds, ax - mov es, ax - mov ss, ax - mov sp, 0x7c00 ; Stack segment starts right before ds - ; and grows down to 0x0. This works because - ; Intel implements a full descending stack - sti ; Enable interrupts - -.load_protected: - cli - lgdt[gdt_table] - mov eax, cr0 - or eax, 0x1 - mov cr0, eax - jmp CODE_SEG:start_32 - -; GDT table description -gdt_start: -gdt_null: ; First segment Descriptor is always 0 - dd 0x0 - dd 0x0 - -;Second segment descriptor... Code segment - 0x8 -gdt_code: - dw 0xffff ; Segment limit 0-15 - dw 0 ; Base address 0-15 - db 0 ; Base 16-23 - db 0x9a ; Access byte field 0b10011010 - - ; We don't have a 4 bits type, so use a byte type - ; to set both Segment limit high bits and flags - - db 11001111b ; Segment limit 16-19 - ; Flags 0-3 - db 0 ; Base address high bits 24-31 - -;Second segment descriptor... Data segment - 0x10 -gdt_data: - dw 0xffff ; Segment limit 0-15 - dw 0 ; Base address 0-15 - db 0 ; Base 16-23 - db 0x92 ; Access byte field 0b10010010 - db 11001111b ; Segment limit 16-19 - ; Flags 0-3 - db 0 ; Base address high bits 24-31 -gdt_end: - -gdt_table: - dw gdt_end - gdt_start - 1 - dd gdt_start - - -[BITS 32] - -; No access to BIOS from now on.... -start_32: - ; Set all segments to the same as the DATA_SEG - mov ax, DATA_SEG - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov ss, ax - - ; Set the stack pointer and base pointer further in mem - mov ebp, 0x00200000 - mov esp, ebp - jmp $ - -; Fill in to the end and add bootloader signature - times 510 - ($ - $$) db 0 - dw 0xAA55 diff --git a/src/boot/bootloader.asm b/src/boot/bootloader.asm new file mode 100644 index 0000000..f42e702 --- /dev/null +++ b/src/boot/bootloader.asm @@ -0,0 +1,94 @@ +ORG 0x7c00 +BITS 16 + +CODE_SEG equ gdt_code - gdt_start ; 0x8 +DATA_SEG equ gdt_data - gdt_start ; 0x10 +_start: + jmp short start + nop + +; Setup dummy BIOS Parameter block so a dumb BIOS does not corrupt us +; if it starts messing up with the parameter block + times 33 db 0 + +start: + ; Set CS to 0 with label step2 as its beginning offset + jmp 0:step2 + +step2: + ; Setup segments + + cli ; Disable interrupts + mov ax, 0 + mov ds, ax + mov es, ax + mov ss, ax + mov sp, 0x7c00 ; Stack segment starts right before ds + ; and grows down to 0x0. This works because + ; Intel implements a full descending stack + sti ; Enable interrupts + +.load_protected: + cli + lgdt[gdt_table] + mov eax, cr0 + or eax, 0x1 + mov cr0, eax + jmp CODE_SEG:start_32 + +; GDT table description +gdt_start: +gdt_null: ; First segment Descriptor is always 0 + dd 0x0 + dd 0x0 + +;Second segment descriptor... Code segment - 0x8 +gdt_code: + dw 0xffff ; Segment limit 0-15 + dw 0 ; Base address 0-15 + db 0 ; Base 16-23 + db 0x9a ; Access byte field 0b10011010 + + ; We don't have a 4 bits type, so use a byte type + ; to set both Segment limit high bits and flags + + db 11001111b ; Segment limit 16-19 + ; Flags 0-3 + db 0 ; Base address high bits 24-31 + +;Second segment descriptor... Data segment - 0x10 +gdt_data: + dw 0xffff ; Segment limit 0-15 + dw 0 ; Base address 0-15 + db 0 ; Base 16-23 + db 0x92 ; Access byte field 0b10010010 + db 11001111b ; Segment limit 16-19 + ; Flags 0-3 + db 0 ; Base address high bits 24-31 +gdt_end: + +gdt_table: + dw gdt_end - gdt_start - 1 + dd gdt_start + + +[BITS 32] + +; No access to BIOS from now on.... +start_32: + ; Set all segments to the same as the DATA_SEG + mov ax, DATA_SEG + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + + ; Set the stack pointer and base pointer further in mem + mov ebp, 0x00200000 + mov esp, ebp + jmp $ + +; Fill in to the end and add bootloader signature + times 510 - ($ - $$) db 0 + dw 0xAA55 -- cgit v1.2.3