forked from askac/dumpifs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixdecifs.c
181 lines (160 loc) · 4.48 KB
/
fixdecifs.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
#include <stdio.h>
#include <stdlib.h>
//#ifdef QNX
#include <stdarg.h>
//#endif
#include "sys/image.h"
#include "sys/startup.h"
#define ENDIAN_RET32(x) ((((x) >> 24) & 0xff) | \
(((x) >> 8) & 0xff00) | \
(((x) & 0xff00) << 8) | \
(((x) & 0xff) << 24))
#define ENDIAN_RET16(x) ((((x) >> 8) & 0xff) | \
(((x) & 0xff) << 8))
static char* progname = 0;
static char* file = 0;
static int fixchksum = 0;
static int find(FILE *fp, const unsigned char *p, int len, int count) {
int c;
int i;
int n;
i = n = 0;
while(i < len && (c = fgetc(fp)) != EOF) {
n++;
if(c == p[i]) {
i++;
} else {
if(count != -1 && n >= count) {
break;
}
i = c == p[0] ? 1 : 0;
}
}
if(i < len) {
return -1;
}
return ftell(fp) - i;
}
static void error(int level, char *p, ...) {
va_list ap;
fprintf(stderr, "%s: ", progname);
va_start(ap, p);
vfprintf(stderr, p, ap);
va_end(ap);
fputc('\n', stderr);
if(level == 0) {
exit(EXIT_FAILURE);
}
}
static int zero_ok (struct startup_header *shdr) {
return(shdr->zero[0] == 0 &&
shdr->zero[1] == 0 &&
shdr->zero[2] == 0 );
}
static void process(FILE *fp)
{
struct startup_header shdr = { STARTUP_HDR_SIGNATURE };
struct image_header ihdr = { IMAGE_SIGNATURE };
struct image_trailer itlr;
int spos;
int ipos;
int fsize;
if((ipos = find(fp, (char*)&ihdr.signature, sizeof ihdr.signature, 0)) == -1) {
rewind(fp);
while(1){
if((spos = find(fp, (char *)&shdr.signature, sizeof shdr.signature, -1)) == -1) {
shdr.signature = ENDIAN_RET32(shdr.signature);
rewind(fp);
if((spos = find(fp, (char *)&shdr.signature, sizeof shdr.signature, -1)) == -1) {
error(1, "Unable to find startup header in %s", (char*)file);
return;
}
}
if(fread((char *)&shdr + sizeof shdr.signature, sizeof shdr - sizeof shdr.signature, 1, fp) != 1) {
error(1, "Unable to read image %s", (char*)file);
return;
}
if (!zero_ok(&shdr)) {
//if (zero_check_enabled)
continue;
//if (verbose)
// printf("Warning: Non zero data in zero fields ignored\n");
//break;
} else {
break;
}
break;
}
}
fseek(fp, spos + shdr.startup_size, SEEK_SET);
if((ipos = find(fp, ihdr.signature, sizeof ihdr.signature, -1)) == -1) {
error(1, "Unable to find image header in %s", file);
return;
}
if(fread((char *)&ihdr + sizeof ihdr.signature, sizeof ihdr - sizeof ihdr.signature, 1, fp) != 1) {
error(1, "Unable to read image %s", file);
return;
}
fseek(fp, 0 , SEEK_END);
fsize = ftell(fp);
if(fsize >= (spos + shdr.startup_size + ihdr.image_size)){
int data;
int sum;
int counted=0;
int len = ihdr.image_size;
unsigned max;
fseek(fp, ipos + ihdr.image_size-sizeof(itlr), SEEK_SET);
if(fread(&itlr, sizeof(itlr), 1, fp) != 1) {
error(1, "Early end reading image trailer");
return;
}
fseek(fp, ipos , SEEK_SET);
sum = 0;
while(len > 4)
{
fread(&data, 4, 1, fp);
sum += data;
counted +=4;
len -= 4;
}
sum = -1*sum;
if(0 != (itlr.cksum - sum))
{
printf("\nStored checksum not correct!\n");
printf("Expected checksum: %#lx\n", sum);
printf(" NG: %02x %02x %02x %02x\n", itlr.cksum & 0xff, (itlr.cksum >> 8) & 0xff, (itlr.cksum >> 16) & 0xff, (itlr.cksum >> 24) & 0xff);
printf("GOOD: %02x %02x %02x %02x\n", sum & 0xff, (sum >> 8) & 0xff, (sum >> 16) & 0xff, (sum >> 24) & 0xff);
if(fixchksum)
{
fseek(fp, ipos + ihdr.image_size-sizeof(itlr), SEEK_SET);
itlr.cksum = sum;
printf("Update - write %d bytes (expect write %d bytes)\n", fwrite(&itlr, sizeof itlr, 1, fp) * sizeof itlr, sizeof itlr);
fclose(fp);
}
}
} else {
printf("Image size is too small! Must at lease %d bytes.\n", (spos + shdr.startup_size + ihdr.image_size));
}
printf("Found ipos=%#lx spos=%#lx (%d)\n", ipos, spos, spos);
printf("Compressed data offset=%#lx\n", spos + shdr.startup_size);
printf("Image startup_size=%#lx (%ld) image_size=%#lx (%ld)\n", shdr.startup_size, shdr.startup_size, ihdr.image_size, ihdr.image_size);
}
int main(int argc, char **argv)
{
FILE *fp;
progname = argv[0];
if(argc < 2){
printf("Usage: %s <decompressed ifs> [Y]\n", argv[0]);
return 0;
}
file = (char*)argv[1];
if(3 == argc && (0 == strcmp("Y", argv[2])))
{
fixchksum = 1;
}
if(0 != (fp = fopen(argv[1], 0==fixchksum?"r":"rw+")))
{
process(fp);
}
return 0;
}