diff --git a/build.sh b/build.sh deleted file mode 100755 index 885d01a..0000000 --- a/build.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/sh - -set -e - -BIN='vendor/cross/root/bin' -SRC='src/x86' -CROSS="$BIN/i386-elf-" - -STAGE1_LDFLAGS='--defsym=program_start=0x7c00' -STAGE2_LDFLAGS='--defsym=program_start=0x7e00' - -STAGE1_OBJS="$SRC/common.o $SRC/stage1.o" -STAGE2_OBJS="$SRC/common.o $SRC/stage2.o $SRC/main.o" - -./clean.sh - -${CROSS}gcc -c $SRC/common.S -o $SRC/common.o -${CROSS}gcc -c $SRC/stage1.S -o $SRC/stage1.o -${CROSS}gcc -c $SRC/stage2.S -o $SRC/stage2.o -${CROSS}gcc -c $SRC/main.c -o $SRC/main.o - -${CROSS}ld -T$SRC/linker.ld -o $SRC/stage1.bin $STAGE1_LDFLAGS $STAGE1_OBJS -${CROSS}ld -T$SRC/linker.ld -o $SRC/stage2.bin $STAGE2_LDFLAGS $STAGE2_OBJS - -./loadwarka mbr mbr.bin $SRC/stage1.bin -cat mbr.bin $SRC/stage2.bin > disk.img diff --git a/clean.sh b/clean.sh deleted file mode 100755 index ffe6c12..0000000 --- a/clean.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -set -e - -rm -fv disk.img mbr.bin src/*.o src/x86/*.bin src/x86/*.o diff --git a/run.sh b/run.sh deleted file mode 100755 index e076954..0000000 --- a/run.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -set -e - -./build.sh - -qemu-system-i386 disk.img diff --git a/src/main.c b/src/main.c index 5d0d327..9836228 100644 --- a/src/main.c +++ b/src/main.c @@ -1,125 +1,4 @@ -#include -#include -#include -#include - -#include - -static bool create_mbr_file( - bool print_debug, - const char *output_filename, - uint32_t disk_id, - const char *bootstrap_filename -); - int main(const int argc, char **const argv) { - if (argc < 2) { - fprintf(stderr, "Invalid usage\n"); - return 1; - } - - const char *const format = argv[1]; - - if (strcmp(format, "mbr") == 0) { - if (argc != 3 && argc != 4) { - fprintf(stderr, "Invalid usage\n"); - return 1; - } - - const char *const output_filename = argv[2]; - const char *const bootstrap_filename = argc == 3 ? NULL :argv[3]; - - const bool result = create_mbr_file( - true, - output_filename, - 0xf01834d0, - bootstrap_filename - ); - if (!result) return 1; - } else { - fprintf(stderr, "Invalid format: %s\n", format); - } - return 0; } - -bool create_mbr_file( - const bool print_debug, - const char *const output_filename, - const uint32_t disk_id, - const char *const bootstrap_filename -) { - struct KernAux_Mbr mbr; - - { - memset(&mbr, 0, sizeof(mbr)); - - mbr.info.magic = KERNAUX_MBR_MAGIC; - mbr.info.disk_id = disk_id; - mbr.info.reserved = 0; - - // 00 - mbr.info.entries[0].drive_attributes = 0; - // 00 02 00 - mbr.info.entries[0].first_sector_chs_addr = 0x0200; - // 01 - mbr.info.entries[0].partition_type = 1; - // 02 03 00 - mbr.info.entries[0].last_sector_chs_addr = 0x0302; - // 01 00 00 00 - mbr.info.entries[0].first_sector_lba_addr = 0x01; - // 80 00 00 00 - mbr.info.entries[0].sectors_count = 0x80; - } - - if (bootstrap_filename) { - FILE *fd = fopen(bootstrap_filename, "r"); - if (fd == NULL) { - fprintf(stderr, "Can't open bootstrap file\n"); - return false; - } - - const size_t size = fread(&mbr.bootstrap, 1, KERNAUX_MBR_BOOTSTRAP_SIZE, fd); - if (size == 0) { - fprintf(stderr, "Empty bootstrap file\n"); - return false; - } - if (!feof(fd)) { - fprintf(stderr, "Too long bootstrap file\n"); - return false; - } - - fclose(fd); - } - - uint8_t *mbr_ptr = (uint8_t*)&mbr; - - if (print_debug) { - for (size_t i = 0; i < 512 / 16; ++i) { - printf("%02x", mbr_ptr[i * 16]); - for (size_t j = 1; j < 16; ++j) { - printf(" %02x", mbr_ptr[i * 16 + j]); - } - printf("\n"); - } - } - - { - FILE *fd = fopen(output_filename, "wb"); - if (fd == NULL) { - fprintf(stderr, "Can't open image file\n"); - return false; - } - - const size_t size = fwrite(mbr_ptr, 1, sizeof(mbr), fd); - if (size != sizeof(mbr)) { - fprintf(stderr, "Can't write image file\n"); - return false; - } - - fclose(fd); - } - - return true; -} diff --git a/src/x86/common.S b/src/x86/common.S deleted file mode 100644 index bfc1453..0000000 --- a/src/x86/common.S +++ /dev/null @@ -1,101 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#define STACK_BASE 0x2000 - -.code16 -.global newline -.global disk_number -.global restore_disk_number -.global hang -.global print_char -.global print_str -.global print_number - -.section .rodata -newline: .string "\r\n" - -.section .data -disk_number: .byte 0 - -.section .header - ljmp $0, $init - -.align 4 -.word program_size - -init: - cli - xor %ax, %ax - mov %ax, %ss - mov %ax, %ds - mov %ax, %es - mov %ax, %fs - mov %ax, %gs - mov $STACK_BASE, %sp - mov %dl, disk_number - ljmp $0, $_start - -.section .text -restore_disk_number: - mov $disk_number, %bx - mov (%bx), %dl - ret - -hang: - cli - hlt - jmp hang - -do_ret: - ret - -// AL - char -print_char: - mov $0x0E, %ah - mov $0x0001, %bx - int $0x10 - ret - -// SI - string pointer -print_str: - lodsb - test %al, %al - jz do_ret - call print_char - jmp print_str - -// AX - number -print_number: - test %ax, %ax - jnz print_number_notnull - mov $0x0e, %ah - mov $'0', %al - int $0x10 - ret - -// AX - number -print_number_notnull: - test %ax, %ax - jz do_ret - - // DX - dividend high (always zero) - // AX - dividend low - xor %dx, %dx - // BX - divisor (always 10) - mov $10, %bx - // AX - quotient - // DX - remainder - div %bx - - push %dx - call print_number_notnull - pop %dx - - mov $0x0e, %ah - mov %dl, %al - add $'0', %al - int $0x10 - - ret diff --git a/src/x86/linker.ld b/src/x86/linker.ld deleted file mode 100644 index 89e885c..0000000 --- a/src/x86/linker.ld +++ /dev/null @@ -1,28 +0,0 @@ -OUTPUT_ARCH(i386) -OUTPUT_FORMAT(binary) -ENTRY(_start) - -program_size = program_end - program_start; - -SECTIONS -{ - . = program_start; - - .text : ALIGN(4) - { - *(.header); - *(.text); - } - - .rodata : ALIGN(4) - { - *(.rodata); - } - - .data : ALIGN(4) - { - *(.data); - } - - program_end = .; -} diff --git a/src/x86/main.c b/src/x86/main.c deleted file mode 100644 index f1226da..0000000 --- a/src/x86/main.c +++ /dev/null @@ -1,7 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -int main() -{ -} diff --git a/src/x86/stage1.S b/src/x86/stage1.S deleted file mode 100644 index 865e7c1..0000000 --- a/src/x86/stage1.S +++ /dev/null @@ -1,181 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#define STAGE2_BASE 0x7e00 -#define STAGE2_SIZE (STAGE2_BASE + 8) - -.code16 -.global _start - -.section .rodata -size_str: .string "Stage 2 size: " -reading_sector: .string "Reading sector " -parenth: .string " (" -of: .string " of " -to_address: .string ") to address " - -.section .text -_start: - mov $1, %ax - mov $STAGE2_BASE, %bx - xor %cx, %cx - call print_reading_state - - mov $0x02, %ah - mov $1, %al // Sectors count - mov $0x80, %dl // Drive - mov $0, %ch // Cylinder - mov $0, %dh // Head - mov $2, %cl // Cylinder and sector - mov $STAGE2_BASE, %bx // Address - int $0x13 - - mov $STAGE2_SIZE, %bx - mov (%bx), %ax - call print_stage2_size - - // DX - dividend high (always zero) - xor %dx, %dx - // AX - dividend low - mov $STAGE2_SIZE, %bx - mov (%bx), %ax - // BX - divisor (always 512) - mov $512, %bx - // AX - quotient - // DX - remainder - div %bx - - test %dx, %dx - jz no_rem - inc %ax -no_rem: - - mov %ax, %cx - mov $STAGE2_BASE, %bx - mov $1, %ax - - // AX - current sector index - // BX - current address - // CX - total number of sectors -read_loop: - cmp %ax, %cx - je finish - - add $512, %bx - inc %ax - - push %ax - push %bx - push %cx - - call print_reading_state - - pop %cx - pop %bx - pop %ax - push %ax - push %bx - push %cx - - add $1, %ax - mov %al, %cl // Cylinder and sector - mov $0x02, %ah - mov $1, %al // Sectors count - mov $0x80, %dl // Drive - mov $0, %ch // Cylinder - mov $0, %dh // Head - int $0x13 - - pop %cx - pop %bx - pop %ax - jmp read_loop - -finish: - mov $newline, %si - call print_str - call restore_disk_number - ljmp $0, $STAGE2_BASE - -// AX - size -print_stage2_size: - push %ax - - mov $size_str, %si - call print_str - - pop %ax - call print_number - - mov $newline, %si - call print_str - - ret - -// AX - current sector index -// BX - current address -// CX - total number of sectors -print_reading_state: - push %ax - push %bx - push %cx - - mov $reading_sector, %si - call print_str - - pop %cx - pop %bx - pop %ax - push %ax - push %bx - push %cx - - inc %ax - call print_number - - mov $parenth, %si - call print_str - - pop %cx - pop %bx - pop %ax - push %ax - push %bx - push %cx - - call print_number - - mov $of, %si - call print_str - - pop %cx - // pop %bx - // pop %ax - // push %ax - // push %bx - push %cx - - mov %cx, %ax - call print_number - - mov $to_address, %si - call print_str - - pop %cx - pop %bx - // pop %ax - // push %ax - push %bx - push %cx - - mov %bx, %ax - call print_number - - mov $newline, %si - call print_str - - pop %cx - pop %bx - pop %ax - ret diff --git a/src/x86/stage2.S b/src/x86/stage2.S deleted file mode 100644 index c4fbbbd..0000000 --- a/src/x86/stage2.S +++ /dev/null @@ -1,30 +0,0 @@ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -.code16 -.global _start - -.section .rodata -disk_number: .string "Disk number: " -main_returned: .string "Main returned\r\n" - -.section .text -_start: - mov $disk_number, %si - call print_str - - call restore_disk_number - xor %ax, %ax - mov %dl, %al - call print_number - - mov $newline, %si - call print_str - - call main - - mov $main_returned, %si - call print_str - - ljmp $0, $hang