blob: 1cc28b24608612e607651b7a833a54c55b97ae47 [file] [log] [blame]
Koushik Dutta68df48c2010-09-13 15:04:54 -07001#include <ctype.h>
2#include <errno.h>
3#include <fcntl.h>
4#include <getopt.h>
5#include <limits.h>
6#include <linux/input.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/reboot.h>
11#include <sys/types.h>
12#include <time.h>
13#include <unistd.h>
14
15#include <sys/wait.h>
16#include <sys/limits.h>
17#include <dirent.h>
18#include <sys/stat.h>
19
20#include <signal.h>
21#include <sys/wait.h>
22
Steve Kondik4123b582010-11-14 03:18:40 -050023extern int __system(const char *command);
Koushik Dutta02c36052010-12-12 02:52:44 -080024#define BML_UNLOCK_ALL 0x8A29 ///< unlock all partition RO -> RW
Koushik Dutta68df48c2010-09-13 15:04:54 -070025
Koushik Dutta134ead72010-12-12 16:54:41 -080026static int restore_internal(const char* bml, const char* filename)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080027{
Koushik Dutta03a4f5b2010-12-12 12:24:02 -080028 char buf[4096];
29 int dstfd, srcfd, bytes_read, bytes_written, total_read = 0;
30 if (filename == NULL)
31 srcfd = 0;
32 else {
33 srcfd = open(filename, O_RDONLY | O_LARGEFILE);
34 if (srcfd < 0)
35 return 2;
36 }
37 dstfd = open(bml, O_RDWR | O_LARGEFILE);
38 if (dstfd < 0)
39 return 3;
40 if (ioctl(dstfd, BML_UNLOCK_ALL, 0))
41 return 4;
42 do {
43 total_read += bytes_read = read(srcfd, buf, 4096);
44 if (!bytes_read)
45 break;
46 if (bytes_read < 4096)
47 memset(&buf[bytes_read], 0, 4096 - bytes_read);
48 if (write(dstfd, buf, 4096) < 4096)
49 return 5;
50 } while(bytes_read == 4096);
Koushik Dutta134ead72010-12-12 16:54:41 -080051
52 close(dstfd);
53 close(srcfd);
54
Koushik Dutta03a4f5b2010-12-12 12:24:02 -080055 return 0;
Koushik Dutta68df48c2010-09-13 15:04:54 -070056}
Koushik Dutta56c1b3b2010-09-13 15:08:49 -070057
Koushik Dutta134ead72010-12-12 16:54:41 -080058int cmd_bml_restore_raw_partition(const char *partition, const char *filename)
59{
60 char *bml;
61 if (strcmp(partition, "boot") == 0 || strcmp(partition, "recovery") == 0)
62 bml = "/dev/block/bml7";
63 else
64 return 6;
65
66 int ret = restore_internal("/dev/block/bml7", filename);
67 if (ret != 0)
68 return ret;
69
70 ret = restore_internal("/dev/block/bml8", filename);
71 return ret;
72}
73
Steve Kondik4123b582010-11-14 03:18:40 -050074int cmd_bml_backup_raw_partition(const char *partition, const char *filename)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080075{
Koushik Dutta56c1b3b2010-09-13 15:08:49 -070076 char tmp[PATH_MAX];
Koushik Dutta134ead72010-12-12 16:54:41 -080077 sprintf(tmp, "dd of=%s if=/dev/block/bml7 bs=4096", filename);
Koushik Dutta56c1b3b2010-09-13 15:08:49 -070078 return __system(tmp);
79}
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080080
Steve Kondik4123b582010-11-14 03:18:40 -050081int cmd_bml_erase_raw_partition(const char *partition)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080082{
83 // TODO: implement raw wipe
84 return 0;
85}
86
Steve Kondik4123b582010-11-14 03:18:40 -050087int cmd_bml_erase_partition(const char *partition, const char *filesystem)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080088{
89 return -1;
90}
91
Steve Kondik4123b582010-11-14 03:18:40 -050092int cmd_bml_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080093{
94 return -1;
95}
96
Steve Kondik4123b582010-11-14 03:18:40 -050097int cmd_bml_get_partition_device(const char *partition, char *device)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080098{
99 return -1;
100}