-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtableExtractor.c
145 lines (120 loc) · 2.89 KB
/
tableExtractor.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include "bSwap.h"
#define UINTSIZE 0x1000000
#define COMPSIZE 0x2000000
#define FILENAME "dmaTable.dat"
/* Structs */
typedef struct
{
uint32_t startV;
uint32_t endV;
uint32_t startP;
uint32_t endP;
}
table_t;
/* Functions */
uint32_t findTable();
table_t getTableEnt();
void errorCheck(int, char**);
/* Globals */
uint8_t* inROM;
uint32_t* fileTab;
int main(int argc, char** argv)
{
FILE* file;
int32_t tabStart, tabSize, tabCount, i;
uint8_t* refTab;
table_t tab;
errorCheck(argc, argv);
/* Open input, read into inROM */
file = fopen(argv[1], "rb");
inROM = malloc(COMPSIZE);
fread(inROM, COMPSIZE, 1, file);
fclose(file);
/* Find file table, write to fileTab */
tabStart = findTable();
fileTab = (uint32_t*)(inROM + tabStart);
tab = getTableEnt(2);
tabSize = tab.endV - tab.startV;
tabCount = tabSize / 16;
/* Open the file */
file = fopen(FILENAME, "w");
/* Allocate space, add first 3 files to exclusion list */
refTab = malloc(tabCount-1);
fprintf(file, "0 1 2");
/* If file is decompressed, write the number to file */
/* If the file doesn't exist, write the negative number */
for(i = 3; i < tabCount; i++)
{
tab = getTableEnt(i);
if(tab.endP == 0)
fprintf(file, " %d", i);
else if(tab.endP == 0xFFFFFFFF)
fprintf(file, " %d", -i);
}
/* Clean up memory */
fclose(file);
free(inROM);
free(refTab);
return(0);
}
uint32_t findTable()
{
uint32_t i;
uint32_t* tempROM;
tempROM = (uint32_t*)inROM;
for(i = 1048; i+4 < UINTSIZE; i += 4)
{
if(tempROM[i] == 0x00000000)
if(tempROM[i+1] == 0x60100000)
return(i * 4);
}
fprintf(stderr, "Error: Couldn't find dma table in the ROM\n");
exit(1);
}
table_t getTableEnt(uint32_t i)
{
table_t tab;
tab.startV = bSwap32(fileTab[i*4]);
tab.endV = bSwap32(fileTab[(i*4)+1]);
tab.startP = bSwap32(fileTab[(i*4)+2]);
tab.endP = bSwap32(fileTab[(i*4)+3]);
return(tab);
}
void errorCheck(int argc, char** argv)
{
FILE* file;
/* Check for arguments */
if(argc != 2)
{
fprintf(stderr, "Usage: %s [Input ROM]\n", argv[0]);
exit(1);
}
/* Check that input ROM exists */
file = fopen(argv[1], "rb");
if(file == NULL)
{
perror(argv[1]);
exit(1);
}
/* Check that input ROM is correct size */
fseek(file, 0, SEEK_END);
if(ftell(file) != COMPSIZE)
{
fprintf(stderr, "Error: Invalid input ROM size\n");
fclose(file);
exit(1);
}
fclose(file);
/* Check that FILENAME is writable */
file = fopen(FILENAME, "w");
if(file == NULL)
{
perror(FILENAME);
exit(1);
}
fclose(file);
}