blob: 760e0f521dc4f856a0724d5feaeaf132ba0cfd67 [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
Tom Cherryc44f6a42017-04-05 15:58:31 -0700285void add_platform_device(const char* path) {
Dima Zavinf395c922013-03-06 16:23:57 -0800286 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700287 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800288 const char *name = path;
289
290 if (!strncmp(path, "/devices/", 9)) {
291 name += 9;
292 if (!strncmp(name, "platform/", 9))
293 name += 9;
294 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700295
Wei Wanga285dac2016-10-04 14:05:39 -0700296 LOG(VERBOSE) << "adding platform device " << name << " (" << path << ")";
Colin Crossfadb85e2011-03-30 18:32:12 -0700297
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800298 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800299 bus->path = strdup(path);
300 bus->path_len = path_len;
301 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700302 list_add_tail(&platform_names, &bus->list);
303}
304
305/*
Dima Zavinf395c922013-03-06 16:23:57 -0800306 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700307 * platform device prefix. If it doesn't start with a platform device, return
308 * 0.
309 */
Dima Zavinf395c922013-03-06 16:23:57 -0800310static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700311{
Dima Zavinf395c922013-03-06 16:23:57 -0800312 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700313 struct listnode *node;
314 struct platform_node *bus;
315
316 list_for_each_reverse(node, &platform_names) {
317 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800318 if ((bus->path_len < path_len) &&
319 (path[bus->path_len] == '/') &&
320 !strncmp(path, bus->path, bus->path_len))
321 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700322 }
323
324 return NULL;
325}
326
Tom Cherryc44f6a42017-04-05 15:58:31 -0700327void remove_platform_device(const char* path) {
Colin Crossfadb85e2011-03-30 18:32:12 -0700328 struct listnode *node;
329 struct platform_node *bus;
330
331 list_for_each_reverse(node, &platform_names) {
332 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800333 if (!strcmp(path, bus->path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700334 LOG(INFO) << "removing platform device " << bus->name;
Dima Zavinf395c922013-03-06 16:23:57 -0800335 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700336 list_remove(node);
337 free(bus);
338 return;
339 }
340 }
341}
342
Sandeep Patil35403eb2017-02-08 20:27:12 -0800343static void destroy_platform_devices() {
344 struct listnode* node;
345 struct listnode* n;
346 struct platform_node* bus;
347
348 list_for_each_safe(node, n, &platform_names) {
349 list_remove(node);
350 bus = node_to_item(node, struct platform_node, list);
351 free(bus->path);
352 free(bus);
353 }
354}
355
Andrew Boiea885d042013-09-13 17:41:20 -0700356/* Given a path that may start with a PCI device, populate the supplied buffer
357 * with the PCI domain/bus number and the peripheral ID and return 0.
358 * If it doesn't start with a PCI device, or there is some error, return -1 */
359static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
360{
361 const char *start, *end;
362
363 if (strncmp(path, "/devices/pci", 12))
364 return -1;
365
366 /* Beginning of the prefix is the initial "pci" after "/devices/" */
367 start = path + 9;
368
369 /* End of the prefix is two path '/' later, capturing the domain/bus number
370 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
371 end = strchr(start, '/');
372 if (!end)
373 return -1;
374 end = strchr(end + 1, '/');
375 if (!end)
376 return -1;
377
378 /* Make sure we have enough room for the string plus null terminator */
379 if (end - start + 1 > buf_sz)
380 return -1;
381
382 strncpy(buf, start, end - start);
383 buf[end - start] = '\0';
384 return 0;
385}
386
Jeremy Compostella937309d2017-03-03 16:27:29 +0100387/* Given a path that may start with a virtual block device, populate
388 * the supplied buffer with the virtual block device ID and return 0.
389 * If it doesn't start with a virtual block device, or there is some
390 * error, return -1 */
391static int find_vbd_device_prefix(const char *path, char *buf, ssize_t buf_sz)
392{
393 const char *start, *end;
394
395 /* Beginning of the prefix is the initial "vbd-" after "/devices/" */
396 if (strncmp(path, "/devices/vbd-", 13))
397 return -1;
398
399 /* End of the prefix is one path '/' later, capturing the
400 virtual block device ID. Example: 768 */
401 start = path + 13;
402 end = strchr(start, '/');
403 if (!end)
404 return -1;
405
406 /* Make sure we have enough room for the string plus null terminator */
407 if (end - start + 1 > buf_sz)
408 return -1;
409
410 strncpy(buf, start, end - start);
411 buf[end - start] = '\0';
412 return 0;
413}
414
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415static void parse_event(const char *msg, struct uevent *uevent)
416{
417 uevent->action = "";
418 uevent->path = "";
419 uevent->subsystem = "";
420 uevent->firmware = "";
421 uevent->major = -1;
422 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700423 uevent->partition_name = NULL;
424 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700425 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700426
427 /* currently ignoring SEQNUM */
428 while(*msg) {
429 if(!strncmp(msg, "ACTION=", 7)) {
430 msg += 7;
431 uevent->action = msg;
432 } else if(!strncmp(msg, "DEVPATH=", 8)) {
433 msg += 8;
434 uevent->path = msg;
435 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
436 msg += 10;
437 uevent->subsystem = msg;
438 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
439 msg += 9;
440 uevent->firmware = msg;
441 } else if(!strncmp(msg, "MAJOR=", 6)) {
442 msg += 6;
443 uevent->major = atoi(msg);
444 } else if(!strncmp(msg, "MINOR=", 6)) {
445 msg += 6;
446 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700447 } else if(!strncmp(msg, "PARTN=", 6)) {
448 msg += 6;
449 uevent->partition_num = atoi(msg);
450 } else if(!strncmp(msg, "PARTNAME=", 9)) {
451 msg += 9;
452 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700453 } else if(!strncmp(msg, "DEVNAME=", 8)) {
454 msg += 8;
455 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700456 }
457
Wei Zhongf97b8872012-03-23 14:15:34 -0700458 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700459 while(*msg++)
460 ;
461 }
462
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800463 if (LOG_UEVENTS) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700464 LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
465 uevent->action, uevent->path, uevent->subsystem,
466 uevent->firmware, uevent->major, uevent->minor);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800467 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700468}
469
Tom Cherryc44f6a42017-04-05 15:58:31 -0700470char** get_character_device_symlinks(struct uevent* uevent) {
Benoit Gobyd2278632010-08-03 14:36:02 -0700471 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700472 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700473 char **links;
474 int link_num = 0;
475 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800476 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700477
Dima Zavinf395c922013-03-06 16:23:57 -0800478 pdev = find_platform_device(uevent->path);
479 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700480 return NULL;
481
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800482 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700483 if (!links)
484 return NULL;
485 memset(links, 0, sizeof(char *) * 2);
486
487 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800488 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100489 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700490 goto err;
491
492 if (!strncmp(parent, "/usb", 4)) {
493 /* skip root hub name and device. use device interface */
494 while (*++parent && *parent != '/');
495 if (*parent)
496 while (*++parent && *parent != '/');
497 if (!*parent)
498 goto err;
499 slash = strchr(++parent, '/');
500 if (!slash)
501 goto err;
502 width = slash - parent;
503 if (width <= 0)
504 goto err;
505
506 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
507 link_num++;
508 else
509 links[link_num] = NULL;
510 mkdir("/dev/usb", 0755);
511 }
512 else {
513 goto err;
514 }
515
516 return links;
517err:
518 free(links);
519 return NULL;
520}
521
Tom Cherryc44f6a42017-04-05 15:58:31 -0700522// replaces any unacceptable characters with '_', the
523// length of the resulting string is equal to the input string
524void sanitize_partition_name(char* s) {
525 const char* accept =
526 "abcdefghijklmnopqrstuvwxyz"
527 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
528 "0123456789"
529 "_-.";
530
531 if (!s) return;
532
533 while (*s) {
534 s += strspn(s, accept);
535 if (*s) *s++ = '_';
536 }
537}
538
539char** get_block_device_symlinks(struct uevent* uevent) {
Colin Crossfadb85e2011-03-30 18:32:12 -0700540 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800541 struct platform_node *pdev;
Dan Austin60b976d2016-03-28 14:22:12 -0700542 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700543 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700544 char buf[256];
545 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700546 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700547 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700548
Dima Zavinf395c922013-03-06 16:23:57 -0800549 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700550 if (pdev) {
551 device = pdev->name;
552 type = "platform";
553 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
554 device = buf;
555 type = "pci";
Jeremy Compostella937309d2017-03-03 16:27:29 +0100556 } else if (!find_vbd_device_prefix(uevent->path, buf, sizeof(buf))) {
557 device = buf;
558 type = "vbd";
Andrew Boiea885d042013-09-13 17:41:20 -0700559 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800560 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700561 }
Dima Zavinf395c922013-03-06 16:23:57 -0800562
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800563 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700564 if (!links)
565 return NULL;
566 memset(links, 0, sizeof(char *) * 4);
567
Sandeep Patil35403eb2017-02-08 20:27:12 -0800568 LOG(VERBOSE) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700569
Andrew Boiea885d042013-09-13 17:41:20 -0700570 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700571
572 if (uevent->partition_name) {
573 p = strdup(uevent->partition_name);
Tom Cherryc44f6a42017-04-05 15:58:31 -0700574 sanitize_partition_name(p);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700575 if (strcmp(uevent->partition_name, p)) {
576 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
577 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700578 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
579 link_num++;
580 else
581 links[link_num] = NULL;
582 free(p);
583 }
584
585 if (uevent->partition_num >= 0) {
586 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
587 link_num++;
588 else
589 links[link_num] = NULL;
590 }
591
Dima Zavinf395c922013-03-06 16:23:57 -0800592 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700593 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
594 link_num++;
595 else
596 links[link_num] = NULL;
597
598 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700599}
600
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700601static void make_link_init(const char* oldpath, const char* newpath) {
602 const char* slash = strrchr(newpath, '/');
603 if (!slash) return;
604
605 if (mkdir_recursive(dirname(newpath), 0755)) {
606 PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
607 }
608
609 if (symlink(oldpath, newpath) && errno != EEXIST) {
610 PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
611 }
612}
613
614static void remove_link(const char* oldpath, const char* newpath) {
615 std::string path;
616 if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
617}
618
Colin Crosseb5ba832011-03-30 17:37:17 -0700619static void handle_device(const char *action, const char *devpath,
620 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700621{
Colin Crosseb5ba832011-03-30 17:37:17 -0700622 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400623 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700624 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700625 for (int i = 0; links[i]; i++) {
Chris Fries79f33842013-09-05 13:19:21 -0500626 make_link_init(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700627 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700628 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700629 }
630
Colin Crosseb5ba832011-03-30 17:37:17 -0700631 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700632 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700633 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700634 remove_link(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700635 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700636 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700637 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700638 }
639
640 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700641 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700642 free(links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700643 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700644 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700645 }
646}
647
Colin Crossfadb85e2011-03-30 18:32:12 -0700648static void handle_platform_device_event(struct uevent *uevent)
649{
Dima Zavinf395c922013-03-06 16:23:57 -0800650 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700651
652 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800653 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700654 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800655 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700656}
657
Colin Crosseb5ba832011-03-30 17:37:17 -0700658static const char *parse_device_name(struct uevent *uevent, unsigned int len)
659{
660 const char *name;
661
662 /* if it's not a /dev device, nothing else to do */
663 if((uevent->major < 0) || (uevent->minor < 0))
664 return NULL;
665
666 /* do we have a name? */
667 name = strrchr(uevent->path, '/');
668 if(!name)
669 return NULL;
670 name++;
671
672 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800673 if(strlen(name) > len) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700674 LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
Colin Crosseb5ba832011-03-30 17:37:17 -0700675 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800676 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700677
678 return name;
679}
680
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800681#define DEVPATH_LEN 96
682#define MAX_DEV_NAME 64
683
Colin Crosseb5ba832011-03-30 17:37:17 -0700684static void handle_block_device_event(struct uevent *uevent)
685{
686 const char *base = "/dev/block/";
687 const char *name;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800688 char devpath[DEVPATH_LEN];
Colin Crosseb5ba832011-03-30 17:37:17 -0700689 char **links = NULL;
690
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800691 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700692 if (!name)
693 return;
694
695 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500696 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700697
Dima Zavinf395c922013-03-06 16:23:57 -0800698 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700699 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700700
701 handle_device(uevent->action, devpath, uevent->path, 1,
702 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700703}
704
Greg Hackmann3312aa82013-11-18 15:24:40 -0800705static bool assemble_devpath(char *devpath, const char *dirname,
706 const char *devname)
707{
708 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
709 if (s < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700710 PLOG(ERROR) << "failed to assemble device path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800711 return false;
712 } else if (s >= DEVPATH_LEN) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700713 LOG(ERROR) << dirname << "/" << devname
714 << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800715 return false;
716 }
717 return true;
718}
719
720static void mkdir_recursive_for_devpath(const char *devpath)
721{
722 char dir[DEVPATH_LEN];
723 char *slash;
724
725 strcpy(dir, devpath);
726 slash = strrchr(dir, '/');
727 *slash = '\0';
728 mkdir_recursive(dir, 0755);
729}
730
Colin Crosseb5ba832011-03-30 17:37:17 -0700731static void handle_generic_device_event(struct uevent *uevent)
732{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800733 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700734 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800735 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700736 char **links = NULL;
737
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800738 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700739 if (!name)
740 return;
741
Greg Hackmann3312aa82013-11-18 15:24:40 -0800742 struct ueventd_subsystem *subsystem =
743 ueventd_subsystem_find_by_name(uevent->subsystem);
744
745 if (subsystem) {
746 const char *devname;
747
748 switch (subsystem->devname_src) {
749 case DEVNAME_UEVENT_DEVNAME:
750 devname = uevent->device_name;
751 break;
752
753 case DEVNAME_UEVENT_DEVPATH:
754 devname = name;
755 break;
756
757 default:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700758 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800759 return;
760 }
761
762 if (!assemble_devpath(devpath, subsystem->dirname, devname))
763 return;
764 mkdir_recursive_for_devpath(devpath);
765 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700766 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700767 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800768 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800769 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800770 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700771 }
772 else {
773 /* This imitates the file system that would be created
774 * if we were using devfs instead.
775 * Minors are broken up into groups of 128, starting at "001"
776 */
777 int bus_id = uevent->minor / 128 + 1;
778 int device_id = uevent->minor % 128 + 1;
779 /* build directories */
780 make_dir("/dev/bus", 0755);
781 make_dir("/dev/bus/usb", 0755);
782 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
783 make_dir(devpath, 0755);
784 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
785 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700786 } else {
787 /* ignore other USB events */
788 return;
789 }
790 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
791 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500792 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200793 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
794 base = "/dev/dri/";
795 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700796 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
797 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500798 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700799 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
800 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500801 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700802 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
803 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500804 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700805 } else if(!strncmp(uevent->subsystem, "input", 5)) {
806 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500807 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700808 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
809 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500810 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700811 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
812 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500813 make_dir(base, 0755);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700814 } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
815 LOG(INFO) << "kernel logger is deprecated";
Colin Crosseb5ba832011-03-30 17:37:17 -0700816 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500817 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700818 name += 4;
819 } else
820 base = "/dev/";
821 links = get_character_device_symlinks(uevent);
822
823 if (!devpath[0])
824 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
825
826 handle_device(uevent->action, devpath, uevent->path, 0,
827 uevent->major, uevent->minor, links);
828}
829
830static void handle_device_event(struct uevent *uevent)
831{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700832 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500833 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700834
835 if (!strncmp(uevent->subsystem, "block", 5)) {
836 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700837 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
838 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700839 } else {
840 handle_generic_device_event(uevent);
841 }
842}
843
Elliott Hughes632e99a2016-11-12 11:44:16 -0800844static void load_firmware(uevent* uevent, const std::string& root,
845 int fw_fd, size_t fw_size,
846 int loading_fd, int data_fd) {
847 // Start transfer.
848 android::base::WriteFully(loading_fd, "1", 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700849
Elliott Hughes632e99a2016-11-12 11:44:16 -0800850 // Copy the firmware.
851 int rc = sendfile(data_fd, fw_fd, nullptr, fw_size);
852 if (rc == -1) {
853 PLOG(ERROR) << "firmware: sendfile failed { '" << root << "', '" << uevent->firmware << "' }";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700854 }
855
Elliott Hughes632e99a2016-11-12 11:44:16 -0800856 // Tell the firmware whether to abort or commit.
857 const char* response = (rc != -1) ? "0" : "-1";
858 android::base::WriteFully(loading_fd, response, strlen(response));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700859}
860
Elliott Hughes632e99a2016-11-12 11:44:16 -0800861static int is_booting() {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700862 return access("/dev/.booting", F_OK) == 0;
863}
864
Elliott Hughes632e99a2016-11-12 11:44:16 -0800865static void process_firmware_event(uevent* uevent) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700866 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700867
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700868 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700869
Elliott Hughes632e99a2016-11-12 11:44:16 -0800870 std::string root = android::base::StringPrintf("/sys%s", uevent->path);
871 std::string loading = root + "/loading";
872 std::string data = root + "/data";
873
874 android::base::unique_fd loading_fd(open(loading.c_str(), O_WRONLY|O_CLOEXEC));
875 if (loading_fd == -1) {
876 PLOG(ERROR) << "couldn't open firmware loading fd for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700877 return;
Elliott Hughes632e99a2016-11-12 11:44:16 -0800878 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700879
Elliott Hughes632e99a2016-11-12 11:44:16 -0800880 android::base::unique_fd data_fd(open(data.c_str(), O_WRONLY|O_CLOEXEC));
881 if (data_fd == -1) {
882 PLOG(ERROR) << "couldn't open firmware data fd for " << uevent->firmware;
883 return;
884 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700885
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700886try_loading_again:
Elliott Hughes632e99a2016-11-12 11:44:16 -0800887 for (size_t i = 0; i < arraysize(firmware_dirs); i++) {
888 std::string file = android::base::StringPrintf("%s/%s", firmware_dirs[i], uevent->firmware);
889 android::base::unique_fd fw_fd(open(file.c_str(), O_RDONLY|O_CLOEXEC));
890 struct stat sb;
891 if (fw_fd != -1 && fstat(fw_fd, &sb) != -1) {
892 load_firmware(uevent, root, fw_fd, sb.st_size, loading_fd, data_fd);
893 return;
Benoit Goby609d8822010-11-09 18:10:24 -0800894 }
Brian Swetland02863b92010-09-19 03:36:39 -0700895 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700896
Elliott Hughes632e99a2016-11-12 11:44:16 -0800897 if (booting) {
898 // If we're not fully booted, we may be missing
899 // filesystems needed for firmware, wait and retry.
Elliott Hughes290a2282016-11-14 17:08:47 -0800900 std::this_thread::sleep_for(100ms);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800901 booting = is_booting();
902 goto try_loading_again;
903 }
904
905 LOG(ERROR) << "firmware: could not find firmware for " << uevent->firmware;
906
907 // Write "-1" as our response to the kernel's firmware request, since we have nothing for it.
908 write(loading_fd, "-1", 2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700909}
910
Elliott Hughes632e99a2016-11-12 11:44:16 -0800911static void handle_firmware_event(uevent* uevent) {
912 if (strcmp(uevent->subsystem, "firmware")) return;
913 if (strcmp(uevent->action, "add")) return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700914
Elliott Hughes632e99a2016-11-12 11:44:16 -0800915 // Loading the firmware in a child means we can do that in parallel...
916 // (We ignore SIGCHLD rather than wait for our children.)
917 pid_t pid = fork();
918 if (pid == 0) {
919 Timer t;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700920 process_firmware_event(uevent);
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000921 LOG(INFO) << "loading " << uevent->path << " took " << t;
Kenny Root17baff42014-08-20 16:16:44 -0700922 _exit(EXIT_SUCCESS);
Elliott Hughes632e99a2016-11-12 11:44:16 -0800923 } else if (pid == -1) {
924 PLOG(ERROR) << "could not fork to process firmware event for " << uevent->firmware;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700925 }
926}
927
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800928static bool inline should_stop_coldboot(coldboot_action_t act)
929{
930 return (act == COLDBOOT_STOP || act == COLDBOOT_FINISH);
931}
932
Ruchi Kandoic6037202014-06-23 11:22:09 -0700933#define UEVENT_MSG_LEN 2048
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800934
Sandeep Patil44a3ee22017-02-08 15:49:47 -0800935static inline coldboot_action_t handle_device_fd_with(
936 std::function<coldboot_action_t(uevent* uevent)> handle_uevent)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700937{
Vernon Tang3f582e92011-04-25 13:08:17 +1000938 char msg[UEVENT_MSG_LEN+2];
939 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700940 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700941 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700942 continue;
943
944 msg[n] = '\0';
945 msg[n+1] = '\0';
946
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700947 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700948 parse_event(msg, &uevent);
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800949 coldboot_action_t act = handle_uevent(&uevent);
950 if (should_stop_coldboot(act))
951 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700952 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800953
954 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700955}
956
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800957coldboot_action_t handle_device_fd(coldboot_callback fn)
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800958{
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800959 coldboot_action_t ret = handle_device_fd_with(
960 [&](uevent* uevent) -> coldboot_action_t {
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800961 if (selinux_status_updated() > 0) {
962 struct selabel_handle *sehandle2;
963 sehandle2 = selinux_android_file_context_handle();
964 if (sehandle2) {
965 selabel_close(sehandle);
966 sehandle = sehandle2;
967 }
968 }
969
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800970 // default is to always create the devices
971 coldboot_action_t act = COLDBOOT_CREATE;
972 if (fn) {
973 act = fn(uevent);
974 }
975
976 if (act == COLDBOOT_CREATE || act == COLDBOOT_STOP) {
977 handle_device_event(uevent);
978 handle_firmware_event(uevent);
979 }
980
981 return act;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800982 });
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800983
984 return ret;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800985}
986
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700987/* Coldboot walks parts of the /sys tree and pokes the uevent files
988** to cause the kernel to regenerate device add events that happened
989** before init's device manager was started
990**
991** We drain any pending events from the netlink socket every time
992** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800993** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700994*/
995
Sandeep Patil957e4ab2017-02-07 18:11:37 -0800996static coldboot_action_t do_coldboot(DIR *d, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700997{
998 struct dirent *de;
999 int dfd, fd;
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001000 coldboot_action_t act = COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001001
1002 dfd = dirfd(d);
1003
1004 fd = openat(dfd, "uevent", O_WRONLY);
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001005 if (fd >= 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001006 write(fd, "add\n", 4);
1007 close(fd);
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001008 act = handle_device_fd(fn);
1009 if (should_stop_coldboot(act))
1010 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001011 }
1012
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001013 while (!should_stop_coldboot(act) && (de = readdir(d))) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001014 DIR *d2;
1015
1016 if(de->d_type != DT_DIR || de->d_name[0] == '.')
1017 continue;
1018
1019 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
1020 if(fd < 0)
1021 continue;
1022
1023 d2 = fdopendir(fd);
1024 if(d2 == 0)
1025 close(fd);
1026 else {
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001027 act = do_coldboot(d2, fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001028 closedir(d2);
1029 }
1030 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001031
1032 // default is always to continue looking for uevents
1033 return act;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001034}
1035
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001036static coldboot_action_t coldboot(const char *path, coldboot_callback fn)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001037{
James Hawkins588a2ca2016-02-18 14:52:46 -08001038 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001039 if (d) {
1040 return do_coldboot(d.get(), fn);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001041 }
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001042
1043 return COLDBOOT_CONTINUE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001044}
1045
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001046void device_init(const char* path, coldboot_callback fn) {
Sandeep Patil971a4602017-02-15 13:37:52 -08001047 if (!sehandle) {
1048 sehandle = selinux_android_file_context_handle();
Elliott Hughes56a06562015-03-28 11:23:32 -07001049 }
Sandeep Patil971a4602017-02-15 13:37:52 -08001050 // open uevent socket and selinux status only if it hasn't been
1051 // done before
1052 if (device_fd == -1) {
1053 /* is 256K enough? udev uses 16MB! */
1054 device_fd.reset(uevent_open_socket(256 * 1024, true));
1055 if (device_fd == -1) {
1056 return;
1057 }
1058 fcntl(device_fd, F_SETFL, O_NONBLOCK);
1059 selinux_status_open(true);
1060 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001061
Elliott Hughes56a06562015-03-28 11:23:32 -07001062 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001063 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -07001064 return;
Colin Crossf83d0b92010-04-21 12:04:20 -07001065 }
Elliott Hughes56a06562015-03-28 11:23:32 -07001066
1067 Timer t;
Sandeep Patil957e4ab2017-02-07 18:11:37 -08001068 coldboot_action_t act;
1069 if (!path) {
1070 act = coldboot("/sys/class", fn);
1071 if (!should_stop_coldboot(act)) {
1072 act = coldboot("/sys/block", fn);
1073 if (!should_stop_coldboot(act)) {
1074 act = coldboot("/sys/devices", fn);
1075 }
1076 }
1077 } else {
1078 act = coldboot(path, fn);
1079 }
1080
1081 // If we have a callback, then do as it says. If no, then the default is
1082 // to always create COLDBOOT_DONE file.
1083 if (!fn || (act == COLDBOOT_FINISH)) {
1084 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
1085 }
1086
Elliott Hughes331cf2f2016-11-29 19:20:58 +00001087 LOG(INFO) << "Coldboot took " << t;
Colin Cross0dd7ca62010-04-13 19:25:51 -07001088}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001089
Sandeep Patil35403eb2017-02-08 20:27:12 -08001090void device_close() {
1091 destroy_platform_devices();
1092 device_fd.reset();
Sandeep Patil971a4602017-02-15 13:37:52 -08001093 selinux_status_close();
Sandeep Patil35403eb2017-02-08 20:27:12 -08001094}
1095
Elliott Hughes632e99a2016-11-12 11:44:16 -08001096int get_device_fd() {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001097 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001098}