blob: 4dc646844e7dc40786380ebd6a5ca5a30c6c6f62 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
Mark Salyzyn154f4602014-02-20 14:59:07 -08002 * Copyright (C) 2007-2014 The Android Open Source Project
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughesf39f7f12016-08-31 14:41:51 -070017#include <dirent.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070018#include <errno.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070019#include <fcntl.h>
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070020#include <fnmatch.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070021#include <libgen.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070022#include <linux/netlink.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080023#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070024#include <stdio.h>
25#include <stdlib.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070026#include <string.h>
Elliott Hughes632e99a2016-11-12 11:44:16 -080027#include <sys/sendfile.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#include <sys/socket.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070029#include <sys/time.h>
30#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070031#include <sys/un.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070032#include <sys/wait.h>
33#include <unistd.h>
34
James Hawkins588a2ca2016-02-18 14:52:46 -080035#include <memory>
Elliott Hughes290a2282016-11-14 17:08:47 -080036#include <thread>
James Hawkins588a2ca2016-02-18 14:52:46 -080037
Biao Ludc848562016-01-28 16:10:54 +080038#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070039#include <android-base/logging.h>
Rob Herring6de783a2016-05-06 10:06:59 -050040#include <android-base/stringprintf.h>
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +080041#include <android-base/unique_fd.h>
Dima Zavinda04c522011-09-01 17:09:44 -070042#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100043#include <cutils/uevent.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070044#include <private/android_filesystem_config.h>
45#include <selinux/android.h>
46#include <selinux/avc.h>
47#include <selinux/label.h>
48#include <selinux/selinux.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100049
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080051#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070052#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070055static const char *firmware_dirs[] = { "/etc/firmware",
56 "/vendor/firmware",
57 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058
Stephen Smalleye096e362012-06-11 13:37:39 -040059extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050060
Sandeep Patil35403eb2017-02-08 20:27:12 -080061static android::base::unique_fd device_fd;
Colin Cross0dd7ca62010-04-13 19:25:51 -070062
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063struct perms_ {
64 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070065 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066 mode_t perm;
67 unsigned int uid;
68 unsigned int gid;
69 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070070 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073struct perm_node {
74 struct perms_ dp;
75 struct listnode plist;
76};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070077
Colin Crossfadb85e2011-03-30 18:32:12 -070078struct platform_node {
79 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080080 char *path;
81 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070082 struct listnode list;
83};
84
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070085static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070086static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070087static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070088
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070089int add_dev_perms(const char *name, const char *attr,
90 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070091 unsigned short prefix,
92 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -080093 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070094 if (!node)
95 return -ENOMEM;
96
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070097 node->dp.name = strdup(name);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -080098 if (!node->dp.name) {
99 free(node);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700100 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800101 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700103 if (attr) {
104 node->dp.attr = strdup(attr);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800105 if (!node->dp.attr) {
106 free(node->dp.name);
107 free(node);
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700108 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800109 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700110 }
111
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112 node->dp.perm = perm;
113 node->dp.uid = uid;
114 node->dp.gid = gid;
115 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700116 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700117
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700118 if (attr)
119 list_add_tail(&sys_perms, &node->plist);
120 else
121 list_add_tail(&dev_perms, &node->plist);
122
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700123 return 0;
124}
125
Colin Cross43d537e2014-07-02 13:08:13 -0700126static bool perm_path_matches(const char *path, struct perms_ *dp)
127{
128 if (dp->prefix) {
129 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
130 return true;
131 } else if (dp->wildcard) {
132 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
133 return true;
134 } else {
135 if (strcmp(path, dp->name) == 0)
136 return true;
137 }
138
139 return false;
140}
141
Rob Herring6de783a2016-05-06 10:06:59 -0500142static bool match_subsystem(perms_* dp, const char* pattern,
143 const char* path, const char* subsystem) {
144 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
145 return false;
146 }
Rob Herringe5636a32016-05-06 12:28:48 -0500147
Rob Herring6de783a2016-05-06 10:06:59 -0500148 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
149 return perm_path_matches(subsys_path.c_str(), dp);
150}
Rob Herringe5636a32016-05-06 12:28:48 -0500151
Rob Herring6de783a2016-05-06 10:06:59 -0500152static void fixup_sys_perms(const char* upath, const char* subsystem) {
153 // upaths omit the "/sys" that paths in this list
154 // contain, so we prepend it...
155 std::string path = std::string(SYSFS_PREFIX) + upath;
156
157 listnode* node;
Rob Herringe5636a32016-05-06 12:28:48 -0500158 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500159 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
160 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
161 ; // matched
162 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
163 ; // matched
164 } else if (!perm_path_matches(path.c_str(), dp)) {
165 continue;
Rob Herringe5636a32016-05-06 12:28:48 -0500166 }
167
168 std::string attr_file = path + "/" + dp->attr;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700169 LOG(INFO) << "fixup " << attr_file
170 << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
Rob Herringe5636a32016-05-06 12:28:48 -0500171 chown(attr_file.c_str(), dp->uid, dp->gid);
172 chmod(attr_file.c_str(), dp->perm);
173 }
174
175 if (access(path.c_str(), F_OK) == 0) {
Dmitry Shmidt7eed4742016-07-28 13:55:39 -0700176 LOG(VERBOSE) << "restorecon_recursive: " << path;
Paul Lawrencea8d84342016-11-14 15:40:18 -0800177 restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
Rob Herringe5636a32016-05-06 12:28:48 -0500178 }
179}
180
Colin Cross43d537e2014-07-02 13:08:13 -0700181static mode_t get_device_perm(const char *path, const char **links,
182 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700183{
Colin Cross44b65d02010-04-20 14:32:50 -0700184 struct listnode *node;
185 struct perm_node *perm_node;
186 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700187
Colin Cross44b65d02010-04-20 14:32:50 -0700188 /* search the perms list in reverse so that ueventd.$hardware can
189 * override ueventd.rc
190 */
191 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700192 bool match = false;
193
Colin Cross44b65d02010-04-20 14:32:50 -0700194 perm_node = node_to_item(node, struct perm_node, plist);
195 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700196
Colin Cross43d537e2014-07-02 13:08:13 -0700197 if (perm_path_matches(path, dp)) {
198 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700199 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700200 if (links) {
201 int i;
202 for (i = 0; links[i]; i++) {
203 if (perm_path_matches(links[i], dp)) {
204 match = true;
205 break;
206 }
207 }
208 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700209 }
Colin Cross43d537e2014-07-02 13:08:13 -0700210
211 if (match) {
212 *uid = dp->uid;
213 *gid = dp->gid;
214 return dp->perm;
215 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700216 }
Colin Cross44b65d02010-04-20 14:32:50 -0700217 /* Default if nothing found. */
218 *uid = 0;
219 *gid = 0;
220 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700221}
222
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700223static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800224 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400225 int block, int major, int minor,
226 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227{
228 unsigned uid;
229 unsigned gid;
230 mode_t mode;
231 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500232 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700233
Colin Cross43d537e2014-07-02 13:08:13 -0700234 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700235
Sandeep Patilea239832017-02-03 07:51:55 -0800236 if (sehandle) {
237 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
238 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
239 return;
240 }
241 setfscreatecon(secontext);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300242 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700243
Colin Cross17dcc5c2010-09-03 12:25:34 -0700244 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800245 /* Temporarily change egid to avoid race condition setting the gid of the
246 * device node. Unforunately changing the euid would prevent creation of
247 * some device nodes, so the uid has to be set with chown() and is still
248 * racy. Fixing the gid race at least fixed the issue with system_server
249 * opening dynamic input devices under the AID_INPUT gid. */
Tom Cherry0506b182017-02-23 13:46:09 -0800250 if (setegid(gid)) {
251 PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
252 goto out;
253 }
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300254 /* If the node already exists update its SELinux label to handle cases when
255 * it was created with the wrong context during coldboot procedure. */
Sandeep Patilea239832017-02-03 07:51:55 -0800256 if (mknod(path, mode, dev) && (errno == EEXIST) && secontext) {
William Roberts397de142016-06-02 09:53:44 -0700257
258 char* fcon = nullptr;
259 int rc = lgetfilecon(path, &fcon);
260 if (rc < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700261 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
William Roberts397de142016-06-02 09:53:44 -0700262 goto out;
263 }
264
265 bool different = strcmp(fcon, secontext) != 0;
266 freecon(fcon);
267
268 if (different && lsetfilecon(path, secontext)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700269 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300270 }
271 }
William Roberts397de142016-06-02 09:53:44 -0700272
273out:
Nick Pelly6405c692010-01-21 18:13:39 -0800274 chown(path, uid, -1);
Tom Cherry0506b182017-02-23 13:46:09 -0800275 if (setegid(AID_ROOT)) {
276 PLOG(FATAL) << "setegid(AID_ROOT) failed";
277 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700278
Sandeep Patilea239832017-02-03 07:51:55 -0800279 if (secontext) {
280 freecon(secontext);
281 setfscreatecon(NULL);
282 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283}
284
Dima Zavinf395c922013-03-06 16:23:57 -0800285static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700286{
Dima Zavinf395c922013-03-06 16:23:57 -0800287 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700288 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800289 const char *name = path;
290
291 if (!strncmp(path, "/devices/", 9)) {
292 name += 9;
293 if (!strncmp(name, "platform/", 9))
294 name += 9;
295 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700296
Wei Wanga285dac2016-10-04 14:05:39 -0700297 LOG(VERBOSE) << "adding platform device " << name << " (" << path << ")";
Colin Crossfadb85e2011-03-30 18:32:12 -0700298
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800299 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800300 bus->path = strdup(path);
301 bus->path_len = path_len;
302 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700303 list_add_tail(&platform_names, &bus->list);
304}
305
306/*
Dima Zavinf395c922013-03-06 16:23:57 -0800307 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700308 * platform device prefix. If it doesn't start with a platform device, return
309 * 0.
310 */
Dima Zavinf395c922013-03-06 16:23:57 -0800311static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700312{
Dima Zavinf395c922013-03-06 16:23:57 -0800313 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700314 struct listnode *node;
315 struct platform_node *bus;
316
317 list_for_each_reverse(node, &platform_names) {
318 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800319 if ((bus->path_len < path_len) &&
320 (path[bus->path_len] == '/') &&
321 !strncmp(path, bus->path, bus->path_len))
322 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700323 }
324
325 return NULL;
326}
327
Dima Zavinf395c922013-03-06 16:23:57 -0800328static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700329{
330 struct listnode *node;
331 struct platform_node *bus;
332
333 list_for_each_reverse(node, &platform_names) {
334 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800335 if (!strcmp(path, bus->path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700336 LOG(INFO) << "removing platform device " << bus->name;
Dima Zavinf395c922013-03-06 16:23:57 -0800337 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700338 list_remove(node);
339 free(bus);
340 return;
341 }
342 }
343}
344
Sandeep Patil35403eb2017-02-08 20:27:12 -0800345static void destroy_platform_devices() {
346 struct listnode* node;
347 struct listnode* n;
348 struct platform_node* bus;
349
350 list_for_each_safe(node, n, &platform_names) {
351 list_remove(node);
352 bus = node_to_item(node, struct platform_node, list);
353 free(bus->path);
354 free(bus);
355 }
356}
357
Andrew Boiea885d042013-09-13 17:41:20 -0700358/* Given a path that may start with a PCI device, populate the supplied buffer
359 * with the PCI domain/bus number and the peripheral ID and return 0.
360 * If it doesn't start with a PCI device, or there is some error, return -1 */
361static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
362{
363 const char *start, *end;
364
365 if (strncmp(path, "/devices/pci", 12))
366 return -1;
367
368 /* Beginning of the prefix is the initial "pci" after "/devices/" */
369 start = path + 9;
370
371 /* End of the prefix is two path '/' later, capturing the domain/bus number
372 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
373 end = strchr(start, '/');
374 if (!end)
375 return -1;
376 end = strchr(end + 1, '/');
377 if (!end)
378 return -1;
379
380 /* Make sure we have enough room for the string plus null terminator */
381 if (end - start + 1 > buf_sz)
382 return -1;
383
384 strncpy(buf, start, end - start);
385 buf[end - start] = '\0';
386 return 0;
387}
388
Jeremy Compostella937309d2017-03-03 16:27:29 +0100389/* Given a path that may start with a virtual block device, populate
390 * the supplied buffer with the virtual block device ID and return 0.
391 * If it doesn't start with a virtual block device, or there is some
392 * error, return -1 */
393static int find_vbd_device_prefix(const char *path, char *buf, ssize_t buf_sz)
394{
395 const char *start, *end;
396
397 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
398 if (strncmp(path, "/devices/vbd-", 13))
399 return -1;
400
401 /* End of the prefix is one path '/' later, capturing the
402 virtual block device ID. Example: 768 */
403 start = path + 13;
404 end = strchr(start, '/');
405 if (!end)
406 return -1;
407
408 /* Make sure we have enough room for the string plus null terminator */
409 if (end - start + 1 > buf_sz)
410 return -1;
411
412 strncpy(buf, start, end - start);
413 buf[end - start] = '\0';
414 return 0;
415}
416
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700417static void parse_event(const char *msg, struct uevent *uevent)
418{
419 uevent->action = "";
420 uevent->path = "";
421 uevent->subsystem = "";
422 uevent->firmware = "";
423 uevent->major = -1;
424 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700425 uevent->partition_name = NULL;
426 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700427 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700428
429 /* currently ignoring SEQNUM */
430 while(*msg) {
431 if(!strncmp(msg, "ACTION=", 7)) {
432 msg += 7;
433 uevent->action = msg;
434 } else if(!strncmp(msg, "DEVPATH=", 8)) {
435 msg += 8;
436 uevent->path = msg;
437 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
438 msg += 10;
439 uevent->subsystem = msg;
440 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
441 msg += 9;
442 uevent->firmware = msg;
443 } else if(!strncmp(msg, "MAJOR=", 6)) {
444 msg += 6;
445 uevent->major = atoi(msg);
446 } else if(!strncmp(msg, "MINOR=", 6)) {
447 msg += 6;
448 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700449 } else if(!strncmp(msg, "PARTN=", 6)) {
450 msg += 6;
451 uevent->partition_num = atoi(msg);
452 } else if(!strncmp(msg, "PARTNAME=", 9)) {
453 msg += 9;
454 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700455 } else if(!strncmp(msg, "DEVNAME=", 8)) {
456 msg += 8;
457 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700458 }
459
Wei Zhongf97b8872012-03-23 14:15:34 -0700460 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700461 while(*msg++)
462 ;
463 }
464
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800465 if (LOG_UEVENTS) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700466 LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
467 uevent->action, uevent->path, uevent->subsystem,
468 uevent->firmware, uevent->major, uevent->minor);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800469 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700470}
471
Benoit Gobyd2278632010-08-03 14:36:02 -0700472static char **get_character_device_symlinks(struct uevent *uevent)
473{
474 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700475 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700476 char **links;
477 int link_num = 0;
478 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800479 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700480
Dima Zavinf395c922013-03-06 16:23:57 -0800481 pdev = find_platform_device(uevent->path);
482 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700483 return NULL;
484
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800485 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700486 if (!links)
487 return NULL;
488 memset(links, 0, sizeof(char *) * 2);
489
490 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800491 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100492 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700493 goto err;
494
495 if (!strncmp(parent, "/usb", 4)) {
496 /* skip root hub name and device. use device interface */
497 while (*++parent && *parent != '/');
498 if (*parent)
499 while (*++parent && *parent != '/');
500 if (!*parent)
501 goto err;
502 slash = strchr(++parent, '/');
503 if (!slash)
504 goto err;
505 width = slash - parent;
506 if (width <= 0)
507 goto err;
508
509 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
510 link_num++;
511 else
512 links[link_num] = NULL;
513 mkdir("/dev/usb", 0755);
514 }
515 else {
516 goto err;
517 }
518
519 return links;
520err:
521 free(links);
522 return NULL;
523}
524
Andrew Boiea885d042013-09-13 17:41:20 -0700525static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700526{
Colin Crossfadb85e2011-03-30 18:32:12 -0700527 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800528 struct platform_node *pdev;
Dan Austin60b976d2016-03-28 14:22:12 -0700529 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700530 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700531 char buf[256];
532 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700533 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700534 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700535
Dima Zavinf395c922013-03-06 16:23:57 -0800536 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700537 if (pdev) {
538 device = pdev->name;
539 type = "platform";
540 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
541 device = buf;
542 type = "pci";
Jeremy Compostella937309d2017-03-03 16:27:29 +0100543 } else if (!find_vbd_device_prefix(uevent->path, buf, sizeof(buf))) {
544 device = buf;
545 type = "vbd";
Andrew Boiea885d042013-09-13 17:41:20 -0700546 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800547 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700548 }
Dima Zavinf395c922013-03-06 16:23:57 -0800549
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800550 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700551 if (!links)
552 return NULL;
553 memset(links, 0, sizeof(char *) * 4);
554
Sandeep Patil35403eb2017-02-08 20:27:12 -0800555 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700556
Andrew Boiea885d042013-09-13 17:41:20 -0700557 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700558
559 if (uevent->partition_name) {
560 p = strdup(uevent->partition_name);
561 sanitize(p);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700562 if (strcmp(uevent->partition_name, p)) {
563 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
564 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700565 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
566 link_num++;
567 else
568 links[link_num] = NULL;
569 free(p);
570 }
571
572 if (uevent->partition_num >= 0) {
573 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
574 link_num++;
575 else
576 links[link_num] = NULL;
577 }
578
Dima Zavinf395c922013-03-06 16:23:57 -0800579 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700580 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
581 link_num++;
582 else
583 links[link_num] = NULL;
584
585 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700586}
587
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700588static void make_link_init(const char* oldpath, const char* newpath) {
589 const char* slash = strrchr(newpath, '/');
590 if (!slash) return;
591
592 if (mkdir_recursive(dirname(newpath), 0755)) {
593 PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
594 }
595
596 if (symlink(oldpath, newpath) && errno != EEXIST) {
597 PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
598 }
599}
600
601static void remove_link(const char* oldpath, const char* newpath) {
602 std::string path;
603 if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
604}
605
Colin Crosseb5ba832011-03-30 17:37:17 -0700606static void handle_device(const char *action, const char *devpath,
607 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700608{
Colin Crosseb5ba832011-03-30 17:37:17 -0700609 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400610 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700611 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700612 for (int i = 0; links[i]; i++) {
Chris Fries79f33842013-09-05 13:19:21 -0500613 make_link_init(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700614 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700615 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700616 }
617
Colin Crosseb5ba832011-03-30 17:37:17 -0700618 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700619 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700620 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700621 remove_link(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700622 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700623 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700624 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700625 }
626
627 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700628 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700629 free(links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700630 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700631 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700632 }
633}
634
Colin Crossfadb85e2011-03-30 18:32:12 -0700635static void handle_platform_device_event(struct uevent *uevent)
636{
Dima Zavinf395c922013-03-06 16:23:57 -0800637 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700638
639 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800640 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700641 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800642 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700643}
644
Colin Crosseb5ba832011-03-30 17:37:17 -0700645static const char *parse_device_name(struct uevent *uevent, unsigned int len)
646{
647 const char *name;
648
649 /* if it's not a /dev device, nothing else to do */
650 if((uevent->major < 0) || (uevent->minor < 0))
651 return NULL;
652
653 /* do we have a name? */
654 name = strrchr(uevent->path, '/');
655 if(!name)
656 return NULL;
657 name++;
658
659 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800660 if(strlen(name) > len) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700661 LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
Colin Crosseb5ba832011-03-30 17:37:17 -0700662 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800663 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700664
665 return name;
666}
667
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800668#define DEVPATH_LEN 96
669#define MAX_DEV_NAME 64
670
Colin Crosseb5ba832011-03-30 17:37:17 -0700671static void handle_block_device_event(struct uevent *uevent)
672{
673 const char *base = "/dev/block/";
674 const char *name;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800675 char devpath[DEVPATH_LEN];
Colin Crosseb5ba832011-03-30 17:37:17 -0700676 char **links = NULL;
677
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800678 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700679 if (!name)
680 return;
681
682 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500683 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700684
Dima Zavinf395c922013-03-06 16:23:57 -0800685 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700686 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700687
688 handle_device(uevent->action, devpath, uevent->path, 1,
689 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700690}
691
Greg Hackmann3312aa82013-11-18 15:24:40 -0800692static bool assemble_devpath(char *devpath, const char *dirname,
693 const char *devname)
694{
695 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
696 if (s < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700697 PLOG(ERROR) << "failed to assemble device path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800698 return false;
699 } else if (s >= DEVPATH_LEN) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700700 LOG(ERROR) << dirname << "/" << devname
701 << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800702 return false;
703 }
704 return true;
705}
706
707static void mkdir_recursive_for_devpath(const char *devpath)
708{
709 char dir[DEVPATH_LEN];
710 char *slash;
711
712 strcpy(dir, devpath);
713 slash = strrchr(dir, '/');
714 *slash = '\0';
715 mkdir_recursive(dir, 0755);
716}
717
Colin Crosseb5ba832011-03-30 17:37:17 -0700718static void handle_generic_device_event(struct uevent *uevent)
719{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800720 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700721 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800722 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700723 char **links = NULL;
724
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800725 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700726 if (!name)
727 return;
728
Greg Hackmann3312aa82013-11-18 15:24:40 -0800729 struct ueventd_subsystem *subsystem =
730 ueventd_subsystem_find_by_name(uevent->subsystem);
731
732 if (subsystem) {
733 const char *devname;
734
735 switch (subsystem->devname_src) {
736 case DEVNAME_UEVENT_DEVNAME:
737 devname = uevent->device_name;
738 break;
739
740 case DEVNAME_UEVENT_DEVPATH:
741 devname = name;
742 break;
743
744 default:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700745 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800746 return;
747 }
748
749 if (!assemble_devpath(devpath, subsystem->dirname, devname))
750 return;
751 mkdir_recursive_for_devpath(devpath);
752 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700753 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700754 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800755 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800756 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800757 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700758 }
759 else {
760 /* This imitates the file system that would be created
761 * if we were using devfs instead.
762 * Minors are broken up into groups of 128, starting at "001"
763 */
764 int bus_id = uevent->minor / 128 + 1;
765 int device_id = uevent->minor % 128 + 1;
766 /* build directories */
767 make_dir("/dev/bus", 0755);
768 make_dir("/dev/bus/usb", 0755);
769 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
770 make_dir(devpath, 0755);
771 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
772 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700773 } else {
774 /* ignore other USB events */
775 return;
776 }
777 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
778 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500779 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200780 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
781 base = "/dev/dri/";
782 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700783 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
784 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500785 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700786 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
787 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500788 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700789 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
790 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500791 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700792 } else if(!strncmp(uevent->subsystem, "input", 5)) {
793 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500794 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700795 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
796 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500797 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700798 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
799 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500800 make_dir(base, 0755);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700801 } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
802 LOG(INFO) << "kernel logger is deprecated";
Colin Crosseb5ba832011-03-30 17:37:17 -0700803 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500804 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700805 name += 4;
806 } else
807 base = "/dev/";
808 links = get_character_device_symlinks(uevent);
809
810 if (!devpath[0])
811 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
812
813 handle_device(uevent->action, devpath, uevent->path, 0,
814 uevent->major, uevent->minor, links);
815}
816
817static void handle_device_event(struct uevent *uevent)
818{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700819 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500820 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700821
822 if (!strncmp(uevent->subsystem, "block", 5)) {
823 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700824 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
825 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700826 } else {
827 handle_generic_device_event(uevent);
828 }
829}
830
Elliott Hughes632e99a2016-11-12 11:44:16 -0800831static void load_firmware(uevent* uevent, const std::string& root,
832 int fw_fd, size_t fw_size,
833 int loading_fd, int data_fd) {
834 // Start transfer.
835 android::base::WriteFully(loading_fd, "1", 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700836
Elliott Hughes632e99a2016-11-12 11:44:16 -0800837 // Copy the firmware.
838 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
839 if (rc == -1) {
840 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent->firmware << "' }";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700841 }
842
Elliott Hughes632e99a2016-11-12 11:44:16 -0800843 // Tell the firmware whether to abort or commit.
844 const char* response = (rc != -1) ? "0" : "-1";
845 android::base::WriteFully(loading_fd, response, strlen(response));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700846}
847
Elliott Hughes632e99a2016-11-12 11:44:16 -0800848static int is_booting() {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700849 return access("/dev/.booting", F_OK) == 0;
850}
851
Elliott Hughes632e99a2016-11-12 11:44:16 -0800852static void process_firmware_event(uevent* uevent) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700853 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700854
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700855 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700856
Elliott Hughes632e99a2016-11-12 11:44:16 -0800857 std::string root = android::base::StringPrintf("/sys%s", uevent->path);
858 std::string loading = root + "/loading";
859 std::string data = root + "/data";
860
861 android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY|O_CLOEXEC));
862 if (loading_fd == -1) {
863 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700864 return;
Elliott Hughes632e99a2016-11-12 11:44:16 -0800865 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700866
Elliott Hughes632e99a2016-11-12 11:44:16 -0800867 android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY|O_CLOEXEC));
868 if (data_fd == -1) {
869 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent->firmware;
870 return;
871 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700872
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700873try_loading_again:
Elliott Hughes632e99a2016-11-12 11:44:16 -0800874 for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
875 std::string file = android::base::StringPrintf("%s/%s", firmware_dirs[i], uevent->firmware);
876 android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY|O_CLOEXEC));
877 struct stat sb;
878 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
879 load_firmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
880 return;
Benoit Goby609d8822010-11-09 18:10:24 -0800881 }
Brian Swetland02863b92010-09-19 03:36:39 -0700882 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700883
Elliott Hughes632e99a2016-11-12 11:44:16 -0800884 if (booting) {
885 // If we're not fully booted, we may be missing
886 // filesystems needed for firmware, wait and retry.
Elliott Hughes290a2282016-11-14 17:08:47 -0800887 std::this_thread::sleep_for(100ms);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800888 booting = is_booting();
889 goto try_loading_again;
890 }
891
892 LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware;
893
894 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
895 write(loading_fd, "-1", 2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700896}
897
Elliott Hughes632e99a2016-11-12 11:44:16 -0800898static void handle_firmware_event(uevent* uevent) {
899 if (strcmp(uevent->subsystem, "firmware")) return;
900 if (strcmp(uevent->action, "add")) return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700901
Elliott Hughes632e99a2016-11-12 11:44:16 -0800902 // Loading the firmware in a child means we can do that in parallel...
903 // (We ignore SIGCHLD rather than wait for our children.)
904 pid_t pid = fork();
905 if (pid == 0) {
906 Timer t;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700907 process_firmware_event(uevent);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000908 LOG(INFO) << "loading " << uevent->path << " took " << t;
Kenny Root17baff42014-08-20 16:16:44 -0700909 _exit(EXIT_SUCCESS);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800910 } else if (pid == -1) {
911 PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700912 }
913}
914
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800915static bool inline should_stop_coldboot(coldboot_action_t act)
916{
917 return (act == COLDBOOT_STOP || act == COLDBOOT_FINISH);
918}
919
Ruchi Kandoic6037202014-06-23 11:22:09 -0700920#define UEVENT_MSG_LEN 2048
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800921
Sandeep Patil44a3ee22017-02-08 15:49:47 -0800922static inline coldboot_action_t handle_device_fd_with(
923 std::function<coldboot_action_t(uevent* uevent)> handle_uevent)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700924{
Vernon Tang3f582e92011-04-25 13:08:17 +1000925 char msg[UEVENT_MSG_LEN+2];
926 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700927 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700928 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700929 continue;
930
931 msg[n] = '\0';
932 msg[n+1] = '\0';
933
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700934 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700935 parse_event(msg, &uevent);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800936 coldboot_action_t act = handle_uevent(&uevent);
937 if (should_stop_coldboot(act))
938 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700939 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800940
941 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700942}
943
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800944coldboot_action_t handle_device_fd(coldboot_callback fn)
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800945{
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800946 coldboot_action_t ret = handle_device_fd_with(
947 [&](uevent* uevent) -> coldboot_action_t {
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800948 if (selinux_status_updated() > 0) {
949 struct selabel_handle *sehandle2;
950 sehandle2 = selinux_android_file_context_handle();
951 if (sehandle2) {
952 selabel_close(sehandle);
953 sehandle = sehandle2;
954 }
955 }
956
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800957 // default is to always create the devices
958 coldboot_action_t act = COLDBOOT_CREATE;
959 if (fn) {
960 act = fn(uevent);
961 }
962
963 if (act == COLDBOOT_CREATE || act == COLDBOOT_STOP) {
964 handle_device_event(uevent);
965 handle_firmware_event(uevent);
966 }
967
968 return act;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800969 });
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800970
971 return ret;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800972}
973
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700974/* Coldboot walks parts of the /sys tree and pokes the uevent files
975** to cause the kernel to regenerate device add events that happened
976** before init's device manager was started
977**
978** We drain any pending events from the netlink socket every time
979** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800980** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700981*/
982
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800983static coldboot_action_t do_coldboot(DIR *d, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700984{
985 struct dirent *de;
986 int dfd, fd;
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800987 coldboot_action_t act = COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700988
989 dfd = dirfd(d);
990
991 fd = openat(dfd, "uevent", O_WRONLY);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800992 if (fd >= 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700993 write(fd, "add\n", 4);
994 close(fd);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800995 act = handle_device_fd(fn);
996 if (should_stop_coldboot(act))
997 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700998 }
999
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001000 while (!should_stop_coldboot(act) && (de = readdir(d))) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001001 DIR *d2;
1002
1003 if(de->d_type != DT_DIR || de->d_name[0] == '.')
1004 continue;
1005
1006 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
1007 if(fd < 0)
1008 continue;
1009
1010 d2 = fdopendir(fd);
1011 if(d2 == 0)
1012 close(fd);
1013 else {
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001014 act = do_coldboot(d2, fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001015 closedir(d2);
1016 }
1017 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001018
1019 // default is always to continue looking for uevents
1020 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001021}
1022
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001023static coldboot_action_t coldboot(const char *path, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001024{
James Hawkins588a2ca2016-02-18 14:52:46 -08001025 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001026 if (d) {
1027 return do_coldboot(d.get(), fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001028 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001029
1030 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001031}
1032
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001033void device_init(const char* path, coldboot_callback fn) {
Sandeep Patil971a4602017-02-15 13:37:52 -08001034 if (!sehandle) {
1035 sehandle = selinux_android_file_context_handle();
Elliott Hughes56a06562015-03-28 11:23:32 -07001036 }
Sandeep Patil971a4602017-02-15 13:37:52 -08001037 // open uevent socket and selinux status only if it hasn't been
1038 // done before
1039 if (device_fd == -1) {
1040 /* is 256K enough? udev uses 16MB! */
1041 device_fd.reset(uevent_open_socket(256 * 1024, true));
1042 if (device_fd == -1) {
1043 return;
1044 }
1045 fcntl(device_fd, F_SETFL, O_NONBLOCK);
1046 selinux_status_open(true);
1047 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001048
Elliott Hughes56a06562015-03-28 11:23:32 -07001049 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001050 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -07001051 return;
Colin Crossf83d0b92010-04-21 12:04:20 -07001052 }
Elliott Hughes56a06562015-03-28 11:23:32 -07001053
1054 Timer t;
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001055 coldboot_action_t act;
1056 if (!path) {
1057 act = coldboot("/sys/class", fn);
1058 if (!should_stop_coldboot(act)) {
1059 act = coldboot("/sys/block", fn);
1060 if (!should_stop_coldboot(act)) {
1061 act = coldboot("/sys/devices", fn);
1062 }
1063 }
1064 } else {
1065 act = coldboot(path, fn);
1066 }
1067
1068 // If we have a callback, then do as it says. If no, then the default is
1069 // to always create COLDBOOT_DONE file.
1070 if (!fn || (act == COLDBOOT_FINISH)) {
1071 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
1072 }
1073
Elliott Hughes331cf2f2016-11-29 19:20:58 +00001074 LOG(INFO) << "Coldboot took " << t;
Colin Cross0dd7ca62010-04-13 19:25:51 -07001075}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001076
Sandeep Patil35403eb2017-02-08 20:27:12 -08001077void device_close() {
1078 destroy_platform_devices();
1079 device_fd.reset();
Sandeep Patil971a4602017-02-15 13:37:52 -08001080 selinux_status_close();
Sandeep Patil35403eb2017-02-08 20:27:12 -08001081}
1082
Elliott Hughes632e99a2016-11-12 11:44:16 -08001083int get_device_fd() {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001084 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001085}