misc: General code reorganisation

This commit is contained in:
mintsuki 2021-02-25 01:24:54 +01:00
parent 7eb22e5c15
commit 27848fc85c
108 changed files with 162 additions and 149 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
bin
/**/*.gen
/**/*.map
/**/*.o

View File

@ -6,38 +6,34 @@ DESTDIR =
PATH := $(shell pwd)/toolchain/bin:$(PATH)
.PHONY: all clean install tinf-clean bootloader bootloader-clean distclean stages stages-clean decompressor decompressor-clean toolchain test.hdd echfs-test ext2-test fat32-test
.PHONY: all clean install tinf-clean bootloader bootloader-clean distclean stage23 stage23-clean decompressor decompressor-clean toolchain test.hdd echfs-test ext2-test fat32-test iso9660-test
all: limine-install
all: bin/limine-install
limine-install: limine-install.c limine.o limine_sys.o
$(CC) $(CFLAGS) -std=c11 limine.o limine_sys.o limine-install.c -o limine-install
bin/limine-install: limine-install.c limine-hdd.o
$(CC) $(CFLAGS) -std=c11 limine-hdd.o limine-install.c -o $@
limine.o: limine.bin
$(OBJCOPY) -B i8086 -I binary -O default limine.bin limine.o
limine_sys.o: limine.bin
$(OBJCOPY) -B i8086 -I binary -O default limine.sys limine_sys.o
limine-hdd.o: bin/limine-hdd.bin
$(OBJCOPY) -B i8086 -I binary -O default bin/limine-hdd.bin $@
clean:
rm -f limine.o limine_sys.o limine-install
rm -f limine-hdd.o
install: all
install -d $(DESTDIR)$(PREFIX)/bin
install -s limine-install $(DESTDIR)$(PREFIX)/bin/
bootloader: | decompressor stages
cd hddboot && nasm bootsect.asm -fbin -o ../limine-hdd.bin
cd cdboot && nasm bootsect.asm -fbin -o ../limine-cd.bin
cd pxeboot && nasm bootsect.asm -fbin -o ../limine-pxe.bin
cp stages/stages.map ./
cp stages/stage3.bin ./limine.sys
bootloader: | decompressor stage23
mkdir -p bin
cd stage1/hdd && nasm bootsect.asm -fbin -o ../../bin/limine-hdd.bin
cd stage1/cd && nasm bootsect.asm -fbin -o ../../bin/limine-cd.bin
cd stage1/pxe && nasm bootsect.asm -fbin -o ../../bin/limine-pxe.bin
cp stage23/limine.sys ./bin/
bootloader-clean: stages-clean decompressor-clean test-clean
rm -f test/stages.map test.hdd
bootloader-clean: stage23-clean decompressor-clean test-clean
distclean: clean bootloader-clean
rm -rf stivale
rm -rf bin stivale
tinf-clean:
cd tinf && rm -rf *.o *.d
@ -46,11 +42,11 @@ stivale:
git clone https://github.com/stivale/stivale.git
cd stivale && git checkout d0a7ca5642d89654f8d688c2481c2771a8653c99
stages: tinf-clean stivale
$(MAKE) -C stages all
stage23: tinf-clean stivale
$(MAKE) -C stage23 all
stages-clean:
$(MAKE) -C stages clean
stage23-clean:
$(MAKE) -C stage23 clean
decompressor: tinf-clean
$(MAKE) -C decompressor all
@ -60,6 +56,7 @@ decompressor-clean:
test-clean:
$(MAKE) -C test clean
rm -f test/limine.map test.hdd
toolchain:
cd toolchain && ./make_toolchain.sh -j`nproc`
@ -76,16 +73,15 @@ echfs-test: test.hdd bootloader | all
sed "s/@GUID@/`cat part_guid`/g" < test/limine.cfg > limine.cfg.tmp
echfs-utils -g -p0 test.hdd import limine.cfg.tmp limine.cfg
rm -f limine.cfg.tmp part_guid
echfs-utils -g -p0 test.hdd import stages.map boot/stages.map
echfs-utils -g -p0 test.hdd import test/test.elf boot/test.elf
echfs-utils -g -p0 test.hdd import test/bg.bmp boot/bg.bmp
./limine-install ./ test.hdd
echfs-utils -g -p0 test.hdd import ./limine.sys boot/limine.sys
echfs-utils -g -p0 test.hdd import bin/limine.sys boot/limine.sys
echfs-utils -g -p0 test.hdd import bin/limine.map boot/limine.map
bin/limine-install test.hdd
qemu-system-x86_64 -net none -smp 4 -enable-kvm -cpu host -hda test.hdd -debugcon stdio
ext2-test: test.hdd bootloader | all
$(MAKE) -C test
cp stages.map test/
rm -rf test_image/
mkdir test_image
sudo losetup -Pf --show test.hdd > loopback_dev
@ -93,17 +89,16 @@ ext2-test: test.hdd bootloader | all
sudo mkfs.ext2 `cat loopback_dev`p1
sudo mount `cat loopback_dev`p1 test_image
sudo mkdir test_image/boot
sudo cp -rv ./limine.sys test/* test_image/boot/
sudo cp -rv bin/* test/* test_image/boot/
sync
sudo umount test_image/
sudo losetup -d `cat loopback_dev`
rm -rf test_image loopback_dev
./limine-install ./ test.hdd
bin/limine-install test.hdd
qemu-system-x86_64 -net none -smp 4 -enable-kvm -cpu host -hda test.hdd -debugcon stdio
fat32-test: test.hdd bootloader | all
$(MAKE) -C test
cp stages.map test/
rm -rf test_image/
mkdir test_image
sudo losetup -Pf --show test.hdd > loopback_dev
@ -111,19 +106,18 @@ fat32-test: test.hdd bootloader | all
sudo mkfs.fat -F 32 `cat loopback_dev`p1
sudo mount `cat loopback_dev`p1 test_image
sudo mkdir test_image/boot
sudo cp -rv ./limine.sys test/* test_image/boot/
sudo cp -rv bin/* test/* test_image/boot/
sync
sudo umount test_image/
sudo losetup -d `cat loopback_dev`
rm -rf test_image loopback_dev
./limine-install ./ test.hdd
bin/limine-install test.hdd
qemu-system-x86_64 -net none -smp 4 -enable-kvm -cpu host -hda test.hdd -debugcon stdio
iso9660-test: bootloader
$(MAKE) -C test
cp stages.map test/
rm -rf test_image/
mkdir -p test_image/boot
cp -rv limine-cd.bin limine.sys stages/stages.bin test/* test_image/boot/
cp -rv bin/* test/* test_image/boot/
genisoimage -no-emul-boot -b boot/limine-cd.bin -o test.iso test_image/
qemu-system-x86_64 -net none -smp 4 -enable-kvm -cpu host -cdrom test.iso -debugcon stdio

View File

@ -259,54 +259,26 @@ static bool _device_write(const void *buffer, uint64_t loc, size_t count) {
goto cleanup; \
} while (0)
extern uint8_t _binary_limine_bin_start[], _binary_limine_bin_end[];
extern uint8_t _binary_limine_sys_start[], _binary_limine_sys_end[];
extern uint8_t _binary_bin_limine_hdd_bin_start[], _binary_bin_limine_hdd_bin_end[];
int main(int argc, char *argv[]) {
int ok = 1;
uint8_t *bootloader_img = _binary_limine_bin_start;
uint8_t *bootloader_img = _binary_bin_limine_hdd_bin_start;
size_t bootloader_file_size =
(size_t)_binary_limine_bin_end - (size_t)_binary_limine_bin_start;
uint8_t *stage3_img = _binary_limine_sys_start;
size_t stage3_file_size =
(size_t)_binary_limine_sys_end - (size_t)_binary_limine_sys_start;
(size_t)_binary_bin_limine_hdd_bin_end - (size_t)_binary_bin_limine_hdd_bin_start;
uint8_t orig_mbr[70], timestamp[6];
char *limine_sys_path = NULL;
int limine_sys = -1;
if (sizeof(off_t) != 8) {
fprintf(stderr, "ERROR: off_t type is not 64-bit.\n");
goto cleanup;
}
if (argc < 3) {
printf("Usage: %s <boot directory> <device> [GPT partition index]\n", argv[0]);
if (argc < 2) {
printf("Usage: %s <device> [GPT partition index]\n", argv[0]);
goto cleanup;
}
#define MAX_STAGE3_PATH 1024
limine_sys_path = malloc(MAX_STAGE3_PATH);
if (limine_sys_path == NULL) {
perror("ERROR");
goto cleanup;
}
snprintf(limine_sys_path, MAX_STAGE3_PATH, "%s/limine.sys", argv[1]);
limine_sys = creat(limine_sys_path, 0644);
if (limine_sys == -1) {
perror("ERROR");
goto cleanup;
}
if (write(limine_sys, stage3_img, stage3_file_size) !=
(ssize_t)stage3_file_size) {
perror("ERROR");
goto cleanup;
}
device = open(argv[2], O_RDWR);
device = open(argv[1], O_RDWR);
if (device == -1) {
perror("ERROR");
goto cleanup;
@ -358,7 +330,7 @@ int main(int argc, char *argv[]) {
if (gpt) {
if (argc > 3) {
uint32_t partition_num;
sscanf(argv[3], "%" SCNu32, &partition_num);
sscanf(argv[2], "%" SCNu32, &partition_num);
partition_num--;
if (partition_num > gpt_header.number_of_partition_entries) {
fprintf(stderr, "ERROR: Partition number is too large.\n");
@ -502,10 +474,6 @@ cleanup:
free(cache);
if (device != -1)
close(device);
if (limine_sys_path != NULL)
free(limine_sys_path);
if (limine_sys != -1)
close(limine_sys);
return ok;
}

View File

@ -62,9 +62,9 @@ skip_bpb:
call read_file
jc err
; Find and load '/BOOT/STAGES.BIN'
mov ebx, TXT_STAGES
mov cl, TXT_STAGES_SZ
; Find and load '/BOOT/LIMINE.SYS'
mov ebx, TXT_LIMINE
mov cl, TXT_LIMINE_SZ
call read_file ; esi is set from the last call
jc err
@ -82,7 +82,7 @@ err:
jmp err
%include 'iso9660.asm'
%include '../hddboot/gdt.inc'
%include '../gdt.asm'
BITS 32
pmode:
@ -102,8 +102,8 @@ pmode:
TXT_BOOT: db "BOOT"
TXT_BOOT_SZ equ $ - TXT_BOOT
TXT_STAGES: db "STAGES.BIN;1"
TXT_STAGES_SZ equ $ - TXT_STAGES
TXT_LIMINE: db "LIMINE.SYS;1"
TXT_LIMINE_SZ equ $ - TXT_LIMINE
; Just making sure the entry point (ISO9660_BUFFER) is not reached
times (0x8000 - 0x7C00) - ($ - $$) db 0

View File

@ -89,8 +89,8 @@ times 6 db 0
; Includes
%include 'disk.inc'
%include 'gdt.inc'
%include 'disk.asm'
%include '../gdt.asm'
bits 32
vector:
@ -126,9 +126,9 @@ dw 0xaa55
; ********************* Stage 2 *********************
decompressor:
incbin '../decompressor/decompressor.bin'
incbin '../../decompressor/decompressor.bin'
align 16
stage2:
incbin '../stages/stage2.bin.gz'
incbin '../../stage23/stage2.bin.gz'
.size: equ $ - stage2

View File

@ -50,15 +50,15 @@ err:
; Includes
%include '../hddboot/gdt.inc'
%include '../gdt.asm'
; ********************* Stage 2 *********************
decompressor:
incbin '../decompressor/decompressor.bin'
incbin '../../decompressor/decompressor.bin'
align 16
stage2:
incbin '../stages/stage2.bin.gz'
incbin '../../stage23/stage2.bin.gz'
.size: equ $ - stage2
.fullsize: equ $ - decompressor

View File

@ -35,8 +35,7 @@ INTERNAL_LDFLAGS = \
-nostdlib \
-no-pie \
-z max-page-size=0x1000 \
-static \
-Tlinker.ld
-static
.PHONY: all clean
@ -45,26 +44,25 @@ ASM_FILES := $(shell find -L ./ -type f -name '*.asm' | sort)
OBJ := $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o)
HEADER_DEPS := $(C_FILES:.c=.d)
all: stages.map stages.bin stage2.bin stage2.bin.gz stage3.bin
all: limine.sys stage2.bin stage2.bin.gz
stage2.bin.gz: stage2.bin
gzip -n -9 < stage2.bin > stage2.bin.gz
stage2.bin: stages.bin
dd if=stages.bin bs=$$(( 0x$$($(READELF) -S stages.elf | grep .stage3 | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) count=1 of=$@
stage2.bin: limine.sys
dd if=limine.sys bs=$$(( 0x$$($(READELF) -S limine.elf | grep .stage3.text | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) count=1 of=$@
stage3.bin: stages.bin
dd if=stages.bin bs=$$(( 0x$$($(READELF) -S stages.elf | grep .stage3 | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) skip=1 of=$@
limine.map.o: limine_nomap.elf
./gensyms.sh $(OBJDUMP) limine_nomap.elf limine
stages.map: stages.elf
./gensyms.sh $(OBJDUMP)
nasm symlist.gen -f bin -o $@
stages.bin: stages.elf
limine.sys: limine.elf
$(OBJCOPY) -O binary $< $@
stages.elf: $(OBJ)
$(LD) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $@
limine_nomap.elf: $(OBJ)
$(LD) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@
limine.elf: $(OBJ) limine.map.o
$(LD) $(OBJ) limine.map.o $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@
-include $(HEADER_DEPS)
@ -75,4 +73,4 @@ stages.elf: $(OBJ)
nasm $< -f elf32 -o $@
clean:
rm -f symlist.gen stages.elf stages.map stages.bin stage2.bin stage2.bin.gz stage3.bin $(OBJ) $(HEADER_DEPS)
rm -f symlist.gen limine.elf limine.sys stage2.bin stage2.bin.gz $(OBJ) $(HEADER_DEPS)

24
stage23/gensyms.sh Executable file
View File

@ -0,0 +1,24 @@
#!/bin/sh
set -e
TMP1=$(mktemp)
TMP2=$(mktemp)
TMP3=$(mktemp)
TMP4=$(mktemp)
$1 -t "$2" | sed '/\bd\b/d' | sort > "$TMP1"
grep "\.text" < "$TMP1" | cut -d' ' -f1 > "$TMP2"
grep "\.text" < "$TMP1" | awk 'NF{ print $NF }' > "$TMP3"
echo "section .map" > "$TMP4"
echo "global $3_map" >> "$TMP4"
echo "$3_map:" >> "$TMP4"
paste -d'$' "$TMP2" "$TMP3" | sed 's/^/dd 0x/g' | sed 's/$/", 0/g' | sed 's/\$/\ndb "/g' >> "$TMP4"
echo "dd 0xffffffff" >> "$TMP4"
nasm -f elf32 "$TMP4" -o $3.map.o
rm "$TMP1" "$TMP2" "$TMP3" "$TMP4"

View File

@ -43,7 +43,7 @@ uint64_t strtoui(const char *s, const char **end, int base);
#define SIZEOF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
typedef void *symbol[];
typedef char symbol[];
#define stage3_text __attribute__((section(".stage3_text")))
#define stage3_data __attribute__((section(".stage3_data")))

View File

@ -27,7 +27,12 @@ int init_config_disk(struct volume *part) {
panic("Could not open stage 3");
}
fread(&stage3, stage3_addr, 0, stage3.size);
print("%x %x %x\n", stage3_addr, (uintptr_t)stage3_addr - 0x8000,
stage3.size - (uintptr_t)stage3_addr - 0x8000);
fread(&stage3, stage3_addr,
(uintptr_t)stage3_addr - 0x8000,
stage3.size - ((uintptr_t)stage3_addr - 0x8000));
stage3_loaded = true;
}

View File

@ -8,45 +8,30 @@
#include <fs/file.h>
#include <mm/pmm.h>
static char *stage2_map = NULL;
void trace_init(void) {
char *map_filename = config_get_value(NULL, 0, "STAGE2_MAP");
if (map_filename == NULL)
return;
struct file_handle stage2_map_file;
if (!uri_open(&stage2_map_file, map_filename))
panic("Could not open stage2 map file `%s`", map_filename);
stage2_map = ext_mem_alloc(stage2_map_file.size);
fread(&stage2_map_file, stage2_map, 0, stage2_map_file.size);
print("trace: Stage 2 map file `%s` loaded.\n", map_filename);
}
extern symbol limine_map;
char *trace_address(size_t *off, size_t addr) {
if (!stage2_map)
if (!stage3_loaded)
return NULL;
uint32_t prev_addr = 0;
char *prev_sym = NULL;
for (size_t i = 0;;) {
if (*((uint32_t *)&stage2_map[i]) >= addr) {
if (*((uint32_t *)&limine_map[i]) >= addr) {
*off = addr - prev_addr;
return prev_sym;
}
prev_addr = *((uint32_t *)&stage2_map[i]);
prev_addr = *((uint32_t *)&limine_map[i]);
i += sizeof(uint32_t);
prev_sym = &stage2_map[i];
while (stage2_map[i++] != 0);
prev_sym = &limine_map[i];
while (limine_map[i++] != 0);
}
}
void print_stacktrace(size_t *base_ptr) {
if (!stage2_map) {
print("trace: Stack trace omitted due to unavailable map file.\n");
if (!stage3_loaded) {
print("trace: Stack trace omitted because stage 3 was not loaded yet.\n");
return;
}

View File

@ -3,7 +3,6 @@
#include <stdint.h>
void trace_init(void);
char *trace_address(size_t *off, size_t addr);
void print_stacktrace(size_t *base_ptr);

View File

@ -13,18 +13,28 @@ SECTIONS
KEEP(*(.realmode*))
}
.stage2 : {
.stage2.text : {
*(.text*)
}
.stage2.data : {
*(.data*)
*(.rodata*)
}
.stage3 : {
.stage3.text : {
stage3_addr = .;
*(.stage3_text*)
}
.stage3.data : {
*(.stage3_data*)
}
.map : {
KEEP(*(.map*))
}
.bss : {
bss_begin = .;
*(COMMON)

48
stage23/linker_nomap.ld Normal file
View File

@ -0,0 +1,48 @@
OUTPUT_FORMAT(elf32-i386)
ENTRY(_start)
SECTIONS
{
. = 0x8000;
.entry : {
KEEP(*(.entry*))
}
.realmode : {
KEEP(*(.realmode*))
}
.stage2.text : {
*(.text*)
}
.stage2.data : {
*(.data*)
*(.rodata*)
}
.stage3.text : {
stage3_addr = .;
*(.stage3_text*)
}
.stage3.data : {
*(.stage3_data*)
}
.map : {
limine_map = .;
}
.bss : {
bss_begin = .;
*(COMMON)
*(.bss*)
bss_end = .;
}
/DISCARD/ : {
*(*)
}
}

View File

@ -87,8 +87,6 @@ void entry(uint8_t _boot_drive, int boot_from) {
break;
}
trace_init();
char *cmdline;
char *config = menu(&cmdline);

Some files were not shown because too many files have changed in this diff Show More