chore: display kernel version

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-07-14 11:22:43 +02:00
parent bd7468e69c
commit cfeeea957c
12 changed files with 299 additions and 241 deletions

1
.gitignore vendored
View file

@ -35,3 +35,4 @@ bochsrc.bxrc
*.mod *.mod
webring.json webring.json
webring.txt webring.txt
kernel/const.inc

View file

@ -17,6 +17,10 @@ SRCS = kernel.asm \
.PHONY: all .PHONY: all
all: $(KERNEL) all: $(KERNEL)
PHONY: const.inc
const.inc: const.inc.in
sh $(TOOLSDIR)/version.sh $< $@
$(KERNEL): $(SRCS) $(KERNEL): $(SRCS)
$(AS) kernel.asm $@ $(AS) kernel.asm $@

View file

@ -7,9 +7,11 @@ PSIZE = 0x1000
STPDBOOT_MAGIC = 0x53545044 STPDBOOT_MAGIC = 0x53545044
; --------- VERSION ------------- ; --------- VERSION -------------
VERSION_MAJOR = 1 VERSION_MAJOR = @MAJOR@
VERSION_MINOR = 0 VERSION_MINOR = @MINOR@
VERSION_COMMIT = "@COMMIT@" VERSION_COMMIT equ "@COMMIT@"
VERSION_FULL equ "@FULLVERSION@"
BUILD_DATE equ "@DATE@"
; ------------- OTHER ----------- ; ------------- OTHER -----------
CR = 0x0D CR = 0x0D

View file

@ -16,7 +16,10 @@ vga_console_clear:
rep stosb rep stosb
ret ret
;; Function: vga_console_putc
;;
;; In:
;; AL - charactere to print
vga_console_putc: vga_console_putc:
ret
ret

View file

@ -1,3 +0,0 @@
irq_timer:
iret

View file

@ -1,9 +1,15 @@
;; File: isr.inc
macro ISR [name,error] { macro ISR [name,error] {
forward forward
dd isr_#name dd isr_#name
forward
local szIntName
szIntName db `name#, 0
forward forward
isr_#name#: isr_#name#:
cli cli
mov esi, szIntName
call klog
if error eq 0 if error eq 0
push 0 push 0
end if end if
@ -25,6 +31,17 @@ idt_setup:
inc ecx inc ecx
cmp ecx, 32 cmp ecx, 32
jb @b jb @b
@@:
mov eax, irq_dummy
mov [aInterruptGate + (ecx * 8)], ax
mov [aInterruptGate + (ecx * 8) + 2], word 0x08
mov [aInterruptGate + (ecx * 8) + 5], word 0x8E
shr eax, 16
mov [aInterruptGate + (ecx * 8) + 6], ax
inc ecx
cmp ecx, 0x30
jb @b
mov ecx, 0x42 mov ecx, 0x42
mov eax, isr_syscall mov eax, isr_syscall
@ -49,6 +66,9 @@ aInterruptGate:
.end: .end:
irq_dummy:
iret
isr_common: isr_common:
pusha pusha
@ -61,6 +81,9 @@ isr_common:
mov fs, ax mov fs, ax
mov gs, ax mov gs, ax
mov eax, [esp+IntFrame.intno]
push eax
mov esi, szMsgInterrupt mov esi, szMsgInterrupt
call klog call klog
@ -110,4 +133,4 @@ ISR \
security_exception, 0, \ security_exception, 0, \
reserved31, 0 reserved31, 0
szMsgInterrupt db "Interrupt", 0 szMsgInterrupt db "Interrupt (int: %x)", 0

View file

@ -63,7 +63,7 @@ kmain:
call idt_setup call idt_setup
int 0x42 ;int 0x42
.halt: .halt:
hlt hlt
@ -84,7 +84,7 @@ kmain:
include 'pic.inc' include 'pic.inc'
szMsgKernelAlive db "Kernel is alive", 0 szMsgKernelAlive db "Kernel (", VERSION_FULL , ") is alive", 0
szErrorBootProtocol db "Error: wrong magic number", 0 szErrorBootProtocol db "Error: wrong magic number", 0
boot_structure BootInfo boot_structure BootInfo

View file

@ -50,6 +50,20 @@ pic_init:
ret ret
;; Function: pic_eoi
;;
;; In:
;; EAX - irq
pic_eoi:
cmp eax, 8
jb @f
mov al, PIC_EOI
out PIC2_COMMAND, al
@@:
mov al, PIC_EOI
out PIC1_COMMAND, al
ret
pic_disable: pic_disable:
mov al, 0xFF mov al, 0xFF
out PIC1_DATA, al out PIC1_DATA, al

View file

@ -133,7 +133,7 @@ struc IDTGate {
;; > +--------------- |----------------+-----------------|-----------------+ ;; > +--------------- |----------------+-----------------|-----------------+
struc intframe { struc IntFrame {
;; registers ;; registers
.edi dd ? .edi dd ?
.esi dd ? .esi dd ?
@ -161,3 +161,7 @@ struc intframe {
.useresp dd ? .useresp dd ?
.ss dd ? .ss dd ?
} }
virtual at 0
IntFrame IntFrame
sizeof.IntFrame:
end virtual

View file

@ -1,5 +1,7 @@
SYSCALL_EXIT = 0x01 SYSCALL_EXIT = 0x01
SYSCALL_FORK = 0x02 SYSCALL_FORK = 0x02
SYSCALL_READ = 0x03
SYSCALL_WRITE = 0x04
isr_syscall: isr_syscall:
push eax push eax

View file

@ -1,226 +1,226 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <libgen.h> #include <libgen.h>
#include <errno.h> #include <errno.h>
typedef struct typedef struct
{ {
uint8_t jmp[3]; uint8_t jmp[3];
uint8_t OEM[8]; uint8_t OEM[8];
uint16_t bytes_per_sector; uint16_t bytes_per_sector;
uint8_t sectors_per_cluster; uint8_t sectors_per_cluster;
uint16_t reserved_sectors; uint16_t reserved_sectors;
uint8_t number_of_FATs; uint8_t number_of_FATs;
uint16_t root_entries; uint16_t root_entries;
uint16_t logical_sectors; uint16_t logical_sectors;
uint8_t media_descriptor; uint8_t media_descriptor;
uint16_t sectors_per_FAT; uint16_t sectors_per_FAT;
uint16_t sectors_per_track; uint16_t sectors_per_track;
uint16_t heads_per_cylinder; uint16_t heads_per_cylinder;
uint32_t hidden_sectors; uint32_t hidden_sectors;
uint32_t total_sectors; uint32_t total_sectors;
/* EBPB */ /* EBPB */
uint8_t drive_number; uint8_t drive_number;
uint8_t flags; uint8_t flags;
uint8_t signature; uint8_t signature;
uint32_t serial; uint32_t serial;
uint8_t label[11]; uint8_t label[11];
uint8_t fs_type[8]; uint8_t fs_type[8];
} __attribute__((packed)) BiosParamBlock; } __attribute__((packed)) BiosParamBlock;
typedef struct typedef struct
{ {
uint8_t name[8]; uint8_t name[8];
uint8_t ext[3]; uint8_t ext[3];
uint8_t attr; uint8_t attr;
uint16_t reserved0; uint16_t reserved0;
uint16_t creation_time; uint16_t creation_time;
uint16_t creation_date; uint16_t creation_date;
uint16_t access_date; uint16_t access_date;
uint16_t reserved1; uint16_t reserved1;
uint16_t mod_time; uint16_t mod_time;
uint16_t mod_date; uint16_t mod_date;
uint16_t start; uint16_t start;
uint32_t size; uint32_t size;
} __attribute__((packed)) Entry; } __attribute__((packed)) Entry;
static const char *prg_name; static const char *prg_name;
static int sector_size = 512; static int sector_size = 512;
static void static void
fatal(const char *str, ...) fatal(const char *str, ...)
{ {
va_list ap; va_list ap;
fprintf(stderr, "%s: ", prg_name); fprintf(stderr, "%s: ", prg_name);
va_start(ap, str); va_start(ap, str);
vfprintf(stderr, str, ap); vfprintf(stderr, str, ap);
va_end(ap); va_end(ap);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
static void static void
read_sector(uint8_t *dest, int sect, FILE *fp) read_sector(uint8_t *dest, int sect, FILE *fp)
{ {
if (fseek(fp, sect*sector_size, SEEK_SET) != 0) goto fatal_sect; if (fseek(fp, sect*sector_size, SEEK_SET) != 0) goto fatal_sect;
if (fread(dest, 1, sector_size, fp) != sector_size) goto fatal_sect; if (fread(dest, 1, sector_size, fp) != sector_size) goto fatal_sect;
return; return;
fatal_sect: fatal_sect:
fatal("Can't read sector %d", sect); fatal("Can't read sector %d", sect);
} }
static void static void
dump_bpb(BiosParamBlock *bpb) dump_bpb(BiosParamBlock *bpb)
{ {
char label[12]; char label[12];
printf("Bytes per logical sector: %hd\n", bpb->bytes_per_sector); printf("Bytes per logical sector: %hd\n", bpb->bytes_per_sector);
sector_size = bpb->bytes_per_sector; sector_size = bpb->bytes_per_sector;
printf("Logical sectors per cluster: %d\n", bpb->sectors_per_cluster); printf("Logical sectors per cluster: %d\n", bpb->sectors_per_cluster);
printf("Reserved logical sectors: %hd\n", bpb->reserved_sectors); printf("Reserved logical sectors: %hd\n", bpb->reserved_sectors);
printf("Number of FATs: %d\n", bpb->number_of_FATs); printf("Number of FATs: %d\n", bpb->number_of_FATs);
printf("Root directory entries: %hd\n", bpb->root_entries); printf("Root directory entries: %hd\n", bpb->root_entries);
printf("Total logical sectors: %hd\n", bpb->logical_sectors); printf("Total logical sectors: %hd\n", bpb->logical_sectors);
printf("Logical sectors per FAT: %hd\n", bpb->sectors_per_FAT); printf("Logical sectors per FAT: %hd\n", bpb->sectors_per_FAT);
printf("Serial: %X\n", bpb->serial); printf("Serial: %X\n", bpb->serial);
memset(label, 0, 12); memset(label, 0, 12);
strncpy(label, bpb->label, 11); strncpy(label, bpb->label, 11);
printf("Label: '%s'\n", label); printf("Label: '%s'\n", label);
printf("signature: %X\n", bpb->signature); printf("signature: %X\n", bpb->signature);
} }
static void static void
dump_entry(Entry *entry) dump_entry(Entry *entry)
{ {
char filename[9]; char filename[9];
char ext[4]; char ext[4];
int i; int i;
if (entry->name[0] == 0x0) return; if (entry->name[0] == 0x0) return;
memset(filename, 0, 9); memset(filename, 0, 9);
strncpy(filename, entry->name, 8); strncpy(filename, entry->name, 8);
memset(ext, 0, 4); memset(ext, 0, 4);
strncpy(ext, entry->ext, 3); strncpy(ext, entry->ext, 3);
for (i = 7; i > 0; i--) for (i = 7; i > 0; i--)
{ {
if (filename[i] == ' ') if (filename[i] == ' ')
{ {
filename[i] = '\0'; filename[i] = '\0';
} }
else else
{ {
break; break;
} }
} }
printf("%s.%s\n", filename, ext); printf("%s.%s\n", filename, ext);
} }
static void static void
dump(const char *img) dump(const char *img)
{ {
FILE *fp; FILE *fp;
BiosParamBlock *bpb; BiosParamBlock *bpb;
uint8_t buffer[512]; uint8_t buffer[512];
int root_start; int root_start;
int root_size; int root_size;
int i; int i;
int j; int j;
fp = fopen(img, "rb"); fp = fopen(img, "rb");
if (fp == NULL) fatal("Can't open '%s': %s", img, strerror(errno)); if (fp == NULL) fatal("Can't open '%s': %s", img, strerror(errno));
read_sector(buffer, 0, fp); read_sector(buffer, 0, fp);
if (buffer[511] != 0xAA || buffer[510] != 0x55) if (buffer[511] != 0xAA || buffer[510] != 0x55)
{ {
fatal("MBR not found"); fatal("MBR not found");
} }
bpb = (BiosParamBlock *)buffer; bpb = (BiosParamBlock *)buffer;
dump_bpb(bpb); dump_bpb(bpb);
root_start = bpb->sectors_per_FAT * bpb->number_of_FATs; root_start = bpb->sectors_per_FAT * bpb->number_of_FATs;
root_start += bpb->reserved_sectors; root_start += bpb->reserved_sectors;
root_size = bpb->root_entries / (sector_size >> 0x5); root_size = bpb->root_entries / (sector_size >> 0x5);
for (i = 0; i < root_size; i++) for (i = 0; i < root_size; i++)
{ {
read_sector(buffer, root_start + i, fp); read_sector(buffer, root_start + i, fp);
for (j = 0; j < sector_size; j+=32) for (j = 0; j < sector_size; j+=32)
{ {
dump_entry((Entry *)(buffer + j)); dump_entry((Entry *)(buffer + j));
} }
} }
fclose(fp); fclose(fp);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
static void static void
usage(int retcode) usage(int retcode)
{ {
if (retcode == EXIT_FAILURE) if (retcode == EXIT_FAILURE)
{ {
fprintf(stderr, "Try '%s -h' form more information.\n", prg_name); fprintf(stderr, "Try '%s -h' form more information.\n", prg_name);
} }
else else
{ {
printf("Usage: %s [-hV] [-D IMG] [-b BIN]\n", prg_name); printf("Usage: %s [-hV] [-D IMG] [-b BIN]\n", prg_name);
printf("\t-h\tdisplay this help and exit\n"); printf("\t-h\tdisplay this help and exit\n");
printf("\t-V\toutput version information\n"); printf("\t-V\toutput version information\n");
printf("\t-D IMG\tdump fs information from IMG\n"); printf("\t-D IMG\tdump fs information from IMG\n");
printf("\nReport bugs to <%s>\n", MK_BUGREPORT); printf("\nReport bugs to <%s>\n", MK_BUGREPORT);
} }
exit(retcode); exit(retcode);
} }
static void static void
version(void) version(void)
{ {
printf("%s commit %s\n", prg_name, MK_COMMIT); printf("%s commit %s\n", prg_name, MK_COMMIT);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
prg_name = argv[0]; prg_name = argv[0];
while ((argc > 1) && (argv[1][0] == '-')) while ((argc > 1) && (argv[1][0] == '-'))
{ {
switch (argv[1][1]) switch (argv[1][1])
{ {
case 'h': case 'h':
usage(EXIT_SUCCESS); usage(EXIT_SUCCESS);
break; break;
case 'V': case 'V':
version(); version();
break; break;
case 'D': case 'D':
argv++; argv++;
argc--; argc--;
if (argc <= 1) usage(EXIT_FAILURE); if (argc <= 1) usage(EXIT_FAILURE);
dump(argv[1]); dump(argv[1]);
break; break;
default: default:
usage(EXIT_FAILURE); usage(EXIT_FAILURE);
break; break;
} }
argv++; argv++;
argc--; argc--;
} }
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

10
tools/git-version → tools/version.sh Executable file → Normal file
View file

@ -6,6 +6,9 @@ if [ ! -n "$vers" ]; then
vers="0.0" vers="0.0"
fi fi
major="$(echo -n "${vers}" | cut -d. -f1)"
minor="$(echo -n "${vers}" | cut -d. -f1)"
commit="$(git rev-parse --short HEAD)" commit="$(git rev-parse --short HEAD)"
full_vers="${vers}-${commit}" full_vers="${vers}-${commit}"
@ -13,4 +16,9 @@ if [ -n "$(git status)" ]; then
full_vers="${full_vers}-dirty" full_vers="${full_vers}-dirty"
fi fi
echo -n "${full_vers}" sed -e "s/@MAJOR@/${major}/g" \
-e "s/@MINOR@/${minor}/g" \
-e "s/@COMMIT@/${commit}/g"\
-e "s/@FULLVERSION@/${full_vers}/g" \
-e "s/@DATE@/$(date)/g" \
"$1" > "$2"