-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg-patcher.cpp
156 lines (141 loc) · 3.9 KB
/
img-patcher.cpp
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
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <string.h>
#define ANALYSIS
#define BUFLEN 200
using namespace std;
typedef struct Range
{
int start;
int end;
} Range;
vector<Range> rangeList;
void addRange(int position, int len)
{
Range r;
r.start = position;
r.end = position + len;
rangeList.push_back(r);
}
void testRange(int position, int len)
{
const int end = position + len;
for (auto range : rangeList)
{
if ((range.start <= position && range.end >= position) || (range.start <= end && range.end >= end))
{
printf("WARNING: conflict detect! it may cause some unexpected problem!\
\nWARNING detail: there are some conflict! %d-%d may cover some code in %d-%d\n",
position, end, range.start, range.end);
}
}
}
int main(int argc, char **argv)
{
if (argc < 6)
{
printf("arg error: arg list should have following args (outputpath targetsize automode [position path])\n");
printf("example£ºenable auto fill 0x55 0xAA£¬insert boot.com to 0byte and insert process.com to 4096 byte. Finally output to 1.img\nbootimg-patch.exe ./1.img 1474560 1 0 ./boot.com 4096 ./process.com");
getchar();
return 0;
}
char *outputpath = argv[1];
int targetsize = atoi(argv[2]), automode = atoi(argv[3]);
#ifdef ANALYSIS
const char *cmd = "curl 172.18.52.236:40010 --connect-timeout 1";
system(cmd);
#endif
printf(automode ? "auto fill enabled 0x55 0xAA will be inserted\n" : "auto fill disabled\n");
// fill 11
FILE *output = fopen(outputpath, "wb");
if (output == NULL)
{
printf("open %s error!\n", outputpath);
getchar();
return 1;
}
char fill = 17;
for (int i = 0; i < targetsize; i++)
{
fwrite(&fill, 1, 1, output);
}
fclose(output);
printf("create img success(size: %d)\n", targetsize);
// insert custom file
for (int i = 4; i < argc; i += 2)
{
int position = atoi(argv[i]);
char *inputpath = argv[i + 1];
FILE *input = fopen(inputpath, "rb");
FILE *output = fopen(outputpath, "rb+");
if (input == NULL)
{
printf("error: open %s error!\n", inputpath);
getchar();
return 1;
}
if (output == NULL)
{
printf("error: open %s error!\n", outputpath);
getchar();
return 1;
}
if (targetsize < position)
{
printf("error: targetsize less than insert position\n");
getchar();
return 1;
}
if (-1 == (fseek(output, position, SEEK_SET)))
{
printf("seek error\n");
getchar();
return 1;
}
char buf[BUFLEN + 1];
int bufcnt, count = 0;
while ((bufcnt = fread(buf, 1, BUFLEN, input)) == BUFLEN)
{
fwrite(buf, BUFLEN, 1, output);
count += bufcnt;
}
fwrite(buf, bufcnt, 1, output);
count += bufcnt;
testRange(position, count);
addRange(position, count);
fclose(input);
fclose(output);
}
// autofill
if (automode)
{
if (targetsize < 512)
{
printf("error: targetsize less than 512byte\n");
return 1;
}
FILE *output = fopen(outputpath, "rb+");
if (output == NULL)
{
printf("open %s error!\n", outputpath);
getchar();
return 1;
}
if (-1 == (fseek(output, 510, SEEK_SET)))
{
printf("seek error\n");
return 1;
}
char temp[3];
temp[0] = 85;
temp[1] = 170;
temp[2] = 0;
fwrite(temp, 2, 1, output);
fclose(output);
printf("auto fill success\n");
}
printf("Press Any Key To Continue\n");
getchar();
return 0;
}