diff options
Diffstat (limited to 'boot')
| -rw-r--r-- | boot/Makefile | 11 | ||||
| -rw-r--r-- | boot/amd64/generic/Makefile | 29 | ||||
| -rw-r--r-- | boot/amd64/generic/biosboot/Makefile | 15 | ||||
| -rw-r--r-- | boot/amd64/generic/biosboot/biosboot.S | 54 | ||||
| -rw-r--r-- | boot/amd64/generic/biosboot/link.ld | 26 | ||||
| -rw-r--r-- | boot/amd64/generic/mbr/Makefile | 15 | ||||
| -rw-r--r-- | boot/amd64/generic/mbr/link.ld | 26 | ||||
| -rw-r--r-- | boot/amd64/generic/mbr/mbr.S | 213 | ||||
| -rw-r--r-- | boot/amd64/generic/mk/build.mk | 19 | ||||
| -rw-r--r-- | boot/amd64/generic/stage3/Makefile | 22 | ||||
| -rw-r--r-- | boot/amd64/generic/stage3/entry.c | 7 | ||||
| -rw-r--r-- | boot/amd64/generic/stage3/link.ld | 26 |
12 files changed, 463 insertions, 0 deletions
diff --git a/boot/Makefile b/boot/Makefile new file mode 100644 index 0000000..9f40b8a --- /dev/null +++ b/boot/Makefile @@ -0,0 +1,11 @@ +# +# Copyright (c) 2026, Apollo Telephone Laboratories. +# Provided under the BSD-3 clause. +# + +.PHONY: all +all: machine + +.PHONY: machine +machine: + cd $(CONFIG_TARGET_ARCH)/$(CONFIG_TARGET_BOARD); $(MAKE) diff --git a/boot/amd64/generic/Makefile b/boot/amd64/generic/Makefile new file mode 100644 index 0000000..c911fd7 --- /dev/null +++ b/boot/amd64/generic/Makefile @@ -0,0 +1,29 @@ +# +# Copyright (c) 2026, Apollo Telephone Laboratories. +# Provided under the BSD-3 clause. +# + +.PHONY: all +all: mbr biosboot stage3 image + +.PHONY: image +image: + mkdir -p rdar_root + cat biosboot/biosboot.bin stage3/stage3.bin >rdar_root/s2.bin + cd rdar_root; rdar -i . -o ../bootar.rdar + rm -rf rdar_root + cat mbr/mbr.bin bootar.rdar >$(GLOBAL_PROJECT_ROOT)/out/ams.img + dd if=/dev/zero of=/tmp/zero-pad.tmp count=8 bs=512 + cat /tmp/zero-pad.tmp >>$(GLOBAL_PROJECT_ROOT)/out/ams.img + +.PHONY: stage3 +stage3: + cd stage3; $(MAKE) + +.PHONY: biosboot +biosboot: + cd biosboot; $(MAKE) + +.PHONY: mbr +mbr: + cd mbr; $(MAKE) diff --git a/boot/amd64/generic/biosboot/Makefile b/boot/amd64/generic/biosboot/Makefile new file mode 100644 index 0000000..e9854be --- /dev/null +++ b/boot/amd64/generic/biosboot/Makefile @@ -0,0 +1,15 @@ +# +# Copyright (c) 2026, Apollo Telephone Laboratories. +# Provided under the BSD-3 clause. +# + +include ../mk/build.mk + +CFLAGS = $(INTERNAL_CFLAGS) + +.PHONY: all +all: biosboot + +.PHONY: biosboot +biosboot: + $(CC) $(CFLAGS) -Tlink.ld biosboot.S -o biosboot.bin diff --git a/boot/amd64/generic/biosboot/biosboot.S b/boot/amd64/generic/biosboot/biosboot.S new file mode 100644 index 0000000..bf2b004 --- /dev/null +++ b/boot/amd64/generic/biosboot/biosboot.S @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + + .text + .code16 + .globl _start +_start: + cli + xor %ax, %ax + mov %ax, %ds + mov %ax, %es + mov %ax, %ss + + lgdt gdtr + mov %cr0, %eax + or $1, %eax + mov %eax, %cr0 + ljmp $0x08,$1f + + .code32 +1: mov $0x10, %ax + mov %ax, %es + mov %ax, %ss + mov %ax, %fs + mov %ax, %gs + mov %ax, %ds + jmp c_thunk + +gdt: +.null: + .quad 0 +.code: + .word 0xFFFF + .word 0x0000 + .byte 0x00 + .byte 0b10011010 + .byte 0b11001111 + .byte 0x00 +.data: + .word 0xFFFF + .word 0x0000 + .byte 0x00 + .byte 0b10010010 + .byte 0b11001111 + .byte 0x00 +gdtr: + .word . - gdt - 1 + .long gdt + +/* Fall-through : everything above this */ +c_thunk: + nop diff --git a/boot/amd64/generic/biosboot/link.ld b/boot/amd64/generic/biosboot/link.ld new file mode 100644 index 0000000..f039b2b --- /dev/null +++ b/boot/amd64/generic/biosboot/link.ld @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + +OUTPUT_FORMAT(binary) + +SECTIONS { + . = 0x7C00; + + .text : { + *(.text) + } + + .data : { + *(.data) + } + + .rodata : { + *(.rodata) + } + + .bss : { + *(.bss) + } +} diff --git a/boot/amd64/generic/mbr/Makefile b/boot/amd64/generic/mbr/Makefile new file mode 100644 index 0000000..c9ae0dc --- /dev/null +++ b/boot/amd64/generic/mbr/Makefile @@ -0,0 +1,15 @@ +# +# Copyright (c) 2026, Apollo Telephone Laboratories. +# Provided under the BSD-3 clause. +# + +include ../mk/build.mk + +CFLAGS = $(INTERNAL_CFLAGS) + +.PHONY: all +all: mbr + +.PHONY: mbr +mbr: + $(CC) $(CFLAGS) -Tlink.ld mbr.S -o mbr.bin diff --git a/boot/amd64/generic/mbr/link.ld b/boot/amd64/generic/mbr/link.ld new file mode 100644 index 0000000..fbc3b21 --- /dev/null +++ b/boot/amd64/generic/mbr/link.ld @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + +OUTPUT_FORMAT(binary) + +SECTIONS { + . = 0x00; + + .text : { + *(.text) + } + + .data : { + *(.data) + } + + .rodata : { + *(.rodata) + } + + .bss : { + *(.bss) + } +} diff --git a/boot/amd64/generic/mbr/mbr.S b/boot/amd64/generic/mbr/mbr.S new file mode 100644 index 0000000..7cd7484 --- /dev/null +++ b/boot/amd64/generic/mbr/mbr.S @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + +#define BOOT_SEG 0x7C0 /* Segment we reside on startup */ +#define BOOT_RELOC_SEG 0x7A0 /* Relocation segment */ +#define RDAR_MAGIC ">RDAR!" +#define RDAR_MAGIC_LEN 6 +#define BOOT_PATH "s2.bin" +#define BOOT_PATH_LEN 6 + + .text + .code16 +_start: + ljmp $BOOT_SEG,$main +main: + cli + cld + + /* Set segment registers */ + xor %ax, %ax + mov %ax, %fs + mov %ax, %gs + mov %ax, %ss + mov %ax, %es + mov $BOOT_SEG, %ax + mov %ax, %ds + + /* Save the drive */ + mov %dl, drive_num + + /* + * The second stage bootloader expects to be loaded to 0x7C00 but since we are + * already executing at this address we'll need to relocate ourselves first before + * we clobber it. + */ + mov $BOOT_SEG, %ax + mov %ax, %ds + mov $BOOT_RELOC_SEG, %ax + mov %ax, %es + xor %si, %si + xor %di, %di + mov $0x200, %cx + cld + rep movsb + + jmp $BOOT_RELOC_SEG,$1f +1: mov $0x7C00, %sp + + /* Load two sectors */ + movw $2, dap_blkcnt + movl $1, dap_lba_low + call disk_read_lba + + mov $BOOT_RELOC_SEG, %ax + mov %ax, %ds + + /* Get stage2 */ + call find_stage2 + + /* Obtain the data offset */ + mov $BOOT_SEG, %ax + mov %ax, %ds + add $40, %si + movw (%si), %si + + mov $BOOT_SEG, %ax + mov %ax, %ds + mov %ax, %es + xor %di, %di + mov $0x200, %cx + cld + rep movsb + + /* Jump to stage 2 */ + xor %ax, %ax + mov %ax, %ds + ljmp $0x0000,$0x7C00 + +/* + * Write a character to the boot console + * + * %al: Character to write + */ +putc: + mov $0x0E, %ah + xor %bh, %bh + int $0x10 + ret + +/* + * Write a string to the boot console + * + * %si: Pointer to string to write + */ +puts: + lodsb + or %al, %al + jz 1f + call putc + jmp puts +1: ret + +/* + * Locate stage2 on the disk + * + * Stage 2 data descriptor is returned in %SI + */ +find_stage2: + push %ds + push %es + + /* Verify that magic is correct */ + mov $BOOT_SEG, %ax + mov %ax, %ds + xor %bx, %bx /* Tracks %SI */ + xor %si, %si + mov $BOOT_RELOC_SEG, %ax + mov %ax, %es + mov $rdar_magic, %di + mov $RDAR_MAGIC_LEN, %cx + rep cmpsb + jne .bad_magic + + /* Iterate every data descriptor */ + clc /* Carry set if found */ +1: movw %ds:6, %cx /* Add data descriptor size */ + mov %bx, %si + mov %si, %dx + add %cx, %bx + mov $rdar_magic, %di + mov $RDAR_MAGIC_LEN, %cx + rep cmpsb + jne .not_found + + /* Does the path match? */ + mov %dx, %si + add $8, %si + mov $stage2_path, %di + mov $BOOT_PATH_LEN, %cx + rep cmpsb + jne 1b + + mov %dx, %si + stc + pop %es + pop %ds + jnc .not_found + ret +.not_found: + pop %ds + mov $not_found_msg, %si + call puts +1: cli + hlt + jmp 1b + +.bad_magic: + /* Restore %DS */ + mov $BOOT_RELOC_SEG, %ax + mov %ax, %ds + + mov $bad_magic_msg, %si + call puts +1: cli + hlt + jmp 1b + +/* + * Read sectors from the disk + */ +disk_read_lba: + pusha + mov $dap, %si + mov $0x42, %ah + mov drive_num, %dl + int $0x13 + jc .error + popa + ret +.error: + mov $disk_err_msg, %si + call puts +1: cli + hlt + jmp 1b + +/* Global stores */ +rdar_magic: .ascii RDAR_MAGIC +bad_magic_msg: .ascii "bad RDAR magic\r\n\0" +not_found_msg: .ascii "stage 2 not found\r\n\0" +disk_err_msg: .ascii "\r\n!!!disk error!!!\r\n\0" +stage2_path: .ascii BOOT_PATH +drive_num: .byte 0 + +dap: + .byte 0x10 + .byte 0x00 +dap_blkcnt: + .word 0 +dap_destptr: + .word 0x7C00 +dap_destseg: + .word 0x0000 +dap_lba_low: + .long 0 +dap_lba_high: + .long 0 + + .org 510 + .byte 0x55 + .byte 0xAA diff --git a/boot/amd64/generic/mk/build.mk b/boot/amd64/generic/mk/build.mk new file mode 100644 index 0000000..3388d41 --- /dev/null +++ b/boot/amd64/generic/mk/build.mk @@ -0,0 +1,19 @@ +# +# Copyright (c) 2026, Apollo Telephone Laboratories. +# Provided under the BSD-3 clause. +# + +CC = \ + clang + +INTERNAL_CFLAGS = \ + -Wall \ + -pedantic \ + -ffreestanding \ + -nostdlib \ + -nostdinc \ + -mno-red-zone \ + -mno-sse \ + -mno-sse2 \ + -mno-avx \ + -mno-avx2 diff --git a/boot/amd64/generic/stage3/Makefile b/boot/amd64/generic/stage3/Makefile new file mode 100644 index 0000000..08e159e --- /dev/null +++ b/boot/amd64/generic/stage3/Makefile @@ -0,0 +1,22 @@ +# +# Copyright (c) 2026, Apollo Telephone Laboratories. +# Provided under the BSD-3 clause. +# + +include ../mk/build.mk + +CFLAGS = $(INTERNAL_CFLAGS) -MMD +CFILES = $(shell find . -name "*.c") +OFILES = $(CFILES:.c=.o) +DFILES = $(CFILES:.c=.d) + +.PHONY: all +all: stage3 + +.PHONY: stage3 +stage3: $(OFILES) + $(CC) $(CFLAGS) -Tlink.ld $(OFILES) -o stage3.bin + +-include $(DFILES) +%.o: %.c + $(CC) -c $< $(CFLAGS) -o $@ diff --git a/boot/amd64/generic/stage3/entry.c b/boot/amd64/generic/stage3/entry.c new file mode 100644 index 0000000..3e4b95b --- /dev/null +++ b/boot/amd64/generic/stage3/entry.c @@ -0,0 +1,7 @@ +void +main(void) +{ + for (;;) { + __asm__ __volatile__("cli; hlt"); + } +} diff --git a/boot/amd64/generic/stage3/link.ld b/boot/amd64/generic/stage3/link.ld new file mode 100644 index 0000000..abbd785 --- /dev/null +++ b/boot/amd64/generic/stage3/link.ld @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2026, Apollo Telephone Laboratories. + * Provided under the BSD-3 clause. + */ + +OUTPUT_FORMAT(binary) + +SECTIONS { + . = 0x0000; + + .text : { + *(.text) + } + + .data : { + *(.data) + } + + .rodata : { + *(.rodata) + } + + .bss : { + *(.bss) + } +} |
