blob: d8dab4327e217fd3dcceda26018134386a4a2d11 [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 "switch.h"
27
28#define DEBUG_BOOTSTRAP 0
29
30static int mmc_bootstrap_switch(char *sysfs_path);
31
32int switch_bootstrap()
33{
34 DIR *d;
35 struct dirent *de;
36
37 if (!(d = opendir(SYSFS_CLASS_SWITCH_PATH))) {
San Mehat1f278212009-07-16 10:44:15 -070038 LOG_ERROR("Unable to open '%s' (%s)", SYSFS_CLASS_SWITCH_PATH,
39 strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040 return -errno;
41 }
42
43 while ((de = readdir(d))) {
44 char tmp[255];
45
46 if (de->d_name[0] == '.')
47 continue;
48
49 sprintf(tmp, "%s/%s", SYSFS_CLASS_SWITCH_PATH, de->d_name);
San Mehat1f278212009-07-16 10:44:15 -070050 if (mmc_bootstrap_switch(tmp)) {
51 LOG_ERROR("Error bootstrapping switch '%s' (%s)", tmp,
52 strerror(errno));
53 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054 }
55
56 closedir(d);
57
58 return 0;
59}
60
61static int mmc_bootstrap_switch(char *sysfs_path)
62{
63#if DEBUG_BOOTSTRAP
64 LOG_VOL("bootstrap_switch(%s):", sysfs_path);
65#endif
66
67 char filename[255];
68 char name[255];
69 char state[255];
70 char tmp[255];
71 char *uevent_params[3];
72 char devpath[255];
73 FILE *fp;
74
75 /*
76 * Read switch name
77 */
78 sprintf(filename, "%s/name", sysfs_path);
79 if (!(fp = fopen(filename, "r"))) {
80 LOGE("Error opening switch name path '%s' (%s)",
81 sysfs_path, strerror(errno));
82 return -errno;
83 }
84 if (!fgets(name, sizeof(name), fp)) {
85 LOGE("Unable to read switch name");
86 fclose(fp);
87 return -EIO;
88 }
89 fclose(fp);
90
91 name[strlen(name) -1] = '\0';
92 sprintf(devpath, "/devices/virtual/switch/%s", name);
93 sprintf(tmp, "SWITCH_NAME=%s", name);
94 uevent_params[0] = (char *) strdup(tmp);
95
96 /*
97 * Read switch state
98 */
99 sprintf(filename, "%s/state", sysfs_path);
100 if (!(fp = fopen(filename, "r"))) {
101 LOGE("Error opening switch state path '%s' (%s)",
102 sysfs_path, strerror(errno));
103 return -errno;
104 }
105 if (!fgets(state, sizeof(state), fp)) {
106 LOGE("Unable to read switch state");
107 fclose(fp);
108 return -EIO;
109 }
110 fclose(fp);
111
112 state[strlen(state) -1] = '\0';
113 sprintf(tmp, "SWITCH_STATE=%s", state);
114 uevent_params[1] = (char *) strdup(tmp);
115
116 uevent_params[2] = (char *) NULL;
117
118 if (simulate_uevent("switch", devpath, "add", uevent_params) < 0) {
119 LOGE("Error simulating uevent (%s)", strerror(errno));
120 return -errno;
121 }
122
123 return 0;
124}