2008-10-21 22:00:00 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* 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>
|
2010-11-18 03:47:23 +08:00
|
|
|
#include <stddef.h>
|
2008-10-21 22:00:00 +08: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>
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <asm/page.h>
|
2010-06-04 03:21:01 +08:00
|
|
|
#include <sys/wait.h>
|
2008-10-21 22:00:00 +08:00
|
|
|
|
|
|
|
#include "devices.h"
|
2010-04-09 07:16:20 +08:00
|
|
|
#include "util.h"
|
2010-04-20 08:05:34 +08:00
|
|
|
#include "log.h"
|
|
|
|
#include "list.h"
|
2008-10-21 22:00:00 +08:00
|
|
|
|
|
|
|
#define SYSFS_PREFIX "/sys"
|
2010-09-19 18:36:39 +08:00
|
|
|
#define FIRMWARE_DIR1 "/etc/firmware"
|
|
|
|
#define FIRMWARE_DIR2 "/vendor/firmware"
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2010-04-14 10:25:51 +08:00
|
|
|
static int device_fd = -1;
|
|
|
|
|
2008-10-21 22:00:00 +08:00
|
|
|
struct uevent {
|
|
|
|
const char *action;
|
|
|
|
const char *path;
|
|
|
|
const char *subsystem;
|
|
|
|
const char *firmware;
|
2010-04-09 07:16:20 +08:00
|
|
|
const char *partition_name;
|
|
|
|
int partition_num;
|
2008-10-21 22:00:00 +08:00
|
|
|
int major;
|
|
|
|
int minor;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int open_uevent_socket(void)
|
|
|
|
{
|
|
|
|
struct sockaddr_nl addr;
|
|
|
|
int sz = 64*1024; // XXX larger? udev uses 16MB!
|
2010-07-20 05:31:20 +08:00
|
|
|
int on = 1;
|
2008-10-21 22:00:00 +08:00
|
|
|
int s;
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.nl_family = AF_NETLINK;
|
|
|
|
addr.nl_pid = getpid();
|
|
|
|
addr.nl_groups = 0xffffffff;
|
|
|
|
|
|
|
|
s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
|
|
|
|
if(s < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
|
2010-07-20 05:31:20 +08:00
|
|
|
setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
|
2008-10-21 22:00:00 +08:00
|
|
|
|
|
|
|
if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct perms_ {
|
|
|
|
char *name;
|
2010-10-27 06:09:43 +08:00
|
|
|
char *attr;
|
2008-10-21 22:00:00 +08:00
|
|
|
mode_t perm;
|
|
|
|
unsigned int uid;
|
|
|
|
unsigned int gid;
|
|
|
|
unsigned short prefix;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct perm_node {
|
|
|
|
struct perms_ dp;
|
|
|
|
struct listnode plist;
|
|
|
|
};
|
2010-10-27 06:09:43 +08:00
|
|
|
|
|
|
|
static list_declare(sys_perms);
|
2010-04-21 05:32:50 +08:00
|
|
|
static list_declare(dev_perms);
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2010-10-27 06:09:43 +08:00
|
|
|
int add_dev_perms(const char *name, const char *attr,
|
|
|
|
mode_t perm, unsigned int uid, unsigned int gid,
|
|
|
|
unsigned short prefix) {
|
|
|
|
struct perm_node *node = calloc(1, sizeof(*node));
|
2008-10-21 22:00:00 +08:00
|
|
|
if (!node)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2010-10-27 06:09:43 +08:00
|
|
|
node->dp.name = strdup(name);
|
|
|
|
if (!node->dp.name)
|
2008-10-21 22:00:00 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2010-10-27 06:09:43 +08:00
|
|
|
if (attr) {
|
|
|
|
node->dp.attr = strdup(attr);
|
|
|
|
if (!node->dp.attr)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2008-10-21 22:00:00 +08:00
|
|
|
node->dp.perm = perm;
|
|
|
|
node->dp.uid = uid;
|
|
|
|
node->dp.gid = gid;
|
|
|
|
node->dp.prefix = prefix;
|
|
|
|
|
2010-10-27 06:09:43 +08:00
|
|
|
if (attr)
|
|
|
|
list_add_tail(&sys_perms, &node->plist);
|
|
|
|
else
|
|
|
|
list_add_tail(&dev_perms, &node->plist);
|
|
|
|
|
2008-10-21 22:00:00 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-27 06:09:43 +08:00
|
|
|
void fixup_sys_perms(const char *upath)
|
2008-10-21 22:00:00 +08:00
|
|
|
{
|
2010-10-27 06:09:43 +08:00
|
|
|
char buf[512];
|
|
|
|
struct listnode *node;
|
|
|
|
struct perms_ *dp;
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2010-10-27 06:09:43 +08:00
|
|
|
/* upaths omit the "/sys" that paths in this list
|
|
|
|
* contain, so we add 4 when comparing...
|
|
|
|
*/
|
|
|
|
list_for_each(node, &sys_perms) {
|
|
|
|
dp = &(node_to_item(node, struct perm_node, plist))->dp;
|
|
|
|
if (dp->prefix) {
|
|
|
|
if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
|
2008-10-21 22:00:00 +08:00
|
|
|
continue;
|
|
|
|
} else {
|
2010-10-27 06:09:43 +08:00
|
|
|
if (strcmp(upath, dp->name + 4))
|
2008-10-21 22:00:00 +08:00
|
|
|
continue;
|
|
|
|
}
|
2010-10-27 06:09:43 +08:00
|
|
|
|
|
|
|
if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
|
|
|
|
return;
|
|
|
|
|
|
|
|
sprintf(buf,"/sys%s/%s", upath, dp->attr);
|
|
|
|
INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
|
|
|
|
chown(buf, dp->uid, dp->gid);
|
|
|
|
chmod(buf, dp->perm);
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
|
|
|
|
{
|
|
|
|
mode_t perm;
|
2010-04-21 05:32:50 +08: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) {
|
|
|
|
perm_node = node_to_item(node, struct perm_node, plist);
|
|
|
|
dp = &perm_node->dp;
|
|
|
|
|
|
|
|
if (dp->prefix) {
|
|
|
|
if (strncmp(path, dp->name, strlen(dp->name)))
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (strcmp(path, dp->name))
|
|
|
|
continue;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
2010-04-21 05:32:50 +08:00
|
|
|
*uid = dp->uid;
|
|
|
|
*gid = dp->gid;
|
|
|
|
return dp->perm;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
2010-04-21 05:32:50 +08:00
|
|
|
/* Default if nothing found. */
|
|
|
|
*uid = 0;
|
|
|
|
*gid = 0;
|
|
|
|
return 0600;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2010-10-27 06:09:43 +08:00
|
|
|
static void make_device(const char *path,
|
|
|
|
const char *upath,
|
|
|
|
int block, int major, int minor)
|
2008-10-21 22:00:00 +08:00
|
|
|
{
|
|
|
|
unsigned uid;
|
|
|
|
unsigned gid;
|
|
|
|
mode_t mode;
|
|
|
|
dev_t dev;
|
|
|
|
|
|
|
|
mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
|
2010-09-04 03:25:34 +08:00
|
|
|
dev = makedev(major, minor);
|
2010-01-22 10:13:39 +08: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);
|
2008-10-21 22:00:00 +08:00
|
|
|
mknod(path, mode, dev);
|
2010-01-22 10:13:39 +08:00
|
|
|
chown(path, uid, -1);
|
|
|
|
setegid(AID_ROOT);
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2008-12-16 06:26:56 +08:00
|
|
|
#if LOG_UEVENTS
|
2008-10-21 22:00:00 +08:00
|
|
|
|
|
|
|
static inline suseconds_t get_usecs(void)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define log_event_print(x...) INFO(x)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define log_event_print(fmt, args...) do { } while (0)
|
|
|
|
#define get_usecs() 0
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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 07:16:20 +08:00
|
|
|
uevent->partition_name = NULL;
|
|
|
|
uevent->partition_num = -1;
|
2008-10-21 22:00:00 +08: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 07:16:20 +08: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;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* advance to after the next \0 */
|
|
|
|
while(*msg++)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
|
|
|
|
uevent->action, uevent->path, uevent->subsystem,
|
|
|
|
uevent->firmware, uevent->major, uevent->minor);
|
|
|
|
}
|
|
|
|
|
2010-08-04 05:36:02 +08:00
|
|
|
static char **get_character_device_symlinks(struct uevent *uevent)
|
|
|
|
{
|
|
|
|
const char *parent;
|
|
|
|
char *slash;
|
|
|
|
char **links;
|
|
|
|
int link_num = 0;
|
|
|
|
int width;
|
|
|
|
|
|
|
|
if (strncmp(uevent->path, "/devices/platform/", 18))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
links = malloc(sizeof(char *) * 2);
|
|
|
|
if (!links)
|
|
|
|
return NULL;
|
|
|
|
memset(links, 0, sizeof(char *) * 2);
|
|
|
|
|
|
|
|
/* skip "/devices/platform/<driver>" */
|
|
|
|
parent = strchr(uevent->path + 18, '/');
|
|
|
|
if (!*parent)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-04-09 07:16:20 +08:00
|
|
|
static char **parse_platform_block_device(struct uevent *uevent)
|
|
|
|
{
|
|
|
|
const char *driver;
|
|
|
|
const char *path;
|
|
|
|
char *slash;
|
|
|
|
int width;
|
|
|
|
char buf[256];
|
|
|
|
char link_path[256];
|
|
|
|
int fd;
|
|
|
|
int link_num = 0;
|
|
|
|
int ret;
|
|
|
|
char *p;
|
|
|
|
unsigned int size;
|
|
|
|
struct stat info;
|
|
|
|
|
|
|
|
char **links = malloc(sizeof(char *) * 4);
|
|
|
|
if (!links)
|
|
|
|
return NULL;
|
|
|
|
memset(links, 0, sizeof(char *) * 4);
|
|
|
|
|
|
|
|
/* Drop "/devices/platform/" */
|
|
|
|
path = uevent->path;
|
|
|
|
driver = path + 18;
|
|
|
|
slash = strchr(driver, '/');
|
|
|
|
if (!slash)
|
|
|
|
goto err;
|
|
|
|
width = slash - driver;
|
|
|
|
if (width <= 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
snprintf(link_path, sizeof(link_path), "/dev/block/platform/%.*s",
|
|
|
|
width, driver);
|
|
|
|
|
|
|
|
if (uevent->partition_name) {
|
|
|
|
p = strdup(uevent->partition_name);
|
|
|
|
sanitize(p);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
slash = strrchr(path, '/');
|
|
|
|
if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
|
|
|
|
link_num++;
|
|
|
|
else
|
|
|
|
links[link_num] = NULL;
|
|
|
|
|
|
|
|
return links;
|
|
|
|
|
|
|
|
err:
|
|
|
|
free(links);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-10-21 22:00:00 +08:00
|
|
|
static void handle_device_event(struct uevent *uevent)
|
|
|
|
{
|
|
|
|
char devpath[96];
|
2010-05-07 01:30:09 +08:00
|
|
|
int devpath_ready = 0;
|
2008-10-21 22:00:00 +08:00
|
|
|
char *base, *name;
|
2010-04-09 07:16:20 +08:00
|
|
|
char **links = NULL;
|
2008-10-21 22:00:00 +08:00
|
|
|
int block;
|
2010-04-09 07:16:20 +08:00
|
|
|
int i;
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2010-10-27 06:09:43 +08:00
|
|
|
if (!strcmp(uevent->action,"add"))
|
|
|
|
fixup_sys_perms(uevent->path);
|
|
|
|
|
|
|
|
/* if it's not a /dev device, nothing else to do */
|
2008-10-21 22:00:00 +08:00
|
|
|
if((uevent->major < 0) || (uevent->minor < 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* do we have a name? */
|
|
|
|
name = strrchr(uevent->path, '/');
|
|
|
|
if(!name)
|
|
|
|
return;
|
|
|
|
name++;
|
|
|
|
|
|
|
|
/* too-long names would overrun our buffer */
|
|
|
|
if(strlen(name) > 64)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* are we block or char? where should we live? */
|
2008-12-18 10:08:08 +08:00
|
|
|
if(!strncmp(uevent->subsystem, "block", 5)) {
|
2008-10-21 22:00:00 +08:00
|
|
|
block = 1;
|
|
|
|
base = "/dev/block/";
|
|
|
|
mkdir(base, 0755);
|
2010-04-09 07:16:20 +08:00
|
|
|
if (!strncmp(uevent->path, "/devices/platform/", 18))
|
|
|
|
links = parse_platform_block_device(uevent);
|
2008-10-21 22:00:00 +08:00
|
|
|
} else {
|
|
|
|
block = 0;
|
|
|
|
/* this should probably be configurable somehow */
|
2010-05-07 01:30:09 +08:00
|
|
|
if (!strncmp(uevent->subsystem, "usb", 3)) {
|
|
|
|
if (!strcmp(uevent->subsystem, "usb")) {
|
|
|
|
/* 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 */
|
|
|
|
mkdir("/dev/bus", 0755);
|
|
|
|
mkdir("/dev/bus/usb", 0755);
|
|
|
|
snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
|
|
|
|
mkdir(devpath, 0755);
|
|
|
|
snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
|
|
|
|
devpath_ready = 1;
|
|
|
|
} else {
|
|
|
|
/* ignore other USB events */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (!strncmp(uevent->subsystem, "graphics", 8)) {
|
2008-10-21 22:00:00 +08:00
|
|
|
base = "/dev/graphics/";
|
|
|
|
mkdir(base, 0755);
|
2008-12-18 10:08:08 +08:00
|
|
|
} else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
|
2008-10-21 22:00:00 +08:00
|
|
|
base = "/dev/oncrpc/";
|
|
|
|
mkdir(base, 0755);
|
2008-12-18 10:08:08 +08:00
|
|
|
} else if (!strncmp(uevent->subsystem, "adsp", 4)) {
|
2008-10-21 22:00:00 +08:00
|
|
|
base = "/dev/adsp/";
|
|
|
|
mkdir(base, 0755);
|
2009-05-02 01:25:05 +08:00
|
|
|
} else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
|
|
|
|
base = "/dev/msm_camera/";
|
|
|
|
mkdir(base, 0755);
|
|
|
|
} else if(!strncmp(uevent->subsystem, "input", 5)) {
|
2008-10-21 22:00:00 +08:00
|
|
|
base = "/dev/input/";
|
|
|
|
mkdir(base, 0755);
|
2008-12-18 10:08:08 +08:00
|
|
|
} else if(!strncmp(uevent->subsystem, "mtd", 3)) {
|
2008-10-21 22:00:00 +08:00
|
|
|
base = "/dev/mtd/";
|
|
|
|
mkdir(base, 0755);
|
2008-11-26 08:20:07 +08:00
|
|
|
} else if(!strncmp(uevent->subsystem, "sound", 5)) {
|
|
|
|
base = "/dev/snd/";
|
|
|
|
mkdir(base, 0755);
|
2008-12-18 10:08:08 +08:00
|
|
|
} else if(!strncmp(uevent->subsystem, "misc", 4) &&
|
2008-10-21 22:00:00 +08:00
|
|
|
!strncmp(name, "log_", 4)) {
|
|
|
|
base = "/dev/log/";
|
|
|
|
mkdir(base, 0755);
|
|
|
|
name += 4;
|
|
|
|
} else
|
|
|
|
base = "/dev/";
|
2010-08-04 05:36:02 +08:00
|
|
|
links = get_character_device_symlinks(uevent);
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
2010-05-07 01:30:09 +08:00
|
|
|
if (!devpath_ready)
|
|
|
|
snprintf(devpath, sizeof(devpath), "%s%s", base, name);
|
2008-10-21 22:00:00 +08:00
|
|
|
|
|
|
|
if(!strcmp(uevent->action, "add")) {
|
2010-10-27 06:09:43 +08:00
|
|
|
make_device(devpath, uevent->path, block, uevent->major, uevent->minor);
|
2010-04-09 07:16:20 +08:00
|
|
|
if (links) {
|
|
|
|
for (i = 0; links[i]; i++)
|
|
|
|
make_link(devpath, links[i]);
|
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!strcmp(uevent->action, "remove")) {
|
2010-04-09 07:16:20 +08:00
|
|
|
if (links) {
|
|
|
|
for (i = 0; links[i]; i++)
|
|
|
|
remove_link(devpath, links[i]);
|
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
unlink(devpath);
|
2010-04-09 07:16:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (links) {
|
|
|
|
for (i = 0; links[i]; i++)
|
|
|
|
free(links[i]);
|
|
|
|
free(links);
|
2008-10-21 22:00:00 +08: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
len_to_copy -= nr;
|
|
|
|
while (nr > 0) {
|
|
|
|
ssize_t nw = 0;
|
|
|
|
|
|
|
|
nw = write(data_fd, buf + nw, nr);
|
|
|
|
if(nw <= 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
nr -= nw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if(!ret)
|
|
|
|
write(loading_fd, "0", 1); /* successful end of transfer */
|
|
|
|
else
|
|
|
|
write(loading_fd, "-1", 2); /* abort transfer */
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void process_firmware_event(struct uevent *uevent)
|
|
|
|
{
|
2010-09-19 18:36:39 +08:00
|
|
|
char *root, *loading, *data, *file1 = NULL, *file2 = NULL;
|
2008-10-21 22:00:00 +08:00
|
|
|
int l, loading_fd, data_fd, fw_fd;
|
|
|
|
|
|
|
|
log_event_print("firmware event { '%s', '%s' }\n",
|
|
|
|
uevent->path, uevent->firmware);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2010-09-19 18:36:39 +08:00
|
|
|
l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
|
|
|
|
if (l == -1)
|
|
|
|
goto data_free_out;
|
|
|
|
|
|
|
|
l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
|
2008-10-21 22:00:00 +08:00
|
|
|
if (l == -1)
|
|
|
|
goto data_free_out;
|
|
|
|
|
|
|
|
loading_fd = open(loading, O_WRONLY);
|
|
|
|
if(loading_fd < 0)
|
|
|
|
goto file_free_out;
|
|
|
|
|
|
|
|
data_fd = open(data, O_WRONLY);
|
|
|
|
if(data_fd < 0)
|
|
|
|
goto loading_close_out;
|
|
|
|
|
2010-09-19 18:36:39 +08:00
|
|
|
fw_fd = open(file1, O_RDONLY);
|
|
|
|
if(fw_fd < 0) {
|
|
|
|
fw_fd = open(file2, O_RDONLY);
|
2010-11-10 10:10:24 +08:00
|
|
|
if (fw_fd < 0) {
|
|
|
|
write(loading_fd, "-1", 2);
|
2010-09-19 18:36:39 +08:00
|
|
|
goto data_close_out;
|
2010-11-10 10:10:24 +08:00
|
|
|
}
|
2010-09-19 18:36:39 +08:00
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
|
|
|
|
if(!load_firmware(fw_fd, loading_fd, data_fd))
|
2010-09-19 18:36:39 +08:00
|
|
|
log_event_print("firmware copy success { '%s', '%s' }\n", root, uevent->firmware);
|
2008-10-21 22:00:00 +08:00
|
|
|
else
|
2010-09-19 18:36:39 +08:00
|
|
|
log_event_print("firmware copy failure { '%s', '%s' }\n", root, uevent->firmware);
|
2008-10-21 22:00:00 +08:00
|
|
|
|
|
|
|
close(fw_fd);
|
|
|
|
data_close_out:
|
|
|
|
close(data_fd);
|
|
|
|
loading_close_out:
|
|
|
|
close(loading_fd);
|
|
|
|
file_free_out:
|
2010-09-19 18:36:39 +08:00
|
|
|
free(file1);
|
|
|
|
free(file2);
|
2008-10-21 22:00:00 +08:00
|
|
|
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;
|
2010-06-04 03:21:01 +08:00
|
|
|
int status;
|
|
|
|
int ret;
|
2008-10-21 22:00:00 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
exit(EXIT_SUCCESS);
|
2010-06-04 03:21:01 +08:00
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
ret = waitpid(pid, &status, 0);
|
|
|
|
} while (ret == -1 && errno == EINTR);
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UEVENT_MSG_LEN 1024
|
2010-04-14 10:25:51 +08:00
|
|
|
void handle_device_fd()
|
2008-10-21 22:00:00 +08:00
|
|
|
{
|
2010-07-20 05:31:20 +08:00
|
|
|
for(;;) {
|
|
|
|
char msg[UEVENT_MSG_LEN+2];
|
|
|
|
char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
|
|
|
|
struct iovec iov = {msg, sizeof(msg)};
|
|
|
|
struct sockaddr_nl snl;
|
|
|
|
struct msghdr hdr = {&snl, sizeof(snl), &iov, 1, cred_msg, sizeof(cred_msg), 0};
|
|
|
|
|
2010-07-20 06:53:05 +08:00
|
|
|
ssize_t n = recvmsg(device_fd, &hdr, 0);
|
2010-07-20 05:31:20 +08:00
|
|
|
if (n <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2010-07-20 05:31:20 +08:00
|
|
|
if ((snl.nl_groups != 1) || (snl.nl_pid != 0)) {
|
|
|
|
/* ignoring non-kernel netlink multicast message */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cmsghdr * cmsg = CMSG_FIRSTHDR(&hdr);
|
|
|
|
if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
|
|
|
|
/* no sender credentials received, ignore message */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ucred * cred = (struct ucred *)CMSG_DATA(cmsg);
|
|
|
|
if (cred->uid != 0) {
|
|
|
|
/* message from non-root user, ignore */
|
|
|
|
continue;
|
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2010-07-20 05:31:20 +08:00
|
|
|
if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
|
2008-10-21 22:00:00 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
msg[n] = '\0';
|
|
|
|
msg[n+1] = '\0';
|
|
|
|
|
2010-07-20 05:31:20 +08:00
|
|
|
struct uevent uevent;
|
2008-10-21 22:00:00 +08:00
|
|
|
parse_event(msg, &uevent);
|
|
|
|
|
|
|
|
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
|
|
|
|
** socket's buffer.
|
|
|
|
*/
|
|
|
|
|
2010-04-14 10:25:51 +08:00
|
|
|
static void do_coldboot(DIR *d)
|
2008-10-21 22:00:00 +08: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 10:25:51 +08:00
|
|
|
handle_device_fd();
|
2008-10-21 22:00:00 +08: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 10:25:51 +08:00
|
|
|
do_coldboot(d2);
|
2008-10-21 22:00:00 +08:00
|
|
|
closedir(d2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-14 10:25:51 +08:00
|
|
|
static void coldboot(const char *path)
|
2008-10-21 22:00:00 +08:00
|
|
|
{
|
|
|
|
DIR *d = opendir(path);
|
|
|
|
if(d) {
|
2010-04-14 10:25:51 +08:00
|
|
|
do_coldboot(d);
|
2008-10-21 22:00:00 +08:00
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-14 10:25:51 +08:00
|
|
|
void device_init(void)
|
2008-10-21 22:00:00 +08:00
|
|
|
{
|
|
|
|
suseconds_t t0, t1;
|
2010-04-22 03:04:20 +08:00
|
|
|
struct stat info;
|
|
|
|
int fd;
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2010-04-14 10:25:51 +08:00
|
|
|
device_fd = open_uevent_socket();
|
|
|
|
if(device_fd < 0)
|
|
|
|
return;
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2010-04-14 10:25:51 +08:00
|
|
|
fcntl(device_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
fcntl(device_fd, F_SETFL, O_NONBLOCK);
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2010-04-22 03:04:20 +08:00
|
|
|
if (stat(coldboot_done, &info) < 0) {
|
|
|
|
t0 = get_usecs();
|
|
|
|
coldboot("/sys/class");
|
|
|
|
coldboot("/sys/block");
|
|
|
|
coldboot("/sys/devices");
|
|
|
|
t1 = get_usecs();
|
|
|
|
fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
|
|
|
|
close(fd);
|
|
|
|
log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
|
|
|
|
} else {
|
|
|
|
log_event_print("skipping coldboot, already done\n");
|
|
|
|
}
|
2010-04-14 10:25:51 +08:00
|
|
|
}
|
2008-10-21 22:00:00 +08:00
|
|
|
|
2010-04-14 10:25:51 +08:00
|
|
|
int get_device_fd()
|
|
|
|
{
|
|
|
|
return device_fd;
|
2008-10-21 22:00:00 +08:00
|
|
|
}
|