Set up environment, wrote loader and Makefile

This commit is contained in:
lucic71 2020-06-18 20:50:27 +03:00
parent 86fa728875
commit 30cdb9c21a
8 changed files with 149 additions and 0 deletions

39
Makefile Normal file
View File

@ -0,0 +1,39 @@
OBJECTS = loader.o kmain.o
CC = gcc
CCFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \
-nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c
LDFLAGS = -T link.ld -melf_i386
AS = nasm
ASFLAGS = -f elf32
all: kernel.elf
kernel.elf: $(OBJECTS)
ld $(LDFLAGS) $(OBJECTS) -o kernel.elf
os.iso: kernel.elf
cp kernel.elf iso/boot/kernel.elf
genisoimage -R \
-b boot/grub/stage2_eltorito \
-no-emul-boot \
-boot-load-size 4 \
-A os \
-input-charset utf8 \
-quiet \
-boot-info-table \
-o os.iso \
iso
run: os.iso
bochs -f bochsrc.txt -q
%.o: %.c
$(CC) $(CCFLAGS) $< -o $@
%.o: %.s
$(AS) $(ASFLAGS) $< -o $@
clean:
rm -rf *.o kernel.elf os.iso bochslog.txt iso/boot/kernel.elf

View File

@ -1 +1,7 @@
# lucicOS
Tools used:
* compiler - gcc
* assembler - nasm
* emulator - bochs
* GRUB legacy bootloader - stage2\_eltorito

9
bochsrc.txt Normal file
View File

@ -0,0 +1,9 @@
megs: 32
display_library: sdl
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest
ata0-master: type=cdrom, path=os.iso, status=inserted
boot: cdrom
log: bochslog.txt
clock: sync=realtime, time0=local
cpu: count=1, ips=1000000

5
iso/boot/grub/menu.lst Normal file
View File

@ -0,0 +1,5 @@
default=0
timeout=0
title os
kernel /boot/kernel.elf

Binary file not shown.

3
kmain.c Normal file
View File

@ -0,0 +1,3 @@
void kmain(void) {
}

44
link.ld Normal file
View File

@ -0,0 +1,44 @@
ENTRY(loader)
SECTIONS {
/*
* The code should be loaded at an address greater or equal to 1MB because
* lower addresses are reserved to GRUB, BIOS and memory mapped I/O.
*
*/
. = 0x00100000;
/*
* Align all sections from all files to a 4KB boundary.
*
*/
.text ALIGN (0x1000) : {
*(.multiboot)
*(.text)
}
.rodata ALIGN (0x1000) : {
*(.rodata*)
}
.data ALIGN (0x1000) : {
*(.data)
}
/*
* Note:
* COMMON section keeps data objects that are not yet allocated,
* this is just a prelinking section, it will be transformed into bss
* and data after linking.
*
*/
.bss ALIGN (0x1000) : {
*(COMMON)
*(.bss)
}
}

43
loader.s Normal file
View File

@ -0,0 +1,43 @@
global loader
; Entry point in the kernel.
extern kmain
; GRUB will load the kernel only if it compiles with the multiboot spec.
; The header must contain a magic number, a flag (which can be set to 0x00)
; and the checksum (checksum = - (flag + magic_number))
MAGIC_NUMBER equ 0x1BADB002
FLAGS equ 0x00
CHECKSUM equ -(MAGIC_NUMBER + FLAGS)
; Put the header in multiboot section so that it is placed in the first 8Kib
; of the executable.
section .multiboot
align 4
dd MAGIC_NUMBER
dd FLAGS
dd CHECKSUM
; Create a kstack so that the entry point in the kernel can be called.
KSTACK_SIZE equ 4096
section .bss
align 4
kstack resb KSTACK_SIZE
; Set the stack and call kmain.
section .text
loader:
mov esp, kstack + KSTACK_SIZE
call kmain
.halt:
jmp .halt