-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_rom.c
183 lines (130 loc) · 3.67 KB
/
extract_rom.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "mio0.c"
const char outfile_prefix[] = "tex";
// Read at most `n` characters (newline included) into `str`.
// If present, the newline is removed (replaced by the null terminator).
// https://alexandra-zaharia.github.io/posts/how-to-read-safely-from-stdin-in-c/
void s_gets(char* str, int n)
{
char* str_read = fgets(str, n, stdin);
if (!str_read) return;
int i = 0;
while (str[i] != '\n' && str[i] != '\0') i++;
if (str[i] == '\n') str[i] = '\0';
}
void print_usage(FILE* stream, char* path) {
fprintf(stream, "Usage: %s path/to/ROM", path);
}
int main(int argc, char** argv) {
char filename[255];
if(argc > 2) {
print_usage(stderr, argv[0]);
return -1;
}
if(argc == 2) {
int i = 0;
while(argv[1][i] != '\0' && argv[1][i] != '\0') {
filename[i] = argv[1][i];
i++;
}
filename[i] = '\0';
} else {
printf("Enter ROM path: ");
s_gets(filename, 255);
printf("\n");
}
int result;
int i;
// Open file
FILE* fp_rom = fopen(filename, "rb");
if(fp_rom == NULL) {
fprintf(stderr, "Failed to open file: %s", filename);
return 1;
}
printf("Opened file: %s\n", filename);
// Get size of file
struct stat st;
result = stat(filename, &st); // How can I use the FILE pointer from `fopen`?
if(result != 0) {
fprintf(stderr, "Failed to stat file: %s", filename);
return 1;
}
unsigned int sz = st.st_size;
printf("File size: %d bytes\n", sz);
if(sz < SIG_SIZE) {
fprintf(stderr, "Invalid ROM file: Too small.\n");
return 1;
}
// Read signature
fseek(fp_rom, 0L, SEEK_SET);
uint8_t sig[SIG_SIZE];
fread(sig, sizeof(uint8_t), 4, fp_rom);
// Print signature
printf("ROM signature: 0x");
for(i = 0; i < SIG_SIZE; i++) {
printf("%2x", sig[i]);
}
printf("\n");
// Ensure signature is not byte-swapped (I would love to support this someday)
int is_valid = is_valid_sig(sig);
if(!is_valid) {
int is_bs = is_valid_sig_bs(sig);
if(is_bs) {
fprintf(stderr, "This is a byte-swapped ROM. Please use '.z64' format.\n");
} else {
fprintf(stderr, "Invalid ROM file: Unknown signature.\n");
}
return 1;
}
printf("Valid signature.\n");
// Count instances of MIO0
int count = 0;
int locations[100];
uint8_t c;
for(i = SIG_SIZE; i < sz; i++) {
c = fgetc(fp_rom); // This could technically be EOF (-1)
if(count == 100) {
printf("Reached max MIO0 sections (because I'm lazy)\n");
break;
}
if(c != 'M') continue;
if(
(fgetc(fp_rom) == 'I')
&& (fgetc(fp_rom) == 'O')
&& (fgetc(fp_rom) == '0')
) {
locations[count++] = i-1;
continue;
}
// Return back to where we were for potential case of "MMMMMMIO0"
fseek(fp_rom, i, SEEK_SET);
}
printf("Found %d instances of \"MIO0\"\n\n", count);
for(i = 0; i < count; i++) {
printf("DECOMPRESSING BLOCK %d: 0x%x ----------------\n", i, locations[i]);
uint8_t* output = NULL;
uint8_t size;
mio0_decompress_f(fp_rom, sz, locations[i], &output, &size);
if(output == NULL) {
printf(" ! `output` ptr was `NULL`\n\n");
continue;
}
char filename[30];
snprintf(filename, 30, "./out/0x%x.texture", locations[i]);
// Open output file for writing
FILE* fp_outfile = fopen(filename, "wb");
if(fp_outfile == NULL) {
fprintf(stderr, "Failed to open file: %s", filename);
} else {
fwrite(output, size, sizeof(uint8_t), fp_outfile);
printf(" ----> Wrote to file %s!\n", filename);
}
free(output);
fclose(fp_outfile);
printf("\n");
}
fclose(fp_rom);
}