feat: generate bootable iso

This commit is contained in:
d0p1 🏳️‍⚧️ 2023-01-17 11:35:11 +01:00
parent a202fadc9e
commit 33d96c6d68
17 changed files with 242 additions and 24 deletions

View file

@ -11,7 +11,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install build-essential llvm lld nasm
sudo apt-get install build-essential llvm lld nasm mtools
- name: Build
run: |
make

6
.gitignore vendored
View file

@ -1,7 +1,13 @@
*.o
*.img
*.bin
*.iso
*~
*.elf
*.a
*.dump
vmstupid
/sysroot

View file

@ -5,10 +5,13 @@
AS = nasm
LD = ld.lld
OBJDUMP = llvm-objdump
QEMU = qemu-system-i386
RM = rm -f
MKCWD = mkdir -p $(@D)
INSTALL = install
BIN_DIR = bin
DOC_DIR = doc
DOC_DIR = docs
KERNEL_DIR = kernel
LIB_DIR = lib
TOOLS_DIR = tools
@ -16,22 +19,28 @@ TOOLS_DIR = tools
include $(TOOLS_DIR)/build.mk
ASFLAGS = -DSTUPID_VERSION=\"$(shell $(GIT-VERSION))\" -Ilib
QEMUFLAGS = -serial stdio
GARBADGE = stupid.img
GARBADGE = stupid.iso
include $(KERNEL_DIR)/build.mk
include $(LIB_DIR)/build.mk
include $(BIN_DIR)/build.mk
all: stupid.img
all: stupid.iso
stupid.img: $(KERNEL_BIN) $(KERNEL_DUMP)
sysroot: $(KERNEL_BIN) $(KERNEL_DUMP) $(LIBS_BIN)
$(INSTALL) -d $@
$(INSTALL) -d $@/bin
$(INSTALL) -d $@/lib
$(INSTALL) $(KERNEL_BIN) $@
$(INSTALL) $(LIBS_BIN) $@/lib
#stupid.img: floppy.bin kern.bin
# mkdosfs -F 12 -C $@ 1440
# dd status=noxfer conv=notrunc if=floppy.bin of=$@
# mcopy -i $@ kern.bin ::kern.sys
stupid.iso: sysroot
$(CREATE-ISO) $@ $<
%.dump: %.elf
$(OBJDUMP) -D $^ > $@
run: stupid.iso
$(QEMU) $(QEMUFLAGS) -cdrom $<
clean:
$(RM) $(GARBADGE)

View file

@ -1,4 +1,3 @@
# StupidOS
32-bit Operating System in assembly.

0
bin/build.mk Normal file
View file

View file

@ -1,9 +1,9 @@
include kernel/drivers/build.mk
KERNEL_SRCS = head.s gdt.s idt.s paging.s lib/log.s
KERNEL_SRCS = head.s gdt.s pic.s isr.s idt.s paging.s lib/log.s
KERNEL_OBJS = $(addprefix kernel/, $(KERNEL_SRCS:.s=.o) $(DRIVERS_OBJS))
KERNEL_BIN = stupid.elf
KERNEL_DUMP = $(KERNEL_BIN:.elf=.dump)
KERNEL_BIN = vmstupid
KERNEL_DUMP = $(KERNEL_BIN).dump
KERNEL_ASFLAGS = $(ASFLAGS) -D__KERNEL__
@ -12,8 +12,11 @@ GARBADGE += $(KERNEL_OBJS) $(KERNEL_BIN) $(KERNEL_DUMP)
$(KERNEL_BIN): $(KERNEL_OBJS)
$(LD) -T $(KERNEL_DIR)/linker.ld -nostdlib -o $@ $^
$(KERNEL_DUMP): $(KERNEL_BIN)
$(OBJDUMP) -D $^ > $@
kernel/lib/%.o: lib/base/%.s
mkdir -p $(@D)
@$(MKCWD)
$(AS) -felf -o $@ $< $(KERNEL_ASFLAGS)
kernel/%.o: kernel/%.s

View file

@ -29,14 +29,21 @@ entry:
extern serial_init
call serial_init
LOG msg_hello_world
extern setup_gdt
call setup_gdt
extern setup_pic
call setup_pic
extern setup_idt
call setup_idt
extern setup_paging
call setup_paging
int3
;extern setup_paging
;call setup_paging
LOG file
@ -44,4 +51,8 @@ hang:
hlt
jmp hang
section .rodata
msg_hello_world db "StupidOS ", STUPID_VERSION, 0
file db __FILE__, 0

View file

@ -2,15 +2,33 @@
section .text
global setup_idt
setup_idt:
%assign i 0
extern isr %+ i
mov eax, isr %+ i
mov [idt_entries + i * 4], ax
shr eax, 16
mov word [idt_entries + i * 4 + 2], 0x8
mov byte [idt_entries + i * 4 + 5], 0x8E
mov [idt_entries + i * 4 + 6], ax
%assign i i+1
%rep 32
%endrep
lidt [idt_ptr]
sti
ret
section .data
idt_ptr:
dw idt_entries.end - idt_entries - 1
dw 256 * 8
dd idt_entries
idt_entries:
times 256 dd 0x00000000, 0x00000000
;; dw isr_low
;; dw kernel cs
;; db zero
;; db attr
;; dw isr_high
.end:

87
kernel/isr.s Normal file
View file

@ -0,0 +1,87 @@
[BITS 32]
%include "base.inc"
%macro ISR_NO_ERR 1
global isr%1
isr%1:
cli
push byte 0
push byte %1
jmp isr_handler
%endmacro
%macro ISR_ERR 1
global isr%1
isr%1:
cli
push byte 1
push byte %1
jmp isr_handler
%endmacro
section .text
ISR_NO_ERR 0
ISR_NO_ERR 1
ISR_NO_ERR 2
ISR_NO_ERR 3
ISR_NO_ERR 4
ISR_NO_ERR 5
ISR_NO_ERR 6
ISR_NO_ERR 7
ISR_ERR 8
ISR_NO_ERR 9
ISR_ERR 10
ISR_ERR 11
ISR_ERR 12
ISR_ERR 13
ISR_ERR 14
ISR_NO_ERR 15
ISR_NO_ERR 16
ISR_NO_ERR 17
ISR_NO_ERR 18
ISR_NO_ERR 19
ISR_NO_ERR 20
ISR_NO_ERR 21
ISR_NO_ERR 22
ISR_NO_ERR 23
ISR_NO_ERR 24
ISR_NO_ERR 25
ISR_NO_ERR 26
ISR_NO_ERR 27
ISR_NO_ERR 28
ISR_NO_ERR 29
ISR_NO_ERR 30
ISR_NO_ERR 31
global isr_handler
isr_handler:
pusha
mov ax, ds
push eax
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
LOG msg_interrupt
pop eax
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
popa
add esp, 8
sti
iret
section .rodata
msg_interrupt db "interrupt", 0
file db __FILE__, 0

24
kernel/pic.s Normal file
View file

@ -0,0 +1,24 @@
[BITS 32]
section .text
global setup_pic
setup_pic:
mov al, 0x11
out 0x20, al
out 0xA0, al
mov al, 0x20
out 0x21, al
mov al, 0x28
out 0xA1, al
mov al, 4
out 0x21, al
mov al, 2
out 0xA1, al
; mask all
mov al, 0xFF
out 0x21, al
out 0xA1, al
ret

View file

@ -1,10 +1,16 @@
extern log_impl
%macro LOG 1
%macro LOG 1-*
%rep %0
%rotate -1
push %1
%endrep
push file
call log_impl
add esp, 8
add esp, 4
%rep %0
add esp, 4
%endrep
%endmacro

2
lib/base/build.mk Normal file
View file

@ -0,0 +1,2 @@
LIBS += base
base_SRCS = log.s

View file

@ -56,10 +56,13 @@ log_impl:
add esp, 4
%else
%endif
.loop:
mov eax, [ebp + 12]
push eax
call putstr
add esp, 4
.end:
%ifdef __KERNEL__
mov al, 0xA

24
lib/build.mk Normal file
View file

@ -0,0 +1,24 @@
LIBS_BIN =
define LIBS_TEMPLATE
$(1)_BIN = lib$(1).a
$(1)_OBJS = $$(addprefix lib/$(1)/, $$($(1)_SRCS:.s=.o))
$$($(1)_BIN): $$($(1)_OBJS)
ar rcs $$@ $$^
GARBADGE += $$($(1)_OBJS) $$($(1)_BIN)
LIBS_BIN += $$($(1)_BIN)
endef
LIBS =
include lib/*/build.mk
$(foreach lib, $(LIBS), $(eval $(call LIBS_TEMPLATE,$(lib))))
lib/%.o: lib/%.s
$(AS) $(ASFLAGS) -o $@ $<

View file

@ -4,6 +4,6 @@ $(1) = $$(addprefix $$(TOOLS_DIR)/, $$(shell echo -n $(1) | tr A-Z a-z))
endef
TOOLS = GIT-VERSION
TOOLS = GIT-VERSION CREATE-ISO
$(foreach tool, $(TOOLS), $(eval $(call TOOLS_TEMPLATE, $(tool))))
$(foreach tool, $(TOOLS), $(eval $(call TOOLS_TEMPLATE,$(tool))))

26
tools/create-iso Executable file
View file

@ -0,0 +1,26 @@
#!/bin/env sh
grub_config=$(cat <<EOF
set timeout=15
set default=0
menuentry "StupidOS" {
echo "verify system integrity"
hashsum --hash sha256 --check /boot/hashfile --prefix /
multiboot /vmstupid
boot
}
EOF
)
gen_iso_file() {
mkdir -p "$2/boot/grub"
echo "$grub_config" > "$2/boot/grub/grub.cfg"
sha256sum "$2"/vmstupid > "$2/boot/hashfile"
grub-mkrescue -o $1 $2
}
gen_iso_file "$1" "$2"