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 | fdd4766 | 2011-04-17 00:49:15 -0700 | [diff] [blame] | 72 | static int detect_partition(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 | f573510 | 2011-04-22 12:12:32 -0700 | [diff] [blame^] | 81 | |
Koushik Dutta | 67fa0c3 | 2011-03-17 11:37:21 -0700 | [diff] [blame] | 82 | return type; |
| 83 | } |
Koushik Dutta | fdd4766 | 2011-04-17 00:49:15 -0700 | [diff] [blame] | 84 | int restore_raw_partition(const char *partition, const char *filename) |
Koushik Dutta | 67fa0c3 | 2011-03-17 11:37:21 -0700 | [diff] [blame] | 85 | { |
Koushik Dutta | fdd4766 | 2011-04-17 00:49:15 -0700 | [diff] [blame] | 86 | int type = detect_partition(partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 87 | switch (type) { |
| 88 | case MTD: |
| 89 | return cmd_mtd_restore_raw_partition(partition, filename); |
| 90 | case MMC: |
| 91 | return cmd_mmc_restore_raw_partition(partition, filename); |
| 92 | case BML: |
| 93 | return cmd_bml_restore_raw_partition(partition, filename); |
| 94 | default: |
| 95 | return -1; |
| 96 | } |
| 97 | } |
| 98 | |
Koushik Dutta | fdd4766 | 2011-04-17 00:49:15 -0700 | [diff] [blame] | 99 | int backup_raw_partition(const char *partition, const char *filename) |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 100 | { |
Koushik Dutta | fdd4766 | 2011-04-17 00:49:15 -0700 | [diff] [blame] | 101 | int type = detect_partition(partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 102 | switch (type) { |
| 103 | case MTD: |
| 104 | return cmd_mtd_backup_raw_partition(partition, filename); |
| 105 | case MMC: |
| 106 | return cmd_mmc_backup_raw_partition(partition, filename); |
| 107 | case BML: |
| 108 | return cmd_bml_backup_raw_partition(partition, filename); |
| 109 | default: |
Koushik Dutta | f573510 | 2011-04-22 12:12:32 -0700 | [diff] [blame^] | 110 | printf("unable to detect device type"); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 111 | return -1; |
| 112 | } |
| 113 | } |
| 114 | |
Koushik Dutta | fdd4766 | 2011-04-17 00:49:15 -0700 | [diff] [blame] | 115 | int erase_raw_partition(const char *partition) |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 116 | { |
Koushik Dutta | fdd4766 | 2011-04-17 00:49:15 -0700 | [diff] [blame] | 117 | int type = detect_partition(partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 118 | switch (type) { |
| 119 | case MTD: |
| 120 | return cmd_mtd_erase_raw_partition(partition); |
| 121 | case MMC: |
| 122 | return cmd_mmc_erase_raw_partition(partition); |
| 123 | case BML: |
| 124 | return cmd_bml_erase_raw_partition(partition); |
| 125 | default: |
| 126 | return -1; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | int erase_partition(const char *partition, const char *filesystem) |
| 131 | { |
Koushik Dutta | fdd4766 | 2011-04-17 00:49:15 -0700 | [diff] [blame] | 132 | int type = detect_partition(partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 133 | switch (type) { |
| 134 | case MTD: |
| 135 | return cmd_mtd_erase_partition(partition, filesystem); |
| 136 | case MMC: |
| 137 | return cmd_mmc_erase_partition(partition, filesystem); |
| 138 | case BML: |
| 139 | return cmd_bml_erase_partition(partition, filesystem); |
| 140 | default: |
| 141 | return -1; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only) |
| 146 | { |
Koushik Dutta | fdd4766 | 2011-04-17 00:49:15 -0700 | [diff] [blame] | 147 | int type = detect_partition(partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 148 | switch (type) { |
| 149 | case MTD: |
| 150 | return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only); |
| 151 | case MMC: |
| 152 | return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only); |
| 153 | case BML: |
| 154 | return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only); |
| 155 | default: |
| 156 | return -1; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | int get_partition_device(const char *partition, char *device) |
| 161 | { |
| 162 | int type = device_flash_type(); |
| 163 | switch (type) { |
| 164 | case MTD: |
| 165 | return cmd_mtd_get_partition_device(partition, device); |
| 166 | case MMC: |
| 167 | return cmd_mmc_get_partition_device(partition, device); |
| 168 | case BML: |
| 169 | return cmd_bml_get_partition_device(partition, device); |
| 170 | default: |
| 171 | return -1; |
| 172 | } |
| 173 | } |