Koushik Dutta | 68df48c | 2010-09-13 15:04:54 -0700 | [diff] [blame] | 1 | #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 Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 23 | extern int __system(const char *command); |
Koushik Dutta | 02c3605 | 2010-12-12 02:52:44 -0800 | [diff] [blame] | 24 | #define BML_UNLOCK_ALL 0x8A29 ///< unlock all partition RO -> RW |
Koushik Dutta | 68df48c | 2010-09-13 15:04:54 -0700 | [diff] [blame] | 25 | |
Koushik Dutta | a75c067 | 2011-07-07 12:55:02 -0700 | [diff] [blame] | 26 | #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 Dutta | 03cf72a | 2010-12-21 15:14:21 -0800 | [diff] [blame] | 33 | |
Koushik Dutta | 134ead7 | 2010-12-12 16:54:41 -0800 | [diff] [blame] | 34 | static int restore_internal(const char* bml, const char* filename) |
Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 35 | { |
Koushik Dutta | 03a4f5b | 2010-12-12 12:24:02 -0800 | [diff] [blame] | 36 | 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 Dutta | 134ead7 | 2010-12-12 16:54:41 -0800 | [diff] [blame] | 59 | |
| 60 | close(dstfd); |
| 61 | close(srcfd); |
| 62 | |
Koushik Dutta | 03a4f5b | 2010-12-12 12:24:02 -0800 | [diff] [blame] | 63 | return 0; |
Koushik Dutta | 68df48c | 2010-09-13 15:04:54 -0700 | [diff] [blame] | 64 | } |
Koushik Dutta | 56c1b3b | 2010-09-13 15:08:49 -0700 | [diff] [blame] | 65 | |
Koushik Dutta | 134ead7 | 2010-12-12 16:54:41 -0800 | [diff] [blame] | 66 | int cmd_bml_restore_raw_partition(const char *partition, const char *filename) |
| 67 | { |
Koushik Dutta | 890b951 | 2011-10-29 21:38:53 -0700 | [diff] [blame] | 68 | if (strcmp(partition, "boot") != 0 && strcmp(partition, "recovery") != 0 && strcmp(partition, "recoveryonly") != 0 && partition[0] != '/') |
Koushik Dutta | 134ead7 | 2010-12-12 16:54:41 -0800 | [diff] [blame] | 69 | return 6; |
| 70 | |
Koushik Dutta | 195e06b | 2011-05-29 19:33:42 -0700 | [diff] [blame] | 71 | 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 Dutta | a75c067 | 2011-07-07 12:55:02 -0700 | [diff] [blame] | 76 | ret = restore_internal(BOARD_BML_BOOT, filename); |
Koushik Dutta | 195e06b | 2011-05-29 19:33:42 -0700 | [diff] [blame] | 77 | if (ret != 0) |
| 78 | return ret; |
| 79 | } |
Koushik Dutta | 134ead7 | 2010-12-12 16:54:41 -0800 | [diff] [blame] | 80 | |
Koushik Dutta | 195e06b | 2011-05-29 19:33:42 -0700 | [diff] [blame] | 81 | if (strcmp(partition, "recovery") == 0 || strcmp(partition, "recoveryonly") == 0) |
Koushik Dutta | a75c067 | 2011-07-07 12:55:02 -0700 | [diff] [blame] | 82 | ret = restore_internal(BOARD_BML_RECOVERY, filename); |
Koushik Dutta | e87f9e6 | 2011-07-12 18:07:45 -0700 | [diff] [blame] | 83 | |
| 84 | // support explicitly provided device paths |
| 85 | if (partition[0] == '/') |
| 86 | ret = restore_internal(partition, filename); |
Koushik Dutta | 134ead7 | 2010-12-12 16:54:41 -0800 | [diff] [blame] | 87 | return ret; |
| 88 | } |
| 89 | |
Koushik Dutta | c8ff793 | 2010-12-21 15:16:05 -0800 | [diff] [blame] | 90 | int cmd_bml_backup_raw_partition(const char *partition, const char *out_file) |
Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 91 | { |
Koushik Dutta | cdb433a | 2010-12-21 15:13:58 -0800 | [diff] [blame] | 92 | char* bml; |
| 93 | if (strcmp("boot", partition) == 0) |
Koushik Dutta | a75c067 | 2011-07-07 12:55:02 -0700 | [diff] [blame] | 94 | bml = BOARD_BML_BOOT; |
Koushik Dutta | cdb433a | 2010-12-21 15:13:58 -0800 | [diff] [blame] | 95 | else if (strcmp("recovery", partition) == 0) |
Koushik Dutta | a75c067 | 2011-07-07 12:55:02 -0700 | [diff] [blame] | 96 | bml = BOARD_BML_RECOVERY; |
Koushik Dutta | e87f9e6 | 2011-07-12 18:07:45 -0700 | [diff] [blame] | 97 | else if (partition[0] == '/') { |
| 98 | // support explicitly provided device paths |
| 99 | bml = partition; |
| 100 | } |
Koushik Dutta | cdb433a | 2010-12-21 15:13:58 -0800 | [diff] [blame] | 101 | 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; |
| 146 | ERROR1: |
| 147 | fclose ( out ); |
| 148 | ERROR2: |
| 149 | fclose ( in ); |
| 150 | ERROR3: |
| 151 | return ret; |
Koushik Dutta | 56c1b3b | 2010-09-13 15:08:49 -0700 | [diff] [blame] | 152 | } |
Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 153 | |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 154 | int cmd_bml_erase_raw_partition(const char *partition) |
Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 155 | { |
| 156 | // TODO: implement raw wipe |
| 157 | return 0; |
| 158 | } |
| 159 | |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 160 | int cmd_bml_erase_partition(const char *partition, const char *filesystem) |
Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 161 | { |
| 162 | return -1; |
| 163 | } |
| 164 | |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 165 | int cmd_bml_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only) |
Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 166 | { |
| 167 | return -1; |
| 168 | } |
| 169 | |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 170 | int cmd_bml_get_partition_device(const char *partition, char *device) |
Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 171 | { |
| 172 | return -1; |
| 173 | } |
Donovan Bartish | a8f265d | 2011-11-10 12:55:22 -0600 | [diff] [blame] | 174 | |
Donovan Bartish | a8f265d | 2011-11-10 12:55:22 -0600 | [diff] [blame] | 175 | int 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 Dutta | 88a233d | 2011-11-11 00:41:57 -0800 | [diff] [blame] | 189 | 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 Bartish | a8f265d | 2011-11-10 12:55:22 -0600 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | // Run fat.format |
Koushik Dutta | 88a233d | 2011-11-11 00:41:57 -0800 | [diff] [blame] | 198 | sprintf(cmd, "/sbin/fat.format -F %s -S 4096 -s %s %s", fatsize, sectorsize, device); |
| 199 | if(__system(cmd)) { |
Donovan Bartish | a8f265d | 2011-11-10 12:55:22 -0600 | [diff] [blame] | 200 | printf("failure while running fat.format\n"); |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |