diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/linker.ld b/linker.ld index b3c2db3..b13d161 100644 --- a/linker.ld +++ b/linker.ld @@ -1,242 +1,24 @@ -/* Script for -z combreloc -z separate-code */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -/* file generated with ld --verbose */ OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib"); -SECTIONS -{ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - _kernel_start = .; +OUTPUT_FORMAT("binary") - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; - PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - _kernel_end = .; - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +SECTIONS { + . = 0x7E00; + + .text BLOCK(4K) : ALIGN(4K) { + *(.entry) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) { + *(COMMON) + *(.bss) + } } \ No newline at end of file diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/linker.ld b/linker.ld index b3c2db3..b13d161 100644 --- a/linker.ld +++ b/linker.ld @@ -1,242 +1,24 @@ -/* Script for -z combreloc -z separate-code */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -/* file generated with ld --verbose */ OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib"); -SECTIONS -{ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - _kernel_start = .; +OUTPUT_FORMAT("binary") - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; - PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - _kernel_end = .; - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +SECTIONS { + . = 0x7E00; + + .text BLOCK(4K) : ALIGN(4K) { + *(.entry) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) { + *(COMMON) + *(.bss) + } } \ No newline at end of file diff --git a/src/boot/___boot.asm b/src/boot/___boot.asm new file mode 100644 index 0000000..bccd64a --- /dev/null +++ b/src/boot/___boot.asm @@ -0,0 +1,263 @@ +org 0x7c00 +bits 16 + +%ifndef SECTOR_CNT + %define SECTOR_CNT 3 +%endif + +jmp 0x0000:mbr_start + +mbr_start: + cli + cld + + ; Clear rest of segments + xor ax, ax + mov ds, ax + mov es, ax + + mov ss, ax + mov sp, 0x7c00 + mov bp, sp + + ; Store boot device + push dx + + ; swap to unreal mode + push ds + lgdt [gdtEnd] + mov eax, cr0 + or al, 1 + mov cr0, eax + jmp .pmode +.pmode: + mov bx, 0x08 + mov ds, bx + and al, 0xFE + mov cr0, eax + pop ds + sti + + ; Clear screen + xor ax, ax + mov al, 0x03 + int 0x10 + + ; Initialize serial port + mov dx, 0x3F9 + xor al, al + out dx, al + + add dx, 2 + mov al, 0x80 + out dx, al + + sub dx, 3 + xor al, al + out dx, al + + inc dx + out dx, al + + add dx, 2 + add al, 3 + out dx, al + + dec dx + mov al, 0xC7 + out dx, al + + add dx, 2 + mov al, 0x0B + out dx, al + +load_second_stage: + pop dx + push dx + mov bx, loader_entry + xor ch, ch + mov cl, 0x02 + xor dh, dh + mov byte [.sectors_to_load], SECTOR_CNT + + .read_start: + mov di, 5 + .read: + mov ah, 0x02 + mov al, byte [.sectors_to_load] + int 0x13 + jc .retry + cmp al, byte [.sectors_to_load] + je .done + sub byte [.sectors_to_load], al + mov cl, 0x01 + xor dh, 1 + jnz .read_start + inc ch + jmp .read_start + .retry: + ; disk read failed, reset disk + xor ah, ah + int 0x13 + dec di + jnz .read + mov si, .msg_diskread_fail + jmp .fail + .done: + jmp loader_entry + .fail: + lodsb + or al, al + mov ah, 0x0E + int 0x10 + test al, al + jnz .fail + .hang: + cli + hlt + jmp .hang + +.sectors_to_load: + db 1 +.msg_diskread_fail: + db "HALT: Failed to read boot disk", 0x0A, 0x0D, 0 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +; Padding & Signature +times 510-($-$$) db 0 +dw 0xAA55 + +loader_entry equ 0x7e00 + +bits 32 +_start: + ; mov ax, 16 + ; mov ds, ax + ; mov ss, ax + ; mov es, ax + ; mov fs, ax + ; mov gs, ax + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp x +x: + mov dword [0xb8000], 0x07690748 + jmp $ + mov ebx, hello + call printString +dump: + mov ebx, 0x7e00 + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + + +old: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/linker.ld b/linker.ld index b3c2db3..b13d161 100644 --- a/linker.ld +++ b/linker.ld @@ -1,242 +1,24 @@ -/* Script for -z combreloc -z separate-code */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -/* file generated with ld --verbose */ OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib"); -SECTIONS -{ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - _kernel_start = .; +OUTPUT_FORMAT("binary") - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; - PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - _kernel_end = .; - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +SECTIONS { + . = 0x7E00; + + .text BLOCK(4K) : ALIGN(4K) { + *(.entry) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) { + *(COMMON) + *(.bss) + } } \ No newline at end of file diff --git a/src/boot/___boot.asm b/src/boot/___boot.asm new file mode 100644 index 0000000..bccd64a --- /dev/null +++ b/src/boot/___boot.asm @@ -0,0 +1,263 @@ +org 0x7c00 +bits 16 + +%ifndef SECTOR_CNT + %define SECTOR_CNT 3 +%endif + +jmp 0x0000:mbr_start + +mbr_start: + cli + cld + + ; Clear rest of segments + xor ax, ax + mov ds, ax + mov es, ax + + mov ss, ax + mov sp, 0x7c00 + mov bp, sp + + ; Store boot device + push dx + + ; swap to unreal mode + push ds + lgdt [gdtEnd] + mov eax, cr0 + or al, 1 + mov cr0, eax + jmp .pmode +.pmode: + mov bx, 0x08 + mov ds, bx + and al, 0xFE + mov cr0, eax + pop ds + sti + + ; Clear screen + xor ax, ax + mov al, 0x03 + int 0x10 + + ; Initialize serial port + mov dx, 0x3F9 + xor al, al + out dx, al + + add dx, 2 + mov al, 0x80 + out dx, al + + sub dx, 3 + xor al, al + out dx, al + + inc dx + out dx, al + + add dx, 2 + add al, 3 + out dx, al + + dec dx + mov al, 0xC7 + out dx, al + + add dx, 2 + mov al, 0x0B + out dx, al + +load_second_stage: + pop dx + push dx + mov bx, loader_entry + xor ch, ch + mov cl, 0x02 + xor dh, dh + mov byte [.sectors_to_load], SECTOR_CNT + + .read_start: + mov di, 5 + .read: + mov ah, 0x02 + mov al, byte [.sectors_to_load] + int 0x13 + jc .retry + cmp al, byte [.sectors_to_load] + je .done + sub byte [.sectors_to_load], al + mov cl, 0x01 + xor dh, 1 + jnz .read_start + inc ch + jmp .read_start + .retry: + ; disk read failed, reset disk + xor ah, ah + int 0x13 + dec di + jnz .read + mov si, .msg_diskread_fail + jmp .fail + .done: + jmp loader_entry + .fail: + lodsb + or al, al + mov ah, 0x0E + int 0x10 + test al, al + jnz .fail + .hang: + cli + hlt + jmp .hang + +.sectors_to_load: + db 1 +.msg_diskread_fail: + db "HALT: Failed to read boot disk", 0x0A, 0x0D, 0 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +; Padding & Signature +times 510-($-$$) db 0 +dw 0xAA55 + +loader_entry equ 0x7e00 + +bits 32 +_start: + ; mov ax, 16 + ; mov ds, ax + ; mov ss, ax + ; mov es, ax + ; mov fs, ax + ; mov gs, ax + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp x +x: + mov dword [0xb8000], 0x07690748 + jmp $ + mov ebx, hello + call printString +dump: + mov ebx, 0x7e00 + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + + +old: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/boot/__boot.asm b/src/boot/__boot.asm new file mode 100644 index 0000000..6c2e1be --- /dev/null +++ b/src/boot/__boot.asm @@ -0,0 +1,278 @@ +; bootp.asm Bootstrap for protected mode image +; Version 1.0, Dec 14, 1997 +; Sample code +; by John S. Fine johnfine@erols.com +; I do not place any restrictions on your use of this source code +; I do not provide any warranty of the correctness of this source code +;_____________________________________________________________________________ +; +; This program should be useable for three purposes: +; +; 1) Use it unmodified, in the boot sector of a floppy, to load a test image +; from the hard drive. In many environments, a floppy boot is the easiest +; way to have a clean boot for a pmode test. By getting the image itself +; from the hard drive, you eliminate most of the delays and restrictions of +; a floppy boot. +; +; 2) As a bootstrap tutorial, it demonstrates many of the steps required by +; a protected mode bootstrap, and many tricks for keeping code size down +; in a bootstrap. +; +; 3) As the basis for a protected mode, partition level, bootstrap. Just +; remove some of the steps that aren't needed in your environment, and add +; some error messages for unsupported conditions. +;_____________________________________________________________________________ +; +; Documentation: +; +; 1) Most of the documentation is in comments at the END of this file +; 2) Some of the documentation is in a separate file: BOOTP.TXT +; 3) In the comments within the source code are several uses of {}, each +; of these indicates a section of documentation at the end of this file +; which applies to that part of the source code. +; 4) The major way I reduced code size was to rely on left-over values in +; registers and/or to initialize registers at points where doing so was +; cheap. To make that understandable, there are several lines beginning +; with either ";>" or ";>>". These lines describe the register contents +; at that point in the code. ";>" indicates that the value is required +; by the next section of code. ";>>" indicates that the value passes +; through the next section unmodified and is needed later. +;_____________________________________________________________________________ + + %include "gdt.inc" + + struc DISK_PARM ;BIOS hard disk parameters +DP_cyl resw 1 +DP_heads resb 1 + resb 5 ;Obsolete +DP_ctrl resb 1 + resb 5 ;Obsolete +DP_sect resb 1 + resb 1 + endstruc + + struc PT_ENT ;Partition table entry +BootFlag resb 1 +BeginHead resb 1 +BeginSector resb 1 +BeginCyl resb 1 +SystemID resb 1 +EndHead resb 1 +EndSector resb 1 +EndCyl resb 1 +RelSectorLow resw 1 +RelSectorHigh resw 1 +NumSectorsLow resw 1 +NumSectorsHigh resw 1 + endstruc + + struc BB ;Partition Boot block + resb 0xD ;Things we ignore +BB_clu resb 1 ;Sectors per cluster +BB_res resw 1 ;Reserved sectors +BB_fats resb 1 ;Number of FATs +BB_root resw 1 ;Root directory entries + resb 3 ;Things we ignore +BB_fat resw 1 ;Sectors per fat + endstruc + +max_xfer equ 0x78 ;Maximum number of sectors to transfer at once + ;Must be divisible by 8 + + +SEGMENT START USE16 + jmp short start ;Standard start of boot sector + nop + resb 0x3B ;Skip over parameters (set by format) + +start: + cli + lgdt [cs:gdt] + mov al, 0x80 + mov es, ax + + xor eax, eax ;Segment and LBN + mov ds, ax ;{1B} + mov ss, ax + mov sp, start + + xor edi, edi + inc di + call read_sectors + +;> eax = 0 +;> edi = 1 +;> ds = 0 +;> es = 80 + + mov si, 0x9BE-PT_ENT_size ;Point at partition table +.10: add si, byte PT_ENT_size + cmp [si+BootFlag], al ;Active? + je .10 ;No + jg boot_error + mov eax, [si+RelSectorLow] ;LBN sector number of partition + call read_sectors ;Read partition boot block + +;> eax = LBN of partition +;> ebx = 0000???? +;> edi = 0000???? +;> ds = 0 + + mov bp, 0x800 ;Point at partition boot block + mov bx, [bp+BB_res] ;Sectors before first FAT + add eax, ebx ;LBN of FAT + push eax ;Need to read the FAT later + movzx bx, [bp+BB_fats] ;Number of FATs + imul bx, [bp+BB_fat] ;Times size of FAT + add eax, ebx ;LBN of Root directory + mov di, [bp+BB_root] ;Root directory entries + push di ; used again later + dec di ;Convert to number of sectors + shr di, 4 ; 16 directory entries per sector + inc di + mov es, bp ;Read directory at 800:0 + call read_sectors + add eax, edi ;LBN of cluster 2 + +;>> [sp+2] = LBN of FAT +;> [sp] = Number of root directory entries +;>> eax = LBN of cluster 2 +;> edi = 0000???? +;>> bp = 800 +;> ds = 0 +;> es = 800 + + pop bx ;Root directory entries + xor di, di ;Point at directory {1C} +.20: mov si, file_name ;Name of file we want + xor ecx, ecx + mov cl, 11 + a32 rep cmpsb ;Found the file? + je found ;Yes + add cl, 21 ;Offset to next directory entry + add edi, ecx ;Advance to next entry + dec bx ;Loop through all entries + jnz .20 + + ;Couldn't find file in directory +boot_error: +disk_error: + mov ax, 0xE07 ;{3} + int 10h + jmp short $ + +;> [sp] = LBN of FAT +;> eax = LBN of cluster 2 +;> es:edi = Pointer to end of name within directory entry +;> bp = 800 +;> ds = 0 +;> es = 800 + +eof: + mov bh, 0x9C + mov es, bx ;es = 9C00 + xor di, di ;{1E} Address of page tables WRT es + mov bh, 4096/256 ;ebx = 4096 +.10: mov ch, 4 ;cx = 1024 + mov al, 7 +.20: stosd + add eax, ebx + int 8 ;{8} + loop .20 + shr eax, 2 ;{4C} (first time only) 4Mb / 4 = 1Mb + shr si, 1 ;Done just one page? + jo .10 ;Yes + + cli ;{6} + + mov eax, 0x9C007 ;First page tbl pointer in page dir + stosd ;{1H} + mov ax, (1024-3)*2 + xchg ax, cx + rep stosw + mov ax, 0xD007 ;0FF800000 page tbl pointer + stosd ;{1F} + mov ah, 0xE0 ;Page directory self pointer + stosd ;{1G} + mov al, 0 + mov CR3, eax ;Set up page directory + mov eax, CR0 ;Turn on paging and protected mode + or eax, 0x80000001 + mov CR0, eax + mov cl, flat_data ;Setup ds and es + push cx ;{5} + pop ds + mov es, cx + jmp dword 8:0xFF800000 ;Go + + read_sectors: +; Input: +; EAX = LBN +; DI = sector count +; ES = segment +; Output: +; BL = low byte of ES +; EBX high half cleared +; DL = 0x80 +; EDX high half cleared +; ESI = 0 +; Clobbered: +; BH, CX, DH + + push eax + push di + push es + +.10: push eax ;LBN + + push ds + lds si, [0x104] ;Hard drive 0 parameters + cdq ;edx = 0 + movzx ebx, byte [si+DP_sect] + div ebx ;EAX=track ;EDX=sector-1 + mov cx, dx ;CL=sector-1 ;CH=0 + inc cx ;CL=Sector number + xor dx, dx + mov bl, [si+DP_heads] + pop ds + div ebx + + mov dh, dl + mov dl, 0x80 + xchg ch, al + shr ax, 2 + or cl, al + mov al, max_xfer ;AX = Maximum sectors to xfer + cmp ax, di ;Total is larger? + jb .20 ;Yes: transfer maximum + xchg ax, di ;No: AX=total ;DI=maximum +.20: mov ah, 2 ;Read + xor bx, bx + int 13h + jc near disk_error + + mov bx, es + add bh, max_xfer>>3 ;Advance segment + mov es, bx + + pop eax + add eax, byte max_xfer + sub di, byte max_xfer + ja .10 + + pop es + pop di + pop eax + xor esi, esi + ret + +gdt start_gdt + +flat_code desc 0, 0xFFBFF, D_CODE+D_READ+D_BIG+D_BIG_LIM + +flat_data desc 0, 0xFFFFF, D_DATA+D_WRITE+D_BIG+D_BIG_LIM + + end_gdt + + resb 0x1FE+$$-$ + db 0x55, 0xAA diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/linker.ld b/linker.ld index b3c2db3..b13d161 100644 --- a/linker.ld +++ b/linker.ld @@ -1,242 +1,24 @@ -/* Script for -z combreloc -z separate-code */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -/* file generated with ld --verbose */ OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib"); -SECTIONS -{ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - _kernel_start = .; +OUTPUT_FORMAT("binary") - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; - PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - _kernel_end = .; - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +SECTIONS { + . = 0x7E00; + + .text BLOCK(4K) : ALIGN(4K) { + *(.entry) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) { + *(COMMON) + *(.bss) + } } \ No newline at end of file diff --git a/src/boot/___boot.asm b/src/boot/___boot.asm new file mode 100644 index 0000000..bccd64a --- /dev/null +++ b/src/boot/___boot.asm @@ -0,0 +1,263 @@ +org 0x7c00 +bits 16 + +%ifndef SECTOR_CNT + %define SECTOR_CNT 3 +%endif + +jmp 0x0000:mbr_start + +mbr_start: + cli + cld + + ; Clear rest of segments + xor ax, ax + mov ds, ax + mov es, ax + + mov ss, ax + mov sp, 0x7c00 + mov bp, sp + + ; Store boot device + push dx + + ; swap to unreal mode + push ds + lgdt [gdtEnd] + mov eax, cr0 + or al, 1 + mov cr0, eax + jmp .pmode +.pmode: + mov bx, 0x08 + mov ds, bx + and al, 0xFE + mov cr0, eax + pop ds + sti + + ; Clear screen + xor ax, ax + mov al, 0x03 + int 0x10 + + ; Initialize serial port + mov dx, 0x3F9 + xor al, al + out dx, al + + add dx, 2 + mov al, 0x80 + out dx, al + + sub dx, 3 + xor al, al + out dx, al + + inc dx + out dx, al + + add dx, 2 + add al, 3 + out dx, al + + dec dx + mov al, 0xC7 + out dx, al + + add dx, 2 + mov al, 0x0B + out dx, al + +load_second_stage: + pop dx + push dx + mov bx, loader_entry + xor ch, ch + mov cl, 0x02 + xor dh, dh + mov byte [.sectors_to_load], SECTOR_CNT + + .read_start: + mov di, 5 + .read: + mov ah, 0x02 + mov al, byte [.sectors_to_load] + int 0x13 + jc .retry + cmp al, byte [.sectors_to_load] + je .done + sub byte [.sectors_to_load], al + mov cl, 0x01 + xor dh, 1 + jnz .read_start + inc ch + jmp .read_start + .retry: + ; disk read failed, reset disk + xor ah, ah + int 0x13 + dec di + jnz .read + mov si, .msg_diskread_fail + jmp .fail + .done: + jmp loader_entry + .fail: + lodsb + or al, al + mov ah, 0x0E + int 0x10 + test al, al + jnz .fail + .hang: + cli + hlt + jmp .hang + +.sectors_to_load: + db 1 +.msg_diskread_fail: + db "HALT: Failed to read boot disk", 0x0A, 0x0D, 0 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +; Padding & Signature +times 510-($-$$) db 0 +dw 0xAA55 + +loader_entry equ 0x7e00 + +bits 32 +_start: + ; mov ax, 16 + ; mov ds, ax + ; mov ss, ax + ; mov es, ax + ; mov fs, ax + ; mov gs, ax + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp x +x: + mov dword [0xb8000], 0x07690748 + jmp $ + mov ebx, hello + call printString +dump: + mov ebx, 0x7e00 + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + + +old: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/boot/__boot.asm b/src/boot/__boot.asm new file mode 100644 index 0000000..6c2e1be --- /dev/null +++ b/src/boot/__boot.asm @@ -0,0 +1,278 @@ +; bootp.asm Bootstrap for protected mode image +; Version 1.0, Dec 14, 1997 +; Sample code +; by John S. Fine johnfine@erols.com +; I do not place any restrictions on your use of this source code +; I do not provide any warranty of the correctness of this source code +;_____________________________________________________________________________ +; +; This program should be useable for three purposes: +; +; 1) Use it unmodified, in the boot sector of a floppy, to load a test image +; from the hard drive. In many environments, a floppy boot is the easiest +; way to have a clean boot for a pmode test. By getting the image itself +; from the hard drive, you eliminate most of the delays and restrictions of +; a floppy boot. +; +; 2) As a bootstrap tutorial, it demonstrates many of the steps required by +; a protected mode bootstrap, and many tricks for keeping code size down +; in a bootstrap. +; +; 3) As the basis for a protected mode, partition level, bootstrap. Just +; remove some of the steps that aren't needed in your environment, and add +; some error messages for unsupported conditions. +;_____________________________________________________________________________ +; +; Documentation: +; +; 1) Most of the documentation is in comments at the END of this file +; 2) Some of the documentation is in a separate file: BOOTP.TXT +; 3) In the comments within the source code are several uses of {}, each +; of these indicates a section of documentation at the end of this file +; which applies to that part of the source code. +; 4) The major way I reduced code size was to rely on left-over values in +; registers and/or to initialize registers at points where doing so was +; cheap. To make that understandable, there are several lines beginning +; with either ";>" or ";>>". These lines describe the register contents +; at that point in the code. ";>" indicates that the value is required +; by the next section of code. ";>>" indicates that the value passes +; through the next section unmodified and is needed later. +;_____________________________________________________________________________ + + %include "gdt.inc" + + struc DISK_PARM ;BIOS hard disk parameters +DP_cyl resw 1 +DP_heads resb 1 + resb 5 ;Obsolete +DP_ctrl resb 1 + resb 5 ;Obsolete +DP_sect resb 1 + resb 1 + endstruc + + struc PT_ENT ;Partition table entry +BootFlag resb 1 +BeginHead resb 1 +BeginSector resb 1 +BeginCyl resb 1 +SystemID resb 1 +EndHead resb 1 +EndSector resb 1 +EndCyl resb 1 +RelSectorLow resw 1 +RelSectorHigh resw 1 +NumSectorsLow resw 1 +NumSectorsHigh resw 1 + endstruc + + struc BB ;Partition Boot block + resb 0xD ;Things we ignore +BB_clu resb 1 ;Sectors per cluster +BB_res resw 1 ;Reserved sectors +BB_fats resb 1 ;Number of FATs +BB_root resw 1 ;Root directory entries + resb 3 ;Things we ignore +BB_fat resw 1 ;Sectors per fat + endstruc + +max_xfer equ 0x78 ;Maximum number of sectors to transfer at once + ;Must be divisible by 8 + + +SEGMENT START USE16 + jmp short start ;Standard start of boot sector + nop + resb 0x3B ;Skip over parameters (set by format) + +start: + cli + lgdt [cs:gdt] + mov al, 0x80 + mov es, ax + + xor eax, eax ;Segment and LBN + mov ds, ax ;{1B} + mov ss, ax + mov sp, start + + xor edi, edi + inc di + call read_sectors + +;> eax = 0 +;> edi = 1 +;> ds = 0 +;> es = 80 + + mov si, 0x9BE-PT_ENT_size ;Point at partition table +.10: add si, byte PT_ENT_size + cmp [si+BootFlag], al ;Active? + je .10 ;No + jg boot_error + mov eax, [si+RelSectorLow] ;LBN sector number of partition + call read_sectors ;Read partition boot block + +;> eax = LBN of partition +;> ebx = 0000???? +;> edi = 0000???? +;> ds = 0 + + mov bp, 0x800 ;Point at partition boot block + mov bx, [bp+BB_res] ;Sectors before first FAT + add eax, ebx ;LBN of FAT + push eax ;Need to read the FAT later + movzx bx, [bp+BB_fats] ;Number of FATs + imul bx, [bp+BB_fat] ;Times size of FAT + add eax, ebx ;LBN of Root directory + mov di, [bp+BB_root] ;Root directory entries + push di ; used again later + dec di ;Convert to number of sectors + shr di, 4 ; 16 directory entries per sector + inc di + mov es, bp ;Read directory at 800:0 + call read_sectors + add eax, edi ;LBN of cluster 2 + +;>> [sp+2] = LBN of FAT +;> [sp] = Number of root directory entries +;>> eax = LBN of cluster 2 +;> edi = 0000???? +;>> bp = 800 +;> ds = 0 +;> es = 800 + + pop bx ;Root directory entries + xor di, di ;Point at directory {1C} +.20: mov si, file_name ;Name of file we want + xor ecx, ecx + mov cl, 11 + a32 rep cmpsb ;Found the file? + je found ;Yes + add cl, 21 ;Offset to next directory entry + add edi, ecx ;Advance to next entry + dec bx ;Loop through all entries + jnz .20 + + ;Couldn't find file in directory +boot_error: +disk_error: + mov ax, 0xE07 ;{3} + int 10h + jmp short $ + +;> [sp] = LBN of FAT +;> eax = LBN of cluster 2 +;> es:edi = Pointer to end of name within directory entry +;> bp = 800 +;> ds = 0 +;> es = 800 + +eof: + mov bh, 0x9C + mov es, bx ;es = 9C00 + xor di, di ;{1E} Address of page tables WRT es + mov bh, 4096/256 ;ebx = 4096 +.10: mov ch, 4 ;cx = 1024 + mov al, 7 +.20: stosd + add eax, ebx + int 8 ;{8} + loop .20 + shr eax, 2 ;{4C} (first time only) 4Mb / 4 = 1Mb + shr si, 1 ;Done just one page? + jo .10 ;Yes + + cli ;{6} + + mov eax, 0x9C007 ;First page tbl pointer in page dir + stosd ;{1H} + mov ax, (1024-3)*2 + xchg ax, cx + rep stosw + mov ax, 0xD007 ;0FF800000 page tbl pointer + stosd ;{1F} + mov ah, 0xE0 ;Page directory self pointer + stosd ;{1G} + mov al, 0 + mov CR3, eax ;Set up page directory + mov eax, CR0 ;Turn on paging and protected mode + or eax, 0x80000001 + mov CR0, eax + mov cl, flat_data ;Setup ds and es + push cx ;{5} + pop ds + mov es, cx + jmp dword 8:0xFF800000 ;Go + + read_sectors: +; Input: +; EAX = LBN +; DI = sector count +; ES = segment +; Output: +; BL = low byte of ES +; EBX high half cleared +; DL = 0x80 +; EDX high half cleared +; ESI = 0 +; Clobbered: +; BH, CX, DH + + push eax + push di + push es + +.10: push eax ;LBN + + push ds + lds si, [0x104] ;Hard drive 0 parameters + cdq ;edx = 0 + movzx ebx, byte [si+DP_sect] + div ebx ;EAX=track ;EDX=sector-1 + mov cx, dx ;CL=sector-1 ;CH=0 + inc cx ;CL=Sector number + xor dx, dx + mov bl, [si+DP_heads] + pop ds + div ebx + + mov dh, dl + mov dl, 0x80 + xchg ch, al + shr ax, 2 + or cl, al + mov al, max_xfer ;AX = Maximum sectors to xfer + cmp ax, di ;Total is larger? + jb .20 ;Yes: transfer maximum + xchg ax, di ;No: AX=total ;DI=maximum +.20: mov ah, 2 ;Read + xor bx, bx + int 13h + jc near disk_error + + mov bx, es + add bh, max_xfer>>3 ;Advance segment + mov es, bx + + pop eax + add eax, byte max_xfer + sub di, byte max_xfer + ja .10 + + pop es + pop di + pop eax + xor esi, esi + ret + +gdt start_gdt + +flat_code desc 0, 0xFFBFF, D_CODE+D_READ+D_BIG+D_BIG_LIM + +flat_data desc 0, 0xFFFFF, D_DATA+D_WRITE+D_BIG+D_BIG_LIM + + end_gdt + + resb 0x1FE+$$-$ + db 0x55, 0xAA diff --git a/src/boot/boot.asm b/src/boot/boot.asm new file mode 100644 index 0000000..19a0342 --- /dev/null +++ b/src/boot/boot.asm @@ -0,0 +1,213 @@ +BITS 16 +ORG 0x7C00 + +section .text + +loadOS: + mov [BOOT_DRIVE], dl + + xor ax, ax + mov ds, ax + mov es, ax + mov ss, ax + mov fs, ax + mov gs, ax + + mov bp, 0x9000 + mov sp, bp +resetDisk: + xor ah, ah + mov dl, [BOOT_DRIVE] + ; int 0x13 +load: + ; mov ah, 0x02 + ; mov al, 1 + ; mov cx, 0x01 + ; mov bx, CODE_SEG + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov dl, [BOOT_DRIVE] + ; mov dh, 0x00 + ; int 0x13 + ; jc error + ; jmp dump + ; mov ah, 0x42 + ; mov dl, [BOOT_DRIVE] + ; mov cx, 0x00 + ; mov ds, cx + ; mov si, diskAddrebx + ; mov eax, 0x0001 + ; mov dl, [BOOT_DRIVE] + ; mov di, KERNEL_POSITION + ; mov cx, 0x01 + +diskLoad: + ; mov ah, 0x02 ; read + ; mov al, 0x01 ; sector count + ; mov cx, 0x0002 ; cylinder & sector + ; mov dh, 0x00 ; head + ; mov dl, [BOOT_DRIVE] ; drive + ; ; xor bx, bx ; position + ; ; mov es, bx + ; xor bx, bx + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov ah, 0x42 + ; mov dx, DAP + ; mov si, DAP + ; xor dh, dh + mov dl, [BOOT_DRIVE] + mov ah, 0x02 ;Read Disk Sectors + mov al, 0x05 ;Read one sector only + mov ch, 0x00 ;Track 0 + mov cl, 0x02 ;Sector 2 + mov dh, 0x00 ;Head 0 + ; mov dl, 0x00 ;Drive 0 (Floppy 1) + ; mov bx, 0x800 ;Segment 0x2000 + ; mov es, bx + mov bx, KERNEL_POSITION ;Start of Segment + + int 0x13 + + mov [errorByte], ah + +switchToX86: + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp CODE_SEG:_start + +bits 32 +_start: + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov al, [BOOT_DRIVE] + call print_hex_byte + mov ebx, hello + call printString + mov al, [errorByte] + call print_hex_byte +dump: + mov ebx, KERNEL_POSITION + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +CODE_SEG equ 8 +DATA_SEG equ 16 + +BOOT_DRIVE: db 0 +ALIGN 4 +DAP: +.size db 0x10 +.reserved db 0x00 +.sectors dw 0x0001 +.offset dw KERNEL_POSITION +.segment dw 0x0000 +.lbaLsd dd 0x00000000 +.lbaMsd dd 0x00000002 + +errorByte db 0x00 + +hello db 'hello world! ', 0x00 +errorMessage db 'error! ', 0x00 + + + +padding: + times 510-($-$$) db 0x00 + dw 0xAA55 + +KERNEL_POSITION equ 0x7e00 +KERNEL_SIZE equ 1 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/linker.ld b/linker.ld index b3c2db3..b13d161 100644 --- a/linker.ld +++ b/linker.ld @@ -1,242 +1,24 @@ -/* Script for -z combreloc -z separate-code */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -/* file generated with ld --verbose */ OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib"); -SECTIONS -{ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - _kernel_start = .; +OUTPUT_FORMAT("binary") - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; - PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - _kernel_end = .; - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +SECTIONS { + . = 0x7E00; + + .text BLOCK(4K) : ALIGN(4K) { + *(.entry) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) { + *(COMMON) + *(.bss) + } } \ No newline at end of file diff --git a/src/boot/___boot.asm b/src/boot/___boot.asm new file mode 100644 index 0000000..bccd64a --- /dev/null +++ b/src/boot/___boot.asm @@ -0,0 +1,263 @@ +org 0x7c00 +bits 16 + +%ifndef SECTOR_CNT + %define SECTOR_CNT 3 +%endif + +jmp 0x0000:mbr_start + +mbr_start: + cli + cld + + ; Clear rest of segments + xor ax, ax + mov ds, ax + mov es, ax + + mov ss, ax + mov sp, 0x7c00 + mov bp, sp + + ; Store boot device + push dx + + ; swap to unreal mode + push ds + lgdt [gdtEnd] + mov eax, cr0 + or al, 1 + mov cr0, eax + jmp .pmode +.pmode: + mov bx, 0x08 + mov ds, bx + and al, 0xFE + mov cr0, eax + pop ds + sti + + ; Clear screen + xor ax, ax + mov al, 0x03 + int 0x10 + + ; Initialize serial port + mov dx, 0x3F9 + xor al, al + out dx, al + + add dx, 2 + mov al, 0x80 + out dx, al + + sub dx, 3 + xor al, al + out dx, al + + inc dx + out dx, al + + add dx, 2 + add al, 3 + out dx, al + + dec dx + mov al, 0xC7 + out dx, al + + add dx, 2 + mov al, 0x0B + out dx, al + +load_second_stage: + pop dx + push dx + mov bx, loader_entry + xor ch, ch + mov cl, 0x02 + xor dh, dh + mov byte [.sectors_to_load], SECTOR_CNT + + .read_start: + mov di, 5 + .read: + mov ah, 0x02 + mov al, byte [.sectors_to_load] + int 0x13 + jc .retry + cmp al, byte [.sectors_to_load] + je .done + sub byte [.sectors_to_load], al + mov cl, 0x01 + xor dh, 1 + jnz .read_start + inc ch + jmp .read_start + .retry: + ; disk read failed, reset disk + xor ah, ah + int 0x13 + dec di + jnz .read + mov si, .msg_diskread_fail + jmp .fail + .done: + jmp loader_entry + .fail: + lodsb + or al, al + mov ah, 0x0E + int 0x10 + test al, al + jnz .fail + .hang: + cli + hlt + jmp .hang + +.sectors_to_load: + db 1 +.msg_diskread_fail: + db "HALT: Failed to read boot disk", 0x0A, 0x0D, 0 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +; Padding & Signature +times 510-($-$$) db 0 +dw 0xAA55 + +loader_entry equ 0x7e00 + +bits 32 +_start: + ; mov ax, 16 + ; mov ds, ax + ; mov ss, ax + ; mov es, ax + ; mov fs, ax + ; mov gs, ax + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp x +x: + mov dword [0xb8000], 0x07690748 + jmp $ + mov ebx, hello + call printString +dump: + mov ebx, 0x7e00 + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + + +old: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/boot/__boot.asm b/src/boot/__boot.asm new file mode 100644 index 0000000..6c2e1be --- /dev/null +++ b/src/boot/__boot.asm @@ -0,0 +1,278 @@ +; bootp.asm Bootstrap for protected mode image +; Version 1.0, Dec 14, 1997 +; Sample code +; by John S. Fine johnfine@erols.com +; I do not place any restrictions on your use of this source code +; I do not provide any warranty of the correctness of this source code +;_____________________________________________________________________________ +; +; This program should be useable for three purposes: +; +; 1) Use it unmodified, in the boot sector of a floppy, to load a test image +; from the hard drive. In many environments, a floppy boot is the easiest +; way to have a clean boot for a pmode test. By getting the image itself +; from the hard drive, you eliminate most of the delays and restrictions of +; a floppy boot. +; +; 2) As a bootstrap tutorial, it demonstrates many of the steps required by +; a protected mode bootstrap, and many tricks for keeping code size down +; in a bootstrap. +; +; 3) As the basis for a protected mode, partition level, bootstrap. Just +; remove some of the steps that aren't needed in your environment, and add +; some error messages for unsupported conditions. +;_____________________________________________________________________________ +; +; Documentation: +; +; 1) Most of the documentation is in comments at the END of this file +; 2) Some of the documentation is in a separate file: BOOTP.TXT +; 3) In the comments within the source code are several uses of {}, each +; of these indicates a section of documentation at the end of this file +; which applies to that part of the source code. +; 4) The major way I reduced code size was to rely on left-over values in +; registers and/or to initialize registers at points where doing so was +; cheap. To make that understandable, there are several lines beginning +; with either ";>" or ";>>". These lines describe the register contents +; at that point in the code. ";>" indicates that the value is required +; by the next section of code. ";>>" indicates that the value passes +; through the next section unmodified and is needed later. +;_____________________________________________________________________________ + + %include "gdt.inc" + + struc DISK_PARM ;BIOS hard disk parameters +DP_cyl resw 1 +DP_heads resb 1 + resb 5 ;Obsolete +DP_ctrl resb 1 + resb 5 ;Obsolete +DP_sect resb 1 + resb 1 + endstruc + + struc PT_ENT ;Partition table entry +BootFlag resb 1 +BeginHead resb 1 +BeginSector resb 1 +BeginCyl resb 1 +SystemID resb 1 +EndHead resb 1 +EndSector resb 1 +EndCyl resb 1 +RelSectorLow resw 1 +RelSectorHigh resw 1 +NumSectorsLow resw 1 +NumSectorsHigh resw 1 + endstruc + + struc BB ;Partition Boot block + resb 0xD ;Things we ignore +BB_clu resb 1 ;Sectors per cluster +BB_res resw 1 ;Reserved sectors +BB_fats resb 1 ;Number of FATs +BB_root resw 1 ;Root directory entries + resb 3 ;Things we ignore +BB_fat resw 1 ;Sectors per fat + endstruc + +max_xfer equ 0x78 ;Maximum number of sectors to transfer at once + ;Must be divisible by 8 + + +SEGMENT START USE16 + jmp short start ;Standard start of boot sector + nop + resb 0x3B ;Skip over parameters (set by format) + +start: + cli + lgdt [cs:gdt] + mov al, 0x80 + mov es, ax + + xor eax, eax ;Segment and LBN + mov ds, ax ;{1B} + mov ss, ax + mov sp, start + + xor edi, edi + inc di + call read_sectors + +;> eax = 0 +;> edi = 1 +;> ds = 0 +;> es = 80 + + mov si, 0x9BE-PT_ENT_size ;Point at partition table +.10: add si, byte PT_ENT_size + cmp [si+BootFlag], al ;Active? + je .10 ;No + jg boot_error + mov eax, [si+RelSectorLow] ;LBN sector number of partition + call read_sectors ;Read partition boot block + +;> eax = LBN of partition +;> ebx = 0000???? +;> edi = 0000???? +;> ds = 0 + + mov bp, 0x800 ;Point at partition boot block + mov bx, [bp+BB_res] ;Sectors before first FAT + add eax, ebx ;LBN of FAT + push eax ;Need to read the FAT later + movzx bx, [bp+BB_fats] ;Number of FATs + imul bx, [bp+BB_fat] ;Times size of FAT + add eax, ebx ;LBN of Root directory + mov di, [bp+BB_root] ;Root directory entries + push di ; used again later + dec di ;Convert to number of sectors + shr di, 4 ; 16 directory entries per sector + inc di + mov es, bp ;Read directory at 800:0 + call read_sectors + add eax, edi ;LBN of cluster 2 + +;>> [sp+2] = LBN of FAT +;> [sp] = Number of root directory entries +;>> eax = LBN of cluster 2 +;> edi = 0000???? +;>> bp = 800 +;> ds = 0 +;> es = 800 + + pop bx ;Root directory entries + xor di, di ;Point at directory {1C} +.20: mov si, file_name ;Name of file we want + xor ecx, ecx + mov cl, 11 + a32 rep cmpsb ;Found the file? + je found ;Yes + add cl, 21 ;Offset to next directory entry + add edi, ecx ;Advance to next entry + dec bx ;Loop through all entries + jnz .20 + + ;Couldn't find file in directory +boot_error: +disk_error: + mov ax, 0xE07 ;{3} + int 10h + jmp short $ + +;> [sp] = LBN of FAT +;> eax = LBN of cluster 2 +;> es:edi = Pointer to end of name within directory entry +;> bp = 800 +;> ds = 0 +;> es = 800 + +eof: + mov bh, 0x9C + mov es, bx ;es = 9C00 + xor di, di ;{1E} Address of page tables WRT es + mov bh, 4096/256 ;ebx = 4096 +.10: mov ch, 4 ;cx = 1024 + mov al, 7 +.20: stosd + add eax, ebx + int 8 ;{8} + loop .20 + shr eax, 2 ;{4C} (first time only) 4Mb / 4 = 1Mb + shr si, 1 ;Done just one page? + jo .10 ;Yes + + cli ;{6} + + mov eax, 0x9C007 ;First page tbl pointer in page dir + stosd ;{1H} + mov ax, (1024-3)*2 + xchg ax, cx + rep stosw + mov ax, 0xD007 ;0FF800000 page tbl pointer + stosd ;{1F} + mov ah, 0xE0 ;Page directory self pointer + stosd ;{1G} + mov al, 0 + mov CR3, eax ;Set up page directory + mov eax, CR0 ;Turn on paging and protected mode + or eax, 0x80000001 + mov CR0, eax + mov cl, flat_data ;Setup ds and es + push cx ;{5} + pop ds + mov es, cx + jmp dword 8:0xFF800000 ;Go + + read_sectors: +; Input: +; EAX = LBN +; DI = sector count +; ES = segment +; Output: +; BL = low byte of ES +; EBX high half cleared +; DL = 0x80 +; EDX high half cleared +; ESI = 0 +; Clobbered: +; BH, CX, DH + + push eax + push di + push es + +.10: push eax ;LBN + + push ds + lds si, [0x104] ;Hard drive 0 parameters + cdq ;edx = 0 + movzx ebx, byte [si+DP_sect] + div ebx ;EAX=track ;EDX=sector-1 + mov cx, dx ;CL=sector-1 ;CH=0 + inc cx ;CL=Sector number + xor dx, dx + mov bl, [si+DP_heads] + pop ds + div ebx + + mov dh, dl + mov dl, 0x80 + xchg ch, al + shr ax, 2 + or cl, al + mov al, max_xfer ;AX = Maximum sectors to xfer + cmp ax, di ;Total is larger? + jb .20 ;Yes: transfer maximum + xchg ax, di ;No: AX=total ;DI=maximum +.20: mov ah, 2 ;Read + xor bx, bx + int 13h + jc near disk_error + + mov bx, es + add bh, max_xfer>>3 ;Advance segment + mov es, bx + + pop eax + add eax, byte max_xfer + sub di, byte max_xfer + ja .10 + + pop es + pop di + pop eax + xor esi, esi + ret + +gdt start_gdt + +flat_code desc 0, 0xFFBFF, D_CODE+D_READ+D_BIG+D_BIG_LIM + +flat_data desc 0, 0xFFFFF, D_DATA+D_WRITE+D_BIG+D_BIG_LIM + + end_gdt + + resb 0x1FE+$$-$ + db 0x55, 0xAA diff --git a/src/boot/boot.asm b/src/boot/boot.asm new file mode 100644 index 0000000..19a0342 --- /dev/null +++ b/src/boot/boot.asm @@ -0,0 +1,213 @@ +BITS 16 +ORG 0x7C00 + +section .text + +loadOS: + mov [BOOT_DRIVE], dl + + xor ax, ax + mov ds, ax + mov es, ax + mov ss, ax + mov fs, ax + mov gs, ax + + mov bp, 0x9000 + mov sp, bp +resetDisk: + xor ah, ah + mov dl, [BOOT_DRIVE] + ; int 0x13 +load: + ; mov ah, 0x02 + ; mov al, 1 + ; mov cx, 0x01 + ; mov bx, CODE_SEG + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov dl, [BOOT_DRIVE] + ; mov dh, 0x00 + ; int 0x13 + ; jc error + ; jmp dump + ; mov ah, 0x42 + ; mov dl, [BOOT_DRIVE] + ; mov cx, 0x00 + ; mov ds, cx + ; mov si, diskAddrebx + ; mov eax, 0x0001 + ; mov dl, [BOOT_DRIVE] + ; mov di, KERNEL_POSITION + ; mov cx, 0x01 + +diskLoad: + ; mov ah, 0x02 ; read + ; mov al, 0x01 ; sector count + ; mov cx, 0x0002 ; cylinder & sector + ; mov dh, 0x00 ; head + ; mov dl, [BOOT_DRIVE] ; drive + ; ; xor bx, bx ; position + ; ; mov es, bx + ; xor bx, bx + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov ah, 0x42 + ; mov dx, DAP + ; mov si, DAP + ; xor dh, dh + mov dl, [BOOT_DRIVE] + mov ah, 0x02 ;Read Disk Sectors + mov al, 0x05 ;Read one sector only + mov ch, 0x00 ;Track 0 + mov cl, 0x02 ;Sector 2 + mov dh, 0x00 ;Head 0 + ; mov dl, 0x00 ;Drive 0 (Floppy 1) + ; mov bx, 0x800 ;Segment 0x2000 + ; mov es, bx + mov bx, KERNEL_POSITION ;Start of Segment + + int 0x13 + + mov [errorByte], ah + +switchToX86: + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp CODE_SEG:_start + +bits 32 +_start: + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov al, [BOOT_DRIVE] + call print_hex_byte + mov ebx, hello + call printString + mov al, [errorByte] + call print_hex_byte +dump: + mov ebx, KERNEL_POSITION + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +CODE_SEG equ 8 +DATA_SEG equ 16 + +BOOT_DRIVE: db 0 +ALIGN 4 +DAP: +.size db 0x10 +.reserved db 0x00 +.sectors dw 0x0001 +.offset dw KERNEL_POSITION +.segment dw 0x0000 +.lbaLsd dd 0x00000000 +.lbaMsd dd 0x00000002 + +errorByte db 0x00 + +hello db 'hello world! ', 0x00 +errorMessage db 'error! ', 0x00 + + + +padding: + times 510-($-$$) db 0x00 + dw 0xAA55 + +KERNEL_POSITION equ 0x7e00 +KERNEL_SIZE equ 1 \ No newline at end of file diff --git a/src/kernel/entry.asm b/src/kernel/entry.asm new file mode 100644 index 0000000..99ceb59 --- /dev/null +++ b/src/kernel/entry.asm @@ -0,0 +1,25 @@ +section .entry +extern kernelMain +bits 32 +_start: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/linker.ld b/linker.ld index b3c2db3..b13d161 100644 --- a/linker.ld +++ b/linker.ld @@ -1,242 +1,24 @@ -/* Script for -z combreloc -z separate-code */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -/* file generated with ld --verbose */ OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib"); -SECTIONS -{ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - _kernel_start = .; +OUTPUT_FORMAT("binary") - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; - PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - _kernel_end = .; - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +SECTIONS { + . = 0x7E00; + + .text BLOCK(4K) : ALIGN(4K) { + *(.entry) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) { + *(COMMON) + *(.bss) + } } \ No newline at end of file diff --git a/src/boot/___boot.asm b/src/boot/___boot.asm new file mode 100644 index 0000000..bccd64a --- /dev/null +++ b/src/boot/___boot.asm @@ -0,0 +1,263 @@ +org 0x7c00 +bits 16 + +%ifndef SECTOR_CNT + %define SECTOR_CNT 3 +%endif + +jmp 0x0000:mbr_start + +mbr_start: + cli + cld + + ; Clear rest of segments + xor ax, ax + mov ds, ax + mov es, ax + + mov ss, ax + mov sp, 0x7c00 + mov bp, sp + + ; Store boot device + push dx + + ; swap to unreal mode + push ds + lgdt [gdtEnd] + mov eax, cr0 + or al, 1 + mov cr0, eax + jmp .pmode +.pmode: + mov bx, 0x08 + mov ds, bx + and al, 0xFE + mov cr0, eax + pop ds + sti + + ; Clear screen + xor ax, ax + mov al, 0x03 + int 0x10 + + ; Initialize serial port + mov dx, 0x3F9 + xor al, al + out dx, al + + add dx, 2 + mov al, 0x80 + out dx, al + + sub dx, 3 + xor al, al + out dx, al + + inc dx + out dx, al + + add dx, 2 + add al, 3 + out dx, al + + dec dx + mov al, 0xC7 + out dx, al + + add dx, 2 + mov al, 0x0B + out dx, al + +load_second_stage: + pop dx + push dx + mov bx, loader_entry + xor ch, ch + mov cl, 0x02 + xor dh, dh + mov byte [.sectors_to_load], SECTOR_CNT + + .read_start: + mov di, 5 + .read: + mov ah, 0x02 + mov al, byte [.sectors_to_load] + int 0x13 + jc .retry + cmp al, byte [.sectors_to_load] + je .done + sub byte [.sectors_to_load], al + mov cl, 0x01 + xor dh, 1 + jnz .read_start + inc ch + jmp .read_start + .retry: + ; disk read failed, reset disk + xor ah, ah + int 0x13 + dec di + jnz .read + mov si, .msg_diskread_fail + jmp .fail + .done: + jmp loader_entry + .fail: + lodsb + or al, al + mov ah, 0x0E + int 0x10 + test al, al + jnz .fail + .hang: + cli + hlt + jmp .hang + +.sectors_to_load: + db 1 +.msg_diskread_fail: + db "HALT: Failed to read boot disk", 0x0A, 0x0D, 0 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +; Padding & Signature +times 510-($-$$) db 0 +dw 0xAA55 + +loader_entry equ 0x7e00 + +bits 32 +_start: + ; mov ax, 16 + ; mov ds, ax + ; mov ss, ax + ; mov es, ax + ; mov fs, ax + ; mov gs, ax + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp x +x: + mov dword [0xb8000], 0x07690748 + jmp $ + mov ebx, hello + call printString +dump: + mov ebx, 0x7e00 + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + + +old: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/boot/__boot.asm b/src/boot/__boot.asm new file mode 100644 index 0000000..6c2e1be --- /dev/null +++ b/src/boot/__boot.asm @@ -0,0 +1,278 @@ +; bootp.asm Bootstrap for protected mode image +; Version 1.0, Dec 14, 1997 +; Sample code +; by John S. Fine johnfine@erols.com +; I do not place any restrictions on your use of this source code +; I do not provide any warranty of the correctness of this source code +;_____________________________________________________________________________ +; +; This program should be useable for three purposes: +; +; 1) Use it unmodified, in the boot sector of a floppy, to load a test image +; from the hard drive. In many environments, a floppy boot is the easiest +; way to have a clean boot for a pmode test. By getting the image itself +; from the hard drive, you eliminate most of the delays and restrictions of +; a floppy boot. +; +; 2) As a bootstrap tutorial, it demonstrates many of the steps required by +; a protected mode bootstrap, and many tricks for keeping code size down +; in a bootstrap. +; +; 3) As the basis for a protected mode, partition level, bootstrap. Just +; remove some of the steps that aren't needed in your environment, and add +; some error messages for unsupported conditions. +;_____________________________________________________________________________ +; +; Documentation: +; +; 1) Most of the documentation is in comments at the END of this file +; 2) Some of the documentation is in a separate file: BOOTP.TXT +; 3) In the comments within the source code are several uses of {}, each +; of these indicates a section of documentation at the end of this file +; which applies to that part of the source code. +; 4) The major way I reduced code size was to rely on left-over values in +; registers and/or to initialize registers at points where doing so was +; cheap. To make that understandable, there are several lines beginning +; with either ";>" or ";>>". These lines describe the register contents +; at that point in the code. ";>" indicates that the value is required +; by the next section of code. ";>>" indicates that the value passes +; through the next section unmodified and is needed later. +;_____________________________________________________________________________ + + %include "gdt.inc" + + struc DISK_PARM ;BIOS hard disk parameters +DP_cyl resw 1 +DP_heads resb 1 + resb 5 ;Obsolete +DP_ctrl resb 1 + resb 5 ;Obsolete +DP_sect resb 1 + resb 1 + endstruc + + struc PT_ENT ;Partition table entry +BootFlag resb 1 +BeginHead resb 1 +BeginSector resb 1 +BeginCyl resb 1 +SystemID resb 1 +EndHead resb 1 +EndSector resb 1 +EndCyl resb 1 +RelSectorLow resw 1 +RelSectorHigh resw 1 +NumSectorsLow resw 1 +NumSectorsHigh resw 1 + endstruc + + struc BB ;Partition Boot block + resb 0xD ;Things we ignore +BB_clu resb 1 ;Sectors per cluster +BB_res resw 1 ;Reserved sectors +BB_fats resb 1 ;Number of FATs +BB_root resw 1 ;Root directory entries + resb 3 ;Things we ignore +BB_fat resw 1 ;Sectors per fat + endstruc + +max_xfer equ 0x78 ;Maximum number of sectors to transfer at once + ;Must be divisible by 8 + + +SEGMENT START USE16 + jmp short start ;Standard start of boot sector + nop + resb 0x3B ;Skip over parameters (set by format) + +start: + cli + lgdt [cs:gdt] + mov al, 0x80 + mov es, ax + + xor eax, eax ;Segment and LBN + mov ds, ax ;{1B} + mov ss, ax + mov sp, start + + xor edi, edi + inc di + call read_sectors + +;> eax = 0 +;> edi = 1 +;> ds = 0 +;> es = 80 + + mov si, 0x9BE-PT_ENT_size ;Point at partition table +.10: add si, byte PT_ENT_size + cmp [si+BootFlag], al ;Active? + je .10 ;No + jg boot_error + mov eax, [si+RelSectorLow] ;LBN sector number of partition + call read_sectors ;Read partition boot block + +;> eax = LBN of partition +;> ebx = 0000???? +;> edi = 0000???? +;> ds = 0 + + mov bp, 0x800 ;Point at partition boot block + mov bx, [bp+BB_res] ;Sectors before first FAT + add eax, ebx ;LBN of FAT + push eax ;Need to read the FAT later + movzx bx, [bp+BB_fats] ;Number of FATs + imul bx, [bp+BB_fat] ;Times size of FAT + add eax, ebx ;LBN of Root directory + mov di, [bp+BB_root] ;Root directory entries + push di ; used again later + dec di ;Convert to number of sectors + shr di, 4 ; 16 directory entries per sector + inc di + mov es, bp ;Read directory at 800:0 + call read_sectors + add eax, edi ;LBN of cluster 2 + +;>> [sp+2] = LBN of FAT +;> [sp] = Number of root directory entries +;>> eax = LBN of cluster 2 +;> edi = 0000???? +;>> bp = 800 +;> ds = 0 +;> es = 800 + + pop bx ;Root directory entries + xor di, di ;Point at directory {1C} +.20: mov si, file_name ;Name of file we want + xor ecx, ecx + mov cl, 11 + a32 rep cmpsb ;Found the file? + je found ;Yes + add cl, 21 ;Offset to next directory entry + add edi, ecx ;Advance to next entry + dec bx ;Loop through all entries + jnz .20 + + ;Couldn't find file in directory +boot_error: +disk_error: + mov ax, 0xE07 ;{3} + int 10h + jmp short $ + +;> [sp] = LBN of FAT +;> eax = LBN of cluster 2 +;> es:edi = Pointer to end of name within directory entry +;> bp = 800 +;> ds = 0 +;> es = 800 + +eof: + mov bh, 0x9C + mov es, bx ;es = 9C00 + xor di, di ;{1E} Address of page tables WRT es + mov bh, 4096/256 ;ebx = 4096 +.10: mov ch, 4 ;cx = 1024 + mov al, 7 +.20: stosd + add eax, ebx + int 8 ;{8} + loop .20 + shr eax, 2 ;{4C} (first time only) 4Mb / 4 = 1Mb + shr si, 1 ;Done just one page? + jo .10 ;Yes + + cli ;{6} + + mov eax, 0x9C007 ;First page tbl pointer in page dir + stosd ;{1H} + mov ax, (1024-3)*2 + xchg ax, cx + rep stosw + mov ax, 0xD007 ;0FF800000 page tbl pointer + stosd ;{1F} + mov ah, 0xE0 ;Page directory self pointer + stosd ;{1G} + mov al, 0 + mov CR3, eax ;Set up page directory + mov eax, CR0 ;Turn on paging and protected mode + or eax, 0x80000001 + mov CR0, eax + mov cl, flat_data ;Setup ds and es + push cx ;{5} + pop ds + mov es, cx + jmp dword 8:0xFF800000 ;Go + + read_sectors: +; Input: +; EAX = LBN +; DI = sector count +; ES = segment +; Output: +; BL = low byte of ES +; EBX high half cleared +; DL = 0x80 +; EDX high half cleared +; ESI = 0 +; Clobbered: +; BH, CX, DH + + push eax + push di + push es + +.10: push eax ;LBN + + push ds + lds si, [0x104] ;Hard drive 0 parameters + cdq ;edx = 0 + movzx ebx, byte [si+DP_sect] + div ebx ;EAX=track ;EDX=sector-1 + mov cx, dx ;CL=sector-1 ;CH=0 + inc cx ;CL=Sector number + xor dx, dx + mov bl, [si+DP_heads] + pop ds + div ebx + + mov dh, dl + mov dl, 0x80 + xchg ch, al + shr ax, 2 + or cl, al + mov al, max_xfer ;AX = Maximum sectors to xfer + cmp ax, di ;Total is larger? + jb .20 ;Yes: transfer maximum + xchg ax, di ;No: AX=total ;DI=maximum +.20: mov ah, 2 ;Read + xor bx, bx + int 13h + jc near disk_error + + mov bx, es + add bh, max_xfer>>3 ;Advance segment + mov es, bx + + pop eax + add eax, byte max_xfer + sub di, byte max_xfer + ja .10 + + pop es + pop di + pop eax + xor esi, esi + ret + +gdt start_gdt + +flat_code desc 0, 0xFFBFF, D_CODE+D_READ+D_BIG+D_BIG_LIM + +flat_data desc 0, 0xFFFFF, D_DATA+D_WRITE+D_BIG+D_BIG_LIM + + end_gdt + + resb 0x1FE+$$-$ + db 0x55, 0xAA diff --git a/src/boot/boot.asm b/src/boot/boot.asm new file mode 100644 index 0000000..19a0342 --- /dev/null +++ b/src/boot/boot.asm @@ -0,0 +1,213 @@ +BITS 16 +ORG 0x7C00 + +section .text + +loadOS: + mov [BOOT_DRIVE], dl + + xor ax, ax + mov ds, ax + mov es, ax + mov ss, ax + mov fs, ax + mov gs, ax + + mov bp, 0x9000 + mov sp, bp +resetDisk: + xor ah, ah + mov dl, [BOOT_DRIVE] + ; int 0x13 +load: + ; mov ah, 0x02 + ; mov al, 1 + ; mov cx, 0x01 + ; mov bx, CODE_SEG + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov dl, [BOOT_DRIVE] + ; mov dh, 0x00 + ; int 0x13 + ; jc error + ; jmp dump + ; mov ah, 0x42 + ; mov dl, [BOOT_DRIVE] + ; mov cx, 0x00 + ; mov ds, cx + ; mov si, diskAddrebx + ; mov eax, 0x0001 + ; mov dl, [BOOT_DRIVE] + ; mov di, KERNEL_POSITION + ; mov cx, 0x01 + +diskLoad: + ; mov ah, 0x02 ; read + ; mov al, 0x01 ; sector count + ; mov cx, 0x0002 ; cylinder & sector + ; mov dh, 0x00 ; head + ; mov dl, [BOOT_DRIVE] ; drive + ; ; xor bx, bx ; position + ; ; mov es, bx + ; xor bx, bx + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov ah, 0x42 + ; mov dx, DAP + ; mov si, DAP + ; xor dh, dh + mov dl, [BOOT_DRIVE] + mov ah, 0x02 ;Read Disk Sectors + mov al, 0x05 ;Read one sector only + mov ch, 0x00 ;Track 0 + mov cl, 0x02 ;Sector 2 + mov dh, 0x00 ;Head 0 + ; mov dl, 0x00 ;Drive 0 (Floppy 1) + ; mov bx, 0x800 ;Segment 0x2000 + ; mov es, bx + mov bx, KERNEL_POSITION ;Start of Segment + + int 0x13 + + mov [errorByte], ah + +switchToX86: + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp CODE_SEG:_start + +bits 32 +_start: + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov al, [BOOT_DRIVE] + call print_hex_byte + mov ebx, hello + call printString + mov al, [errorByte] + call print_hex_byte +dump: + mov ebx, KERNEL_POSITION + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +CODE_SEG equ 8 +DATA_SEG equ 16 + +BOOT_DRIVE: db 0 +ALIGN 4 +DAP: +.size db 0x10 +.reserved db 0x00 +.sectors dw 0x0001 +.offset dw KERNEL_POSITION +.segment dw 0x0000 +.lbaLsd dd 0x00000000 +.lbaMsd dd 0x00000002 + +errorByte db 0x00 + +hello db 'hello world! ', 0x00 +errorMessage db 'error! ', 0x00 + + + +padding: + times 510-($-$$) db 0x00 + dw 0xAA55 + +KERNEL_POSITION equ 0x7e00 +KERNEL_SIZE equ 1 \ No newline at end of file diff --git a/src/kernel/entry.asm b/src/kernel/entry.asm new file mode 100644 index 0000000..99ceb59 --- /dev/null +++ b/src/kernel/entry.asm @@ -0,0 +1,25 @@ +section .entry +extern kernelMain +bits 32 +_start: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index cfdf333..21c60b4 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -9,11 +9,9 @@ extern uint32_t _kernel_end; void kernelMain() { + puts("hello world"); initMemoryAllocation(0x100000); // initializing stacks after the kernel seems not to work :( otherwise, _kernel_end should be passed here initOSTasks(); drawLogo(); - printf("Switching to otherTask... \n"); - yields(); - printf("Returned to mainTask!\n\n"); yield(); } \ No newline at end of file diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/linker.ld b/linker.ld index b3c2db3..b13d161 100644 --- a/linker.ld +++ b/linker.ld @@ -1,242 +1,24 @@ -/* Script for -z combreloc -z separate-code */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -/* file generated with ld --verbose */ OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib"); -SECTIONS -{ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - _kernel_start = .; +OUTPUT_FORMAT("binary") - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; - PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - _kernel_end = .; - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +SECTIONS { + . = 0x7E00; + + .text BLOCK(4K) : ALIGN(4K) { + *(.entry) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) { + *(COMMON) + *(.bss) + } } \ No newline at end of file diff --git a/src/boot/___boot.asm b/src/boot/___boot.asm new file mode 100644 index 0000000..bccd64a --- /dev/null +++ b/src/boot/___boot.asm @@ -0,0 +1,263 @@ +org 0x7c00 +bits 16 + +%ifndef SECTOR_CNT + %define SECTOR_CNT 3 +%endif + +jmp 0x0000:mbr_start + +mbr_start: + cli + cld + + ; Clear rest of segments + xor ax, ax + mov ds, ax + mov es, ax + + mov ss, ax + mov sp, 0x7c00 + mov bp, sp + + ; Store boot device + push dx + + ; swap to unreal mode + push ds + lgdt [gdtEnd] + mov eax, cr0 + or al, 1 + mov cr0, eax + jmp .pmode +.pmode: + mov bx, 0x08 + mov ds, bx + and al, 0xFE + mov cr0, eax + pop ds + sti + + ; Clear screen + xor ax, ax + mov al, 0x03 + int 0x10 + + ; Initialize serial port + mov dx, 0x3F9 + xor al, al + out dx, al + + add dx, 2 + mov al, 0x80 + out dx, al + + sub dx, 3 + xor al, al + out dx, al + + inc dx + out dx, al + + add dx, 2 + add al, 3 + out dx, al + + dec dx + mov al, 0xC7 + out dx, al + + add dx, 2 + mov al, 0x0B + out dx, al + +load_second_stage: + pop dx + push dx + mov bx, loader_entry + xor ch, ch + mov cl, 0x02 + xor dh, dh + mov byte [.sectors_to_load], SECTOR_CNT + + .read_start: + mov di, 5 + .read: + mov ah, 0x02 + mov al, byte [.sectors_to_load] + int 0x13 + jc .retry + cmp al, byte [.sectors_to_load] + je .done + sub byte [.sectors_to_load], al + mov cl, 0x01 + xor dh, 1 + jnz .read_start + inc ch + jmp .read_start + .retry: + ; disk read failed, reset disk + xor ah, ah + int 0x13 + dec di + jnz .read + mov si, .msg_diskread_fail + jmp .fail + .done: + jmp loader_entry + .fail: + lodsb + or al, al + mov ah, 0x0E + int 0x10 + test al, al + jnz .fail + .hang: + cli + hlt + jmp .hang + +.sectors_to_load: + db 1 +.msg_diskread_fail: + db "HALT: Failed to read boot disk", 0x0A, 0x0D, 0 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +; Padding & Signature +times 510-($-$$) db 0 +dw 0xAA55 + +loader_entry equ 0x7e00 + +bits 32 +_start: + ; mov ax, 16 + ; mov ds, ax + ; mov ss, ax + ; mov es, ax + ; mov fs, ax + ; mov gs, ax + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp x +x: + mov dword [0xb8000], 0x07690748 + jmp $ + mov ebx, hello + call printString +dump: + mov ebx, 0x7e00 + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + + +old: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/boot/__boot.asm b/src/boot/__boot.asm new file mode 100644 index 0000000..6c2e1be --- /dev/null +++ b/src/boot/__boot.asm @@ -0,0 +1,278 @@ +; bootp.asm Bootstrap for protected mode image +; Version 1.0, Dec 14, 1997 +; Sample code +; by John S. Fine johnfine@erols.com +; I do not place any restrictions on your use of this source code +; I do not provide any warranty of the correctness of this source code +;_____________________________________________________________________________ +; +; This program should be useable for three purposes: +; +; 1) Use it unmodified, in the boot sector of a floppy, to load a test image +; from the hard drive. In many environments, a floppy boot is the easiest +; way to have a clean boot for a pmode test. By getting the image itself +; from the hard drive, you eliminate most of the delays and restrictions of +; a floppy boot. +; +; 2) As a bootstrap tutorial, it demonstrates many of the steps required by +; a protected mode bootstrap, and many tricks for keeping code size down +; in a bootstrap. +; +; 3) As the basis for a protected mode, partition level, bootstrap. Just +; remove some of the steps that aren't needed in your environment, and add +; some error messages for unsupported conditions. +;_____________________________________________________________________________ +; +; Documentation: +; +; 1) Most of the documentation is in comments at the END of this file +; 2) Some of the documentation is in a separate file: BOOTP.TXT +; 3) In the comments within the source code are several uses of {}, each +; of these indicates a section of documentation at the end of this file +; which applies to that part of the source code. +; 4) The major way I reduced code size was to rely on left-over values in +; registers and/or to initialize registers at points where doing so was +; cheap. To make that understandable, there are several lines beginning +; with either ";>" or ";>>". These lines describe the register contents +; at that point in the code. ";>" indicates that the value is required +; by the next section of code. ";>>" indicates that the value passes +; through the next section unmodified and is needed later. +;_____________________________________________________________________________ + + %include "gdt.inc" + + struc DISK_PARM ;BIOS hard disk parameters +DP_cyl resw 1 +DP_heads resb 1 + resb 5 ;Obsolete +DP_ctrl resb 1 + resb 5 ;Obsolete +DP_sect resb 1 + resb 1 + endstruc + + struc PT_ENT ;Partition table entry +BootFlag resb 1 +BeginHead resb 1 +BeginSector resb 1 +BeginCyl resb 1 +SystemID resb 1 +EndHead resb 1 +EndSector resb 1 +EndCyl resb 1 +RelSectorLow resw 1 +RelSectorHigh resw 1 +NumSectorsLow resw 1 +NumSectorsHigh resw 1 + endstruc + + struc BB ;Partition Boot block + resb 0xD ;Things we ignore +BB_clu resb 1 ;Sectors per cluster +BB_res resw 1 ;Reserved sectors +BB_fats resb 1 ;Number of FATs +BB_root resw 1 ;Root directory entries + resb 3 ;Things we ignore +BB_fat resw 1 ;Sectors per fat + endstruc + +max_xfer equ 0x78 ;Maximum number of sectors to transfer at once + ;Must be divisible by 8 + + +SEGMENT START USE16 + jmp short start ;Standard start of boot sector + nop + resb 0x3B ;Skip over parameters (set by format) + +start: + cli + lgdt [cs:gdt] + mov al, 0x80 + mov es, ax + + xor eax, eax ;Segment and LBN + mov ds, ax ;{1B} + mov ss, ax + mov sp, start + + xor edi, edi + inc di + call read_sectors + +;> eax = 0 +;> edi = 1 +;> ds = 0 +;> es = 80 + + mov si, 0x9BE-PT_ENT_size ;Point at partition table +.10: add si, byte PT_ENT_size + cmp [si+BootFlag], al ;Active? + je .10 ;No + jg boot_error + mov eax, [si+RelSectorLow] ;LBN sector number of partition + call read_sectors ;Read partition boot block + +;> eax = LBN of partition +;> ebx = 0000???? +;> edi = 0000???? +;> ds = 0 + + mov bp, 0x800 ;Point at partition boot block + mov bx, [bp+BB_res] ;Sectors before first FAT + add eax, ebx ;LBN of FAT + push eax ;Need to read the FAT later + movzx bx, [bp+BB_fats] ;Number of FATs + imul bx, [bp+BB_fat] ;Times size of FAT + add eax, ebx ;LBN of Root directory + mov di, [bp+BB_root] ;Root directory entries + push di ; used again later + dec di ;Convert to number of sectors + shr di, 4 ; 16 directory entries per sector + inc di + mov es, bp ;Read directory at 800:0 + call read_sectors + add eax, edi ;LBN of cluster 2 + +;>> [sp+2] = LBN of FAT +;> [sp] = Number of root directory entries +;>> eax = LBN of cluster 2 +;> edi = 0000???? +;>> bp = 800 +;> ds = 0 +;> es = 800 + + pop bx ;Root directory entries + xor di, di ;Point at directory {1C} +.20: mov si, file_name ;Name of file we want + xor ecx, ecx + mov cl, 11 + a32 rep cmpsb ;Found the file? + je found ;Yes + add cl, 21 ;Offset to next directory entry + add edi, ecx ;Advance to next entry + dec bx ;Loop through all entries + jnz .20 + + ;Couldn't find file in directory +boot_error: +disk_error: + mov ax, 0xE07 ;{3} + int 10h + jmp short $ + +;> [sp] = LBN of FAT +;> eax = LBN of cluster 2 +;> es:edi = Pointer to end of name within directory entry +;> bp = 800 +;> ds = 0 +;> es = 800 + +eof: + mov bh, 0x9C + mov es, bx ;es = 9C00 + xor di, di ;{1E} Address of page tables WRT es + mov bh, 4096/256 ;ebx = 4096 +.10: mov ch, 4 ;cx = 1024 + mov al, 7 +.20: stosd + add eax, ebx + int 8 ;{8} + loop .20 + shr eax, 2 ;{4C} (first time only) 4Mb / 4 = 1Mb + shr si, 1 ;Done just one page? + jo .10 ;Yes + + cli ;{6} + + mov eax, 0x9C007 ;First page tbl pointer in page dir + stosd ;{1H} + mov ax, (1024-3)*2 + xchg ax, cx + rep stosw + mov ax, 0xD007 ;0FF800000 page tbl pointer + stosd ;{1F} + mov ah, 0xE0 ;Page directory self pointer + stosd ;{1G} + mov al, 0 + mov CR3, eax ;Set up page directory + mov eax, CR0 ;Turn on paging and protected mode + or eax, 0x80000001 + mov CR0, eax + mov cl, flat_data ;Setup ds and es + push cx ;{5} + pop ds + mov es, cx + jmp dword 8:0xFF800000 ;Go + + read_sectors: +; Input: +; EAX = LBN +; DI = sector count +; ES = segment +; Output: +; BL = low byte of ES +; EBX high half cleared +; DL = 0x80 +; EDX high half cleared +; ESI = 0 +; Clobbered: +; BH, CX, DH + + push eax + push di + push es + +.10: push eax ;LBN + + push ds + lds si, [0x104] ;Hard drive 0 parameters + cdq ;edx = 0 + movzx ebx, byte [si+DP_sect] + div ebx ;EAX=track ;EDX=sector-1 + mov cx, dx ;CL=sector-1 ;CH=0 + inc cx ;CL=Sector number + xor dx, dx + mov bl, [si+DP_heads] + pop ds + div ebx + + mov dh, dl + mov dl, 0x80 + xchg ch, al + shr ax, 2 + or cl, al + mov al, max_xfer ;AX = Maximum sectors to xfer + cmp ax, di ;Total is larger? + jb .20 ;Yes: transfer maximum + xchg ax, di ;No: AX=total ;DI=maximum +.20: mov ah, 2 ;Read + xor bx, bx + int 13h + jc near disk_error + + mov bx, es + add bh, max_xfer>>3 ;Advance segment + mov es, bx + + pop eax + add eax, byte max_xfer + sub di, byte max_xfer + ja .10 + + pop es + pop di + pop eax + xor esi, esi + ret + +gdt start_gdt + +flat_code desc 0, 0xFFBFF, D_CODE+D_READ+D_BIG+D_BIG_LIM + +flat_data desc 0, 0xFFFFF, D_DATA+D_WRITE+D_BIG+D_BIG_LIM + + end_gdt + + resb 0x1FE+$$-$ + db 0x55, 0xAA diff --git a/src/boot/boot.asm b/src/boot/boot.asm new file mode 100644 index 0000000..19a0342 --- /dev/null +++ b/src/boot/boot.asm @@ -0,0 +1,213 @@ +BITS 16 +ORG 0x7C00 + +section .text + +loadOS: + mov [BOOT_DRIVE], dl + + xor ax, ax + mov ds, ax + mov es, ax + mov ss, ax + mov fs, ax + mov gs, ax + + mov bp, 0x9000 + mov sp, bp +resetDisk: + xor ah, ah + mov dl, [BOOT_DRIVE] + ; int 0x13 +load: + ; mov ah, 0x02 + ; mov al, 1 + ; mov cx, 0x01 + ; mov bx, CODE_SEG + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov dl, [BOOT_DRIVE] + ; mov dh, 0x00 + ; int 0x13 + ; jc error + ; jmp dump + ; mov ah, 0x42 + ; mov dl, [BOOT_DRIVE] + ; mov cx, 0x00 + ; mov ds, cx + ; mov si, diskAddrebx + ; mov eax, 0x0001 + ; mov dl, [BOOT_DRIVE] + ; mov di, KERNEL_POSITION + ; mov cx, 0x01 + +diskLoad: + ; mov ah, 0x02 ; read + ; mov al, 0x01 ; sector count + ; mov cx, 0x0002 ; cylinder & sector + ; mov dh, 0x00 ; head + ; mov dl, [BOOT_DRIVE] ; drive + ; ; xor bx, bx ; position + ; ; mov es, bx + ; xor bx, bx + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov ah, 0x42 + ; mov dx, DAP + ; mov si, DAP + ; xor dh, dh + mov dl, [BOOT_DRIVE] + mov ah, 0x02 ;Read Disk Sectors + mov al, 0x05 ;Read one sector only + mov ch, 0x00 ;Track 0 + mov cl, 0x02 ;Sector 2 + mov dh, 0x00 ;Head 0 + ; mov dl, 0x00 ;Drive 0 (Floppy 1) + ; mov bx, 0x800 ;Segment 0x2000 + ; mov es, bx + mov bx, KERNEL_POSITION ;Start of Segment + + int 0x13 + + mov [errorByte], ah + +switchToX86: + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp CODE_SEG:_start + +bits 32 +_start: + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov al, [BOOT_DRIVE] + call print_hex_byte + mov ebx, hello + call printString + mov al, [errorByte] + call print_hex_byte +dump: + mov ebx, KERNEL_POSITION + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +CODE_SEG equ 8 +DATA_SEG equ 16 + +BOOT_DRIVE: db 0 +ALIGN 4 +DAP: +.size db 0x10 +.reserved db 0x00 +.sectors dw 0x0001 +.offset dw KERNEL_POSITION +.segment dw 0x0000 +.lbaLsd dd 0x00000000 +.lbaMsd dd 0x00000002 + +errorByte db 0x00 + +hello db 'hello world! ', 0x00 +errorMessage db 'error! ', 0x00 + + + +padding: + times 510-($-$$) db 0x00 + dw 0xAA55 + +KERNEL_POSITION equ 0x7e00 +KERNEL_SIZE equ 1 \ No newline at end of file diff --git a/src/kernel/entry.asm b/src/kernel/entry.asm new file mode 100644 index 0000000..99ceb59 --- /dev/null +++ b/src/kernel/entry.asm @@ -0,0 +1,25 @@ +section .entry +extern kernelMain +bits 32 +_start: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index cfdf333..21c60b4 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -9,11 +9,9 @@ extern uint32_t _kernel_end; void kernelMain() { + puts("hello world"); initMemoryAllocation(0x100000); // initializing stacks after the kernel seems not to work :( otherwise, _kernel_end should be passed here initOSTasks(); drawLogo(); - printf("Switching to otherTask... \n"); - yields(); - printf("Returned to mainTask!\n\n"); yield(); } \ No newline at end of file diff --git a/src/kernel/lib/task/task.c b/src/kernel/lib/task/task.c index 5d57403..6163772 100644 --- a/src/kernel/lib/task/task.c +++ b/src/kernel/lib/task/task.c @@ -29,6 +29,7 @@ puts("No more tasks to run, halting . . .\n"); while (1) { asm("hlt"); + puts("interrupt!\n"); } } last->nextTask = 0x00; diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/linker.ld b/linker.ld index b3c2db3..b13d161 100644 --- a/linker.ld +++ b/linker.ld @@ -1,242 +1,24 @@ -/* Script for -z combreloc -z separate-code */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -/* file generated with ld --verbose */ OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib"); -SECTIONS -{ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - _kernel_start = .; +OUTPUT_FORMAT("binary") - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; - PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - _kernel_end = .; - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +SECTIONS { + . = 0x7E00; + + .text BLOCK(4K) : ALIGN(4K) { + *(.entry) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) { + *(COMMON) + *(.bss) + } } \ No newline at end of file diff --git a/src/boot/___boot.asm b/src/boot/___boot.asm new file mode 100644 index 0000000..bccd64a --- /dev/null +++ b/src/boot/___boot.asm @@ -0,0 +1,263 @@ +org 0x7c00 +bits 16 + +%ifndef SECTOR_CNT + %define SECTOR_CNT 3 +%endif + +jmp 0x0000:mbr_start + +mbr_start: + cli + cld + + ; Clear rest of segments + xor ax, ax + mov ds, ax + mov es, ax + + mov ss, ax + mov sp, 0x7c00 + mov bp, sp + + ; Store boot device + push dx + + ; swap to unreal mode + push ds + lgdt [gdtEnd] + mov eax, cr0 + or al, 1 + mov cr0, eax + jmp .pmode +.pmode: + mov bx, 0x08 + mov ds, bx + and al, 0xFE + mov cr0, eax + pop ds + sti + + ; Clear screen + xor ax, ax + mov al, 0x03 + int 0x10 + + ; Initialize serial port + mov dx, 0x3F9 + xor al, al + out dx, al + + add dx, 2 + mov al, 0x80 + out dx, al + + sub dx, 3 + xor al, al + out dx, al + + inc dx + out dx, al + + add dx, 2 + add al, 3 + out dx, al + + dec dx + mov al, 0xC7 + out dx, al + + add dx, 2 + mov al, 0x0B + out dx, al + +load_second_stage: + pop dx + push dx + mov bx, loader_entry + xor ch, ch + mov cl, 0x02 + xor dh, dh + mov byte [.sectors_to_load], SECTOR_CNT + + .read_start: + mov di, 5 + .read: + mov ah, 0x02 + mov al, byte [.sectors_to_load] + int 0x13 + jc .retry + cmp al, byte [.sectors_to_load] + je .done + sub byte [.sectors_to_load], al + mov cl, 0x01 + xor dh, 1 + jnz .read_start + inc ch + jmp .read_start + .retry: + ; disk read failed, reset disk + xor ah, ah + int 0x13 + dec di + jnz .read + mov si, .msg_diskread_fail + jmp .fail + .done: + jmp loader_entry + .fail: + lodsb + or al, al + mov ah, 0x0E + int 0x10 + test al, al + jnz .fail + .hang: + cli + hlt + jmp .hang + +.sectors_to_load: + db 1 +.msg_diskread_fail: + db "HALT: Failed to read boot disk", 0x0A, 0x0D, 0 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +; Padding & Signature +times 510-($-$$) db 0 +dw 0xAA55 + +loader_entry equ 0x7e00 + +bits 32 +_start: + ; mov ax, 16 + ; mov ds, ax + ; mov ss, ax + ; mov es, ax + ; mov fs, ax + ; mov gs, ax + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp x +x: + mov dword [0xb8000], 0x07690748 + jmp $ + mov ebx, hello + call printString +dump: + mov ebx, 0x7e00 + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + + +old: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/boot/__boot.asm b/src/boot/__boot.asm new file mode 100644 index 0000000..6c2e1be --- /dev/null +++ b/src/boot/__boot.asm @@ -0,0 +1,278 @@ +; bootp.asm Bootstrap for protected mode image +; Version 1.0, Dec 14, 1997 +; Sample code +; by John S. Fine johnfine@erols.com +; I do not place any restrictions on your use of this source code +; I do not provide any warranty of the correctness of this source code +;_____________________________________________________________________________ +; +; This program should be useable for three purposes: +; +; 1) Use it unmodified, in the boot sector of a floppy, to load a test image +; from the hard drive. In many environments, a floppy boot is the easiest +; way to have a clean boot for a pmode test. By getting the image itself +; from the hard drive, you eliminate most of the delays and restrictions of +; a floppy boot. +; +; 2) As a bootstrap tutorial, it demonstrates many of the steps required by +; a protected mode bootstrap, and many tricks for keeping code size down +; in a bootstrap. +; +; 3) As the basis for a protected mode, partition level, bootstrap. Just +; remove some of the steps that aren't needed in your environment, and add +; some error messages for unsupported conditions. +;_____________________________________________________________________________ +; +; Documentation: +; +; 1) Most of the documentation is in comments at the END of this file +; 2) Some of the documentation is in a separate file: BOOTP.TXT +; 3) In the comments within the source code are several uses of {}, each +; of these indicates a section of documentation at the end of this file +; which applies to that part of the source code. +; 4) The major way I reduced code size was to rely on left-over values in +; registers and/or to initialize registers at points where doing so was +; cheap. To make that understandable, there are several lines beginning +; with either ";>" or ";>>". These lines describe the register contents +; at that point in the code. ";>" indicates that the value is required +; by the next section of code. ";>>" indicates that the value passes +; through the next section unmodified and is needed later. +;_____________________________________________________________________________ + + %include "gdt.inc" + + struc DISK_PARM ;BIOS hard disk parameters +DP_cyl resw 1 +DP_heads resb 1 + resb 5 ;Obsolete +DP_ctrl resb 1 + resb 5 ;Obsolete +DP_sect resb 1 + resb 1 + endstruc + + struc PT_ENT ;Partition table entry +BootFlag resb 1 +BeginHead resb 1 +BeginSector resb 1 +BeginCyl resb 1 +SystemID resb 1 +EndHead resb 1 +EndSector resb 1 +EndCyl resb 1 +RelSectorLow resw 1 +RelSectorHigh resw 1 +NumSectorsLow resw 1 +NumSectorsHigh resw 1 + endstruc + + struc BB ;Partition Boot block + resb 0xD ;Things we ignore +BB_clu resb 1 ;Sectors per cluster +BB_res resw 1 ;Reserved sectors +BB_fats resb 1 ;Number of FATs +BB_root resw 1 ;Root directory entries + resb 3 ;Things we ignore +BB_fat resw 1 ;Sectors per fat + endstruc + +max_xfer equ 0x78 ;Maximum number of sectors to transfer at once + ;Must be divisible by 8 + + +SEGMENT START USE16 + jmp short start ;Standard start of boot sector + nop + resb 0x3B ;Skip over parameters (set by format) + +start: + cli + lgdt [cs:gdt] + mov al, 0x80 + mov es, ax + + xor eax, eax ;Segment and LBN + mov ds, ax ;{1B} + mov ss, ax + mov sp, start + + xor edi, edi + inc di + call read_sectors + +;> eax = 0 +;> edi = 1 +;> ds = 0 +;> es = 80 + + mov si, 0x9BE-PT_ENT_size ;Point at partition table +.10: add si, byte PT_ENT_size + cmp [si+BootFlag], al ;Active? + je .10 ;No + jg boot_error + mov eax, [si+RelSectorLow] ;LBN sector number of partition + call read_sectors ;Read partition boot block + +;> eax = LBN of partition +;> ebx = 0000???? +;> edi = 0000???? +;> ds = 0 + + mov bp, 0x800 ;Point at partition boot block + mov bx, [bp+BB_res] ;Sectors before first FAT + add eax, ebx ;LBN of FAT + push eax ;Need to read the FAT later + movzx bx, [bp+BB_fats] ;Number of FATs + imul bx, [bp+BB_fat] ;Times size of FAT + add eax, ebx ;LBN of Root directory + mov di, [bp+BB_root] ;Root directory entries + push di ; used again later + dec di ;Convert to number of sectors + shr di, 4 ; 16 directory entries per sector + inc di + mov es, bp ;Read directory at 800:0 + call read_sectors + add eax, edi ;LBN of cluster 2 + +;>> [sp+2] = LBN of FAT +;> [sp] = Number of root directory entries +;>> eax = LBN of cluster 2 +;> edi = 0000???? +;>> bp = 800 +;> ds = 0 +;> es = 800 + + pop bx ;Root directory entries + xor di, di ;Point at directory {1C} +.20: mov si, file_name ;Name of file we want + xor ecx, ecx + mov cl, 11 + a32 rep cmpsb ;Found the file? + je found ;Yes + add cl, 21 ;Offset to next directory entry + add edi, ecx ;Advance to next entry + dec bx ;Loop through all entries + jnz .20 + + ;Couldn't find file in directory +boot_error: +disk_error: + mov ax, 0xE07 ;{3} + int 10h + jmp short $ + +;> [sp] = LBN of FAT +;> eax = LBN of cluster 2 +;> es:edi = Pointer to end of name within directory entry +;> bp = 800 +;> ds = 0 +;> es = 800 + +eof: + mov bh, 0x9C + mov es, bx ;es = 9C00 + xor di, di ;{1E} Address of page tables WRT es + mov bh, 4096/256 ;ebx = 4096 +.10: mov ch, 4 ;cx = 1024 + mov al, 7 +.20: stosd + add eax, ebx + int 8 ;{8} + loop .20 + shr eax, 2 ;{4C} (first time only) 4Mb / 4 = 1Mb + shr si, 1 ;Done just one page? + jo .10 ;Yes + + cli ;{6} + + mov eax, 0x9C007 ;First page tbl pointer in page dir + stosd ;{1H} + mov ax, (1024-3)*2 + xchg ax, cx + rep stosw + mov ax, 0xD007 ;0FF800000 page tbl pointer + stosd ;{1F} + mov ah, 0xE0 ;Page directory self pointer + stosd ;{1G} + mov al, 0 + mov CR3, eax ;Set up page directory + mov eax, CR0 ;Turn on paging and protected mode + or eax, 0x80000001 + mov CR0, eax + mov cl, flat_data ;Setup ds and es + push cx ;{5} + pop ds + mov es, cx + jmp dword 8:0xFF800000 ;Go + + read_sectors: +; Input: +; EAX = LBN +; DI = sector count +; ES = segment +; Output: +; BL = low byte of ES +; EBX high half cleared +; DL = 0x80 +; EDX high half cleared +; ESI = 0 +; Clobbered: +; BH, CX, DH + + push eax + push di + push es + +.10: push eax ;LBN + + push ds + lds si, [0x104] ;Hard drive 0 parameters + cdq ;edx = 0 + movzx ebx, byte [si+DP_sect] + div ebx ;EAX=track ;EDX=sector-1 + mov cx, dx ;CL=sector-1 ;CH=0 + inc cx ;CL=Sector number + xor dx, dx + mov bl, [si+DP_heads] + pop ds + div ebx + + mov dh, dl + mov dl, 0x80 + xchg ch, al + shr ax, 2 + or cl, al + mov al, max_xfer ;AX = Maximum sectors to xfer + cmp ax, di ;Total is larger? + jb .20 ;Yes: transfer maximum + xchg ax, di ;No: AX=total ;DI=maximum +.20: mov ah, 2 ;Read + xor bx, bx + int 13h + jc near disk_error + + mov bx, es + add bh, max_xfer>>3 ;Advance segment + mov es, bx + + pop eax + add eax, byte max_xfer + sub di, byte max_xfer + ja .10 + + pop es + pop di + pop eax + xor esi, esi + ret + +gdt start_gdt + +flat_code desc 0, 0xFFBFF, D_CODE+D_READ+D_BIG+D_BIG_LIM + +flat_data desc 0, 0xFFFFF, D_DATA+D_WRITE+D_BIG+D_BIG_LIM + + end_gdt + + resb 0x1FE+$$-$ + db 0x55, 0xAA diff --git a/src/boot/boot.asm b/src/boot/boot.asm new file mode 100644 index 0000000..19a0342 --- /dev/null +++ b/src/boot/boot.asm @@ -0,0 +1,213 @@ +BITS 16 +ORG 0x7C00 + +section .text + +loadOS: + mov [BOOT_DRIVE], dl + + xor ax, ax + mov ds, ax + mov es, ax + mov ss, ax + mov fs, ax + mov gs, ax + + mov bp, 0x9000 + mov sp, bp +resetDisk: + xor ah, ah + mov dl, [BOOT_DRIVE] + ; int 0x13 +load: + ; mov ah, 0x02 + ; mov al, 1 + ; mov cx, 0x01 + ; mov bx, CODE_SEG + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov dl, [BOOT_DRIVE] + ; mov dh, 0x00 + ; int 0x13 + ; jc error + ; jmp dump + ; mov ah, 0x42 + ; mov dl, [BOOT_DRIVE] + ; mov cx, 0x00 + ; mov ds, cx + ; mov si, diskAddrebx + ; mov eax, 0x0001 + ; mov dl, [BOOT_DRIVE] + ; mov di, KERNEL_POSITION + ; mov cx, 0x01 + +diskLoad: + ; mov ah, 0x02 ; read + ; mov al, 0x01 ; sector count + ; mov cx, 0x0002 ; cylinder & sector + ; mov dh, 0x00 ; head + ; mov dl, [BOOT_DRIVE] ; drive + ; ; xor bx, bx ; position + ; ; mov es, bx + ; xor bx, bx + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov ah, 0x42 + ; mov dx, DAP + ; mov si, DAP + ; xor dh, dh + mov dl, [BOOT_DRIVE] + mov ah, 0x02 ;Read Disk Sectors + mov al, 0x05 ;Read one sector only + mov ch, 0x00 ;Track 0 + mov cl, 0x02 ;Sector 2 + mov dh, 0x00 ;Head 0 + ; mov dl, 0x00 ;Drive 0 (Floppy 1) + ; mov bx, 0x800 ;Segment 0x2000 + ; mov es, bx + mov bx, KERNEL_POSITION ;Start of Segment + + int 0x13 + + mov [errorByte], ah + +switchToX86: + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp CODE_SEG:_start + +bits 32 +_start: + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov al, [BOOT_DRIVE] + call print_hex_byte + mov ebx, hello + call printString + mov al, [errorByte] + call print_hex_byte +dump: + mov ebx, KERNEL_POSITION + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +CODE_SEG equ 8 +DATA_SEG equ 16 + +BOOT_DRIVE: db 0 +ALIGN 4 +DAP: +.size db 0x10 +.reserved db 0x00 +.sectors dw 0x0001 +.offset dw KERNEL_POSITION +.segment dw 0x0000 +.lbaLsd dd 0x00000000 +.lbaMsd dd 0x00000002 + +errorByte db 0x00 + +hello db 'hello world! ', 0x00 +errorMessage db 'error! ', 0x00 + + + +padding: + times 510-($-$$) db 0x00 + dw 0xAA55 + +KERNEL_POSITION equ 0x7e00 +KERNEL_SIZE equ 1 \ No newline at end of file diff --git a/src/kernel/entry.asm b/src/kernel/entry.asm new file mode 100644 index 0000000..99ceb59 --- /dev/null +++ b/src/kernel/entry.asm @@ -0,0 +1,25 @@ +section .entry +extern kernelMain +bits 32 +_start: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index cfdf333..21c60b4 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -9,11 +9,9 @@ extern uint32_t _kernel_end; void kernelMain() { + puts("hello world"); initMemoryAllocation(0x100000); // initializing stacks after the kernel seems not to work :( otherwise, _kernel_end should be passed here initOSTasks(); drawLogo(); - printf("Switching to otherTask... \n"); - yields(); - printf("Returned to mainTask!\n\n"); yield(); } \ No newline at end of file diff --git a/src/kernel/lib/task/task.c b/src/kernel/lib/task/task.c index 5d57403..6163772 100644 --- a/src/kernel/lib/task/task.c +++ b/src/kernel/lib/task/task.c @@ -29,6 +29,7 @@ puts("No more tasks to run, halting . . .\n"); while (1) { asm("hlt"); + puts("interrupt!\n"); } } last->nextTask = 0x00; diff --git a/src/kernel/multiboot.h b/src/kernel/multiboot.h deleted file mode 100644 index c6f5ff8..0000000 --- a/src/kernel/multiboot.h +++ /dev/null @@ -1,274 +0,0 @@ -/* multiboot.h - Multiboot header file. */ -/* Copyright (C) 1999,2003,2007,2008,2009,2010 Free Software Foundation, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY - * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR - * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef MULTIBOOT_HEADER -#define MULTIBOOT_HEADER 1 - -/* How many bytes from the start of the file we search for the header. */ -#define MULTIBOOT_SEARCH 8192 -#define MULTIBOOT_HEADER_ALIGN 4 - -/* The magic field should contain this. */ -#define MULTIBOOT_HEADER_MAGIC 0x1BADB002 - -/* This should be in %eax. */ -#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002 - -/* Alignment of multiboot modules. */ -#define MULTIBOOT_MOD_ALIGN 0x00001000 - -/* Alignment of the multiboot info structure. */ -#define MULTIBOOT_INFO_ALIGN 0x00000004 - -/* Flags set in the ’flags’ member of the multiboot header. */ - -/* Align all boot modules on i386 page (4KB) boundaries. */ -#define MULTIBOOT_PAGE_ALIGN 0x00000001 - -/* Must pass memory information to OS. */ -#define MULTIBOOT_MEMORY_INFO 0x00000002 - -/* Must pass video information to OS. */ -#define MULTIBOOT_VIDEO_MODE 0x00000004 - -/* This flag indicates the use of the address fields in the header. */ -#define MULTIBOOT_AOUT_KLUDGE 0x00010000 - -/* Flags to be set in the ’flags’ member of the multiboot info structure. */ - -/* is there basic lower/upper memory information? */ -#define MULTIBOOT_INFO_MEMORY 0x00000001 -/* is there a boot device set? */ -#define MULTIBOOT_INFO_BOOTDEV 0x00000002 -/* is the command-line defined? */ -#define MULTIBOOT_INFO_CMDLINE 0x00000004 -/* are there modules to do something with? */ -#define MULTIBOOT_INFO_MODS 0x00000008 - -/* These next two are mutually exclusive */ - -/* is there a symbol table loaded? */ -#define MULTIBOOT_INFO_AOUT_SYMS 0x00000010 -/* is there an ELF section header table? */ -#define MULTIBOOT_INFO_ELF_SHDR 0X00000020 - -/* is there a full memory map? */ -#define MULTIBOOT_INFO_MEM_MAP 0x00000040 - -/* Is there drive info? */ -#define MULTIBOOT_INFO_DRIVE_INFO 0x00000080 - -/* Is there a config table? */ -#define MULTIBOOT_INFO_CONFIG_TABLE 0x00000100 - -/* Is there a boot loader name? */ -#define MULTIBOOT_INFO_BOOT_LOADER_NAME 0x00000200 - -/* Is there a APM table? */ -#define MULTIBOOT_INFO_APM_TABLE 0x00000400 - -/* Is there video information? */ -#define MULTIBOOT_INFO_VBE_INFO 0x00000800 -#define MULTIBOOT_INFO_FRAMEBUFFER_INFO 0x00001000 - -#ifndef ASM_FILE - -typedef unsigned char multiboot_uint8_t; -typedef unsigned short multiboot_uint16_t; -typedef unsigned int multiboot_uint32_t; -typedef unsigned long long multiboot_uint64_t; - -struct multiboot_header -{ - /* Must be MULTIBOOT_MAGIC - see above. */ - multiboot_uint32_t magic; - - /* Feature flags. */ - multiboot_uint32_t flags; - - /* The above fields plus this one must equal 0 mod 2^32. */ - multiboot_uint32_t checksum; - - /* These are only valid if MULTIBOOT_AOUT_KLUDGE is set. */ - multiboot_uint32_t header_addr; - multiboot_uint32_t load_addr; - multiboot_uint32_t load_end_addr; - multiboot_uint32_t bss_end_addr; - multiboot_uint32_t entry_addr; - - /* These are only valid if MULTIBOOT_VIDEO_MODE is set. */ - multiboot_uint32_t mode_type; - multiboot_uint32_t width; - multiboot_uint32_t height; - multiboot_uint32_t depth; -}; - -/* The symbol table for a.out. */ -struct multiboot_aout_symbol_table -{ - multiboot_uint32_t tabsize; - multiboot_uint32_t strsize; - multiboot_uint32_t addr; - multiboot_uint32_t reserved; -}; -typedef struct multiboot_aout_symbol_table multiboot_aout_symbol_table_t; - -/* The section header table for ELF. */ -struct multiboot_elf_section_header_table -{ - multiboot_uint32_t num; - multiboot_uint32_t size; - multiboot_uint32_t addr; - multiboot_uint32_t shndx; -}; -typedef struct multiboot_elf_section_header_table multiboot_elf_section_header_table_t; - -struct multiboot_info -{ - /* Multiboot info version number */ - multiboot_uint32_t flags; - - /* Available memory from BIOS */ - multiboot_uint32_t mem_lower; - multiboot_uint32_t mem_upper; - - /* "root" partition */ - multiboot_uint32_t boot_device; - - /* Kernel command line */ - multiboot_uint32_t cmdline; - - /* Boot-Module list */ - multiboot_uint32_t mods_count; - multiboot_uint32_t mods_addr; - - union - { - multiboot_aout_symbol_table_t aout_sym; - multiboot_elf_section_header_table_t elf_sec; - } u; - - /* Memory Mapping buffer */ - multiboot_uint32_t mmap_length; - multiboot_uint32_t mmap_addr; - - /* Drive Info buffer */ - multiboot_uint32_t drives_length; - multiboot_uint32_t drives_addr; - - /* ROM configuration table */ - multiboot_uint32_t config_table; - - /* Boot Loader Name */ - multiboot_uint32_t boot_loader_name; - - /* APM table */ - multiboot_uint32_t apm_table; - - /* Video */ - multiboot_uint32_t vbe_control_info; - multiboot_uint32_t vbe_mode_info; - multiboot_uint16_t vbe_mode; - multiboot_uint16_t vbe_interface_seg; - multiboot_uint16_t vbe_interface_off; - multiboot_uint16_t vbe_interface_len; - - multiboot_uint64_t framebuffer_addr; - multiboot_uint32_t framebuffer_pitch; - multiboot_uint32_t framebuffer_width; - multiboot_uint32_t framebuffer_height; - multiboot_uint8_t framebuffer_bpp; -#define MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED 0 -#define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1 -#define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2 - multiboot_uint8_t framebuffer_type; - union - { - struct - { - multiboot_uint32_t framebuffer_palette_addr; - multiboot_uint16_t framebuffer_palette_num_colors; - }; - struct - { - multiboot_uint8_t framebuffer_red_field_position; - multiboot_uint8_t framebuffer_red_mask_size; - multiboot_uint8_t framebuffer_green_field_position; - multiboot_uint8_t framebuffer_green_mask_size; - multiboot_uint8_t framebuffer_blue_field_position; - multiboot_uint8_t framebuffer_blue_mask_size; - }; - }; -}; -typedef struct multiboot_info multiboot_info_t; - -struct multiboot_color -{ - multiboot_uint8_t red; - multiboot_uint8_t green; - multiboot_uint8_t blue; -}; - -struct multiboot_mmap_entry -{ - multiboot_uint32_t size; - multiboot_uint64_t addr; - multiboot_uint64_t len; -#define MULTIBOOT_MEMORY_AVAILABLE 1 -#define MULTIBOOT_MEMORY_RESERVED 2 -#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 -#define MULTIBOOT_MEMORY_NVS 4 -#define MULTIBOOT_MEMORY_BADRAM 5 - multiboot_uint32_t type; -} __attribute__((packed)); -typedef struct multiboot_mmap_entry multiboot_memory_map_t; - -struct multiboot_mod_list -{ - /* the memory used goes from bytes ’mod_start’ to ’mod_end-1’ inclusive */ - multiboot_uint32_t mod_start; - multiboot_uint32_t mod_end; - - /* Module command line */ - multiboot_uint32_t cmdline; - - /* padding to take it to 16 bytes (must be zero) */ - multiboot_uint32_t pad; -}; -typedef struct multiboot_mod_list multiboot_module_t; - -/* APM BIOS info. */ -struct multiboot_apm_info -{ - multiboot_uint16_t version; - multiboot_uint16_t cseg; - multiboot_uint32_t offset; - multiboot_uint16_t cseg_16; - multiboot_uint16_t dseg; - multiboot_uint16_t flags; - multiboot_uint16_t cseg_len; - multiboot_uint16_t cseg_16_len; - multiboot_uint16_t dseg_len; -}; - -#endif /* ! ASM_FILE */ - -#endif /* ! MULTIBOOT_HEADER */ \ No newline at end of file diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/linker.ld b/linker.ld index b3c2db3..b13d161 100644 --- a/linker.ld +++ b/linker.ld @@ -1,242 +1,24 @@ -/* Script for -z combreloc -z separate-code */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -/* file generated with ld --verbose */ OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib"); -SECTIONS -{ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - _kernel_start = .; +OUTPUT_FORMAT("binary") - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; - PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - _kernel_end = .; - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +SECTIONS { + . = 0x7E00; + + .text BLOCK(4K) : ALIGN(4K) { + *(.entry) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) { + *(COMMON) + *(.bss) + } } \ No newline at end of file diff --git a/src/boot/___boot.asm b/src/boot/___boot.asm new file mode 100644 index 0000000..bccd64a --- /dev/null +++ b/src/boot/___boot.asm @@ -0,0 +1,263 @@ +org 0x7c00 +bits 16 + +%ifndef SECTOR_CNT + %define SECTOR_CNT 3 +%endif + +jmp 0x0000:mbr_start + +mbr_start: + cli + cld + + ; Clear rest of segments + xor ax, ax + mov ds, ax + mov es, ax + + mov ss, ax + mov sp, 0x7c00 + mov bp, sp + + ; Store boot device + push dx + + ; swap to unreal mode + push ds + lgdt [gdtEnd] + mov eax, cr0 + or al, 1 + mov cr0, eax + jmp .pmode +.pmode: + mov bx, 0x08 + mov ds, bx + and al, 0xFE + mov cr0, eax + pop ds + sti + + ; Clear screen + xor ax, ax + mov al, 0x03 + int 0x10 + + ; Initialize serial port + mov dx, 0x3F9 + xor al, al + out dx, al + + add dx, 2 + mov al, 0x80 + out dx, al + + sub dx, 3 + xor al, al + out dx, al + + inc dx + out dx, al + + add dx, 2 + add al, 3 + out dx, al + + dec dx + mov al, 0xC7 + out dx, al + + add dx, 2 + mov al, 0x0B + out dx, al + +load_second_stage: + pop dx + push dx + mov bx, loader_entry + xor ch, ch + mov cl, 0x02 + xor dh, dh + mov byte [.sectors_to_load], SECTOR_CNT + + .read_start: + mov di, 5 + .read: + mov ah, 0x02 + mov al, byte [.sectors_to_load] + int 0x13 + jc .retry + cmp al, byte [.sectors_to_load] + je .done + sub byte [.sectors_to_load], al + mov cl, 0x01 + xor dh, 1 + jnz .read_start + inc ch + jmp .read_start + .retry: + ; disk read failed, reset disk + xor ah, ah + int 0x13 + dec di + jnz .read + mov si, .msg_diskread_fail + jmp .fail + .done: + jmp loader_entry + .fail: + lodsb + or al, al + mov ah, 0x0E + int 0x10 + test al, al + jnz .fail + .hang: + cli + hlt + jmp .hang + +.sectors_to_load: + db 1 +.msg_diskread_fail: + db "HALT: Failed to read boot disk", 0x0A, 0x0D, 0 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +; Padding & Signature +times 510-($-$$) db 0 +dw 0xAA55 + +loader_entry equ 0x7e00 + +bits 32 +_start: + ; mov ax, 16 + ; mov ds, ax + ; mov ss, ax + ; mov es, ax + ; mov fs, ax + ; mov gs, ax + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp x +x: + mov dword [0xb8000], 0x07690748 + jmp $ + mov ebx, hello + call printString +dump: + mov ebx, 0x7e00 + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + + +old: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/boot/__boot.asm b/src/boot/__boot.asm new file mode 100644 index 0000000..6c2e1be --- /dev/null +++ b/src/boot/__boot.asm @@ -0,0 +1,278 @@ +; bootp.asm Bootstrap for protected mode image +; Version 1.0, Dec 14, 1997 +; Sample code +; by John S. Fine johnfine@erols.com +; I do not place any restrictions on your use of this source code +; I do not provide any warranty of the correctness of this source code +;_____________________________________________________________________________ +; +; This program should be useable for three purposes: +; +; 1) Use it unmodified, in the boot sector of a floppy, to load a test image +; from the hard drive. In many environments, a floppy boot is the easiest +; way to have a clean boot for a pmode test. By getting the image itself +; from the hard drive, you eliminate most of the delays and restrictions of +; a floppy boot. +; +; 2) As a bootstrap tutorial, it demonstrates many of the steps required by +; a protected mode bootstrap, and many tricks for keeping code size down +; in a bootstrap. +; +; 3) As the basis for a protected mode, partition level, bootstrap. Just +; remove some of the steps that aren't needed in your environment, and add +; some error messages for unsupported conditions. +;_____________________________________________________________________________ +; +; Documentation: +; +; 1) Most of the documentation is in comments at the END of this file +; 2) Some of the documentation is in a separate file: BOOTP.TXT +; 3) In the comments within the source code are several uses of {}, each +; of these indicates a section of documentation at the end of this file +; which applies to that part of the source code. +; 4) The major way I reduced code size was to rely on left-over values in +; registers and/or to initialize registers at points where doing so was +; cheap. To make that understandable, there are several lines beginning +; with either ";>" or ";>>". These lines describe the register contents +; at that point in the code. ";>" indicates that the value is required +; by the next section of code. ";>>" indicates that the value passes +; through the next section unmodified and is needed later. +;_____________________________________________________________________________ + + %include "gdt.inc" + + struc DISK_PARM ;BIOS hard disk parameters +DP_cyl resw 1 +DP_heads resb 1 + resb 5 ;Obsolete +DP_ctrl resb 1 + resb 5 ;Obsolete +DP_sect resb 1 + resb 1 + endstruc + + struc PT_ENT ;Partition table entry +BootFlag resb 1 +BeginHead resb 1 +BeginSector resb 1 +BeginCyl resb 1 +SystemID resb 1 +EndHead resb 1 +EndSector resb 1 +EndCyl resb 1 +RelSectorLow resw 1 +RelSectorHigh resw 1 +NumSectorsLow resw 1 +NumSectorsHigh resw 1 + endstruc + + struc BB ;Partition Boot block + resb 0xD ;Things we ignore +BB_clu resb 1 ;Sectors per cluster +BB_res resw 1 ;Reserved sectors +BB_fats resb 1 ;Number of FATs +BB_root resw 1 ;Root directory entries + resb 3 ;Things we ignore +BB_fat resw 1 ;Sectors per fat + endstruc + +max_xfer equ 0x78 ;Maximum number of sectors to transfer at once + ;Must be divisible by 8 + + +SEGMENT START USE16 + jmp short start ;Standard start of boot sector + nop + resb 0x3B ;Skip over parameters (set by format) + +start: + cli + lgdt [cs:gdt] + mov al, 0x80 + mov es, ax + + xor eax, eax ;Segment and LBN + mov ds, ax ;{1B} + mov ss, ax + mov sp, start + + xor edi, edi + inc di + call read_sectors + +;> eax = 0 +;> edi = 1 +;> ds = 0 +;> es = 80 + + mov si, 0x9BE-PT_ENT_size ;Point at partition table +.10: add si, byte PT_ENT_size + cmp [si+BootFlag], al ;Active? + je .10 ;No + jg boot_error + mov eax, [si+RelSectorLow] ;LBN sector number of partition + call read_sectors ;Read partition boot block + +;> eax = LBN of partition +;> ebx = 0000???? +;> edi = 0000???? +;> ds = 0 + + mov bp, 0x800 ;Point at partition boot block + mov bx, [bp+BB_res] ;Sectors before first FAT + add eax, ebx ;LBN of FAT + push eax ;Need to read the FAT later + movzx bx, [bp+BB_fats] ;Number of FATs + imul bx, [bp+BB_fat] ;Times size of FAT + add eax, ebx ;LBN of Root directory + mov di, [bp+BB_root] ;Root directory entries + push di ; used again later + dec di ;Convert to number of sectors + shr di, 4 ; 16 directory entries per sector + inc di + mov es, bp ;Read directory at 800:0 + call read_sectors + add eax, edi ;LBN of cluster 2 + +;>> [sp+2] = LBN of FAT +;> [sp] = Number of root directory entries +;>> eax = LBN of cluster 2 +;> edi = 0000???? +;>> bp = 800 +;> ds = 0 +;> es = 800 + + pop bx ;Root directory entries + xor di, di ;Point at directory {1C} +.20: mov si, file_name ;Name of file we want + xor ecx, ecx + mov cl, 11 + a32 rep cmpsb ;Found the file? + je found ;Yes + add cl, 21 ;Offset to next directory entry + add edi, ecx ;Advance to next entry + dec bx ;Loop through all entries + jnz .20 + + ;Couldn't find file in directory +boot_error: +disk_error: + mov ax, 0xE07 ;{3} + int 10h + jmp short $ + +;> [sp] = LBN of FAT +;> eax = LBN of cluster 2 +;> es:edi = Pointer to end of name within directory entry +;> bp = 800 +;> ds = 0 +;> es = 800 + +eof: + mov bh, 0x9C + mov es, bx ;es = 9C00 + xor di, di ;{1E} Address of page tables WRT es + mov bh, 4096/256 ;ebx = 4096 +.10: mov ch, 4 ;cx = 1024 + mov al, 7 +.20: stosd + add eax, ebx + int 8 ;{8} + loop .20 + shr eax, 2 ;{4C} (first time only) 4Mb / 4 = 1Mb + shr si, 1 ;Done just one page? + jo .10 ;Yes + + cli ;{6} + + mov eax, 0x9C007 ;First page tbl pointer in page dir + stosd ;{1H} + mov ax, (1024-3)*2 + xchg ax, cx + rep stosw + mov ax, 0xD007 ;0FF800000 page tbl pointer + stosd ;{1F} + mov ah, 0xE0 ;Page directory self pointer + stosd ;{1G} + mov al, 0 + mov CR3, eax ;Set up page directory + mov eax, CR0 ;Turn on paging and protected mode + or eax, 0x80000001 + mov CR0, eax + mov cl, flat_data ;Setup ds and es + push cx ;{5} + pop ds + mov es, cx + jmp dword 8:0xFF800000 ;Go + + read_sectors: +; Input: +; EAX = LBN +; DI = sector count +; ES = segment +; Output: +; BL = low byte of ES +; EBX high half cleared +; DL = 0x80 +; EDX high half cleared +; ESI = 0 +; Clobbered: +; BH, CX, DH + + push eax + push di + push es + +.10: push eax ;LBN + + push ds + lds si, [0x104] ;Hard drive 0 parameters + cdq ;edx = 0 + movzx ebx, byte [si+DP_sect] + div ebx ;EAX=track ;EDX=sector-1 + mov cx, dx ;CL=sector-1 ;CH=0 + inc cx ;CL=Sector number + xor dx, dx + mov bl, [si+DP_heads] + pop ds + div ebx + + mov dh, dl + mov dl, 0x80 + xchg ch, al + shr ax, 2 + or cl, al + mov al, max_xfer ;AX = Maximum sectors to xfer + cmp ax, di ;Total is larger? + jb .20 ;Yes: transfer maximum + xchg ax, di ;No: AX=total ;DI=maximum +.20: mov ah, 2 ;Read + xor bx, bx + int 13h + jc near disk_error + + mov bx, es + add bh, max_xfer>>3 ;Advance segment + mov es, bx + + pop eax + add eax, byte max_xfer + sub di, byte max_xfer + ja .10 + + pop es + pop di + pop eax + xor esi, esi + ret + +gdt start_gdt + +flat_code desc 0, 0xFFBFF, D_CODE+D_READ+D_BIG+D_BIG_LIM + +flat_data desc 0, 0xFFFFF, D_DATA+D_WRITE+D_BIG+D_BIG_LIM + + end_gdt + + resb 0x1FE+$$-$ + db 0x55, 0xAA diff --git a/src/boot/boot.asm b/src/boot/boot.asm new file mode 100644 index 0000000..19a0342 --- /dev/null +++ b/src/boot/boot.asm @@ -0,0 +1,213 @@ +BITS 16 +ORG 0x7C00 + +section .text + +loadOS: + mov [BOOT_DRIVE], dl + + xor ax, ax + mov ds, ax + mov es, ax + mov ss, ax + mov fs, ax + mov gs, ax + + mov bp, 0x9000 + mov sp, bp +resetDisk: + xor ah, ah + mov dl, [BOOT_DRIVE] + ; int 0x13 +load: + ; mov ah, 0x02 + ; mov al, 1 + ; mov cx, 0x01 + ; mov bx, CODE_SEG + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov dl, [BOOT_DRIVE] + ; mov dh, 0x00 + ; int 0x13 + ; jc error + ; jmp dump + ; mov ah, 0x42 + ; mov dl, [BOOT_DRIVE] + ; mov cx, 0x00 + ; mov ds, cx + ; mov si, diskAddrebx + ; mov eax, 0x0001 + ; mov dl, [BOOT_DRIVE] + ; mov di, KERNEL_POSITION + ; mov cx, 0x01 + +diskLoad: + ; mov ah, 0x02 ; read + ; mov al, 0x01 ; sector count + ; mov cx, 0x0002 ; cylinder & sector + ; mov dh, 0x00 ; head + ; mov dl, [BOOT_DRIVE] ; drive + ; ; xor bx, bx ; position + ; ; mov es, bx + ; xor bx, bx + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov ah, 0x42 + ; mov dx, DAP + ; mov si, DAP + ; xor dh, dh + mov dl, [BOOT_DRIVE] + mov ah, 0x02 ;Read Disk Sectors + mov al, 0x05 ;Read one sector only + mov ch, 0x00 ;Track 0 + mov cl, 0x02 ;Sector 2 + mov dh, 0x00 ;Head 0 + ; mov dl, 0x00 ;Drive 0 (Floppy 1) + ; mov bx, 0x800 ;Segment 0x2000 + ; mov es, bx + mov bx, KERNEL_POSITION ;Start of Segment + + int 0x13 + + mov [errorByte], ah + +switchToX86: + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp CODE_SEG:_start + +bits 32 +_start: + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov al, [BOOT_DRIVE] + call print_hex_byte + mov ebx, hello + call printString + mov al, [errorByte] + call print_hex_byte +dump: + mov ebx, KERNEL_POSITION + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +CODE_SEG equ 8 +DATA_SEG equ 16 + +BOOT_DRIVE: db 0 +ALIGN 4 +DAP: +.size db 0x10 +.reserved db 0x00 +.sectors dw 0x0001 +.offset dw KERNEL_POSITION +.segment dw 0x0000 +.lbaLsd dd 0x00000000 +.lbaMsd dd 0x00000002 + +errorByte db 0x00 + +hello db 'hello world! ', 0x00 +errorMessage db 'error! ', 0x00 + + + +padding: + times 510-($-$$) db 0x00 + dw 0xAA55 + +KERNEL_POSITION equ 0x7e00 +KERNEL_SIZE equ 1 \ No newline at end of file diff --git a/src/kernel/entry.asm b/src/kernel/entry.asm new file mode 100644 index 0000000..99ceb59 --- /dev/null +++ b/src/kernel/entry.asm @@ -0,0 +1,25 @@ +section .entry +extern kernelMain +bits 32 +_start: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index cfdf333..21c60b4 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -9,11 +9,9 @@ extern uint32_t _kernel_end; void kernelMain() { + puts("hello world"); initMemoryAllocation(0x100000); // initializing stacks after the kernel seems not to work :( otherwise, _kernel_end should be passed here initOSTasks(); drawLogo(); - printf("Switching to otherTask... \n"); - yields(); - printf("Returned to mainTask!\n\n"); yield(); } \ No newline at end of file diff --git a/src/kernel/lib/task/task.c b/src/kernel/lib/task/task.c index 5d57403..6163772 100644 --- a/src/kernel/lib/task/task.c +++ b/src/kernel/lib/task/task.c @@ -29,6 +29,7 @@ puts("No more tasks to run, halting . . .\n"); while (1) { asm("hlt"); + puts("interrupt!\n"); } } last->nextTask = 0x00; diff --git a/src/kernel/multiboot.h b/src/kernel/multiboot.h deleted file mode 100644 index c6f5ff8..0000000 --- a/src/kernel/multiboot.h +++ /dev/null @@ -1,274 +0,0 @@ -/* multiboot.h - Multiboot header file. */ -/* Copyright (C) 1999,2003,2007,2008,2009,2010 Free Software Foundation, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY - * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR - * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef MULTIBOOT_HEADER -#define MULTIBOOT_HEADER 1 - -/* How many bytes from the start of the file we search for the header. */ -#define MULTIBOOT_SEARCH 8192 -#define MULTIBOOT_HEADER_ALIGN 4 - -/* The magic field should contain this. */ -#define MULTIBOOT_HEADER_MAGIC 0x1BADB002 - -/* This should be in %eax. */ -#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002 - -/* Alignment of multiboot modules. */ -#define MULTIBOOT_MOD_ALIGN 0x00001000 - -/* Alignment of the multiboot info structure. */ -#define MULTIBOOT_INFO_ALIGN 0x00000004 - -/* Flags set in the ’flags’ member of the multiboot header. */ - -/* Align all boot modules on i386 page (4KB) boundaries. */ -#define MULTIBOOT_PAGE_ALIGN 0x00000001 - -/* Must pass memory information to OS. */ -#define MULTIBOOT_MEMORY_INFO 0x00000002 - -/* Must pass video information to OS. */ -#define MULTIBOOT_VIDEO_MODE 0x00000004 - -/* This flag indicates the use of the address fields in the header. */ -#define MULTIBOOT_AOUT_KLUDGE 0x00010000 - -/* Flags to be set in the ’flags’ member of the multiboot info structure. */ - -/* is there basic lower/upper memory information? */ -#define MULTIBOOT_INFO_MEMORY 0x00000001 -/* is there a boot device set? */ -#define MULTIBOOT_INFO_BOOTDEV 0x00000002 -/* is the command-line defined? */ -#define MULTIBOOT_INFO_CMDLINE 0x00000004 -/* are there modules to do something with? */ -#define MULTIBOOT_INFO_MODS 0x00000008 - -/* These next two are mutually exclusive */ - -/* is there a symbol table loaded? */ -#define MULTIBOOT_INFO_AOUT_SYMS 0x00000010 -/* is there an ELF section header table? */ -#define MULTIBOOT_INFO_ELF_SHDR 0X00000020 - -/* is there a full memory map? */ -#define MULTIBOOT_INFO_MEM_MAP 0x00000040 - -/* Is there drive info? */ -#define MULTIBOOT_INFO_DRIVE_INFO 0x00000080 - -/* Is there a config table? */ -#define MULTIBOOT_INFO_CONFIG_TABLE 0x00000100 - -/* Is there a boot loader name? */ -#define MULTIBOOT_INFO_BOOT_LOADER_NAME 0x00000200 - -/* Is there a APM table? */ -#define MULTIBOOT_INFO_APM_TABLE 0x00000400 - -/* Is there video information? */ -#define MULTIBOOT_INFO_VBE_INFO 0x00000800 -#define MULTIBOOT_INFO_FRAMEBUFFER_INFO 0x00001000 - -#ifndef ASM_FILE - -typedef unsigned char multiboot_uint8_t; -typedef unsigned short multiboot_uint16_t; -typedef unsigned int multiboot_uint32_t; -typedef unsigned long long multiboot_uint64_t; - -struct multiboot_header -{ - /* Must be MULTIBOOT_MAGIC - see above. */ - multiboot_uint32_t magic; - - /* Feature flags. */ - multiboot_uint32_t flags; - - /* The above fields plus this one must equal 0 mod 2^32. */ - multiboot_uint32_t checksum; - - /* These are only valid if MULTIBOOT_AOUT_KLUDGE is set. */ - multiboot_uint32_t header_addr; - multiboot_uint32_t load_addr; - multiboot_uint32_t load_end_addr; - multiboot_uint32_t bss_end_addr; - multiboot_uint32_t entry_addr; - - /* These are only valid if MULTIBOOT_VIDEO_MODE is set. */ - multiboot_uint32_t mode_type; - multiboot_uint32_t width; - multiboot_uint32_t height; - multiboot_uint32_t depth; -}; - -/* The symbol table for a.out. */ -struct multiboot_aout_symbol_table -{ - multiboot_uint32_t tabsize; - multiboot_uint32_t strsize; - multiboot_uint32_t addr; - multiboot_uint32_t reserved; -}; -typedef struct multiboot_aout_symbol_table multiboot_aout_symbol_table_t; - -/* The section header table for ELF. */ -struct multiboot_elf_section_header_table -{ - multiboot_uint32_t num; - multiboot_uint32_t size; - multiboot_uint32_t addr; - multiboot_uint32_t shndx; -}; -typedef struct multiboot_elf_section_header_table multiboot_elf_section_header_table_t; - -struct multiboot_info -{ - /* Multiboot info version number */ - multiboot_uint32_t flags; - - /* Available memory from BIOS */ - multiboot_uint32_t mem_lower; - multiboot_uint32_t mem_upper; - - /* "root" partition */ - multiboot_uint32_t boot_device; - - /* Kernel command line */ - multiboot_uint32_t cmdline; - - /* Boot-Module list */ - multiboot_uint32_t mods_count; - multiboot_uint32_t mods_addr; - - union - { - multiboot_aout_symbol_table_t aout_sym; - multiboot_elf_section_header_table_t elf_sec; - } u; - - /* Memory Mapping buffer */ - multiboot_uint32_t mmap_length; - multiboot_uint32_t mmap_addr; - - /* Drive Info buffer */ - multiboot_uint32_t drives_length; - multiboot_uint32_t drives_addr; - - /* ROM configuration table */ - multiboot_uint32_t config_table; - - /* Boot Loader Name */ - multiboot_uint32_t boot_loader_name; - - /* APM table */ - multiboot_uint32_t apm_table; - - /* Video */ - multiboot_uint32_t vbe_control_info; - multiboot_uint32_t vbe_mode_info; - multiboot_uint16_t vbe_mode; - multiboot_uint16_t vbe_interface_seg; - multiboot_uint16_t vbe_interface_off; - multiboot_uint16_t vbe_interface_len; - - multiboot_uint64_t framebuffer_addr; - multiboot_uint32_t framebuffer_pitch; - multiboot_uint32_t framebuffer_width; - multiboot_uint32_t framebuffer_height; - multiboot_uint8_t framebuffer_bpp; -#define MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED 0 -#define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1 -#define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2 - multiboot_uint8_t framebuffer_type; - union - { - struct - { - multiboot_uint32_t framebuffer_palette_addr; - multiboot_uint16_t framebuffer_palette_num_colors; - }; - struct - { - multiboot_uint8_t framebuffer_red_field_position; - multiboot_uint8_t framebuffer_red_mask_size; - multiboot_uint8_t framebuffer_green_field_position; - multiboot_uint8_t framebuffer_green_mask_size; - multiboot_uint8_t framebuffer_blue_field_position; - multiboot_uint8_t framebuffer_blue_mask_size; - }; - }; -}; -typedef struct multiboot_info multiboot_info_t; - -struct multiboot_color -{ - multiboot_uint8_t red; - multiboot_uint8_t green; - multiboot_uint8_t blue; -}; - -struct multiboot_mmap_entry -{ - multiboot_uint32_t size; - multiboot_uint64_t addr; - multiboot_uint64_t len; -#define MULTIBOOT_MEMORY_AVAILABLE 1 -#define MULTIBOOT_MEMORY_RESERVED 2 -#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 -#define MULTIBOOT_MEMORY_NVS 4 -#define MULTIBOOT_MEMORY_BADRAM 5 - multiboot_uint32_t type; -} __attribute__((packed)); -typedef struct multiboot_mmap_entry multiboot_memory_map_t; - -struct multiboot_mod_list -{ - /* the memory used goes from bytes ’mod_start’ to ’mod_end-1’ inclusive */ - multiboot_uint32_t mod_start; - multiboot_uint32_t mod_end; - - /* Module command line */ - multiboot_uint32_t cmdline; - - /* padding to take it to 16 bytes (must be zero) */ - multiboot_uint32_t pad; -}; -typedef struct multiboot_mod_list multiboot_module_t; - -/* APM BIOS info. */ -struct multiboot_apm_info -{ - multiboot_uint16_t version; - multiboot_uint16_t cseg; - multiboot_uint32_t offset; - multiboot_uint16_t cseg_16; - multiboot_uint16_t dseg; - multiboot_uint16_t flags; - multiboot_uint16_t cseg_len; - multiboot_uint16_t cseg_16_len; - multiboot_uint16_t dseg_len; -}; - -#endif /* ! ASM_FILE */ - -#endif /* ! MULTIBOOT_HEADER */ \ No newline at end of file diff --git a/src/multiboot/boot.asm b/src/multiboot/boot.asm deleted file mode 100644 index 965b8e1..0000000 --- a/src/multiboot/boot.asm +++ /dev/null @@ -1,25 +0,0 @@ -global loader -MAGIC_NUMBER equ 0x1BADB002 ; 1 bad boot -FLAGS equ 0x0 -CHECKSUM equ -MAGIC_NUMBER -section .text -align 4 - dd MAGIC_NUMBER - dd FLAGS - dd CHECKSUM -extern kernelMain - -KERNEL_STACK_SIZE equ 4096 ; 4kb of stack -global _start -_start: - ; setup stack for kernel main function - mov esp, stack + KERNEL_STACK_SIZE - ; call main function - call kernelMain -.loop: - jmp .loop - -section .bss -align 4 -stack: - resb KERNEL_STACK_SIZE \ No newline at end of file diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/linker.ld b/linker.ld index b3c2db3..b13d161 100644 --- a/linker.ld +++ b/linker.ld @@ -1,242 +1,24 @@ -/* Script for -z combreloc -z separate-code */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -/* file generated with ld --verbose */ OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib"); -SECTIONS -{ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - _kernel_start = .; +OUTPUT_FORMAT("binary") - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; - PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - _kernel_end = .; - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +SECTIONS { + . = 0x7E00; + + .text BLOCK(4K) : ALIGN(4K) { + *(.entry) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) { + *(COMMON) + *(.bss) + } } \ No newline at end of file diff --git a/src/boot/___boot.asm b/src/boot/___boot.asm new file mode 100644 index 0000000..bccd64a --- /dev/null +++ b/src/boot/___boot.asm @@ -0,0 +1,263 @@ +org 0x7c00 +bits 16 + +%ifndef SECTOR_CNT + %define SECTOR_CNT 3 +%endif + +jmp 0x0000:mbr_start + +mbr_start: + cli + cld + + ; Clear rest of segments + xor ax, ax + mov ds, ax + mov es, ax + + mov ss, ax + mov sp, 0x7c00 + mov bp, sp + + ; Store boot device + push dx + + ; swap to unreal mode + push ds + lgdt [gdtEnd] + mov eax, cr0 + or al, 1 + mov cr0, eax + jmp .pmode +.pmode: + mov bx, 0x08 + mov ds, bx + and al, 0xFE + mov cr0, eax + pop ds + sti + + ; Clear screen + xor ax, ax + mov al, 0x03 + int 0x10 + + ; Initialize serial port + mov dx, 0x3F9 + xor al, al + out dx, al + + add dx, 2 + mov al, 0x80 + out dx, al + + sub dx, 3 + xor al, al + out dx, al + + inc dx + out dx, al + + add dx, 2 + add al, 3 + out dx, al + + dec dx + mov al, 0xC7 + out dx, al + + add dx, 2 + mov al, 0x0B + out dx, al + +load_second_stage: + pop dx + push dx + mov bx, loader_entry + xor ch, ch + mov cl, 0x02 + xor dh, dh + mov byte [.sectors_to_load], SECTOR_CNT + + .read_start: + mov di, 5 + .read: + mov ah, 0x02 + mov al, byte [.sectors_to_load] + int 0x13 + jc .retry + cmp al, byte [.sectors_to_load] + je .done + sub byte [.sectors_to_load], al + mov cl, 0x01 + xor dh, 1 + jnz .read_start + inc ch + jmp .read_start + .retry: + ; disk read failed, reset disk + xor ah, ah + int 0x13 + dec di + jnz .read + mov si, .msg_diskread_fail + jmp .fail + .done: + jmp loader_entry + .fail: + lodsb + or al, al + mov ah, 0x0E + int 0x10 + test al, al + jnz .fail + .hang: + cli + hlt + jmp .hang + +.sectors_to_load: + db 1 +.msg_diskread_fail: + db "HALT: Failed to read boot disk", 0x0A, 0x0D, 0 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +; Padding & Signature +times 510-($-$$) db 0 +dw 0xAA55 + +loader_entry equ 0x7e00 + +bits 32 +_start: + ; mov ax, 16 + ; mov ds, ax + ; mov ss, ax + ; mov es, ax + ; mov fs, ax + ; mov gs, ax + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp x +x: + mov dword [0xb8000], 0x07690748 + jmp $ + mov ebx, hello + call printString +dump: + mov ebx, 0x7e00 + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + + +old: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/boot/__boot.asm b/src/boot/__boot.asm new file mode 100644 index 0000000..6c2e1be --- /dev/null +++ b/src/boot/__boot.asm @@ -0,0 +1,278 @@ +; bootp.asm Bootstrap for protected mode image +; Version 1.0, Dec 14, 1997 +; Sample code +; by John S. Fine johnfine@erols.com +; I do not place any restrictions on your use of this source code +; I do not provide any warranty of the correctness of this source code +;_____________________________________________________________________________ +; +; This program should be useable for three purposes: +; +; 1) Use it unmodified, in the boot sector of a floppy, to load a test image +; from the hard drive. In many environments, a floppy boot is the easiest +; way to have a clean boot for a pmode test. By getting the image itself +; from the hard drive, you eliminate most of the delays and restrictions of +; a floppy boot. +; +; 2) As a bootstrap tutorial, it demonstrates many of the steps required by +; a protected mode bootstrap, and many tricks for keeping code size down +; in a bootstrap. +; +; 3) As the basis for a protected mode, partition level, bootstrap. Just +; remove some of the steps that aren't needed in your environment, and add +; some error messages for unsupported conditions. +;_____________________________________________________________________________ +; +; Documentation: +; +; 1) Most of the documentation is in comments at the END of this file +; 2) Some of the documentation is in a separate file: BOOTP.TXT +; 3) In the comments within the source code are several uses of {}, each +; of these indicates a section of documentation at the end of this file +; which applies to that part of the source code. +; 4) The major way I reduced code size was to rely on left-over values in +; registers and/or to initialize registers at points where doing so was +; cheap. To make that understandable, there are several lines beginning +; with either ";>" or ";>>". These lines describe the register contents +; at that point in the code. ";>" indicates that the value is required +; by the next section of code. ";>>" indicates that the value passes +; through the next section unmodified and is needed later. +;_____________________________________________________________________________ + + %include "gdt.inc" + + struc DISK_PARM ;BIOS hard disk parameters +DP_cyl resw 1 +DP_heads resb 1 + resb 5 ;Obsolete +DP_ctrl resb 1 + resb 5 ;Obsolete +DP_sect resb 1 + resb 1 + endstruc + + struc PT_ENT ;Partition table entry +BootFlag resb 1 +BeginHead resb 1 +BeginSector resb 1 +BeginCyl resb 1 +SystemID resb 1 +EndHead resb 1 +EndSector resb 1 +EndCyl resb 1 +RelSectorLow resw 1 +RelSectorHigh resw 1 +NumSectorsLow resw 1 +NumSectorsHigh resw 1 + endstruc + + struc BB ;Partition Boot block + resb 0xD ;Things we ignore +BB_clu resb 1 ;Sectors per cluster +BB_res resw 1 ;Reserved sectors +BB_fats resb 1 ;Number of FATs +BB_root resw 1 ;Root directory entries + resb 3 ;Things we ignore +BB_fat resw 1 ;Sectors per fat + endstruc + +max_xfer equ 0x78 ;Maximum number of sectors to transfer at once + ;Must be divisible by 8 + + +SEGMENT START USE16 + jmp short start ;Standard start of boot sector + nop + resb 0x3B ;Skip over parameters (set by format) + +start: + cli + lgdt [cs:gdt] + mov al, 0x80 + mov es, ax + + xor eax, eax ;Segment and LBN + mov ds, ax ;{1B} + mov ss, ax + mov sp, start + + xor edi, edi + inc di + call read_sectors + +;> eax = 0 +;> edi = 1 +;> ds = 0 +;> es = 80 + + mov si, 0x9BE-PT_ENT_size ;Point at partition table +.10: add si, byte PT_ENT_size + cmp [si+BootFlag], al ;Active? + je .10 ;No + jg boot_error + mov eax, [si+RelSectorLow] ;LBN sector number of partition + call read_sectors ;Read partition boot block + +;> eax = LBN of partition +;> ebx = 0000???? +;> edi = 0000???? +;> ds = 0 + + mov bp, 0x800 ;Point at partition boot block + mov bx, [bp+BB_res] ;Sectors before first FAT + add eax, ebx ;LBN of FAT + push eax ;Need to read the FAT later + movzx bx, [bp+BB_fats] ;Number of FATs + imul bx, [bp+BB_fat] ;Times size of FAT + add eax, ebx ;LBN of Root directory + mov di, [bp+BB_root] ;Root directory entries + push di ; used again later + dec di ;Convert to number of sectors + shr di, 4 ; 16 directory entries per sector + inc di + mov es, bp ;Read directory at 800:0 + call read_sectors + add eax, edi ;LBN of cluster 2 + +;>> [sp+2] = LBN of FAT +;> [sp] = Number of root directory entries +;>> eax = LBN of cluster 2 +;> edi = 0000???? +;>> bp = 800 +;> ds = 0 +;> es = 800 + + pop bx ;Root directory entries + xor di, di ;Point at directory {1C} +.20: mov si, file_name ;Name of file we want + xor ecx, ecx + mov cl, 11 + a32 rep cmpsb ;Found the file? + je found ;Yes + add cl, 21 ;Offset to next directory entry + add edi, ecx ;Advance to next entry + dec bx ;Loop through all entries + jnz .20 + + ;Couldn't find file in directory +boot_error: +disk_error: + mov ax, 0xE07 ;{3} + int 10h + jmp short $ + +;> [sp] = LBN of FAT +;> eax = LBN of cluster 2 +;> es:edi = Pointer to end of name within directory entry +;> bp = 800 +;> ds = 0 +;> es = 800 + +eof: + mov bh, 0x9C + mov es, bx ;es = 9C00 + xor di, di ;{1E} Address of page tables WRT es + mov bh, 4096/256 ;ebx = 4096 +.10: mov ch, 4 ;cx = 1024 + mov al, 7 +.20: stosd + add eax, ebx + int 8 ;{8} + loop .20 + shr eax, 2 ;{4C} (first time only) 4Mb / 4 = 1Mb + shr si, 1 ;Done just one page? + jo .10 ;Yes + + cli ;{6} + + mov eax, 0x9C007 ;First page tbl pointer in page dir + stosd ;{1H} + mov ax, (1024-3)*2 + xchg ax, cx + rep stosw + mov ax, 0xD007 ;0FF800000 page tbl pointer + stosd ;{1F} + mov ah, 0xE0 ;Page directory self pointer + stosd ;{1G} + mov al, 0 + mov CR3, eax ;Set up page directory + mov eax, CR0 ;Turn on paging and protected mode + or eax, 0x80000001 + mov CR0, eax + mov cl, flat_data ;Setup ds and es + push cx ;{5} + pop ds + mov es, cx + jmp dword 8:0xFF800000 ;Go + + read_sectors: +; Input: +; EAX = LBN +; DI = sector count +; ES = segment +; Output: +; BL = low byte of ES +; EBX high half cleared +; DL = 0x80 +; EDX high half cleared +; ESI = 0 +; Clobbered: +; BH, CX, DH + + push eax + push di + push es + +.10: push eax ;LBN + + push ds + lds si, [0x104] ;Hard drive 0 parameters + cdq ;edx = 0 + movzx ebx, byte [si+DP_sect] + div ebx ;EAX=track ;EDX=sector-1 + mov cx, dx ;CL=sector-1 ;CH=0 + inc cx ;CL=Sector number + xor dx, dx + mov bl, [si+DP_heads] + pop ds + div ebx + + mov dh, dl + mov dl, 0x80 + xchg ch, al + shr ax, 2 + or cl, al + mov al, max_xfer ;AX = Maximum sectors to xfer + cmp ax, di ;Total is larger? + jb .20 ;Yes: transfer maximum + xchg ax, di ;No: AX=total ;DI=maximum +.20: mov ah, 2 ;Read + xor bx, bx + int 13h + jc near disk_error + + mov bx, es + add bh, max_xfer>>3 ;Advance segment + mov es, bx + + pop eax + add eax, byte max_xfer + sub di, byte max_xfer + ja .10 + + pop es + pop di + pop eax + xor esi, esi + ret + +gdt start_gdt + +flat_code desc 0, 0xFFBFF, D_CODE+D_READ+D_BIG+D_BIG_LIM + +flat_data desc 0, 0xFFFFF, D_DATA+D_WRITE+D_BIG+D_BIG_LIM + + end_gdt + + resb 0x1FE+$$-$ + db 0x55, 0xAA diff --git a/src/boot/boot.asm b/src/boot/boot.asm new file mode 100644 index 0000000..19a0342 --- /dev/null +++ b/src/boot/boot.asm @@ -0,0 +1,213 @@ +BITS 16 +ORG 0x7C00 + +section .text + +loadOS: + mov [BOOT_DRIVE], dl + + xor ax, ax + mov ds, ax + mov es, ax + mov ss, ax + mov fs, ax + mov gs, ax + + mov bp, 0x9000 + mov sp, bp +resetDisk: + xor ah, ah + mov dl, [BOOT_DRIVE] + ; int 0x13 +load: + ; mov ah, 0x02 + ; mov al, 1 + ; mov cx, 0x01 + ; mov bx, CODE_SEG + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov dl, [BOOT_DRIVE] + ; mov dh, 0x00 + ; int 0x13 + ; jc error + ; jmp dump + ; mov ah, 0x42 + ; mov dl, [BOOT_DRIVE] + ; mov cx, 0x00 + ; mov ds, cx + ; mov si, diskAddrebx + ; mov eax, 0x0001 + ; mov dl, [BOOT_DRIVE] + ; mov di, KERNEL_POSITION + ; mov cx, 0x01 + +diskLoad: + ; mov ah, 0x02 ; read + ; mov al, 0x01 ; sector count + ; mov cx, 0x0002 ; cylinder & sector + ; mov dh, 0x00 ; head + ; mov dl, [BOOT_DRIVE] ; drive + ; ; xor bx, bx ; position + ; ; mov es, bx + ; xor bx, bx + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov ah, 0x42 + ; mov dx, DAP + ; mov si, DAP + ; xor dh, dh + mov dl, [BOOT_DRIVE] + mov ah, 0x02 ;Read Disk Sectors + mov al, 0x05 ;Read one sector only + mov ch, 0x00 ;Track 0 + mov cl, 0x02 ;Sector 2 + mov dh, 0x00 ;Head 0 + ; mov dl, 0x00 ;Drive 0 (Floppy 1) + ; mov bx, 0x800 ;Segment 0x2000 + ; mov es, bx + mov bx, KERNEL_POSITION ;Start of Segment + + int 0x13 + + mov [errorByte], ah + +switchToX86: + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp CODE_SEG:_start + +bits 32 +_start: + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov al, [BOOT_DRIVE] + call print_hex_byte + mov ebx, hello + call printString + mov al, [errorByte] + call print_hex_byte +dump: + mov ebx, KERNEL_POSITION + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +CODE_SEG equ 8 +DATA_SEG equ 16 + +BOOT_DRIVE: db 0 +ALIGN 4 +DAP: +.size db 0x10 +.reserved db 0x00 +.sectors dw 0x0001 +.offset dw KERNEL_POSITION +.segment dw 0x0000 +.lbaLsd dd 0x00000000 +.lbaMsd dd 0x00000002 + +errorByte db 0x00 + +hello db 'hello world! ', 0x00 +errorMessage db 'error! ', 0x00 + + + +padding: + times 510-($-$$) db 0x00 + dw 0xAA55 + +KERNEL_POSITION equ 0x7e00 +KERNEL_SIZE equ 1 \ No newline at end of file diff --git a/src/kernel/entry.asm b/src/kernel/entry.asm new file mode 100644 index 0000000..99ceb59 --- /dev/null +++ b/src/kernel/entry.asm @@ -0,0 +1,25 @@ +section .entry +extern kernelMain +bits 32 +_start: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index cfdf333..21c60b4 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -9,11 +9,9 @@ extern uint32_t _kernel_end; void kernelMain() { + puts("hello world"); initMemoryAllocation(0x100000); // initializing stacks after the kernel seems not to work :( otherwise, _kernel_end should be passed here initOSTasks(); drawLogo(); - printf("Switching to otherTask... \n"); - yields(); - printf("Returned to mainTask!\n\n"); yield(); } \ No newline at end of file diff --git a/src/kernel/lib/task/task.c b/src/kernel/lib/task/task.c index 5d57403..6163772 100644 --- a/src/kernel/lib/task/task.c +++ b/src/kernel/lib/task/task.c @@ -29,6 +29,7 @@ puts("No more tasks to run, halting . . .\n"); while (1) { asm("hlt"); + puts("interrupt!\n"); } } last->nextTask = 0x00; diff --git a/src/kernel/multiboot.h b/src/kernel/multiboot.h deleted file mode 100644 index c6f5ff8..0000000 --- a/src/kernel/multiboot.h +++ /dev/null @@ -1,274 +0,0 @@ -/* multiboot.h - Multiboot header file. */ -/* Copyright (C) 1999,2003,2007,2008,2009,2010 Free Software Foundation, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY - * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR - * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef MULTIBOOT_HEADER -#define MULTIBOOT_HEADER 1 - -/* How many bytes from the start of the file we search for the header. */ -#define MULTIBOOT_SEARCH 8192 -#define MULTIBOOT_HEADER_ALIGN 4 - -/* The magic field should contain this. */ -#define MULTIBOOT_HEADER_MAGIC 0x1BADB002 - -/* This should be in %eax. */ -#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002 - -/* Alignment of multiboot modules. */ -#define MULTIBOOT_MOD_ALIGN 0x00001000 - -/* Alignment of the multiboot info structure. */ -#define MULTIBOOT_INFO_ALIGN 0x00000004 - -/* Flags set in the ’flags’ member of the multiboot header. */ - -/* Align all boot modules on i386 page (4KB) boundaries. */ -#define MULTIBOOT_PAGE_ALIGN 0x00000001 - -/* Must pass memory information to OS. */ -#define MULTIBOOT_MEMORY_INFO 0x00000002 - -/* Must pass video information to OS. */ -#define MULTIBOOT_VIDEO_MODE 0x00000004 - -/* This flag indicates the use of the address fields in the header. */ -#define MULTIBOOT_AOUT_KLUDGE 0x00010000 - -/* Flags to be set in the ’flags’ member of the multiboot info structure. */ - -/* is there basic lower/upper memory information? */ -#define MULTIBOOT_INFO_MEMORY 0x00000001 -/* is there a boot device set? */ -#define MULTIBOOT_INFO_BOOTDEV 0x00000002 -/* is the command-line defined? */ -#define MULTIBOOT_INFO_CMDLINE 0x00000004 -/* are there modules to do something with? */ -#define MULTIBOOT_INFO_MODS 0x00000008 - -/* These next two are mutually exclusive */ - -/* is there a symbol table loaded? */ -#define MULTIBOOT_INFO_AOUT_SYMS 0x00000010 -/* is there an ELF section header table? */ -#define MULTIBOOT_INFO_ELF_SHDR 0X00000020 - -/* is there a full memory map? */ -#define MULTIBOOT_INFO_MEM_MAP 0x00000040 - -/* Is there drive info? */ -#define MULTIBOOT_INFO_DRIVE_INFO 0x00000080 - -/* Is there a config table? */ -#define MULTIBOOT_INFO_CONFIG_TABLE 0x00000100 - -/* Is there a boot loader name? */ -#define MULTIBOOT_INFO_BOOT_LOADER_NAME 0x00000200 - -/* Is there a APM table? */ -#define MULTIBOOT_INFO_APM_TABLE 0x00000400 - -/* Is there video information? */ -#define MULTIBOOT_INFO_VBE_INFO 0x00000800 -#define MULTIBOOT_INFO_FRAMEBUFFER_INFO 0x00001000 - -#ifndef ASM_FILE - -typedef unsigned char multiboot_uint8_t; -typedef unsigned short multiboot_uint16_t; -typedef unsigned int multiboot_uint32_t; -typedef unsigned long long multiboot_uint64_t; - -struct multiboot_header -{ - /* Must be MULTIBOOT_MAGIC - see above. */ - multiboot_uint32_t magic; - - /* Feature flags. */ - multiboot_uint32_t flags; - - /* The above fields plus this one must equal 0 mod 2^32. */ - multiboot_uint32_t checksum; - - /* These are only valid if MULTIBOOT_AOUT_KLUDGE is set. */ - multiboot_uint32_t header_addr; - multiboot_uint32_t load_addr; - multiboot_uint32_t load_end_addr; - multiboot_uint32_t bss_end_addr; - multiboot_uint32_t entry_addr; - - /* These are only valid if MULTIBOOT_VIDEO_MODE is set. */ - multiboot_uint32_t mode_type; - multiboot_uint32_t width; - multiboot_uint32_t height; - multiboot_uint32_t depth; -}; - -/* The symbol table for a.out. */ -struct multiboot_aout_symbol_table -{ - multiboot_uint32_t tabsize; - multiboot_uint32_t strsize; - multiboot_uint32_t addr; - multiboot_uint32_t reserved; -}; -typedef struct multiboot_aout_symbol_table multiboot_aout_symbol_table_t; - -/* The section header table for ELF. */ -struct multiboot_elf_section_header_table -{ - multiboot_uint32_t num; - multiboot_uint32_t size; - multiboot_uint32_t addr; - multiboot_uint32_t shndx; -}; -typedef struct multiboot_elf_section_header_table multiboot_elf_section_header_table_t; - -struct multiboot_info -{ - /* Multiboot info version number */ - multiboot_uint32_t flags; - - /* Available memory from BIOS */ - multiboot_uint32_t mem_lower; - multiboot_uint32_t mem_upper; - - /* "root" partition */ - multiboot_uint32_t boot_device; - - /* Kernel command line */ - multiboot_uint32_t cmdline; - - /* Boot-Module list */ - multiboot_uint32_t mods_count; - multiboot_uint32_t mods_addr; - - union - { - multiboot_aout_symbol_table_t aout_sym; - multiboot_elf_section_header_table_t elf_sec; - } u; - - /* Memory Mapping buffer */ - multiboot_uint32_t mmap_length; - multiboot_uint32_t mmap_addr; - - /* Drive Info buffer */ - multiboot_uint32_t drives_length; - multiboot_uint32_t drives_addr; - - /* ROM configuration table */ - multiboot_uint32_t config_table; - - /* Boot Loader Name */ - multiboot_uint32_t boot_loader_name; - - /* APM table */ - multiboot_uint32_t apm_table; - - /* Video */ - multiboot_uint32_t vbe_control_info; - multiboot_uint32_t vbe_mode_info; - multiboot_uint16_t vbe_mode; - multiboot_uint16_t vbe_interface_seg; - multiboot_uint16_t vbe_interface_off; - multiboot_uint16_t vbe_interface_len; - - multiboot_uint64_t framebuffer_addr; - multiboot_uint32_t framebuffer_pitch; - multiboot_uint32_t framebuffer_width; - multiboot_uint32_t framebuffer_height; - multiboot_uint8_t framebuffer_bpp; -#define MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED 0 -#define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1 -#define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2 - multiboot_uint8_t framebuffer_type; - union - { - struct - { - multiboot_uint32_t framebuffer_palette_addr; - multiboot_uint16_t framebuffer_palette_num_colors; - }; - struct - { - multiboot_uint8_t framebuffer_red_field_position; - multiboot_uint8_t framebuffer_red_mask_size; - multiboot_uint8_t framebuffer_green_field_position; - multiboot_uint8_t framebuffer_green_mask_size; - multiboot_uint8_t framebuffer_blue_field_position; - multiboot_uint8_t framebuffer_blue_mask_size; - }; - }; -}; -typedef struct multiboot_info multiboot_info_t; - -struct multiboot_color -{ - multiboot_uint8_t red; - multiboot_uint8_t green; - multiboot_uint8_t blue; -}; - -struct multiboot_mmap_entry -{ - multiboot_uint32_t size; - multiboot_uint64_t addr; - multiboot_uint64_t len; -#define MULTIBOOT_MEMORY_AVAILABLE 1 -#define MULTIBOOT_MEMORY_RESERVED 2 -#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 -#define MULTIBOOT_MEMORY_NVS 4 -#define MULTIBOOT_MEMORY_BADRAM 5 - multiboot_uint32_t type; -} __attribute__((packed)); -typedef struct multiboot_mmap_entry multiboot_memory_map_t; - -struct multiboot_mod_list -{ - /* the memory used goes from bytes ’mod_start’ to ’mod_end-1’ inclusive */ - multiboot_uint32_t mod_start; - multiboot_uint32_t mod_end; - - /* Module command line */ - multiboot_uint32_t cmdline; - - /* padding to take it to 16 bytes (must be zero) */ - multiboot_uint32_t pad; -}; -typedef struct multiboot_mod_list multiboot_module_t; - -/* APM BIOS info. */ -struct multiboot_apm_info -{ - multiboot_uint16_t version; - multiboot_uint16_t cseg; - multiboot_uint32_t offset; - multiboot_uint16_t cseg_16; - multiboot_uint16_t dseg; - multiboot_uint16_t flags; - multiboot_uint16_t cseg_len; - multiboot_uint16_t cseg_16_len; - multiboot_uint16_t dseg_len; -}; - -#endif /* ! ASM_FILE */ - -#endif /* ! MULTIBOOT_HEADER */ \ No newline at end of file diff --git a/src/multiboot/boot.asm b/src/multiboot/boot.asm deleted file mode 100644 index 965b8e1..0000000 --- a/src/multiboot/boot.asm +++ /dev/null @@ -1,25 +0,0 @@ -global loader -MAGIC_NUMBER equ 0x1BADB002 ; 1 bad boot -FLAGS equ 0x0 -CHECKSUM equ -MAGIC_NUMBER -section .text -align 4 - dd MAGIC_NUMBER - dd FLAGS - dd CHECKSUM -extern kernelMain - -KERNEL_STACK_SIZE equ 4096 ; 4kb of stack -global _start -_start: - ; setup stack for kernel main function - mov esp, stack + KERNEL_STACK_SIZE - ; call main function - call kernelMain -.loop: - jmp .loop - -section .bss -align 4 -stack: - resb KERNEL_STACK_SIZE \ No newline at end of file diff --git a/tree-os-files.tar b/tree-os-files.tar new file mode 100644 index 0000000..f68d3c0 --- /dev/null +++ b/tree-os-files.tar Binary files differ diff --git a/.gitignore b/.gitignore index 98ee2d2..74d21ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ tree-os.iso build -iso/boot/grub/stage2_eltorito -iso/boot/tree-os.elf \ No newline at end of file +loader.bin +tree-os.flp \ No newline at end of file diff --git a/Makefile b/Makefile index 8b250bc..b1bf9d1 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,35 @@ CC = i686-elf-gcc CCFLAGS = -m32 -mtune=generic -ffreestanding -nostdlib -c -I src/kernel LD = i686-elf-ld +LD_FLAGS = -m elf_i386 -nostdlib -T linker.ld -o AS = nasm ASFlAGS = -felf32 -GENISO = genisoimage -GENISOFLAGS = -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -input-charset utf8 -quiet -boot-info-table -A tree-os EMU = qemu-system-x86_64 -EMUFLAGS = -m 256M -cdrom +EMUFLAGS = -no-reboot -m 256M -fda +DD = dd +DD_FLAGS = status=noxfer conv=notrunc BUILD_FOLDER = build -SOURCE_FILES := $(shell find src -name *.c -or -name *.asm -or -name *.s) +SOURCE_FILES := $(shell find src/kernel -name *.c -or -name *.asm -or -name *.s) OBJS := $(SOURCE_FILES:%=$(BUILD_FOLDER)/%.o) default: run -run: tree-os.iso +run: tree-os.flp $(EMU) $(EMUFLAGS) $^ -tree-os.iso: iso/boot/tree-os.elf - $(GENISO) $(GENISOFLAGS) -o $@ iso +tree-os.flp: tree-os.bin + $(DD) $(DD_FLAGS) if=$< of=$@ -iso/boot/tree-os.elf: linker.ld $(OBJS) - $(LD) -T linker.ld -o $@ $(OBJS) +tree-os.bin: loader.bin kernel.bin + cat $^ > $@ + +kernel.bin: linker.ld $(OBJS) + $(LD) $(LD_FLAGS) $@ $(OBJS) + +loader.bin: src/boot/boot.asm + $(AS) -f bin -o $@ $< $(BUILD_FOLDER)/%.asm.o: %.asm mkdir -p $(dir $@) @@ -37,7 +44,4 @@ $(CC) $(CCFLAGS) -r $< -o $@ clean: - rm -r $(BUILD_FOLDER) tree-os.iso iso/boot/tree-os.elf - -cleanELF: - rm iso/boot/tree-os.elf \ No newline at end of file + rm -r $(BUILD_FOLDER) diff --git a/iso/boot/grub/menu.lst b/iso/boot/grub/menu.lst deleted file mode 100644 index 9c7846e..0000000 --- a/iso/boot/grub/menu.lst +++ /dev/null @@ -1,5 +0,0 @@ -default=0 -timeout=0 - -title tree-os -kernel /boot/tree-os.elf \ No newline at end of file diff --git a/kernel.bin b/kernel.bin new file mode 100755 index 0000000..d5bbc38 --- /dev/null +++ b/kernel.bin Binary files differ diff --git a/linker.ld b/linker.ld index b3c2db3..b13d161 100644 --- a/linker.ld +++ b/linker.ld @@ -1,242 +1,24 @@ -/* Script for -z combreloc -z separate-code */ -/* Copyright (C) 2014-2020 Free Software Foundation, Inc. - Copying and distribution of this script, with or without modification, - are permitted in any medium without royalty provided the copyright - notice and this notice are preserved. */ -/* file generated with ld --verbose */ OUTPUT_ARCH(i386) -ENTRY(_start) -SEARCH_DIR("=/usr/local/lib/x86_64-linux-gnu"); SEARCH_DIR("=/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu"); SEARCH_DIR("=/usr/lib/x86_64-linux-gnu64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib64"); SEARCH_DIR("=/usr/x86_64-linux-gnu/lib"); -SECTIONS -{ - PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS; - .interp : { *(.interp) } - .note.gnu.build-id : { *(.note.gnu.build-id) } - .hash : { *(.hash) } - .gnu.hash : { *(.gnu.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .gnu.version : { *(.gnu.version) } - .gnu.version_d : { *(.gnu.version_d) } - .gnu.version_r : { *(.gnu.version_r) } - .rela.dyn : - { - *(.rela.init) - *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) - *(.rela.fini) - *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) - *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) - *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) - *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) - *(.rela.ctors) - *(.rela.dtors) - *(.rela.got) - *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) - *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*) - *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*) - *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*) - *(.rela.ifunc) - } - .rela.plt : - { - *(.rela.plt) - PROVIDE_HIDDEN (__rela_iplt_start = .); - *(.rela.iplt) - PROVIDE_HIDDEN (__rela_iplt_end = .); - } - . = ALIGN(CONSTANT (MAXPAGESIZE)); - .init : - { - KEEP (*(SORT_NONE(.init))) - } - .plt : { *(.plt) *(.iplt) } -.plt.got : { *(.plt.got) } -.plt.sec : { *(.plt.sec) } - _kernel_start = .; +OUTPUT_FORMAT("binary") - .text : - { - *(.text.unlikely .text.*_unlikely .text.unlikely.*) - *(.text.exit .text.exit.*) - *(.text.startup .text.startup.*) - *(.text.hot .text.hot.*) - *(SORT(.text.sorted.*)) - *(.text .stub .text.* .gnu.linkonce.t.*) - /* .gnu.warning sections are handled specially by elf.em. */ - *(.gnu.warning) - } - .fini : - { - KEEP (*(SORT_NONE(.fini))) - } - PROVIDE (__etext = .); - PROVIDE (_etext = .); - PROVIDE (etext = .); - . = ALIGN(CONSTANT (MAXPAGESIZE)); - /* Adjust the address for the rodata segment. We want to adjust up to - the same address within the page on the next page up. */ - . = SEGMENT_START("rodata-segment", ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1))); - .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) } - .rodata1 : { *(.rodata1) } - .eh_frame_hdr : { *(.eh_frame_hdr) *(.eh_frame_entry .eh_frame_entry.*) } - .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) } - .gnu_extab : ONLY_IF_RO { *(.gnu_extab*) } - /* These sections are generated by the Sun/Oracle C++ compiler. */ - .exception_ranges : ONLY_IF_RO { *(.exception_ranges*) } - /* Adjust the address for the data segment. We want to adjust up to - the same address within the page on the next page up. */ - . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE)); - /* Exception handling */ - .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) *(.eh_frame.*) } - .gnu_extab : ONLY_IF_RW { *(.gnu_extab) } - .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) } - .exception_ranges : ONLY_IF_RW { *(.exception_ranges*) } - /* Thread Local Storage sections */ - .tdata : - { - PROVIDE_HIDDEN (__tdata_start = .); - *(.tdata .tdata.* .gnu.linkonce.td.*) - } - .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) } - .preinit_array : - { - PROVIDE_HIDDEN (__preinit_array_start = .); - KEEP (*(.preinit_array)) - PROVIDE_HIDDEN (__preinit_array_end = .); - } - .init_array : - { - PROVIDE_HIDDEN (__init_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) - KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors)) - PROVIDE_HIDDEN (__init_array_end = .); - } - .fini_array : - { - PROVIDE_HIDDEN (__fini_array_start = .); - KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) - KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors)) - PROVIDE_HIDDEN (__fini_array_end = .); - } - .ctors : - { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) - KEEP (*(SORT(.ctors.*))) - KEEP (*(.ctors)) - } - .dtors : - { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } - .jcr : { KEEP (*(.jcr)) } - .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) } - .dynamic : { *(.dynamic) } - .got : { *(.got) *(.igot) } - . = DATA_SEGMENT_RELRO_END (SIZEOF (.got.plt) >= 24 ? 24 : 0, .); - .got.plt : { *(.got.plt) *(.igot.plt) } - .data : - { - *(.data .data.* .gnu.linkonce.d.*) - SORT(CONSTRUCTORS) - } - .data1 : { *(.data1) } - _edata = .; PROVIDE (edata = .); - . = .; - __bss_start = .; - .bss : - { - *(.dynbss) - *(.bss .bss.* .gnu.linkonce.b.*) - *(COMMON) - /* Align here to ensure that the .bss section occupies space up to - _end. Align after .bss to ensure correct alignment even if the - .bss section disappears because there are no input sections. - FIXME: Why do we need it? When there is no .bss section, we do not - pad the .data section. */ - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - .lbss : - { - *(.dynlbss) - *(.lbss .lbss.* .gnu.linkonce.lb.*) - *(LARGE_COMMON) - } - . = ALIGN(64 / 8); - . = SEGMENT_START("ldata-segment", .); - .lrodata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.lrodata .lrodata.* .gnu.linkonce.lr.*) - } - .ldata ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1)) : - { - *(.ldata .ldata.* .gnu.linkonce.l.*) - . = ALIGN(. != 0 ? 64 / 8 : 1); - } - . = ALIGN(64 / 8); - _end = .; - PROVIDE (end = .); - . = DATA_SEGMENT_END (.); - /* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } - .gnu.build.attributes : { *(.gnu.build.attributes .gnu.build.attributes.*) } - /* DWARF debug sections. - Symbols in the DWARF debugging sections are relative to the beginning - of the section so we begin them at 0. */ - /* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } - /* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } - /* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } - /* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } - /* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } - /* DWARF 3 */ - .debug_pubtypes 0 : { *(.debug_pubtypes) } - .debug_ranges 0 : { *(.debug_ranges) } - /* DWARF Extension. */ - .debug_macro 0 : { *(.debug_macro) } - .debug_addr 0 : { *(.debug_addr) } - .gnu.attributes 0 : { KEEP (*(.gnu.attributes)) } - _kernel_end = .; - /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) } +SECTIONS { + . = 0x7E00; + + .text BLOCK(4K) : ALIGN(4K) { + *(.entry) + *(.text) + } + + .rodata BLOCK(4K) : ALIGN(4K) { + *(.rodata) + } + + .data BLOCK(4K) : ALIGN(4K) { + *(.data) + } + + .bss BLOCK(4K) : ALIGN(4K) { + *(COMMON) + *(.bss) + } } \ No newline at end of file diff --git a/src/boot/___boot.asm b/src/boot/___boot.asm new file mode 100644 index 0000000..bccd64a --- /dev/null +++ b/src/boot/___boot.asm @@ -0,0 +1,263 @@ +org 0x7c00 +bits 16 + +%ifndef SECTOR_CNT + %define SECTOR_CNT 3 +%endif + +jmp 0x0000:mbr_start + +mbr_start: + cli + cld + + ; Clear rest of segments + xor ax, ax + mov ds, ax + mov es, ax + + mov ss, ax + mov sp, 0x7c00 + mov bp, sp + + ; Store boot device + push dx + + ; swap to unreal mode + push ds + lgdt [gdtEnd] + mov eax, cr0 + or al, 1 + mov cr0, eax + jmp .pmode +.pmode: + mov bx, 0x08 + mov ds, bx + and al, 0xFE + mov cr0, eax + pop ds + sti + + ; Clear screen + xor ax, ax + mov al, 0x03 + int 0x10 + + ; Initialize serial port + mov dx, 0x3F9 + xor al, al + out dx, al + + add dx, 2 + mov al, 0x80 + out dx, al + + sub dx, 3 + xor al, al + out dx, al + + inc dx + out dx, al + + add dx, 2 + add al, 3 + out dx, al + + dec dx + mov al, 0xC7 + out dx, al + + add dx, 2 + mov al, 0x0B + out dx, al + +load_second_stage: + pop dx + push dx + mov bx, loader_entry + xor ch, ch + mov cl, 0x02 + xor dh, dh + mov byte [.sectors_to_load], SECTOR_CNT + + .read_start: + mov di, 5 + .read: + mov ah, 0x02 + mov al, byte [.sectors_to_load] + int 0x13 + jc .retry + cmp al, byte [.sectors_to_load] + je .done + sub byte [.sectors_to_load], al + mov cl, 0x01 + xor dh, 1 + jnz .read_start + inc ch + jmp .read_start + .retry: + ; disk read failed, reset disk + xor ah, ah + int 0x13 + dec di + jnz .read + mov si, .msg_diskread_fail + jmp .fail + .done: + jmp loader_entry + .fail: + lodsb + or al, al + mov ah, 0x0E + int 0x10 + test al, al + jnz .fail + .hang: + cli + hlt + jmp .hang + +.sectors_to_load: + db 1 +.msg_diskread_fail: + db "HALT: Failed to read boot disk", 0x0A, 0x0D, 0 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +; Padding & Signature +times 510-($-$$) db 0 +dw 0xAA55 + +loader_entry equ 0x7e00 + +bits 32 +_start: + ; mov ax, 16 + ; mov ds, ax + ; mov ss, ax + ; mov es, ax + ; mov fs, ax + ; mov gs, ax + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp x +x: + mov dword [0xb8000], 0x07690748 + jmp $ + mov ebx, hello + call printString +dump: + mov ebx, 0x7e00 + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + + +old: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/boot/__boot.asm b/src/boot/__boot.asm new file mode 100644 index 0000000..6c2e1be --- /dev/null +++ b/src/boot/__boot.asm @@ -0,0 +1,278 @@ +; bootp.asm Bootstrap for protected mode image +; Version 1.0, Dec 14, 1997 +; Sample code +; by John S. Fine johnfine@erols.com +; I do not place any restrictions on your use of this source code +; I do not provide any warranty of the correctness of this source code +;_____________________________________________________________________________ +; +; This program should be useable for three purposes: +; +; 1) Use it unmodified, in the boot sector of a floppy, to load a test image +; from the hard drive. In many environments, a floppy boot is the easiest +; way to have a clean boot for a pmode test. By getting the image itself +; from the hard drive, you eliminate most of the delays and restrictions of +; a floppy boot. +; +; 2) As a bootstrap tutorial, it demonstrates many of the steps required by +; a protected mode bootstrap, and many tricks for keeping code size down +; in a bootstrap. +; +; 3) As the basis for a protected mode, partition level, bootstrap. Just +; remove some of the steps that aren't needed in your environment, and add +; some error messages for unsupported conditions. +;_____________________________________________________________________________ +; +; Documentation: +; +; 1) Most of the documentation is in comments at the END of this file +; 2) Some of the documentation is in a separate file: BOOTP.TXT +; 3) In the comments within the source code are several uses of {}, each +; of these indicates a section of documentation at the end of this file +; which applies to that part of the source code. +; 4) The major way I reduced code size was to rely on left-over values in +; registers and/or to initialize registers at points where doing so was +; cheap. To make that understandable, there are several lines beginning +; with either ";>" or ";>>". These lines describe the register contents +; at that point in the code. ";>" indicates that the value is required +; by the next section of code. ";>>" indicates that the value passes +; through the next section unmodified and is needed later. +;_____________________________________________________________________________ + + %include "gdt.inc" + + struc DISK_PARM ;BIOS hard disk parameters +DP_cyl resw 1 +DP_heads resb 1 + resb 5 ;Obsolete +DP_ctrl resb 1 + resb 5 ;Obsolete +DP_sect resb 1 + resb 1 + endstruc + + struc PT_ENT ;Partition table entry +BootFlag resb 1 +BeginHead resb 1 +BeginSector resb 1 +BeginCyl resb 1 +SystemID resb 1 +EndHead resb 1 +EndSector resb 1 +EndCyl resb 1 +RelSectorLow resw 1 +RelSectorHigh resw 1 +NumSectorsLow resw 1 +NumSectorsHigh resw 1 + endstruc + + struc BB ;Partition Boot block + resb 0xD ;Things we ignore +BB_clu resb 1 ;Sectors per cluster +BB_res resw 1 ;Reserved sectors +BB_fats resb 1 ;Number of FATs +BB_root resw 1 ;Root directory entries + resb 3 ;Things we ignore +BB_fat resw 1 ;Sectors per fat + endstruc + +max_xfer equ 0x78 ;Maximum number of sectors to transfer at once + ;Must be divisible by 8 + + +SEGMENT START USE16 + jmp short start ;Standard start of boot sector + nop + resb 0x3B ;Skip over parameters (set by format) + +start: + cli + lgdt [cs:gdt] + mov al, 0x80 + mov es, ax + + xor eax, eax ;Segment and LBN + mov ds, ax ;{1B} + mov ss, ax + mov sp, start + + xor edi, edi + inc di + call read_sectors + +;> eax = 0 +;> edi = 1 +;> ds = 0 +;> es = 80 + + mov si, 0x9BE-PT_ENT_size ;Point at partition table +.10: add si, byte PT_ENT_size + cmp [si+BootFlag], al ;Active? + je .10 ;No + jg boot_error + mov eax, [si+RelSectorLow] ;LBN sector number of partition + call read_sectors ;Read partition boot block + +;> eax = LBN of partition +;> ebx = 0000???? +;> edi = 0000???? +;> ds = 0 + + mov bp, 0x800 ;Point at partition boot block + mov bx, [bp+BB_res] ;Sectors before first FAT + add eax, ebx ;LBN of FAT + push eax ;Need to read the FAT later + movzx bx, [bp+BB_fats] ;Number of FATs + imul bx, [bp+BB_fat] ;Times size of FAT + add eax, ebx ;LBN of Root directory + mov di, [bp+BB_root] ;Root directory entries + push di ; used again later + dec di ;Convert to number of sectors + shr di, 4 ; 16 directory entries per sector + inc di + mov es, bp ;Read directory at 800:0 + call read_sectors + add eax, edi ;LBN of cluster 2 + +;>> [sp+2] = LBN of FAT +;> [sp] = Number of root directory entries +;>> eax = LBN of cluster 2 +;> edi = 0000???? +;>> bp = 800 +;> ds = 0 +;> es = 800 + + pop bx ;Root directory entries + xor di, di ;Point at directory {1C} +.20: mov si, file_name ;Name of file we want + xor ecx, ecx + mov cl, 11 + a32 rep cmpsb ;Found the file? + je found ;Yes + add cl, 21 ;Offset to next directory entry + add edi, ecx ;Advance to next entry + dec bx ;Loop through all entries + jnz .20 + + ;Couldn't find file in directory +boot_error: +disk_error: + mov ax, 0xE07 ;{3} + int 10h + jmp short $ + +;> [sp] = LBN of FAT +;> eax = LBN of cluster 2 +;> es:edi = Pointer to end of name within directory entry +;> bp = 800 +;> ds = 0 +;> es = 800 + +eof: + mov bh, 0x9C + mov es, bx ;es = 9C00 + xor di, di ;{1E} Address of page tables WRT es + mov bh, 4096/256 ;ebx = 4096 +.10: mov ch, 4 ;cx = 1024 + mov al, 7 +.20: stosd + add eax, ebx + int 8 ;{8} + loop .20 + shr eax, 2 ;{4C} (first time only) 4Mb / 4 = 1Mb + shr si, 1 ;Done just one page? + jo .10 ;Yes + + cli ;{6} + + mov eax, 0x9C007 ;First page tbl pointer in page dir + stosd ;{1H} + mov ax, (1024-3)*2 + xchg ax, cx + rep stosw + mov ax, 0xD007 ;0FF800000 page tbl pointer + stosd ;{1F} + mov ah, 0xE0 ;Page directory self pointer + stosd ;{1G} + mov al, 0 + mov CR3, eax ;Set up page directory + mov eax, CR0 ;Turn on paging and protected mode + or eax, 0x80000001 + mov CR0, eax + mov cl, flat_data ;Setup ds and es + push cx ;{5} + pop ds + mov es, cx + jmp dword 8:0xFF800000 ;Go + + read_sectors: +; Input: +; EAX = LBN +; DI = sector count +; ES = segment +; Output: +; BL = low byte of ES +; EBX high half cleared +; DL = 0x80 +; EDX high half cleared +; ESI = 0 +; Clobbered: +; BH, CX, DH + + push eax + push di + push es + +.10: push eax ;LBN + + push ds + lds si, [0x104] ;Hard drive 0 parameters + cdq ;edx = 0 + movzx ebx, byte [si+DP_sect] + div ebx ;EAX=track ;EDX=sector-1 + mov cx, dx ;CL=sector-1 ;CH=0 + inc cx ;CL=Sector number + xor dx, dx + mov bl, [si+DP_heads] + pop ds + div ebx + + mov dh, dl + mov dl, 0x80 + xchg ch, al + shr ax, 2 + or cl, al + mov al, max_xfer ;AX = Maximum sectors to xfer + cmp ax, di ;Total is larger? + jb .20 ;Yes: transfer maximum + xchg ax, di ;No: AX=total ;DI=maximum +.20: mov ah, 2 ;Read + xor bx, bx + int 13h + jc near disk_error + + mov bx, es + add bh, max_xfer>>3 ;Advance segment + mov es, bx + + pop eax + add eax, byte max_xfer + sub di, byte max_xfer + ja .10 + + pop es + pop di + pop eax + xor esi, esi + ret + +gdt start_gdt + +flat_code desc 0, 0xFFBFF, D_CODE+D_READ+D_BIG+D_BIG_LIM + +flat_data desc 0, 0xFFFFF, D_DATA+D_WRITE+D_BIG+D_BIG_LIM + + end_gdt + + resb 0x1FE+$$-$ + db 0x55, 0xAA diff --git a/src/boot/boot.asm b/src/boot/boot.asm new file mode 100644 index 0000000..19a0342 --- /dev/null +++ b/src/boot/boot.asm @@ -0,0 +1,213 @@ +BITS 16 +ORG 0x7C00 + +section .text + +loadOS: + mov [BOOT_DRIVE], dl + + xor ax, ax + mov ds, ax + mov es, ax + mov ss, ax + mov fs, ax + mov gs, ax + + mov bp, 0x9000 + mov sp, bp +resetDisk: + xor ah, ah + mov dl, [BOOT_DRIVE] + ; int 0x13 +load: + ; mov ah, 0x02 + ; mov al, 1 + ; mov cx, 0x01 + ; mov bx, CODE_SEG + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov dl, [BOOT_DRIVE] + ; mov dh, 0x00 + ; int 0x13 + ; jc error + ; jmp dump + ; mov ah, 0x42 + ; mov dl, [BOOT_DRIVE] + ; mov cx, 0x00 + ; mov ds, cx + ; mov si, diskAddrebx + ; mov eax, 0x0001 + ; mov dl, [BOOT_DRIVE] + ; mov di, KERNEL_POSITION + ; mov cx, 0x01 + +diskLoad: + ; mov ah, 0x02 ; read + ; mov al, 0x01 ; sector count + ; mov cx, 0x0002 ; cylinder & sector + ; mov dh, 0x00 ; head + ; mov dl, [BOOT_DRIVE] ; drive + ; ; xor bx, bx ; position + ; ; mov es, bx + ; xor bx, bx + ; mov es, bx + ; mov bx, KERNEL_POSITION + ; mov ah, 0x42 + ; mov dx, DAP + ; mov si, DAP + ; xor dh, dh + mov dl, [BOOT_DRIVE] + mov ah, 0x02 ;Read Disk Sectors + mov al, 0x05 ;Read one sector only + mov ch, 0x00 ;Track 0 + mov cl, 0x02 ;Sector 2 + mov dh, 0x00 ;Head 0 + ; mov dl, 0x00 ;Drive 0 (Floppy 1) + ; mov bx, 0x800 ;Segment 0x2000 + ; mov es, bx + mov bx, KERNEL_POSITION ;Start of Segment + + int 0x13 + + mov [errorByte], ah + +switchToX86: + cli + + in al, 0x70 + or al, 0x08 + out 0x70, al + + lgdt [gdtEnd] + + mov eax, cr0 + or eax, 0x01 + mov cr0, eax + + jmp CODE_SEG:_start + +bits 32 +_start: + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov al, [BOOT_DRIVE] + call print_hex_byte + mov ebx, hello + call printString + mov al, [errorByte] + call print_hex_byte +dump: + mov ebx, KERNEL_POSITION + mov cl, 5 +.loop: + mov al, [ebx] + call print_hex_byte + inc ebx + dec cl + jnz .loop + jmp $ + +printString: + push ebx + push ax +.loop: + mov al, [ebx] + or al, al + jz .return + call printChar + inc ebx + jmp .loop +.return: + pop ax + pop ebx + ret + + +print_hex_byte: + mov [.temp],al + shr al,4 + cmp al,10 + sbb al,69h + das + + call printChar + + mov al,[.temp] + ror al,4 + shr al,4 + cmp al,10 + sbb al,69h + das + call printChar + + ret +.temp db 0x00 + +printChar: + push ebx + mov ebx, [.position] + mov [ebx], byte al + inc ebx + mov [ebx], byte 0x07 + inc ebx + mov [.position], ebx + pop ebx + ret + +.position dd 0xb8000 + +ALIGN 4 + +gdt32: + dq 0 + +code: + dw 0xffff + dw 0 + db 0 + db 10011010b + db 11001111b + db 0 + +data: + dw 0xffff + dw 0 + db 0 + db 10010010b + db 11001111b + db 0 +gdtEnd: + dw gdtEnd - gdt32 - 1 + dd gdt32 + +CODE_SEG equ 8 +DATA_SEG equ 16 + +BOOT_DRIVE: db 0 +ALIGN 4 +DAP: +.size db 0x10 +.reserved db 0x00 +.sectors dw 0x0001 +.offset dw KERNEL_POSITION +.segment dw 0x0000 +.lbaLsd dd 0x00000000 +.lbaMsd dd 0x00000002 + +errorByte db 0x00 + +hello db 'hello world! ', 0x00 +errorMessage db 'error! ', 0x00 + + + +padding: + times 510-($-$$) db 0x00 + dw 0xAA55 + +KERNEL_POSITION equ 0x7e00 +KERNEL_SIZE equ 1 \ No newline at end of file diff --git a/src/kernel/entry.asm b/src/kernel/entry.asm new file mode 100644 index 0000000..99ceb59 --- /dev/null +++ b/src/kernel/entry.asm @@ -0,0 +1,25 @@ +section .entry +extern kernelMain +bits 32 +_start: + nop + nop + mov ax, 16 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + mov dword [0xb8000], 0x07690748 + jmp $ + +hello db 'hello world! ', 0x00 + + +db 0xB1, 0x05, 0xF0, 0x0D ; biosfood + +CODE_SEG equ 0x08 + +dq 0x00 + +stackTop: \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index cfdf333..21c60b4 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -9,11 +9,9 @@ extern uint32_t _kernel_end; void kernelMain() { + puts("hello world"); initMemoryAllocation(0x100000); // initializing stacks after the kernel seems not to work :( otherwise, _kernel_end should be passed here initOSTasks(); drawLogo(); - printf("Switching to otherTask... \n"); - yields(); - printf("Returned to mainTask!\n\n"); yield(); } \ No newline at end of file diff --git a/src/kernel/lib/task/task.c b/src/kernel/lib/task/task.c index 5d57403..6163772 100644 --- a/src/kernel/lib/task/task.c +++ b/src/kernel/lib/task/task.c @@ -29,6 +29,7 @@ puts("No more tasks to run, halting . . .\n"); while (1) { asm("hlt"); + puts("interrupt!\n"); } } last->nextTask = 0x00; diff --git a/src/kernel/multiboot.h b/src/kernel/multiboot.h deleted file mode 100644 index c6f5ff8..0000000 --- a/src/kernel/multiboot.h +++ /dev/null @@ -1,274 +0,0 @@ -/* multiboot.h - Multiboot header file. */ -/* Copyright (C) 1999,2003,2007,2008,2009,2010 Free Software Foundation, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY - * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR - * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifndef MULTIBOOT_HEADER -#define MULTIBOOT_HEADER 1 - -/* How many bytes from the start of the file we search for the header. */ -#define MULTIBOOT_SEARCH 8192 -#define MULTIBOOT_HEADER_ALIGN 4 - -/* The magic field should contain this. */ -#define MULTIBOOT_HEADER_MAGIC 0x1BADB002 - -/* This should be in %eax. */ -#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002 - -/* Alignment of multiboot modules. */ -#define MULTIBOOT_MOD_ALIGN 0x00001000 - -/* Alignment of the multiboot info structure. */ -#define MULTIBOOT_INFO_ALIGN 0x00000004 - -/* Flags set in the ’flags’ member of the multiboot header. */ - -/* Align all boot modules on i386 page (4KB) boundaries. */ -#define MULTIBOOT_PAGE_ALIGN 0x00000001 - -/* Must pass memory information to OS. */ -#define MULTIBOOT_MEMORY_INFO 0x00000002 - -/* Must pass video information to OS. */ -#define MULTIBOOT_VIDEO_MODE 0x00000004 - -/* This flag indicates the use of the address fields in the header. */ -#define MULTIBOOT_AOUT_KLUDGE 0x00010000 - -/* Flags to be set in the ’flags’ member of the multiboot info structure. */ - -/* is there basic lower/upper memory information? */ -#define MULTIBOOT_INFO_MEMORY 0x00000001 -/* is there a boot device set? */ -#define MULTIBOOT_INFO_BOOTDEV 0x00000002 -/* is the command-line defined? */ -#define MULTIBOOT_INFO_CMDLINE 0x00000004 -/* are there modules to do something with? */ -#define MULTIBOOT_INFO_MODS 0x00000008 - -/* These next two are mutually exclusive */ - -/* is there a symbol table loaded? */ -#define MULTIBOOT_INFO_AOUT_SYMS 0x00000010 -/* is there an ELF section header table? */ -#define MULTIBOOT_INFO_ELF_SHDR 0X00000020 - -/* is there a full memory map? */ -#define MULTIBOOT_INFO_MEM_MAP 0x00000040 - -/* Is there drive info? */ -#define MULTIBOOT_INFO_DRIVE_INFO 0x00000080 - -/* Is there a config table? */ -#define MULTIBOOT_INFO_CONFIG_TABLE 0x00000100 - -/* Is there a boot loader name? */ -#define MULTIBOOT_INFO_BOOT_LOADER_NAME 0x00000200 - -/* Is there a APM table? */ -#define MULTIBOOT_INFO_APM_TABLE 0x00000400 - -/* Is there video information? */ -#define MULTIBOOT_INFO_VBE_INFO 0x00000800 -#define MULTIBOOT_INFO_FRAMEBUFFER_INFO 0x00001000 - -#ifndef ASM_FILE - -typedef unsigned char multiboot_uint8_t; -typedef unsigned short multiboot_uint16_t; -typedef unsigned int multiboot_uint32_t; -typedef unsigned long long multiboot_uint64_t; - -struct multiboot_header -{ - /* Must be MULTIBOOT_MAGIC - see above. */ - multiboot_uint32_t magic; - - /* Feature flags. */ - multiboot_uint32_t flags; - - /* The above fields plus this one must equal 0 mod 2^32. */ - multiboot_uint32_t checksum; - - /* These are only valid if MULTIBOOT_AOUT_KLUDGE is set. */ - multiboot_uint32_t header_addr; - multiboot_uint32_t load_addr; - multiboot_uint32_t load_end_addr; - multiboot_uint32_t bss_end_addr; - multiboot_uint32_t entry_addr; - - /* These are only valid if MULTIBOOT_VIDEO_MODE is set. */ - multiboot_uint32_t mode_type; - multiboot_uint32_t width; - multiboot_uint32_t height; - multiboot_uint32_t depth; -}; - -/* The symbol table for a.out. */ -struct multiboot_aout_symbol_table -{ - multiboot_uint32_t tabsize; - multiboot_uint32_t strsize; - multiboot_uint32_t addr; - multiboot_uint32_t reserved; -}; -typedef struct multiboot_aout_symbol_table multiboot_aout_symbol_table_t; - -/* The section header table for ELF. */ -struct multiboot_elf_section_header_table -{ - multiboot_uint32_t num; - multiboot_uint32_t size; - multiboot_uint32_t addr; - multiboot_uint32_t shndx; -}; -typedef struct multiboot_elf_section_header_table multiboot_elf_section_header_table_t; - -struct multiboot_info -{ - /* Multiboot info version number */ - multiboot_uint32_t flags; - - /* Available memory from BIOS */ - multiboot_uint32_t mem_lower; - multiboot_uint32_t mem_upper; - - /* "root" partition */ - multiboot_uint32_t boot_device; - - /* Kernel command line */ - multiboot_uint32_t cmdline; - - /* Boot-Module list */ - multiboot_uint32_t mods_count; - multiboot_uint32_t mods_addr; - - union - { - multiboot_aout_symbol_table_t aout_sym; - multiboot_elf_section_header_table_t elf_sec; - } u; - - /* Memory Mapping buffer */ - multiboot_uint32_t mmap_length; - multiboot_uint32_t mmap_addr; - - /* Drive Info buffer */ - multiboot_uint32_t drives_length; - multiboot_uint32_t drives_addr; - - /* ROM configuration table */ - multiboot_uint32_t config_table; - - /* Boot Loader Name */ - multiboot_uint32_t boot_loader_name; - - /* APM table */ - multiboot_uint32_t apm_table; - - /* Video */ - multiboot_uint32_t vbe_control_info; - multiboot_uint32_t vbe_mode_info; - multiboot_uint16_t vbe_mode; - multiboot_uint16_t vbe_interface_seg; - multiboot_uint16_t vbe_interface_off; - multiboot_uint16_t vbe_interface_len; - - multiboot_uint64_t framebuffer_addr; - multiboot_uint32_t framebuffer_pitch; - multiboot_uint32_t framebuffer_width; - multiboot_uint32_t framebuffer_height; - multiboot_uint8_t framebuffer_bpp; -#define MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED 0 -#define MULTIBOOT_FRAMEBUFFER_TYPE_RGB 1 -#define MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT 2 - multiboot_uint8_t framebuffer_type; - union - { - struct - { - multiboot_uint32_t framebuffer_palette_addr; - multiboot_uint16_t framebuffer_palette_num_colors; - }; - struct - { - multiboot_uint8_t framebuffer_red_field_position; - multiboot_uint8_t framebuffer_red_mask_size; - multiboot_uint8_t framebuffer_green_field_position; - multiboot_uint8_t framebuffer_green_mask_size; - multiboot_uint8_t framebuffer_blue_field_position; - multiboot_uint8_t framebuffer_blue_mask_size; - }; - }; -}; -typedef struct multiboot_info multiboot_info_t; - -struct multiboot_color -{ - multiboot_uint8_t red; - multiboot_uint8_t green; - multiboot_uint8_t blue; -}; - -struct multiboot_mmap_entry -{ - multiboot_uint32_t size; - multiboot_uint64_t addr; - multiboot_uint64_t len; -#define MULTIBOOT_MEMORY_AVAILABLE 1 -#define MULTIBOOT_MEMORY_RESERVED 2 -#define MULTIBOOT_MEMORY_ACPI_RECLAIMABLE 3 -#define MULTIBOOT_MEMORY_NVS 4 -#define MULTIBOOT_MEMORY_BADRAM 5 - multiboot_uint32_t type; -} __attribute__((packed)); -typedef struct multiboot_mmap_entry multiboot_memory_map_t; - -struct multiboot_mod_list -{ - /* the memory used goes from bytes ’mod_start’ to ’mod_end-1’ inclusive */ - multiboot_uint32_t mod_start; - multiboot_uint32_t mod_end; - - /* Module command line */ - multiboot_uint32_t cmdline; - - /* padding to take it to 16 bytes (must be zero) */ - multiboot_uint32_t pad; -}; -typedef struct multiboot_mod_list multiboot_module_t; - -/* APM BIOS info. */ -struct multiboot_apm_info -{ - multiboot_uint16_t version; - multiboot_uint16_t cseg; - multiboot_uint32_t offset; - multiboot_uint16_t cseg_16; - multiboot_uint16_t dseg; - multiboot_uint16_t flags; - multiboot_uint16_t cseg_len; - multiboot_uint16_t cseg_16_len; - multiboot_uint16_t dseg_len; -}; - -#endif /* ! ASM_FILE */ - -#endif /* ! MULTIBOOT_HEADER */ \ No newline at end of file diff --git a/src/multiboot/boot.asm b/src/multiboot/boot.asm deleted file mode 100644 index 965b8e1..0000000 --- a/src/multiboot/boot.asm +++ /dev/null @@ -1,25 +0,0 @@ -global loader -MAGIC_NUMBER equ 0x1BADB002 ; 1 bad boot -FLAGS equ 0x0 -CHECKSUM equ -MAGIC_NUMBER -section .text -align 4 - dd MAGIC_NUMBER - dd FLAGS - dd CHECKSUM -extern kernelMain - -KERNEL_STACK_SIZE equ 4096 ; 4kb of stack -global _start -_start: - ; setup stack for kernel main function - mov esp, stack + KERNEL_STACK_SIZE - ; call main function - call kernelMain -.loop: - jmp .loop - -section .bss -align 4 -stack: - resb KERNEL_STACK_SIZE \ No newline at end of file diff --git a/tree-os-files.tar b/tree-os-files.tar new file mode 100644 index 0000000..f68d3c0 --- /dev/null +++ b/tree-os-files.tar Binary files differ diff --git a/tree-os.bin b/tree-os.bin new file mode 100644 index 0000000..3eac878 --- /dev/null +++ b/tree-os.bin Binary files differ