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