chore: code update, but no new feature

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-05-02 13:34:27 +02:00
parent 0d4c772300
commit 6ff773de41
13 changed files with 312 additions and 93 deletions

View file

@ -10,11 +10,20 @@ INCDIR = /usr/include
ASMDIR = /usr/asm
AS = fasm
CC ?= gcc
RM = rm -f
MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\"
MK_COMMIT := \"$(shell git rev-parse --short HEAD)\"
ifneq (,$(findstring cl,$(CC)))
CFLAGS = /TC /D MK_COMMIT="$(MK_COMMIT)" /DMK_BUGREPORT="$(MK_BUGREPORT)" /INCLUDE:$(TOPDIR)include
LDFLAGS = /SUBSYSTEM:CONSOLE
else
CFLAGS = -DMK_COMMIT="$(MK_COMMIT)" -DMK_BUGREPORT="$(MK_BUGREPORT)" -I$(TOPDIR)include
LDFLAGS =
endif
SUBDIRS := external tools include boot kernel lib bin
TARGET = stupid.tar.gz floppy1440.img floppy2880.img

49
boot/bootsect/hdd.asm Normal file
View file

@ -0,0 +1,49 @@
;; File: floppy.asm
format binary
use16
include '../common/const.inc'
include '../common/macro.inc'
jmp short _start
OEM_identifier db 'STUPID '
bytes_per_sector dw 512
sectors_per_cluster db 0
reserved_sectors dw 0
FAT_count db 0
root_dir_entries dw 0
total_sectors dw 0
media_desc_type db 0
sectors_per_FAT dw 0
sectors_per_track dw 18
heads_per_cylinder dw 2
hidden_sectors dd 0
large_sector_count dd 0
; Extended Boot Record
drive_number db 0
reserved db 0
signature db 0
volume_id dd 0xB00B135 ; hope mine will grow :'(
volume_label db 'Stupid Boot'
system_id db ' '
_start:
mov [drive_number], dl
; ensure int 13h extension
mov ah, 0x41
mov bx, 0x55AA
int 0x13
jc .error_lba
.error_lba:
mov si, msg_error_13ext
jmp .error_print
msg_error_13ext db "We don't support CHS", CR, LF, 0
rb 0x7C00+512-2-$
db 0x55, 0xAA

View file

@ -20,6 +20,7 @@
mov di, MBR_BASE
rep movsw
jmp 0x0:start
start:
; set LBA
mov ah, 0x41

View file

@ -36,6 +36,8 @@ _start:
call a20_enable
jc .error_a20
xchg bx, bx
; check drive type
; dl <= 0x7F == floppy
; dl >= 0x80 == hard drive
@ -55,10 +57,15 @@ _start:
@@:
; detect filesystem (FAT12/16 or StpdFS)
; load kernel from filesystem
; +---------+--------+---------+
; | bootsec | sect 1 | stpd sb |
; +---------+--------+---------+
; for now fat12 is asumed
; fetch memory map from bios
call memory_get_map
jc .error_memory
call memory_get_map
jc .error_memory
; video information
call video_setup
@ -121,7 +128,8 @@ multiboot:
; get kernel from module
common32:
mov dword [0xb8000], 0x07690748
xchg bx, bx
mov dword [0xB8000], 0x07690748
; paging
; identity map first 1MB

View file

@ -29,7 +29,51 @@ bios_log_time:
ret
;; Function: bios_log_number
;;
;; Parameters:
;; XX - number
;;
bios_log_number:
push ebp
mov ebp, esp
sub esp, 10
leave
ret
;; Function: bios_log_hex
;;
bios_log_hex:
push bp
mov ebp, esp
sub esp, 12
mov si, hex_prefix
call bios_print
or edi, edi
jz .print_zero
.loop:
or edi, edi
jz .print_number
rol edi, 4
mov eax, edi
and eax, 0xF
mov al, [hex_digits + eax]
mov [esp], al
inc esp
jmp .loop
.print_zero:
mov al, '0'
mov ah, 0x0E
int 0x10
jmp .end
.print_number:
mov [esp], byte 0
mov si, [ebp - 12]
call bios_print
.end:
leave
ret
;; Function: bios_log
@ -43,12 +87,50 @@ bios_log:
call bios_log_time
pop si
.loop:
mov al, [si]
or al, al
jz .end
cmp al, '%'
jne .putchar
inc si
mov al, [si]
cmp al, '%'
je .putchar
cmp al, 's'
jne .check_x
mov di, si
pop ax
pop si
push ax
call bios_print
mov si, di
jmp .next
.check_x:
cmp al, 'x'
jne .unknown_format
pop ax
pop edi
push ax
push si
call bios_log_hex
pop si
jmp .next
.unknown_format:
mov al, '?'
.putchar:
mov ah, 0x0E
int 0x10
.next:
inc si
jmp .loop
.end:
mov si, endline
call bios_print
ret
time db '[00:00.00] ', 0
hex_prefix db '0x', 0
hex_digits db '0123456789ABCDEF'
endline db CR, LF, 0

View file

@ -1,46 +1,48 @@
KBASE = 0xC0000000
PSIZE = 0x1000
; --------- VERSION -------------
VERSION_MAJOR = 1
VERSION_MINOR = 0
; --------- Registers ------------
CR0_PE = 0x00000001
CR0_MP = 0x00000002
CR0_EM = 0x00000004
CR0_TS = 0x00000008
CR0_ET = 0x00000010
CR0_NE = 0x00000020
CR0_WP = 0x00010000
CR0_AM = 0x00040000
CR0_NW = 0x20000000
CR0_CD = 0x40000000
CR0_PG = 0x80000000
CR3_PWT = 0x08
CR3_PCD = 0x10
CR4_VME = 0x0000001
CR4_PVI = 0x0000002
CR4_TSD = 0x0000004
CR4_DE = 0x0000008
CR4_PSE = 0x0000010
CR4_PAE = 0x0000020
CR4_MCE = 0x0000040
CR4_PGE = 0x0000080
CR4_PCE = 0x0000100
CR4_OSDXSR = 0x0000200
CR4_OSXMMEXCPT = 0x0000400
CR4_UMIP = 0x0000800
CR4_VMXE = 0x0002000
CR4_SMXE = 0x0004000
CR4_FSGSBASE = 0x0010000
CR4_PCIDE = 0x0020000
CR4_OSXSAVE = 0x0040000
CR4_SMEP = 0x0100000
CR4_SMAP = 0x0200000
CR4_PKE = 0x0400000
CR4_CET = 0x0800000
CR4_PKS = 0x1000000
KBASE = 0xC0000000
PSIZE = 0x1000
STPDBOOT_MAGIC = 0x53545044
; --------- VERSION -------------
VERSION_MAJOR = 1
VERSION_MINOR = 0
; --------- Registers ------------
CR0_PE = 0x00000001
CR0_MP = 0x00000002
CR0_EM = 0x00000004
CR0_TS = 0x00000008
CR0_ET = 0x00000010
CR0_NE = 0x00000020
CR0_WP = 0x00010000
CR0_AM = 0x00040000
CR0_NW = 0x20000000
CR0_CD = 0x40000000
CR0_PG = 0x80000000
CR3_PWT = 0x08
CR3_PCD = 0x10
CR4_VME = 0x0000001
CR4_PVI = 0x0000002
CR4_TSD = 0x0000004
CR4_DE = 0x0000008
CR4_PSE = 0x0000010
CR4_PAE = 0x0000020
CR4_MCE = 0x0000040
CR4_PGE = 0x0000080
CR4_PCE = 0x0000100
CR4_OSDXSR = 0x0000200
CR4_OSXMMEXCPT = 0x0000400
CR4_UMIP = 0x0000800
CR4_VMXE = 0x0002000
CR4_SMXE = 0x0004000
CR4_FSGSBASE = 0x0010000
CR4_PCIDE = 0x0020000
CR4_OSXSAVE = 0x0040000
CR4_SMEP = 0x0100000
CR4_SMAP = 0x0200000
CR4_PKE = 0x0400000
CR4_CET = 0x0800000
CR4_PKS = 0x1000000

22
kernel/intro.txt Normal file
View file

@ -0,0 +1,22 @@
About: kernel memory map
>
> Physical map
> +---------------+
> | |
> +---------------+
> | ISA hole ? |
> 0x00F00000 +---------------+
> | RAM |
> +---------------+
> | kernel |
> 0x00100000 +---------------+
> | ROM |
> 0x000BFFFF +---------------+
> | video display |
> | memory |
> 0x0009FFFF +---------------+
> | RAM |
> | |
> 0x00000000 +---------------+
>

View file

@ -6,6 +6,7 @@
jmp kmain
include 'klog.inc'
include 'mm/mm.inc'
;; Function: kmain
@ -17,7 +18,16 @@
;;
kmain:
; TODO: interupt, vmm
nop
cmp eax, STPDBOOT_MAGIC
jne .halt
KLOG_INIT
KLOG "kernel alive"
.halt:
hlt
jmp $
_edata:

5
kernel/klog.inc Normal file
View file

@ -0,0 +1,5 @@
macro KLOG_INIT {
}
macro KLOG msg {
}

0
kernel/mm/pmm.inc Normal file
View file

View file

@ -3,40 +3,19 @@
#include <stdint.h>
#include <string.h>
#include <errno.h>
#define MBR_MAGIC0 0x55
#define MBR_MAGIC1 0xAA
#define MBR_PART_BOOTABLE (1 << 7)
enum {
MBR_PART_TYPE_EMPTY = 0x00,
MBR_PART_TYPE_FAT12 = 0x01,
MBR_PART_TYPE_FAT16 = 0x04,
MBR_PART_TYPE_GPT_PROTECTIVE = 0xEE,
MBR_PART_TYPE_EFI = 0xEF
};
typedef struct partition {
uint8_t attributes;
uint8_t chs_start[3];
uint8_t type;
uint8_t chs_last_sector[3];
uint32_t lba_start;
uint32_t sectors_count;
} __attribute__((packed)) Partition;
typedef struct mbr_header {
uint8_t raw[436];
uint8_t uid[10];
struct partition part[4];
uint8_t magic[2];
} __attribute__((packed)) MBRHeader;
#include <libgen.h>
#include "mbr.h"
static char *prg_name = NULL;
static int dump_info = 0;
static const char *diskpath = NULL;
static FILE *diskfd = NULL;
static FILE *diskfp = NULL;
static FILE *partfp[4] = {
NULL,
NULL,
NULL,
NULL
};
static MBRHeader header;
static void
@ -88,8 +67,9 @@ usage(int retcode)
printf("\t-h\tdisplay this help and exit\n");
printf("\t-V\toutput version information.\n");
printf("\t-d\tdump disk information\n");
printf("\t-p0-3\t\n");
printf("\t-o out\twrite to file 'out'\n");
printf("\t-e\textract parts\n");
printf("\t-e\textract\n");
printf("\t-i img\t\n");
printf("\nReport bugs to <%s>\n", MK_BUGREPORT);
}
@ -107,7 +87,8 @@ version(void)
int
main(int argc, char **argv)
{
prg_name = argv[0];
char c;
prg_name = basename(argv[0]);
while ((argc > 1) && (argv[1][0] == '-'))
{
@ -122,6 +103,23 @@ main(int argc, char **argv)
case 'd':
dump_info = 1;
break;
case 'p':
c = argv[1][2] - '0';
argv++;
argc--;
if (c != 0 && c != 1 && c != 2 && c != 3)
{
usage(EXIT_FAILURE);
}
partfp[c] = fopen(argv[2], "rb");
if (partfp[c] == NULL)
{
fprintf(stderr, "%s: %s\n", diskpath, strerror(errno));
return (EXIT_FAILURE);
}
break;
case 'e':
break;
default:
usage(EXIT_FAILURE);
}
@ -133,14 +131,14 @@ main(int argc, char **argv)
if (argc <= 1) usage(EXIT_FAILURE);
diskpath = argv[1];
diskfd = fopen(diskpath, "rb");
if (diskfd == NULL)
diskfp = fopen(diskpath, "rb");
if (diskfp == NULL)
{
fprintf(stderr, "%s: %s\n", diskpath, strerror(errno));
return (EXIT_FAILURE);
}
if (fread(&header, sizeof(uint8_t), sizeof(MBRHeader), diskfd) != sizeof(MBRHeader))
if (fread(&header, sizeof(uint8_t), sizeof(MBRHeader), diskfp) != sizeof(MBRHeader))
{
fprintf(stderr, "%s: can't read mbr header\n", diskpath);
return (EXIT_FAILURE);

35
sbin/parted/mbr.h Normal file
View file

@ -0,0 +1,35 @@
#ifndef PARTED_MBR_H
# define PARTED_MBR_H 1
# include <stdint.h>
# define MBR_MAGIC0 0x55
# define MBR_MAGIC1 0xAA
# define MBR_PART_BOOTABLE (1 << 7)
enum {
MBR_PART_TYPE_EMPTY = 0x00,
MBR_PART_TYPE_FAT12 = 0x01,
MBR_PART_TYPE_FAT16 = 0x04,
MBR_PART_TYPE_GPT_PROTECTIVE = 0xEE,
MBR_PART_TYPE_EFI = 0xEF
};
typedef struct partition {
uint8_t attributes;
uint8_t chs_start[3];
uint8_t type;
uint8_t chs_last_sector[3];
uint32_t lba_start;
uint32_t sectors_count;
} __attribute__((packed)) Partition;
typedef struct mbr_header {
uint8_t raw[436];
uint8_t uid[10];
struct partition part[4];
uint8_t magic[2];
} __attribute__((packed)) MBRHeader;
#endif /* !PARTED_MBR_H */

View file

@ -1,11 +1,5 @@
CC = gcc
RM = rm -f
TARGET = fat$(EXEXT) coff-ld$(EXEXT) parted$(EXEXT) mkfs.stpd$(EXEXT)
CFLAGS = -DMK_COMMIT="$(MK_COMMIT)" -DMK_BUGREPORT="$(MK_BUGREPORT)" -I../include
LDFLAGS =
.PHONY: all
all: $(TARGET)
@ -16,7 +10,11 @@ coff-ld$(EXEXT): coff-ld.c
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
parted$(EXEXT): ../sbin/parted/main.c
ifneq (,$(findstring cl,$(CC)))
$(CC) /Fo:$@ $^ $(CFLAGS) $(LDFLAGS)
else
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
endif
mkfs.stpd$(EXEXT): ../sbin/mkfs.stpd/main.c
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)