-
Notifications
You must be signed in to change notification settings - Fork 3
/
ddftw.c
621 lines (522 loc) · 17.7 KB
/
ddftw.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
* Copyright (C) 2011 The Android Open Source 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 <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <unistd.h>
#include "ddftw.h"
#include "common.h"
#include "extra-functions.h"
#include "bootloader.h"
#include "backstore.h"
#include "data.h"
struct dInfo tmp, sys, dat, boo, rec, cac, sdcext, sdcint, ase, sde, sp1, sp2, sp3;
char tw_device_name[20];
void dumpPartitionTable(void);
static int isMTDdevice = 0;
struct dInfo* findDeviceByLabel(const char* label)
{
if (!label) return NULL;
if (strcmp(label, "system") == 0) return &sys;
if (strcmp(label, "userdata") == 0) return &dat;
if (strcmp(label, "data") == 0) return &dat;
if (strcmp(label, "boot") == 0) return &boo;
if (strcmp(label, "recovery") == 0) return &rec;
if (strcmp(label, "cache") == 0) return &cac;
if (strcmp(label, "sd-ext") == 0) return &sde;
if (strcmp(label, "and-sec") == 0) return &ase;
// New sdcard methods
if (strcmp(label, "sdcard") == 0) return &sdcext;
if (strcmp(label, "sdc-ext") == 0) return &sdcext;
if (strcmp(label, "sdc-int") == 0) return &sdcint;
// Special Partitions (such as WiMAX, efs, and PDS)
if (strcmp(label, EXPAND(SP1_NAME)) == 0) return &sp1;
if (strcmp(label, EXPAND(SP2_NAME)) == 0) return &sp2;
if (strcmp(label, EXPAND(SP3_NAME)) == 0) return &sp3;
return NULL;
}
struct dInfo* findDeviceByBlockDevice(const char* blockDevice)
{
if (!blockDevice) return NULL;
if (strcmp(blockDevice, sys.blk) == 0) return &sys;
if (strcmp(blockDevice, dat.blk) == 0) return &dat;
if (strcmp(blockDevice, boo.blk) == 0) return &boo;
if (strcmp(blockDevice, rec.blk) == 0) return &rec;
if (strcmp(blockDevice, cac.blk) == 0) return &cac;
if (strcmp(blockDevice, sde.blk) == 0) return &sde;
if (strcmp(blockDevice, sdcext.blk) == 0) return &sdcext;
if (strcmp(blockDevice, sdcint.blk) == 0) return &sdcint;
if (strcmp(blockDevice, sp1.blk) == 0) return &sp1;
if (strcmp(blockDevice, sp2.blk) == 0) return &sp2;
if (strcmp(blockDevice, sp3.blk) == 0) return &sp3;
return NULL;
}
#define SAFE_STR(str) (str ? str : "<NULL>")
// This routine handles the case where we can't open either /proc/mtd or /proc/emmc
int setLocationData(const char* label, const char* blockDevice, const char* mtdDevice, const char* fstype, unsigned long long size)
{
struct dInfo* loc = NULL;
if (label) loc = findDeviceByLabel(label);
if (!loc && blockDevice) loc = findDeviceByBlockDevice(blockDevice);
if (!loc)
return -1;
if (label) strcpy(loc->mnt, label);
if (blockDevice) strcpy(loc->blk, blockDevice);
if (mtdDevice) strcpy(loc->dev, mtdDevice);
// This is a simple
if (strcmp(loc->mnt, "boot") == 0 && fstype)
{
loc->mountable = 0;
if (strcmp(fstype, "vfat") == 0 || memcmp(fstype, "ext", 3) == 0)
loc->mountable = 1;
}
if (fstype) strcpy(loc->fst, fstype);
if (size && loc->sze == 0) loc->sze = size;
return 0;
}
int getSizesViaPartitions()
{
FILE* fp;
char line[512];
int ret = 0;
// In this case, we'll first get the partitions we care about (with labels)
fp = fopen("/proc/partitions", "rt");
if (fp == NULL)
return -1;
while (fgets(line, sizeof(line), fp) != NULL)
{
unsigned long major, minor, blocks;
char device[64];
char tmpString[64];
if (strlen(line) < 7 || line[0] == 'm') continue;
sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
// Adjust block size to byte size
unsigned long long size = blocks * 1024LL;
sprintf(tmpString, "%s%s", tw_block, device);
setLocationData(NULL, tmpString, NULL, NULL, size);
}
fclose(fp);
return 0;
}
int getLocationsViafstab()
{
FILE* fp;
char line[512];
int ret = 0;
// In this case, we'll first get the partitions we care about (with labels)
fp = fopen("/etc/recovery.fstab", "rt");
if (fp == NULL)
{
LOGE("=> Unable to open /etc/recovery.fstab\n");
return -1;
}
while (fgets(line, sizeof(line), fp) != NULL)
{
char mount[32], fstype[32], device[256];
char* pDevice = device;
if (line[0] != '/') continue;
sscanf(line + 1, "%s %s %s", mount, fstype, device);
// Attempt to flip mounts until we find the block device
char realDevice[256];
memset(realDevice, 0, sizeof(realDevice));
while (readlink(device, realDevice, sizeof(realDevice)) > 0)
{
strcpy(device, realDevice);
memset(realDevice, 0, sizeof(realDevice));
}
if (device[0] != '/') pDevice = NULL;
setLocationData(mount, pDevice, pDevice, fstype, 0);
}
fclose(fp);
// We can ignore the results of this call. But if it works, it at least helps get details
getSizesViaPartitions();
// Now, let's retrieve base partition sizes
if (!isMTDdevice)
{
fp = __popen("fdisk -l /dev/block/mmcblk0","r");
if (fp == NULL)
{
LOGE("=> Unable to retrieve partition sizes via fdisk\n");
return -1;
}
while (fgets(line, sizeof(line), fp) != NULL)
{
char isBoot[64], device[64], blocks[2][16], *pSizeBlock;
unsigned long long size = 0;
if (line[0] != '/') continue;
sscanf(line, "%s %s %*s %s %s", device, isBoot, blocks[0], blocks[1]);
if (isBoot[0] == '*') pSizeBlock = blocks[1];
else pSizeBlock = blocks[0];
// If the block size isn't accurate, don't record it.
if (pSizeBlock[strlen(pSizeBlock)-1] == '+') pSizeBlock = NULL;
// This could be NULL if we decided the size wasn't accurate
if (pSizeBlock)
{
size = ((unsigned long long) atol(pSizeBlock)) * 1024;
}
if (size && (setLocationData(NULL, device, NULL, NULL, size) == 0))
{
// LOGI(" Mount %s size: %d\n", device, size);
}
}
fclose(fp);
}
return ret;
}
// get locations from our device.info
int getLocationsViaProc(const char* fstype)
{
FILE *fp = NULL;
char line[255];
if (fstype == NULL)
{
LOGE("Invalid argument to getLocationsViaProc\n");
return -1;
}
sprintf(line, "/proc/%s", fstype);
fp = fopen(line, "rt");
if (fp == NULL)
{
LOGW("Device does not support %s\n", line);
return -1;
}
while (fgets(line, sizeof(line), fp) != NULL)
{
char device[32], label[32];
unsigned long size = 0;
char mtdDevice[32];
char* fstype = NULL;
int deviceId;
sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
// Skip header and blank lines
if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
continue;
// Strip off the : and " from the details
device[strlen(device)-1] = '\0';
label[strlen(label)-1] = '\0';
if (sscanf(device,"mtd%d", &deviceId) == 1)
{
isMTDdevice = 1;
sprintf(mtdDevice, "%s%s", tw_mtd, device);
sprintf(device, "%smtdblock%d", tw_block, deviceId);
strcpy(tmp.fst,"mtd");
fstype = "mtd";
}
else
{
strcpy(mtdDevice, device);
fstype = "emmc";
}
if (strcmp(label, "efs") == 0)
fstype = "yaffs2";
setLocationData(label, device, mtdDevice, fstype, (unsigned long long) size);
}
fclose(fp);
// We still proceed on to use the fstab to query devices as well
return getLocationsViafstab();
}
unsigned long long getUsedSizeViaDu(const char* path)
{
char cmd[512];
sprintf(cmd, "du %s | awk '{ print $1 }'", path);
FILE *fp;
fp = __popen(cmd, "r");
char str[512];
fgets(str, sizeof(str), fp);
__pclose(fp);
unsigned long long size = atol(str);
size *= 1024;
return size;
}
void updateMntUsedSize(struct dInfo* mMnt)
{
#ifdef RECOVERY_SDCARD_ON_DATA
if (mMnt == &sdcext)
{
struct statfs st;
if (statfs("/mnt/data-sdc/.", &st) != 0) return;
mMnt->used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
return;
}
#endif
if (strcmp(mMnt->mnt, ".android_secure") == 0)
{
// android_secure is a little different - we mount sdcard and use du to figure out how much space is being taken up by android_secure
// We can ignore any error from this, it doesn't matter
tw_mount(sdcext);
mMnt->used = getUsedSizeViaDu("/sdcard/.android_secure");
mMnt->sze = mMnt->used;
return;
}
char path[512];
struct statfs st;
int mounted;
if (!mMnt->mountable)
{
// Since this partition isn't mountable, we're going to mark it's used size as it's standard size
mMnt->used = mMnt->sze;
return;
}
mounted = tw_isMounted(*mMnt);
if (!mounted)
{
// If we fail, just move on
if (tw_mount(*mMnt))
return;
}
sprintf(path, "/%s/.", mMnt->mnt);
if (statfs(path, &st) != 0)
{
LOGE("Unable to stat '%s'\n", path);
return;
}
mMnt->used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
if (!mounted) tw_unmount(*mMnt);
return;
}
void updateUsedSized()
{
updateMntUsedSize(&boo);
updateMntUsedSize(&sys);
updateMntUsedSize(&dat);
updateMntUsedSize(&cac);
updateMntUsedSize(&rec);
updateMntUsedSize(&sdcext);
updateMntUsedSize(&sdcint);
updateMntUsedSize(&sde);
updateMntUsedSize(&ase);
updateMntUsedSize(&sp1);
updateMntUsedSize(&sp2);
updateMntUsedSize(&sp3);
dumpPartitionTable();
return;
}
int getLocations()
{
// This decides if a partition can be mounted and appears in the fstab
sys.mountable = 1;
dat.mountable = 1;
cac.mountable = 1;
sde.mountable = 1;
// boo.mountable = 1; // Boot it detected earlier
rec.mountable = 0;
sdcext.mountable = 1;
sdcint.mountable = 1;
ase.mountable = 0;
sp1.mountable = SP1_MOUNTABLE;
sp2.mountable = SP2_MOUNTABLE;
sp3.mountable = SP3_MOUNTABLE;
// This decides how we backup/restore a block
sys.backup = files;
dat.backup = files;
cac.backup = files;
sde.backup = files;
boo.backup = image;
rec.backup = image;
sdcext.backup = none;
sdcint.backup = none;
ase.backup = files;
sp1.backup = SP1_BACKUP_METHOD;
sp2.backup = SP2_BACKUP_METHOD;
sp3.backup = SP3_BACKUP_METHOD;
if (getLocationsViaProc("emmc") != 0 && getLocationsViaProc("mtd") != 0 && getLocationsViafstab() != 0)
{
LOGE("E: Unable to get device locations.\n");
return -1;
}
// Handle adding .android_secure and sd-ext
strcpy(ase.dev, "/sdcard/.android_secure"); // android secure stuff
strcpy(ase.blk, ase.dev);
strcpy(ase.mnt, ".android_secure");
strcpy(ase.fst, "vfat");
if (strlen(sdcext.blk) > 0)
{
int tmpInt;
char tmpBase[50];
char tmpWildCard[50];
// We make the base via sdcard block
strcpy(sde.mnt, "sd-ext");
strcpy(tmpBase, sdcext.blk);
tmpBase[strlen(tmpBase)-1] = '\0';
sprintf(tmpWildCard,"%s%%d",tmpBase);
sscanf(sdcext.blk, tmpWildCard, &tmpInt); // sdcard block used as sd-ext base
sprintf(sde.blk, "%s%d",tmpBase, tmpInt+1);
}
get_device_id();
LOGI("=> Let's update filesystem types.\n");
verifyFst(); // use blkid to REALLY REALLY determine partition filesystem type
LOGI("=> And update our fstab also.\n");
createFstab(); // used for our busybox mount command
LOGI("=> Update the usage statistics.\n\n");
updateUsedSized(); // Retrieves the used space of all partitions
// Now, let's update the data manager...
DataManager_SetIntValue("tw_boot_is_mountable", boo.mountable ? 1 : 0);
DataManager_SetIntValue("tw_system_is_mountable", sys.mountable ? 1 : 0);
DataManager_SetIntValue("tw_data_is_mountable", dat.mountable ? 1 : 0);
DataManager_SetIntValue("tw_cache_is_mountable", cac.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sdcext_is_mountable", sdcext.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sdcint_is_mountable", sdcint.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sd-ext_is_mountable", sde.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sp1_is_mountable", sp1.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sp2_is_mountable", sp2.mountable ? 1 : 0);
DataManager_SetIntValue("tw_sp3_is_mountable", sp3.mountable ? 1 : 0);
return 0;
}
static void createFstabEntry(FILE* fp, struct dInfo* mnt)
{
char tmpString[255];
struct stat st;
if (!*(mnt->blk))
{
mnt->mountable = 0;
return;
}
sprintf(tmpString,"%s /%s %s rw\n", mnt->blk, mnt->mnt, mnt->fst);
fputs(tmpString, fp);
sprintf(tmpString, "/%s", mnt->mnt);
// We only create the folder if the block device exists
if (stat(mnt->blk, &st) == 0 && stat(tmpString, &st) != 0)
{
if (mkdir(tmpString, 0777) == -1)
LOGI("=> Can not create %s folder.\n", tmpString);
else
LOGI("=> Created %s folder.\n", tmpString);
}
return;
}
// write fstab so we can mount in adb shell
void createFstab()
{
FILE *fp;
fp = fopen("/etc/fstab", "w");
if (fp == NULL)
LOGI("=> Can not open /etc/fstab.\n");
else
{
if (boo.mountable) createFstabEntry(fp, &boo);
if (sys.mountable) createFstabEntry(fp, &sys);
if (dat.mountable) createFstabEntry(fp, &dat);
if (cac.mountable) createFstabEntry(fp, &cac);
if (sdcext.mountable) createFstabEntry(fp, &sdcext);
if (sdcint.mountable) createFstabEntry(fp, &sdcint);
if (sde.mountable) createFstabEntry(fp, &sde);
if (sp1.mountable) createFstabEntry(fp, &sp1);
if (sp2.mountable) createFstabEntry(fp, &sp2);
if (sp3.mountable) createFstabEntry(fp, &sp3);
}
fclose(fp);
}
void verifyFst()
{
FILE *fp;
char blkOutput[100];
char* blk;
char* arg;
char* ptr;
struct dInfo* dat;
// This has a tendency to hang on MTD devices.
if (isMTDdevice) return;
fp = __popen("blkid","r");
while (fgets(blkOutput,sizeof(blkOutput),fp) != NULL)
{
blk = blkOutput;
ptr = blkOutput;
while (*ptr > 32 && *ptr != ':') ptr++;
if (*ptr == 0) continue;
*ptr = 0;
// Increment by two, but verify that we don't hit a NULL
ptr++;
if (*ptr != 0) ptr++;
// Now, find the TYPE field
while (1)
{
arg = ptr;
while (*ptr > 32) ptr++;
if (*ptr != 0)
{
*ptr = 0;
ptr++;
}
if (strlen(arg) > 6)
{
if (memcmp(arg, "TYPE=\"", 6) == 0) break;
if (memcmp(arg, "TYPE=\"", 6) == 0) break;
if (memcmp(arg, "TYPE=\"", 6) == 0) break;
}
if (*ptr == 0)
{
arg = NULL;
break;
}
}
if (arg && strlen(arg) > 7)
{
arg += 6; // Skip the TYPE=" portion
arg[strlen(arg)-1] = '\0'; // Drop the tail quote
}
else
continue;
dat = findDeviceByBlockDevice(blk);
if (dat)
strcpy(sys.fst,arg);
}
__pclose(fp);
}
char backupToChar(enum backup_method method)
{
switch (method)
{
case unknown:
default:
return 'u';
case none:
return 'n';
case image:
return 'i';
case files:
return 'f';
}
return 'u';
}
void dumpPartitionEntry(struct dInfo* mnt)
{
char* mntName = mnt->mnt;
if (strcmp(mntName, ".android_secure") == 0) mntName = (char*) "andsec";
fprintf(stderr, "| %8s | %27s | %4s | %8lu | %8lu | %d | %c |\n",
mntName, mnt->blk, mnt->fst, (unsigned long) (mnt->sze / 1024), (unsigned long) (mnt->used / 1024), mnt->mountable ? 1 : 0,
backupToChar(mnt->backup));
}
void dumpPartitionTable(void)
{
fprintf(stderr, "+----------+-----------------------------+------+----------+----------+---+---+\n");
fprintf(stderr, "| Mount | Block Device | fst | Size(KB) | Used(KB) | M | B |\n");
fprintf(stderr, "+----------+-----------------------------+------+----------+----------+---+---+\n");
dumpPartitionEntry(&tmp);
dumpPartitionEntry(&sys);
dumpPartitionEntry(&dat);
dumpPartitionEntry(&boo);
dumpPartitionEntry(&rec);
dumpPartitionEntry(&cac);
dumpPartitionEntry(&sdcext);
dumpPartitionEntry(&sdcint);
dumpPartitionEntry(&ase);
dumpPartitionEntry(&sde);
dumpPartitionEntry(&sp1);
dumpPartitionEntry(&sp2);
dumpPartitionEntry(&sp3);
fprintf(stderr, "+----------+-----------------------------+------+----------+----------+---+---+\n");
}