diff --git a/Makefile b/Makefile index c390e83..0305c0e 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ QEMU_COMMON = \ SUBDIRS := external tools include boot kernel lib bin -TARGET = stupid.tar.gz floppy1440.img floppy2880.img +TARGET = stupid.tar.gz floppy1440.img floppy2880.img ifneq ($(OS),Windows_NT) EXEXT = TARGET += stupid.iso stupid.hdd @@ -56,7 +56,10 @@ stupid.iso: $(SUBDIRS) .PHONY: stupid.hdd stupid.hdd: $(SUBDIRS) - @echo "" + dd if=/dev/zero of=$@ bs=1M count=0 seek=64 + mformat -L 12 -i $@ +# mcopy -i $@ boot/loader/stpdldr.sys ::/STPDLDR.SYS +# mcopy -i $@ kernel/vmstupid.sys ::/VMSTUPID.SYS .PHONY: stupid.tar.gz stupid.tar.gz: $(SUBDIRS) diff --git a/boot/bootsect/Makefile b/boot/bootsect/Makefile index a9baec3..45a0e70 100644 --- a/boot/bootsect/Makefile +++ b/boot/bootsect/Makefile @@ -1,11 +1,16 @@ TARGET = boot_floppy1440.bin \ boot_floppy2880.bin \ - boot_mbr.bin + boot_mbr.bin \ + boot_hdd.bin FLOPPY_SRCS = floppy.asm \ ../common/const.inc \ ../common/fat12.inc +HDD_SRCS = hdd.asm \ + ../common/const.inc \ + ../common/fat12.inc + MBR_SRCS = mbr.asm \ ../common/const.inc \ ../common/fat12.inc @@ -22,6 +27,9 @@ boot_floppy2880.bin: $(FLOPPY_SRCS) boot_mbr.bin: $(MBR_SRCS) $(AS) mbr.asm $@ +boot_hdd.bin: $(HDD_SRCS) + $(AS) hdd.asm $@ + .PHONY: clean clean: $(RM) $(TARGET) diff --git a/boot/bootsect/floppy.asm b/boot/bootsect/floppy.asm index 04527ba..25daef1 100644 --- a/boot/bootsect/floppy.asm +++ b/boot/bootsect/floppy.asm @@ -108,11 +108,72 @@ _start: hlt jmp $ + ; CHS to LBA + ; LBA = (C * HPC + H) * SPT + (S - 1) + + ;; Function: disk_read_sectors + ;; Read sectors from disk to buffer + ;; + ;; Parameters: + ;; + ;; AX - LBA starting sector + ;; CX - sector count + ;; ES:BX - buffer + ;; +disk_read_sectors: + ; https://en.wikipedia.org/wiki/Logical_block_addressing + ; convert LBA to CHS + ; HPC = Head per Cluster + ; SPT = Sector per Track + + ; S = (LBA % SPT) + 1 + push ax + push bx + push cx + xor dx, dx + div word [sectors_per_track] + inc dx + mov [S], dx + + ; H = (LBA / SPT) % HPC + ; C = LBA / (HPC * SPT) + xor dx, dx + div word [heads_per_cylinder] + mov [C], ax + mov [H], dx + + ; read sectors + mov ah, 0x2 + mov al, 0x1 + mov ch, byte [C] + mov cl, byte [S] + mov dh, byte [H] + mov dl, [drive_number] + + int 0x13 + jc @f + pop cx + pop bx + pop ax + add bx, word [bytes_per_sector] + inc ax + loop disk_read_sectors + ret +@@: + mov si, msg_error_sector + call bios_print + ret + +C dw 0x00 +H dw 0x00 +S dw 0x00 + include '../common/bios.inc' include '../common/fat12.inc' -msg_error db "ERROR: ", 0 -msg_not_found db " not found", CR, LF, 0 +msg_error db "ERROR: ", 0 +msg_not_found db " not found", CR, LF, 0 +msg_error_sector db "reading sector", CR, LF, 0 stage1_file db "STPDLDR SYS", 0 diff --git a/boot/bootsect/hdd.asm b/boot/bootsect/hdd.asm index 3045647..280e321 100644 --- a/boot/bootsect/hdd.asm +++ b/boot/bootsect/hdd.asm @@ -1,4 +1,4 @@ - ;; File: floppy.asm + ;; File: hdd.asm format binary use16 @@ -38,12 +38,90 @@ _start: int 0x13 jc .error_lba + call fat_load_root + + ; search + mov si, stage1_file + call fat_search_root + jc .error_not_found + mov [stage1_start], ax + + xor ax, ax + mov al, [FAT_count] + mul word [sectors_per_FAT] + mov cx, ax + mov ax, [reserved_sectors] + + xor bx, bx + + call disk_read_sectors + + ; load stage 2 + mov ax, LOADER_BASE/0x10 + mov es, ax + mov ax, [stage1_start] + xor bx, bx + call fat_load_binary + + mov dl, [drive_number] + jmp 0x0:LOADER_BASE + +.error_not_found: + mov si, msg_error_not_found + jmp .error .error_lba: mov si, msg_error_13ext - jmp .error_print +.error: + push si + mov si, msg_error + call bios_print + pop si + call bios_print + hlt + jmp $ + ;; Function: disk_read_sectors + ;; Read sectors from disk to buffer + ;; + ;; Parameters: + ;; + ;; AX - LBA starting sector + ;; CX - sector count + ;; ES:BX - buffer + ;; +disk_read_sectors: + mov word [disk_packet.sectors], cx + mov word [disk_packet.segment], es + mov word [disk_packet.offset], bx + mov word [disk_packet.lba], ax + mov ds, [disk_packet] + mov dl, [drive_number] + mov ah, 0x42 + int 0x13 + ret -msg_error_13ext db "We don't support CHS", CR, LF, 0 +include '../common/bios.inc' +include '../common/fat12.inc' + +msg_error db "ERROR: ", 0 +msg_error_13ext db "We don't support CHS", CR, LF, 0 +msg_error_not_found db "loader not found", CR, LF, 0 + +stage1_file db "STPDLDR SYS", 0 +stage1_start dw 0x0 + +disk_packet: + db 0x10 + db 0 +.sectors: + dw 0 +.segment: + dw 0 +.offset: + dw 0 +.lba: + dd 0 + dd 0 rb 0x7C00+512-2-$ db 0x55, 0xAA diff --git a/boot/common/bios.inc b/boot/common/bios.inc index 0713b05..f1c2056 100644 --- a/boot/common/bios.inc +++ b/boot/common/bios.inc @@ -1,5 +1,16 @@ ;; File: bios.inc +struc bios_disk_packet +{ + .size db 0x10 + .zero db 0 + .sectors dw ? + .segment dw ? + .offset dw ? + .lba_lower dd ? + .lba_upper dd ? +} + ;; Function: bios_print ;; ;; Parameters: diff --git a/boot/common/fat12.inc b/boot/common/fat12.inc index 77a59f3..c1297e5 100644 --- a/boot/common/fat12.inc +++ b/boot/common/fat12.inc @@ -24,66 +24,6 @@ struc fat_entry } defn fat_entry - ; CHS to LBA - ; LBA = (C * HPC + H) * SPT + (S - 1) - - ;; Function: disk_read_sectors - ;; Read sectors from disk to buffer - ;; - ;; Parameters: - ;; - ;; AX - LBA starting sector - ;; CX - sector count - ;; ES:BX - buffer - ;; -disk_read_sectors: - ; https://en.wikipedia.org/wiki/Logical_block_addressing - ; convert LBA to CHS - ; HPC = Head per Cluster - ; SPT = Sector per Track - - ; S = (LBA % SPT) + 1 - push ax - push bx - push cx - xor dx, dx - div word [sectors_per_track] - inc dx - mov [S], dx - - ; H = (LBA / SPT) % HPC - ; C = LBA / (HPC * SPT) - xor dx, dx - div word [heads_per_cylinder] - mov [C], ax - mov [H], dx - - ; read sectors - mov ah, 0x2 - mov al, 0x1 - mov ch, byte [C] - mov cl, byte [S] - mov dh, byte [H] - mov dl, [drive_number] - - int 0x13 - jc @f - pop cx - pop bx - pop ax - add bx, word [bytes_per_sector] - inc ax - loop disk_read_sectors - ret -@@: - mov si, msg_error_sector - call bios_print - ret - -C dw 0x00 -H dw 0x00 -S dw 0x00 - ;; Function: fat_load_root fat_load_root: mov ax, DISK_BUFFER/0x10 @@ -185,6 +125,4 @@ fat_load_binary: jb fat_load_binary ret - -msg_error_sector db "ERROR: reading sector", CR, LF, 0 data_start dw 0x0 diff --git a/boot/efi/bootia32.asm b/boot/efi/bootia32.asm index 9ddddda..bdf399e 100644 --- a/boot/efi/bootia32.asm +++ b/boot/efi/bootia32.asm @@ -38,7 +38,17 @@ efimain: call [ebx + EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString] add esp, 8 + + ; load config + + ; #=======================# ; search and load kernel + ; openVolume() + ; for path in search_path + ; if (open(path + file) == ok) + ; break + ; if not found + ; error ; get memory map diff --git a/boot/loader/disk.inc b/boot/loader/disk.inc new file mode 100644 index 0000000..ef312b4 --- /dev/null +++ b/boot/loader/disk.inc @@ -0,0 +1,76 @@ +struc +{ + .boot_jmp db 3 dup(?) +} + +disk_read_sectors: + test byte [drive_lba], 1 + je .lba_read + push ax + push bx + push cx + xor dx, dx + div word [sectors_per_track] + inc dx + mov [S], dx + xor dx, dx + div word [heads_per_cylinder] + mov [C], ax + mov [H], dx + mov ah, 0x2 + mov al, 0x1 + mov ch, byte [C] + mov cl, byte [S] + mov dh, byte [H] + mov dl, [drive_number] + int 0x13 + jc @f + pop cx + pop bx + pop ax + add bx, word [bytes_per_sector] + inc ax + loop disk_read_sectors + ret +@@: + mov si, msg_error_sector + call bios_print + ret +.lba_read: + mov word [disk_packet.sectors], cx + mov word [disk_packet.segment], es + mov word [disk_packet.offset], bx + mov word [disk_packet.lba], ax + mov ds, [disk_packet] + mov dl, [drive_number] + mov ah, 0x42 + int 0x13 + ret + + +C dw 0x00 +H dw 0x00 +S dw 0x00 + +disk_packet: + db 0x10 + db 0 +.sectors: + dw 0 +.segment: + dw 0 +.offset: + dw 0 +.lba: + dd 0 + dd 0 + + +sectors_per_track dw 0 +heads_per_cylinder dw 0 +bytes_per_sector dw 0 +sectors_per_FAT dw 0 +FAT_count db 0 +reserved_sectors dw 0 +root_dir_entries dw 0 +sectors_per_cluster db 0 diff --git a/boot/loader/loader.asm b/boot/loader/loader.asm index 1dd432b..b920a89 100644 --- a/boot/loader/loader.asm +++ b/boot/loader/loader.asm @@ -46,7 +46,7 @@ _start: mov dl, [drive_number] cmp dl, 0x7F ; skip disk extension check - jle @f + jle @f ; check disk extensions mov ah, 0x41 @@ -60,7 +60,14 @@ _start: ; +---------+--------+---------+ ; | bootsec | sect 1 | stpd sb | ; +---------+--------+---------+ + ; 0 512 1024 + ; ; for now fat12 is asumed + call fat_load_root + + mov si, kernel_fat12_file + call fat_search_root + jc .error_not_found ; fetch memory map from bios @@ -87,6 +94,9 @@ _start: mov ss, ax jmp 0x8:common32 +.error_not_found: + mov si, msg_error_not_found + jmp .error .error_memory: mov si, msg_error_memory jmp .error @@ -94,12 +104,15 @@ _start: mov si, msg_error_a20 .error: call bios_log + @@: hlt jmp @b include 'a20.inc' include '../common/bios.inc' + include '../common/fat12.inc' + include 'disk.inc' include 'logger.inc' include 'memory.inc' include 'video.inc' @@ -113,6 +126,8 @@ kernel_fat12_file db "VMSTUPIDSYS", 0 config_fat12_file db "BOOT INI", 0 msg_error_a20 db "ERROR: can't enable a20 line", 0 msg_error_memory db "ERROR: can't detect available memory", 0 +msg_error_sector db "ERROR: reading sector", CR, LF, 0 +msg_error_not_found db "ERROR: kernel not found", 0 use32 ; ========================================================================= diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..ec6a7f2 --- /dev/null +++ b/build.sh @@ -0,0 +1,88 @@ +#!/usr/bin/env bash + +# ----------------------------------------------------------------------------- +# log reporting +# ----------------------------------------------------------------------------- +plain() { + local mesg=$1; shift + + # shellcheck disable=SC2059 + printf "${BOLD} ${mesg}${ALL_OFF}\n" "$@" +} + +msg() { + local mesg=$1; shift + + # shellcheck disable=SC2059 + printf "${MAGENTA}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" +} + +msg2() { + local mesg=$1; shift + + # shellcheck disable=SC2059 + printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" +} + +error() { + local mesg=$1; shift + + # shellcheck disable=SC2059 + printf "${RED}==> ERROR:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" + exit 1 +} + +warning() { + local mesg=$1; shift + + # shellcheck disable=SC2059 + printf "${YELLOW}==> WARNING:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" +} + +success() { + local mesg=$1; shift + + # shellcheck disable=SC2059 + printf "${GREEN}==> SUCCESS:${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" +} + +# ----------------------------------------------------------------------------- +# entry +# ----------------------------------------------------------------------------- +LC_ALL=C +export LC_ALL + +unset INFODIR +unset LESSCHARSET +unset MAKEFLAGS +unset TERMINFO + +unset ALL_OFF BOLD RED GREEN BLUE MAGENTA YELLOW +if [ ! -v NO_COLOR ]; then + ALL_OFF="\e[1;0m" + BOLD="\e[1;1m" + BLUE="${BOLD}\e[1;34m" + GREEN="${BOLD}\e[1;32m" + RED="${BOLD}\e[1;31m" + MAGENTA="${BOLD}\e[1;35m" + YELLOW="${BOLD}\e[1;33m" +fi +readonly ALL_OFF BOLD RED GREEN BLUE MAGENTA YELLOW + +unset topdir prgname build_start +prgname="$(basename $0)" +topdir="$(realpath "$0")" +topdir="$(dirname "${topdir}")" +build_start="$(date)" +readonly topdir prgname build_start + +SRC_DIR="${topdir}" +BUILD_DIR="${topdir}/.build" +TOOLS_DIR="${topdir}/.tools" +TOOLS_PREFIX="stpd-" + +if [ $# -eq 0 ]; then + printf "Try '%s -h' for more information.\n" "${prgname}" >&2 + exit 1 +fi + diff --git a/kernel/intro.txt b/kernel/intro.txt index 460fee6..1cb5cdd 100644 --- a/kernel/intro.txt +++ b/kernel/intro.txt @@ -1,22 +1,22 @@ About: kernel memory map -> -> Physical map -> +---------------+ -> | | -> +---------------+ -> | ISA hole ? | -> 0x00F00000 +---------------+ -> | RAM | -> +---------------+ -> | kernel | -> 0x00100000 +---------------+ -> | ROM | -> 0x000BFFFF +---------------+ -> | video display | -> | memory | -> 0x0009FFFF +---------------+ -> | RAM | -> | | -> 0x00000000 +---------------+ -> +> +> Physical map +> +---------------+ +> | | +> +---------------+ +> | ISA hole ? | +> 0x00F00000 +---------------+ +> | RAM | +> +---------------+ +> | kernel | +> 0x00100000 +---------------+ +> | ROM | +> 0x000BFFFF +---------------+ +> | video display | +> | memory | +> 0x0009FFFF +---------------+ +> | RAM | +> | | +> 0x00000000 +---------------+ +> diff --git a/kernel/kernel.asm b/kernel/kernel.asm index 86052bf..1f175ee 100644 --- a/kernel/kernel.asm +++ b/kernel/kernel.asm @@ -4,7 +4,10 @@ org KBASE use32 - jmp kmain + jmp short kmain + +db 'STPDKRNL' +db 32 dup(0) include 'klog.inc' include 'mm/mm.inc' diff --git a/releasetools/cdimage.sh b/releasetools/cdimage.sh new file mode 100644 index 0000000..0660e5a --- /dev/null +++ b/releasetools/cdimage.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e + +if [ ! -f build.sh ] +then + exit 1 +fi + +. ./releasetools/image.defaults +. ./releasetools/image.functions + +DESTDIR=${BUILDDIR}/iso + diff --git a/releasetools/floppyimage.sh b/releasetools/floppyimage.sh new file mode 100644 index 0000000..abdce9b --- /dev/null +++ b/releasetools/floppyimage.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -e + +if [ ! -f build.sh ] +then + exit 1 +fi + +. ./releasetools/image.defaults +. ./releasetools/image.functions diff --git a/releasetools/hdimage.sh b/releasetools/hdimage.sh new file mode 100644 index 0000000..abdce9b --- /dev/null +++ b/releasetools/hdimage.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -e + +if [ ! -f build.sh ] +then + exit 1 +fi + +. ./releasetools/image.defaults +. ./releasetools/image.functions diff --git a/releasetools/image.defaults b/releasetools/image.defaults new file mode 100644 index 0000000..36fc06a --- /dev/null +++ b/releasetools/image.defaults @@ -0,0 +1,3 @@ +: ${BUILDDIR=.build} +: ${OBJ=${BUILDDIR}/obj} +: ${DESTDIR=${BUILDDIR}/dist} diff --git a/releasetools/image.functions b/releasetools/image.functions new file mode 100644 index 0000000..2e2b4de --- /dev/null +++ b/releasetools/image.functions @@ -0,0 +1,18 @@ +create_grub_cfg() +{ + grub_config=$(cat < +#include + +static const char *prg_name = "boot-install"; + +static void +usage(int retval) +{ + if (retval == EXIT_FAILURE) + { + fprintf(stderr, "Try '%s -h' for more informations.\n", prg_name); + } + else + { + printf("Usage: %s [-hV]\n", prg_name); + printf("Flags:\n"); + printf("\t-h\tdisplay this menu.\n"); + printf("\nReport bug to: <%s>\n", MK_BUGREPORT); + } + + exit(retval); +} + +static void +version(void) +{ + printf("%s (%s) %s", prg_name, MK_PACKAGE, MK_COMMIT); + exit(EXIT_SUCCESS); +} + +int +main(int argc, char **argv) +{ + + return (EXIT_SUCCESS); +} diff --git a/sbin/cryptotools/main.c b/sbin/cryptotools/main.c new file mode 100644 index 0000000..9933069 --- /dev/null +++ b/sbin/cryptotools/main.c @@ -0,0 +1,71 @@ +#include +#include +#include + +static char *prg_name; + +struct kernel_header { + uint8_t jump[3]; + uint8_t magic[8]; + uint8_t signature[32]; +}; + +static void +do_keygen() +{ + uint8_t pk[crypto_sign_ed25519_PUBLICKEYBYTES]; + uint8_t sk[crypto_sign_ed25519_SECRETKEYBYTES]; +} + +static void +usage(int retcode) +{ + if (retcode == EXIT_FAILURE) + { + fprintf(stderr, "Try '%s -h' form more information.\n", prg_name); + } + else + { + printf("Usage: %s [-hV] [sign|verify|keygen]\n", prg_name); + printf("\t-h\tdisplay this help and exit\n"); + printf("\t-V\toutput version information\n"); + + printf("\nReport bugs to <%s>\n", MK_BUGREPORT); + } + + exit(retcode); +} + +static void +version(void) +{ + printf("%s commit %s\n", prg_name, MK_COMMIT); + exit(EXIT_SUCCESS); +} + +int +main(int argc, char **argv) +{ + prg_name = argv[0]; + + if (sodium_init() < 0) abort(); + + while ((argc > 1) && (argv[1][0] == '-')) + { + switch (argv[1][1]) + { + case 'h': + usage(EXIT_SUCCESS); + break; + case 'V': + version(); + } + + argv++; + argc--; + } + + if (argc <= 1) usage(EXIT_FAILURE); + + return (EXIT_SUCCESS); +}