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 | |
Koushik Dutta | 4187347 | 2011-05-26 17:30:43 -0700 | [diff] [blame] | 32 | int get_flash_type(const char* partitionType) { |
| 33 | int type = UNSUPPORTED; |
| 34 | if (strcmp(partitionType, "mtd") == 0) |
| 35 | type = MTD; |
| 36 | else if (strcmp(partitionType, "emmc") == 0) |
| 37 | type = MMC; |
| 38 | else if (strcmp(partitionType, "bml") == 0) |
| 39 | type = BML; |
| 40 | return type; |
| 41 | } |
| 42 | |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 43 | static int detect_partition(const char *partitionType, const char *partition) |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 44 | { |
| 45 | int type = device_flash_type(); |
Koushik Dutta | 67fa0c3 | 2011-03-17 11:37:21 -0700 | [diff] [blame] | 46 | if (strstr(partition, "/dev/block/mtd") != NULL) |
| 47 | type = MTD; |
| 48 | else if (strstr(partition, "/dev/block/mmc") != NULL) |
| 49 | type = MMC; |
| 50 | else if (strstr(partition, "/dev/block/bml") != NULL) |
| 51 | type = BML; |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 52 | |
| 53 | if (partitionType != NULL) { |
Koushik Dutta | 4187347 | 2011-05-26 17:30:43 -0700 | [diff] [blame] | 54 | type = get_flash_type(partitionType); |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Koushik Dutta | 01143a5 | 2011-05-30 15:22:04 -0700 | [diff] [blame] | 57 | return type; |
Koushik Dutta | 67fa0c3 | 2011-03-17 11:37:21 -0700 | [diff] [blame] | 58 | } |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 59 | 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] | 60 | { |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 61 | int type = detect_partition(partitionType, partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 62 | switch (type) { |
| 63 | case MTD: |
| 64 | return cmd_mtd_restore_raw_partition(partition, filename); |
| 65 | case MMC: |
| 66 | return cmd_mmc_restore_raw_partition(partition, filename); |
| 67 | case BML: |
| 68 | return cmd_bml_restore_raw_partition(partition, filename); |
| 69 | default: |
| 70 | return -1; |
| 71 | } |
| 72 | } |
| 73 | |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 74 | 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] | 75 | { |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 76 | int type = detect_partition(partitionType, partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 77 | switch (type) { |
| 78 | case MTD: |
| 79 | return cmd_mtd_backup_raw_partition(partition, filename); |
| 80 | case MMC: |
| 81 | return cmd_mmc_backup_raw_partition(partition, filename); |
| 82 | case BML: |
| 83 | return cmd_bml_backup_raw_partition(partition, filename); |
| 84 | default: |
Koushik Dutta | f573510 | 2011-04-22 12:12:32 -0700 | [diff] [blame] | 85 | printf("unable to detect device type"); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 86 | return -1; |
| 87 | } |
| 88 | } |
| 89 | |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 90 | int erase_raw_partition(const char* partitionType, const char *partition) |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 91 | { |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 92 | int type = detect_partition(partitionType, partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 93 | switch (type) { |
| 94 | case MTD: |
| 95 | return cmd_mtd_erase_raw_partition(partition); |
| 96 | case MMC: |
| 97 | return cmd_mmc_erase_raw_partition(partition); |
| 98 | case BML: |
| 99 | return cmd_bml_erase_raw_partition(partition); |
| 100 | default: |
| 101 | return -1; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | int erase_partition(const char *partition, const char *filesystem) |
| 106 | { |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 107 | int type = detect_partition(NULL, partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 108 | switch (type) { |
| 109 | case MTD: |
| 110 | return cmd_mtd_erase_partition(partition, filesystem); |
| 111 | case MMC: |
| 112 | return cmd_mmc_erase_partition(partition, filesystem); |
| 113 | case BML: |
| 114 | return cmd_bml_erase_partition(partition, filesystem); |
| 115 | default: |
| 116 | return -1; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only) |
| 121 | { |
Koushik Dutta | 8a6bc77 | 2011-05-26 11:14:15 -0700 | [diff] [blame] | 122 | int type = detect_partition(NULL, partition); |
Steve Kondik | 4123b58 | 2010-11-14 03:18:40 -0500 | [diff] [blame] | 123 | switch (type) { |
| 124 | case MTD: |
| 125 | return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only); |
| 126 | case MMC: |
| 127 | return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only); |
| 128 | case BML: |
| 129 | return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only); |
| 130 | default: |
| 131 | return -1; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | int get_partition_device(const char *partition, char *device) |
| 136 | { |
| 137 | int type = device_flash_type(); |
| 138 | switch (type) { |
| 139 | case MTD: |
| 140 | return cmd_mtd_get_partition_device(partition, device); |
| 141 | case MMC: |
| 142 | return cmd_mmc_get_partition_device(partition, device); |
| 143 | case BML: |
| 144 | return cmd_bml_get_partition_device(partition, device); |
| 145 | default: |
| 146 | return -1; |
| 147 | } |
| 148 | } |