]> git.sesse.net Git - pistorm/blob - platforms/amiga/hunk-reloc.c
Add license information to source
[pistorm] / platforms / amiga / hunk-reloc.c
1 // SPDX-License-Identifier: MIT
2
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <endian.h>
9 #include "hunk-reloc.h"
10
11 #ifdef FAKESTORM
12 #define lseek64 lseek
13 #endif
14
15 #define DEBUG(...)
16 //#define DEBUG printf
17
18 #define BE(val) be32toh(val)
19 #define BE16(val) be16toh(val)
20
21 #define READLW(a, b) fread(&a, 4, 1, b); a = be32toh(a);
22 #define READW(a, b) fread(&a, 2, 1, b); a = be16toh(a);
23
24 uint32_t lw = 0;
25 static uint32_t file_offset = 0, add_size = 0;
26
27 char *hunk_id_name(uint32_t index) {
28     switch (index) {
29         case HUNKTYPE_HEADER:
30             return "HUNK_HEADER";
31         case HUNKTYPE_CODE:
32             return "HUNK_CODE";
33         case HUNKTYPE_HUNK_RELOC32:
34             return "HUNK_RELOC32";
35         case HUNKTYPE_SYMBOL:
36             return "HUNK_SYMBOL";
37         case HUNKTYPE_BSS:
38             return "HUNK_BSS";
39         case HUNKTYPE_DATA:
40             return "HUNK_DATA";
41         case HUNKTYPE_END:
42             return "HUNK_END";
43         default:
44             return "UNKNOWN HUNK TYPE";
45     }
46 }
47
48 int process_hunk(uint32_t index, struct hunk_info *info, FILE *f, struct hunk_reloc *r) {
49     if (!f)
50         return -1;
51     
52     uint32_t discard = 0, cur_hunk = 0, offs32 = 0;
53     
54     switch (index) {
55         case HUNKTYPE_HEADER:
56             DEBUG("Processing hunk header.\n");
57             do {
58                 READLW(discard, f);
59                 if (discard) {
60                     info->libnames[info->num_libs] = malloc(discard * 4);
61                     fread(info->libnames[info->num_libs], discard, 4, f);
62                     info->num_libs++;
63                 }
64             } while (discard);
65             
66             READLW(info->table_size, f);
67             DEBUG("Table size: %d\n", info->table_size);
68             READLW(info->first_hunk, f);
69             READLW(info->last_hunk, f);
70             info->num_hunks = (info->last_hunk - info->first_hunk) + 1;
71             DEBUG("First: %d Last: %d Num: %d\n", info->first_hunk, info->last_hunk, info->num_hunks);
72             info->hunk_sizes = malloc(info->num_hunks * 4);
73             info->hunk_offsets = malloc(info->num_hunks * 4);
74             for (uint32_t i = 0; i < info->table_size; i++) {
75                 READLW(info->hunk_sizes[i], f);
76                 DEBUG("Hunk %d: %d (%.8X)\n", i, info->hunk_sizes[i] * 4, info->hunk_sizes[i] * 4);
77             }
78             return 0;
79             break;
80         case HUNKTYPE_CODE:
81             DEBUG("Hunk %d: CODE.\n", info->current_hunk);
82             READLW(discard, f);
83             info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
84             DEBUG("Code hunk size: %d (%.8X)\n", discard * 4, discard * 4);
85             fseek(f, discard * 4, SEEK_CUR);
86             return 0;
87             break;
88         case HUNKTYPE_HUNK_RELOC32:
89             DEBUG("Hunk %d: RELOC32.\n", info->current_hunk);
90             DEBUG("Processing Reloc32 hunk.\n");
91             do {
92                 READLW(discard, f);
93                 if (discard && discard != 0xFFFFFFFF) {
94                     READLW(cur_hunk, f);
95                     DEBUG("Relocating %d offsets pointing to hunk %d.\n", discard, cur_hunk);
96                     for(uint32_t i = 0; i < discard; i++) {
97                         READLW(offs32, f);
98                         DEBUG("#%d: @%.8X in hunk %d\n", i + 1, offs32, cur_hunk);
99                         r[info->reloc_hunks].offset = offs32;
100                         r[info->reloc_hunks].src_hunk = info->current_hunk;
101                         r[info->reloc_hunks].target_hunk = cur_hunk;
102                         info->reloc_hunks++;
103                     }
104                 }
105             } while(discard);
106             return 0;
107             break;
108         case HUNKTYPE_SYMBOL:
109             DEBUG("Hunk %d: SYMBOL.\n", info->current_hunk);
110             DEBUG("Processing Symbol hunk.\n");
111             READLW(discard, f);
112             do {
113                 if (discard) {
114                     char sstr[256];
115                     memset(sstr, 0x00, 256);
116                     fread(sstr, discard, 4, f);
117                     READLW(discard, f);
118                     DEBUG("Symbol: %s - %.8X\n", sstr, discard);
119                 }
120                 READLW(discard, f);
121             } while (discard);
122             return 0;
123             break;
124         case HUNKTYPE_BSS:
125             DEBUG("Hunk %d: BSS.\n", info->current_hunk);
126             READLW(discard, f);
127             info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
128             DEBUG("Skipping BSS hunk. Size: %d\n", discard * 4);
129             add_size += (discard * 4);
130             return 0;
131         case HUNKTYPE_DATA:
132             DEBUG("Hunk %d: DATA.\n", info->current_hunk);
133             READLW(discard, f);
134             info->hunk_offsets[info->current_hunk] = ftell(f) - file_offset;
135             DEBUG("Skipping data hunk. Size: %d.\n", discard * 4);
136             fseek(f, discard * 4, SEEK_CUR);
137             return 0;
138             break;
139         case HUNKTYPE_END:
140             DEBUG("END: Ending hunk %d.\n", info->current_hunk);
141             info->current_hunk++;
142             return 0;
143             break;
144         default:
145             DEBUG("Unknown hunk type %.8X! Can't process!\n", index);
146             break;
147     }
148
149     return -1;
150 }
151
152 void reloc_hunk(struct hunk_reloc *h, uint8_t *buf, struct hunk_info *i) {
153     uint32_t rel = i->hunk_offsets[h->target_hunk];
154     uint32_t *src_ptr = (uint32_t *)(&buf[i->hunk_offsets[h->src_hunk] + h->offset]);
155
156     uint32_t src = be32toh(*src_ptr);
157     uint32_t dst = src + i->base_offset + rel;
158     DEBUG("%.8X -> %.8X\n", src, dst);
159     *src_ptr = htobe32(dst);
160 }
161
162 void process_hunks(FILE *in, struct hunk_info *h_info, struct hunk_reloc *r, uint32_t offset) {
163     READLW(lw, in);
164     DEBUG("Hunk ID: %.8X (%s)\n", lw, hunk_id_name(lw));
165
166     file_offset = offset;
167     add_size = 0;
168
169     while (!feof(in) && process_hunk(lw, h_info, in, r) != -1) {
170         READLW(lw, in);
171         if (feof(in)) goto end_parse;
172         DEBUG("Hunk ID: %.8X (%s)\n", lw, hunk_id_name(lw));
173         DEBUG("File pos: %.8lX\n", ftell(in) - file_offset);
174     }
175     end_parse:;
176     DEBUG("Done processing hunks.\n");
177 }
178
179 void reloc_hunks(struct hunk_reloc *r, uint8_t *buf, struct hunk_info *h_info) {
180     DEBUG("Relocating %d offsets.\n", h_info->reloc_hunks);
181     for (uint32_t i = 0; i < h_info->reloc_hunks; i++) {
182         DEBUG("Relocating offset %d.\n", i);
183         reloc_hunk(&r[i], buf, h_info);
184     }
185     DEBUG("Done relocating offsets.\n");
186 }
187
188 struct LoadSegBlock {
189     uint32_t   lsb_ID;
190     uint32_t   lsb_SummedLongs;
191     int32_t    lsb_ChkSum;
192     uint32_t   lsb_HostID;
193     uint32_t   lsb_Next;
194     uint32_t   lsb_LoadData[123]; // Assumes 512 byte blocks
195 };
196 #define LOADSEG_IDENTIFIER 0x4C534547
197
198 int load_lseg(int fd, uint8_t **buf_p, struct hunk_info *i, struct hunk_reloc *relocs) {
199     if (fd == -1)
200         return -1;
201
202     uint8_t *block = malloc(512);
203     uint32_t next_blk = 0;
204     struct LoadSegBlock *lsb = (struct LoadSegBlock *)block;
205
206     read(fd, block, 512);
207     if (BE(lsb->lsb_ID) != LOADSEG_IDENTIFIER) {
208         DEBUG("[LOAD_LSEG] Attempted to load a non LSEG-block: %.8X", BE(lsb->lsb_ID));
209         goto fail;
210     }
211
212     char *filename = "data/lsegout.bin";
213     FILE *out = fopen(filename, "wb+");
214
215     DEBUG("[LOAD_LSEG] LSEG data:\n");
216     DEBUG("[LOAD_LSEG] Longs: %d HostID: %d\n", BE(lsb->lsb_SummedLongs), BE(lsb->lsb_HostID));
217     DEBUG("[LOAD_LSEG] Next: %d LoadData: %p\n", BE(lsb->lsb_Next), (void *)lsb->lsb_LoadData);
218     next_blk = BE(lsb->lsb_Next);
219     do {
220         next_blk = BE(lsb->lsb_Next);
221         fwrite(lsb->lsb_LoadData, 4, 123, out);
222         lseek64(fd, next_blk * 512, SEEK_SET);
223         read(fd, block, 512);
224     } while (next_blk != 0xFFFFFFFF);
225     
226     uint32_t file_size = ftell(out);
227     fseek(out, 0, SEEK_SET);
228     uint8_t *buf = malloc(file_size + 1024);
229     fread(buf, file_size, 1, out);
230     fseek(out, 0, SEEK_SET);
231     process_hunks(out, i, relocs, 0x0);
232
233     fclose(out);
234     *buf_p = buf;
235     i->byte_size = file_size;
236     i->alloc_size = file_size + add_size;
237
238     return 0;
239
240 fail:;
241     if (block)
242         free(block);
243
244     return -1;
245 }