blob: a7b09b4168e7c71a0c44093bd9d0baa74e71d776 [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 Duttaa75c0672011-07-07 12:55:02 -070026#ifndef BOARD_BML_BOOT
27#define BOARD_BML_BOOT "/dev/block/bml7"
28#endif
29
30#ifndef BOARD_BML_RECOVERY
31#define BOARD_BML_RECOVERY "/dev/block/bml8"
32#endif
Koushik Dutta03cf72a2010-12-21 15:14:21 -080033
Koushik Dutta134ead72010-12-12 16:54:41 -080034static int restore_internal(const char* bml, const char* filename)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080035{
Koushik Dutta03a4f5b2010-12-12 12:24:02 -080036 char buf[4096];
37 int dstfd, srcfd, bytes_read, bytes_written, total_read = 0;
38 if (filename == NULL)
39 srcfd = 0;
40 else {
41 srcfd = open(filename, O_RDONLY | O_LARGEFILE);
42 if (srcfd < 0)
43 return 2;
44 }
45 dstfd = open(bml, O_RDWR | O_LARGEFILE);
46 if (dstfd < 0)
47 return 3;
48 if (ioctl(dstfd, BML_UNLOCK_ALL, 0))
49 return 4;
50 do {
51 total_read += bytes_read = read(srcfd, buf, 4096);
52 if (!bytes_read)
53 break;
54 if (bytes_read < 4096)
55 memset(&buf[bytes_read], 0, 4096 - bytes_read);
56 if (write(dstfd, buf, 4096) < 4096)
57 return 5;
58 } while(bytes_read == 4096);
Koushik Dutta134ead72010-12-12 16:54:41 -080059
60 close(dstfd);
61 close(srcfd);
62
Koushik Dutta03a4f5b2010-12-12 12:24:02 -080063 return 0;
Koushik Dutta68df48c2010-09-13 15:04:54 -070064}
Koushik Dutta56c1b3b2010-09-13 15:08:49 -070065
Koushik Dutta134ead72010-12-12 16:54:41 -080066int cmd_bml_restore_raw_partition(const char *partition, const char *filename)
67{
Koushik Dutta890b9512011-10-29 21:38:53 -070068 if (strcmp(partition, "boot") != 0 && strcmp(partition, "recovery") != 0 && strcmp(partition, "recoveryonly") != 0 && partition[0] != '/')
Koushik Dutta134ead72010-12-12 16:54:41 -080069 return 6;
70
Koushik Dutta195e06b2011-05-29 19:33:42 -070071 int ret = -1;
72 if (strcmp(partition, "recoveryonly") != 0) {
73 // always restore boot, regardless of whether recovery or boot is flashed.
74 // this is because boot and recovery are the same on some samsung phones.
75 // unless of course, recoveryonly is explictly chosen (bml8)
Koushik Duttaa75c0672011-07-07 12:55:02 -070076 ret = restore_internal(BOARD_BML_BOOT, filename);
Koushik Dutta195e06b2011-05-29 19:33:42 -070077 if (ret != 0)
78 return ret;
79 }
Koushik Dutta134ead72010-12-12 16:54:41 -080080
Koushik Dutta195e06b2011-05-29 19:33:42 -070081 if (strcmp(partition, "recovery") == 0 || strcmp(partition, "recoveryonly") == 0)
Koushik Duttaa75c0672011-07-07 12:55:02 -070082 ret = restore_internal(BOARD_BML_RECOVERY, filename);
Koushik Duttae87f9e62011-07-12 18:07:45 -070083
84 // support explicitly provided device paths
85 if (partition[0] == '/')
86 ret = restore_internal(partition, filename);
Koushik Dutta134ead72010-12-12 16:54:41 -080087 return ret;
88}
89
Koushik Duttac8ff7932010-12-21 15:16:05 -080090int cmd_bml_backup_raw_partition(const char *partition, const char *out_file)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080091{
Koushik Duttacdb433a2010-12-21 15:13:58 -080092 char* bml;
93 if (strcmp("boot", partition) == 0)
Koushik Duttaa75c0672011-07-07 12:55:02 -070094 bml = BOARD_BML_BOOT;
Koushik Duttacdb433a2010-12-21 15:13:58 -080095 else if (strcmp("recovery", partition) == 0)
Koushik Duttaa75c0672011-07-07 12:55:02 -070096 bml = BOARD_BML_RECOVERY;
Koushik Duttae87f9e62011-07-12 18:07:45 -070097 else if (partition[0] == '/') {
98 // support explicitly provided device paths
99 bml = partition;
100 }
Koushik Duttacdb433a2010-12-21 15:13:58 -0800101 else {
102 printf("Invalid partition.\n");
103 return -1;
104 }
105
106 int ch;
107 FILE *in;
108 FILE *out;
109 int val = 0;
110 char buf[512];
111 unsigned sz = 0;
112 unsigned i;
113 int ret = -1;
114 char *in_file = bml;
115
116 in = fopen ( in_file, "r" );
117 if (in == NULL)
118 goto ERROR3;
119
120 out = fopen ( out_file, "w" );
121 if (out == NULL)
122 goto ERROR2;
123
124 fseek(in, 0L, SEEK_END);
125 sz = ftell(in);
126 fseek(in, 0L, SEEK_SET);
127
128 if (sz % 512)
129 {
130 while ( ( ch = fgetc ( in ) ) != EOF )
131 fputc ( ch, out );
132 }
133 else
134 {
135 for (i=0; i< (sz/512); i++)
136 {
137 if ((fread(buf, 512, 1, in)) != 1)
138 goto ERROR1;
139 if ((fwrite(buf, 512, 1, out)) != 1)
140 goto ERROR1;
141 }
142 }
143
144 fsync(out);
145 ret = 0;
146ERROR1:
147 fclose ( out );
148ERROR2:
149 fclose ( in );
150ERROR3:
151 return ret;
Koushik Dutta56c1b3b2010-09-13 15:08:49 -0700152}
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800153
Steve Kondik4123b582010-11-14 03:18:40 -0500154int cmd_bml_erase_raw_partition(const char *partition)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800155{
156 // TODO: implement raw wipe
157 return 0;
158}
159
Steve Kondik4123b582010-11-14 03:18:40 -0500160int cmd_bml_erase_partition(const char *partition, const char *filesystem)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800161{
162 return -1;
163}
164
Steve Kondik4123b582010-11-14 03:18:40 -0500165int cmd_bml_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800166{
167 return -1;
168}
169
Steve Kondik4123b582010-11-14 03:18:40 -0500170int cmd_bml_get_partition_device(const char *partition, char *device)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800171{
172 return -1;
173}
Donovan Bartisha8f265d2011-11-10 12:55:22 -0600174
Donovan Bartisha8f265d2011-11-10 12:55:22 -0600175int format_rfs_device (const char *device, const char *path) {
176 const char *fatsize = "32";
177 const char *sectorsize = "1";
178
179 if (strcmp(path, "/datadata") == 0 || strcmp(path, "/cache") == 0) {
180 fatsize = "16";
181 }
182
183 // Just in case /data sector size needs to be altered
184 else if (strcmp(path, "/data") == 0 ) {
185 sectorsize = "1";
186 }
187
188 // dump 10KB of zeros to partition before format due to fat.format bug
Koushik Dutta88a233d2011-11-11 00:41:57 -0800189 char cmd[PATH_MAX];
190
191 sprintf(cmd, "/sbin/dd if=/dev/zero of=%s bs=4096 count=10", device);
192 if(__system(cmd)) {
193 printf("failure while zeroing rfs partition.\n");
Donovan Bartisha8f265d2011-11-10 12:55:22 -0600194 return -1;
195 }
196
197 // Run fat.format
Koushik Dutta88a233d2011-11-11 00:41:57 -0800198 sprintf(cmd, "/sbin/fat.format -F %s -S 4096 -s %s %s", fatsize, sectorsize, device);
199 if(__system(cmd)) {
Donovan Bartisha8f265d2011-11-10 12:55:22 -0600200 printf("failure while running fat.format\n");
201 return -1;
202 }
203
204 return 0;
205}