forked from raidzero/RZrecovery
/
recovery_lib.c
executable file
·423 lines (373 loc) · 12.2 KB
/
recovery_lib.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <linux/input.h>
#include <limits.h>
#include <sys/wait.h>
#include <limits.h>
#include "common.h"
#include "roots.h"
#include "bootloader.h"
#include "minui/minui.h"
#include "recovery_ui.h"
#include "minzip/DirUtil.h"
static const char *INTENT_FILE = "CACHE:recovery/intent";
static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
static const char *COMMAND_FILE = "CACHE:recovery/command";
static const char *LOG_FILE = "CACHE:recovery/log";
// clear the recovery command and prepare to boot a (hopefully working) system,
// copy our log file to cache as well (for the system to read), and
// record any intent we were asked to communicate back to the system.
// this function is idempotent: call it as many times as you like.
// close a file, log an error if the error indicator is set
void check_and_fclose(FILE *fp, const char *name) {
fflush(fp);
if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
fclose(fp);
}
// open a file given in root:path format, mounting partitions as necessary
FILE* fopen_root_path(const char *root_path, const char *mode) {
if (ensure_root_path_mounted(root_path) != 0) {
LOGE("Can't mount %s\n", root_path);
return NULL;
}
char path[PATH_MAX] = "";
if (translate_root_path(root_path, path, sizeof(path)) == NULL) {
LOGE("Bad path %s\n", root_path);
return NULL;
}
// When writing, try to create the containing directory, if necessary.
// Use generous permissions, the system (init.rc) will reset them.
if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1);
FILE *fp = fopen(path, mode);
// if (fp == NULL) LOGE("Can't open %s\n", path);
return fp;
}
void finish_recovery(const char *send_intent)
{
// By this point, we're ready to return to the main system...
if (send_intent != NULL) {
FILE *fp = fopen_root_path(INTENT_FILE, "w");
if (fp != NULL) {
fputs(send_intent, fp);
check_and_fclose(fp, INTENT_FILE);
}
}
// Copy logs to cache so the system can find out what happened.
FILE *log = fopen_root_path(LOG_FILE, "a");
if (log != NULL) {
FILE *tmplog = fopen(TEMPORARY_LOG_FILE, "r");
if (tmplog == NULL) {
LOGE("Can't open %s\n", TEMPORARY_LOG_FILE);
} else {
static long tmplog_offset = 0;
fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
char buf[4096];
while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
tmplog_offset = ftell(tmplog);
check_and_fclose(tmplog, TEMPORARY_LOG_FILE);
}
check_and_fclose(log, LOG_FILE);
}
// Reset the bootloader message to revert to a normal main system boot.
struct bootloader_message boot;
memset(&boot, 0, sizeof(boot));
set_bootloader_message(&boot);
// Remove the command file, so recovery won't repeat indefinitely.
char path[PATH_MAX] = "";
if (ensure_root_path_mounted(COMMAND_FILE) != 0 ||
translate_root_path(COMMAND_FILE, path, sizeof(path)) == NULL ||
(unlink(path) && errno != ENOENT)) {
LOGW("Can't unlink %s\n", COMMAND_FILE);
}
sync(); // For good measure.
}
char** prepend_title(char** headers) {
FILE* f = fopen("/recovery.version","r");
char* vers = calloc(8,sizeof(char));
fgets(vers, 8, f);
strtok(vers," "); // truncate vers to before the first space
char* patch_line_ptr = calloc((strlen(headers[0])+11),sizeof(char));
char* patch_line = patch_line_ptr;
strcpy(patch_line,headers[0]);
strcat(patch_line," (");
strcat(patch_line,vers);
strcat(patch_line,")");
int c = 0;
char ** p1;
for (p1 = headers; *p1; ++p1, ++c);
char** ver_headers = calloc((c+1),sizeof(char*));
ver_headers[0]=patch_line;
ver_headers[c]=NULL;
char** v = ver_headers+1;
for (p1 = headers+1; *p1; ++p1, ++v) *v = *p1;
char* title[] = { "Android system recovery <"
EXPAND(RECOVERY_API_VERSION) "e>",
"",
NULL };
// count the number of lines in our title, plus the
// caller-provided headers.
int count = 0;
char** p;
for (p = title; *p; ++p, ++count);
for (p = ver_headers; *p; ++p, ++count);
char** new_headers = calloc((count+1),sizeof(char*));
char** h = new_headers;
for (p = title; *p; ++p, ++h) *h = *p;
for (p = ver_headers; *p; ++p, ++h) *h = *p;
*h = NULL;
return new_headers;
}
void set_sdcard_update_bootloader_message()
{
struct bootloader_message boot;
memset(&boot, 0, sizeof(boot));
strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
set_bootloader_message(&boot);
}
int erase_root(const char *root)
{
ui_set_background(BACKGROUND_ICON_ERROR);
ui_show_indeterminate_progress();
ui_print("Formatting %s...\n", root);
return format_root_device(root);
}
int get_menu_selection(char** headers, char** items, int menu_only, int selected) {
// throw away keys pressed previously, so user doesn't
// accidentally trigger menu items.
ui_clear_key_queue();
ui_start_menu(headers, items,selected);
int chosen_item = -1;
while (chosen_item < 0) {
int key = ui_wait_key();
/* char* key_str = calloc(18,sizeof(char));
sprintf(key_str, "Key %d pressed.\n", key);
ui_print(key_str);*/
if (key == KEY_BACKSPACE || key == KEY_END) {
return(ITEM_BACK);
}
int visible = ui_text_visible();
int action = device_handle_key(key, visible);
if (action < 0) {
switch (action) {
case ITEM_BACK:
return(ITEM_BACK);
break;
case HIGHLIGHT_UP:
--selected;
selected = ui_menu_select(selected);
break;
case HIGHLIGHT_DOWN:
++selected;
selected = ui_menu_select(selected);
break;
case SELECT_ITEM:
chosen_item = selected;
break;
case NO_ACTION:
break;
}
} else if (!menu_only) {
chosen_item = action;
}
}
ui_end_menu();
return chosen_item;
}
void wipe_data(int confirm) {
if (confirm) {
static char** title_headers = NULL;
if (title_headers == NULL) {
char* headers[] = { "Confirm wipe of all user data?",
" THIS CAN NOT BE UNDONE.",
"",
NULL };
title_headers = prepend_title(headers);
}
char* items[] = { " No",
" No",
" No",
" No",
" No",
" No",
" No",
" Yes -- delete all user data", // [7]
" No",
" No",
" No",
NULL };
int chosen_item = get_menu_selection(title_headers, items, 1, 0);
if (chosen_item != 7) {
return;
}
}
ui_print("\n-- Wiping data...\n");
device_wipe_data();
erase_root("DATA:");
erase_root("CACHE:");
ui_print("Data wipe complete.\n");
}
void ui_printf_int(const char* format, int arg)
{
char* out = calloc(strlen(format)+12,sizeof(char));
sprintf(out,format,arg);
ui_print(out);
free(out);
}
void get_check_menu_opts(char** items, char** chk_items, int* flags)
{
int i;
int count;
for (count=0;chk_items[count];count++); // count it
for (i=0; i<count; i++) {
if (items[i]!=NULL) free(items[i]);
items[i]=calloc(strlen(chk_items[i])+5,sizeof(char)); // 4 for "(*) " and 1 for the NULL-terminator
sprintf(items[i],"(%s) %s", (*flags&(1<<i))?"*":" " , chk_items[i]);
}
}
void show_check_menu(char** headers, char** chk_items, int* flags)
{
int chosen_item = -1;
int i;
for (i=0;chk_items[i];i++); // count it
i+=2; // i is the index of NULL in chk_items[], and we want an array 1 larger than chk_items, so add 1 for that and 1 for the NULL at the end => i+2
char** items = calloc(i,sizeof(char*));
items[0]="Finished";
items[i]=NULL;
while(chosen_item!=0) {
get_check_menu_opts(items+1,chk_items,flags); // items+1 so we don't overwrite the Finished option
chosen_item = get_menu_selection(headers,items,0,chosen_item==-1?0:chosen_item);
if (chosen_item==0) {continue;}
chosen_item-=1; // make it 0-indexed for bit-flipping the int
*flags^=(1<<chosen_item); // flip the bit corresponding to the chosen item
chosen_item+=1; // don't ruin the loop!
}
}
int runve(char* filename, char** argv, char** envp, int secs)
{
int opipe[2];
int ipipe[2];
pipe(opipe);
pipe(ipipe);
pid_t pid = fork();
int status = 0;
if (pid == 0) {
dup2(opipe[1],1);
dup2(ipipe[0],0);
close(opipe[0]);
close(ipipe[1]);
execve(filename,argv,envp);
char* error_msg = calloc(strlen(filename)+20,sizeof(char));
sprintf(error_msg,"Could not execute %s\n",filename);
ui_print(error_msg);
free(error_msg);
return(9999);
}
close(opipe[1]);
close(ipipe[0]);
FILE* from = fdopen(opipe[0],"r");
FILE* to = fdopen(ipipe[1],"w");
char* cur_line = calloc(100,sizeof(char));
char* tok;
int total_lines;
int cur_lines;
int num_items;
int num_headers;
int num_chks;
float cur_progress;
char** items = NULL;
char** headers = NULL;
char** chks = NULL;
int i = 0; // iterator for menu items
int j = 0; // iterator for menu headers
int k = 0; // iterator for check menu items
int l = 0; // iterator for outputting flags from check menu
int flags = INT_MAX;
int choice;
while (fgets(cur_line,100,from)!=NULL) {
printf(cur_line);
tok=strtok(cur_line," \n");
if(tok==NULL) {continue;}
if(strcmp(tok,"*")==0) {
tok=strtok(NULL," \n");
if(tok==NULL) {continue;}
if(strcmp(tok,"ptotal")==0) {
ui_set_progress(0.0);
ui_show_progress(1.0,0);
total_lines=atoi(strtok(NULL," "));
} else if (strcmp(tok,"print")==0) {
ui_print(strtok(NULL,""));
} else if (strcmp(tok,"items")==0) {
num_items=atoi(strtok(NULL," \n"));
if(items!=NULL) free(items);
items=calloc((num_items+1),sizeof(char*));
items[num_items]=NULL;
i=0;
} else if (strcmp(tok,"item")==0) {
if (i < num_items) {
tok=strtok(NULL,"\n");
items[i]=calloc((strlen(tok)+1),sizeof(char));
strcpy(items[i],tok);
i++;
}
} else if (strcmp(tok,"headers")==0) {
num_headers=atoi(strtok(NULL," \n"));
if(headers!=NULL) free(headers);
headers=calloc((num_headers+1),sizeof(char*));
headers[num_headers]=NULL;
j=0;
} else if (strcmp(tok,"header")==0) {
if (j < num_headers) {
tok=strtok(NULL,"\n");
if (tok) {
headers[j]=calloc((strlen(tok)+1),sizeof(char));
strcpy(headers[j],tok);
} else {
headers[j]="";
}
j++;
}
} else if (strcmp(tok,"show_menu")==0) {
choice=get_menu_selection(headers,items,0,0);
fprintf(to, "%d\n", choice);
fflush(to);
} else if (strcmp(tok,"pcur")==0) {
cur_lines=atoi(strtok(NULL,"\n"));
if (cur_lines%10==0 || total_lines-cur_lines<10) {
cur_progress=(float)cur_lines/((float)total_lines);
ui_set_progress(cur_progress);
}
if (cur_lines==total_lines) ui_reset_progress();
} else if (strcmp(tok,"check_items")==0) {
num_chks=atoi(strtok(NULL," \n"));
if(chks!=NULL) free(chks);
chks=calloc((num_chks+1),sizeof(char*));
chks[num_chks]=NULL;
k = 0;
} else if (strcmp(tok,"check_item")==0) {
if (k < num_chks) {
tok=strtok(NULL,"\n");
chks[k]=calloc(strlen(tok)+1,sizeof(char));
strcpy(chks[k],tok);
k++;
}
} else if (strcmp(tok,"show_check_menu")==0) {
show_check_menu(headers,chks,&flags);
for(l=0;l<num_chks;l++) {
fprintf(to, "%d\n", !!(flags&(1<<l)));
}
fflush(to);
} else if (strcmp(tok,"show_indeterminate_progress")==0) {
ui_show_indeterminate_progress();
} else {ui_print("unrecognized command "); ui_print(tok); ui_print("\n");}
}
}
while (waitpid(pid, &status, WNOHANG) == 0) {
sleep(1);
}
ui_print("\n");
free(cur_line);
return status;
}