blob: 6401939692be90beca5b21c8808218ba557a1053 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001#include <signal.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <sys/wait.h>
5#include <stdio.h>
Ethan Yonkerc798c9c2015-10-09 11:15:26 -05006#include <string.h>
Dees_Troy51a0e822012-09-05 15:24:24 -04007
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
18int the_flash_type = UNKNOWN;
19
20int 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_Troy51a0e822012-09-05 15:24:24 -040025 } else if (access("/proc/mtd", F_OK) == 0) {
26 the_flash_type = MTD;
James Christopher Adduono9eb27692016-05-24 19:23:03 -040027 } 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_Troy51a0e822012-09-05 15:24:24 -040031 } else {
32 the_flash_type = UNSUPPORTED;
33 }
34 }
35 return the_flash_type;
36}
37
38char* get_default_filesystem()
39{
40 return device_flash_type() == MMC ? "ext3" : "yaffs2";
41}
42
43int 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
54static 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 Adduono0de3b7e2016-03-29 15:42:55 -040059 else if (strstr(partition, "/dev/block/mmc") != NULL || strstr(partition, "/dev/block/sd") != NULL)
Dees_Troy51a0e822012-09-05 15:24:24 -040060 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}
70int 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
85int 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
101int 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
116int 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
131int 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
146int 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}