blob: de45e87b85948bb7b2be17771f770832f2485159 [file] [log] [blame]
Steve Kondik4123b582010-11-14 03:18:40 -05001#include <signal.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <sys/wait.h>
Koushik Dutta7adeadc2011-05-26 11:47:56 -07005#include <stdio.h>
Steve Kondik4123b582010-11-14 03:18:40 -05006
7#include "flashutils/flashutils.h"
8
Koushik Duttaa75c0672011-07-07 12:55:02 -07009#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 Kondik4123b582010-11-14 03:18:40 -050017int the_flash_type = UNKNOWN;
18
19int device_flash_type()
20{
21 if (the_flash_type == UNKNOWN) {
Koushik Duttaa75c0672011-07-07 12:55:02 -070022 if (access(BOARD_BML_BOOT, F_OK) == 0) {
Steve Kondik4123b582010-11-14 03:18:40 -050023 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
Koushik Dutta41873472011-05-26 17:30:43 -070040int 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 Dutta8a6bc772011-05-26 11:14:15 -070051static int detect_partition(const char *partitionType, const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -050052{
53 int type = device_flash_type();
Koushik Dutta67fa0c32011-03-17 11:37:21 -070054 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 Dutta8a6bc772011-05-26 11:14:15 -070060
61 if (partitionType != NULL) {
Koushik Dutta41873472011-05-26 17:30:43 -070062 type = get_flash_type(partitionType);
Koushik Dutta8a6bc772011-05-26 11:14:15 -070063 }
64
Koushik Dutta01143a52011-05-30 15:22:04 -070065 return type;
Koushik Dutta67fa0c32011-03-17 11:37:21 -070066}
Koushik Dutta8a6bc772011-05-26 11:14:15 -070067int restore_raw_partition(const char* partitionType, const char *partition, const char *filename)
Koushik Dutta67fa0c32011-03-17 11:37:21 -070068{
Koushik Dutta8a6bc772011-05-26 11:14:15 -070069 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -050070 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 Dutta8a6bc772011-05-26 11:14:15 -070082int backup_raw_partition(const char* partitionType, const char *partition, const char *filename)
Steve Kondik4123b582010-11-14 03:18:40 -050083{
Koushik Dutta8a6bc772011-05-26 11:14:15 -070084 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -050085 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 Duttaf5735102011-04-22 12:12:32 -070093 printf("unable to detect device type");
Steve Kondik4123b582010-11-14 03:18:40 -050094 return -1;
95 }
96}
97
Koushik Dutta8a6bc772011-05-26 11:14:15 -070098int erase_raw_partition(const char* partitionType, const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -050099{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700100 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500101 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{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700115 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500116 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{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700130 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500131 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}