blob: d81cf111379a662871b66e7c120337e18345ebc2 [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 Dutta195e06b2011-05-29 19:33:42 -070064 int ret = -1;
65 if (strcmp(partition, "recoveryonly") != 0) {
66 // always restore boot, regardless of whether recovery or boot is flashed.
67 // this is because boot and recovery are the same on some samsung phones.
68 // unless of course, recoveryonly is explictly chosen (bml8)
69 ret = restore_internal("/dev/block/bml7", filename);
70 if (ret != 0)
71 return ret;
72 }
Koushik Dutta134ead72010-12-12 16:54:41 -080073
Koushik Dutta195e06b2011-05-29 19:33:42 -070074 if (strcmp(partition, "recovery") == 0 || strcmp(partition, "recoveryonly") == 0)
Koushik Dutta03cf72a2010-12-21 15:14:21 -080075 ret = restore_internal("/dev/block/bml8", filename);
Koushik Dutta134ead72010-12-12 16:54:41 -080076 return ret;
77}
78
Koushik Duttac8ff7932010-12-21 15:16:05 -080079int cmd_bml_backup_raw_partition(const char *partition, const char *out_file)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080080{
Koushik Duttacdb433a2010-12-21 15:13:58 -080081 char* bml;
82 if (strcmp("boot", partition) == 0)
83 bml = "/dev/block/bml7";
84 else if (strcmp("recovery", partition) == 0)
85 bml = "/dev/block/bml8";
86 else {
87 printf("Invalid partition.\n");
88 return -1;
89 }
90
91 int ch;
92 FILE *in;
93 FILE *out;
94 int val = 0;
95 char buf[512];
96 unsigned sz = 0;
97 unsigned i;
98 int ret = -1;
99 char *in_file = bml;
100
101 in = fopen ( in_file, "r" );
102 if (in == NULL)
103 goto ERROR3;
104
105 out = fopen ( out_file, "w" );
106 if (out == NULL)
107 goto ERROR2;
108
109 fseek(in, 0L, SEEK_END);
110 sz = ftell(in);
111 fseek(in, 0L, SEEK_SET);
112
113 if (sz % 512)
114 {
115 while ( ( ch = fgetc ( in ) ) != EOF )
116 fputc ( ch, out );
117 }
118 else
119 {
120 for (i=0; i< (sz/512); i++)
121 {
122 if ((fread(buf, 512, 1, in)) != 1)
123 goto ERROR1;
124 if ((fwrite(buf, 512, 1, out)) != 1)
125 goto ERROR1;
126 }
127 }
128
129 fsync(out);
130 ret = 0;
131ERROR1:
132 fclose ( out );
133ERROR2:
134 fclose ( in );
135ERROR3:
136 return ret;
Koushik Dutta56c1b3b2010-09-13 15:08:49 -0700137}
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800138
Steve Kondik4123b582010-11-14 03:18:40 -0500139int cmd_bml_erase_raw_partition(const char *partition)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800140{
141 // TODO: implement raw wipe
142 return 0;
143}
144
Steve Kondik4123b582010-11-14 03:18:40 -0500145int cmd_bml_erase_partition(const char *partition, const char *filesystem)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800146{
147 return -1;
148}
149
Steve Kondik4123b582010-11-14 03:18:40 -0500150int cmd_bml_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800151{
152 return -1;
153}
154
Steve Kondik4123b582010-11-14 03:18:40 -0500155int cmd_bml_get_partition_device(const char *partition, char *device)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800156{
157 return -1;
158}