forked from Havoc-OS/android_bootable_recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bu.cpp
326 lines (269 loc) · 7.17 KB
/
bu.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
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
/*
* Copyright (C) 2019 The LineageOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <unistd.h>
#include <cutils/log.h>
#include <cutils/properties.h>
#include <selinux/label.h>
#include <fs_mgr.h>
#include "roots.h"
#include "bu.h"
#define PATHNAME_RC "/tmp/burc"
using namespace std;
using namespace android;
struct selabel_handle* sehandle;
int adb_ifd;
int adb_ofd;
TAR* tar;
gzFile gzf;
char* hash_name;
size_t hash_datalen;
SHA_CTX sha_ctx;
MD5_CTX md5_ctx;
void ui_print(const char* format, ...) {
char buffer[256];
va_list ap;
va_start(ap, format);
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
fputs(buffer, stdout);
}
void logmsg(const char* fmt, ...) {
char msg[1024];
FILE* fp;
va_list ap;
va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
fp = fopen("/tmp/bu.log", "a");
if (fp) {
fprintf(fp, "[%d] %s", getpid(), msg);
fclose(fp);
}
}
static partspec partlist[MAX_PART];
static partspec* curpart;
int part_add(const char* name) {
Volume* vol = NULL;
char* path = NULL;
int i;
path = (char*)malloc(1 + strlen(name) + 1);
sprintf(path, "/%s", name);
vol = volume_for_mount_point(path);
if (vol == NULL || vol->fs_type == NULL) {
logmsg("missing vol info for %s, ignoring\n", name);
goto err;
}
for (i = 0; i < MAX_PART; ++i) {
if (partlist[i].name == NULL) {
partlist[i].name = strdup(name);
partlist[i].path = path;
partlist[i].vol = vol;
logmsg("part_add: i=%d, name=%s, path=%s\n", i, name, path);
return 0;
}
if (strcmp(partlist[i].name, name) == 0) {
logmsg("duplicate partition %s, ignoring\n", name);
goto err;
}
}
err:
free(path);
return -1;
}
partspec* part_get(int i) {
if (i >= 0 && i < MAX_PART) {
if (partlist[i].name != NULL) {
return &partlist[i];
}
}
return NULL;
}
partspec* part_find(const char* name) {
for (int i = 0; i < MAX_PART; ++i) {
if (partlist[i].name && !strcmp(name, partlist[i].name)) {
return &partlist[i];
}
}
return NULL;
}
void part_set(partspec* part) {
curpart = part;
curpart->off = 0;
}
int update_progress(uint64_t off) {
static time_t last_time = 0;
static int last_pct = 0;
if (curpart) {
curpart->off += off;
time_t now = time(NULL);
int pct = min(100, (int)((uint64_t)100 * curpart->off / curpart->used));
if (now != last_time && pct != last_pct) {
char msg[256];
sprintf(msg, "%s: %d%% complete", curpart->name, pct);
ui_print(msg);
last_time = now;
last_pct = pct;
}
}
return 0;
}
static int tar_cb_open(const char* path, int mode, ...) {
errno = EINVAL;
return -1;
}
static int tar_cb_close(int fd) {
return 0;
}
static ssize_t tar_cb_read(int fd, void* buf, size_t len) {
ssize_t nread;
nread = ::read(fd, buf, len);
if (nread > 0 && hash_name) {
SHA1_Update(&sha_ctx, (u_char*)buf, nread);
MD5_Update(&md5_ctx, buf, nread);
hash_datalen += nread;
}
update_progress(nread);
return nread;
}
static ssize_t tar_cb_write(int fd, const void* buf, size_t len) {
ssize_t written = 0;
if (hash_name) {
SHA1_Update(&sha_ctx, (u_char*)buf, len);
MD5_Update(&md5_ctx, buf, len);
hash_datalen += len;
}
while (len > 0) {
ssize_t n = ::write(fd, buf, len);
if (n < 0) {
logmsg("tar_cb_write: error: n=%d\n", n);
return n;
}
if (n == 0) break;
buf = (const char*)buf + n;
len -= n;
written += n;
}
update_progress(written);
return written;
}
static tartype_t tar_io = { tar_cb_open, tar_cb_close, tar_cb_read, tar_cb_write };
static ssize_t tar_gz_cb_read(int fd, void* buf, size_t len) {
int nread;
nread = gzread(gzf, buf, len);
if (nread > 0 && hash_name) {
SHA1_Update(&sha_ctx, (u_char*)buf, nread);
MD5_Update(&md5_ctx, buf, nread);
hash_datalen += nread;
}
update_progress(nread);
return nread;
}
static ssize_t tar_gz_cb_write(int fd, const void* buf, size_t len) {
ssize_t written = 0;
if (hash_name) {
SHA1_Update(&sha_ctx, (u_char*)buf, len);
MD5_Update(&md5_ctx, buf, len);
hash_datalen += len;
}
while (len > 0) {
ssize_t n = gzwrite(gzf, buf, len);
if (n < 0) {
logmsg("tar_gz_cb_write: error: n=%d\n", n);
return n;
}
if (n == 0) break;
buf = (const char*)buf + n;
len -= n;
written += n;
}
update_progress(written);
return written;
}
static tartype_t tar_io_gz = { tar_cb_open, tar_cb_close, tar_gz_cb_read, tar_gz_cb_write };
int create_tar(int fd, const char* compress, const char* mode) {
int rc = -1;
SHA1_Init(&sha_ctx);
MD5_Init(&md5_ctx);
if (!compress || strcasecmp(compress, "none") == 0) {
rc = tar_fdopen(&tar, fd, "foobar", &tar_io, 0, /* oflags: unused */
0, /* mode: unused */
TAR_GNU | TAR_STORE_SELINUX /* options */);
} else if (strcasecmp(compress, "gzip") == 0) {
gzf = gzdopen(fd, mode);
if (gzf != NULL) {
rc = tar_fdopen(&tar, 0, "foobar", &tar_io_gz, 0, /* oflags: unused */
0, /* mode: unused */
TAR_GNU | TAR_STORE_SELINUX /* options */);
}
}
return rc;
}
static void do_exit(int rc) {
char rcstr[80];
int len;
len = sprintf(rcstr, "%d\n", rc);
unlink(PATHNAME_RC);
int fd = open(PATHNAME_RC, O_RDWR | O_CREAT, 0644);
write(fd, rcstr, len);
close(fd);
exit(rc);
}
int main(int argc, char** argv) {
int rc = 1;
const char* logfile = "/tmp/recovery.log";
adb_ifd = dup(STDIN_FILENO);
adb_ofd = dup(STDOUT_FILENO);
freopen(logfile, "a", stdout);
setbuf(stdout, NULL);
freopen(logfile, "a", stderr);
setbuf(stderr, NULL);
logmsg("bu: invoked with %d args\n", argc);
if (argc < 2) {
logmsg("Not enough args (%d)\n", argc);
do_exit(1);
}
// progname args...
int optidx = 1;
const char* opname = argv[optidx++];
struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, "/file_contexts" } };
sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
load_volume_table();
if (!strcmp(opname, "backup")) {
ui_print("Backup in progress...");
rc = do_backup(argc - optidx, &argv[optidx]);
} else if (!strcmp(opname, "restore")) {
ui_print("Restore in progress...");
rc = do_restore(argc - optidx, &argv[optidx]);
} else {
logmsg("Unknown operation %s\n", opname);
rc = 1;
}
close(adb_ofd);
close(adb_ifd);
sleep(1);
logmsg("bu exiting\n");
do_exit(rc);
return rc;
}