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