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