feat(tools): dump bpb info
Some checks are pending
Build / test (push) Waiting to run
Docs / test (push) Waiting to run

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-03-03 15:07:47 +01:00
parent aa5ef02a2c
commit e71c7ce555
4 changed files with 178 additions and 6 deletions

3
.gitignore vendored
View file

@ -2,6 +2,7 @@
*.img
*.bin
*.iso
*.exe
*~
\#*\#
@ -11,7 +12,7 @@
vmstupid
vmstupid-dbg
/tools/fat
/sysroot
log.txt

View file

@ -1,14 +1,22 @@
.EXPORT_ALL_VARIABLES:
TOPDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
SYSROOTDIR := $(TOPDIR)/sysroot
TOOLSDIR := $(TOPDIR)/tools
RM = echo
SUBDIRS := boot kernel lib bin
MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\"
MK_COMMIT := \"$(shell git rev-parse --short HEAD)\"
SUBDIRS := tools boot kernel lib bin
TARGET = stupid.tar.gz floppy_boot.img
ifneq ($(OS),Windows_NT)
TARGET += stupid.iso
EXEXT =
TARGET += stupid.iso stdupid.hdd
else
EXEXT = .exe
endif
.PHONY: all
@ -26,17 +34,32 @@ $(SUBDIRS):
stupid.iso: $(SUBDIRS)
$(TOPDIR)/tools/create-iso $@ sysroot
.PHONY: stupid.hdd
stupid.hdd: $(SUBDIRS)
@echo ""
.PHONY: stupid.tar.gz
stupid.tar.gz: $(SUBDIRS)
tar -czvf $@ sysroot
.PHONY: floppy_boot.img
floppy_boot.img:
floppy_boot.img: $(SUBDIRS)
dd if=/dev/zero of=$@ bs=512 count=1440
mformat -C -f 1440 -i $@
dd if=boot/bootsector.bin of=$@ conv=notrunc
mcopy -i $@ boot/stpdboot.sys ::/STPDBOOT.SYS
mcopy -i $@ kernel/vmstupid ::/VMSTUPID.SYS
mcopy -i $@ kernel/vmstupid.sys ::/VMSTUPID.SYS
.PHONY: run
run: all
qemu-system-i386 \
-rtc base=localtime \
-drive file=floppy_boot.img,if=none,format=raw,id=boot \
-drive file=fat:rw:./sysroot,if=none,id=hdd \
-device floppy,drive=boot \
-device ide-hd,drive=hdd \
-global isa-fdc.bootindexA=0 \
-serial mon:stdio
.PHONY: clean
clean: $(SUBDIRS)

16
tools/Makefile Normal file
View file

@ -0,0 +1,16 @@
CC = gcc
RM = rm -f
TARGET = fat$(EXEXT)
CFLAGS = -DMK_COMMIT="$(MK_COMMIT)" -DMK_BUGREPORT="$(MK_BUGREPORT)"
LDFLAGS =
.PHONY: all
all: $(TARGET)
fat$(EXEXT): fat.c
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
.PHONY: install
install: $(TARGET)

132
tools/fat.c Normal file
View file

@ -0,0 +1,132 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <libgen.h>
#include <errno.h>
typedef struct
{
uint8_t jmp[3];
uint8_t OEM[8];
uint16_t bytes_per_sector;
uint8_t sectors_per_cluster;
uint16_t reserved_sectors;
uint8_t number_of_FATs;
uint16_t root_entries;
uint16_t logical_sectors;
uint8_t media_descriptor;
uint16_t sectors_per_FAT;
uint16_t sectors_per_track;
uint16_t heads_per_cylinder;
uint32_t hidden_sectors;
uint32_t total_sectors;
/* EBPB */
uint8_t drive_number;
uint8_t flags;
uint8_t signature;
uint32_t serial;
uint8_t label[11];
uint8_t fs_type[8];
} __attribute__((packed)) BiosParamBlock;
static const char *prg_name;
static void
fatal(const char *str, ...)
{
va_list ap;
fprintf(stderr, "%s: ", prg_name);
va_start(ap, str);
vfprintf(stderr, str, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
static void
dump(const char *img)
{
FILE *fp;
BiosParamBlock bpb;
char label[12];
fp = fopen(img, "rb");
if (fp == NULL) fatal("Can't open '%s': %s", img, strerror(errno));
if (fread(&bpb, 1, sizeof(BiosParamBlock), fp) != sizeof(BiosParamBlock))
{
fatal("Can't read BPB");
}
printf("Bytes per logical sector: %hd\n", bpb.bytes_per_sector);
printf("Logical sectors per cluster: %d\n", bpb.sectors_per_cluster);
memset(label, 0, 12);
strncpy(label, bpb.label, 11);
printf("Label: '%s'\n", label);
printf("signature: %X\n", bpb.signature);
fclose(fp);
exit(EXIT_SUCCESS);
}
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] [-D IMG] [-b BIN]\n", prg_name);
printf("\t-h\tdisplay this help and exit\n");
printf("\t-V\toutput version information\n");
printf("\t-D IMG\tdump fs information from IMG\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];
while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'h':
usage(EXIT_SUCCESS);
break;
case 'V':
version();
break;
case 'D':
argv++;
argc--;
if (argc <= 1) usage(EXIT_FAILURE);
dump(argv[1]);
break;
default:
usage(EXIT_FAILURE);
break;
}
argv++;
argc--;
}
return (EXIT_SUCCESS);
}