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