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