1 // SPDX-License-Identifier: MIT
9 #include "hunk-reloc.h"
10 #include "piscsi/piscsi-enums.h"
17 //#define DEBUG printf
19 #define BE(val) be32toh(val)
20 #define BE16(val) be16toh(val)
22 #define READLW(a, b) fread(&a, 4, 1, b); a = be32toh(a);
23 #define READW(a, b) fread(&a, 2, 1, b); a = be16toh(a);
26 static uint32_t file_offset = 0, add_size = 0;
28 char *hunk_id_name(uint32_t index) {
34 case HUNKTYPE_HUNK_RELOC32:
35 return "HUNK_RELOC32";
45 return "UNKNOWN HUNK TYPE";
49 int process_hunk(uint32_t index, struct hunk_info *info, FILE *f, struct hunk_reloc *r) {
53 uint32_t discard = 0, cur_hunk = 0, offs32 = 0;
57 DEBUG("Processing hunk header.\n");
61 info->libnames[info->num_libs] = malloc(discard * 4);
62 fread(info->libnames[info->num_libs], discard, 4, f);
67 READLW(info->table_size, f);
68 DEBUG("Table size: %d\n", info->table_size);
69 READLW(info->first_hunk, f);
70 READLW(info->last_hunk, f);
71 info->num_hunks = (info->last_hunk - info->first_hunk) + 1;
72 DEBUG("First: %d Last: %d Num: %d\n", info->first_hunk, info->last_hunk, info->num_hunks);
73 info->hunk_sizes = malloc(info->num_hunks * 4);
74 info->hunk_offsets = malloc(info->num_hunks * 4);
75 for (uint32_t i = 0; i < info->table_size; i++) {
76 READLW(info->hunk_sizes[i], f);
77 DEBUG("Hunk %d: %d (%.8X)\n", i, info->hunk_sizes[i] * 4, info->hunk_sizes[i] * 4);
82 DEBUG("Hunk %d: CODE.\n", info->current_hunk);
84 info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
85 DEBUG("Code hunk size: %d (%.8X)\n", discard * 4, discard * 4);
86 fseek(f, discard * 4, SEEK_CUR);
89 case HUNKTYPE_HUNK_RELOC32:
90 DEBUG("Hunk %d: RELOC32.\n", info->current_hunk);
91 DEBUG("Processing Reloc32 hunk.\n");
94 if (discard && discard != 0xFFFFFFFF) {
96 DEBUG("Relocating %d offsets pointing to hunk %d.\n", discard, cur_hunk);
97 for(uint32_t i = 0; i < discard; i++) {
99 DEBUG("#%d: @%.8X in hunk %d\n", i + 1, offs32, cur_hunk);
100 r[info->reloc_hunks].offset = offs32;
101 r[info->reloc_hunks].src_hunk = info->current_hunk;
102 r[info->reloc_hunks].target_hunk = cur_hunk;
109 case HUNKTYPE_SYMBOL:
110 DEBUG("Hunk %d: SYMBOL.\n", info->current_hunk);
111 DEBUG("Processing Symbol hunk.\n");
116 memset(sstr, 0x00, 256);
117 fread(sstr, discard, 4, f);
119 DEBUG("Symbol: %s - %.8X\n", sstr, discard);
126 DEBUG("Hunk %d: BSS.\n", info->current_hunk);
128 info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
129 DEBUG("Skipping BSS hunk. Size: %d\n", discard * 4);
130 add_size += (discard * 4);
133 DEBUG("Hunk %d: DATA.\n", info->current_hunk);
135 info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
136 DEBUG("Skipping data hunk. Size: %d.\n", discard * 4);
137 fseek(f, discard * 4, SEEK_CUR);
141 DEBUG("END: Ending hunk %d.\n", info->current_hunk);
142 info->current_hunk++;
146 DEBUG("Unknown hunk type %.8X! Can't process!\n", index);
153 void reloc_hunk(struct hunk_reloc *h, uint8_t *buf, struct hunk_info *i) {
154 uint32_t rel = i->hunk_offsets[h->target_hunk];
155 uint32_t *src_ptr = (uint32_t *)(&buf[i->hunk_offsets[h->src_hunk] + h->offset]);
157 uint32_t src = be32toh(*src_ptr);
158 uint32_t dst = src + i->base_offset + rel;
159 DEBUG("%.8X -> %.8X\n", src, dst);
160 *src_ptr = htobe32(dst);
163 void process_hunks(FILE *in, struct hunk_info *h_info, struct hunk_reloc *r, uint32_t offset) {
165 DEBUG("Hunk ID: %.8X (%s)\n", lw, hunk_id_name(lw));
167 file_offset = offset;
170 while (!feof(in) && process_hunk(lw, h_info, in, r) != -1) {
172 if (feof(in)) goto end_parse;
173 DEBUG("Hunk ID: %.8X (%s)\n", lw, hunk_id_name(lw));
174 DEBUG("File pos: %.8lX\n", ftell(in) - file_offset);
177 DEBUG("Done processing hunks.\n");
180 void reloc_hunks(struct hunk_reloc *r, uint8_t *buf, struct hunk_info *h_info) {
181 DEBUG("Relocating %d offsets.\n", h_info->reloc_hunks);
182 for (uint32_t i = 0; i < h_info->reloc_hunks; i++) {
183 DEBUG("Relocating offset %d.\n", i);
184 reloc_hunk(&r[i], buf, h_info);
186 DEBUG("Done relocating offsets.\n");
189 struct LoadSegBlock {
191 uint32_t lsb_SummedLongs;
195 uint32_t lsb_LoadData[PISCSI_MAX_BLOCK_SIZE / 4];
197 #define LOADSEG_IDENTIFIER 0x4C534547
199 int load_lseg(int fd, uint8_t **buf_p, struct hunk_info *i, struct hunk_reloc *relocs, uint32_t block_size) {
206 uint8_t *block = malloc(block_size);
207 uint32_t next_blk = 0;
208 struct LoadSegBlock *lsb = (struct LoadSegBlock *)block;
210 read(fd, block, block_size);
211 if (BE(lsb->lsb_ID) != LOADSEG_IDENTIFIER) {
212 DEBUG("[LOAD_LSEG] Attempted to load a non LSEG-block: %.8X", BE(lsb->lsb_ID));
216 char *filename = "data/lsegout.bin";
217 FILE *out = fopen(filename, "wb+");
219 DEBUG("[LOAD_LSEG] LSEG data:\n");
220 DEBUG("[LOAD_LSEG] Longs: %d HostID: %d\n", BE(lsb->lsb_SummedLongs), BE(lsb->lsb_HostID));
221 DEBUG("[LOAD_LSEG] Next: %d LoadData: %p\n", BE(lsb->lsb_Next), (void *)lsb->lsb_LoadData);
222 next_blk = BE(lsb->lsb_Next);
224 next_blk = BE(lsb->lsb_Next);
225 fwrite(lsb->lsb_LoadData, 1, block_size - 20, out);
226 lseek64(fd, next_blk * block_size, SEEK_SET);
227 read(fd, block, block_size);
228 } while (next_blk != 0xFFFFFFFF);
230 uint32_t file_size = ftell(out);
231 fseek(out, 0, SEEK_SET);
232 uint8_t *buf = malloc(file_size + 1024);
233 fread(buf, file_size, 1, out);
234 fseek(out, 0, SEEK_SET);
235 process_hunks(out, i, relocs, 0x0);
239 i->byte_size = file_size;
240 i->alloc_size = file_size + add_size;