forked from apritzel/sunxi-fw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunxi-img.c
284 lines (252 loc) · 6.67 KB
/
sunxi-img.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// SPDX-License-Identifier: GPL-2.0
/*
* (C) Copyright 2018, Andre Przywara
*
* sunxi-img: routines that iterate through an image file to identify, find
* or extract firmware components
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h> /* for ntohl() */
#include <errno.h>
#include "sunxi-fw.h"
#define EGON_MAGIC1 0x4e4f4765 // "eGON"
#define EGON_MAGIC2 0x3054422e // ".BT0"
#define SPL_MAGIC 0x004c5053 // "SPL\0"
#define IH_MAGIC 0x27051956
#define FDT_MAGIC 0xd00dfeed
#define TOC0_MAGIC1 0x30434f54 // "TOC0"
#define TOC0_MAGIC2 0x484c472e // ".GLH"
#define RK_IDBL_RC4 0xfcdc8c3b // RC4 encoded magic
#define RK_SIG_RK32 0x32334b52 // "RK32"
#define RK_SIG_RK33 0x33334b52 // "RK33"
#define AML_MAGIC 0x4c4d4140 // "@AML"
#define IMAGEWTY_MAGIC1 0x494d4147 // "IMAG"
#define IMAGEWTY_MAGIC2 0x45575459 // "EWTY"
static bool check_image_error(FILE *error, enum image_type type)
{
switch (type) {
case IMAGE_ERROR:
fprintf(error, "error while reading image\n");
return true;
case IMAGE_SHORT:
fprintf(error, "image file too short\n");
return true;
case IMAGE_UNKNOWN:
fprintf(error, "unknown image file\n");
return true;
default:
return false;
}
}
/* iterates through an image files to find and report about components */
void output_image_info(FILE *inf, FILE *outf, bool verbose, bool scan_all)
{
char sector[512];
enum image_type type;
size_t ret;
int ofs = 0;
do {
ret = fread(sector, 512, 1, inf);
if (ret == 0 && feof(inf))
break;
type = identify_image(sector);
switch (type) {
case IMAGE_BOOT0:
fprintf(outf, "@%4d: boot0: Allwinner boot0\n", ofs);
ofs += output_boot0_info(sector, inf, outf, verbose);
break;
case IMAGE_SPL1:
case IMAGE_SPL2:
case IMAGE_SPLx:
fprintf(outf, "@%4d: spl: U-Boot SPLv%c\n", ofs,
type == IMAGE_SPL1 ? '1' :
(type == IMAGE_SPL2 ? '2' : 'x'));
ofs += output_spl_info(sector, inf, outf, verbose);
break;
case IMAGE_TOC0:
fprintf(outf, "@%4d: toc0: signed boot image\n", ofs);
output_toc0_info(sector, inf, outf, verbose);
break;
case IMAGE_ROCKCHIP:
fprintf(outf, "@%4d: spl: Rockchip SPL image\n", ofs);
break;
case IMAGE_AML:
fprintf(outf, "@%4d: spl: Amlogic SPL image\n", ofs);
break;
case IMAGE_UBOOT:
fprintf(outf, "@%4d: u-boot.img: U-Boot legacy image\n",
ofs);
output_uboot_info(sector, inf, outf, verbose);
if (!scan_all)
return;
break;
case IMAGE_FIT:
fprintf(outf, "@%4d: fit: U-Boot FIT image\n", ofs);
ofs += dump_dt_info(sector, inf, outf, verbose);
if (!scan_all)
return;
break;
case IMAGE_MBR:
fprintf(outf, "@%4d: mbr: DOS MBR\n", ofs);
ofs += output_mbr_info(sector, inf, outf, verbose);
break;
case IMAGE_GPT:
fprintf(outf, "@%4d: gpt: UEFI GPT\n", ofs);
if (!scan_all)
pseek(inf, 17408 - 1024);
break;
case IMAGE_PHOENIX:
fprintf(outf, "@%4d: wty: PhoenixSuite image file\n",
ofs);
ofs += output_wty_info(sector, inf, outf, verbose);
if (!scan_all)
return;
break;
case IMAGE_UNKNOWN:
break;
default:
fprintf(stderr, "unknown\n");
if (check_image_error(stderr, type))
return;
break;
}
ofs++;
} while (1);
}
/* check a given sector-sized buffer for magic numbers to identify components */
enum image_type identify_image(const void *buffer)
{
const uint32_t *magic = buffer;
if (ntohl(magic[0]) == IMAGEWTY_MAGIC1 &&
ntohl(magic[1]) == IMAGEWTY_MAGIC2)
return IMAGE_PHOENIX;
if (ntohl(magic[0]) == FDT_MAGIC)
return IMAGE_FIT;
if (ntohl(magic[0]) == IH_MAGIC)
return IMAGE_UBOOT;
if (magic[1] == EGON_MAGIC1 && magic[2] == EGON_MAGIC2) {
if (magic[5] < 0x10000)
return IMAGE_BOOT0;
if (magic[5] == (SPL_MAGIC | (1U << 24)))
return IMAGE_SPL1;
if (magic[5] == (SPL_MAGIC | (2U << 24)))
return IMAGE_SPL2;
if ((magic[5] & 0xffffff) == SPL_MAGIC)
return IMAGE_SPLx;
return IMAGE_UNKNOWN;
}
if (magic[0] == TOC0_MAGIC1 && magic[1] == TOC0_MAGIC2)
return IMAGE_TOC0;
if (((unsigned char *)buffer)[510] == 0x55 &&
((unsigned char *)buffer)[511] == 0xaa)
return IMAGE_MBR;
if (!strncmp(buffer, "EFI PART", 8))
return IMAGE_GPT;
if (magic[0] == RK_IDBL_RC4 || magic[0] == RK_SIG_RK32 ||
magic[0] == RK_SIG_RK33)
return IMAGE_ROCKCHIP;
if (magic[0] == AML_MAGIC || magic[4] == AML_MAGIC)
return IMAGE_AML;
return IMAGE_UNKNOWN;
}
/* scans a file to find a specific firmware component type */
int find_firmware_image(FILE *inf, enum image_type img, void *sector,
FILE *outf)
{
enum image_type type;
size_t ret, size;
do {
ret = fread(sector, 512, 1, inf);
if (ret < 0)
return ret;
if (feof(inf))
return -ENOENT;
type = identify_image(sector);
if (type == img)
return 0;
if (img == IMAGE_SPLx &&
(type == IMAGE_SPL1 || type == IMAGE_SPL2))
return 0;
switch (type) {
case IMAGE_MBR:
if (outf) {
fwrite(sector, 512, 1, outf);
copy_file(inf, outf, 8192 - 512);
} else
pseek(inf, 8192 - 512);
break;
case IMAGE_BOOT0:
case IMAGE_SPL1:
case IMAGE_SPL2:
case IMAGE_SPLx:
size = ((uint32_t *)sector)[4];
if (outf) {
fwrite(sector, 512, 1, outf);
copy_file(inf, outf, size - 512);
} else
pseek(inf, size - 512);
break;
case IMAGE_UBOOT:
case IMAGE_FIT:
return -ENOENT;
default:
if (check_image_error(stderr, type))
return -ENOENT;
}
} while (1);
}
/* find an image type identified by a string and writes it to @outf */
int extract_image(FILE *inf, FILE *outf, const char *extract)
{
char sector[512];
enum image_type type = IMAGE_UNKNOWN;
int ret, size;
if (!strcmp(extract, "mbr"))
type = IMAGE_MBR;
if (!strcmp(extract, "boot0"))
type = IMAGE_BOOT0;
if (!strcmp(extract, "spl"))
type = IMAGE_SPLx;
if (!strncmp(extract, "u-boot", 6))
type = IMAGE_UBOOT;
if (!strncmp(extract, "fit", 3))
type = IMAGE_FIT;
if (!strncmp(extract, "wty", 3))
type = IMAGE_PHOENIX;
ret = find_firmware_image(inf, type, sector, NULL);
if (ret)
return ret;
switch (type) {
case IMAGE_MBR:
fwrite(sector, 1, 512, outf);
return 0;
case IMAGE_BOOT0:
case IMAGE_SPL1:
case IMAGE_SPL2:
case IMAGE_SPLx:
size = ((uint32_t *)sector)[4];
fwrite(sector, 1, 512, outf);
copy_file(inf, outf, size - 512);
return 0;
case IMAGE_UBOOT:
if (!strcmp(extract, "u-boot.img"))
dump_uboot_legacy(sector, inf, outf, 0);
else if (!strcmp(extract, "u-boot"))
dump_uboot_legacy(sector, inf, outf, 1);
return 0;
case IMAGE_FIT:
extract_fit_image(sector, inf, outf, extract);
return 0;
case IMAGE_PHOENIX:
extract_wty_image(sector, inf, outf, extract);
return 0;
default:
if (check_image_error(stderr, type))
return -type;
}
return -ENOENT;
}