-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchange_os_version.c
More file actions
286 lines (242 loc) · 6.81 KB
/
change_os_version.c
File metadata and controls
286 lines (242 loc) · 6.81 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
#include <errno.h>
#include <err.h>
#include <stdbool.h>
#include <stdint.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <linux/fs.h>
#define BOOT_MAGIC_SIZE 8
#define BOOT_IMAGE_HEADER_V1_SIZE 1648
#define BOOT_IMAGE_HEADER_V2_SIZE 1660
#define HEADER_VERSION_OFFSET ( \
sizeof(uint8_t) * BOOT_MAGIC_SIZE + \
8 * sizeof(uint32_t) \
)
#define OS_VERSION_OFFSET_V0 (HEADER_VERSION_OFFSET + sizeof(uint32_t))
#define OS_VERSION_OFFSET_V1 OS_VERSION_OFFSET_V0
#define OS_VERSION_OFFSET_V2 OS_VERSION_OFFSET_V0
#define OS_VERSION_OFFSET_V3 (HEADER_VERSION_OFFSET - 6 * sizeof(uint32_t))
typedef union __attribute__((packed)) {
uint32_t version;
struct {
unsigned os_patch_level:11;
unsigned os_version:21;
};
struct {
unsigned month:4;
unsigned year:7;
unsigned c:7;
unsigned b:7;
unsigned a:7;
};
} os_version_t;
static bool stop_on(bool print_errno, bool cond, const char *message, ...)
{
if (!cond)
return false;
va_list args;
va_start(args, message);
vfprintf(stderr, message, args);
va_end(args);
if (print_errno)
fprintf(stderr, ": %s", strerror(errno));
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
return true;
}
#define stopx(cond, ...) stop_on(false, cond, __VA_ARGS__)
#define stop(cond, ...) stop_on(true, cond, __VA_ARGS__)
static bool check_cond(bool print_errno, bool cond, const char *message, ...)
{
if (!cond)
return false;
va_list args;
va_start(args, message);
vfprintf(stderr, message, args);
va_end(args);
if (print_errno)
fprintf(stderr, ": %s", strerror(errno));
fprintf(stderr, "\n");
return true;
}
#define checkx(cond, ...) check_cond(false, cond, __VA_ARGS__)
#define check(cond, ...) check_cond(true, cond, __VA_ARGS__)
static inline uint32_t get_header_version(uint8_t *addr)
{
return *(uint32_t *)(addr + HEADER_VERSION_OFFSET);
}
static inline os_version_t get_os_version(uint8_t *addr)
{
os_version_t v;
uint32_t hdr_ver = get_header_version(addr);
switch (hdr_ver) {
case 0:
case 1:
case 2:
v.version = *(uint32_t *)(addr + OS_VERSION_OFFSET_V2);
break;
case 3:
v.version = *(uint32_t *)(addr + OS_VERSION_OFFSET_V3);
break;
default:
errx(1, "Unsupported header version %u", hdr_ver);
break;
}
return v;
}
static inline void set_os_version(uint8_t *addr, os_version_t v)
{
uint32_t hdr_ver = get_header_version(addr);
switch (hdr_ver) {
case 0:
case 1:
case 2:
*(uint32_t *)(addr + OS_VERSION_OFFSET_V2) = v.version;
break;
case 3:
*(uint32_t *)(addr + OS_VERSION_OFFSET_V3) = v.version;
break;
default:
errx(1, "Unsupported header version %u", hdr_ver);
break;
}
}
static uint8_t *mmap_boot_image(const char *file, int flags)
{
uint8_t *addr;
uint32_t header_version;
struct stat st;
int mmap_flags = PROT_READ;
size_t block_size;
int fd = open(file, flags);
stop(fd < 0, "open %s failed", file);
if (flags == O_RDWR)
mmap_flags |= PROT_WRITE;
stop(fstat(fd, &st) < 0, "stat %s failed", file);
stopx(!(S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) &&
st.st_size < BOOT_IMAGE_HEADER_V2_SIZE,
"%s is too small", file);
stopx(S_ISCHR(st.st_mode), "%s is not a block device", file);
if (S_ISBLK(st.st_mode)) {
stop(ioctl(fd, BLKGETSIZE64, &block_size) < 0,
"%s can't determine block device size", file);
stopx(block_size < BOOT_IMAGE_HEADER_V2_SIZE,
"%s is too small", file);
}
addr = mmap(NULL, BOOT_IMAGE_HEADER_V2_SIZE, mmap_flags,
MAP_SHARED, fd, 0);
stop(addr == MAP_FAILED, "mmap %s failed", file);
close(fd);
stopx(strncmp((const char *)addr, "ANDROID!", 8),
"%s has incorrect magic number, not an android boot image",
file
);
header_version = get_header_version(addr);
stopx(header_version > 3,
"%s unsupported header version (%u)",
file, header_version
);
return addr;
}
int main(int argc, char *argv[])
{
uint8_t *addr;
const char *file, *os_version, *os_patch_level;
char *delim = NULL;
int year, month;
int a, b, c;
os_version_t curv, newv;
bool preserve_os_version = false;
bool preserve_os_patch_level = false;
stopx(argc != 4,
"Usage: %s <file> <os_version|same> <os_patch_level|same>",
argv[0]);
file = argv[1];
os_version = argv[2];
os_patch_level = argv[3];
if (!strcmp(os_version, "same")) {
preserve_os_version = true;
} else {
// Format: a.b.c
for (const char *p = os_version; *p != '\0'; ++p) {
stopx(!(isdigit(*p) || *p == '.'),
"Incorrect os_version '%s'. Format: a.b.c",
os_version);
}
a = strtol(os_version, &delim, 10);
stopx(*delim != '.',
"Incorrect os_version '%s'. Format: a.b.c", os_version);
b = strtol(delim + 1, &delim, 10);
stopx(*delim != '.',
"Incorrect os_version '%s'. Format: a.b.c", os_version);
c = strtol(delim + 1, NULL, 10);
stopx(!(0 <= a && a <= 127 &&
0 <= b && b <= 127 &&
0 <= c && c <= 127),
"Incorrect os_version '%s'. Format: a.b.c", os_version);
}
if (!strcmp(os_patch_level, "same")) {
preserve_os_patch_level = true;
} else {
// Format: YYYY-MM
stopx(strlen(os_patch_level) != 7 ||
os_patch_level[4] != '-' ||
!isdigit(os_patch_level[0]) ||
!isdigit(os_patch_level[1]) ||
!isdigit(os_patch_level[2]) ||
!isdigit(os_patch_level[3]) ||
!isdigit(os_patch_level[5]) ||
!isdigit(os_patch_level[6]),
"Incorrent os_patch_level '%s'. Format: YYYY-MM",
os_patch_level);
year = atoi(os_patch_level);
month = atoi(os_patch_level + 5);
stopx(!(2000 <= year && year <= 2127),
"Incorrect year: %ld (2000 <= year <= 2127)", year);
stopx(!(1 <= month && month <= 12),
"Incorrect month: %ld (01 <= month <= 12)", month);
}
addr = mmap_boot_image(file, O_RDWR);
curv = get_os_version(addr);
printf("Current OS version:\t%u.%u.%u %u-%02u\n",
curv.a, curv.b, curv.c,
curv.year + 2000, curv.month);
if (preserve_os_version) {
newv.os_version = curv.os_version;
} else {
newv.a = a;
newv.b = b;
newv.c = c;
}
if (preserve_os_patch_level) {
newv.os_patch_level = curv.os_patch_level;
} else {
newv.year = (year - 2000);
newv.month = month;
}
if (curv.version != newv.version) {
printf("New OS version:\t\t%u.%u.%u %u-%02u\n",
newv.a, newv.b, newv.c,
newv.year + 2000, newv.month);
checkx(curv.os_version > newv.os_version,
"warn: new os_version is lower than current"
);
checkx(curv.os_patch_level > newv.os_patch_level,
"warn: new os_patch_level version is lower than current"
);
set_os_version(addr, newv);
check(msync(addr, BOOT_IMAGE_HEADER_V2_SIZE, MS_SYNC) < 0,
"msync failed");
} else {
printf("The dates are the same. Nothing to be done.\n");
}
munmap(addr, BOOT_IMAGE_HEADER_V2_SIZE);
return 0;
}