blob: 609ab86740ec84bd42a83f9e6b485dc158e569f6 [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
Evan McClain8ba9a632012-05-18 01:28:26 -040023#include <common.h>
24
Koushik Dutta02c36052010-12-12 02:52:44 -080025#define BML_UNLOCK_ALL 0x8A29 ///< unlock all partition RO -> RW
Koushik Dutta68df48c2010-09-13 15:04:54 -070026
Koushik Duttaa75c0672011-07-07 12:55:02 -070027#ifndef BOARD_BML_BOOT
28#define BOARD_BML_BOOT "/dev/block/bml7"
29#endif
30
31#ifndef BOARD_BML_RECOVERY
32#define BOARD_BML_RECOVERY "/dev/block/bml8"
33#endif
Koushik Dutta03cf72a2010-12-21 15:14:21 -080034
Koushik Dutta134ead72010-12-12 16:54:41 -080035static int restore_internal(const char* bml, const char* filename)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080036{
Koushik Dutta03a4f5b2010-12-12 12:24:02 -080037 char buf[4096];
38 int dstfd, srcfd, bytes_read, bytes_written, total_read = 0;
39 if (filename == NULL)
40 srcfd = 0;
41 else {
42 srcfd = open(filename, O_RDONLY | O_LARGEFILE);
43 if (srcfd < 0)
44 return 2;
45 }
46 dstfd = open(bml, O_RDWR | O_LARGEFILE);
47 if (dstfd < 0)
48 return 3;
49 if (ioctl(dstfd, BML_UNLOCK_ALL, 0))
50 return 4;
51 do {
52 total_read += bytes_read = read(srcfd, buf, 4096);
53 if (!bytes_read)
54 break;
55 if (bytes_read < 4096)
56 memset(&buf[bytes_read], 0, 4096 - bytes_read);
57 if (write(dstfd, buf, 4096) < 4096)
58 return 5;
59 } while(bytes_read == 4096);
Koushik Dutta134ead72010-12-12 16:54:41 -080060
61 close(dstfd);
62 close(srcfd);
63
Koushik Dutta03a4f5b2010-12-12 12:24:02 -080064 return 0;
Koushik Dutta68df48c2010-09-13 15:04:54 -070065}
Koushik Dutta56c1b3b2010-09-13 15:08:49 -070066
Koushik Dutta134ead72010-12-12 16:54:41 -080067int cmd_bml_restore_raw_partition(const char *partition, const char *filename)
68{
Koushik Dutta890b9512011-10-29 21:38:53 -070069 if (strcmp(partition, "boot") != 0 && strcmp(partition, "recovery") != 0 && strcmp(partition, "recoveryonly") != 0 && partition[0] != '/')
Koushik Dutta134ead72010-12-12 16:54:41 -080070 return 6;
71
Koushik Dutta195e06b2011-05-29 19:33:42 -070072 int ret = -1;
73 if (strcmp(partition, "recoveryonly") != 0) {
74 // always restore boot, regardless of whether recovery or boot is flashed.
75 // this is because boot and recovery are the same on some samsung phones.
76 // unless of course, recoveryonly is explictly chosen (bml8)
Koushik Duttaa75c0672011-07-07 12:55:02 -070077 ret = restore_internal(BOARD_BML_BOOT, filename);
Koushik Dutta195e06b2011-05-29 19:33:42 -070078 if (ret != 0)
79 return ret;
80 }
Koushik Dutta134ead72010-12-12 16:54:41 -080081
Koushik Dutta195e06b2011-05-29 19:33:42 -070082 if (strcmp(partition, "recovery") == 0 || strcmp(partition, "recoveryonly") == 0)
Koushik Duttaa75c0672011-07-07 12:55:02 -070083 ret = restore_internal(BOARD_BML_RECOVERY, filename);
Koushik Duttae87f9e62011-07-12 18:07:45 -070084
85 // support explicitly provided device paths
86 if (partition[0] == '/')
87 ret = restore_internal(partition, filename);
Koushik Dutta134ead72010-12-12 16:54:41 -080088 return ret;
89}
90
Koushik Duttac8ff7932010-12-21 15:16:05 -080091int cmd_bml_backup_raw_partition(const char *partition, const char *out_file)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -080092{
Koushik Duttacdb433a2010-12-21 15:13:58 -080093 char* bml;
94 if (strcmp("boot", partition) == 0)
Koushik Duttaa75c0672011-07-07 12:55:02 -070095 bml = BOARD_BML_BOOT;
Koushik Duttacdb433a2010-12-21 15:13:58 -080096 else if (strcmp("recovery", partition) == 0)
Koushik Duttaa75c0672011-07-07 12:55:02 -070097 bml = BOARD_BML_RECOVERY;
Koushik Duttae87f9e62011-07-12 18:07:45 -070098 else if (partition[0] == '/') {
99 // support explicitly provided device paths
100 bml = partition;
101 }
Koushik Duttacdb433a2010-12-21 15:13:58 -0800102 else {
103 printf("Invalid partition.\n");
104 return -1;
105 }
106
107 int ch;
108 FILE *in;
109 FILE *out;
110 int val = 0;
111 char buf[512];
112 unsigned sz = 0;
113 unsigned i;
114 int ret = -1;
115 char *in_file = bml;
116
117 in = fopen ( in_file, "r" );
118 if (in == NULL)
119 goto ERROR3;
120
121 out = fopen ( out_file, "w" );
122 if (out == NULL)
123 goto ERROR2;
124
125 fseek(in, 0L, SEEK_END);
126 sz = ftell(in);
127 fseek(in, 0L, SEEK_SET);
128
129 if (sz % 512)
130 {
131 while ( ( ch = fgetc ( in ) ) != EOF )
132 fputc ( ch, out );
133 }
134 else
135 {
136 for (i=0; i< (sz/512); i++)
137 {
138 if ((fread(buf, 512, 1, in)) != 1)
139 goto ERROR1;
140 if ((fwrite(buf, 512, 1, out)) != 1)
141 goto ERROR1;
142 }
143 }
144
145 fsync(out);
146 ret = 0;
147ERROR1:
148 fclose ( out );
149ERROR2:
150 fclose ( in );
151ERROR3:
152 return ret;
Koushik Dutta56c1b3b2010-09-13 15:08:49 -0700153}
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800154
Steve Kondik4123b582010-11-14 03:18:40 -0500155int cmd_bml_erase_raw_partition(const char *partition)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800156{
157 // TODO: implement raw wipe
158 return 0;
159}
160
Steve Kondik4123b582010-11-14 03:18:40 -0500161int cmd_bml_erase_partition(const char *partition, const char *filesystem)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800162{
163 return -1;
164}
165
Steve Kondik4123b582010-11-14 03:18:40 -0500166int cmd_bml_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800167{
168 return -1;
169}
170
Steve Kondik4123b582010-11-14 03:18:40 -0500171int cmd_bml_get_partition_device(const char *partition, char *device)
Koushik Duttae5c7e0e2010-11-11 01:19:10 -0800172{
173 return -1;
174}
Donovan Bartisha8f265d2011-11-10 12:55:22 -0600175
Donovan Bartisha8f265d2011-11-10 12:55:22 -0600176int format_rfs_device (const char *device, const char *path) {
177 const char *fatsize = "32";
178 const char *sectorsize = "1";
179
180 if (strcmp(path, "/datadata") == 0 || strcmp(path, "/cache") == 0) {
181 fatsize = "16";
182 }
183
184 // Just in case /data sector size needs to be altered
185 else if (strcmp(path, "/data") == 0 ) {
186 sectorsize = "1";
187 }
188
189 // dump 10KB of zeros to partition before format due to fat.format bug
Koushik Dutta88a233d2011-11-11 00:41:57 -0800190 char cmd[PATH_MAX];
191
192 sprintf(cmd, "/sbin/dd if=/dev/zero of=%s bs=4096 count=10", device);
193 if(__system(cmd)) {
194 printf("failure while zeroing rfs partition.\n");
Donovan Bartisha8f265d2011-11-10 12:55:22 -0600195 return -1;
196 }
197
198 // Run fat.format
Koushik Dutta88a233d2011-11-11 00:41:57 -0800199 sprintf(cmd, "/sbin/fat.format -F %s -S 4096 -s %s %s", fatsize, sectorsize, device);
200 if(__system(cmd)) {
Donovan Bartisha8f265d2011-11-10 12:55:22 -0600201 printf("failure while running fat.format\n");
202 return -1;
203 }
204
205 return 0;
206}