2008-10-21 16:00:00 +02:00
|
|
|
/*
|
2014-02-20 23:59:07 +01:00
|
|
|
* Copyright (C) 2007-2014 The Android Open Source Project
|
2008-10-21 16:00:00 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2012-07-02 20:32:30 +02:00
|
|
|
#include <fnmatch.h>
|
2010-11-17 20:47:23 +01:00
|
|
|
#include <stddef.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <linux/netlink.h>
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2016-02-18 23:52:46 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2012-01-13 14:48:47 +01:00
|
|
|
#include <selinux/selinux.h>
|
|
|
|
#include <selinux/label.h>
|
2012-05-01 21:02:53 +02:00
|
|
|
#include <selinux/android.h>
|
2013-04-16 15:30:30 +02:00
|
|
|
#include <selinux/avc.h>
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
#include <sys/time.h>
|
2010-06-03 21:21:01 +02:00
|
|
|
#include <sys/wait.h>
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2016-01-28 09:10:54 +01:00
|
|
|
#include <android-base/file.h>
|
2016-05-06 17:06:59 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2011-09-02 02:09:44 +02:00
|
|
|
#include <cutils/list.h>
|
2011-04-25 05:08:17 +02:00
|
|
|
#include <cutils/uevent.h>
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
#include "devices.h"
|
2013-11-19 00:24:40 +01:00
|
|
|
#include "ueventd_parser.h"
|
2010-04-09 01:16:20 +02:00
|
|
|
#include "util.h"
|
2010-04-20 02:05:34 +02:00
|
|
|
#include "log.h"
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
#define SYSFS_PREFIX "/sys"
|
2015-03-20 23:10:29 +01:00
|
|
|
static const char *firmware_dirs[] = { "/etc/firmware",
|
|
|
|
"/vendor/firmware",
|
|
|
|
"/firmware/image" };
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2012-06-11 19:37:39 +02:00
|
|
|
extern struct selabel_handle *sehandle;
|
2012-01-13 14:48:47 +01:00
|
|
|
|
2010-04-14 04:25:51 +02:00
|
|
|
static int device_fd = -1;
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
struct uevent {
|
|
|
|
const char *action;
|
|
|
|
const char *path;
|
|
|
|
const char *subsystem;
|
|
|
|
const char *firmware;
|
2010-04-09 01:16:20 +02:00
|
|
|
const char *partition_name;
|
2012-03-23 22:15:34 +01:00
|
|
|
const char *device_name;
|
2010-04-09 01:16:20 +02:00
|
|
|
int partition_num;
|
2008-10-21 16:00:00 +02:00
|
|
|
int major;
|
|
|
|
int minor;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct perms_ {
|
|
|
|
char *name;
|
2010-10-27 00:09:43 +02:00
|
|
|
char *attr;
|
2008-10-21 16:00:00 +02:00
|
|
|
mode_t perm;
|
|
|
|
unsigned int uid;
|
|
|
|
unsigned int gid;
|
|
|
|
unsigned short prefix;
|
2012-07-02 20:32:30 +02:00
|
|
|
unsigned short wildcard;
|
2008-10-21 16:00:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct perm_node {
|
|
|
|
struct perms_ dp;
|
|
|
|
struct listnode plist;
|
|
|
|
};
|
2010-10-27 00:09:43 +02:00
|
|
|
|
2011-03-31 03:32:12 +02:00
|
|
|
struct platform_node {
|
|
|
|
char *name;
|
2013-03-07 01:23:57 +01:00
|
|
|
char *path;
|
|
|
|
int path_len;
|
2011-03-31 03:32:12 +02:00
|
|
|
struct listnode list;
|
|
|
|
};
|
|
|
|
|
2010-10-27 00:09:43 +02:00
|
|
|
static list_declare(sys_perms);
|
2010-04-20 23:32:50 +02:00
|
|
|
static list_declare(dev_perms);
|
2011-03-31 03:32:12 +02:00
|
|
|
static list_declare(platform_names);
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2010-10-27 00:09:43 +02:00
|
|
|
int add_dev_perms(const char *name, const char *attr,
|
|
|
|
mode_t perm, unsigned int uid, unsigned int gid,
|
2012-07-02 20:32:30 +02:00
|
|
|
unsigned short prefix,
|
|
|
|
unsigned short wildcard) {
|
2015-02-04 02:12:07 +01:00
|
|
|
struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
|
2008-10-21 16:00:00 +02:00
|
|
|
if (!node)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2010-10-27 00:09:43 +02:00
|
|
|
node->dp.name = strdup(name);
|
|
|
|
if (!node->dp.name)
|
2008-10-21 16:00:00 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2010-10-27 00:09:43 +02:00
|
|
|
if (attr) {
|
|
|
|
node->dp.attr = strdup(attr);
|
|
|
|
if (!node->dp.attr)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
node->dp.perm = perm;
|
|
|
|
node->dp.uid = uid;
|
|
|
|
node->dp.gid = gid;
|
|
|
|
node->dp.prefix = prefix;
|
2012-07-02 20:32:30 +02:00
|
|
|
node->dp.wildcard = wildcard;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2010-10-27 00:09:43 +02:00
|
|
|
if (attr)
|
|
|
|
list_add_tail(&sys_perms, &node->plist);
|
|
|
|
else
|
|
|
|
list_add_tail(&dev_perms, &node->plist);
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-02 22:08:13 +02:00
|
|
|
static bool perm_path_matches(const char *path, struct perms_ *dp)
|
|
|
|
{
|
|
|
|
if (dp->prefix) {
|
|
|
|
if (strncmp(path, dp->name, strlen(dp->name)) == 0)
|
|
|
|
return true;
|
|
|
|
} else if (dp->wildcard) {
|
|
|
|
if (fnmatch(dp->name, path, FNM_PATHNAME) == 0)
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (strcmp(path, dp->name) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-06 17:06:59 +02:00
|
|
|
static bool match_subsystem(perms_* dp, const char* pattern,
|
|
|
|
const char* path, const char* subsystem) {
|
|
|
|
if (!pattern || !subsystem || strstr(dp->name, subsystem) == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-06 19:28:48 +02:00
|
|
|
|
2016-05-06 17:06:59 +02:00
|
|
|
std::string subsys_path = android::base::StringPrintf(pattern, subsystem, basename(path));
|
|
|
|
return perm_path_matches(subsys_path.c_str(), dp);
|
|
|
|
}
|
2016-05-06 19:28:48 +02:00
|
|
|
|
2016-05-06 17:06:59 +02:00
|
|
|
static void fixup_sys_perms(const char* upath, const char* subsystem) {
|
|
|
|
// upaths omit the "/sys" that paths in this list
|
|
|
|
// contain, so we prepend it...
|
|
|
|
std::string path = std::string(SYSFS_PREFIX) + upath;
|
2016-05-06 19:28:48 +02:00
|
|
|
|
2016-05-06 17:06:59 +02:00
|
|
|
listnode* node;
|
|
|
|
list_for_each(node, &sys_perms) {
|
|
|
|
perms_* dp = &(node_to_item(node, perm_node, plist))->dp;
|
|
|
|
if (match_subsystem(dp, SYSFS_PREFIX "/class/%s/%s", path.c_str(), subsystem)) {
|
|
|
|
; // matched
|
|
|
|
} else if (match_subsystem(dp, SYSFS_PREFIX "/bus/%s/devices/%s", path.c_str(), subsystem)) {
|
|
|
|
; // matched
|
|
|
|
} else if (!perm_path_matches(path.c_str(), dp)) {
|
|
|
|
continue;
|
2016-05-06 19:28:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string attr_file = path + "/" + dp->attr;
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "fixup " << attr_file
|
|
|
|
<< " " << dp->uid << " " << dp->gid << " " << std::oct << dp->perm;
|
2016-05-06 19:28:48 +02:00
|
|
|
chown(attr_file.c_str(), dp->uid, dp->gid);
|
|
|
|
chmod(attr_file.c_str(), dp->perm);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (access(path.c_str(), F_OK) == 0) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "restorecon_recursive: " << path;
|
2016-05-06 19:28:48 +02:00
|
|
|
restorecon_recursive(path.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 22:08:13 +02:00
|
|
|
static mode_t get_device_perm(const char *path, const char **links,
|
|
|
|
unsigned *uid, unsigned *gid)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
2010-04-20 23:32:50 +02:00
|
|
|
struct listnode *node;
|
|
|
|
struct perm_node *perm_node;
|
|
|
|
struct perms_ *dp;
|
|
|
|
|
|
|
|
/* search the perms list in reverse so that ueventd.$hardware can
|
|
|
|
* override ueventd.rc
|
|
|
|
*/
|
|
|
|
list_for_each_reverse(node, &dev_perms) {
|
2014-07-02 22:08:13 +02:00
|
|
|
bool match = false;
|
|
|
|
|
2010-04-20 23:32:50 +02:00
|
|
|
perm_node = node_to_item(node, struct perm_node, plist);
|
|
|
|
dp = &perm_node->dp;
|
|
|
|
|
2014-07-02 22:08:13 +02:00
|
|
|
if (perm_path_matches(path, dp)) {
|
|
|
|
match = true;
|
2010-04-20 23:32:50 +02:00
|
|
|
} else {
|
2014-07-02 22:08:13 +02:00
|
|
|
if (links) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; links[i]; i++) {
|
|
|
|
if (perm_path_matches(links[i], dp)) {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
*uid = dp->uid;
|
|
|
|
*gid = dp->gid;
|
|
|
|
return dp->perm;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-20 23:32:50 +02:00
|
|
|
/* Default if nothing found. */
|
|
|
|
*uid = 0;
|
|
|
|
*gid = 0;
|
|
|
|
return 0600;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2010-10-27 00:09:43 +02:00
|
|
|
static void make_device(const char *path,
|
2015-02-06 21:19:48 +01:00
|
|
|
const char */*upath*/,
|
2014-06-12 18:29:14 +02:00
|
|
|
int block, int major, int minor,
|
|
|
|
const char **links)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
|
|
|
unsigned uid;
|
|
|
|
unsigned gid;
|
|
|
|
mode_t mode;
|
|
|
|
dev_t dev;
|
2012-01-13 14:48:47 +01:00
|
|
|
char *secontext = NULL;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2014-07-02 22:08:13 +02:00
|
|
|
mode = get_device_perm(path, links, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
|
2012-10-17 08:07:05 +02:00
|
|
|
|
2016-04-25 17:22:27 +02:00
|
|
|
if (selabel_lookup_best_match(sehandle, &secontext, path, links, mode)) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "Device '" << path << "' not created; cannot find SELinux label";
|
2016-04-25 17:22:27 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-06-13 07:03:50 +02:00
|
|
|
setfscreatecon(secontext);
|
2012-10-17 08:07:05 +02:00
|
|
|
|
2010-09-03 21:25:34 +02:00
|
|
|
dev = makedev(major, minor);
|
2010-01-22 03:13:39 +01:00
|
|
|
/* Temporarily change egid to avoid race condition setting the gid of the
|
|
|
|
* device node. Unforunately changing the euid would prevent creation of
|
|
|
|
* some device nodes, so the uid has to be set with chown() and is still
|
|
|
|
* racy. Fixing the gid race at least fixed the issue with system_server
|
|
|
|
* opening dynamic input devices under the AID_INPUT gid. */
|
|
|
|
setegid(gid);
|
2016-04-25 17:22:27 +02:00
|
|
|
/* If the node already exists update its SELinux label to handle cases when
|
|
|
|
* it was created with the wrong context during coldboot procedure. */
|
|
|
|
if (mknod(path, mode, dev) && (errno == EEXIST)) {
|
2016-06-02 18:53:44 +02:00
|
|
|
|
|
|
|
char* fcon = nullptr;
|
|
|
|
int rc = lgetfilecon(path, &fcon);
|
|
|
|
if (rc < 0) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "Cannot get SELinux label on '" << path << "' device";
|
2016-06-02 18:53:44 +02:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool different = strcmp(fcon, secontext) != 0;
|
|
|
|
freecon(fcon);
|
|
|
|
|
|
|
|
if (different && lsetfilecon(path, secontext)) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "Cannot set '" << secontext << "' SELinux label on '" << path << "' device";
|
2016-04-25 17:22:27 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-02 18:53:44 +02:00
|
|
|
|
|
|
|
out:
|
2010-01-22 03:13:39 +01:00
|
|
|
chown(path, uid, -1);
|
|
|
|
setegid(AID_ROOT);
|
2012-10-17 08:07:05 +02:00
|
|
|
|
2016-04-25 17:22:27 +02:00
|
|
|
freecon(secontext);
|
|
|
|
setfscreatecon(NULL);
|
2012-01-13 14:48:47 +01:00
|
|
|
}
|
|
|
|
|
2013-03-07 01:23:57 +01:00
|
|
|
static void add_platform_device(const char *path)
|
2011-03-31 03:32:12 +02:00
|
|
|
{
|
2013-03-07 01:23:57 +01:00
|
|
|
int path_len = strlen(path);
|
2011-03-31 03:32:12 +02:00
|
|
|
struct platform_node *bus;
|
2013-03-07 01:23:57 +01:00
|
|
|
const char *name = path;
|
|
|
|
|
|
|
|
if (!strncmp(path, "/devices/", 9)) {
|
|
|
|
name += 9;
|
|
|
|
if (!strncmp(name, "platform/", 9))
|
|
|
|
name += 9;
|
|
|
|
}
|
2011-03-31 03:32:12 +02:00
|
|
|
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "adding platform device " << name << " (" << path << ")";
|
2011-03-31 03:32:12 +02:00
|
|
|
|
2015-02-04 02:12:07 +01:00
|
|
|
bus = (platform_node*) calloc(1, sizeof(struct platform_node));
|
2013-03-07 01:23:57 +01:00
|
|
|
bus->path = strdup(path);
|
|
|
|
bus->path_len = path_len;
|
|
|
|
bus->name = bus->path + (name - path);
|
2011-03-31 03:32:12 +02:00
|
|
|
list_add_tail(&platform_names, &bus->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-07 01:23:57 +01:00
|
|
|
* given a path that may start with a platform device, find the length of the
|
2011-03-31 03:32:12 +02:00
|
|
|
* platform device prefix. If it doesn't start with a platform device, return
|
|
|
|
* 0.
|
|
|
|
*/
|
2013-03-07 01:23:57 +01:00
|
|
|
static struct platform_node *find_platform_device(const char *path)
|
2011-03-31 03:32:12 +02:00
|
|
|
{
|
2013-03-07 01:23:57 +01:00
|
|
|
int path_len = strlen(path);
|
2011-03-31 03:32:12 +02:00
|
|
|
struct listnode *node;
|
|
|
|
struct platform_node *bus;
|
|
|
|
|
|
|
|
list_for_each_reverse(node, &platform_names) {
|
|
|
|
bus = node_to_item(node, struct platform_node, list);
|
2013-03-07 01:23:57 +01:00
|
|
|
if ((bus->path_len < path_len) &&
|
|
|
|
(path[bus->path_len] == '/') &&
|
|
|
|
!strncmp(path, bus->path, bus->path_len))
|
|
|
|
return bus;
|
2011-03-31 03:32:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-07 01:23:57 +01:00
|
|
|
static void remove_platform_device(const char *path)
|
2011-03-31 03:32:12 +02:00
|
|
|
{
|
|
|
|
struct listnode *node;
|
|
|
|
struct platform_node *bus;
|
|
|
|
|
|
|
|
list_for_each_reverse(node, &platform_names) {
|
|
|
|
bus = node_to_item(node, struct platform_node, list);
|
2013-03-07 01:23:57 +01:00
|
|
|
if (!strcmp(path, bus->path)) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "removing platform device " << bus->name;
|
2013-03-07 01:23:57 +01:00
|
|
|
free(bus->path);
|
2011-03-31 03:32:12 +02:00
|
|
|
list_remove(node);
|
|
|
|
free(bus);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 02:41:20 +02:00
|
|
|
/* Given a path that may start with a PCI device, populate the supplied buffer
|
|
|
|
* with the PCI domain/bus number and the peripheral ID and return 0.
|
|
|
|
* If it doesn't start with a PCI device, or there is some error, return -1 */
|
|
|
|
static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
|
|
|
|
{
|
|
|
|
const char *start, *end;
|
|
|
|
|
|
|
|
if (strncmp(path, "/devices/pci", 12))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Beginning of the prefix is the initial "pci" after "/devices/" */
|
|
|
|
start = path + 9;
|
|
|
|
|
|
|
|
/* End of the prefix is two path '/' later, capturing the domain/bus number
|
|
|
|
* and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
|
|
|
|
end = strchr(start, '/');
|
|
|
|
if (!end)
|
|
|
|
return -1;
|
|
|
|
end = strchr(end + 1, '/');
|
|
|
|
if (!end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Make sure we have enough room for the string plus null terminator */
|
|
|
|
if (end - start + 1 > buf_sz)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
strncpy(buf, start, end - start);
|
|
|
|
buf[end - start] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
static void parse_event(const char *msg, struct uevent *uevent)
|
|
|
|
{
|
|
|
|
uevent->action = "";
|
|
|
|
uevent->path = "";
|
|
|
|
uevent->subsystem = "";
|
|
|
|
uevent->firmware = "";
|
|
|
|
uevent->major = -1;
|
|
|
|
uevent->minor = -1;
|
2010-04-09 01:16:20 +02:00
|
|
|
uevent->partition_name = NULL;
|
|
|
|
uevent->partition_num = -1;
|
2012-03-23 22:15:34 +01:00
|
|
|
uevent->device_name = NULL;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
/* currently ignoring SEQNUM */
|
|
|
|
while(*msg) {
|
|
|
|
if(!strncmp(msg, "ACTION=", 7)) {
|
|
|
|
msg += 7;
|
|
|
|
uevent->action = msg;
|
|
|
|
} else if(!strncmp(msg, "DEVPATH=", 8)) {
|
|
|
|
msg += 8;
|
|
|
|
uevent->path = msg;
|
|
|
|
} else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
|
|
|
|
msg += 10;
|
|
|
|
uevent->subsystem = msg;
|
|
|
|
} else if(!strncmp(msg, "FIRMWARE=", 9)) {
|
|
|
|
msg += 9;
|
|
|
|
uevent->firmware = msg;
|
|
|
|
} else if(!strncmp(msg, "MAJOR=", 6)) {
|
|
|
|
msg += 6;
|
|
|
|
uevent->major = atoi(msg);
|
|
|
|
} else if(!strncmp(msg, "MINOR=", 6)) {
|
|
|
|
msg += 6;
|
|
|
|
uevent->minor = atoi(msg);
|
2010-04-09 01:16:20 +02:00
|
|
|
} else if(!strncmp(msg, "PARTN=", 6)) {
|
|
|
|
msg += 6;
|
|
|
|
uevent->partition_num = atoi(msg);
|
|
|
|
} else if(!strncmp(msg, "PARTNAME=", 9)) {
|
|
|
|
msg += 9;
|
|
|
|
uevent->partition_name = msg;
|
2012-03-23 22:15:34 +01:00
|
|
|
} else if(!strncmp(msg, "DEVNAME=", 8)) {
|
|
|
|
msg += 8;
|
|
|
|
uevent->device_name = msg;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2012-03-23 22:15:34 +01:00
|
|
|
/* advance to after the next \0 */
|
2008-10-21 16:00:00 +02:00
|
|
|
while(*msg++)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2015-02-04 23:46:36 +01:00
|
|
|
if (LOG_UEVENTS) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << android::base::StringPrintf("event { '%s', '%s', '%s', '%s', %d, %d }",
|
|
|
|
uevent->action, uevent->path, uevent->subsystem,
|
|
|
|
uevent->firmware, uevent->major, uevent->minor);
|
2015-02-04 23:46:36 +01:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2010-08-03 23:36:02 +02:00
|
|
|
static char **get_character_device_symlinks(struct uevent *uevent)
|
|
|
|
{
|
|
|
|
const char *parent;
|
2016-03-28 23:22:12 +02:00
|
|
|
const char *slash;
|
2010-08-03 23:36:02 +02:00
|
|
|
char **links;
|
|
|
|
int link_num = 0;
|
|
|
|
int width;
|
2013-03-07 01:23:57 +01:00
|
|
|
struct platform_node *pdev;
|
2010-08-03 23:36:02 +02:00
|
|
|
|
2013-03-07 01:23:57 +01:00
|
|
|
pdev = find_platform_device(uevent->path);
|
|
|
|
if (!pdev)
|
2010-08-03 23:36:02 +02:00
|
|
|
return NULL;
|
|
|
|
|
2015-02-04 02:12:07 +01:00
|
|
|
links = (char**) malloc(sizeof(char *) * 2);
|
2010-08-03 23:36:02 +02:00
|
|
|
if (!links)
|
|
|
|
return NULL;
|
|
|
|
memset(links, 0, sizeof(char *) * 2);
|
|
|
|
|
|
|
|
/* skip "/devices/platform/<driver>" */
|
2013-03-07 01:23:57 +01:00
|
|
|
parent = strchr(uevent->path + pdev->path_len, '/');
|
2013-11-05 13:17:45 +01:00
|
|
|
if (!parent)
|
2010-08-03 23:36:02 +02:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!strncmp(parent, "/usb", 4)) {
|
|
|
|
/* skip root hub name and device. use device interface */
|
|
|
|
while (*++parent && *parent != '/');
|
|
|
|
if (*parent)
|
|
|
|
while (*++parent && *parent != '/');
|
|
|
|
if (!*parent)
|
|
|
|
goto err;
|
|
|
|
slash = strchr(++parent, '/');
|
|
|
|
if (!slash)
|
|
|
|
goto err;
|
|
|
|
width = slash - parent;
|
|
|
|
if (width <= 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
|
|
|
|
link_num++;
|
|
|
|
else
|
|
|
|
links[link_num] = NULL;
|
|
|
|
mkdir("/dev/usb", 0755);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return links;
|
|
|
|
err:
|
|
|
|
free(links);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-14 02:41:20 +02:00
|
|
|
static char **get_block_device_symlinks(struct uevent *uevent)
|
2010-04-09 01:16:20 +02:00
|
|
|
{
|
2011-03-31 03:32:12 +02:00
|
|
|
const char *device;
|
2013-03-07 01:23:57 +01:00
|
|
|
struct platform_node *pdev;
|
2016-03-28 23:22:12 +02:00
|
|
|
const char *slash;
|
2013-09-14 02:41:20 +02:00
|
|
|
const char *type;
|
2010-04-09 01:16:20 +02:00
|
|
|
char buf[256];
|
|
|
|
char link_path[256];
|
|
|
|
int link_num = 0;
|
|
|
|
char *p;
|
|
|
|
|
2013-03-07 01:23:57 +01:00
|
|
|
pdev = find_platform_device(uevent->path);
|
2013-09-14 02:41:20 +02:00
|
|
|
if (pdev) {
|
|
|
|
device = pdev->name;
|
|
|
|
type = "platform";
|
|
|
|
} else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
|
|
|
|
device = buf;
|
|
|
|
type = "pci";
|
|
|
|
} else {
|
2013-03-07 01:23:57 +01:00
|
|
|
return NULL;
|
2013-09-14 02:41:20 +02:00
|
|
|
}
|
2013-03-07 01:23:57 +01:00
|
|
|
|
2015-02-04 02:12:07 +01:00
|
|
|
char **links = (char**) malloc(sizeof(char *) * 4);
|
2010-04-09 01:16:20 +02:00
|
|
|
if (!links)
|
|
|
|
return NULL;
|
|
|
|
memset(links, 0, sizeof(char *) * 4);
|
|
|
|
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "found " << type << " device " << device;
|
2011-03-31 03:32:12 +02:00
|
|
|
|
2013-09-14 02:41:20 +02:00
|
|
|
snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
|
2010-04-09 01:16:20 +02:00
|
|
|
|
|
|
|
if (uevent->partition_name) {
|
|
|
|
p = strdup(uevent->partition_name);
|
|
|
|
sanitize(p);
|
2016-06-25 00:12:21 +02:00
|
|
|
if (strcmp(uevent->partition_name, p)) {
|
|
|
|
LOG(VERBOSE) << "Linking partition '" << uevent->partition_name << "' as '" << p << "'";
|
|
|
|
}
|
2010-04-09 01:16:20 +02:00
|
|
|
if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
|
|
|
|
link_num++;
|
|
|
|
else
|
|
|
|
links[link_num] = NULL;
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uevent->partition_num >= 0) {
|
|
|
|
if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
|
|
|
|
link_num++;
|
|
|
|
else
|
|
|
|
links[link_num] = NULL;
|
|
|
|
}
|
|
|
|
|
2013-03-07 01:23:57 +01:00
|
|
|
slash = strrchr(uevent->path, '/');
|
2010-04-09 01:16:20 +02:00
|
|
|
if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
|
|
|
|
link_num++;
|
|
|
|
else
|
|
|
|
links[link_num] = NULL;
|
|
|
|
|
|
|
|
return links;
|
|
|
|
}
|
|
|
|
|
2011-03-31 02:37:17 +02:00
|
|
|
static void handle_device(const char *action, const char *devpath,
|
|
|
|
const char *path, int block, int major, int minor, char **links)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
2010-04-09 01:16:20 +02:00
|
|
|
int i;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2011-03-31 02:37:17 +02:00
|
|
|
if(!strcmp(action, "add")) {
|
2014-06-12 18:29:14 +02:00
|
|
|
make_device(devpath, path, block, major, minor, (const char **)links);
|
2010-04-09 01:16:20 +02:00
|
|
|
if (links) {
|
|
|
|
for (i = 0; links[i]; i++)
|
2013-09-05 20:19:21 +02:00
|
|
|
make_link_init(devpath, links[i]);
|
2010-04-09 01:16:20 +02:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
2011-03-31 02:37:17 +02:00
|
|
|
if(!strcmp(action, "remove")) {
|
2010-04-09 01:16:20 +02:00
|
|
|
if (links) {
|
|
|
|
for (i = 0; links[i]; i++)
|
|
|
|
remove_link(devpath, links[i]);
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
unlink(devpath);
|
2010-04-09 01:16:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (links) {
|
|
|
|
for (i = 0; links[i]; i++)
|
|
|
|
free(links[i]);
|
|
|
|
free(links);
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-31 03:32:12 +02:00
|
|
|
static void handle_platform_device_event(struct uevent *uevent)
|
|
|
|
{
|
2013-03-07 01:23:57 +01:00
|
|
|
const char *path = uevent->path;
|
2011-03-31 03:32:12 +02:00
|
|
|
|
|
|
|
if (!strcmp(uevent->action, "add"))
|
2013-03-07 01:23:57 +01:00
|
|
|
add_platform_device(path);
|
2011-03-31 03:32:12 +02:00
|
|
|
else if (!strcmp(uevent->action, "remove"))
|
2013-03-07 01:23:57 +01:00
|
|
|
remove_platform_device(path);
|
2011-03-31 03:32:12 +02:00
|
|
|
}
|
|
|
|
|
2011-03-31 02:37:17 +02:00
|
|
|
static const char *parse_device_name(struct uevent *uevent, unsigned int len)
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
/* if it's not a /dev device, nothing else to do */
|
|
|
|
if((uevent->major < 0) || (uevent->minor < 0))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* do we have a name? */
|
|
|
|
name = strrchr(uevent->path, '/');
|
|
|
|
if(!name)
|
|
|
|
return NULL;
|
|
|
|
name++;
|
|
|
|
|
|
|
|
/* too-long names would overrun our buffer */
|
2013-11-21 22:26:48 +01:00
|
|
|
if(strlen(name) > len) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << "DEVPATH=" << name << " exceeds " << len << "-character limit on filename; ignoring event";
|
2011-03-31 02:37:17 +02:00
|
|
|
return NULL;
|
2013-11-21 22:26:48 +01:00
|
|
|
}
|
2011-03-31 02:37:17 +02:00
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_block_device_event(struct uevent *uevent)
|
|
|
|
{
|
|
|
|
const char *base = "/dev/block/";
|
|
|
|
const char *name;
|
|
|
|
char devpath[96];
|
|
|
|
char **links = NULL;
|
|
|
|
|
|
|
|
name = parse_device_name(uevent, 64);
|
|
|
|
if (!name)
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf(devpath, sizeof(devpath), "%s%s", base, name);
|
2012-01-13 14:48:47 +01:00
|
|
|
make_dir(base, 0755);
|
2011-03-31 02:37:17 +02:00
|
|
|
|
2013-03-07 01:23:57 +01:00
|
|
|
if (!strncmp(uevent->path, "/devices/", 9))
|
2013-09-14 02:41:20 +02:00
|
|
|
links = get_block_device_symlinks(uevent);
|
2011-03-31 02:37:17 +02:00
|
|
|
|
|
|
|
handle_device(uevent->action, devpath, uevent->path, 1,
|
|
|
|
uevent->major, uevent->minor, links);
|
|
|
|
}
|
|
|
|
|
2013-11-19 00:24:40 +01:00
|
|
|
#define DEVPATH_LEN 96
|
|
|
|
|
|
|
|
static bool assemble_devpath(char *devpath, const char *dirname,
|
|
|
|
const char *devname)
|
|
|
|
{
|
|
|
|
int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
|
|
|
|
if (s < 0) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "failed to assemble device path; ignoring event";
|
2013-11-19 00:24:40 +01:00
|
|
|
return false;
|
|
|
|
} else if (s >= DEVPATH_LEN) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << dirname << "/" << devname
|
|
|
|
<< " exceeds " << DEVPATH_LEN << "-character limit on path; ignoring event";
|
2013-11-19 00:24:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mkdir_recursive_for_devpath(const char *devpath)
|
|
|
|
{
|
|
|
|
char dir[DEVPATH_LEN];
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
strcpy(dir, devpath);
|
|
|
|
slash = strrchr(dir, '/');
|
|
|
|
*slash = '\0';
|
|
|
|
mkdir_recursive(dir, 0755);
|
|
|
|
}
|
|
|
|
|
2011-03-31 02:37:17 +02:00
|
|
|
static void handle_generic_device_event(struct uevent *uevent)
|
|
|
|
{
|
2015-02-04 02:12:07 +01:00
|
|
|
const char *base;
|
2011-03-31 02:37:17 +02:00
|
|
|
const char *name;
|
2013-11-19 00:24:40 +01:00
|
|
|
char devpath[DEVPATH_LEN] = {0};
|
2011-03-31 02:37:17 +02:00
|
|
|
char **links = NULL;
|
|
|
|
|
|
|
|
name = parse_device_name(uevent, 64);
|
|
|
|
if (!name)
|
|
|
|
return;
|
|
|
|
|
2013-11-19 00:24:40 +01:00
|
|
|
struct ueventd_subsystem *subsystem =
|
|
|
|
ueventd_subsystem_find_by_name(uevent->subsystem);
|
|
|
|
|
|
|
|
if (subsystem) {
|
|
|
|
const char *devname;
|
|
|
|
|
|
|
|
switch (subsystem->devname_src) {
|
|
|
|
case DEVNAME_UEVENT_DEVNAME:
|
|
|
|
devname = uevent->device_name;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEVNAME_UEVENT_DEVPATH:
|
|
|
|
devname = name;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(ERROR) << uevent->subsystem << " subsystem's devpath option is not set; ignoring event";
|
2013-11-19 00:24:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!assemble_devpath(devpath, subsystem->dirname, devname))
|
|
|
|
return;
|
|
|
|
mkdir_recursive_for_devpath(devpath);
|
|
|
|
} else if (!strncmp(uevent->subsystem, "usb", 3)) {
|
2011-03-31 02:37:17 +02:00
|
|
|
if (!strcmp(uevent->subsystem, "usb")) {
|
2012-03-23 22:15:34 +01:00
|
|
|
if (uevent->device_name) {
|
2013-11-19 00:24:40 +01:00
|
|
|
if (!assemble_devpath(devpath, "/dev", uevent->device_name))
|
2013-11-21 22:26:48 +01:00
|
|
|
return;
|
2013-11-19 00:24:40 +01:00
|
|
|
mkdir_recursive_for_devpath(devpath);
|
2012-03-23 22:15:34 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* This imitates the file system that would be created
|
|
|
|
* if we were using devfs instead.
|
|
|
|
* Minors are broken up into groups of 128, starting at "001"
|
|
|
|
*/
|
|
|
|
int bus_id = uevent->minor / 128 + 1;
|
|
|
|
int device_id = uevent->minor % 128 + 1;
|
|
|
|
/* build directories */
|
|
|
|
make_dir("/dev/bus", 0755);
|
|
|
|
make_dir("/dev/bus/usb", 0755);
|
|
|
|
snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
|
|
|
|
make_dir(devpath, 0755);
|
|
|
|
snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
|
|
|
|
}
|
2011-03-31 02:37:17 +02:00
|
|
|
} else {
|
|
|
|
/* ignore other USB events */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (!strncmp(uevent->subsystem, "graphics", 8)) {
|
|
|
|
base = "/dev/graphics/";
|
2012-01-13 14:48:47 +01:00
|
|
|
make_dir(base, 0755);
|
2011-09-28 15:30:07 +02:00
|
|
|
} else if (!strncmp(uevent->subsystem, "drm", 3)) {
|
|
|
|
base = "/dev/dri/";
|
|
|
|
make_dir(base, 0755);
|
2011-03-31 02:37:17 +02:00
|
|
|
} else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
|
|
|
|
base = "/dev/oncrpc/";
|
2012-01-13 14:48:47 +01:00
|
|
|
make_dir(base, 0755);
|
2011-03-31 02:37:17 +02:00
|
|
|
} else if (!strncmp(uevent->subsystem, "adsp", 4)) {
|
|
|
|
base = "/dev/adsp/";
|
2012-01-13 14:48:47 +01:00
|
|
|
make_dir(base, 0755);
|
2011-03-31 02:37:17 +02:00
|
|
|
} else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
|
|
|
|
base = "/dev/msm_camera/";
|
2012-01-13 14:48:47 +01:00
|
|
|
make_dir(base, 0755);
|
2011-03-31 02:37:17 +02:00
|
|
|
} else if(!strncmp(uevent->subsystem, "input", 5)) {
|
|
|
|
base = "/dev/input/";
|
2012-01-13 14:48:47 +01:00
|
|
|
make_dir(base, 0755);
|
2011-03-31 02:37:17 +02:00
|
|
|
} else if(!strncmp(uevent->subsystem, "mtd", 3)) {
|
|
|
|
base = "/dev/mtd/";
|
2012-01-13 14:48:47 +01:00
|
|
|
make_dir(base, 0755);
|
2011-03-31 02:37:17 +02:00
|
|
|
} else if(!strncmp(uevent->subsystem, "sound", 5)) {
|
|
|
|
base = "/dev/snd/";
|
2012-01-13 14:48:47 +01:00
|
|
|
make_dir(base, 0755);
|
2016-06-25 00:12:21 +02:00
|
|
|
} else if(!strncmp(uevent->subsystem, "misc", 4) && !strncmp(name, "log_", 4)) {
|
|
|
|
LOG(INFO) << "kernel logger is deprecated";
|
2011-03-31 02:37:17 +02:00
|
|
|
base = "/dev/log/";
|
2012-01-13 14:48:47 +01:00
|
|
|
make_dir(base, 0755);
|
2011-03-31 02:37:17 +02:00
|
|
|
name += 4;
|
|
|
|
} else
|
|
|
|
base = "/dev/";
|
|
|
|
links = get_character_device_symlinks(uevent);
|
|
|
|
|
|
|
|
if (!devpath[0])
|
|
|
|
snprintf(devpath, sizeof(devpath), "%s%s", base, name);
|
|
|
|
|
|
|
|
handle_device(uevent->action, devpath, uevent->path, 0,
|
|
|
|
uevent->major, uevent->minor, links);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_device_event(struct uevent *uevent)
|
|
|
|
{
|
2014-04-30 04:14:37 +02:00
|
|
|
if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
|
2016-05-06 17:06:59 +02:00
|
|
|
fixup_sys_perms(uevent->path, uevent->subsystem);
|
2011-03-31 02:37:17 +02:00
|
|
|
|
|
|
|
if (!strncmp(uevent->subsystem, "block", 5)) {
|
|
|
|
handle_block_device_event(uevent);
|
2011-03-31 03:32:12 +02:00
|
|
|
} else if (!strncmp(uevent->subsystem, "platform", 8)) {
|
|
|
|
handle_platform_device_event(uevent);
|
2011-03-31 02:37:17 +02:00
|
|
|
} else {
|
|
|
|
handle_generic_device_event(uevent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
static int load_firmware(int fw_fd, int loading_fd, int data_fd)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
long len_to_copy;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if(fstat(fw_fd, &st) < 0)
|
|
|
|
return -1;
|
|
|
|
len_to_copy = st.st_size;
|
|
|
|
|
|
|
|
write(loading_fd, "1", 1); /* start transfer */
|
|
|
|
|
|
|
|
while (len_to_copy > 0) {
|
|
|
|
char buf[PAGE_SIZE];
|
|
|
|
ssize_t nr;
|
|
|
|
|
|
|
|
nr = read(fw_fd, buf, sizeof(buf));
|
|
|
|
if(!nr)
|
|
|
|
break;
|
|
|
|
if(nr < 0) {
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
2016-01-28 09:10:54 +01:00
|
|
|
if (!android::base::WriteFully(data_fd, buf, nr)) {
|
|
|
|
ret = -1;
|
|
|
|
break;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
2016-01-28 09:10:54 +01:00
|
|
|
len_to_copy -= nr;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
write(loading_fd, "0", 1); /* successful end of transfer */
|
|
|
|
else
|
|
|
|
write(loading_fd, "-1", 2); /* abort transfer */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-24 23:45:30 +01:00
|
|
|
static int is_booting(void)
|
|
|
|
{
|
|
|
|
return access("/dev/.booting", F_OK) == 0;
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
static void process_firmware_event(struct uevent *uevent)
|
|
|
|
{
|
2015-03-20 23:10:29 +01:00
|
|
|
char *root, *loading, *data;
|
2008-10-21 16:00:00 +02:00
|
|
|
int l, loading_fd, data_fd, fw_fd;
|
2015-03-20 23:10:29 +01:00
|
|
|
size_t i;
|
2011-03-24 23:45:30 +01:00
|
|
|
int booting = is_booting();
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "firmware: loading '" << uevent->firmware << "' for '" << uevent->path << "'";
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
|
|
|
|
if (l == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
l = asprintf(&loading, "%sloading", root);
|
|
|
|
if (l == -1)
|
|
|
|
goto root_free_out;
|
|
|
|
|
|
|
|
l = asprintf(&data, "%sdata", root);
|
|
|
|
if (l == -1)
|
|
|
|
goto loading_free_out;
|
|
|
|
|
2015-02-02 23:37:22 +01:00
|
|
|
loading_fd = open(loading, O_WRONLY|O_CLOEXEC);
|
2008-10-21 16:00:00 +02:00
|
|
|
if(loading_fd < 0)
|
2015-03-20 23:10:29 +01:00
|
|
|
goto data_free_out;
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-02-02 23:37:22 +01:00
|
|
|
data_fd = open(data, O_WRONLY|O_CLOEXEC);
|
2008-10-21 16:00:00 +02:00
|
|
|
if(data_fd < 0)
|
|
|
|
goto loading_close_out;
|
|
|
|
|
2011-03-24 23:45:30 +01:00
|
|
|
try_loading_again:
|
2016-06-25 00:12:21 +02:00
|
|
|
for (i = 0; i < arraysize(firmware_dirs); i++) {
|
2015-03-20 23:10:29 +01:00
|
|
|
char *file = NULL;
|
|
|
|
l = asprintf(&file, "%s/%s", firmware_dirs[i], uevent->firmware);
|
|
|
|
if (l == -1)
|
|
|
|
goto data_free_out;
|
|
|
|
fw_fd = open(file, O_RDONLY|O_CLOEXEC);
|
|
|
|
free(file);
|
|
|
|
if (fw_fd >= 0) {
|
2016-06-25 00:12:21 +02:00
|
|
|
if (!load_firmware(fw_fd, loading_fd, data_fd)) {
|
|
|
|
LOG(INFO) << "firmware: copy success { '" << root << "', '" << uevent->firmware << "' }";
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "firmware: copy failure { '" << root << "', '" << uevent->firmware << "' }";
|
|
|
|
}
|
2015-03-20 23:10:29 +01:00
|
|
|
break;
|
2010-11-10 03:10:24 +01:00
|
|
|
}
|
2010-09-19 12:36:39 +02:00
|
|
|
}
|
2015-03-20 23:10:29 +01:00
|
|
|
if (fw_fd < 0) {
|
|
|
|
if (booting) {
|
|
|
|
/* If we're not fully booted, we may be missing
|
|
|
|
* filesystems needed for firmware, wait and retry.
|
|
|
|
*/
|
|
|
|
usleep(100000);
|
|
|
|
booting = is_booting();
|
|
|
|
goto try_loading_again;
|
|
|
|
}
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "firmware: could not open '" << uevent->firmware << "'";
|
2015-03-20 23:10:29 +01:00
|
|
|
write(loading_fd, "-1", 2);
|
|
|
|
goto data_close_out;
|
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
|
|
|
close(fw_fd);
|
|
|
|
data_close_out:
|
|
|
|
close(data_fd);
|
|
|
|
loading_close_out:
|
|
|
|
close(loading_fd);
|
|
|
|
data_free_out:
|
|
|
|
free(data);
|
|
|
|
loading_free_out:
|
|
|
|
free(loading);
|
|
|
|
root_free_out:
|
|
|
|
free(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_firmware_event(struct uevent *uevent)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
if(strcmp(uevent->subsystem, "firmware"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(strcmp(uevent->action, "add"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* we fork, to avoid making large memory allocations in init proper */
|
|
|
|
pid = fork();
|
|
|
|
if (!pid) {
|
|
|
|
process_firmware_event(uevent);
|
2014-08-21 01:16:44 +02:00
|
|
|
_exit(EXIT_SUCCESS);
|
|
|
|
} else if (pid < 0) {
|
2016-06-25 00:12:21 +02:00
|
|
|
PLOG(ERROR) << "could not fork to process firmware event";
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 20:22:09 +02:00
|
|
|
#define UEVENT_MSG_LEN 2048
|
2010-04-14 04:25:51 +02:00
|
|
|
void handle_device_fd()
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
2011-04-25 05:08:17 +02:00
|
|
|
char msg[UEVENT_MSG_LEN+2];
|
|
|
|
int n;
|
2011-05-11 23:58:24 +02:00
|
|
|
while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
|
2010-07-19 23:31:20 +02:00
|
|
|
if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
|
2008-10-21 16:00:00 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
msg[n] = '\0';
|
|
|
|
msg[n+1] = '\0';
|
|
|
|
|
2010-07-19 23:31:20 +02:00
|
|
|
struct uevent uevent;
|
2008-10-21 16:00:00 +02:00
|
|
|
parse_event(msg, &uevent);
|
|
|
|
|
2015-06-13 07:03:50 +02:00
|
|
|
if (selinux_status_updated() > 0) {
|
2013-04-16 15:30:30 +02:00
|
|
|
struct selabel_handle *sehandle2;
|
|
|
|
sehandle2 = selinux_android_file_context_handle();
|
|
|
|
if (sehandle2) {
|
|
|
|
selabel_close(sehandle);
|
|
|
|
sehandle = sehandle2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-21 16:00:00 +02:00
|
|
|
handle_device_event(&uevent);
|
|
|
|
handle_firmware_event(&uevent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Coldboot walks parts of the /sys tree and pokes the uevent files
|
|
|
|
** to cause the kernel to regenerate device add events that happened
|
|
|
|
** before init's device manager was started
|
|
|
|
**
|
|
|
|
** We drain any pending events from the netlink socket every time
|
|
|
|
** we poke another uevent file to make sure we don't overrun the
|
2015-02-04 23:46:36 +01:00
|
|
|
** socket's buffer.
|
2008-10-21 16:00:00 +02:00
|
|
|
*/
|
|
|
|
|
2010-04-14 04:25:51 +02:00
|
|
|
static void do_coldboot(DIR *d)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
|
|
|
struct dirent *de;
|
|
|
|
int dfd, fd;
|
|
|
|
|
|
|
|
dfd = dirfd(d);
|
|
|
|
|
|
|
|
fd = openat(dfd, "uevent", O_WRONLY);
|
|
|
|
if(fd >= 0) {
|
|
|
|
write(fd, "add\n", 4);
|
|
|
|
close(fd);
|
2010-04-14 04:25:51 +02:00
|
|
|
handle_device_fd();
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while((de = readdir(d))) {
|
|
|
|
DIR *d2;
|
|
|
|
|
|
|
|
if(de->d_type != DT_DIR || de->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
|
|
|
|
if(fd < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
d2 = fdopendir(fd);
|
|
|
|
if(d2 == 0)
|
|
|
|
close(fd);
|
|
|
|
else {
|
2010-04-14 04:25:51 +02:00
|
|
|
do_coldboot(d2);
|
2008-10-21 16:00:00 +02:00
|
|
|
closedir(d2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-14 04:25:51 +02:00
|
|
|
static void coldboot(const char *path)
|
2008-10-21 16:00:00 +02:00
|
|
|
{
|
2016-02-18 23:52:46 +01:00
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path), closedir);
|
2008-10-21 16:00:00 +02:00
|
|
|
if(d) {
|
2016-02-18 23:52:46 +01:00
|
|
|
do_coldboot(d.get());
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-28 18:51:23 +01:00
|
|
|
void device_init() {
|
2015-06-13 07:03:50 +02:00
|
|
|
sehandle = selinux_android_file_context_handle();
|
|
|
|
selinux_status_open(true);
|
2012-10-17 08:07:05 +02:00
|
|
|
|
2012-12-05 00:47:20 +01:00
|
|
|
/* is 256K enough? udev uses 16MB! */
|
|
|
|
device_fd = uevent_open_socket(256*1024, true);
|
2015-03-28 19:23:32 +01:00
|
|
|
if (device_fd == -1) {
|
2010-04-14 04:25:51 +02:00
|
|
|
return;
|
2015-03-28 19:23:32 +01:00
|
|
|
}
|
2010-04-14 04:25:51 +02:00
|
|
|
fcntl(device_fd, F_SETFL, O_NONBLOCK);
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2015-03-28 19:23:32 +01:00
|
|
|
if (access(COLDBOOT_DONE, F_OK) == 0) {
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(VERBOSE) << "Skipping coldboot, already done!";
|
2015-03-28 19:23:32 +01:00
|
|
|
return;
|
2010-04-21 21:04:20 +02:00
|
|
|
}
|
2015-03-28 19:23:32 +01:00
|
|
|
|
|
|
|
Timer t;
|
|
|
|
coldboot("/sys/class");
|
|
|
|
coldboot("/sys/block");
|
|
|
|
coldboot("/sys/devices");
|
|
|
|
close(open(COLDBOOT_DONE, O_WRONLY|O_CREAT|O_CLOEXEC, 0000));
|
2016-06-25 00:12:21 +02:00
|
|
|
LOG(INFO) << "Coldboot took " << t.duration() << "s.";
|
2010-04-14 04:25:51 +02:00
|
|
|
}
|
2008-10-21 16:00:00 +02:00
|
|
|
|
2010-04-14 04:25:51 +02:00
|
|
|
int get_device_fd()
|
|
|
|
{
|
|
|
|
return device_fd;
|
2008-10-21 16:00:00 +02:00
|
|
|
}
|