Compare commits

..

10 commits

7 changed files with 520 additions and 28 deletions

View file

@ -13,8 +13,11 @@ config.mk: | config.default.mk
boot.bin: boot.elf boot.bin: boot.elf
objcopy -O binary --only-section=.text boot.elf $@ objcopy -O binary --only-section=.text boot.elf $@
boot.elf: lboot.o fernlader.ld boot.elf: lboot.o loader.o fernlader.ld
$(LD) $(LDFLAGS) -o $@ lboot.o $(LD) $(LDFLAGS) -o $@ lboot.o loader.o
.S.o: lboot.o: lboot.S
$(CC) $(CFLAGS) -c -o $@ $(@:.o=.S) $(CC) $(CFLAGS) -c -o $@ $(@:.o=.S)
loader.o: loader.c
$(CC) $(CFLAGS) -m64 -Os -c -o $@ $(@:.o=.c)

155
bootboot.h Normal file
View file

@ -0,0 +1,155 @@
/*
* bootboot.h
* https://gitlab.com/bztsrc/bootboot
*
* Copyright (C) 2017 - 2021 bzt (bztsrc@gitlab)
*
* 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 THE AUTHORS OR COPYRIGHT
* HOLDERS 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.
*
* This file is part of the BOOTBOOT Protocol package.
* @brief The BOOTBOOT structure
*
*/
#ifndef _BOOTBOOT_H_
#define _BOOTBOOT_H_
#ifdef __cplusplus
extern "C" {
#endif
#ifndef _MSC_VER
#define _pack __attribute__((packed))
#else
#define _pack
#pragma pack(push)
#pragma pack(1)
#endif
#define BOOTBOOT_MAGIC "BOOT"
/* default virtual addresses for level 0 and 1 static loaders */
#define BOOTBOOT_MMIO 0xfffffffff8000000 /* memory mapped IO virtual address */
#define BOOTBOOT_FB 0xfffffffffc000000 /* frame buffer virtual address */
#define BOOTBOOT_INFO 0xffffffffffe00000 /* bootboot struct virtual address */
#define BOOTBOOT_ENV 0xffffffffffe01000 /* environment string virtual address */
#define BOOTBOOT_CORE 0xffffffffffe02000 /* core loadable segment start */
/* minimum protocol level:
* hardcoded kernel name, static kernel memory addresses */
#define PROTOCOL_MINIMAL 0
/* static protocol level:
* kernel name parsed from environment, static kernel memory addresses */
#define PROTOCOL_STATIC 1
/* dynamic protocol level:
* kernel name parsed, kernel memory addresses from ELF or PE symbols */
#define PROTOCOL_DYNAMIC 2
/* big-endian flag */
#define PROTOCOL_BIGENDIAN 0x80
/* loader types, just informational */
#define LOADER_BIOS (0<<2)
#define LOADER_UEFI (1<<2)
#define LOADER_RPI (2<<2)
#define LOADER_COREBOOT (3<<2)
/* framebuffer pixel format, only 32 bits supported */
#define FB_ARGB 0
#define FB_RGBA 1
#define FB_ABGR 2
#define FB_BGRA 3
/* mmap entry, type is stored in least significant tetrad (half byte) of size
* this means size described in 16 byte units (not a problem, most modern
* firmware report memory in pages, 4096 byte units anyway). */
typedef struct {
uint64_t ptr;
uint64_t size;
} _pack MMapEnt;
#define MMapEnt_Ptr(a) ((a)->ptr)
#define MMapEnt_Size(a) ((a)->size & 0xFFFFFFFFFFFFFFF0)
#define MMapEnt_Type(a) ((a)->size & 0xF)
#define MMapEnt_IsFree(a) (((a)->size&0xF)==1)
#define MMAP_USED 0 /* don't use. Reserved or unknown regions */
#define MMAP_FREE 1 /* usable memory */
#define MMAP_ACPI 2 /* acpi memory, volatile and non-volatile as well */
#define MMAP_MMIO 3 /* memory mapped IO region */
#define INITRD_MAXSIZE 16 /* Mb */
typedef struct {
/* first 64 bytes is platform independent */
uint8_t magic[4]; /* 'BOOT' magic */
uint32_t size; /* length of bootboot structure, minimum 128 */
uint8_t protocol; /* 1, static addresses, see PROTOCOL_* and LOADER_* above */
uint8_t fb_type; /* framebuffer type, see FB_* above */
uint16_t numcores; /* number of processor cores */
uint16_t bspid; /* Bootsrap processor ID (Local APIC Id on x86_64) */
int16_t timezone; /* in minutes -1440..1440 */
uint8_t datetime[8]; /* in BCD yyyymmddhhiiss UTC (independent to timezone) */
uint64_t initrd_ptr; /* ramdisk image position and size */
uint64_t initrd_size;
uint64_t fb_ptr; /* framebuffer pointer and dimensions */
uint32_t fb_size;
uint32_t fb_width;
uint32_t fb_height;
uint32_t fb_scanline;
/* the rest (64 bytes) is platform specific */
union {
struct {
uint64_t acpi_ptr;
uint64_t smbi_ptr;
uint64_t efi_ptr;
uint64_t mp_ptr;
uint64_t unused0;
uint64_t unused1;
uint64_t unused2;
uint64_t unused3;
} x86_64;
struct {
uint64_t acpi_ptr;
uint64_t mmio_ptr;
uint64_t efi_ptr;
uint64_t unused0;
uint64_t unused1;
uint64_t unused2;
uint64_t unused3;
uint64_t unused4;
} aarch64;
} arch;
/* from 128th byte, MMapEnt[], more records may follow */
MMapEnt mmap;
/* use like this:
* MMapEnt *mmap_ent = &bootboot.mmap; mmap_ent++;
* until you reach bootboot->size, while(mmap_ent < bootboot + bootboot->size) */
} _pack BOOTBOOT;
#ifdef _MSC_VER
#pragma pack(pop)
#endif
#ifdef __cplusplus
}
#endif
#endif

1
fernlader.cfg Normal file
View file

@ -0,0 +1 @@
kernel=sys/core

View file

@ -1,6 +1,7 @@
SECTIONS { SECTIONS {
. = 0x7C00; .text 0x7C00: {
.text : { lboot.o(.text*, .data*)
*(*) loader.o(.text*, .data*, .bss*)
} }
memmap = ALIGN(8);
} }

369
lboot.S
View file

@ -4,8 +4,22 @@
.text .text
.code16 .code16
.macro pxe_call, opcode
mov %sp, %bx
push %ss
push %bx
push $\opcode
lcall *pxe_api
add $6, %sp
mov %sp, %bx
or %ss:(%bx), %ax
jnz _pcerr
.endm
// _start: entry point
_start: cli _start: cli
cld cld
mov %sp, %bp
// we keep our text and data close to each other // we keep our text and data close to each other
xor %ax, %ax xor %ax, %ax
@ -16,6 +30,18 @@ _start: cli
mov $msg_start, %si mov $msg_start, %si
call print call print
mov %ss:4(%bp), %si
mov %ss:6(%bp), %ax
mov %ax, %es
.set PXE_MAGIC, 0x45585021
cmpl $PXE_MAGIC, %es:(%si)
je 1f
mov $msg_pserr, %si
call print
jmp hang
1: mov %es:16(%si), %eax
mov %eax, pxe_api
mov $msg_a20, %si mov $msg_a20, %si
call print call print
call enable_a20 call enable_a20
@ -24,18 +50,48 @@ _start: cli
call print call print
call unreal call unreal
mov $msg_fin, %si mov $msg_getmap, %si
call print call print
1: hlt call get_map
jmp 1b
.set COM1, 0x3F8 mov $msg_mkheap, %si
.macro com1_write offset=0, byte call print
call make_heap
mov $msg_paging, %si
call print
call paging
.set PXE_GET_CACHED_INFO, 0x0071
push $0
push %cs
push $tx_buf
push $1024
push $2
push $0
pxe_call PXE_GET_CACHED_INFO
add $12, %sp
mov tx_buf+20, %eax
mov %eax, server_ip
mov $msg_read, %si
call print
call read_file
mov $msg_long, %si
call print
call long
jmp hang
.set COM1, 0x3F8
.macro com1_write offset=0, byte
mov $COM1+\offset, %dx mov $COM1+\offset, %dx
mov $\byte, %al mov $\byte, %al
outb %al, %dx outb %al, %dx
.endm .endm
// init_com1: Set up COM1 port for debug output
init_com1: com1_write 1, 0x00 // clear interrupts init_com1: com1_write 1, 0x00 // clear interrupts
com1_write 3, 0x80 // set DLAB to 1 com1_write 3, 0x80 // set DLAB to 1
com1_write 0, 0x0C // 9600 baud rate com1_write 0, 0x0C // 9600 baud rate
@ -43,51 +99,256 @@ init_com1: com1_write 1, 0x00 // clear interrupts
com1_write 3, 0x07 // 8 bit data + 1 parity bit com1_write 3, 0x07 // 8 bit data + 1 parity bit
ret ret
// enable_a20: Allow use of 'high' memory
enable_a20: // TODO more thorough implementation enable_a20: // TODO more thorough implementation
inb $0x92, %al inb $0x92, %al
or $2, %al or $2, %al
outb %al, $0x92 outb %al, $0x92
ret ret
// unreal: Enter unreal mode
unreal: push %ds unreal: push %ds
lgdt gdt_ptr lgdt gdt16_ptr
mov %cr0, %eax mov %cr0, %eax
or $0x01, %al or $0x01, %al
mov %eax, %cr0 mov %eax, %cr0
ljmp $0x8, $1f ljmp $0x8, $_urprot
1: mov $0x10, %cx _urprot: mov $0x10, %cx
mov %cx, %ds mov %cx, %ds
and $0xFE, %al and $0xFE, %al
mov %eax, %cr0 mov %eax, %cr0
ljmp $0x0, $2f ljmp $0x0, $_urunreal
2: pop %ds _urunreal: pop %ds
ret ret
// get_map: Retrieve memory map using e820 BIOS function
get_map: mov %ds, %ax
mov %ax, %es
mov $memmap, %di
xor %ebx, %ebx
mov $0x534D4150, %edx // e820 magic number
_gmnext: movl $0, 20(%di)
mov $24, %ecx
mov $0xE820, %eax
int $0x15
jc _gmdone
test %ebx, %ebx
jz _gmdone
add $24, %di
jmp _gmnext
_gmdone: add $24, %di
mov %di, memmap_end
ret
// paging: Set up initial page tables for long mode
paging: mov $4*4096, %ecx
call alloc
mov %eax, pd_ptr
mov $4096, %ecx
call alloc
mov %eax, pdp_ptr
mov $4096, %ecx
call alloc
mov %eax, pml4_ptr
// fill PDEs with identity map < 4Gb
mov pd_ptr, %edi
xor %ecx, %ecx
_pgnext: mov %ecx, %eax
shl $21, %eax
or $0b10000011, %eax
movl %eax, 0(%edi)
movl $0, 4(%edi)
add $8, %edi
inc %ecx
cmp $4*512, %ecx
jne _pgnext
// link to PDs in PDP
mov pdp_ptr, %edi
mov pd_ptr, %eax
orl $0b11, %eax
movl %eax, 0(%edi)
movl $0, 4(%edi)
add $0x1000, %eax
movl %eax, 8(%edi)
movl $0, 12(%edi)
add $0x1000, %eax
movl %eax, 16(%edi)
movl $0, 20(%edi)
add $0x1000, %eax
movl %eax, 24(%edi)
movl $0, 28(%edi)
// link to PDP in PML4
mov pml4_ptr, %edi
mov pdp_ptr, %eax
orl $0b11, %eax
movl %eax, 0(%edi)
movl $0, 4(%edi)
ret
// make_heap: find a memory range suitable for heap usage
make_heap: mov $memmap-24, %si
_mhnext: add $24, %si
cmp memmap_end, %si
jae _mhdone
cmpl $1, 16(%si)
jne _mhnext
cmpl $0, 4(%si)
ja _mhnext
mov 0(%si), %ebx
mov 8(%si), %ecx
// find end of range, clip to 4Gb
add %ebx, %ecx
jnc 1f
mov $0xFFFFFFFF, %ecx
// handle wraparound if length > 4Gb
1: cmpl $0, 12(%si)
je 1f
mov $0xFFFFFFFF, %ecx
// adjust base to above 1Mb, above the bootloader
1: cmp $0x10000, %ebx
jae 1f
mov $0x10000, %ebx
// align to 4Kb boundaries
1: add $0xFFF, %ebx
and $0xFFFFF000, %ebx
and $0xFFFFF000, %ecx
sub %ebx, %ecx
cmp heap_size, %ecx
jbe _mhnext
mov %ebx, heap_start
mov %ecx, heap_size
jmp _mhnext
_mhdone: ret
// alloc: take ECX bytes from heap, return ptr in EAX
// The allocation does not get marked in the memmap.
// No realignment is performed, so only alloc aligned sizes.
alloc: cmp heap_size, %ecx
ja _aerr
mov heap_start, %eax
add %ecx, heap_start
sub %ecx, heap_size
ret
_aerr: mov $msg_aerr, %si
call print
jmp hang
read_file:
.set PXE_TFTP_OPEN, 0x0020
push $1024
push $0x4500
sub $128, %sp
mov $fn_config, %esi
mov %ss, %ax
mov %ax, %es
mov %esp, %edi
mov $fn_config_l, %ecx
rep movsb
push $0
push $0
sub $4, %sp
mov %sp, %di
mov server_ip+0, %al
mov %al, %ss:0(%di)
mov server_ip+1, %al
mov %al, %ss:1(%di)
mov server_ip+2, %al
mov %al, %ss:2(%di)
mov server_ip+3, %al
mov %al, %ss:3(%di)
push $0
pxe_call PXE_TFTP_OPEN
add $14+128, %sp
ret
// print: print NUL-terminated string pointed to by SI
print: xor %bx, %bx print: xor %bx, %bx
1: mov $COM1+5, %dx _prnext: mov $COM1+5, %dx
inb %dx, %al inb %dx, %al
test $0x20, %al test $0x20, %al
jz 1b jz _prnext
lodsb lodsb
or %al, %al or %al, %al
jz 2f jz _prdone
mov $COM1, %dx mov $COM1, %dx
outb %al, %dx outb %al, %dx
mov $0x0E, %ah mov $0x0E, %ah
int $0x10 int $0x10
jmp 1b jmp _prnext
2: ret _prdone: ret
gdt: // entry 0: null descriptor // long: Enter long mode
long:
// Enable PAE
mov %cr4, %eax
or $0b100000, %eax
mov %eax, %cr4
// Load page table
mov pml4_ptr, %eax
mov %eax, %cr3
// Enable long mode
.set IA32_EFER, 0xC0000080
mov $IA32_EFER, %ecx
rdmsr
or $0x100, %eax
wrmsr
// Enable protected mode + paging
mov %cr0, %eax
or $0x80000001, %eax
mov %eax, %cr0
// Linearize stack address
mov %ss, %eax
shl $4, %eax
add %eax, %esp
mov %esp, %ebp
// Load long mode GDT, switch to 64-bit CS
lgdt gdt64_ptr
ljmp $0x8, $trampo64
// hang: sleep indefinitely
hang: hlt
jmp hang
_pcerr: mov $msg_pcerr, %si
call print
jmp hang
// gdt16: Protected mode / Unreal mode 16-bit GDT
gdt16: // entry 0: null descriptor
.word 0 .word 0
.word 0 .word 0
.byte 0 .byte 0
@ -108,11 +369,77 @@ gdt: // entry 0: null descriptor
.byte 0b10010010 .byte 0b10010010
.byte 0x8F .byte 0x8F
.byte 0 .byte 0
.set gdt_size, .-gdt .set gdt16_size, .-gdt16
gdt_ptr: .word gdt_size-1 gdt16_ptr: .word gdt16_size-1
.long gdt .long gdt16
// gdt64: Long mode 64-bit GDT
gdt64: // entry 0: null descriptor
.quad 0
// entry 1: code segment
.word 0
.word 0
.byte 0
.byte 0x98
.byte 0x60
.byte 0
// entry 2: data segment
.word 0
.word 0
.byte 0
.byte 0x92
.byte 0x00
.byte 0
.set gdt64_size, .-gdt64
gdt64_ptr: .word gdt64_size-1
.quad gdt64
// Messages to print
msg_start: .asciz "Netboot via fernlader v1 ...\r\n" msg_start: .asciz "Netboot via fernlader v1 ...\r\n"
msg_a20: .asciz " * Enabling A20\r\n" msg_a20: .asciz " * Enabling A20\r\n"
msg_unreal: .asciz " * Unreal Mode\r\n" msg_unreal: .asciz " * Unreal Mode\r\n"
msg_fin: .asciz "Finished.\r\n" msg_getmap: .asciz " * Memory Map\r\n"
msg_mkheap: .asciz " * Making Space\r\n"
msg_paging: .asciz " * Paging\r\n"
msg_read: .asciz " * Retrieving\r\n"
msg_long: .asciz " * Long Mode\r\n"
msg_pserr: .asciz "panic: Missing !PXE structure.\r\n"
msg_pcerr: .asciz "panic: PXE call failed.\r\n"
msg_aerr: .asciz "panic: Out of heap space.\r\n"
fn_config: .asciz "fernlader.cfg"
.set fn_config_l, .-fn_config
pxe_api: .long 0
server_ip: .space 4
heap_start: .long 0
heap_size: .long 0
// Long mode initial page tables
pd_ptr: .long 0
pdp_ptr: .long 0
pml4_ptr: .long 0
// Points to the end of the memory map
memmap_end: .word 0
tx_buf: .space 1024
.code64
// trampo64: Trampoline function to load long-mode segments
// before entering the loader.
trampo64:
mov $0x10, %eax
mov %eax, %ds
mov %eax, %es
mov %eax, %fs
mov %eax, %gs
mov %eax, %ss
jmp loader_main
// ToDo List:
// - Sorting the memmap
// - Sanitizing the memmap
// - Translating the memmap to bootboot format
// - Parsing a config file

5
loader.c Normal file
View file

@ -0,0 +1,5 @@
void
loader_main(void)
{
for (;;) {}
}

View file

@ -1,2 +1,2 @@
#!/bin/sh #!/bin/sh
qemu-system-x86_64 -netdev user,id=n1,net=10.0.0.5/24,tftp=netboot,bootfile=/boot.bin -device virtio-net-pci,netdev=n1 qemu-system-x86_64 -netdev user,id=n1,net=10.0.0.5/24,tftp=.,bootfile=/boot.bin -device virtio-net-pci,netdev=n1,bootindex=0 "$@"