forked from CyanogenMod/android_bootable_recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextendedcommands.c
388 lines (336 loc) · 9.74 KB
/
extendedcommands.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
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <linux/input.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/reboot.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/limits.h>
#include <dirent.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/wait.h>
#include "common.h"
#include "cutils/properties.h"
#include "make_ext4fs.h"
#include "minui/minui.h"
#include "roots.h"
#include "recovery_ui.h"
#include "extendedcommands.h"
#include "mounts.h"
#include "flashutils/flashutils.h"
#include <libgen.h>
#include "mtdutils/mtdutils.h"
#include "bmlutils/bmlutils.h"
#include "cutils/android_reboot.h"
int
get_filtered_menu_selection(char** headers, char** items, int menu_only, int initial_selection, int items_count) {
int index;
int offset = 0;
int* translate_table = (int*)malloc(sizeof(int) * items_count);
for (index = 0; index < items_count; index++) {
if (items[index] == NULL)
continue;
char *item = items[index];
items[index] = NULL;
items[offset] = item;
translate_table[offset] = index;
offset++;
}
items[offset] = NULL;
initial_selection = translate_table[initial_selection];
int ret = get_menu_selection(headers, items, menu_only, initial_selection);
if (ret < 0 || ret >= offset) {
free(translate_table);
return ret;
}
ret = translate_table[ret];
free(translate_table);
return ret;
}
void write_string_to_file(const char* filename, const char* string) {
ensure_path_mounted(filename);
char tmp[PATH_MAX];
sprintf(tmp, "mkdir -p $(dirname %s)", filename);
__system(tmp);
FILE *file = fopen(filename, "w");
fprintf(file, "%s", string);
fclose(file);
}
void free_string_array(char** array)
{
if (array == NULL)
return;
char* cursor = array[0];
int i = 0;
while (cursor != NULL)
{
free(cursor);
cursor = array[++i];
}
free(array);
}
char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
{
char path[PATH_MAX] = "";
DIR *dir;
struct dirent *de;
int total = 0;
int i;
char** files = NULL;
int pass;
*numFiles = 0;
int dirLen = strlen(directory);
dir = opendir(directory);
if (dir == NULL) {
ui_print("Couldn't open directory.\n");
return NULL;
}
int extension_length = 0;
if (fileExtensionOrDirectory != NULL)
extension_length = strlen(fileExtensionOrDirectory);
int isCounting = 1;
i = 0;
for (pass = 0; pass < 2; pass++) {
while ((de=readdir(dir)) != NULL) {
// skip hidden files
if (de->d_name[0] == '.')
continue;
// NULL means that we are gathering directories, so skip this
if (fileExtensionOrDirectory != NULL)
{
// make sure that we can have the desired extension (prevent seg fault)
if (strlen(de->d_name) < extension_length)
continue;
// compare the extension
if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
continue;
}
else
{
struct stat info;
char fullFileName[PATH_MAX];
strcpy(fullFileName, directory);
strcat(fullFileName, de->d_name);
stat(fullFileName, &info);
// make sure it is a directory
if (!(S_ISDIR(info.st_mode)))
continue;
}
if (pass == 0)
{
total++;
continue;
}
files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
strcpy(files[i], directory);
strcat(files[i], de->d_name);
if (fileExtensionOrDirectory == NULL)
strcat(files[i], "/");
i++;
}
if (pass == 1)
break;
if (total == 0)
break;
rewinddir(dir);
*numFiles = total;
files = (char**) malloc((total+1)*sizeof(char*));
files[total]=NULL;
}
if (total==0) {
return NULL;
}
// sort the result
if (files != NULL) {
for (i = 0; i < total; i++) {
int curMax = -1;
int j;
for (j = 0; j < total - i; j++) {
if (curMax == -1 || strcmp(files[curMax], files[j]) < 0)
curMax = j;
}
char* temp = files[curMax];
files[curMax] = files[total - i - 1];
files[total - i - 1] = temp;
}
}
return files;
}
#define STRINGIFY(s) #s
extern struct selabel_handle *sehandle;
typedef struct {
char mount[255];
char unmount[255];
Volume* v;
} MountMenuEntry;
typedef struct {
char txt[255];
Volume* v;
} FormatMenuEntry;
void write_fstab_root(char *path, FILE *file)
{
Volume *vol = volume_for_path(path);
if (vol == NULL) {
return;
}
char device[200];
if (vol->device[0] != '/')
get_partition_device(vol->device, device);
else
strcpy(device, vol->device);
fprintf(file, "%s ", device);
fprintf(file, "%s ", path);
// special case rfs cause auto will mount it as vfat on samsung.
fprintf(file, "%s rw\n", vol->fs_type2 != NULL && strcmp(vol->fs_type, "rfs") != 0 ? "auto" : vol->fs_type);
}
void create_fstab()
{
struct stat info;
__system("touch /etc/mtab");
FILE *file = fopen("/etc/fstab", "w");
if (file == NULL) {
return;
}
Volume *vol = volume_for_path("/boot");
if (NULL != vol && strcmp(vol->fs_type, "mtd") != 0 && strcmp(vol->fs_type, "emmc") != 0 && strcmp(vol->fs_type, "bml") != 0)
write_fstab_root("/boot", file);
write_fstab_root("/cache", file);
write_fstab_root("/data", file);
write_fstab_root("/datadata", file);
write_fstab_root("/emmc", file);
write_fstab_root("/system", file);
write_fstab_root("/sdcard", file);
write_fstab_root("/sd-ext", file);
fclose(file);
}
int bml_check_volume(const char *path) {
ui_print("Checking %s...\n", path);
ensure_path_unmounted(path);
if (0 == ensure_path_mounted(path)) {
ensure_path_unmounted(path);
return 0;
}
Volume *vol = volume_for_path(path);
if (vol == NULL) {
return 0;
}
ui_print("%s may be rfs. Checking...\n", path);
char tmp[PATH_MAX];
sprintf(tmp, "mount -t rfs %s %s", vol->device, path);
int ret = __system(tmp);
printf("%d\n", ret);
return ret == 0 ? 1 : 0;
}
void process_volumes() {
create_fstab();
if (is_data_media()) {
setup_data_media();
}
return;
// dead code.
if (device_flash_type() != BML)
return;
ui_print("Checking for ext4 partitions...\n");
int ret = 0;
ret = bml_check_volume("/system");
ret |= bml_check_volume("/data");
if (has_datadata())
ret |= bml_check_volume("/datadata");
ret |= bml_check_volume("/cache");
if (ret == 0) {
ui_print("Done!\n");
return;
}
char backup_path[PATH_MAX];
time_t t = time(NULL);
char backup_name[PATH_MAX];
struct timeval tp;
gettimeofday(&tp, NULL);
sprintf(backup_name, "before-ext4-convert-%d", tp.tv_sec);
sprintf(backup_path, "/sdcard/clockworkmod/backup/%s", backup_name);
ui_set_show_text(1);
ui_print("Filesystems need to be converted to ext4.\n");
ui_print("A backup and restore will now take place.\n");
ui_print("If anything goes wrong, your backup will be\n");
ui_print("named %s. Try restoring it\n", backup_name);
ui_print("in case of error.\n");
ui_set_show_text(0);
}
void handle_failure(int ret)
{
if (ret == 0)
return;
if (0 != ensure_path_mounted("/sdcard"))
return;
mkdir("/sdcard/clockworkmod", S_IRWXU | S_IRWXG | S_IRWXO);
__system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
ui_print("/tmp/recovery.log was copied to /sdcard/clockworkmod/recovery.log. Please open ROM Manager to report the issue.\n");
}
int is_path_mounted(const char* path) {
Volume* v = volume_for_path(path);
if (v == NULL) {
return 0;
}
if (strcmp(v->fs_type, "ramdisk") == 0) {
// the ramdisk is always mounted.
return 1;
}
int result;
result = scan_mounted_volumes();
if (result < 0) {
return 0;
}
const MountedVolume* mv =
find_mounted_volume_by_mount_point(v->mount_point);
if (mv) {
// volume is already mounted
return 1;
}
return 0;
}
int has_datadata() {
Volume *vol = volume_for_path("/datadata");
return vol != NULL;
}
int volume_main(int argc, char **argv) {
load_volume_table();
return 0;
}
int verify_root_and_recovery() {
if (ensure_path_mounted("/system") != 0)
return 0;
int ret = 0;
struct stat st;
if (0 == lstat("/system/etc/install-recovery.sh", &st)) {
if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
ui_show_text(1);
ret = 1;
}
}
if (0 == lstat("/system/bin/su", &st)) {
if (S_ISREG(st.st_mode)) {
if ((st.st_mode & (S_ISUID | S_ISGID)) != (S_ISUID | S_ISGID)) {
ui_show_text(1);
ret = 1;
}
}
}
if (0 == lstat("/system/xbin/su", &st)) {
if (S_ISREG(st.st_mode)) {
if ((st.st_mode & (S_ISUID | S_ISGID)) != (S_ISUID | S_ISGID)) {
ui_show_text(1);
ret = 1;
}
}
}
ensure_path_unmounted("/system");
return ret;
}