blob: 958476ce0fdedbd929fe0324d052171239591041 [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
Steve Kondik4123b582010-11-14 03:18:40 -05009int the_flash_type = UNKNOWN;
10
11int device_flash_type()
12{
13 if (the_flash_type == UNKNOWN) {
Koushik Duttaf5735102011-04-22 12:12:32 -070014 if (access("/dev/block/bml7", F_OK) == 0) {
Steve Kondik4123b582010-11-14 03:18:40 -050015 the_flash_type = BML;
16 } else if (access("/proc/emmc", F_OK) == 0) {
17 the_flash_type = MMC;
18 } else if (access("/proc/mtd", F_OK) == 0) {
19 the_flash_type = MTD;
20 } else {
21 the_flash_type = UNSUPPORTED;
22 }
23 }
24 return the_flash_type;
25}
26
27char* get_default_filesystem()
28{
29 return device_flash_type() == MMC ? "ext3" : "yaffs2";
30}
31
Koushik Dutta41873472011-05-26 17:30:43 -070032int get_flash_type(const char* partitionType) {
33 int type = UNSUPPORTED;
34 if (strcmp(partitionType, "mtd") == 0)
35 type = MTD;
36 else if (strcmp(partitionType, "emmc") == 0)
37 type = MMC;
38 else if (strcmp(partitionType, "bml") == 0)
39 type = BML;
40 return type;
41}
42
Koushik Dutta8a6bc772011-05-26 11:14:15 -070043static int detect_partition(const char *partitionType, const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -050044{
45 int type = device_flash_type();
Koushik Dutta67fa0c32011-03-17 11:37:21 -070046 if (strstr(partition, "/dev/block/mtd") != NULL)
47 type = MTD;
48 else if (strstr(partition, "/dev/block/mmc") != NULL)
49 type = MMC;
50 else if (strstr(partition, "/dev/block/bml") != NULL)
51 type = BML;
Koushik Dutta8a6bc772011-05-26 11:14:15 -070052
53 if (partitionType != NULL) {
Koushik Dutta41873472011-05-26 17:30:43 -070054 type = get_flash_type(partitionType);
Koushik Dutta8a6bc772011-05-26 11:14:15 -070055 }
56
Koushik Dutta01143a52011-05-30 15:22:04 -070057 return type;
Koushik Dutta67fa0c32011-03-17 11:37:21 -070058}
Koushik Dutta8a6bc772011-05-26 11:14:15 -070059int restore_raw_partition(const char* partitionType, const char *partition, const char *filename)
Koushik Dutta67fa0c32011-03-17 11:37:21 -070060{
Koushik Dutta8a6bc772011-05-26 11:14:15 -070061 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -050062 switch (type) {
63 case MTD:
64 return cmd_mtd_restore_raw_partition(partition, filename);
65 case MMC:
66 return cmd_mmc_restore_raw_partition(partition, filename);
67 case BML:
68 return cmd_bml_restore_raw_partition(partition, filename);
69 default:
70 return -1;
71 }
72}
73
Koushik Dutta8a6bc772011-05-26 11:14:15 -070074int backup_raw_partition(const char* partitionType, const char *partition, const char *filename)
Steve Kondik4123b582010-11-14 03:18:40 -050075{
Koushik Dutta8a6bc772011-05-26 11:14:15 -070076 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -050077 switch (type) {
78 case MTD:
79 return cmd_mtd_backup_raw_partition(partition, filename);
80 case MMC:
81 return cmd_mmc_backup_raw_partition(partition, filename);
82 case BML:
83 return cmd_bml_backup_raw_partition(partition, filename);
84 default:
Koushik Duttaf5735102011-04-22 12:12:32 -070085 printf("unable to detect device type");
Steve Kondik4123b582010-11-14 03:18:40 -050086 return -1;
87 }
88}
89
Koushik Dutta8a6bc772011-05-26 11:14:15 -070090int erase_raw_partition(const char* partitionType, const char *partition)
Steve Kondik4123b582010-11-14 03:18:40 -050091{
Koushik Dutta8a6bc772011-05-26 11:14:15 -070092 int type = detect_partition(partitionType, partition);
Steve Kondik4123b582010-11-14 03:18:40 -050093 switch (type) {
94 case MTD:
95 return cmd_mtd_erase_raw_partition(partition);
96 case MMC:
97 return cmd_mmc_erase_raw_partition(partition);
98 case BML:
99 return cmd_bml_erase_raw_partition(partition);
100 default:
101 return -1;
102 }
103}
104
105int erase_partition(const char *partition, const char *filesystem)
106{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700107 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500108 switch (type) {
109 case MTD:
110 return cmd_mtd_erase_partition(partition, filesystem);
111 case MMC:
112 return cmd_mmc_erase_partition(partition, filesystem);
113 case BML:
114 return cmd_bml_erase_partition(partition, filesystem);
115 default:
116 return -1;
117 }
118}
119
120int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
121{
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700122 int type = detect_partition(NULL, partition);
Steve Kondik4123b582010-11-14 03:18:40 -0500123 switch (type) {
124 case MTD:
125 return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only);
126 case MMC:
127 return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only);
128 case BML:
129 return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only);
130 default:
131 return -1;
132 }
133}
134
135int get_partition_device(const char *partition, char *device)
136{
137 int type = device_flash_type();
138 switch (type) {
139 case MTD:
140 return cmd_mtd_get_partition_device(partition, device);
141 case MMC:
142 return cmd_mmc_get_partition_device(partition, device);
143 case BML:
144 return cmd_bml_get_partition_device(partition, device);
145 default:
146 return -1;
147 }
148}