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 | |
| 98 | printf("partitionType: %s\n", partitionType); |
| 99 | printf("partition: %s\n", partition); |
| 100 | printf("detected type: %d\n", type); |
Koushik Dutta | 67fa0c3 | 2011-03-17 11:37:21 -0700 | [diff] [blame] | 101 | return type; |
| 102 | } |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 103 | 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] | 104 | { |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 105 | int type = detect_partition(partitionType, partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 106 | switch (type) { |
| 107 | case MTD: |
| 108 | return cmd_mtd_restore_raw_partition(partition, filename); |
| 109 | case MMC: |
| 110 | return cmd_mmc_restore_raw_partition(partition, filename); |
| 111 | case BML: |
| 112 | return cmd_bml_restore_raw_partition(partition, filename); |
| 113 | default: |
| 114 | return -1; |
| 115 | } |
| 116 | } |
| 117 | |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 118 | 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] | 119 | { |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 120 | int type = detect_partition(partitionType, partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 121 | switch (type) { |
| 122 | case MTD: |
| 123 | return cmd_mtd_backup_raw_partition(partition, filename); |
| 124 | case MMC: |
| 125 | return cmd_mmc_backup_raw_partition(partition, filename); |
| 126 | case BML: |
| 127 | return cmd_bml_backup_raw_partition(partition, filename); |
| 128 | default: |
Koushik Dutta | f573510 | 2011-04-22 12:12:32 -0700 | [diff] [blame] | 129 | printf("unable to detect device type"); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 130 | return -1; |
| 131 | } |
| 132 | } |
| 133 | |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 134 | int erase_raw_partition(const char* partitionType, const char *partition) |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 135 | { |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 136 | int type = detect_partition(partitionType, partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 137 | switch (type) { |
| 138 | case MTD: |
| 139 | return cmd_mtd_erase_raw_partition(partition); |
| 140 | case MMC: |
| 141 | return cmd_mmc_erase_raw_partition(partition); |
| 142 | case BML: |
| 143 | return cmd_bml_erase_raw_partition(partition); |
| 144 | default: |
| 145 | return -1; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | int erase_partition(const char *partition, const char *filesystem) |
| 150 | { |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 151 | int type = detect_partition(NULL, partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 152 | switch (type) { |
| 153 | case MTD: |
| 154 | return cmd_mtd_erase_partition(partition, filesystem); |
| 155 | case MMC: |
| 156 | return cmd_mmc_erase_partition(partition, filesystem); |
| 157 | case BML: |
| 158 | return cmd_bml_erase_partition(partition, filesystem); |
| 159 | default: |
| 160 | return -1; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only) |
| 165 | { |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 166 | int type = detect_partition(NULL, partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 167 | switch (type) { |
| 168 | case MTD: |
| 169 | return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only); |
| 170 | case MMC: |
| 171 | return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only); |
| 172 | case BML: |
| 173 | return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only); |
| 174 | default: |
| 175 | return -1; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | int get_partition_device(const char *partition, char *device) |
| 180 | { |
| 181 | int type = device_flash_type(); |
| 182 | switch (type) { |
| 183 | case MTD: |
| 184 | return cmd_mtd_get_partition_device(partition, device); |
| 185 | case MMC: |
| 186 | return cmd_mmc_get_partition_device(partition, device); |
| 187 | case BML: |
| 188 | return cmd_bml_get_partition_device(partition, device); |
| 189 | default: |
| 190 | return -1; |
| 191 | } |
| 192 | } |