| 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 | 134ead7 | 2010-12-12 16:54:41 -0800 | [diff] [blame] | 26 | static int restore_internal(const char* bml, const char* filename) | 
| Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 27 | { | 
| Koushik Dutta | 03a4f5b | 2010-12-12 12:24:02 -0800 | [diff] [blame] | 28 |     char buf[4096]; | 
 | 29 |     int dstfd, srcfd, bytes_read, bytes_written, total_read = 0; | 
 | 30 |     if (filename == NULL) | 
 | 31 |         srcfd = 0; | 
 | 32 |     else { | 
 | 33 |         srcfd = open(filename, O_RDONLY | O_LARGEFILE); | 
 | 34 |         if (srcfd < 0) | 
 | 35 |             return 2; | 
 | 36 |     } | 
 | 37 |     dstfd = open(bml, O_RDWR | O_LARGEFILE); | 
 | 38 |     if (dstfd < 0) | 
 | 39 |         return 3; | 
 | 40 |     if (ioctl(dstfd, BML_UNLOCK_ALL, 0)) | 
 | 41 |         return 4; | 
 | 42 |     do { | 
 | 43 |         total_read += bytes_read = read(srcfd, buf, 4096); | 
 | 44 |         if (!bytes_read) | 
 | 45 |             break; | 
 | 46 |         if (bytes_read < 4096) | 
 | 47 |             memset(&buf[bytes_read], 0, 4096 - bytes_read); | 
 | 48 |         if (write(dstfd, buf, 4096) < 4096) | 
 | 49 |             return 5; | 
 | 50 |     } while(bytes_read == 4096); | 
| Koushik Dutta | 134ead7 | 2010-12-12 16:54:41 -0800 | [diff] [blame] | 51 |      | 
 | 52 |     close(dstfd); | 
 | 53 |     close(srcfd); | 
 | 54 |      | 
| Koushik Dutta | 03a4f5b | 2010-12-12 12:24:02 -0800 | [diff] [blame] | 55 |     return 0; | 
| Koushik Dutta | 68df48c | 2010-09-13 15:04:54 -0700 | [diff] [blame] | 56 | } | 
| Koushik Dutta | 56c1b3b | 2010-09-13 15:08:49 -0700 | [diff] [blame] | 57 |  | 
| Koushik Dutta | 134ead7 | 2010-12-12 16:54:41 -0800 | [diff] [blame] | 58 | int cmd_bml_restore_raw_partition(const char *partition, const char *filename) | 
 | 59 | { | 
 | 60 |     char *bml; | 
 | 61 |     if (strcmp(partition, "boot") == 0 || strcmp(partition, "recovery") == 0) | 
 | 62 |         bml = "/dev/block/bml7"; | 
 | 63 |     else | 
 | 64 |         return 6; | 
 | 65 |  | 
 | 66 |     int ret = restore_internal("/dev/block/bml7", filename); | 
 | 67 |     if (ret != 0) | 
 | 68 |         return ret; | 
 | 69 |  | 
 | 70 |     ret = restore_internal("/dev/block/bml8", filename); | 
 | 71 |     return ret; | 
 | 72 | } | 
 | 73 |  | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 74 | int cmd_bml_backup_raw_partition(const char *partition, const char *filename) | 
| Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 75 | { | 
| Koushik Dutta | cdb433a | 2010-12-21 15:13:58 -0800 | [diff] [blame^] | 76 |     char* bml; | 
 | 77 |     if (strcmp("boot", partition) == 0) | 
 | 78 |         bml = "/dev/block/bml7"; | 
 | 79 |     else if (strcmp("recovery", partition) == 0) | 
 | 80 |         bml = "/dev/block/bml8"; | 
 | 81 |     else { | 
 | 82 |         printf("Invalid partition.\n"); | 
 | 83 |         return -1; | 
 | 84 |     } | 
 | 85 |  | 
 | 86 |     int ch; | 
 | 87 |     FILE *in; | 
 | 88 |     FILE *out; | 
 | 89 |     int val = 0; | 
 | 90 |     char buf[512]; | 
 | 91 |     unsigned sz = 0; | 
 | 92 |     unsigned i; | 
 | 93 |     int ret = -1; | 
 | 94 |     char *in_file = bml; | 
 | 95 |  | 
 | 96 |     in  = fopen ( in_file,  "r" ); | 
 | 97 |     if (in == NULL) | 
 | 98 |         goto ERROR3; | 
 | 99 |  | 
 | 100 |     out = fopen ( out_file,  "w" ); | 
 | 101 |     if (out == NULL) | 
 | 102 |         goto ERROR2; | 
 | 103 |  | 
 | 104 |     fseek(in, 0L, SEEK_END); | 
 | 105 |     sz = ftell(in); | 
 | 106 |     fseek(in, 0L, SEEK_SET); | 
 | 107 |  | 
 | 108 |     if (sz % 512) | 
 | 109 |     { | 
 | 110 |         while ( ( ch = fgetc ( in ) ) != EOF ) | 
 | 111 |             fputc ( ch, out ); | 
 | 112 |     } | 
 | 113 |     else | 
 | 114 |     { | 
 | 115 |         for (i=0; i< (sz/512); i++) | 
 | 116 |         { | 
 | 117 |             if ((fread(buf, 512, 1, in)) != 1) | 
 | 118 |                 goto ERROR1; | 
 | 119 |             if ((fwrite(buf, 512, 1, out)) != 1) | 
 | 120 |                 goto ERROR1; | 
 | 121 |         } | 
 | 122 |     } | 
 | 123 |  | 
 | 124 |     fsync(out); | 
 | 125 |     ret = 0; | 
 | 126 | ERROR1: | 
 | 127 |     fclose ( out ); | 
 | 128 | ERROR2: | 
 | 129 |     fclose ( in ); | 
 | 130 | ERROR3: | 
 | 131 |     return ret; | 
| Koushik Dutta | 56c1b3b | 2010-09-13 15:08:49 -0700 | [diff] [blame] | 132 | } | 
| Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 133 |  | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 134 | int cmd_bml_erase_raw_partition(const char *partition) | 
| Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 135 | { | 
 | 136 |     // TODO: implement raw wipe | 
 | 137 |     return 0; | 
 | 138 | } | 
 | 139 |  | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 140 | int cmd_bml_erase_partition(const char *partition, const char *filesystem) | 
| Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 141 | { | 
 | 142 |     return -1; | 
 | 143 | } | 
 | 144 |  | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 145 | 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] | 146 | { | 
 | 147 |     return -1; | 
 | 148 | } | 
 | 149 |  | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 150 | int cmd_bml_get_partition_device(const char *partition, char *device) | 
| Koushik Dutta | e5c7e0e | 2010-11-11 01:19:10 -0800 | [diff] [blame] | 151 | { | 
 | 152 |     return -1; | 
 | 153 | } |