forked from raidzero/RZrecovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount_menu.c
360 lines (311 loc) · 8.23 KB
/
mount_menu.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
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include "recovery.h"
#include "roots.h"
#include "recovery_ui.h"
#ifndef BOARD_UMS_LUNFILE
#define BOARD_UMS_LUNFILE "/sys/devices/platform/usb_mass_storage/lun0/file"
#endif
int num_volumes;
int is_path_mountable(char* path)
{
/*
this function will return:
-1 if volume does not exist/not mountable,
3 for yaffs/bml
2 if it is not mtd, is vfat or ext
1 if it is not mtd, is vfat
0 if it is not mtd
If just looking for path mountable, check for ! -1
for USB on windows, check for 1
for USB on linux, check for 2
*/
Volume* v = volume_for_path(path);
if (v == NULL)
{
return -1;
}
//tmp should not be included, its always mounted
if (strcmp(v->mount_point,"/tmp") ==0)
{
return -1;
}
//vfat only
else if (strcmp(v->fs_type, "vfat") == 0 && strcmp(v->fs_type, "ext2") != 0 && strcmp(v->fs_type, "ext3") != 0 &&strcmp(v->fs_type, "ext4") != 0)
{
return 1;
}
//vfat or ext
else if (strcmp(v->fs_type, "vfat") == 0 || strcmp(v->fs_type, "ext2") == 0 || strcmp(v->fs_type, "ext3") == 0 || strcmp(v->fs_type, "ext4") == 0)
{
return 2;
}
//mtd
if (strcmp(v->fs_type, "mtd") == 0)
{
return -1;
}
//yaffs/bml
else if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "bml") ==0)
{
return 3;
}
else if (strcmp(v->fs_type, "mtd") == 0 && strcmp(v->fs_type, "yaffs2") == 0 && strcmp(v->fs_type, "bml") ==0)
{
return 0;
}
return 0;
}
static int
is_usb_storage_enabled ()
{
if (access(BOARD_UMS_LUNFILE,F_OK) != -1)
{
printf("LUN file present.\n");
FILE *fp = fopen (BOARD_UMS_LUNFILE, "r");
char *buf = malloc (11 * sizeof (char));
fgets (buf, 11, fp);
fclose (fp);
if (strcmp (buf, "/dev/block") == 0)
{
return (1);
}
else
{
return (0);
}
}
else
{
printf("USB Mass storage will not work.\n");
return (3);
}
}
void show_usb_menu()
{
char *mode = calloc(5, sizeof(char)+1);
if (access("/tmp/.rzrpref_usb", F_OK) != -1)
{
FILE* fp = fopen("/tmp/.rzrpref_usb", "r");
fgets(mode, 5, fp);
fclose(fp);
}
else mode = "fat";
int ext;
if (strcmp(mode,"ext") == 0) ext = 1;
else ext = 0;
printf("ext UMS enabled: %i\n", ext);
int num_volumes = get_num_volumes();
Volume* device_volumes;
device_volumes = get_device_volumes();
int i;
int valid = 0;
char** usb_volumes = malloc (num_volumes * sizeof (char *));
int mountable_volumes = 0;
for (i=0; i<num_volumes; i++)
{
usb_volumes[i] = "";
Volume *v = &device_volumes[i];
usb_volumes[i] = malloc(strlen(v->mount_point)+2);
valid = 0;
if (is_path_mountable(v->mount_point) != -1)
{
if (strcmp(v->fs_type, "vfat") == 0)
{
printf("%s is windows-mountable.\n", v->mount_point);
valid = 1;
}
if (ext && (strstr(v->fs_type, "ext")))
{
printf("%s is linux-mountable.\n", v->mount_point);
valid = 1;
}
if (valid)
{
printf("usb_volumes[%d]: %s\n", mountable_volumes, v->mount_point);
sprintf(usb_volumes[mountable_volumes], "%s", v->mount_point);
mountable_volumes++;
}
}
}
char* headers[] = { "Choose a USB",
" storage device",
"",
NULL
};
char** items = malloc (mountable_volumes * sizeof (char *));
int z;
for (z=0; z<mountable_volumes; z++)
{
items[z] = usb_volumes[z];
printf("items[%d]: %s\n", z, items[z]);
items[z+1] = NULL;
}
int chosen_item = -1;
while (chosen_item != ITEM_BACK)
{
chosen_item = get_menu_selection (headers, items, 0, chosen_item < 0 ? 0 : chosen_item);
char* selection = usb_volumes[chosen_item];
switch(chosen_item)
{
case ITEM_BACK:
return;
default:
show_usb_storage_enabled_screen(selection);
return;
}
return;
}
}
void show_usb_storage_enabled_screen(char* selection)
{
enable_usb_mass_storage(selection);
char *headers[] = { "",
"is in USB mass storage mode",
"Go back to disable",
"",
NULL
};
char* items[] = { "Disable", NULL };
#define ITEM_DISABLE 0
headers[0] = selection;
int chosen_item = -1;
while (chosen_item != ITEM_BACK)
{
chosen_item = get_menu_selection (headers, items, 0, chosen_item < 0 ? 0 : chosen_item);
switch (chosen_item)
{
case ITEM_DISABLE:
disable_usb_mass_storage();
return;
}
disable_usb_mass_storage();
return;
}
}
void
enable_usb_mass_storage (char* volume)
{
int fd;
Volume *vol = volume_for_path (volume);
if ((fd = open (BOARD_UMS_LUNFILE, O_WRONLY)) < 0)
{
LOGE ("\nUnable to open ums lunfile (%s)", strerror (errno));
return -1;
}
if ((write (fd, vol->device, strlen (vol->device)) < 0) &&
(!vol->device2 || (write (fd, vol->device, strlen (vol->device2)) < 0)))
{
LOGE ("\nUnable to write to ums lunfile (%s)", strerror (errno));
close (fd);
return -1;
}
}
void
disable_usb_mass_storage ()
{
printf("Disabling USB Mass storage...\n");
if (access(BOARD_UMS_LUNFILE, F_OK) != -1)
{
FILE *fp = fopen (BOARD_UMS_LUNFILE, "w");
fprintf (fp, "\n");
fclose (fp);
printf("Disabled!\n");
}
}
char** get_mount_menu_options()
{
Volume * device_volumes = get_device_volumes();
num_volumes = get_num_volumes();
char** volumes = malloc (num_volumes * sizeof (char *));
int mountable_volumes = 0;
int usb_storage_enabled = is_usb_storage_enabled();
int i = 0;
if (usb_storage_enabled != 3)
{
volumes[mountable_volumes] = "USB Mass Storage";
printf("volumes[%i]: %s\n", mountable_volumes, volumes[mountable_volumes]);
mountable_volumes++;
i = 1;
}
for (i; i<num_volumes; i++)
{
volumes[i] = "";
Volume *v = &device_volumes[i];
char* operation;
if (is_path_mountable(v->mount_point) != -1)
{
if (is_path_mounted(v->mount_point)) operation = "Unmount";
else operation = "Mount";
printf("volumes[%i]: %s %s\n", mountable_volumes, operation, v->mount_point);
volumes[mountable_volumes] = malloc(strlen(operation)+strlen(v->mount_point)+2);
sprintf(volumes[mountable_volumes], "%s %s", operation, v->mount_point);
mountable_volumes++;
}
}
char **main_items = malloc (num_volumes * sizeof (char *));
int z = 0;
for (z; z<mountable_volumes; z++)
{
main_items[z] = malloc(strlen(volumes[z])+2);
main_items[z] = volumes[z];
}
main_items[z] = NULL;
return main_items;
}
void
show_mount_menu ()
{
num_volumes = get_num_volumes();
char** main_items = get_mount_menu_options();
static char *headers[] = { "Choose a mount",
"or unmount option",
"",
NULL
};
int chosen_item = -1;
char* selected;
while (chosen_item != ITEM_BACK)
{
chosen_item =
get_menu_selection (headers, main_items, 0,
chosen_item < 0 ? 0 : chosen_item);
switch (chosen_item)
{
case ITEM_BACK:
return;
default:
//printf("selected: %s\n", main_items[chosen_item]);
if (strcmp(main_items[chosen_item], "USB Mass Storage") ==0)
{
show_usb_menu();
}
else
{
char* partition=main_items[chosen_item];
char* strptr = strstr(partition, " ") + 1; //get a pointer to the string beginning 1 position after the space
char* result = calloc(strlen(strptr) + 1, sizeof(char)); // allocate some mem for the new string
strcpy(result, strptr); //copy the pointer's contents into the new string
printf("\npartition: %s\n", result);
int mp = is_path_mounted(result);
printf("%s is mounted: %i\n", result, mp);
char* prefix;
main_items[chosen_item] = NULL;
if (mp)
{
printf("Unmounting %s...\n", result);
ensure_path_unmounted(result);
}
else
{
printf("Mounting %s...\n", result);
ensure_path_mounted(result);
}
}
}
main_items = get_mount_menu_options();
}
return;
}