1 // SPDX-License-Identifier: MIT
9 #include "hunk-reloc.h"
10 #include "piscsi/piscsi-enums.h"
11 #include "piscsi/piscsi.h"
17 #define DEBUG_SPAMMY(...)
18 //#define DEBUG_SPAMMY printf
20 //#define DEBUG printf
22 #define BE(val) be32toh(val)
23 #define BE16(val) be16toh(val)
25 #define READLW(a, b) fread(&a, 4, 1, b); a = be32toh(a);
26 #define READW(a, b) fread(&a, 2, 1, b); a = be16toh(a);
29 static uint32_t file_offset = 0, add_size = 0;
31 char *hunk_id_name(uint32_t index) {
37 case HUNKTYPE_HUNK_RELOC32:
38 return "HUNK_RELOC32";
48 return "UNKNOWN HUNK TYPE";
52 int process_hunk(uint32_t index, struct hunk_info *info, FILE *f, struct hunk_reloc *r) {
56 uint32_t discard = 0, cur_hunk = 0, offs32 = 0;
60 DEBUG("[HUNK_RELOC] Processing hunk HEADER.\n");
64 if (info->libnames[info->num_libs]) {
65 free(info->libnames[info->num_libs]);
66 info->libnames[info->num_libs] = NULL;
68 info->libnames[info->num_libs] = malloc(discard * 4);
69 fread(info->libnames[info->num_libs], discard, 4, f);
74 READLW(info->table_size, f);
75 DEBUG("[HUNK_RELOC] [HEADER] Table size: %d\n", info->table_size);
76 READLW(info->first_hunk, f);
77 READLW(info->last_hunk, f);
78 info->num_hunks = (info->last_hunk - info->first_hunk) + 1;
79 DEBUG("[HUNK_RELOC] [HEADER] First: %d Last: %d Num: %d\n", info->first_hunk, info->last_hunk, info->num_hunks);
80 if (info->hunk_sizes) {
81 free(info->hunk_sizes);
82 info->hunk_sizes = NULL;
84 if (info->hunk_offsets) {
85 free(info->hunk_offsets);
86 info->hunk_offsets = NULL;
88 info->hunk_sizes = malloc(info->num_hunks * 4);
89 info->hunk_offsets = malloc(info->num_hunks * 4);
90 for (uint32_t i = 0; i < info->table_size; i++) {
91 READLW(info->hunk_sizes[i], f);
92 DEBUG("[HUNK_RELOC] [HEADER] Hunk %d: %d (%.8X)\n", i, info->hunk_sizes[i] * 4, info->hunk_sizes[i] * 4);
94 info->header_size = (uint32_t)ftell(f) - file_offset;
95 DEBUG("[HUNK_RELOC] [HEADER] ~~~~~~~~~~~ Hunk HEADER size is %d ~~~~~~~~~~~~.\n", info->header_size);
99 DEBUG("[HUNK_RELOC] Hunk %d: CODE.\n", info->current_hunk);
101 info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
102 DEBUG("[HUNK_RELOC] [CODE] Code hunk size: %d (%.8X)\n", discard * 4, discard * 4);
103 fseek(f, discard * 4, SEEK_CUR);
106 case HUNKTYPE_HUNK_RELOC32:
107 DEBUG("[HUNK_RELOC] Hunk %d: RELOC32.\n", info->current_hunk);
108 DEBUG("Processing Reloc32 hunk.\n");
111 if (discard && discard != 0xFFFFFFFF) {
113 DEBUG("[HUNK_RELOC] [RELOC32] Relocating %d offsets pointing to hunk %d.\n", discard, cur_hunk);
114 for(uint32_t i = 0; i < discard; i++) {
116 DEBUG_SPAMMY("[HUNK_RELOC] [RELOC32] #%d: @%.8X in hunk %d\n", i + 1, offs32, cur_hunk);
117 r[info->reloc_hunks].offset = offs32;
118 r[info->reloc_hunks].src_hunk = info->current_hunk;
119 r[info->reloc_hunks].target_hunk = cur_hunk;
126 case HUNKTYPE_SYMBOL:
127 DEBUG("[HUNK_RELOC] Hunk %d: SYMBOL.\n", info->current_hunk);
128 DEBUG("[HUNK_RELOC] [SYMBOL] Processing Symbol hunk.\n");
133 memset(sstr, 0x00, 256);
134 fread(sstr, discard, 4, f);
136 DEBUG("[HUNK_RELOC] [SYMBOL] Symbol: %s - %.8X\n", sstr, discard);
143 DEBUG("[HUNK_RELOC] Hunk %d: BSS.\n", info->current_hunk);
145 info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
146 DEBUG("[HUNK_RELOC] [BSS] Skipping BSS hunk. Size: %d\n", discard * 4);
147 add_size += (discard * 4);
150 DEBUG("[HUNK_RELOC] Hunk %d: DATA.\n", info->current_hunk);
152 info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
153 DEBUG("[HUNK_RELOC] [DATA] Skipping data hunk. Size: %d.\n", discard * 4);
154 fseek(f, discard * 4, SEEK_CUR);
158 DEBUG("[HUNK_RELOC] END: Ending hunk %d.\n", info->current_hunk);
159 info->current_hunk++;
163 DEBUG("[!!!HUNK_RELOC] Unknown hunk type %.8X! Can't process!\n", index);
170 void reloc_hunk(struct hunk_reloc *h, uint8_t *buf, struct hunk_info *i) {
171 uint32_t rel = i->hunk_offsets[h->target_hunk];
172 uint32_t *src_ptr = (uint32_t *)(&buf[i->hunk_offsets[h->src_hunk] + h->offset]);
174 uint32_t src = be32toh(*src_ptr);
175 uint32_t dst = src + i->base_offset + rel;
176 DEBUG_SPAMMY("[HUNK-RELOC] %.8X -> %.8X\n", src, dst);
177 *src_ptr = htobe32(dst);
180 void process_hunks(FILE *in, struct hunk_info *h_info, struct hunk_reloc *r, uint32_t offset) {
182 DEBUG_SPAMMY("Hunk ID: %.8X (%s)\n", lw, hunk_id_name(lw));
184 file_offset = offset;
187 while (!feof(in) && process_hunk(lw, h_info, in, r) != -1) {
189 if (feof(in)) goto end_parse;
190 DEBUG("Hunk ID: %.8X (%s)\n", lw, hunk_id_name(lw));
191 DEBUG("File pos: %.8lX\n", ftell(in) - file_offset);
194 DEBUG("Done processing hunks.\n");
197 void reloc_hunks(struct hunk_reloc *r, uint8_t *buf, struct hunk_info *h_info) {
198 DEBUG("[HUNK-RELOC] Relocating %d offsets.\n", h_info->reloc_hunks);
199 for (uint32_t i = 0; i < h_info->reloc_hunks; i++) {
200 DEBUG_SPAMMY("[HUNK-RELOC] Relocating offset %d.\n", i);
201 reloc_hunk(&r[i], buf, h_info);
203 DEBUG("[HUNK-RELOC] Done relocating offsets.\n");
206 struct LoadSegBlock {
208 uint32_t lsb_SummedLongs;
212 uint32_t lsb_LoadData[PISCSI_MAX_BLOCK_SIZE / 4];
214 #define LOADSEG_IDENTIFIER 0x4C534547
216 int load_lseg(int fd, uint8_t **buf_p, struct hunk_info *i, struct hunk_reloc *relocs, uint32_t block_size) {
223 uint8_t *block = malloc(block_size);
224 uint32_t next_blk = 0;
225 struct LoadSegBlock *lsb = (struct LoadSegBlock *)block;
227 read(fd, block, block_size);
228 if (BE(lsb->lsb_ID) != LOADSEG_IDENTIFIER) {
229 DEBUG("[LOAD_LSEG] Attempted to load a non LSEG-block: %.8X", BE(lsb->lsb_ID));
233 char *filename = "data/lsegout.bin";
234 FILE *out = fopen(filename, "wb+");
236 DEBUG("[LOAD_LSEG] LSEG data:\n");
237 DEBUG("[LOAD_LSEG] Longs: %d HostID: %d\n", BE(lsb->lsb_SummedLongs), BE(lsb->lsb_HostID));
238 DEBUG("[LOAD_LSEG] Next: %d LoadData: %p\n", BE(lsb->lsb_Next), (void *)lsb->lsb_LoadData);
239 next_blk = BE(lsb->lsb_Next);
241 next_blk = BE(lsb->lsb_Next);
242 fwrite(lsb->lsb_LoadData, 1, block_size - 20, out);
243 lseek64(fd, next_blk * block_size, SEEK_SET);
244 read(fd, block, block_size);
245 } while (next_blk != 0xFFFFFFFF);
247 uint32_t file_size = ftell(out);
248 fseek(out, 0, SEEK_SET);
249 uint8_t *buf = malloc(file_size + 1024);
250 fread(buf, file_size, 1, out);
251 fseek(out, 0, SEEK_SET);
252 process_hunks(out, i, relocs, 0x0);
256 i->byte_size = file_size;
257 i->alloc_size = file_size + add_size;
268 int load_fs(struct piscsi_fs *fs, char *dosID) {
270 memset(filename, 0x00, 256);
271 sprintf(filename, "./data/fs/%c%c%c.%d", dosID[0], dosID[1], dosID[2], dosID[3]);
273 FILE *in = fopen(filename, "rb");
277 fseek(in, 0, SEEK_END);
278 uint32_t file_size = ftell(in);
279 fseek(in, 0, SEEK_SET);
281 fs->binary_data = malloc(file_size);
282 fread(fs->binary_data, file_size, 1, in);
283 fseek(in, 0, SEEK_SET);
284 process_hunks(in, &fs->h_info, fs->relocs, 0x0);
285 fs->h_info.byte_size = file_size;
286 fs->h_info.alloc_size = file_size + add_size;