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