forked from zeldaret/oot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeromfs.c
342 lines (286 loc) · 7.85 KB
/
makeromfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "n64chksum.h"
#include "util.h"
#define ROM_SIZE 0x02000000
enum InputObjType
{
OBJ_NULL,
OBJ_FILE,
OBJ_TABLE,
};
struct InputFile
{
enum InputObjType type;
const char *name;
uint8_t *data;
size_t size;
unsigned int valign;
uint32_t virtStart;
uint32_t virtEnd;
uint32_t physStart;
uint32_t physEnd;
};
static struct InputFile *g_inputFiles = NULL;
static int g_inputFilesCount = 0;
static unsigned int round_up(unsigned int num, unsigned int multiple)
{
num += multiple - 1;
return num / multiple * multiple;
}
static bool is_yaz0_header(const uint8_t *data)
{
return data[0] == 'Y'
&& data[1] == 'a'
&& data[2] == 'z'
&& data[3] == '0';
}
static void compute_offsets(void)
{
size_t physOffset = 0;
size_t virtOffset = 0;
int i;
for (i = 0; i < g_inputFilesCount; i++)
{
bool compressed = false;
if (g_inputFiles[i].type == OBJ_FILE)
{
if (is_yaz0_header(g_inputFiles[i].data))
compressed = true;
}
else if (g_inputFiles[i].type == OBJ_TABLE)
{
g_inputFiles[i].size = g_inputFilesCount * 16;
}
virtOffset = round_up(virtOffset, g_inputFiles[i].valign);
if (g_inputFiles[i].type == OBJ_NULL)
{
g_inputFiles[i].virtStart = 0;
g_inputFiles[i].virtEnd = 0;
g_inputFiles[i].physStart = 0;
g_inputFiles[i].physEnd = 0;
}
else if (compressed)
{
size_t compSize = round_up(g_inputFiles[i].size, 16);
size_t uncompSize = util_read_uint32_be(g_inputFiles[i].data + 4);
g_inputFiles[i].virtStart = virtOffset;
g_inputFiles[i].virtEnd = virtOffset + uncompSize;
g_inputFiles[i].physStart = physOffset;
g_inputFiles[i].physEnd = physOffset + compSize;
physOffset += compSize;
virtOffset += uncompSize;
}
else
{
size_t size = g_inputFiles[i].size;
g_inputFiles[i].virtStart = virtOffset;
g_inputFiles[i].virtEnd = virtOffset + size;
g_inputFiles[i].physStart = physOffset;
g_inputFiles[i].physEnd = 0;
physOffset += size;
virtOffset += size;
}
}
}
static void build_rom(const char *filename)
{
uint8_t *romData = calloc(ROM_SIZE, 1);
size_t pos = 0;
int i;
int j;
uint32_t chksum[2];
FILE *outFile;
for (i = 0; i < g_inputFilesCount; i++)
{
size_t size = g_inputFiles[i].size;
if (pos + round_up(size, 16) > ROM_SIZE)
util_fatal_error("size exceeds max ROM size of 32 KiB");
assert(pos % 16 == 0);
switch (g_inputFiles[i].type)
{
case OBJ_FILE:
// write file data
memcpy(romData + pos, g_inputFiles[i].data, size);
pos += round_up(size, 16);
free(g_inputFiles[i].data);
break;
case OBJ_TABLE:
for (j = 0; j < g_inputFilesCount; j++)
{
util_write_uint32_be(romData + pos + 0, g_inputFiles[j].virtStart);
util_write_uint32_be(romData + pos + 4, g_inputFiles[j].virtEnd);
util_write_uint32_be(romData + pos + 8, g_inputFiles[j].physStart);
util_write_uint32_be(romData + pos + 12, g_inputFiles[j].physEnd);
pos += 16;
}
break;
case OBJ_NULL:
break;
}
}
// Pad the rest of the ROM
while (pos < ROM_SIZE)
{
// This is such a weird thing to pad with. Whatever, Nintendo.
romData[pos] = pos & 0xFF;
pos++;
}
// calculate checksum
n64chksum_calculate(romData, 6105, chksum);
util_write_uint32_be(romData + 0x10, chksum[0]);
util_write_uint32_be(romData + 0x14, chksum[1]);
// write file
outFile = fopen(filename, "wb");
if (outFile == NULL)
util_fatal_error("failed to open file '%s' for writing", filename);
fwrite(romData, ROM_SIZE, 1, outFile);
fclose(outFile);
free(romData);
}
static struct InputFile *new_file(void)
{
int index = g_inputFilesCount;
g_inputFilesCount++;
g_inputFiles = realloc(g_inputFiles, g_inputFilesCount * sizeof(*g_inputFiles));
g_inputFiles[index].valign = 1;
return &g_inputFiles[index];
}
// null terminates the current token and returns a pointer to the next token
static char *token_split(char *str)
{
while (!isspace(*str))
{
if (*str == 0)
return str; // end of string
str++;
}
*str = 0; // terminate token
str++;
// skip remaining whitespace
while (isspace(*str))
str++;
return str;
}
// null terminates the current line and returns a pointer to the next line
static char *line_split(char *str)
{
while (*str != '\n')
{
if (*str == 0)
return str; // end of string
str++;
}
*str = 0; // terminate line
return str + 1;
}
static void parse_line(char *line, int lineNum)
{
char *token = line;
int i = 0;
char *filename = NULL;
enum InputObjType type = -1;
int valign = 1;
struct InputFile *file;
// iterate through each token
while (token[0] != 0)
{
char *nextToken = token_split(token);
if (token[0] == '#') // comment - ignore rest of line
return;
switch (i)
{
case 0:
if (strcmp(token, "file") == 0)
type = OBJ_FILE;
else if (strcmp(token, "filetable") == 0)
type = OBJ_TABLE;
else if (strcmp(token, "null") == 0)
type = OBJ_NULL;
else
util_fatal_error("unknown object type '%s' on line %i", token, lineNum);
break;
case 1:
filename = token;
break;
case 2:
{
int n;
if (sscanf(token, "align(%i)", &n) == 1)
valign = n;
else
goto junk;
}
break;
default:
junk:
util_fatal_error("junk '%s' on line %i", token, lineNum);
break;
}
token = nextToken;
i++;
}
if (i == 0) // empty line
return;
file = new_file();
file->valign = valign;
switch (type)
{
case OBJ_FILE:
if (filename == NULL)
util_fatal_error("no filename specified on line %i", lineNum);
file->type = OBJ_FILE;
file->data = util_read_whole_file(filename, &file->size);
break;
case OBJ_TABLE:
file->type = OBJ_TABLE;
break;
case OBJ_NULL:
file->type = OBJ_NULL;
file->size = 0;
break;
}
}
static void parse_list(char *list)
{
char *line = list;
int lineNum = 1;
// iterate through each line
while (line[0] != 0)
{
char *nextLine = line_split(line);
parse_line(line, lineNum);
line = nextLine;
lineNum++;
}
}
static void usage(const char *execName)
{
printf("usage: %s FILE_LIST OUTPUT_FILE\n"
"where FILE_LIST is a list of files to include\n"
"and OUTPUT_FILE is the name of the output ROM\n"
"note that 'dmadata' refers to the file list itself and not an external file\n",
execName);
}
int main(int argc, char **argv)
{
char *list;
if (argc != 3)
{
puts("invalid args");
usage(argv[0]);
return 1;
}
list = util_read_whole_file(argv[1], NULL);
parse_list(list);
compute_offsets();
build_rom(argv[2]);
free(list);
return 0;
}