-
Notifications
You must be signed in to change notification settings - Fork 46
/
chsize.c
58 lines (50 loc) · 2.25 KB
/
chsize.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define ERR_EXIT(...) \
do { fprintf(stderr, __VA_ARGS__); exit(1); } while (0)
static uint8_t* loadfile(const char* fn, size_t* num, size_t extra) {
size_t n, j = 0; uint8_t* buf = 0;
FILE* fi = fopen(fn, "rb");
if (fi) {
fseek(fi, 0, SEEK_END);
n = ftell(fi);
if (n) {
fseek(fi, 0, SEEK_SET);
buf = (uint8_t*)malloc(n + extra);
if (buf) j = fread(buf, 1, n, fi);
}
fclose(fi);
}
if (num) *num = j;
return buf;
}
int main(int argc, char** argv)
{
if (argc < 2) ERR_EXIT("Usage: %s <filename>\n", argv[0]);
char* filename = argv[1];
uint8_t* mem; size_t size = 0;
mem = loadfile(filename, &size, 0);
if (!mem) ERR_EXIT("loadfile(\"%s\") failed\n", filename);
if ((uint64_t)size >> 32) ERR_EXIT("file too big\n");
if (!(*(uint32_t*)&mem[0x30])) ERR_EXIT("The file is not sprd trusted firmware\n");
if (*(uint32_t*)&mem[0x30] + 0x200 > size) ERR_EXIT("The file is not complete\n");
if (*(uint32_t*)&mem[0x30] != *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x10])
size = *(uint32_t*)&mem[0x30] + 0x200;
else if (*(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x50])
size = *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x50] + *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x58];
else if (*(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x30])
size = *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x30] + *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x38];
else
size = *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x20] + *(uint32_t*)&mem[(*(uint32_t*)&mem[0x30]) + 0x200 + 0x28];
printf("0x%zx\n", size);
FILE* file = fopen("temp", "wb");
if (file == NULL) ERR_EXIT("Failed to create the file.\n");
size_t bytes_written = fwrite(mem, sizeof(unsigned char), size, file);
if (bytes_written != size) ERR_EXIT("Failed to write the file.\n");
fclose(file);
if (remove(filename)) ERR_EXIT("Failed to delete the file.\n");
if (rename("temp", filename)) ERR_EXIT("Failed to rename the file.\n");
free(mem);
return 0;
}