-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathimg_maker.c
191 lines (154 loc) · 3.88 KB
/
img_maker.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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include "rkrom_29xx.h"
#include "rkafp.h"
#include "md5.h"
unsigned int chiptype = 0x50;
unsigned int import_data(const char* infile, void *head, size_t head_len, FILE *fp)
{
FILE *in_fp = NULL;
unsigned readlen = 0;
unsigned char buffer[1024];
in_fp = fopen(infile, "rb");
if (!in_fp)
goto import_end;
readlen = fread(head, 1, head_len, in_fp);
if (readlen)
{
fwrite(head, 1, readlen, fp);
}
while (1)
{
int len = fread(buffer, 1, sizeof(buffer), in_fp);
if (len)
{
fwrite(buffer, 1, len, fp);
readlen += len;
}
if (len != sizeof(buffer))
break;
}
import_end:
if (in_fp)
fclose(in_fp);
return readlen;
}
void append_md5sum(FILE *fp)
{
MD5_CTX md5_ctx;
unsigned char buffer[1024];
int i;
MD5_Init(&md5_ctx);
fseek(fp, 0, SEEK_SET);
while (1)
{
int len = fread(buffer, 1, sizeof(buffer), fp);
if (len)
{
MD5_Update(&md5_ctx, buffer, len);
}
if (len != sizeof(buffer))
break;
}
MD5_Final(buffer, &md5_ctx);
for (i = 0; i < 16; ++i)
{
fprintf(fp, "%02x", buffer[i]);
}
}
int pack_rom(const char *loader_filename, const char *image_filename, const char *outfile)
{
time_t nowtime;
struct tm local_time;
int i;
struct _rkfw_header rom_header = {
.head_code = "RKFW",
.head_len = 0x66,
.loader_offset = 0x66
};
struct update_header rkaf_header;
struct bootloader_header loader_header;
rom_header.chip = chiptype;
rom_header.code = 0x01030000;
nowtime = time(NULL);
localtime_r(&nowtime, &local_time);
rom_header.year = local_time.tm_year + 1900;
rom_header.month = local_time.tm_mon + 1;
rom_header.day = local_time.tm_mday;
rom_header.hour = local_time.tm_hour;
rom_header.minute = local_time.tm_min;
rom_header.second = local_time.tm_sec;
FILE *fp = fopen(outfile, "wb+");
if (!fp)
{
fprintf(stderr, "Can't open file %s\n, reason: %s\n", outfile, strerror(errno));
goto pack_fail;
}
unsigned char buffer[0x66];
if (1 != fwrite(buffer, 0x66, 1, fp))
goto pack_fail;
/*
printf("rom version: %x.%x.%x\n",
(rom_header.version >> 24) & 0xFF,
(rom_header.version >> 16) & 0xFF,
(rom_header.version) & 0xFFFF);
printf("build time: %d-%02d-%02d %02d:%02d:%02d\n",
rom_header.year, rom_header.month, rom_header.day,
rom_header.hour, rom_header.minute, rom_header.second);
printf("chip: %x\n", rom_header.chip);
*/
fseek(fp, rom_header.loader_offset, SEEK_SET);
fprintf(stderr, "generate image...\n");
rom_header.loader_length = import_data(loader_filename, &loader_header, sizeof(loader_header), fp);
if (rom_header.loader_length < sizeof(loader_header))
{
fprintf(stderr, "invalid loader :\"\%s\"\n", loader_filename);
goto pack_fail;
}
rom_header.image_offset = rom_header.loader_offset + rom_header.loader_length;
rom_header.image_length = import_data(image_filename, &rkaf_header, sizeof(rkaf_header), fp);
if (rom_header.image_length < sizeof(rkaf_header))
{
fprintf(stderr, "invalid rom :\"\%s\"\n", image_filename);
goto pack_fail;
}
rom_header.version = rkaf_header.version;
rom_header.unknown2 = 1;
rom_header.system_fstype = 0;
for (i = 0; i < rkaf_header.num_parts; ++i)
{
if (strcmp(rkaf_header.parts[i].name, "backup") == 0)
break;
}
if (i < rkaf_header.num_parts)
rom_header.backup_endpos = (rkaf_header.parts[i].nand_addr + rkaf_header.parts[i].nand_size) / 0x800;
else
rom_header.backup_endpos = 0;
fseek(fp, 0, SEEK_SET);
if (1 != fwrite(&rom_header, sizeof(rom_header), 1, fp))
goto pack_fail;
fprintf(stderr, "append md5sum...\n");
append_md5sum(fp);
fclose(fp);
fprintf(stderr, "success!\n");
return 0;
pack_fail:
if (fp)
fclose(fp);
return -1;
}
int main(int argc, char **argv)
{
if (argc == 4)
{
pack_rom(argv[1], argv[2], argv[3]);
}
else
{
fprintf(stderr, "usage: %s <loader> <old image> <out image>\n", argv[0]);
}
return 0;
}