Generate floppy image with a python script

This commit is contained in:
Juhani Krekelä 2023-03-16 17:14:53 +02:00
parent 400f282725
commit c695688e3a
4 changed files with 65 additions and 20 deletions

View File

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

63
assemble_floppy.py Normal file
View File

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

View File

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

View File

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