blob: fcf7c2f9ebc113869db63eb1ddea9f929058ae8f [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001
2/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <stdlib.h>
19#include <string.h>
20#include <dirent.h>
21#include <errno.h>
22
23#include <sys/types.h>
24
25#include "vold.h"
26#include "mmc.h"
27#include "media.h"
Ben Winslow227c74a2009-08-15 09:52:10 -040028#include "diskmbr.h" /* for NDOSPART */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
30#define DEBUG_BOOTSTRAP 0
31
32static int mmc_bootstrap_controller(char *sysfs_path);
33static int mmc_bootstrap_card(char *sysfs_path);
34static int mmc_bootstrap_block(char *devpath);
35static int mmc_bootstrap_mmcblk(char *devpath);
36static int mmc_bootstrap_mmcblk_partition(char *devpath);
37
38/*
39 * Bootstrap our mmc information.
40 */
41int mmc_bootstrap()
42{
43 DIR *d;
44 struct dirent *de;
45
46 if (!(d = opendir(SYSFS_CLASS_MMC_PATH))) {
San Mehat1f278212009-07-16 10:44:15 -070047 LOG_ERROR("Unable to open '%s' (%s)", SYSFS_CLASS_MMC_PATH,
48 strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049 return -errno;
50 }
51
52 while ((de = readdir(d))) {
53 char tmp[255];
54
55 if (de->d_name[0] == '.')
56 continue;
57
58 sprintf(tmp, "%s/%s", SYSFS_CLASS_MMC_PATH, de->d_name);
San Mehat1f278212009-07-16 10:44:15 -070059 if (mmc_bootstrap_controller(tmp)) {
60 LOG_ERROR("Error bootstrapping controller '%s' (%s)", tmp,
61 strerror(errno));
62 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063 }
64
65 closedir(d);
66
67 return 0;
68}
69
70static int mmc_bootstrap_controller(char *sysfs_path)
71{
72 DIR *d;
73 struct dirent *de;
74
75#if DEBUG_BOOTSTRAP
76 LOG_VOL("bootstrap_controller(%s):", sysfs_path);
77#endif
78 if (!(d = opendir(sysfs_path))) {
San Mehat1f278212009-07-16 10:44:15 -070079 LOG_ERROR("Unable to open '%s' (%s)", sysfs_path, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080 return -errno;
81 }
82
83 while ((de = readdir(d))) {
84 char tmp[255];
85
86 if (de->d_name[0] == '.')
87 continue;
88
89 if ((!strcmp(de->d_name, "uevent")) ||
90 (!strcmp(de->d_name, "subsystem")) ||
91 (!strcmp(de->d_name, "device")) ||
92 (!strcmp(de->d_name, "power"))) {
93 continue;
94 }
95
96 sprintf(tmp, "%s/%s", sysfs_path, de->d_name);
97
98 if (mmc_bootstrap_card(tmp) < 0)
San Mehat1f278212009-07-16 10:44:15 -070099 LOG_ERROR("Error bootstrapping card '%s' (%s)", tmp, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100 } // while
101
102 closedir(d);
103 return 0;
104}
105
106static int mmc_bootstrap_card(char *sysfs_path)
107{
108 char saved_cwd[255];
109 char new_cwd[255];
110 char *devpath;
111 char *uevent_params[4];
112 char *p;
113 char filename[255];
114 char tmp[255];
115 ssize_t sz;
116
117#if DEBUG_BOOTSTRAP
118 LOG_VOL("bootstrap_card(%s):", sysfs_path);
119#endif
120
121 /*
122 * sysfs_path is based on /sys/class, but we want the actual device class
123 */
124 if (!getcwd(saved_cwd, sizeof(saved_cwd))) {
125 LOGE("Error getting working dir path");
126 return -errno;
127 }
128
129 if (chdir(sysfs_path) < 0) {
San Mehat1f278212009-07-16 10:44:15 -0700130 LOGE("Unable to chdir to %s (%s)", sysfs_path, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 return -errno;
132 }
133
134 if (!getcwd(new_cwd, sizeof(new_cwd))) {
135 LOGE("Buffer too small for device path");
136 return -errno;
137 }
138
139 if (chdir(saved_cwd) < 0) {
140 LOGE("Unable to restore working dir");
141 return -errno;
142 }
143
144 devpath = &new_cwd[4]; // Skip over '/sys'
145
146 /*
147 * Collect parameters so we can simulate a UEVENT
148 */
149 sprintf(tmp, "DEVPATH=%s", devpath);
150 uevent_params[0] = (char *) strdup(tmp);
151
152 sprintf(filename, "/sys%s/type", devpath);
153 p = read_file(filename, &sz);
154 p[strlen(p) - 1] = '\0';
155 sprintf(tmp, "MMC_TYPE=%s", p);
156 free(p);
157 uevent_params[1] = (char *) strdup(tmp);
158
159 sprintf(filename, "/sys%s/name", devpath);
160 p = read_file(filename, &sz);
161 p[strlen(p) - 1] = '\0';
162 sprintf(tmp, "MMC_NAME=%s", p);
163 free(p);
164 uevent_params[2] = (char *) strdup(tmp);
165
166 uevent_params[3] = (char *) NULL;
167
168 if (simulate_uevent("mmc", devpath, "add", uevent_params) < 0) {
San Mehat1f278212009-07-16 10:44:15 -0700169 LOGE("Error simulating uevent (%s)", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 return -errno;
171 }
172
173 /*
174 * Check for block drivers
175 */
176 char block_devpath[255];
177 sprintf(tmp, "%s/block", devpath);
178 sprintf(filename, "/sys%s/block", devpath);
179 if (!access(filename, F_OK)) {
180 if (mmc_bootstrap_block(tmp)) {
181 LOGE("Error bootstrapping block @ %s", tmp);
182 }
183 }
184
185 return 0;
186}
187
188static int mmc_bootstrap_block(char *devpath)
189{
190 char blockdir_path[255];
191 DIR *d;
192 struct dirent *de;
193
194#if DEBUG_BOOTSTRAP
195 LOG_VOL("mmc_bootstrap_block(%s):", devpath);
196#endif
197
198 sprintf(blockdir_path, "/sys%s", devpath);
199
200 if (!(d = opendir(blockdir_path))) {
201 LOGE("Failed to opendir %s", devpath);
202 return -errno;
203 }
204
205 while ((de = readdir(d))) {
206 char tmp[255];
207
208 if (de->d_name[0] == '.')
209 continue;
210 sprintf(tmp, "%s/%s", devpath, de->d_name);
211 if (mmc_bootstrap_mmcblk(tmp))
212 LOGE("Error bootstraping mmcblk @ %s", tmp);
213 }
214 closedir(d);
215 return 0;
216}
217
218static int mmc_bootstrap_mmcblk(char *devpath)
219{
220 char *mmcblk_devname;
221 int part_no;
222 int rc;
223
224#if DEBUG_BOOTSTRAP
225 LOG_VOL("mmc_bootstrap_mmcblk(%s):", devpath);
226#endif
227
228 if ((rc = mmc_bootstrap_mmcblk_partition(devpath))) {
229 LOGE("Error bootstrapping mmcblk partition '%s'", devpath);
230 return rc;
231 }
232
233 for (mmcblk_devname = &devpath[strlen(devpath)];
234 *mmcblk_devname != '/'; mmcblk_devname--);
235 mmcblk_devname++;
236
Ben Winslow227c74a2009-08-15 09:52:10 -0400237 for (part_no = 1; part_no <= NDOSPART; part_no++) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 char part_file[255];
239 sprintf(part_file, "/sys%s/%sp%d", devpath, mmcblk_devname, part_no);
240 if (!access(part_file, F_OK)) {
241 char part_devpath[255];
242
243 sprintf(part_devpath, "%s/%sp%d", devpath, mmcblk_devname, part_no);
244 if (mmc_bootstrap_mmcblk_partition(part_devpath))
245 LOGE("Error bootstrapping mmcblk partition '%s'", part_devpath);
246 }
247 }
248
249 return 0;
250}
251
252static int mmc_bootstrap_mmcblk_partition(char *devpath)
253{
254 char filename[255];
255 char *uevent_buffer;
256 ssize_t sz;
257 char *uevent_params[4];
258 char tmp[255];
259 FILE *fp;
260 char line[255];
261
262#if DEBUG_BOOTSTRAP
263 LOG_VOL("mmc_bootstrap_mmcblk_partition(%s):", devpath);
264#endif
265
266 sprintf(tmp, "DEVPATH=%s", devpath);
267 uevent_params[0] = strdup(tmp);
268
269 sprintf(filename, "/sys%s/uevent", devpath);
270 if (!(fp = fopen(filename, "r"))) {
San Mehat1f278212009-07-16 10:44:15 -0700271 LOGE("Unable to open '%s' (%s)", filename, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 return -errno;
273 }
274
275 while (fgets(line, sizeof(line), fp)) {
276 line[strlen(line)-1] = 0;
277 if (!strncmp(line, "DEVTYPE=", 8))
278 uevent_params[1] = strdup(line);
279 else if (!strncmp(line, "MAJOR=",6))
280 uevent_params[2] = strdup(line);
281 else if (!strncmp(line, "MINOR=",6))
282 uevent_params[3] = strdup(line);
283 }
284 fclose(fp);
285
286 if (!uevent_params[1] || !uevent_params[2] || !uevent_params[3]) {
287 LOGE("mmcblk uevent missing required params");
288 return -1;
289 }
290 uevent_params[4] = '\0';
291
292 if (simulate_uevent("block", devpath, "add", uevent_params) < 0) {
San Mehat1f278212009-07-16 10:44:15 -0700293 LOGE("Error simulating uevent (%s)", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 return -errno;
295 }
296 return 0;
297}