-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathelftool.cpp
More file actions
378 lines (343 loc) · 11.2 KB
/
elftool.cpp
File metadata and controls
378 lines (343 loc) · 11.2 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* File: packelf.cpp
* Author: srl3gx@gmail.com
*
* Packing and unpacking boot image of sony mobile devices
* https://forum.xda-developers.com/xperia-j-e/development/arm-elftool-pack-unpack-boot-image-sony-t2146022
*
* Thanks to sony for providing boot image format in packelf.py
*
*/
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "elfboot.h"
using namespace std;
void handlePackElf(int, char**);
void handleUnpackElf(int, char**);
void usage();
unsigned int getAddressFromHeader(string);
long getFileSize(FILE*);
void writeElfHeader(FILE*, unsigned int, int);
struct elfphdr part_headers[5];
struct file {
string file_path;
unsigned int address;
unsigned int size;
string flag;
unsigned int offset;
};
void writeElfPHeader(FILE*, struct file);
int main(int argc, char** argv) {
if (argc <= 1) {
printf("Invalid format...EXIT\n");
usage();
}
char* arg = argv[1];
if (strcmp(arg, "pack") == 0) {
printf("Packing elf file.....\n");
handlePackElf(argc, argv);
} else if (strcmp(arg, "unpack") == 0) {
printf("Unpacking elf file.....\n");
handleUnpackElf(argc, argv);
} else {
printf("Invalid format....EXIT");
usage();
}
return 0;
}
void handlePackElf(int argc, char** argv) {
struct file files[argc - 1];
string output_path;
FILE *header;
FILE *temp_file;
int parts = 0;
int offset = 4096;
for (int i = 2; i < argc; i++) {
char* arg = argv[i];
string arguments[3];
if (strcmp(arg, "-o") == 0 || strcmp(arg, "--output") == 0) {
i++;
output_path = argv[i];
continue;
}
arg = strtok(arg, "@,=");
int k = 0;
while (arg != NULL) {
arguments[k] = arg;
arg = strtok(NULL, "@,");
k++;
}
long address;
if (arguments[1].compare("cmdline") == 0) {
address = 0L;
arguments[2] = "cmdline";
} else if (arguments[0].compare("header") == 0) {
header = fopen(arguments[1].c_str(), "rb");
struct elf_header boot_header;
fseek(header, 0, SEEK_SET);
if (fread(&boot_header, elf_header_size, 1, header)) {};
if (memcmp(boot_header.e_ident, elf_magic, 8) != 0) {
printf("Header file is not a valid elf image header...Exit");
exit(EXIT_FAILURE);
}
int offset = 52;
for (int i = 0; i < boot_header.e_phnum; i++) {
struct elfphdr part_header;
fseek(header, offset, SEEK_SET);
if (fread(&part_header, elf_p_header_size, 1, header)) {};
part_headers[i] = part_header;
offset += 32;
}
fclose(header);
continue;
} else {
address = strtol(arguments[1].c_str(), NULL, 16);
if (address == 0) {
if (arguments[1].empty()){
arguments[1] = "kernel";
}
arguments[2] = arguments[1];
address = getAddressFromHeader(arguments[1]);
}
}
printf("Reading file %s\n", arguments[0].c_str());
temp_file = fopen(arguments[0].c_str(), "r");
if (temp_file == NULL) {
printf("Failed to open %s\n", arguments[0].c_str());
exit(EXIT_FAILURE);
}
long size = getFileSize(temp_file);
struct file f = {
arguments[0],
static_cast<unsigned int>(address),
static_cast<unsigned int>(size),
arguments[2],
static_cast<unsigned int>(offset)
};
offset += size;
fclose(temp_file);
files[parts] = f;
parts++;
}
if (parts < 2) {
printf("Kernel and ramdisk must be specified....Error\n");
usage();
}
if (output_path.empty()) {
printf("Output path must be specified....Error\n");
usage();
}
FILE* output_file = fopen(output_path.c_str(), "wb");
if (output_file == NULL) {
printf("Invalid path %s\n", output_path.c_str());
}
writeElfHeader(output_file, files[0].address, parts);
for (int i = 0; i < parts; i++) {
writeElfPHeader(output_file, files[i]);
}
for (int i = 0; i < parts; i++) {
struct file current_file = files[i];
fseek(output_file, current_file.offset, SEEK_SET);
printf("Writing file : %s to elf image\n", current_file.file_path.c_str());
temp_file = fopen(current_file.file_path.c_str(), "r");
if (temp_file == NULL) {
printf("Cannot read file %s\n", current_file.file_path.c_str());
exit(EXIT_FAILURE);
}
int size = getFileSize(temp_file);
unsigned char* data = new unsigned char [size];
fseek(temp_file, 0, SEEK_SET);
if (fread(data, size, 1, temp_file)) {};
fclose(temp_file);
fwrite(data, size, 1, output_file);
}
fclose(output_file);
}
void writeElfHeader(FILE* file, unsigned int address, int number) {
printf("Writing elf header\t address : %x \t number : %i\n", address, number);
struct elf_header header = {
{0x7F, 0x45, 0x4C, 0x46, 0x01, 0x01, 0x01, 0x61},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
2,
40,
1,
address,
52,
0,
0,
52,
32,
static_cast<short unsigned int>(number),
0,
0,
0
};
fwrite((char *) &header, sizeof (header), 1, file);
}
void writeElfPHeader(FILE* file, struct file f) {
printf("Writing part header for %s\t", f.file_path.c_str());
long flags;
long type = 1;
if (f.flag.compare("ramdisk") == 0) {
printf("Found ramdisk\n");
flags = 0x80000000;
} else if (f.flag.compare("ipl") == 0) {
printf("Found ipl\n");
flags = 0x40000000;
} else if (f.flag.compare("cmdline") == 0) {
printf("Found cmdline\n");
flags = 0x20000000;
type = 4;
} else if (f.flag.compare("rpm") == 0) {
printf("Found rpm\n");
flags = 0x01000000;
} else {
printf("Using zero flag\n");
flags = 0;
}
printf("Write part header part:%s offset:%i address:%x, size:%i\n", f.flag.c_str(), f.offset, f.address, f.size);
struct elfphdr phdr = {
static_cast<unsigned int>(type),
f.offset,
f.address,
f.address,
f.size,
f.size,
static_cast<unsigned int>(flags),
0
};
fwrite((char *) &phdr, sizeof (phdr), 1, file);
}
long getFileSize(FILE* file) {
fseek(file, 0, SEEK_END);
return ftell(file);
}
void handleUnpackElf(int argc, char** argv) {
string input_path;
string output_path;
for (int i = 2; i < argc; i += 2) {
char* arg = argv[i];
if (strcmp(arg, "-i") == 0 || strcmp(arg, "--input") == 0) {
input_path = argv[i + 1];
} else if (strcmp(arg, "-o") == 0 || strcmp(arg, "--output") == 0) {
output_path = argv[i + 1];
}
}
if (input_path.empty()) {
printf("Input path not provided...Exit\n");
usage();
}
if (output_path.empty()) {
printf("Output path must be specified....Exit\n");
usage();
}
int offset = 0;
FILE* temp;
string tempstr;
FILE* input_file = fopen(input_path.c_str(), "rb");
if (input_file == NULL) {
printf("Cannot read input boot image %s....Exit", input_path.c_str());
}
fseek(input_file, offset, SEEK_SET);
struct elf_header header;
if (fread(&header, elf_header_size, 1, input_file)) {};
offset += elf_header_size;
if (memcmp(elf_magic, header.e_ident, elf_magic_size) != 0) {
printf("ELF magic not found....EXIT");
exit(EXIT_FAILURE);
}
int number_of_parts = header.e_phnum;
printf("Found %i parts in elf image\n", number_of_parts);
struct elfphdr pheaders[number_of_parts];
for (int i = 0; i < number_of_parts; i++) {
fseek(input_file, offset, SEEK_SET);
if (fread(&pheaders[i], elf_p_header_size, 1, input_file)) {};
offset += elf_p_header_size;
}
//dump header
printf("Writing header....\n");
unsigned char* fileBuffer = (unsigned char*) malloc(4096);
fseek(input_file, 0, SEEK_SET);
if (fread(fileBuffer, 4096, 1, input_file)) {};
tempstr = output_path + "/header";
temp = fopen(tempstr.c_str(), "w+");
fwrite(fileBuffer, 4096, 1, temp);
fclose(temp);
free(fileBuffer);
printf("Done...\n");
for (int i = 0; i < number_of_parts; i++) {
struct elfphdr pheader = pheaders[i];
printf("flag : %u\n", pheader.p_flags);
printf("offset : %i\n", pheader.p_offset);
printf("size : %i\n", pheader.p_memsz);
string name;
switch (pheader.p_flags) {
case p_flags_cmdline:
name = "cmdline";
break;
case p_flags_kernel:
name = "kernel";
break;
case p_flags_ipl:
name = "ipl";
break;
case p_flags_rpm:
name = "rpm";
default:
name = "ramdisk";
}
tempstr = output_path + "/" + name;
printf("%s found at offset %i with size %i\n", name.c_str(), pheader.p_offset, pheader.p_memsz);
temp = fopen(tempstr.c_str(), "w+");
unsigned char* buffer = (unsigned char*) malloc(pheader.p_memsz);
fseek(input_file, pheader.p_offset, SEEK_SET);
if (fread(buffer, pheader.p_memsz, 1, input_file)) {};
fwrite(buffer, pheader.p_memsz, 1, temp);
fclose(temp);
free(buffer);
}
fclose(input_file);
}
unsigned int getAddressFromHeader(string flag) {
unsigned int part_flag;
if (flag.compare("cmdline") == 0) {
part_flag = p_flags_cmdline;
} else if (flag.compare("kernel") == 0) {
part_flag = p_flags_kernel;
} else if (flag.compare("ramdisk") == 0) {
part_flag = p_flags_ramdisk;
} else if (flag.compare("rpm") == 0) {
part_flag = p_flags_rpm;
} else if (flag.compare("ipl") == 0) {
part_flag = p_flags_ipl;
} else {
printf("Unknown flag : %s\n", flag.c_str());
exit(EXIT_FAILURE);
}
for (int i = 0; i < 5; i++) {
struct elfphdr part_header;
part_header = part_headers[i];
if (part_header.p_flags == part_flag) {
return part_header.p_paddr;
}
}
printf("Address of %x cannot found from header file", part_flag);
exit(EXIT_FAILURE);
}
void usage() {
printf("Usage:\n\n");
printf("For packing\n");
printf("If you have header file containing address of kernel, ramdisk etc..\n");
printf("elftool pack -o output-path header=path/to/header kernel-path "
"ramdisk-path,ramdisk ipl-path,ipl "
"rpm-path,rpm cmdline-path@cmdline\n\n");
printf("elftool pack -o output-path kernel-path@address "
"ramdisk-path@address,ramdisk ipl-path@address,ipl "
"rpm-path@address,rpm cmdline-path@cmdline\n\n");
printf("For unpacking\n");
printf("elftool unpack -i input-path -o output-path\n");
exit(EXIT_FAILURE);
}