[boot] holy crap

This commit is contained in:
Kevin Lange 2011-02-21 03:11:30 -06:00
parent e74247cd18
commit c3c63db8e9
7 changed files with 144 additions and 97 deletions

5
.gitignore vendored
View File

@ -2,7 +2,6 @@ toaruos-kernel
toaruos-initrd
*.o
*.swp
bootloader/stage1
bootloader/stage2
bootloader/stage2.s
bootloader/stage1.bin
bootloader/stage2.bin
initrd/boot

View File

@ -1,5 +1,6 @@
#CC = gcc
CC = clang
GCC = gcc
CFLAGS = -Wall -Wextra -pedantic -m32 -O0 -std=c99 -finline-functions -fno-stack-protector -nostdinc -ffreestanding -Wno-unused-function -Wno-unused-parameter
LD = ld -m elf_i386
NASM = nasm
@ -45,11 +46,11 @@ kernel/start.o: kernel/start.asm
################
# Ram disk #
################
toaruos-initrd: initrd bootloader/stage1 initrd/boot/stage2 initrd/boot/kernel
toaruos-initrd: initrd bootloader/stage1.bin initrd/boot/kernel
@${ECHO} -n "\033[32m initrd Generating initial RAM disk\033[0m"
@-rm -f toaruos-initrd
@${GENEXT} -d initrd -q -b 249 toaruos-initrd
@${DD} if=bootloader/stage1 of=toaruos-initrd 2>/dev/null
@${DD} if=bootloader/stage1.bin of=toaruos-initrd 2>/dev/null
@${ECHO} "\r\033[32;1m initrd Generated initial RAM disk image\033[0m"
### Ram Disk installers...
@ -60,27 +61,27 @@ initrd/boot/kernel: toaruos-kernel
@cp toaruos-kernel initrd/boot/kernel
# Second-stage bootloader
initrd/boot/stage2: bootloader/stage2
initrd/boot/stage2: bootloader/stage2.bin
@mkdir -p initrd/boot
@cp bootloader/stage2 initrd/boot/stage2
@cp bootloader/stage2.bin initrd/boot/stage2
################
# Bootloader #
################
bootloader/stage1: bootloader/stage1.s
@${ECHO} -n "\033[32m nasm $<\033[0m"
@${NASM} -f bin -o $@ $<
@${ECHO} "\r\033[32;1m nasm $<\033[0m"
bootloader/stage1/main.o: bootloader/stage1/main.c
@${ECHO} -n "\033[32m CC $<\033[0m"
@${GCC} ${CFLAGS} -c -o $@ $<
@${ECHO} "\r\033[32;1m CC $<\033[0m"
bootloader/stage2: bootloader/stage2.s
bootloader/stage1/start.o: bootloader/stage1/start.s
@${ECHO} -n "\033[32m yasm $<\033[0m"
@${YASM} -p gas -f bin -w: -o $@ $<
@${YASM} -f elf32 -p gas -o $@ $<
@${ECHO} "\r\033[32;1m yasm $<\033[0m"
bootloader/stage2.s: bootloader/stage2.c
@${ECHO} -n "\033[32m CC $<\033[0m"
@${CC} ${CFLAGS} -S -o $@ $<
@${ECHO} "\r\033[32;1m CC $<\033[0m"
bootloader/stage1.bin: bootloader/stage1/main.o bootloader/stage1/start.o bootloader/stage1/link.ld
@${ECHO} -n "\033[32m ld $<\033[0m"
@${LD} -o bootloader/stage1.bin -T bootloader/stage1/link.ld bootloader/stage1/start.o bootloader/stage1/main.o
@${ECHO} "\r\033[32;1m ld $<\033[0m"
clean:
@${ECHO} -n "\033[31m RM Cleaning... \033[0m"
@ -89,9 +90,8 @@ clean:
@-rm -f kernel/*.o
@-rm -f kernel/core/*.o
@-rm -f kernel/core/fs/*.o
@-rm -f bootloader/stage1
@-rm -f bootloader/stage2
@-rm -f bootloader/stage2.s
@-rm -f bootloader/stage1.bin
@-rm -f bootloader/stage1/*.o
@-rm -f initrd/boot/stage2
@-rm -f initrd/boot/kernel
@${ECHO} "\r\033[31;1m RM Finished cleaning.\033[0m\033[K"

View File

@ -1,49 +0,0 @@
; Mr. Boots - Stage 1
; Find Stage 2 and immediately load it.
;
; NOTICE: This stage should be loaded from a partition on
; an EXT2-only disk without a partition table.
; If you want to use it with a different set up
; you need to patch it to include an MBR header
; and all the other necessary bits.
;
; Part of the ToAruOS Distribution of the ToAru Kernel
;
; NCSA license is available from the root directory of the
; source tree in which this file is shipped.
;
;
[BITS 16] ; 16-bit Boot Loader
[ORG 0x7C00] ; Start point
start:
mov ax, 0x00 ; Initialize data segment
mov ds, ax ; ...
mov si, bmsg ; Print "Starting..."
call print ; ...
;mov ax, 0x4F02 ; VESA function "Set Video Mode"
;mov bx, 0x0107 ; 1024x768x256
;int 0x10 ; BIOS video interrupt
jmp $ ; sadloop
print:
mov ah, 0x0E ; set registers for
mov bh, 0x00 ; bios video call
mov bl, 0x07
.next:
lodsb ; string stuff
or al,al ; if 0...
jz .return ; return
int 0x10 ; print character
jmp .next ; continue
.return:
ret
bmsg db 'Starting...',13,10,0
; Boot magic
times 510-($-$$) db 0
dw 0xAA55

36
bootloader/stage1/link.ld Normal file
View File

@ -0,0 +1,36 @@
OUTPUT_FORMAT(binary)
/*
OUTPUT_FORMAT(elf32-i386)
*/
ENTRY(_start)
load = 0x7c00;
phys = 0x0;
boot = 0x1FE;
SECTIONS
{
.text load : AT(phys)
{
*(.text)
. = ALIGN(4);
}
.data : AT(phys + SIZEOF(.text))
{
*(.data)
. = ALIGN(4);
}
.rodata : AT (phys + SIZEOF(.text) + SIZEOF(.data))
{
*(.rodata)
}
.bootsig : AT(boot)
{
SHORT(0xaa55)
}
/DISCARD/ :
{
*(.bss)
*(.comment)
*(.eh_frame)
*(.note.gnu.build-id)
}
}

27
bootloader/stage1/main.c Normal file
View File

@ -0,0 +1,27 @@
/*
* Mr. Boots Stage 2
*/
/*
* 16-bit bootloader
*/
__asm__(".code16gcc\n");
void kprint(short s)
{
__asm__ __volatile__ ("movw %0, %%si\n"
"call _print" : : "l" ((short)s));
}
/*
* Main entry point
*/
int main() {
char s[] = "Herp Derp\r\n";
__asm__ __volatile__ ("movw %0, %%si\n"
"call _print" : : "l" ((short)(int)s));
kprint((short)(int)"Derp\r\n");
kprint((short)(int)"Herp\r\n");
//while (1) {};
return 0;
}

62
bootloader/stage1/start.s Normal file
View File

@ -0,0 +1,62 @@
# Mr. Boots - Stage 1
# Find Stage 2 and immediately load it.
#
# NOTICE: This stage should be loaded from a partition on
# an EXT2-only disk without a partition table.
# If you want to use it with a different set up
# you need to patch it to include an MBR header
# and all the other necessary bits.
#
# Part of the ToAruOS Distribution of the ToAru Kernel
#
# NCSA license is available from the root directory of the
# source tree in which this file is shipped.
#
#
.code16
.text
.global _start
.global _print
_start:
movw $0x00,%ax # Initialize data segment
movw %ax,%ds # ...
movw %ax,%es
movw %ax,%ss
movw $0x7bf0,%sp # stack
#mov ax, 0x4F02 ; VESA function "Set Video Mode"
#mov bx, 0x0107 ; 1024x768x256
#int 0x10 ; BIOS video interrupt
movw $bmsga, %si # Print "Starting..."
calll _print # ...
.extern main
calll main
movw $bmsgb, %si # Print ":("
calll _print # ...
# uh...
hlt
_print:
movb $0x0E,%ah # set registers for
movb $0x00,%bh # bios video call
movb $0x07,%bl
_print.next:
lodsb # string stuff
orb %al,%al # if 0...
jz _print.return # return
int $0x10 # print character
jmp _print.next # continue
_print.return:
retl
.data
bmsga:
.string "Starting up...\r\n"
bmsgb:
.string ":(\r\n"

View File

@ -1,28 +0,0 @@
/*
* Mr. Boots Stage 2
*/
/*
* 16-bit bootloader
*/
__asm__(".code16\n");
/*
* Main entry point
*/
int main() {
return 0;
}
void print(char * str) {
__asm__ ("movb $0x0E, %ah\n"
"movb $0x00, %bh\n"
"movb $0x07, %bl\n"
);
int i = 0;
while (str[i] != '\0') {
__asm__ __volatile__ ("movb %0, %%al\n"
"int $0x10" : : "m" (str[i]));
++i;
}
}