blob: 9f8ee2f49ade8c6efd7805f74c26018b84df067d [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 Dutta03cf72a2010-12-21 15:14:21 -080026
Koushik Dutta134ead72010-12-12 16:54:41 -080027static int restore_internal(const char* bml, const char* filename)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080028{
Koushik Dutta03a4f5b2010-12-12 12:24:02 -080029 char buf[4096];
30 int dstfd, srcfd, bytes_read, bytes_written, total_read = 0;
31 if (filename == NULL)
32 srcfd = 0;
33 else {
34 srcfd = open(filename, O_RDONLY | O_LARGEFILE);
35 if (srcfd < 0)
36 return 2;
37 }
38 dstfd = open(bml, O_RDWR | O_LARGEFILE);
39 if (dstfd < 0)
40 return 3;
41 if (ioctl(dstfd, BML_UNLOCK_ALL, 0))
42 return 4;
43 do {
44 total_read += bytes_read = read(srcfd, buf, 4096);
45 if (!bytes_read)
46 break;
47 if (bytes_read < 4096)
48 memset(&buf[bytes_read], 0, 4096 - bytes_read);
49 if (write(dstfd, buf, 4096) < 4096)
50 return 5;
51 } while(bytes_read == 4096);
Koushik Dutta134ead72010-12-12 16:54:41 -080052
53 close(dstfd);
54 close(srcfd);
55
Koushik Dutta03a4f5b2010-12-12 12:24:02 -080056 return 0;
Koushik Dutta68df48c2010-09-13 15:04:54 -070057}
Koushik Dutta56c1b3b2010-09-13 15:08:49 -070058
Koushik Dutta134ead72010-12-12 16:54:41 -080059int cmd_bml_restore_raw_partition(const char *partition, const char *filename)
60{
Koushik Dutta03cf72a2010-12-21 15:14:21 -080061 if (strcmp(partition, "boot") != 0 && strcmp(partition, "recovery") != 0)
Koushik Dutta134ead72010-12-12 16:54:41 -080062 return 6;
63
Koushik Dutta03cf72a2010-12-21 15:14:21 -080064 // always restore boot, regardless of whether recovery or boot is flashed.
65 // this is because boot and recovery are the same on some samsung phones.
Koushik Dutta134ead72010-12-12 16:54:41 -080066 int ret = restore_internal("/dev/block/bml7", filename);
67 if (ret != 0)
68 return ret;
69
Koushik Dutta03cf72a2010-12-21 15:14:21 -080070 if (strcmp(partition, "recovery") == 0)
71 ret = restore_internal("/dev/block/bml8", filename);
Koushik Dutta134ead72010-12-12 16:54:41 -080072 return ret;
73}
74
Koushik Duttac8ff7932010-12-21 15:16:05 -080075int cmd_bml_backup_raw_partition(const char *partition, const char *out_file)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080076{
Koushik Duttacdb433a2010-12-21 15:13:58 -080077 char* bml;
78 if (strcmp("boot", partition) == 0)
79 bml = "/dev/block/bml7";
80 else if (strcmp("recovery", partition) == 0)
81 bml = "/dev/block/bml8";
82 else {
83 printf("Invalid partition.\n");
84 return -1;
85 }
86
87 int ch;
88 FILE *in;
89 FILE *out;
90 int val = 0;
91 char buf[512];
92 unsigned sz = 0;
93 unsigned i;
94 int ret = -1;
95 char *in_file = bml;
96
97 in = fopen ( in_file, "r" );
98 if (in == NULL)
99 goto ERROR3;
100
101 out = fopen ( out_file, "w" );
102 if (out == NULL)
103 goto ERROR2;
104
105 fseek(in, 0L, SEEK_END);
106 sz = ftell(in);
107 fseek(in, 0L, SEEK_SET);
108
109 if (sz % 512)
110 {
111 while ( ( ch = fgetc ( in ) ) != EOF )
112 fputc ( ch, out );
113 }
114 else
115 {
116 for (i=0; i< (sz/512); i++)
117 {
118 if ((fread(buf, 512, 1, in)) != 1)
119 goto ERROR1;
120 if ((fwrite(buf, 512, 1, out)) != 1)
121 goto ERROR1;
122 }
123 }
124
125 fsync(out);
126 ret = 0;
127ERROR1:
128 fclose ( out );
129ERROR2:
130 fclose ( in );
131ERROR3:
132 return ret;
Koushik Dutta56c1b3b2010-09-13 15:08:49 -0700133}
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800134
Steve Kondik4123b582010-11-14 03:18:40 -0500135int cmd_bml_erase_raw_partition(const char *partition)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800136{
137 // TODO: implement raw wipe
138 return 0;
139}
140
Steve Kondik4123b582010-11-14 03:18:40 -0500141int cmd_bml_erase_partition(const char *partition, const char *filesystem)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800142{
143 return -1;
144}
145
Steve Kondik4123b582010-11-14 03:18:40 -0500146int cmd_bml_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800147{
148 return -1;
149}
150
Steve Kondik4123b582010-11-14 03:18:40 -0500151int cmd_bml_get_partition_device(const char *partition, char *device)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800152{
153 return -1;
154}