forked from dayongxie/rk2918_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkkrnlimg.c
146 lines (118 loc) · 2.43 KB
/
mkkrnlimg.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "rkcrc.h"
#define MAGIC_CODE "KRNL"
struct krnl_header
{
char magic[4];
unsigned int length;
};
int pack_krnl(FILE *fp_in, FILE *fp_out)
{
char buf[1024];
struct krnl_header header =
{
"KRNL",
0
};
unsigned int crc = 0;
fwrite(&header, sizeof(header), 1, fp_out);
while (1)
{
int readlen = fread(buf, 1, sizeof(buf), fp_in);
if (readlen == 0)
break;
header.length += readlen;
fwrite(buf, 1, readlen, fp_out);
RKCRC(crc, buf, readlen);
}
fwrite(&crc, sizeof(crc), 1, fp_out);
fseek(fp_out, 0, SEEK_SET);
fwrite(&header, sizeof(header), 1, fp_out);
printf("%04X\n", crc);
return 0;
//fail:
fprintf(stderr, "FAIL\n");
return -1;
}
int unpack_krnl(FILE *fp_in, FILE *fp_out)
{
char buf[1024];
struct krnl_header header;
size_t length = 0;
unsigned int crc = 0;
unsigned int file_crc = 0;
fprintf(stderr, "unpacking...");
fflush(stderr);
if (sizeof(header) != fread(&header, 1, sizeof(header), fp_in))
{
goto fail;
}
fseek(fp_in, header.length + sizeof(header), SEEK_SET);
if (sizeof(file_crc) != fread(&file_crc, 1, sizeof(file_crc), fp_in))
goto fail;
length = header.length;
fseek(fp_in, sizeof(header), SEEK_SET);
while (length > 0)
{
int readlen = length < sizeof(buf) ? length : sizeof(buf);
readlen = fread(buf, 1, readlen, fp_in);
length -= readlen;
fwrite(buf, 1, readlen, fp_out);
RKCRC(crc, buf, readlen);
if (readlen == 0)
break;
}
if (file_crc != crc)
fprintf(stderr, "WARNING: bad crc checksum\n");
fprintf(stderr, "OK\n");
return 0;
fail:
fprintf(stderr, "FAIL\n");
return -1;
}
int main(int argc, char **argv)
{
FILE *fp_in, *fp_out;
int action = 0;
if (argc != 4)
{
fprintf(stderr, "usage: %s [-a|-r] <input> <output>\n", argv[0]);
return 1;
}
if (strcmp(argv[1], "-a") == 0)
{
action = 1;
} else if (strcmp(argv[1], "-r") == 0)
{
action = 2;
} else {
fprintf(stderr, "usage: %s [-a|-r] <input> <output>\n", argv[0]);
}
fp_in = fopen(argv[2], "rb");
if (!fp_in)
{
fprintf(stderr, "can't open input file '%s': %s\n", argv[2], strerror(errno));
return 1;
}
fp_out = fopen(argv[3], "wb");
if (!fp_out)
{
fprintf(stderr, "can't open output file '%s': %s\n", argv[3], strerror(errno));
return 1;
}
switch (action)
{
case 1:
pack_krnl(fp_in, fp_out);
break;
case 2:
unpack_krnl(fp_in, fp_out);
break;
default:
break;
}
return 0;
}