chore: sync repo

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-05-28 06:54:10 +02:00
parent 974b0862bf
commit 3b064c7d24
19 changed files with 544 additions and 92 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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:

View file

@ -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

View file

@ -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

76
boot/loader/disk.inc Normal file
View file

@ -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

View file

@ -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
; =========================================================================

88
build.sh Normal file
View file

@ -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

View file

@ -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 +---------------+
>

View file

@ -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'

13
releasetools/cdimage.sh Normal file
View file

@ -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

View file

@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -e
if [ ! -f build.sh ]
then
exit 1
fi
. ./releasetools/image.defaults
. ./releasetools/image.functions

10
releasetools/hdimage.sh Normal file
View file

@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -e
if [ ! -f build.sh ]
then
exit 1
fi
. ./releasetools/image.defaults
. ./releasetools/image.functions

View file

@ -0,0 +1,3 @@
: ${BUILDDIR=.build}
: ${OBJ=${BUILDDIR}/obj}
: ${DESTDIR=${BUILDDIR}/dist}

View file

@ -0,0 +1,18 @@
create_grub_cfg()
{
grub_config=$(cat <<EOF
set timeout=15
set default=0
menuentry "StupidOS" {
echo "verify system integrity"
hashsum --hash sha256 --check /boot/hashfile --prefix /
multiboot /stpdldr.sys
module /vmstupid.sys
boot
}
EOF )
}

36
sbin/boot-install/main.c Normal file
View file

@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
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);
}

71
sbin/cryptotools/main.c Normal file
View file

@ -0,0 +1,71 @@
#include <stdlib.h>
#include <stdio.h>
#include <sodium.h>
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);
}