| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 1 | #include <signal.h> | 
 | 2 | #include <stdlib.h> | 
 | 3 | #include <unistd.h> | 
 | 4 | #include <sys/wait.h> | 
| Koushik Dutta | 7adeadc | 2011-05-26 11:47:56 -0700 | [diff] [blame] | 5 | #include <stdio.h> | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 6 |  | 
 | 7 | #include "flashutils/flashutils.h" | 
 | 8 |  | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 9 | int the_flash_type = UNKNOWN; | 
 | 10 |  | 
 | 11 | int device_flash_type() | 
 | 12 | { | 
 | 13 |     if (the_flash_type == UNKNOWN) { | 
| Koushik Dutta | f573510 | 2011-04-22 12:12:32 -0700 | [diff] [blame] | 14 |         if (access("/dev/block/bml7", F_OK) == 0) { | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 15 |             the_flash_type = BML; | 
 | 16 |         } else if (access("/proc/emmc", F_OK) == 0) { | 
 | 17 |             the_flash_type = MMC; | 
 | 18 |         } else if (access("/proc/mtd", F_OK) == 0) { | 
 | 19 |             the_flash_type = MTD; | 
 | 20 |         } else { | 
 | 21 |             the_flash_type = UNSUPPORTED; | 
 | 22 |         } | 
 | 23 |     } | 
 | 24 |     return the_flash_type; | 
 | 25 | } | 
 | 26 |  | 
 | 27 | char* get_default_filesystem() | 
 | 28 | { | 
 | 29 |     return device_flash_type() == MMC ? "ext3" : "yaffs2"; | 
 | 30 | } | 
 | 31 |  | 
 | 32 | // This was pulled from bionic: The default system command always looks | 
 | 33 | // for shell in /system/bin/sh. This is bad. | 
 | 34 | #define _PATH_BSHELL "/sbin/sh" | 
 | 35 |  | 
 | 36 | extern char **environ; | 
 | 37 | int | 
 | 38 | __system(const char *command) | 
 | 39 | { | 
 | 40 |   pid_t pid; | 
 | 41 |     sig_t intsave, quitsave; | 
 | 42 |     sigset_t mask, omask; | 
 | 43 |     int pstat; | 
 | 44 |     char *argp[] = {"sh", "-c", NULL, NULL}; | 
 | 45 |  | 
 | 46 |     if (!command)        /* just checking... */ | 
 | 47 |         return(1); | 
 | 48 |  | 
 | 49 |     argp[2] = (char *)command; | 
 | 50 |  | 
 | 51 |     sigemptyset(&mask); | 
 | 52 |     sigaddset(&mask, SIGCHLD); | 
 | 53 |     sigprocmask(SIG_BLOCK, &mask, &omask); | 
 | 54 |     switch (pid = vfork()) { | 
 | 55 |     case -1:            /* error */ | 
 | 56 |         sigprocmask(SIG_SETMASK, &omask, NULL); | 
 | 57 |         return(-1); | 
 | 58 |     case 0:                /* child */ | 
 | 59 |         sigprocmask(SIG_SETMASK, &omask, NULL); | 
 | 60 |         execve(_PATH_BSHELL, argp, environ); | 
 | 61 |     _exit(127); | 
 | 62 |   } | 
 | 63 |  | 
 | 64 |     intsave = (sig_t)  bsd_signal(SIGINT, SIG_IGN); | 
 | 65 |     quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN); | 
 | 66 |     pid = waitpid(pid, (int *)&pstat, 0); | 
 | 67 |     sigprocmask(SIG_SETMASK, &omask, NULL); | 
 | 68 |     (void)bsd_signal(SIGINT, intsave); | 
 | 69 |     (void)bsd_signal(SIGQUIT, quitsave); | 
 | 70 |     return (pid == -1 ? -1 : pstat); | 
 | 71 | } | 
 | 72 |  | 
| Koushik Dutta | 4187347 | 2011-05-26 17:30:43 -0700 | [diff] [blame] | 73 | int get_flash_type(const char* partitionType) { | 
 | 74 |     int type = UNSUPPORTED; | 
 | 75 |     if (strcmp(partitionType, "mtd") == 0) | 
 | 76 |         type = MTD; | 
 | 77 |     else if (strcmp(partitionType, "emmc") == 0) | 
 | 78 |         type = MMC; | 
 | 79 |     else if (strcmp(partitionType, "bml") == 0) | 
 | 80 |         type = BML; | 
 | 81 |     return type; | 
 | 82 | } | 
 | 83 |  | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 84 | static int detect_partition(const char *partitionType, const char *partition) | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 85 | { | 
 | 86 |     int type = device_flash_type(); | 
| Koushik Dutta | 67fa0c3 | 2011-03-17 11:37:21 -0700 | [diff] [blame] | 87 |     if (strstr(partition, "/dev/block/mtd") != NULL) | 
 | 88 |         type = MTD; | 
 | 89 |     else if (strstr(partition, "/dev/block/mmc") != NULL) | 
 | 90 |         type = MMC; | 
 | 91 |     else if (strstr(partition, "/dev/block/bml") != NULL) | 
 | 92 |         type = BML; | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 93 |  | 
 | 94 |     if (partitionType != NULL) { | 
| Koushik Dutta | 4187347 | 2011-05-26 17:30:43 -0700 | [diff] [blame] | 95 |         type = get_flash_type(partitionType); | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 96 |     } | 
 | 97 |  | 
| Koushik Dutta | 01143a5 | 2011-05-30 15:22:04 -0700 | [diff] [blame] | 98 |     return type; | 
| Koushik Dutta | 67fa0c3 | 2011-03-17 11:37:21 -0700 | [diff] [blame] | 99 | } | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 100 | int restore_raw_partition(const char* partitionType, const char *partition, const char *filename) | 
| Koushik Dutta | 67fa0c3 | 2011-03-17 11:37:21 -0700 | [diff] [blame] | 101 | { | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 102 |     int type = detect_partition(partitionType, partition); | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 103 |     switch (type) { | 
 | 104 |         case MTD: | 
 | 105 |             return cmd_mtd_restore_raw_partition(partition, filename); | 
 | 106 |         case MMC: | 
 | 107 |             return cmd_mmc_restore_raw_partition(partition, filename); | 
 | 108 |         case BML: | 
 | 109 |             return cmd_bml_restore_raw_partition(partition, filename); | 
 | 110 |         default: | 
 | 111 |             return -1; | 
 | 112 |     } | 
 | 113 | } | 
 | 114 |  | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 115 | int backup_raw_partition(const char* partitionType, const char *partition, const char *filename) | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 116 | { | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 117 |     int type = detect_partition(partitionType, partition); | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 118 |     switch (type) { | 
 | 119 |         case MTD: | 
 | 120 |             return cmd_mtd_backup_raw_partition(partition, filename); | 
 | 121 |         case MMC: | 
 | 122 |             return cmd_mmc_backup_raw_partition(partition, filename); | 
 | 123 |         case BML: | 
 | 124 |             return cmd_bml_backup_raw_partition(partition, filename); | 
 | 125 |         default: | 
| Koushik Dutta | f573510 | 2011-04-22 12:12:32 -0700 | [diff] [blame] | 126 |             printf("unable to detect device type"); | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 127 |             return -1; | 
 | 128 |     } | 
 | 129 | } | 
 | 130 |  | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 131 | int erase_raw_partition(const char* partitionType, const char *partition) | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 132 | { | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 133 |     int type = detect_partition(partitionType, partition); | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 134 |     switch (type) { | 
 | 135 |         case MTD: | 
 | 136 |             return cmd_mtd_erase_raw_partition(partition); | 
 | 137 |         case MMC: | 
 | 138 |             return cmd_mmc_erase_raw_partition(partition); | 
 | 139 |         case BML: | 
 | 140 |             return cmd_bml_erase_raw_partition(partition); | 
 | 141 |         default: | 
 | 142 |             return -1; | 
 | 143 |     } | 
 | 144 | } | 
 | 145 |  | 
 | 146 | int erase_partition(const char *partition, const char *filesystem) | 
 | 147 | { | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 148 |     int type = detect_partition(NULL, partition); | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 149 |     switch (type) { | 
 | 150 |         case MTD: | 
 | 151 |             return cmd_mtd_erase_partition(partition, filesystem); | 
 | 152 |         case MMC: | 
 | 153 |             return cmd_mmc_erase_partition(partition, filesystem); | 
 | 154 |         case BML: | 
 | 155 |             return cmd_bml_erase_partition(partition, filesystem); | 
 | 156 |         default: | 
 | 157 |             return -1; | 
 | 158 |     } | 
 | 159 | } | 
 | 160 |  | 
 | 161 | int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only) | 
 | 162 | { | 
| Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 163 |     int type = detect_partition(NULL, partition); | 
| Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 164 |     switch (type) { | 
 | 165 |         case MTD: | 
 | 166 |             return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only); | 
 | 167 |         case MMC: | 
 | 168 |             return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only); | 
 | 169 |         case BML: | 
 | 170 |             return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only); | 
 | 171 |         default: | 
 | 172 |             return -1; | 
 | 173 |     } | 
 | 174 | } | 
 | 175 |  | 
 | 176 | int get_partition_device(const char *partition, char *device) | 
 | 177 | { | 
 | 178 |     int type = device_flash_type(); | 
 | 179 |     switch (type) { | 
 | 180 |         case MTD: | 
 | 181 |             return cmd_mtd_get_partition_device(partition, device); | 
 | 182 |         case MMC: | 
 | 183 |             return cmd_mmc_get_partition_device(partition, device); | 
 | 184 |         case BML: | 
 | 185 |             return cmd_bml_get_partition_device(partition, device); | 
 | 186 |         default: | 
 | 187 |             return -1; | 
 | 188 |     } | 
 | 189 | } |