blob: de45e87b85948bb7b2be17771f770832f2485159 [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>
6
7#include "flashutils/flashutils.h"
8
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
17int the_flash_type = UNKNOWN;
18
19int device_flash_type()
20{
21 if (the_flash_type == UNKNOWN) {
22 if (access(BOARD_BML_BOOT, F_OK) == 0) {
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
35char* get_default_filesystem()
36{
37 return device_flash_type() == MMC ? "ext3" : "yaffs2";
38}
39
40int 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
51static int detect_partition(const char *partitionType, const char *partition)
52{
53 int type = device_flash_type();
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;
60
61 if (partitionType != NULL) {
62 type = get_flash_type(partitionType);
63 }
64
65 return type;
66}
67int restore_raw_partition(const char* partitionType, const char *partition, const char *filename)
68{
69 int type = detect_partition(partitionType, partition);
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
82int backup_raw_partition(const char* partitionType, const char *partition, const char *filename)
83{
84 int type = detect_partition(partitionType, partition);
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:
93 printf("unable to detect device type");
94 return -1;
95 }
96}
97
98int erase_raw_partition(const char* partitionType, const char *partition)
99{
100 int type = detect_partition(partitionType, partition);
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
113int erase_partition(const char *partition, const char *filesystem)
114{
115 int type = detect_partition(NULL, partition);
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
128int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
129{
130 int type = detect_partition(NULL, partition);
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
143int 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}