blob: 6b7bab9634c5262e4dea80dd82c07b3d386b6445 [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 Hughes41d07882016-08-31 14:41:51 -070017#include <dirent.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070018#include <errno.h>
Elliott Hughes41d07882016-08-31 14:41:51 -070019#include <fcntl.h>
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070020#include <fnmatch.h>
Elliott Hughes41d07882016-08-31 14:41:51 -070021#include <libgen.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080022#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070023#include <stdio.h>
24#include <stdlib.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070025#include <string.h>
Elliott Hughes6d5fd492016-11-12 11:44:16 -080026#include <sys/sendfile.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070027#include <sys/socket.h>
Elliott Hughes41d07882016-08-31 14:41:51 -070028#include <sys/stat.h>
29#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 Hughes41d07882016-08-31 14:41:51 -070032#include <sys/wait.h>
33#include <unistd.h>
34
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036
James Hawkinsfb6bb0a2016-02-18 14:52:46 -080037#include <memory>
Elliott Hughes1c5fa222016-11-14 17:08:47 -080038#include <thread>
James Hawkinsfb6bb0a2016-02-18 14:52:46 -080039
Stephen Smalleye46f9d52012-01-13 08:48:47 -050040#include <selinux/selinux.h>
41#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040042#include <selinux/android.h>
Stephen Smalleye2eb69d2013-04-16 09:30:30 -040043#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050044
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070045#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046
Biao Lu9c762a52016-01-28 16:10:54 +080047#include <android-base/file.h>
Rob Herringb075bdf2016-05-06 10:06:59 -050048#include <android-base/stringprintf.h>
Hung-ying Tyand816f962016-02-01 15:07:40 +080049#include <android-base/unique_fd.h>
Dima Zavinda04c522011-09-01 17:09:44 -070050#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100051#include <cutils/uevent.h>
52
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080054#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070055#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070056#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058#define SYSFS_PREFIX "/sys"
Daniel Rosenbergb2c315c2015-03-20 15:10:29 -070059static const char *firmware_dirs[] = { "/etc/firmware",
60 "/vendor/firmware",
61 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062
Stephen Smalleye096e362012-06-11 13:37:39 -040063extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050064
Sandeep Patilc6adcc52017-02-08 20:27:12 -080065static android::base::unique_fd device_fd;
Colin Cross0dd7ca62010-04-13 19:25:51 -070066
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067struct perms_ {
68 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070069 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070 mode_t perm;
71 unsigned int uid;
72 unsigned int gid;
73 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070074 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077struct perm_node {
78 struct perms_ dp;
79 struct listnode plist;
80};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070081
Colin Crossfadb85e2011-03-30 18:32:12 -070082struct platform_node {
83 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080084 char *path;
85 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070086 struct listnode list;
87};
88
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070089static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070090static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070091static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070092
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070093int add_dev_perms(const char *name, const char *attr,
94 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070095 unsigned short prefix,
96 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -080097 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070098 if (!node)
99 return -ENOMEM;
100
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700101 node->dp.name = strdup(name);
Ting-Yuan Huang6bc086c2016-11-15 15:35:16 -0800102 if (!node->dp.name) {
103 free(node);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104 return -ENOMEM;
Ting-Yuan Huang6bc086c2016-11-15 15:35:16 -0800105 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700106
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700107 if (attr) {
108 node->dp.attr = strdup(attr);
Ting-Yuan Huang6bc086c2016-11-15 15:35:16 -0800109 if (!node->dp.attr) {
110 free(node->dp.name);
111 free(node);
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700112 return -ENOMEM;
Ting-Yuan Huang6bc086c2016-11-15 15:35:16 -0800113 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700114 }
115
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700116 node->dp.perm = perm;
117 node->dp.uid = uid;
118 node->dp.gid = gid;
119 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700120 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700121
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700122 if (attr)
123 list_add_tail(&sys_perms, &node->plist);
124 else
125 list_add_tail(&dev_perms, &node->plist);
126
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127 return 0;
128}
129
Colin Cross43d537e2014-07-02 13:08:13 -0700130static bool perm_path_matches(const char *path, struct perms_ *dp)
131{
132 if (dp->prefix) {
133 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
134 return true;
135 } else if (dp->wildcard) {
136 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
137 return true;
138 } else {
139 if (strcmp(path, dp->name) == 0)
140 return true;
141 }
142
143 return false;
144}
145
Rob Herringb075bdf2016-05-06 10:06:59 -0500146static bool match_subsystem(perms_* dp, const char* pattern,
147 const char* path, const char* subsystem) {
148 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
149 return false;
150 }
Rob Herringb0f05082016-05-06 12:28:48 -0500151
Rob Herringb075bdf2016-05-06 10:06:59 -0500152 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
153 return perm_path_matches(subsys_path.c_str(), dp);
154}
Rob Herringb0f05082016-05-06 12:28:48 -0500155
Rob Herringb075bdf2016-05-06 10:06:59 -0500156static void fixup_sys_perms(const char* upath, const char* subsystem) {
157 // upaths omit the "/sys" that paths in this list
158 // contain, so we prepend it...
159 std::string path = std::string(SYSFS_PREFIX) + upath;
160
161 listnode* node;
Rob Herringb0f05082016-05-06 12:28:48 -0500162 list_for_each(node, &sys_perms) {
Rob Herringb075bdf2016-05-06 10:06:59 -0500163 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
164 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
165 ; // matched
166 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
167 ; // matched
168 } else if (!perm_path_matches(path.c_str(), dp)) {
169 continue;
Rob Herringb0f05082016-05-06 12:28:48 -0500170 }
171
172 std::string attr_file = path + "/" + dp->attr;
Elliott Hughes495d0be2016-06-24 15:12:21 -0700173 LOG(INFO) << "fixup " << attr_file
174 << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
Rob Herringb0f05082016-05-06 12:28:48 -0500175 chown(attr_file.c_str(), dp->uid, dp->gid);
176 chmod(attr_file.c_str(), dp->perm);
177 }
178
179 if (access(path.c_str(), F_OK) == 0) {
Dmitry Shmidt3a75e212016-07-28 13:55:39 -0700180 LOG(VERBOSE) << "restorecon_recursive: " << path;
Paul Lawrencee50c7b02016-11-14 15:40:18 -0800181 restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE);
Rob Herringb0f05082016-05-06 12:28:48 -0500182 }
183}
184
Colin Cross43d537e2014-07-02 13:08:13 -0700185static mode_t get_device_perm(const char *path, const char **links,
186 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700187{
Colin Cross44b65d02010-04-20 14:32:50 -0700188 struct listnode *node;
189 struct perm_node *perm_node;
190 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700191
Colin Cross44b65d02010-04-20 14:32:50 -0700192 /* search the perms list in reverse so that ueventd.$hardware can
193 * override ueventd.rc
194 */
195 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700196 bool match = false;
197
Colin Cross44b65d02010-04-20 14:32:50 -0700198 perm_node = node_to_item(node, struct perm_node, plist);
199 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700200
Colin Cross43d537e2014-07-02 13:08:13 -0700201 if (perm_path_matches(path, dp)) {
202 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700203 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700204 if (links) {
205 int i;
206 for (i = 0; links[i]; i++) {
207 if (perm_path_matches(links[i], dp)) {
208 match = true;
209 break;
210 }
211 }
212 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213 }
Colin Cross43d537e2014-07-02 13:08:13 -0700214
215 if (match) {
216 *uid = dp->uid;
217 *gid = dp->gid;
218 return dp->perm;
219 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220 }
Colin Cross44b65d02010-04-20 14:32:50 -0700221 /* Default if nothing found. */
222 *uid = 0;
223 *gid = 0;
224 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225}
226
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700227static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800228 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400229 int block, int major, int minor,
230 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700231{
232 unsigned uid;
233 unsigned gid;
234 mode_t mode;
235 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500236 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237
Colin Cross43d537e2014-07-02 13:08:13 -0700238 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700239
Sandeep Patilad6b1c72017-02-03 07:51:55 -0800240 if (sehandle) {
241 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
242 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
243 return;
244 }
245 setfscreatecon(secontext);
Mihai Serbanc84c6f72016-04-25 18:22:27 +0300246 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700247
Colin Cross17dcc5c2010-09-03 12:25:34 -0700248 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800249 /* Temporarily change egid to avoid race condition setting the gid of the
250 * device node. Unforunately changing the euid would prevent creation of
251 * some device nodes, so the uid has to be set with chown() and is still
252 * racy. Fixing the gid race at least fixed the issue with system_server
253 * opening dynamic input devices under the AID_INPUT gid. */
Tom Cherrycffdcba2017-02-23 13:46:09 -0800254 if (setegid(gid)) {
255 PLOG(ERROR) << "setegid(" << gid << ") for " << path << " device failed";
256 goto out;
257 }
Mihai Serbanc84c6f72016-04-25 18:22:27 +0300258 /* If the node already exists update its SELinux label to handle cases when
259 * it was created with the wrong context during coldboot procedure. */
Sandeep Patilad6b1c72017-02-03 07:51:55 -0800260 if (mknod(path, mode, dev) && (errno == EEXIST) && secontext) {
William Roberts730c7df2016-06-02 09:53:44 -0700261
262 char* fcon = nullptr;
263 int rc = lgetfilecon(path, &fcon);
264 if (rc < 0) {
Elliott Hughes495d0be2016-06-24 15:12:21 -0700265 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
William Roberts730c7df2016-06-02 09:53:44 -0700266 goto out;
267 }
268
269 bool different = strcmp(fcon, secontext) != 0;
270 freecon(fcon);
271
272 if (different && lsetfilecon(path, secontext)) {
Elliott Hughes495d0be2016-06-24 15:12:21 -0700273 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
Mihai Serbanc84c6f72016-04-25 18:22:27 +0300274 }
275 }
William Roberts730c7df2016-06-02 09:53:44 -0700276
277out:
Nick Pelly6405c692010-01-21 18:13:39 -0800278 chown(path, uid, -1);
Tom Cherrycffdcba2017-02-23 13:46:09 -0800279 if (setegid(AID_ROOT)) {
280 PLOG(FATAL) << "setegid(AID_ROOT) failed";
281 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700282
Sandeep Patilad6b1c72017-02-03 07:51:55 -0800283 if (secontext) {
284 freecon(secontext);
285 setfscreatecon(NULL);
286 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287}
288
Tom Cherry0af29882017-04-05 15:58:31 -0700289void add_platform_device(const char* path) {
Dima Zavinf395c922013-03-06 16:23:57 -0800290 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700291 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800292 const char *name = path;
293
294 if (!strncmp(path, "/devices/", 9)) {
295 name += 9;
296 if (!strncmp(name, "platform/", 9))
297 name += 9;
298 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700299
Wei Wanga4021c32016-10-04 14:05:39 -0700300 LOG(VERBOSE) << "adding platform device " << name << " (" << path << ")";
Colin Crossfadb85e2011-03-30 18:32:12 -0700301
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800302 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800303 bus->path = strdup(path);
304 bus->path_len = path_len;
305 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700306 list_add_tail(&platform_names, &bus->list);
307}
308
309/*
Dima Zavinf395c922013-03-06 16:23:57 -0800310 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700311 * platform device prefix. If it doesn't start with a platform device, return
312 * 0.
313 */
Dima Zavinf395c922013-03-06 16:23:57 -0800314static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700315{
Dima Zavinf395c922013-03-06 16:23:57 -0800316 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700317 struct listnode *node;
318 struct platform_node *bus;
319
320 list_for_each_reverse(node, &platform_names) {
321 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800322 if ((bus->path_len < path_len) &&
323 (path[bus->path_len] == '/') &&
324 !strncmp(path, bus->path, bus->path_len))
325 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700326 }
327
328 return NULL;
329}
330
Tom Cherry0af29882017-04-05 15:58:31 -0700331void remove_platform_device(const char* path) {
Colin Crossfadb85e2011-03-30 18:32:12 -0700332 struct listnode *node;
333 struct platform_node *bus;
334
335 list_for_each_reverse(node, &platform_names) {
336 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800337 if (!strcmp(path, bus->path)) {
Elliott Hughes495d0be2016-06-24 15:12:21 -0700338 LOG(INFO) << "removing platform device " << bus->name;
Dima Zavinf395c922013-03-06 16:23:57 -0800339 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700340 list_remove(node);
341 free(bus);
342 return;
343 }
344 }
345}
346
Sandeep Patilc6adcc52017-02-08 20:27:12 -0800347static void destroy_platform_devices() {
348 struct listnode* node;
349 struct listnode* n;
350 struct platform_node* bus;
351
352 list_for_each_safe(node, n, &platform_names) {
353 list_remove(node);
354 bus = node_to_item(node, struct platform_node, list);
355 free(bus->path);
356 free(bus);
357 }
358}
359
Andrew Boiea885d042013-09-13 17:41:20 -0700360/* Given a path that may start with a PCI device, populate the supplied buffer
361 * with the PCI domain/bus number and the peripheral ID and return 0.
362 * If it doesn't start with a PCI device, or there is some error, return -1 */
363static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
364{
365 const char *start, *end;
366
367 if (strncmp(path, "/devices/pci", 12))
368 return -1;
369
370 /* Beginning of the prefix is the initial "pci" after "/devices/" */
371 start = path + 9;
372
373 /* End of the prefix is two path '/' later, capturing the domain/bus number
374 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
375 end = strchr(start, '/');
376 if (!end)
377 return -1;
378 end = strchr(end + 1, '/');
379 if (!end)
380 return -1;
381
382 /* Make sure we have enough room for the string plus null terminator */
383 if (end - start + 1 > buf_sz)
384 return -1;
385
386 strncpy(buf, start, end - start);
387 buf[end - start] = '\0';
388 return 0;
389}
390
Jeremy Compostella5e0b52b2017-03-03 16:27:29 +0100391/* Given a path that may start with a virtual block device, populate
392 * the supplied buffer with the virtual block device ID and return 0.
393 * If it doesn't start with a virtual block device, or there is some
394 * error, return -1 */
395static int find_vbd_device_prefix(const char *path, char *buf, ssize_t buf_sz)
396{
397 const char *start, *end;
398
399 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
400 if (strncmp(path, "/devices/vbd-", 13))
401 return -1;
402
403 /* End of the prefix is one path '/' later, capturing the
404 virtual block device ID. Example: 768 */
405 start = path + 13;
406 end = strchr(start, '/');
407 if (!end)
408 return -1;
409
410 /* Make sure we have enough room for the string plus null terminator */
411 if (end - start + 1 > buf_sz)
412 return -1;
413
414 strncpy(buf, start, end - start);
415 buf[end - start] = '\0';
416 return 0;
417}
418
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700419static void parse_event(const char *msg, struct uevent *uevent)
420{
421 uevent->action = "";
422 uevent->path = "";
423 uevent->subsystem = "";
424 uevent->firmware = "";
425 uevent->major = -1;
426 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700427 uevent->partition_name = NULL;
428 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700429 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430
431 /* currently ignoring SEQNUM */
432 while(*msg) {
433 if(!strncmp(msg, "ACTION=", 7)) {
434 msg += 7;
435 uevent->action = msg;
436 } else if(!strncmp(msg, "DEVPATH=", 8)) {
437 msg += 8;
438 uevent->path = msg;
439 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
440 msg += 10;
441 uevent->subsystem = msg;
442 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
443 msg += 9;
444 uevent->firmware = msg;
445 } else if(!strncmp(msg, "MAJOR=", 6)) {
446 msg += 6;
447 uevent->major = atoi(msg);
448 } else if(!strncmp(msg, "MINOR=", 6)) {
449 msg += 6;
450 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700451 } else if(!strncmp(msg, "PARTN=", 6)) {
452 msg += 6;
453 uevent->partition_num = atoi(msg);
454 } else if(!strncmp(msg, "PARTNAME=", 9)) {
455 msg += 9;
456 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700457 } else if(!strncmp(msg, "DEVNAME=", 8)) {
458 msg += 8;
459 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700460 }
461
Wei Zhongf97b8872012-03-23 14:15:34 -0700462 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700463 while(*msg++)
464 ;
465 }
466
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800467 if (LOG_UEVENTS) {
Elliott Hughes495d0be2016-06-24 15:12:21 -0700468 LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
469 uevent->action, uevent->path, uevent->subsystem,
470 uevent->firmware, uevent->major, uevent->minor);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800471 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700472}
473
Tom Cherry0af29882017-04-05 15:58:31 -0700474char** get_character_device_symlinks(struct uevent* uevent) {
Benoit Gobyd2278632010-08-03 14:36:02 -0700475 const char *parent;
Dan Austin2bb6c982016-03-28 14:22:12 -0700476 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700477 char **links;
478 int link_num = 0;
479 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800480 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700481
Dima Zavinf395c922013-03-06 16:23:57 -0800482 pdev = find_platform_device(uevent->path);
483 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700484 return NULL;
485
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800486 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700487 if (!links)
488 return NULL;
489 memset(links, 0, sizeof(char *) * 2);
490
491 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800492 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100493 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700494 goto err;
495
496 if (!strncmp(parent, "/usb", 4)) {
497 /* skip root hub name and device. use device interface */
498 while (*++parent && *parent != '/');
499 if (*parent)
500 while (*++parent && *parent != '/');
501 if (!*parent)
502 goto err;
503 slash = strchr(++parent, '/');
504 if (!slash)
505 goto err;
506 width = slash - parent;
507 if (width <= 0)
508 goto err;
509
510 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
511 link_num++;
512 else
513 links[link_num] = NULL;
514 mkdir("/dev/usb", 0755);
515 }
516 else {
517 goto err;
518 }
519
520 return links;
521err:
522 free(links);
523 return NULL;
524}
525
Tom Cherry0af29882017-04-05 15:58:31 -0700526// replaces any unacceptable characters with '_', the
527// length of the resulting string is equal to the input string
528void sanitize_partition_name(char* s) {
529 const char* accept =
530 "abcdefghijklmnopqrstuvwxyz"
531 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
532 "0123456789"
533 "_-.";
534
535 if (!s) return;
536
537 while (*s) {
538 s += strspn(s, accept);
539 if (*s) *s++ = '_';
540 }
541}
542
543char** get_block_device_symlinks(struct uevent* uevent) {
Colin Crossfadb85e2011-03-30 18:32:12 -0700544 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800545 struct platform_node *pdev;
Dan Austin2bb6c982016-03-28 14:22:12 -0700546 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700547 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700548 char buf[256];
549 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700550 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700551 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700552
Dima Zavinf395c922013-03-06 16:23:57 -0800553 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700554 if (pdev) {
555 device = pdev->name;
556 type = "platform";
557 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
558 device = buf;
559 type = "pci";
Jeremy Compostella5e0b52b2017-03-03 16:27:29 +0100560 } else if (!find_vbd_device_prefix(uevent->path, buf, sizeof(buf))) {
561 device = buf;
562 type = "vbd";
Andrew Boiea885d042013-09-13 17:41:20 -0700563 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800564 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700565 }
Dima Zavinf395c922013-03-06 16:23:57 -0800566
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800567 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700568 if (!links)
569 return NULL;
570 memset(links, 0, sizeof(char *) * 4);
571
Sandeep Patilc6adcc52017-02-08 20:27:12 -0800572 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700573
Andrew Boiea885d042013-09-13 17:41:20 -0700574 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700575
576 if (uevent->partition_name) {
577 p = strdup(uevent->partition_name);
Tom Cherry0af29882017-04-05 15:58:31 -0700578 sanitize_partition_name(p);
Elliott Hughes495d0be2016-06-24 15:12:21 -0700579 if (strcmp(uevent->partition_name, p)) {
580 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
581 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700582 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
583 link_num++;
584 else
585 links[link_num] = NULL;
586 free(p);
587 }
588
589 if (uevent->partition_num >= 0) {
590 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
591 link_num++;
592 else
593 links[link_num] = NULL;
594 }
595
Dima Zavinf395c922013-03-06 16:23:57 -0800596 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700597 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
598 link_num++;
599 else
600 links[link_num] = NULL;
601
602 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700603}
604
Elliott Hughes41d07882016-08-31 14:41:51 -0700605static void make_link_init(const char* oldpath, const char* newpath) {
606 const char* slash = strrchr(newpath, '/');
607 if (!slash) return;
608
609 if (mkdir_recursive(dirname(newpath), 0755)) {
610 PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
611 }
612
613 if (symlink(oldpath, newpath) && errno != EEXIST) {
614 PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
615 }
616}
617
618static void remove_link(const char* oldpath, const char* newpath) {
619 std::string path;
620 if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
621}
622
Colin Crosseb5ba832011-03-30 17:37:17 -0700623static void handle_device(const char *action, const char *devpath,
624 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700625{
Colin Crosseb5ba832011-03-30 17:37:17 -0700626 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400627 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700628 if (links) {
Elliott Hughes41d07882016-08-31 14:41:51 -0700629 for (int i = 0; links[i]; i++) {
Chris Fries79f33842013-09-05 13:19:21 -0500630 make_link_init(devpath, links[i]);
Elliott Hughes41d07882016-08-31 14:41:51 -0700631 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700632 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700633 }
634
Colin Crosseb5ba832011-03-30 17:37:17 -0700635 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700636 if (links) {
Elliott Hughes41d07882016-08-31 14:41:51 -0700637 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700638 remove_link(devpath, links[i]);
Elliott Hughes41d07882016-08-31 14:41:51 -0700639 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700640 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700641 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700642 }
643
644 if (links) {
Elliott Hughes41d07882016-08-31 14:41:51 -0700645 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700646 free(links[i]);
Elliott Hughes41d07882016-08-31 14:41:51 -0700647 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700648 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700649 }
650}
651
Colin Crossfadb85e2011-03-30 18:32:12 -0700652static void handle_platform_device_event(struct uevent *uevent)
653{
Dima Zavinf395c922013-03-06 16:23:57 -0800654 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700655
656 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800657 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700658 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800659 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700660}
661
Colin Crosseb5ba832011-03-30 17:37:17 -0700662static const char *parse_device_name(struct uevent *uevent, unsigned int len)
663{
664 const char *name;
665
666 /* if it's not a /dev device, nothing else to do */
667 if((uevent->major < 0) || (uevent->minor < 0))
668 return NULL;
669
670 /* do we have a name? */
671 name = strrchr(uevent->path, '/');
672 if(!name)
673 return NULL;
674 name++;
675
676 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800677 if(strlen(name) > len) {
Elliott Hughes495d0be2016-06-24 15:12:21 -0700678 LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
Colin Crosseb5ba832011-03-30 17:37:17 -0700679 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800680 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700681
682 return name;
683}
684
Hung-ying Tyand816f962016-02-01 15:07:40 +0800685#define DEVPATH_LEN 96
686#define MAX_DEV_NAME 64
687
Colin Crosseb5ba832011-03-30 17:37:17 -0700688static void handle_block_device_event(struct uevent *uevent)
689{
690 const char *base = "/dev/block/";
691 const char *name;
Hung-ying Tyand816f962016-02-01 15:07:40 +0800692 char devpath[DEVPATH_LEN];
Colin Crosseb5ba832011-03-30 17:37:17 -0700693 char **links = NULL;
694
Hung-ying Tyand816f962016-02-01 15:07:40 +0800695 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700696 if (!name)
697 return;
698
699 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500700 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700701
Dima Zavinf395c922013-03-06 16:23:57 -0800702 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700703 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700704
705 handle_device(uevent->action, devpath, uevent->path, 1,
706 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700707}
708
Greg Hackmann3312aa82013-11-18 15:24:40 -0800709static bool assemble_devpath(char *devpath, const char *dirname,
710 const char *devname)
711{
712 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
713 if (s < 0) {
Elliott Hughes495d0be2016-06-24 15:12:21 -0700714 PLOG(ERROR) << "failed to assemble device path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800715 return false;
716 } else if (s >= DEVPATH_LEN) {
Elliott Hughes495d0be2016-06-24 15:12:21 -0700717 LOG(ERROR) << dirname << "/" << devname
718 << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800719 return false;
720 }
721 return true;
722}
723
724static void mkdir_recursive_for_devpath(const char *devpath)
725{
726 char dir[DEVPATH_LEN];
727 char *slash;
728
729 strcpy(dir, devpath);
730 slash = strrchr(dir, '/');
731 *slash = '\0';
732 mkdir_recursive(dir, 0755);
733}
734
Colin Crosseb5ba832011-03-30 17:37:17 -0700735static void handle_generic_device_event(struct uevent *uevent)
736{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800737 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700738 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800739 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700740 char **links = NULL;
741
Hung-ying Tyand816f962016-02-01 15:07:40 +0800742 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700743 if (!name)
744 return;
745
Greg Hackmann3312aa82013-11-18 15:24:40 -0800746 struct ueventd_subsystem *subsystem =
747 ueventd_subsystem_find_by_name(uevent->subsystem);
748
749 if (subsystem) {
750 const char *devname;
751
752 switch (subsystem->devname_src) {
753 case DEVNAME_UEVENT_DEVNAME:
754 devname = uevent->device_name;
755 break;
756
757 case DEVNAME_UEVENT_DEVPATH:
758 devname = name;
759 break;
760
761 default:
Elliott Hughes495d0be2016-06-24 15:12:21 -0700762 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800763 return;
764 }
765
766 if (!assemble_devpath(devpath, subsystem->dirname, devname))
767 return;
768 mkdir_recursive_for_devpath(devpath);
769 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700770 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700771 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800772 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800773 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800774 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700775 }
776 else {
777 /* This imitates the file system that would be created
778 * if we were using devfs instead.
779 * Minors are broken up into groups of 128, starting at "001"
780 */
781 int bus_id = uevent->minor / 128 + 1;
782 int device_id = uevent->minor % 128 + 1;
783 /* build directories */
784 make_dir("/dev/bus", 0755);
785 make_dir("/dev/bus/usb", 0755);
786 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
787 make_dir(devpath, 0755);
788 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
789 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700790 } else {
791 /* ignore other USB events */
792 return;
793 }
794 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
795 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500796 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200797 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
798 base = "/dev/dri/";
799 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700800 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
801 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500802 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700803 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
804 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500805 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700806 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
807 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500808 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700809 } else if(!strncmp(uevent->subsystem, "input", 5)) {
810 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500811 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700812 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
813 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500814 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700815 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
816 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500817 make_dir(base, 0755);
Elliott Hughes495d0be2016-06-24 15:12:21 -0700818 } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
819 LOG(INFO) << "kernel logger is deprecated";
Colin Crosseb5ba832011-03-30 17:37:17 -0700820 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500821 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700822 name += 4;
823 } else
824 base = "/dev/";
825 links = get_character_device_symlinks(uevent);
826
827 if (!devpath[0])
828 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
829
830 handle_device(uevent->action, devpath, uevent->path, 0,
831 uevent->major, uevent->minor, links);
832}
833
834static void handle_device_event(struct uevent *uevent)
835{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700836 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herringb075bdf2016-05-06 10:06:59 -0500837 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700838
839 if (!strncmp(uevent->subsystem, "block", 5)) {
840 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700841 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
842 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700843 } else {
844 handle_generic_device_event(uevent);
845 }
846}
847
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800848static void load_firmware(uevent* uevent, const std::string& root,
849 int fw_fd, size_t fw_size,
850 int loading_fd, int data_fd) {
851 // Start transfer.
852 android::base::WriteFully(loading_fd, "1", 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700853
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800854 // Copy the firmware.
855 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
856 if (rc == -1) {
857 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent->firmware << "' }";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858 }
859
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800860 // Tell the firmware whether to abort or commit.
861 const char* response = (rc != -1) ? "0" : "-1";
862 android::base::WriteFully(loading_fd, response, strlen(response));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700863}
864
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800865static int is_booting() {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700866 return access("/dev/.booting", F_OK) == 0;
867}
868
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800869static void process_firmware_event(uevent* uevent) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700870 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700871
Elliott Hughes495d0be2016-06-24 15:12:21 -0700872 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700873
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800874 std::string root = android::base::StringPrintf("/sys%s", uevent->path);
875 std::string loading = root + "/loading";
876 std::string data = root + "/data";
877
878 android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY|O_CLOEXEC));
879 if (loading_fd == -1) {
880 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700881 return;
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800882 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700883
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800884 android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY|O_CLOEXEC));
885 if (data_fd == -1) {
886 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent->firmware;
887 return;
888 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700889
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700890try_loading_again:
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800891 for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
892 std::string file = android::base::StringPrintf("%s/%s", firmware_dirs[i], uevent->firmware);
893 android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY|O_CLOEXEC));
894 struct stat sb;
895 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
896 load_firmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
897 return;
Benoit Goby609d8822010-11-09 18:10:24 -0800898 }
Brian Swetland02863b92010-09-19 03:36:39 -0700899 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700900
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800901 if (booting) {
902 // If we're not fully booted, we may be missing
903 // filesystems needed for firmware, wait and retry.
Elliott Hughes1c5fa222016-11-14 17:08:47 -0800904 std::this_thread::sleep_for(100ms);
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800905 booting = is_booting();
906 goto try_loading_again;
907 }
908
909 LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware;
910
911 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
912 write(loading_fd, "-1", 2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700913}
914
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800915static void handle_firmware_event(uevent* uevent) {
916 if (strcmp(uevent->subsystem, "firmware")) return;
917 if (strcmp(uevent->action, "add")) return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700918
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800919 // Loading the firmware in a child means we can do that in parallel...
920 // (We ignore SIGCHLD rather than wait for our children.)
921 pid_t pid = fork();
922 if (pid == 0) {
923 Timer t;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700924 process_firmware_event(uevent);
Elliott Hughes7dd28482016-11-29 19:20:58 +0000925 LOG(INFO) << "loading " << uevent->path << " took " << t;
Kenny Root17baff42014-08-20 16:16:44 -0700926 _exit(EXIT_SUCCESS);
Elliott Hughes6d5fd492016-11-12 11:44:16 -0800927 } else if (pid == -1) {
928 PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700929 }
930}
931
Sandeep Patil592ba0f2017-02-07 18:11:37 -0800932static bool inline should_stop_coldboot(coldboot_action_t act)
933{
934 return (act == COLDBOOT_STOP || act == COLDBOOT_FINISH);
935}
936
Ruchi Kandoic6037202014-06-23 11:22:09 -0700937#define UEVENT_MSG_LEN 2048
Hung-ying Tyand816f962016-02-01 15:07:40 +0800938
Sandeep Patilf4ca7c62017-02-08 15:49:47 -0800939static inline coldboot_action_t handle_device_fd_with(
940 std::function<coldboot_action_t(uevent* uevent)> handle_uevent)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700941{
Vernon Tang3f582e92011-04-25 13:08:17 +1000942 char msg[UEVENT_MSG_LEN+2];
943 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700944 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700945 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700946 continue;
947
948 msg[n] = '\0';
949 msg[n+1] = '\0';
950
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700951 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700952 parse_event(msg, &uevent);
Sandeep Patil592ba0f2017-02-07 18:11:37 -0800953 coldboot_action_t act = handle_uevent(&uevent);
954 if (should_stop_coldboot(act))
955 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700956 }
Sandeep Patil592ba0f2017-02-07 18:11:37 -0800957
958 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700959}
960
Sandeep Patil592ba0f2017-02-07 18:11:37 -0800961coldboot_action_t handle_device_fd(coldboot_callback fn)
Hung-ying Tyand816f962016-02-01 15:07:40 +0800962{
Sandeep Patil592ba0f2017-02-07 18:11:37 -0800963 coldboot_action_t ret = handle_device_fd_with(
964 [&](uevent* uevent) -> coldboot_action_t {
Hung-ying Tyand816f962016-02-01 15:07:40 +0800965 if (selinux_status_updated() > 0) {
966 struct selabel_handle *sehandle2;
967 sehandle2 = selinux_android_file_context_handle();
968 if (sehandle2) {
969 selabel_close(sehandle);
970 sehandle = sehandle2;
971 }
972 }
973
Sandeep Patil592ba0f2017-02-07 18:11:37 -0800974 // default is to always create the devices
975 coldboot_action_t act = COLDBOOT_CREATE;
976 if (fn) {
977 act = fn(uevent);
978 }
979
980 if (act == COLDBOOT_CREATE || act == COLDBOOT_STOP) {
981 handle_device_event(uevent);
982 handle_firmware_event(uevent);
983 }
984
985 return act;
Hung-ying Tyand816f962016-02-01 15:07:40 +0800986 });
Sandeep Patil592ba0f2017-02-07 18:11:37 -0800987
988 return ret;
Hung-ying Tyand816f962016-02-01 15:07:40 +0800989}
990
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700991/* Coldboot walks parts of the /sys tree and pokes the uevent files
992** to cause the kernel to regenerate device add events that happened
993** before init's device manager was started
994**
995** We drain any pending events from the netlink socket every time
996** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800997** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700998*/
999
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001000static coldboot_action_t do_coldboot(DIR *d, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001001{
1002 struct dirent *de;
1003 int dfd, fd;
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001004 coldboot_action_t act = COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001005
1006 dfd = dirfd(d);
1007
1008 fd = openat(dfd, "uevent", O_WRONLY);
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001009 if (fd >= 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001010 write(fd, "add\n", 4);
1011 close(fd);
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001012 act = handle_device_fd(fn);
1013 if (should_stop_coldboot(act))
1014 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001015 }
1016
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001017 while (!should_stop_coldboot(act) && (de = readdir(d))) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001018 DIR *d2;
1019
1020 if(de->d_type != DT_DIR || de->d_name[0] == '.')
1021 continue;
1022
1023 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
1024 if(fd < 0)
1025 continue;
1026
1027 d2 = fdopendir(fd);
1028 if(d2 == 0)
1029 close(fd);
1030 else {
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001031 act = do_coldboot(d2, fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001032 closedir(d2);
1033 }
1034 }
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001035
1036 // default is always to continue looking for uevents
1037 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001038}
1039
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001040static coldboot_action_t coldboot(const char *path, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001041{
James Hawkinsfb6bb0a2016-02-18 14:52:46 -08001042 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001043 if (d) {
1044 return do_coldboot(d.get(), fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001045 }
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001046
1047 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001048}
1049
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001050void device_init(const char* path, coldboot_callback fn) {
Sandeep Patil3861b492017-02-15 13:37:52 -08001051 if (!sehandle) {
1052 sehandle = selinux_android_file_context_handle();
Elliott Hughesb9f7fb32015-03-28 11:23:32 -07001053 }
Sandeep Patil3861b492017-02-15 13:37:52 -08001054 // open uevent socket and selinux status only if it hasn't been
1055 // done before
1056 if (device_fd == -1) {
1057 /* is 256K enough? udev uses 16MB! */
1058 device_fd.reset(uevent_open_socket(256 * 1024, true));
1059 if (device_fd == -1) {
1060 return;
1061 }
1062 fcntl(device_fd, F_SETFL, O_NONBLOCK);
1063 selinux_status_open(true);
1064 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001065
Elliott Hughesb9f7fb32015-03-28 11:23:32 -07001066 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughes495d0be2016-06-24 15:12:21 -07001067 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughesb9f7fb32015-03-28 11:23:32 -07001068 return;
Colin Crossf83d0b92010-04-21 12:04:20 -07001069 }
Elliott Hughesb9f7fb32015-03-28 11:23:32 -07001070
1071 Timer t;
Sandeep Patil592ba0f2017-02-07 18:11:37 -08001072 coldboot_action_t act;
1073 if (!path) {
1074 act = coldboot("/sys/class", fn);
1075 if (!should_stop_coldboot(act)) {
1076 act = coldboot("/sys/block", fn);
1077 if (!should_stop_coldboot(act)) {
1078 act = coldboot("/sys/devices", fn);
1079 }
1080 }
1081 } else {
1082 act = coldboot(path, fn);
1083 }
1084
1085 // If we have a callback, then do as it says. If no, then the default is
1086 // to always create COLDBOOT_DONE file.
1087 if (!fn || (act == COLDBOOT_FINISH)) {
1088 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
1089 }
1090
Elliott Hughes7dd28482016-11-29 19:20:58 +00001091 LOG(INFO) << "Coldboot took " << t;
Colin Cross0dd7ca62010-04-13 19:25:51 -07001092}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001093
Sandeep Patilc6adcc52017-02-08 20:27:12 -08001094void device_close() {
1095 destroy_platform_devices();
1096 device_fd.reset();
Sandeep Patil3861b492017-02-15 13:37:52 -08001097 selinux_status_close();
Sandeep Patilc6adcc52017-02-08 20:27:12 -08001098}
1099
Elliott Hughes6d5fd492016-11-12 11:44:16 -08001100int get_device_fd() {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001101 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001102}