blob: ff449c9c53cffa747dc7f7f66dba675d217abdca [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>
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>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070026#include <sys/socket.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070027#include <sys/stat.h>
28#include <sys/time.h>
29#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070030#include <sys/un.h>
Elliott Hughesf39f7f12016-08-31 14:41:51 -070031#include <sys/wait.h>
32#include <unistd.h>
33
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070034#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050035
James Hawkins588a2ca2016-02-18 14:52:46 -080036#include <memory>
37
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038#include <selinux/selinux.h>
39#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040040#include <selinux/android.h>
Stephen Smalleye2eb69d2013-04-16 09:30:30 -040041#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050042
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043#include <private/android_filesystem_config.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070044
Biao Ludc848562016-01-28 16:10:54 +080045#include <android-base/file.h>
Rob Herring6de783a2016-05-06 10:06:59 -050046#include <android-base/stringprintf.h>
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +080047#include <android-base/unique_fd.h>
Dima Zavinda04c522011-09-01 17:09:44 -070048#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100049#include <cutils/uevent.h>
50
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070051#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080052#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070053#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070054#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056#define SYSFS_PREFIX "/sys"
Daniel Rosenbergd1d96022015-03-20 15:10:29 -070057static const char *firmware_dirs[] = { "/etc/firmware",
58 "/vendor/firmware",
59 "/firmware/image" };
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060
Stephen Smalleye096e362012-06-11 13:37:39 -040061extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050062
Colin Cross0dd7ca62010-04-13 19:25:51 -070063static int device_fd = -1;
64
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065struct uevent {
66 const char *action;
67 const char *path;
68 const char *subsystem;
69 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070070 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070071 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070072 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073 int major;
74 int minor;
75};
76
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077struct perms_ {
78 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070079 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080 mode_t perm;
81 unsigned int uid;
82 unsigned int gid;
83 unsigned short prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -070084 unsigned short wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070087struct perm_node {
88 struct perms_ dp;
89 struct listnode plist;
90};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070091
Colin Crossfadb85e2011-03-30 18:32:12 -070092struct platform_node {
93 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080094 char *path;
95 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070096 struct listnode list;
97};
98
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070099static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -0700100static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -0700101static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700103int add_dev_perms(const char *name, const char *attr,
104 mode_t perm, unsigned int uid, unsigned int gid,
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700105 unsigned short prefix,
106 unsigned short wildcard) {
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800107 struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700108 if (!node)
109 return -ENOMEM;
110
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700111 node->dp.name = strdup(name);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800112 if (!node->dp.name) {
113 free(node);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700114 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800115 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700116
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700117 if (attr) {
118 node->dp.attr = strdup(attr);
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800119 if (!node->dp.attr) {
120 free(node->dp.name);
121 free(node);
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700122 return -ENOMEM;
Ting-Yuan Huang09bd41d2016-11-15 15:35:16 -0800123 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700124 }
125
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700126 node->dp.perm = perm;
127 node->dp.uid = uid;
128 node->dp.gid = gid;
129 node->dp.prefix = prefix;
Daniel Leungc0c1ffe2012-07-02 11:32:30 -0700130 node->dp.wildcard = wildcard;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700132 if (attr)
133 list_add_tail(&sys_perms, &node->plist);
134 else
135 list_add_tail(&dev_perms, &node->plist);
136
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137 return 0;
138}
139
Colin Cross43d537e2014-07-02 13:08:13 -0700140static bool perm_path_matches(const char *path, struct perms_ *dp)
141{
142 if (dp->prefix) {
143 if (strncmp(path, dp->name, strlen(dp->name)) == 0)
144 return true;
145 } else if (dp->wildcard) {
146 if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
147 return true;
148 } else {
149 if (strcmp(path, dp->name) == 0)
150 return true;
151 }
152
153 return false;
154}
155
Rob Herring6de783a2016-05-06 10:06:59 -0500156static bool match_subsystem(perms_* dp, const char* pattern,
157 const char* path, const char* subsystem) {
158 if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
159 return false;
160 }
Rob Herringe5636a32016-05-06 12:28:48 -0500161
Rob Herring6de783a2016-05-06 10:06:59 -0500162 std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
163 return perm_path_matches(subsys_path.c_str(), dp);
164}
Rob Herringe5636a32016-05-06 12:28:48 -0500165
Rob Herring6de783a2016-05-06 10:06:59 -0500166static void fixup_sys_perms(const char* upath, const char* subsystem) {
167 // upaths omit the "/sys" that paths in this list
168 // contain, so we prepend it...
169 std::string path = std::string(SYSFS_PREFIX) + upath;
170
171 listnode* node;
Rob Herringe5636a32016-05-06 12:28:48 -0500172 list_for_each(node, &sys_perms) {
Rob Herring6de783a2016-05-06 10:06:59 -0500173 perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
174 if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
175 ; // matched
176 } else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
177 ; // matched
178 } else if (!perm_path_matches(path.c_str(), dp)) {
179 continue;
Rob Herringe5636a32016-05-06 12:28:48 -0500180 }
181
182 std::string attr_file = path + "/" + dp->attr;
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700183 LOG(INFO) << "fixup " << attr_file
184 << " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
Rob Herringe5636a32016-05-06 12:28:48 -0500185 chown(attr_file.c_str(), dp->uid, dp->gid);
186 chmod(attr_file.c_str(), dp->perm);
187 }
188
189 if (access(path.c_str(), F_OK) == 0) {
Dmitry Shmidt7eed4742016-07-28 13:55:39 -0700190 LOG(VERBOSE) << "restorecon_recursive: " << path;
Rob Herringe5636a32016-05-06 12:28:48 -0500191 restorecon_recursive(path.c_str());
192 }
193}
194
Colin Cross43d537e2014-07-02 13:08:13 -0700195static mode_t get_device_perm(const char *path, const char **links,
196 unsigned *uid, unsigned *gid)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197{
Colin Cross44b65d02010-04-20 14:32:50 -0700198 struct listnode *node;
199 struct perm_node *perm_node;
200 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700201
Colin Cross44b65d02010-04-20 14:32:50 -0700202 /* search the perms list in reverse so that ueventd.$hardware can
203 * override ueventd.rc
204 */
205 list_for_each_reverse(node, &dev_perms) {
Colin Cross43d537e2014-07-02 13:08:13 -0700206 bool match = false;
207
Colin Cross44b65d02010-04-20 14:32:50 -0700208 perm_node = node_to_item(node, struct perm_node, plist);
209 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700210
Colin Cross43d537e2014-07-02 13:08:13 -0700211 if (perm_path_matches(path, dp)) {
212 match = true;
Colin Cross44b65d02010-04-20 14:32:50 -0700213 } else {
Colin Cross43d537e2014-07-02 13:08:13 -0700214 if (links) {
215 int i;
216 for (i = 0; links[i]; i++) {
217 if (perm_path_matches(links[i], dp)) {
218 match = true;
219 break;
220 }
221 }
222 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700223 }
Colin Cross43d537e2014-07-02 13:08:13 -0700224
225 if (match) {
226 *uid = dp->uid;
227 *gid = dp->gid;
228 return dp->perm;
229 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230 }
Colin Cross44b65d02010-04-20 14:32:50 -0700231 /* Default if nothing found. */
232 *uid = 0;
233 *gid = 0;
234 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235}
236
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700237static void make_device(const char *path,
Elliott Hughesf682b472015-02-06 12:19:48 -0800238 const char */*upath*/,
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400239 int block, int major, int minor,
240 const char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241{
242 unsigned uid;
243 unsigned gid;
244 mode_t mode;
245 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500246 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700247
Colin Cross43d537e2014-07-02 13:08:13 -0700248 mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700249
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300250 if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700251 PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300252 return;
253 }
Nick Kralevich4d870952015-06-12 22:03:50 -0700254 setfscreatecon(secontext);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700255
Colin Cross17dcc5c2010-09-03 12:25:34 -0700256 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800257 /* Temporarily change egid to avoid race condition setting the gid of the
258 * device node. Unforunately changing the euid would prevent creation of
259 * some device nodes, so the uid has to be set with chown() and is still
260 * racy. Fixing the gid race at least fixed the issue with system_server
261 * opening dynamic input devices under the AID_INPUT gid. */
262 setegid(gid);
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300263 /* If the node already exists update its SELinux label to handle cases when
264 * it was created with the wrong context during coldboot procedure. */
265 if (mknod(path, mode, dev) && (errno == EEXIST)) {
William Roberts397de142016-06-02 09:53:44 -0700266
267 char* fcon = nullptr;
268 int rc = lgetfilecon(path, &fcon);
269 if (rc < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700270 PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
William Roberts397de142016-06-02 09:53:44 -0700271 goto out;
272 }
273
274 bool different = strcmp(fcon, secontext) != 0;
275 freecon(fcon);
276
277 if (different && lsetfilecon(path, secontext)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700278 PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300279 }
280 }
William Roberts397de142016-06-02 09:53:44 -0700281
282out:
Nick Pelly6405c692010-01-21 18:13:39 -0800283 chown(path, uid, -1);
284 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700285
Mihai Serban24a3cbf2016-04-25 18:22:27 +0300286 freecon(secontext);
287 setfscreatecon(NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288}
289
Dima Zavinf395c922013-03-06 16:23:57 -0800290static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700291{
Dima Zavinf395c922013-03-06 16:23:57 -0800292 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700293 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800294 const char *name = path;
295
296 if (!strncmp(path, "/devices/", 9)) {
297 name += 9;
298 if (!strncmp(name, "platform/", 9))
299 name += 9;
300 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700301
Wei Wanga285dac2016-10-04 14:05:39 -0700302 LOG(VERBOSE) << "adding platform device " << name << " (" << path << ")";
Colin Crossfadb85e2011-03-30 18:32:12 -0700303
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800304 bus = (platform_node*) calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800305 bus->path = strdup(path);
306 bus->path_len = path_len;
307 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700308 list_add_tail(&platform_names, &bus->list);
309}
310
311/*
Dima Zavinf395c922013-03-06 16:23:57 -0800312 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700313 * platform device prefix. If it doesn't start with a platform device, return
314 * 0.
315 */
Dima Zavinf395c922013-03-06 16:23:57 -0800316static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700317{
Dima Zavinf395c922013-03-06 16:23:57 -0800318 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700319 struct listnode *node;
320 struct platform_node *bus;
321
322 list_for_each_reverse(node, &platform_names) {
323 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800324 if ((bus->path_len < path_len) &&
325 (path[bus->path_len] == '/') &&
326 !strncmp(path, bus->path, bus->path_len))
327 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700328 }
329
330 return NULL;
331}
332
Dima Zavinf395c922013-03-06 16:23:57 -0800333static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700334{
335 struct listnode *node;
336 struct platform_node *bus;
337
338 list_for_each_reverse(node, &platform_names) {
339 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800340 if (!strcmp(path, bus->path)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700341 LOG(INFO) << "removing platform device " << bus->name;
Dima Zavinf395c922013-03-06 16:23:57 -0800342 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700343 list_remove(node);
344 free(bus);
345 return;
346 }
347 }
348}
349
Andrew Boiea885d042013-09-13 17:41:20 -0700350/* Given a path that may start with a PCI device, populate the supplied buffer
351 * with the PCI domain/bus number and the peripheral ID and return 0.
352 * If it doesn't start with a PCI device, or there is some error, return -1 */
353static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
354{
355 const char *start, *end;
356
357 if (strncmp(path, "/devices/pci", 12))
358 return -1;
359
360 /* Beginning of the prefix is the initial "pci" after "/devices/" */
361 start = path + 9;
362
363 /* End of the prefix is two path '/' later, capturing the domain/bus number
364 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
365 end = strchr(start, '/');
366 if (!end)
367 return -1;
368 end = strchr(end + 1, '/');
369 if (!end)
370 return -1;
371
372 /* Make sure we have enough room for the string plus null terminator */
373 if (end - start + 1 > buf_sz)
374 return -1;
375
376 strncpy(buf, start, end - start);
377 buf[end - start] = '\0';
378 return 0;
379}
380
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700381static void parse_event(const char *msg, struct uevent *uevent)
382{
383 uevent->action = "";
384 uevent->path = "";
385 uevent->subsystem = "";
386 uevent->firmware = "";
387 uevent->major = -1;
388 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700389 uevent->partition_name = NULL;
390 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700391 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700392
393 /* currently ignoring SEQNUM */
394 while(*msg) {
395 if(!strncmp(msg, "ACTION=", 7)) {
396 msg += 7;
397 uevent->action = msg;
398 } else if(!strncmp(msg, "DEVPATH=", 8)) {
399 msg += 8;
400 uevent->path = msg;
401 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
402 msg += 10;
403 uevent->subsystem = msg;
404 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
405 msg += 9;
406 uevent->firmware = msg;
407 } else if(!strncmp(msg, "MAJOR=", 6)) {
408 msg += 6;
409 uevent->major = atoi(msg);
410 } else if(!strncmp(msg, "MINOR=", 6)) {
411 msg += 6;
412 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700413 } else if(!strncmp(msg, "PARTN=", 6)) {
414 msg += 6;
415 uevent->partition_num = atoi(msg);
416 } else if(!strncmp(msg, "PARTNAME=", 9)) {
417 msg += 9;
418 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700419 } else if(!strncmp(msg, "DEVNAME=", 8)) {
420 msg += 8;
421 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700422 }
423
Wei Zhongf97b8872012-03-23 14:15:34 -0700424 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700425 while(*msg++)
426 ;
427 }
428
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800429 if (LOG_UEVENTS) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700430 LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
431 uevent->action, uevent->path, uevent->subsystem,
432 uevent->firmware, uevent->major, uevent->minor);
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800433 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700434}
435
Benoit Gobyd2278632010-08-03 14:36:02 -0700436static char **get_character_device_symlinks(struct uevent *uevent)
437{
438 const char *parent;
Dan Austin60b976d2016-03-28 14:22:12 -0700439 const char *slash;
Benoit Gobyd2278632010-08-03 14:36:02 -0700440 char **links;
441 int link_num = 0;
442 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800443 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700444
Dima Zavinf395c922013-03-06 16:23:57 -0800445 pdev = find_platform_device(uevent->path);
446 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700447 return NULL;
448
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800449 links = (char**) malloc(sizeof(char *) * 2);
Benoit Gobyd2278632010-08-03 14:36:02 -0700450 if (!links)
451 return NULL;
452 memset(links, 0, sizeof(char *) * 2);
453
454 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800455 parent = strchr(uevent->path + pdev->path_len, '/');
Tomasz Kondelfca58f42013-11-05 13:17:45 +0100456 if (!parent)
Benoit Gobyd2278632010-08-03 14:36:02 -0700457 goto err;
458
459 if (!strncmp(parent, "/usb", 4)) {
460 /* skip root hub name and device. use device interface */
461 while (*++parent && *parent != '/');
462 if (*parent)
463 while (*++parent && *parent != '/');
464 if (!*parent)
465 goto err;
466 slash = strchr(++parent, '/');
467 if (!slash)
468 goto err;
469 width = slash - parent;
470 if (width <= 0)
471 goto err;
472
473 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
474 link_num++;
475 else
476 links[link_num] = NULL;
477 mkdir("/dev/usb", 0755);
478 }
479 else {
480 goto err;
481 }
482
483 return links;
484err:
485 free(links);
486 return NULL;
487}
488
Andrew Boiea885d042013-09-13 17:41:20 -0700489static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700490{
Colin Crossfadb85e2011-03-30 18:32:12 -0700491 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800492 struct platform_node *pdev;
Dan Austin60b976d2016-03-28 14:22:12 -0700493 const char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700494 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700495 char buf[256];
496 char link_path[256];
Colin Crossb0ab94b2010-04-08 16:16:20 -0700497 int link_num = 0;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700498 char *p;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700499
Dima Zavinf395c922013-03-06 16:23:57 -0800500 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700501 if (pdev) {
502 device = pdev->name;
503 type = "platform";
504 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
505 device = buf;
506 type = "pci";
507 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800508 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700509 }
Dima Zavinf395c922013-03-06 16:23:57 -0800510
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800511 char **links = (char**) malloc(sizeof(char *) * 4);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700512 if (!links)
513 return NULL;
514 memset(links, 0, sizeof(char *) * 4);
515
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700516 LOG(INFO) << "found " << type << " device " << device;
Colin Crossfadb85e2011-03-30 18:32:12 -0700517
Andrew Boiea885d042013-09-13 17:41:20 -0700518 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700519
520 if (uevent->partition_name) {
521 p = strdup(uevent->partition_name);
522 sanitize(p);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700523 if (strcmp(uevent->partition_name, p)) {
524 LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
525 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700526 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
527 link_num++;
528 else
529 links[link_num] = NULL;
530 free(p);
531 }
532
533 if (uevent->partition_num >= 0) {
534 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
535 link_num++;
536 else
537 links[link_num] = NULL;
538 }
539
Dima Zavinf395c922013-03-06 16:23:57 -0800540 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700541 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
542 link_num++;
543 else
544 links[link_num] = NULL;
545
546 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700547}
548
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700549static void make_link_init(const char* oldpath, const char* newpath) {
550 const char* slash = strrchr(newpath, '/');
551 if (!slash) return;
552
553 if (mkdir_recursive(dirname(newpath), 0755)) {
554 PLOG(ERROR) << "Failed to create directory " << dirname(newpath);
555 }
556
557 if (symlink(oldpath, newpath) && errno != EEXIST) {
558 PLOG(ERROR) << "Failed to symlink " << oldpath << " to " << newpath;
559 }
560}
561
562static void remove_link(const char* oldpath, const char* newpath) {
563 std::string path;
564 if (android::base::Readlink(newpath, &path) && path == oldpath) unlink(newpath);
565}
566
Colin Crosseb5ba832011-03-30 17:37:17 -0700567static void handle_device(const char *action, const char *devpath,
568 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700569{
Colin Crosseb5ba832011-03-30 17:37:17 -0700570 if(!strcmp(action, "add")) {
Stephen Smalleyb4c52002014-06-12 12:29:14 -0400571 make_device(devpath, path, block, major, minor, (const char **)links);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700572 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700573 for (int i = 0; links[i]; i++) {
Chris Fries79f33842013-09-05 13:19:21 -0500574 make_link_init(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700575 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700576 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700577 }
578
Colin Crosseb5ba832011-03-30 17:37:17 -0700579 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700580 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700581 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700582 remove_link(devpath, links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700583 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700584 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700585 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700586 }
587
588 if (links) {
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700589 for (int i = 0; links[i]; i++) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700590 free(links[i]);
Elliott Hughesf39f7f12016-08-31 14:41:51 -0700591 }
Colin Crossb0ab94b2010-04-08 16:16:20 -0700592 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700593 }
594}
595
Colin Crossfadb85e2011-03-30 18:32:12 -0700596static void handle_platform_device_event(struct uevent *uevent)
597{
Dima Zavinf395c922013-03-06 16:23:57 -0800598 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700599
600 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800601 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700602 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800603 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700604}
605
Colin Crosseb5ba832011-03-30 17:37:17 -0700606static const char *parse_device_name(struct uevent *uevent, unsigned int len)
607{
608 const char *name;
609
610 /* if it's not a /dev device, nothing else to do */
611 if((uevent->major < 0) || (uevent->minor < 0))
612 return NULL;
613
614 /* do we have a name? */
615 name = strrchr(uevent->path, '/');
616 if(!name)
617 return NULL;
618 name++;
619
620 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800621 if(strlen(name) > len) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700622 LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
Colin Crosseb5ba832011-03-30 17:37:17 -0700623 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800624 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700625
626 return name;
627}
628
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800629#define DEVPATH_LEN 96
630#define MAX_DEV_NAME 64
631
Colin Crosseb5ba832011-03-30 17:37:17 -0700632static void handle_block_device_event(struct uevent *uevent)
633{
634 const char *base = "/dev/block/";
635 const char *name;
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800636 char devpath[DEVPATH_LEN];
Colin Crosseb5ba832011-03-30 17:37:17 -0700637 char **links = NULL;
638
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800639 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700640 if (!name)
641 return;
642
643 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500644 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700645
Dima Zavinf395c922013-03-06 16:23:57 -0800646 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700647 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700648
649 handle_device(uevent->action, devpath, uevent->path, 1,
650 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700651}
652
Greg Hackmann3312aa82013-11-18 15:24:40 -0800653static bool assemble_devpath(char *devpath, const char *dirname,
654 const char *devname)
655{
656 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
657 if (s < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700658 PLOG(ERROR) << "failed to assemble device path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800659 return false;
660 } else if (s >= DEVPATH_LEN) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700661 LOG(ERROR) << dirname << "/" << devname
662 << " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800663 return false;
664 }
665 return true;
666}
667
668static void mkdir_recursive_for_devpath(const char *devpath)
669{
670 char dir[DEVPATH_LEN];
671 char *slash;
672
673 strcpy(dir, devpath);
674 slash = strrchr(dir, '/');
675 *slash = '\0';
676 mkdir_recursive(dir, 0755);
677}
678
Colin Crosseb5ba832011-03-30 17:37:17 -0700679static void handle_generic_device_event(struct uevent *uevent)
680{
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800681 const char *base;
Colin Crosseb5ba832011-03-30 17:37:17 -0700682 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800683 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700684 char **links = NULL;
685
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800686 name = parse_device_name(uevent, MAX_DEV_NAME);
Colin Crosseb5ba832011-03-30 17:37:17 -0700687 if (!name)
688 return;
689
Greg Hackmann3312aa82013-11-18 15:24:40 -0800690 struct ueventd_subsystem *subsystem =
691 ueventd_subsystem_find_by_name(uevent->subsystem);
692
693 if (subsystem) {
694 const char *devname;
695
696 switch (subsystem->devname_src) {
697 case DEVNAME_UEVENT_DEVNAME:
698 devname = uevent->device_name;
699 break;
700
701 case DEVNAME_UEVENT_DEVPATH:
702 devname = name;
703 break;
704
705 default:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700706 LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
Greg Hackmann3312aa82013-11-18 15:24:40 -0800707 return;
708 }
709
710 if (!assemble_devpath(devpath, subsystem->dirname, devname))
711 return;
712 mkdir_recursive_for_devpath(devpath);
713 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700714 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700715 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800716 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800717 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800718 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700719 }
720 else {
721 /* This imitates the file system that would be created
722 * if we were using devfs instead.
723 * Minors are broken up into groups of 128, starting at "001"
724 */
725 int bus_id = uevent->minor / 128 + 1;
726 int device_id = uevent->minor % 128 + 1;
727 /* build directories */
728 make_dir("/dev/bus", 0755);
729 make_dir("/dev/bus/usb", 0755);
730 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
731 make_dir(devpath, 0755);
732 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
733 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700734 } else {
735 /* ignore other USB events */
736 return;
737 }
738 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
739 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500740 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200741 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
742 base = "/dev/dri/";
743 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700744 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
745 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500746 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700747 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
748 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500749 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700750 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
751 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500752 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700753 } else if(!strncmp(uevent->subsystem, "input", 5)) {
754 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500755 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700756 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
757 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500758 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700759 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
760 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500761 make_dir(base, 0755);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700762 } else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
763 LOG(INFO) << "kernel logger is deprecated";
Colin Crosseb5ba832011-03-30 17:37:17 -0700764 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500765 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700766 name += 4;
767 } else
768 base = "/dev/";
769 links = get_character_device_symlinks(uevent);
770
771 if (!devpath[0])
772 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
773
774 handle_device(uevent->action, devpath, uevent->path, 0,
775 uevent->major, uevent->minor, links);
776}
777
778static void handle_device_event(struct uevent *uevent)
779{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700780 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Rob Herring6de783a2016-05-06 10:06:59 -0500781 fixup_sys_perms(uevent->path, uevent->subsystem);
Colin Crosseb5ba832011-03-30 17:37:17 -0700782
783 if (!strncmp(uevent->subsystem, "block", 5)) {
784 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700785 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
786 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700787 } else {
788 handle_generic_device_event(uevent);
789 }
790}
791
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700792static int load_firmware(int fw_fd, int loading_fd, int data_fd)
793{
794 struct stat st;
795 long len_to_copy;
796 int ret = 0;
797
798 if(fstat(fw_fd, &st) < 0)
799 return -1;
800 len_to_copy = st.st_size;
801
802 write(loading_fd, "1", 1); /* start transfer */
803
804 while (len_to_copy > 0) {
805 char buf[PAGE_SIZE];
806 ssize_t nr;
807
808 nr = read(fw_fd, buf, sizeof(buf));
809 if(!nr)
810 break;
811 if(nr < 0) {
812 ret = -1;
813 break;
814 }
Biao Ludc848562016-01-28 16:10:54 +0800815 if (!android::base::WriteFully(data_fd, buf, nr)) {
816 ret = -1;
817 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700818 }
Biao Ludc848562016-01-28 16:10:54 +0800819 len_to_copy -= nr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700820 }
821
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700822 if(!ret)
823 write(loading_fd, "0", 1); /* successful end of transfer */
824 else
825 write(loading_fd, "-1", 2); /* abort transfer */
826
827 return ret;
828}
829
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700830static int is_booting(void)
831{
832 return access("/dev/.booting", F_OK) == 0;
833}
834
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700835static void process_firmware_event(struct uevent *uevent)
836{
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700837 char *root, *loading, *data;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700838 int l, loading_fd, data_fd, fw_fd;
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700839 size_t i;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700840 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700841
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700842 LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700843
844 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
845 if (l == -1)
846 return;
847
848 l = asprintf(&loading, "%sloading", root);
849 if (l == -1)
850 goto root_free_out;
851
852 l = asprintf(&data, "%sdata", root);
853 if (l == -1)
854 goto loading_free_out;
855
Nick Kralevich45a884f2015-02-02 14:37:22 -0800856 loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700857 if(loading_fd < 0)
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700858 goto data_free_out;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700859
Nick Kralevich45a884f2015-02-02 14:37:22 -0800860 data_fd = open(data, O_WRONLY|O_CLOEXEC);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700861 if(data_fd < 0)
862 goto loading_close_out;
863
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700864try_loading_again:
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700865 for (i = 0; i < arraysize(firmware_dirs); i++) {
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700866 char *file = NULL;
867 l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
868 if (l == -1)
869 goto data_free_out;
870 fw_fd = open(file, O_RDONLY|O_CLOEXEC);
871 free(file);
872 if (fw_fd >= 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700873 if (!load_firmware(fw_fd, loading_fd, data_fd)) {
874 LOG(INFO) << "firmware: copy success { '" << root << "', '" << uevent->firmware << "' }";
875 } else {
876 LOG(ERROR) << "firmware: copy failure { '" << root << "', '" << uevent->firmware << "' }";
877 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700878 break;
Benoit Goby609d8822010-11-09 18:10:24 -0800879 }
Brian Swetland02863b92010-09-19 03:36:39 -0700880 }
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700881 if (fw_fd < 0) {
882 if (booting) {
883 /* If we're not fully booted, we may be missing
884 * filesystems needed for firmware, wait and retry.
885 */
886 usleep(100000);
887 booting = is_booting();
888 goto try_loading_again;
889 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700890 PLOG(ERROR) << "firmware: could not open '" << uevent->firmware << "'";
Daniel Rosenbergd1d96022015-03-20 15:10:29 -0700891 write(loading_fd, "-1", 2);
892 goto data_close_out;
893 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700894
895 close(fw_fd);
896data_close_out:
897 close(data_fd);
898loading_close_out:
899 close(loading_fd);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700900data_free_out:
901 free(data);
902loading_free_out:
903 free(loading);
904root_free_out:
905 free(root);
906}
907
908static void handle_firmware_event(struct uevent *uevent)
909{
910 pid_t pid;
911
912 if(strcmp(uevent->subsystem, "firmware"))
913 return;
914
915 if(strcmp(uevent->action, "add"))
916 return;
917
918 /* we fork, to avoid making large memory allocations in init proper */
919 pid = fork();
920 if (!pid) {
921 process_firmware_event(uevent);
Kenny Root17baff42014-08-20 16:16:44 -0700922 _exit(EXIT_SUCCESS);
923 } else if (pid < 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700924 PLOG(ERROR) << "could not fork to process firmware event";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700925 }
926}
927
Ruchi Kandoic6037202014-06-23 11:22:09 -0700928#define UEVENT_MSG_LEN 2048
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800929
930static inline void handle_device_fd_with(void (handle_uevent)(struct uevent*))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700931{
Vernon Tang3f582e92011-04-25 13:08:17 +1000932 char msg[UEVENT_MSG_LEN+2];
933 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700934 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700935 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700936 continue;
937
938 msg[n] = '\0';
939 msg[n+1] = '\0';
940
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700941 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700942 parse_event(msg, &uevent);
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800943 handle_uevent(&uevent);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700944 }
945}
946
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +0800947void handle_device_fd()
948{
949 handle_device_fd_with(
950 [](struct uevent *uevent) {
951 if (selinux_status_updated() > 0) {
952 struct selabel_handle *sehandle2;
953 sehandle2 = selinux_android_file_context_handle();
954 if (sehandle2) {
955 selabel_close(sehandle);
956 sehandle = sehandle2;
957 }
958 }
959
960 handle_device_event(uevent);
961 handle_firmware_event(uevent);
962 });
963}
964
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700965/* Coldboot walks parts of the /sys tree and pokes the uevent files
966** to cause the kernel to regenerate device add events that happened
967** before init's device manager was started
968**
969** We drain any pending events from the netlink socket every time
970** we poke another uevent file to make sure we don't overrun the
Elliott Hughesc0e919c2015-02-04 14:46:36 -0800971** socket's buffer.
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700972*/
973
Colin Cross0dd7ca62010-04-13 19:25:51 -0700974static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700975{
976 struct dirent *de;
977 int dfd, fd;
978
979 dfd = dirfd(d);
980
981 fd = openat(dfd, "uevent", O_WRONLY);
982 if(fd >= 0) {
983 write(fd, "add\n", 4);
984 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700985 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700986 }
987
988 while((de = readdir(d))) {
989 DIR *d2;
990
991 if(de->d_type != DT_DIR || de->d_name[0] == '.')
992 continue;
993
994 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
995 if(fd < 0)
996 continue;
997
998 d2 = fdopendir(fd);
999 if(d2 == 0)
1000 close(fd);
1001 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001002 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001003 closedir(d2);
1004 }
1005 }
1006}
1007
Colin Cross0dd7ca62010-04-13 19:25:51 -07001008static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001009{
James Hawkins588a2ca2016-02-18 14:52:46 -08001010 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001011 if(d) {
James Hawkins588a2ca2016-02-18 14:52:46 -08001012 do_coldboot(d.get());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001013 }
1014}
1015
Hung-ying Tyan99c4a8a2016-02-01 15:07:40 +08001016static void early_uevent_handler(struct uevent *uevent, const char *base, bool is_block)
1017{
1018 const char *name;
1019 char devpath[DEVPATH_LEN];
1020
1021 if (is_block && strncmp(uevent->subsystem, "block", 5))
1022 return;
1023
1024 name = parse_device_name(uevent, MAX_DEV_NAME);
1025 if (!name) {
1026 LOG(ERROR) << "Failed to parse dev name from uevent: " << uevent->action
1027 << " " << uevent->partition_name << " " << uevent->partition_num
1028 << " " << uevent->major << ":" << uevent->minor;
1029 return;
1030 }
1031
1032 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
1033 make_dir(base, 0755);
1034
1035 dev_t dev = makedev(uevent->major, uevent->minor);
1036 mode_t mode = 0600 | (is_block ? S_IFBLK : S_IFCHR);
1037 mknod(devpath, mode, dev);
1038}
1039
1040void early_create_dev(const std::string& syspath, early_device_type dev_type)
1041{
1042 android::base::unique_fd dfd(open(syspath.c_str(), O_RDONLY));
1043 if (dfd < 0) {
1044 LOG(ERROR) << "Failed to open " << syspath;
1045 return;
1046 }
1047
1048 android::base::unique_fd fd(openat(dfd, "uevent", O_WRONLY));
1049 if (fd < 0) {
1050 LOG(ERROR) << "Failed to open " << syspath << "/uevent";
1051 return;
1052 }
1053
1054 fcntl(device_fd, F_SETFL, O_NONBLOCK);
1055
1056 write(fd, "add\n", 4);
1057 handle_device_fd_with(dev_type == EARLY_BLOCK_DEV ?
1058 [](struct uevent *uevent) {
1059 early_uevent_handler(uevent, "/dev/block/", true);
1060 } :
1061 [](struct uevent *uevent) {
1062 early_uevent_handler(uevent, "/dev/", false);
1063 });
1064}
1065
1066int early_device_socket_open() {
1067 device_fd = uevent_open_socket(256*1024, true);
1068 return device_fd < 0;
1069}
1070
1071void early_device_socket_close() {
1072 close(device_fd);
1073}
1074
Elliott Hughes74738362015-03-28 10:51:23 -07001075void device_init() {
Nick Kralevich4d870952015-06-12 22:03:50 -07001076 sehandle = selinux_android_file_context_handle();
1077 selinux_status_open(true);
Kenny Rootb5982bf2012-10-16 23:07:05 -07001078
Andrew Boied562ca72012-12-04 15:47:20 -08001079 /* is 256K enough? udev uses 16MB! */
1080 device_fd = uevent_open_socket(256*1024, true);
Elliott Hughes56a06562015-03-28 11:23:32 -07001081 if (device_fd == -1) {
Colin Cross0dd7ca62010-04-13 19:25:51 -07001082 return;
Elliott Hughes56a06562015-03-28 11:23:32 -07001083 }
Colin Cross0dd7ca62010-04-13 19:25:51 -07001084 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001085
Elliott Hughes56a06562015-03-28 11:23:32 -07001086 if (access(COLDBOOT_DONE, F_OK) == 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001087 LOG(VERBOSE) << "Skipping coldboot, already done!";
Elliott Hughes56a06562015-03-28 11:23:32 -07001088 return;
Colin Crossf83d0b92010-04-21 12:04:20 -07001089 }
Elliott Hughes56a06562015-03-28 11:23:32 -07001090
1091 Timer t;
1092 coldboot("/sys/class");
1093 coldboot("/sys/block");
1094 coldboot("/sys/devices");
1095 close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
Elliott Hughesf86b5a62016-06-24 15:12:21 -07001096 LOG(INFO) << "Coldboot took " << t.duration() << "s.";
Colin Cross0dd7ca62010-04-13 19:25:51 -07001097}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001098
Colin Cross0dd7ca62010-04-13 19:25:51 -07001099int get_device_fd()
1100{
1101 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001102}