diff --git a/Makefile b/Makefile index 94114bc..50c28b6 100644 --- a/Makefile +++ b/Makefile @@ -7,9 +7,7 @@ PYTHON = python3 all: ponydos.img ponydos.img: ponydos.bin wallpaper.bin - rw -i /dev/zero -o $@ -c 1440K - rw -i ponydos.bin -o $@ - rw -i wallpaper.bin -o $@ -O 1024 + $(PYTHON) assemble_floppy.py $@ ponydos.bin wallpaper.bin .asm.bin: $(NASM) -fbin -o $@ $< diff --git a/assemble_floppy.py b/assemble_floppy.py new file mode 100644 index 0000000..44e0f53 --- /dev/null +++ b/assemble_floppy.py @@ -0,0 +1,63 @@ +import sys +import os.path + +floppy_bytes = 1440 * 1024 +sector_bytes = 512 +dirent_bytes = 32 + +floppy = bytearray() + +def extend_padded(buffer, alignment, data): + padding_len = (alignment - len(data)) % alignment + buffer.extend(data) + buffer.extend(bytes(padding_len)) + +if len(sys.argv) < 3: + print(f'Usage: {sys.argv[0]} disk.img bootsector.bin files…', file=sys.stderr) + sys.exit(1) + +output_path = sys.argv[1] +bootsector_path = sys.argv[2] +files_paths = sys.argv[3:] + +with open(bootsector_path, 'rb') as f: + bootsector = f.read() +assert len(bootsector) == sector_bytes +floppy.extend(bootsector) + +files = [] +directory = bytearray() + +for file_path in files_paths: + with open(file_path, 'rb') as f: + file_contents = f.read() + assert len(file_contents) <= 128 * sector_bytes # 128 sector files + files.append(file_contents) + + # dirent: + # 2 bytes size in 512B sectors, 0 for end of listing + # 30 bytes null-terminated name + sectors = (len(file_contents) + sector_bytes - 1) // sector_bytes + file_name = os.path.basename(file_path).encode() + b'\0' + assert len(file_name) <= 30 + dirent = bytes([sectors & 0xff, sectors >> 8]) + file_name + extend_padded(directory, dirent_bytes, dirent) + +# Add a zero dirent to mark the end of the directory +# (and the start of the consulate) +directory.extend(bytes(dirent_bytes)) + +assert len(directory) <= sector_bytes + +extend_padded(floppy, sector_bytes, directory) + +for file_contents in files: + extend_padded(floppy, sector_bytes, file_contents) + +assert len(floppy) <= floppy_bytes + +output = bytearray() +extend_padded(output, floppy_bytes, floppy) + +with open(output_path, 'wb') as f: + f.write(output) diff --git a/filesystem.asm b/filesystem.asm deleted file mode 100644 index 18edef4..0000000 --- a/filesystem.asm +++ /dev/null @@ -1,14 +0,0 @@ -; TODO: have tooling for this - -; 128 sector files, starting at 2 - -; dirent: -; 2 bytes size in 512B sectors, 0 for end of listing -; 30 bytes null-terminated name - -wallpaper: - dw 8 - db 'wallpaper.bin', 0 - times 512+32-($-$$) db 0 - -times 1024-($-$$) db 0 \ No newline at end of file diff --git a/ponydos.asm b/ponydos.asm index d1c7532..3934af8 100644 --- a/ponydos.asm +++ b/ponydos.asm @@ -441,8 +441,6 @@ times 510-($-$$) db 0 db 0x55 db 0xaa -%include "filesystem.asm" - ; ------------------------------------------------------------------ ; Zero-initialized variables ; ------------------------------------------------------------------ @@ -459,4 +457,4 @@ mouse_row resw 1 boot_disk resb 1 -_bss_end: \ No newline at end of file +_bss_end: