sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
mkimage.py
1#!/usr/bin/env python3
2"""Writes src/iop/image.c: a 64 KiB FAT12 volume holding HELLO.TXT, stored as its non-zero sectors."""
3
4import os
5import struct
6
7SECTOR = 512
8TOTAL = 128
9HELLO = b"hello from the ramdisk\n"
10
11
12def boot_sector():
13 bs = bytearray(SECTOR)
14 bs[0:3] = b"\xEB\x3C\x90"
15 bs[3:11] = b"RAMDISK "
16 struct.pack_into("<HBHBHHBHHHII", bs, 11,
17 SECTOR, # bytes per sector
18 1, # sectors per cluster
19 1, # reserved sectors (the boot sector)
20 2, # FAT copies
21 16, # root directory entries (one sector)
22 TOTAL, # total sectors
23 0xF8, # media descriptor
24 1, # sectors per FAT
25 32, # sectors per track
26 2, # heads
27 0, # hidden sectors
28 0) # total sectors (32-bit field, unused)
29 struct.pack_into("<BBBI", bs, 36, 0x80, 0, 0x29, 0x52414D44)
30 bs[43:54] = b"RAMDISK "
31 bs[54:62] = b"FAT12 "
32 bs[510:512] = b"\x55\xAA"
33 return bytes(bs)
34
35
36def fat():
37 # Entries 0 and 1 are reserved; entry 2 (HELLO.TXT's only cluster) is end of chain.
38 f = bytearray(SECTOR)
39 f[0:5] = b"\xF8\xFF\xFF\xFF\x0F"
40 return bytes(f)
41
42
43def root_dir():
44 d = bytearray(SECTOR)
45 d[0:11] = b"RAMDISK "
46 d[11] = 0x08
47 entry = bytearray(32)
48 entry[0:11] = b"HELLO TXT"
49 entry[11] = 0x20
50 struct.pack_into("<HHHI", entry, 22, 0, 0x5B50, 2, len(HELLO))
51 struct.pack_into("<H", entry, 16, 0x5B50)
52 struct.pack_into("<H", entry, 18, 0x5B50)
53 d[32:64] = entry
54 return bytes(d)
55
56
57def hello():
58 return HELLO + bytes(SECTOR - len(HELLO))
59
60
61# Layout: 0 boot, 1-2 FATs, 3 root directory, 4 onward clusters 2 onward.
62SECTORS = [(0, boot_sector()), (1, fat()), (2, fat()), (3, root_dir()), (4, hello())]
63
64
65def main():
66 here = os.path.dirname(os.path.abspath(__file__))
67 out = os.path.join(here, "src", "iop", "image.c")
68 with open(out, "w", newline="\n") as f:
69 f.write("/* Generated by mkimage.py. */\n\n")
70 f.write('#include "image.h"\n\n')
71 f.write("const unsigned int ramdisk_sector_count = %d;\n\n" % TOTAL)
72 f.write("const struct ramdisk_seed ramdisk_seed[] = {\n")
73 for lba, data in SECTORS:
74 f.write(" {%d, {\n" % lba)
75 for i in range(0, SECTOR, 16):
76 f.write(" " + ", ".join("0x%02x" % b for b in data[i:i + 16]) + ",\n")
77 f.write(" }},\n")
78 f.write("};\n\n")
79 f.write("const unsigned int ramdisk_seed_count = sizeof(ramdisk_seed) / sizeof(ramdisk_seed[0]);\n")
80
81
82if __name__ == "__main__":
83 main()