The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 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 | |
| 17 | #include <errno.h> |
Olivier Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 18 | #include <stddef.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | |
| 24 | #include <fcntl.h> |
| 25 | #include <dirent.h> |
| 26 | #include <unistd.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include <sys/socket.h> |
| 30 | #include <sys/un.h> |
| 31 | #include <linux/netlink.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 32 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 33 | #include <selinux/selinux.h> |
| 34 | #include <selinux/label.h> |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 35 | #include <selinux/android.h> |
Stephen Smalley | e2eb69d | 2013-04-16 09:30:30 -0400 | [diff] [blame] | 36 | #include <selinux/avc.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 37 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
| 39 | #include <sys/time.h> |
| 40 | #include <asm/page.h> |
Colin Cross | 982a815 | 2010-06-03 12:21:01 -0700 | [diff] [blame] | 41 | #include <sys/wait.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | |
Dima Zavin | da04c52 | 2011-09-01 17:09:44 -0700 | [diff] [blame] | 43 | #include <cutils/list.h> |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 44 | #include <cutils/uevent.h> |
| 45 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | #include "devices.h" |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 47 | #include "util.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 48 | #include "log.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | #define SYSFS_PREFIX "/sys" |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 51 | #define FIRMWARE_DIR1 "/etc/firmware" |
| 52 | #define FIRMWARE_DIR2 "/vendor/firmware" |
Iliyan Malchev | 029d44e | 2012-06-08 10:42:26 -0700 | [diff] [blame] | 53 | #define FIRMWARE_DIR3 "/firmware/image" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 55 | extern struct selabel_handle *sehandle; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 56 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 57 | static int device_fd = -1; |
| 58 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 59 | struct uevent { |
| 60 | const char *action; |
| 61 | const char *path; |
| 62 | const char *subsystem; |
| 63 | const char *firmware; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 64 | const char *partition_name; |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 65 | const char *device_name; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 66 | int partition_num; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 67 | int major; |
| 68 | int minor; |
| 69 | }; |
| 70 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 71 | struct perms_ { |
| 72 | char *name; |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 73 | char *attr; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | mode_t perm; |
| 75 | unsigned int uid; |
| 76 | unsigned int gid; |
| 77 | unsigned short prefix; |
| 78 | }; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 79 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 80 | struct perm_node { |
| 81 | struct perms_ dp; |
| 82 | struct listnode plist; |
| 83 | }; |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 84 | |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 85 | struct platform_node { |
| 86 | char *name; |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 87 | char *path; |
| 88 | int path_len; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 89 | struct listnode list; |
| 90 | }; |
| 91 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 92 | static list_declare(sys_perms); |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 93 | static list_declare(dev_perms); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 94 | static list_declare(platform_names); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 96 | int add_dev_perms(const char *name, const char *attr, |
| 97 | mode_t perm, unsigned int uid, unsigned int gid, |
| 98 | unsigned short prefix) { |
| 99 | struct perm_node *node = calloc(1, sizeof(*node)); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | if (!node) |
| 101 | return -ENOMEM; |
| 102 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 103 | node->dp.name = strdup(name); |
| 104 | if (!node->dp.name) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | return -ENOMEM; |
| 106 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 107 | if (attr) { |
| 108 | node->dp.attr = strdup(attr); |
| 109 | if (!node->dp.attr) |
| 110 | return -ENOMEM; |
| 111 | } |
| 112 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 113 | node->dp.perm = perm; |
| 114 | node->dp.uid = uid; |
| 115 | node->dp.gid = gid; |
| 116 | node->dp.prefix = prefix; |
| 117 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 118 | if (attr) |
| 119 | list_add_tail(&sys_perms, &node->plist); |
| 120 | else |
| 121 | list_add_tail(&dev_perms, &node->plist); |
| 122 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 126 | void fixup_sys_perms(const char *upath) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | { |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 128 | char buf[512]; |
| 129 | struct listnode *node; |
| 130 | struct perms_ *dp; |
Stephen Smalley | 5f7b017 | 2013-04-03 13:15:01 -0400 | [diff] [blame] | 131 | char *secontext; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 133 | /* upaths omit the "/sys" that paths in this list |
| 134 | * contain, so we add 4 when comparing... |
| 135 | */ |
| 136 | list_for_each(node, &sys_perms) { |
| 137 | dp = &(node_to_item(node, struct perm_node, plist))->dp; |
| 138 | if (dp->prefix) { |
| 139 | if (strncmp(upath, dp->name + 4, strlen(dp->name + 4))) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 140 | continue; |
| 141 | } else { |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 142 | if (strcmp(upath, dp->name + 4)) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 143 | continue; |
| 144 | } |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 145 | |
| 146 | if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf)) |
| 147 | return; |
| 148 | |
| 149 | sprintf(buf,"/sys%s/%s", upath, dp->attr); |
| 150 | INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm); |
| 151 | chown(buf, dp->uid, dp->gid); |
| 152 | chmod(buf, dp->perm); |
Stephen Smalley | 5f7b017 | 2013-04-03 13:15:01 -0400 | [diff] [blame] | 153 | if (sehandle) { |
| 154 | secontext = NULL; |
| 155 | selabel_lookup(sehandle, &secontext, buf, 0); |
| 156 | if (secontext) { |
| 157 | setfilecon(buf, secontext); |
| 158 | freecon(secontext); |
| 159 | } |
| 160 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 161 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | } |
| 163 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 164 | static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid) |
| 165 | { |
| 166 | mode_t perm; |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 167 | struct listnode *node; |
| 168 | struct perm_node *perm_node; |
| 169 | struct perms_ *dp; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 170 | |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 171 | /* search the perms list in reverse so that ueventd.$hardware can |
| 172 | * override ueventd.rc |
| 173 | */ |
| 174 | list_for_each_reverse(node, &dev_perms) { |
| 175 | perm_node = node_to_item(node, struct perm_node, plist); |
| 176 | dp = &perm_node->dp; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 178 | if (dp->prefix) { |
| 179 | if (strncmp(path, dp->name, strlen(dp->name))) |
| 180 | continue; |
| 181 | } else { |
| 182 | if (strcmp(path, dp->name)) |
| 183 | continue; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | } |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 185 | *uid = dp->uid; |
| 186 | *gid = dp->gid; |
| 187 | return dp->perm; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 188 | } |
Colin Cross | 44b65d0 | 2010-04-20 14:32:50 -0700 | [diff] [blame] | 189 | /* Default if nothing found. */ |
| 190 | *uid = 0; |
| 191 | *gid = 0; |
| 192 | return 0600; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Brian Swetland | bc57d4c | 2010-10-26 15:09:43 -0700 | [diff] [blame] | 195 | static void make_device(const char *path, |
| 196 | const char *upath, |
| 197 | int block, int major, int minor) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 198 | { |
| 199 | unsigned uid; |
| 200 | unsigned gid; |
| 201 | mode_t mode; |
| 202 | dev_t dev; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 203 | char *secontext = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 205 | mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR); |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 206 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 207 | if (sehandle) { |
| 208 | selabel_lookup(sehandle, &secontext, path, mode); |
| 209 | setfscreatecon(secontext); |
| 210 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 211 | |
Colin Cross | 17dcc5c | 2010-09-03 12:25:34 -0700 | [diff] [blame] | 212 | dev = makedev(major, minor); |
Nick Pelly | 6405c69 | 2010-01-21 18:13:39 -0800 | [diff] [blame] | 213 | /* Temporarily change egid to avoid race condition setting the gid of the |
| 214 | * device node. Unforunately changing the euid would prevent creation of |
| 215 | * some device nodes, so the uid has to be set with chown() and is still |
| 216 | * racy. Fixing the gid race at least fixed the issue with system_server |
| 217 | * opening dynamic input devices under the AID_INPUT gid. */ |
| 218 | setegid(gid); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 219 | mknod(path, mode, dev); |
Nick Pelly | 6405c69 | 2010-01-21 18:13:39 -0800 | [diff] [blame] | 220 | chown(path, uid, -1); |
| 221 | setegid(AID_ROOT); |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 222 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 223 | if (secontext) { |
| 224 | freecon(secontext); |
| 225 | setfscreatecon(NULL); |
| 226 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 229 | static void add_platform_device(const char *path) |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 230 | { |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 231 | int path_len = strlen(path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 232 | struct listnode *node; |
| 233 | struct platform_node *bus; |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 234 | const char *name = path; |
| 235 | |
| 236 | if (!strncmp(path, "/devices/", 9)) { |
| 237 | name += 9; |
| 238 | if (!strncmp(name, "platform/", 9)) |
| 239 | name += 9; |
| 240 | } |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 241 | |
| 242 | list_for_each_reverse(node, &platform_names) { |
| 243 | bus = node_to_item(node, struct platform_node, list); |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 244 | if ((bus->path_len < path_len) && |
| 245 | (path[bus->path_len] == '/') && |
| 246 | !strncmp(path, bus->path, bus->path_len)) |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 247 | /* subdevice of an existing platform, ignore it */ |
| 248 | return; |
| 249 | } |
| 250 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 251 | INFO("adding platform device %s (%s)\n", name, path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 252 | |
| 253 | bus = calloc(1, sizeof(struct platform_node)); |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 254 | bus->path = strdup(path); |
| 255 | bus->path_len = path_len; |
| 256 | bus->name = bus->path + (name - path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 257 | list_add_tail(&platform_names, &bus->list); |
| 258 | } |
| 259 | |
| 260 | /* |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 261 | * given a path that may start with a platform device, find the length of the |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 262 | * platform device prefix. If it doesn't start with a platform device, return |
| 263 | * 0. |
| 264 | */ |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 265 | static struct platform_node *find_platform_device(const char *path) |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 266 | { |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 267 | int path_len = strlen(path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 268 | struct listnode *node; |
| 269 | struct platform_node *bus; |
| 270 | |
| 271 | list_for_each_reverse(node, &platform_names) { |
| 272 | bus = node_to_item(node, struct platform_node, list); |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 273 | if ((bus->path_len < path_len) && |
| 274 | (path[bus->path_len] == '/') && |
| 275 | !strncmp(path, bus->path, bus->path_len)) |
| 276 | return bus; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | return NULL; |
| 280 | } |
| 281 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 282 | static void remove_platform_device(const char *path) |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 283 | { |
| 284 | struct listnode *node; |
| 285 | struct platform_node *bus; |
| 286 | |
| 287 | list_for_each_reverse(node, &platform_names) { |
| 288 | bus = node_to_item(node, struct platform_node, list); |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 289 | if (!strcmp(path, bus->path)) { |
| 290 | INFO("removing platform device %s\n", bus->name); |
| 291 | free(bus->path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 292 | list_remove(node); |
| 293 | free(bus); |
| 294 | return; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
Chuck Tuffli | 1e07084 | 2008-12-15 14:26:56 -0800 | [diff] [blame] | 299 | #if LOG_UEVENTS |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 300 | |
| 301 | static inline suseconds_t get_usecs(void) |
| 302 | { |
| 303 | struct timeval tv; |
| 304 | gettimeofday(&tv, 0); |
| 305 | return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec; |
| 306 | } |
| 307 | |
| 308 | #define log_event_print(x...) INFO(x) |
| 309 | |
| 310 | #else |
| 311 | |
| 312 | #define log_event_print(fmt, args...) do { } while (0) |
| 313 | #define get_usecs() 0 |
| 314 | |
| 315 | #endif |
| 316 | |
| 317 | static void parse_event(const char *msg, struct uevent *uevent) |
| 318 | { |
| 319 | uevent->action = ""; |
| 320 | uevent->path = ""; |
| 321 | uevent->subsystem = ""; |
| 322 | uevent->firmware = ""; |
| 323 | uevent->major = -1; |
| 324 | uevent->minor = -1; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 325 | uevent->partition_name = NULL; |
| 326 | uevent->partition_num = -1; |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 327 | uevent->device_name = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 328 | |
| 329 | /* currently ignoring SEQNUM */ |
| 330 | while(*msg) { |
| 331 | if(!strncmp(msg, "ACTION=", 7)) { |
| 332 | msg += 7; |
| 333 | uevent->action = msg; |
| 334 | } else if(!strncmp(msg, "DEVPATH=", 8)) { |
| 335 | msg += 8; |
| 336 | uevent->path = msg; |
| 337 | } else if(!strncmp(msg, "SUBSYSTEM=", 10)) { |
| 338 | msg += 10; |
| 339 | uevent->subsystem = msg; |
| 340 | } else if(!strncmp(msg, "FIRMWARE=", 9)) { |
| 341 | msg += 9; |
| 342 | uevent->firmware = msg; |
| 343 | } else if(!strncmp(msg, "MAJOR=", 6)) { |
| 344 | msg += 6; |
| 345 | uevent->major = atoi(msg); |
| 346 | } else if(!strncmp(msg, "MINOR=", 6)) { |
| 347 | msg += 6; |
| 348 | uevent->minor = atoi(msg); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 349 | } else if(!strncmp(msg, "PARTN=", 6)) { |
| 350 | msg += 6; |
| 351 | uevent->partition_num = atoi(msg); |
| 352 | } else if(!strncmp(msg, "PARTNAME=", 9)) { |
| 353 | msg += 9; |
| 354 | uevent->partition_name = msg; |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 355 | } else if(!strncmp(msg, "DEVNAME=", 8)) { |
| 356 | msg += 8; |
| 357 | uevent->device_name = msg; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 360 | /* advance to after the next \0 */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 361 | while(*msg++) |
| 362 | ; |
| 363 | } |
| 364 | |
| 365 | log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n", |
| 366 | uevent->action, uevent->path, uevent->subsystem, |
| 367 | uevent->firmware, uevent->major, uevent->minor); |
| 368 | } |
| 369 | |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 370 | static char **get_character_device_symlinks(struct uevent *uevent) |
| 371 | { |
| 372 | const char *parent; |
| 373 | char *slash; |
| 374 | char **links; |
| 375 | int link_num = 0; |
| 376 | int width; |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 377 | struct platform_node *pdev; |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 378 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 379 | pdev = find_platform_device(uevent->path); |
| 380 | if (!pdev) |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 381 | return NULL; |
| 382 | |
| 383 | links = malloc(sizeof(char *) * 2); |
| 384 | if (!links) |
| 385 | return NULL; |
| 386 | memset(links, 0, sizeof(char *) * 2); |
| 387 | |
| 388 | /* skip "/devices/platform/<driver>" */ |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 389 | parent = strchr(uevent->path + pdev->path_len, '/'); |
Benoit Goby | d227863 | 2010-08-03 14:36:02 -0700 | [diff] [blame] | 390 | if (!*parent) |
| 391 | goto err; |
| 392 | |
| 393 | if (!strncmp(parent, "/usb", 4)) { |
| 394 | /* skip root hub name and device. use device interface */ |
| 395 | while (*++parent && *parent != '/'); |
| 396 | if (*parent) |
| 397 | while (*++parent && *parent != '/'); |
| 398 | if (!*parent) |
| 399 | goto err; |
| 400 | slash = strchr(++parent, '/'); |
| 401 | if (!slash) |
| 402 | goto err; |
| 403 | width = slash - parent; |
| 404 | if (width <= 0) |
| 405 | goto err; |
| 406 | |
| 407 | if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0) |
| 408 | link_num++; |
| 409 | else |
| 410 | links[link_num] = NULL; |
| 411 | mkdir("/dev/usb", 0755); |
| 412 | } |
| 413 | else { |
| 414 | goto err; |
| 415 | } |
| 416 | |
| 417 | return links; |
| 418 | err: |
| 419 | free(links); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 423 | static char **parse_platform_block_device(struct uevent *uevent) |
| 424 | { |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 425 | const char *device; |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 426 | struct platform_node *pdev; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 427 | char *slash; |
| 428 | int width; |
| 429 | char buf[256]; |
| 430 | char link_path[256]; |
| 431 | int fd; |
| 432 | int link_num = 0; |
| 433 | int ret; |
| 434 | char *p; |
| 435 | unsigned int size; |
| 436 | struct stat info; |
| 437 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 438 | pdev = find_platform_device(uevent->path); |
| 439 | if (!pdev) |
| 440 | return NULL; |
| 441 | device = pdev->name; |
| 442 | |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 443 | char **links = malloc(sizeof(char *) * 4); |
| 444 | if (!links) |
| 445 | return NULL; |
| 446 | memset(links, 0, sizeof(char *) * 4); |
| 447 | |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 448 | INFO("found platform device %s\n", device); |
| 449 | |
| 450 | snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 451 | |
| 452 | if (uevent->partition_name) { |
| 453 | p = strdup(uevent->partition_name); |
| 454 | sanitize(p); |
Johan Redestig | 93ca79b | 2012-04-18 16:41:19 +0200 | [diff] [blame] | 455 | if (strcmp(uevent->partition_name, p)) |
| 456 | NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 457 | if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0) |
| 458 | link_num++; |
| 459 | else |
| 460 | links[link_num] = NULL; |
| 461 | free(p); |
| 462 | } |
| 463 | |
| 464 | if (uevent->partition_num >= 0) { |
| 465 | if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0) |
| 466 | link_num++; |
| 467 | else |
| 468 | links[link_num] = NULL; |
| 469 | } |
| 470 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 471 | slash = strrchr(uevent->path, '/'); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 472 | if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0) |
| 473 | link_num++; |
| 474 | else |
| 475 | links[link_num] = NULL; |
| 476 | |
| 477 | return links; |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 480 | static void handle_device(const char *action, const char *devpath, |
| 481 | const char *path, int block, int major, int minor, char **links) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 482 | { |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 483 | int i; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 484 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 485 | if(!strcmp(action, "add")) { |
| 486 | make_device(devpath, path, block, major, minor); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 487 | if (links) { |
| 488 | for (i = 0; links[i]; i++) |
| 489 | make_link(devpath, links[i]); |
| 490 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 493 | if(!strcmp(action, "remove")) { |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 494 | if (links) { |
| 495 | for (i = 0; links[i]; i++) |
| 496 | remove_link(devpath, links[i]); |
| 497 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 498 | unlink(devpath); |
Colin Cross | b0ab94b | 2010-04-08 16:16:20 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | if (links) { |
| 502 | for (i = 0; links[i]; i++) |
| 503 | free(links[i]); |
| 504 | free(links); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 508 | static void handle_platform_device_event(struct uevent *uevent) |
| 509 | { |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 510 | const char *path = uevent->path; |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 511 | |
| 512 | if (!strcmp(uevent->action, "add")) |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 513 | add_platform_device(path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 514 | else if (!strcmp(uevent->action, "remove")) |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 515 | remove_platform_device(path); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 518 | static const char *parse_device_name(struct uevent *uevent, unsigned int len) |
| 519 | { |
| 520 | const char *name; |
| 521 | |
| 522 | /* if it's not a /dev device, nothing else to do */ |
| 523 | if((uevent->major < 0) || (uevent->minor < 0)) |
| 524 | return NULL; |
| 525 | |
| 526 | /* do we have a name? */ |
| 527 | name = strrchr(uevent->path, '/'); |
| 528 | if(!name) |
| 529 | return NULL; |
| 530 | name++; |
| 531 | |
| 532 | /* too-long names would overrun our buffer */ |
Greg Hackmann | f6e009e | 2013-11-21 13:26:48 -0800 | [diff] [blame^] | 533 | if(strlen(name) > len) { |
| 534 | ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n", |
| 535 | name, len); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 536 | return NULL; |
Greg Hackmann | f6e009e | 2013-11-21 13:26:48 -0800 | [diff] [blame^] | 537 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 538 | |
| 539 | return name; |
| 540 | } |
| 541 | |
| 542 | static void handle_block_device_event(struct uevent *uevent) |
| 543 | { |
| 544 | const char *base = "/dev/block/"; |
| 545 | const char *name; |
| 546 | char devpath[96]; |
| 547 | char **links = NULL; |
| 548 | |
| 549 | name = parse_device_name(uevent, 64); |
| 550 | if (!name) |
| 551 | return; |
| 552 | |
| 553 | snprintf(devpath, sizeof(devpath), "%s%s", base, name); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 554 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 555 | |
Dima Zavin | f395c92 | 2013-03-06 16:23:57 -0800 | [diff] [blame] | 556 | if (!strncmp(uevent->path, "/devices/", 9)) |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 557 | links = parse_platform_block_device(uevent); |
| 558 | |
| 559 | handle_device(uevent->action, devpath, uevent->path, 1, |
| 560 | uevent->major, uevent->minor, links); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static void handle_generic_device_event(struct uevent *uevent) |
| 564 | { |
| 565 | char *base; |
| 566 | const char *name; |
| 567 | char devpath[96] = {0}; |
| 568 | char **links = NULL; |
| 569 | |
| 570 | name = parse_device_name(uevent, 64); |
| 571 | if (!name) |
| 572 | return; |
| 573 | |
| 574 | if (!strncmp(uevent->subsystem, "usb", 3)) { |
| 575 | if (!strcmp(uevent->subsystem, "usb")) { |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 576 | if (uevent->device_name) { |
| 577 | /* |
| 578 | * create device node provided by kernel if present |
| 579 | * see drivers/base/core.c |
| 580 | */ |
| 581 | char *p = devpath; |
Greg Hackmann | f6e009e | 2013-11-21 13:26:48 -0800 | [diff] [blame^] | 582 | int s = snprintf(devpath, sizeof(devpath), "/dev/%s", |
| 583 | uevent->device_name); |
| 584 | if (s < 0) { |
| 585 | ERROR("failed to assemble device path (%s); ignoring event\n", |
| 586 | strerror(errno)); |
| 587 | return; |
| 588 | } else if ((size_t)s >= sizeof(devpath)) { |
| 589 | ERROR("/dev/%s exceeds %u-character limit on path; ignoring event\n", |
| 590 | uevent->device_name, sizeof(devpath)); |
| 591 | return; |
| 592 | } |
| 593 | |
Wei Zhong | f97b887 | 2012-03-23 14:15:34 -0700 | [diff] [blame] | 594 | /* skip leading /dev/ */ |
| 595 | p += 5; |
| 596 | /* build directories */ |
| 597 | while (*p) { |
| 598 | if (*p == '/') { |
| 599 | *p = 0; |
| 600 | make_dir(devpath, 0755); |
| 601 | *p = '/'; |
| 602 | } |
| 603 | p++; |
| 604 | } |
| 605 | } |
| 606 | else { |
| 607 | /* This imitates the file system that would be created |
| 608 | * if we were using devfs instead. |
| 609 | * Minors are broken up into groups of 128, starting at "001" |
| 610 | */ |
| 611 | int bus_id = uevent->minor / 128 + 1; |
| 612 | int device_id = uevent->minor % 128 + 1; |
| 613 | /* build directories */ |
| 614 | make_dir("/dev/bus", 0755); |
| 615 | make_dir("/dev/bus/usb", 0755); |
| 616 | snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id); |
| 617 | make_dir(devpath, 0755); |
| 618 | snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id); |
| 619 | } |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 620 | } else { |
| 621 | /* ignore other USB events */ |
| 622 | return; |
| 623 | } |
| 624 | } else if (!strncmp(uevent->subsystem, "graphics", 8)) { |
| 625 | base = "/dev/graphics/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 626 | make_dir(base, 0755); |
Lukasz Anaczkowski | e6f8d45 | 2011-09-28 15:30:07 +0200 | [diff] [blame] | 627 | } else if (!strncmp(uevent->subsystem, "drm", 3)) { |
| 628 | base = "/dev/dri/"; |
| 629 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 630 | } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) { |
| 631 | base = "/dev/oncrpc/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 632 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 633 | } else if (!strncmp(uevent->subsystem, "adsp", 4)) { |
| 634 | base = "/dev/adsp/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 635 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 636 | } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) { |
| 637 | base = "/dev/msm_camera/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 638 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 639 | } else if(!strncmp(uevent->subsystem, "input", 5)) { |
| 640 | base = "/dev/input/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 641 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 642 | } else if(!strncmp(uevent->subsystem, "mtd", 3)) { |
| 643 | base = "/dev/mtd/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 644 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 645 | } else if(!strncmp(uevent->subsystem, "sound", 5)) { |
| 646 | base = "/dev/snd/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 647 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 648 | } else if(!strncmp(uevent->subsystem, "misc", 4) && |
| 649 | !strncmp(name, "log_", 4)) { |
| 650 | base = "/dev/log/"; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 651 | make_dir(base, 0755); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 652 | name += 4; |
| 653 | } else |
| 654 | base = "/dev/"; |
| 655 | links = get_character_device_symlinks(uevent); |
| 656 | |
| 657 | if (!devpath[0]) |
| 658 | snprintf(devpath, sizeof(devpath), "%s%s", base, name); |
| 659 | |
| 660 | handle_device(uevent->action, devpath, uevent->path, 0, |
| 661 | uevent->major, uevent->minor, links); |
| 662 | } |
| 663 | |
| 664 | static void handle_device_event(struct uevent *uevent) |
| 665 | { |
Colin Cross | 308bc52 | 2012-07-23 16:31:28 -0700 | [diff] [blame] | 666 | if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change")) |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 667 | fixup_sys_perms(uevent->path); |
| 668 | |
| 669 | if (!strncmp(uevent->subsystem, "block", 5)) { |
| 670 | handle_block_device_event(uevent); |
Colin Cross | fadb85e | 2011-03-30 18:32:12 -0700 | [diff] [blame] | 671 | } else if (!strncmp(uevent->subsystem, "platform", 8)) { |
| 672 | handle_platform_device_event(uevent); |
Colin Cross | eb5ba83 | 2011-03-30 17:37:17 -0700 | [diff] [blame] | 673 | } else { |
| 674 | handle_generic_device_event(uevent); |
| 675 | } |
| 676 | } |
| 677 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 678 | static int load_firmware(int fw_fd, int loading_fd, int data_fd) |
| 679 | { |
| 680 | struct stat st; |
| 681 | long len_to_copy; |
| 682 | int ret = 0; |
| 683 | |
| 684 | if(fstat(fw_fd, &st) < 0) |
| 685 | return -1; |
| 686 | len_to_copy = st.st_size; |
| 687 | |
| 688 | write(loading_fd, "1", 1); /* start transfer */ |
| 689 | |
| 690 | while (len_to_copy > 0) { |
| 691 | char buf[PAGE_SIZE]; |
| 692 | ssize_t nr; |
| 693 | |
| 694 | nr = read(fw_fd, buf, sizeof(buf)); |
| 695 | if(!nr) |
| 696 | break; |
| 697 | if(nr < 0) { |
| 698 | ret = -1; |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | len_to_copy -= nr; |
| 703 | while (nr > 0) { |
| 704 | ssize_t nw = 0; |
| 705 | |
| 706 | nw = write(data_fd, buf + nw, nr); |
| 707 | if(nw <= 0) { |
| 708 | ret = -1; |
| 709 | goto out; |
| 710 | } |
| 711 | nr -= nw; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | out: |
| 716 | if(!ret) |
| 717 | write(loading_fd, "0", 1); /* successful end of transfer */ |
| 718 | else |
| 719 | write(loading_fd, "-1", 2); /* abort transfer */ |
| 720 | |
| 721 | return ret; |
| 722 | } |
| 723 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 724 | static int is_booting(void) |
| 725 | { |
| 726 | return access("/dev/.booting", F_OK) == 0; |
| 727 | } |
| 728 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 729 | static void process_firmware_event(struct uevent *uevent) |
| 730 | { |
Iliyan Malchev | 029d44e | 2012-06-08 10:42:26 -0700 | [diff] [blame] | 731 | char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 732 | int l, loading_fd, data_fd, fw_fd; |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 733 | int booting = is_booting(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 734 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 735 | INFO("firmware: loading '%s' for '%s'\n", |
| 736 | uevent->firmware, uevent->path); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 737 | |
| 738 | l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path); |
| 739 | if (l == -1) |
| 740 | return; |
| 741 | |
| 742 | l = asprintf(&loading, "%sloading", root); |
| 743 | if (l == -1) |
| 744 | goto root_free_out; |
| 745 | |
| 746 | l = asprintf(&data, "%sdata", root); |
| 747 | if (l == -1) |
| 748 | goto loading_free_out; |
| 749 | |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 750 | l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware); |
| 751 | if (l == -1) |
| 752 | goto data_free_out; |
| 753 | |
| 754 | l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 755 | if (l == -1) |
| 756 | goto data_free_out; |
| 757 | |
Iliyan Malchev | 029d44e | 2012-06-08 10:42:26 -0700 | [diff] [blame] | 758 | l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware); |
| 759 | if (l == -1) |
| 760 | goto data_free_out; |
| 761 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 762 | loading_fd = open(loading, O_WRONLY); |
| 763 | if(loading_fd < 0) |
| 764 | goto file_free_out; |
| 765 | |
| 766 | data_fd = open(data, O_WRONLY); |
| 767 | if(data_fd < 0) |
| 768 | goto loading_close_out; |
| 769 | |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 770 | try_loading_again: |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 771 | fw_fd = open(file1, O_RDONLY); |
| 772 | if(fw_fd < 0) { |
| 773 | fw_fd = open(file2, O_RDONLY); |
Benoit Goby | 609d882 | 2010-11-09 18:10:24 -0800 | [diff] [blame] | 774 | if (fw_fd < 0) { |
Iliyan Malchev | 029d44e | 2012-06-08 10:42:26 -0700 | [diff] [blame] | 775 | fw_fd = open(file3, O_RDONLY); |
| 776 | if (fw_fd < 0) { |
| 777 | if (booting) { |
| 778 | /* If we're not fully booted, we may be missing |
| 779 | * filesystems needed for firmware, wait and retry. |
| 780 | */ |
| 781 | usleep(100000); |
| 782 | booting = is_booting(); |
| 783 | goto try_loading_again; |
| 784 | } |
| 785 | INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno); |
| 786 | write(loading_fd, "-1", 2); |
| 787 | goto data_close_out; |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 788 | } |
Benoit Goby | 609d882 | 2010-11-09 18:10:24 -0800 | [diff] [blame] | 789 | } |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 790 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 791 | |
| 792 | if(!load_firmware(fw_fd, loading_fd, data_fd)) |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 793 | INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 794 | else |
Brian Swetland | 8d48c8e | 2011-03-24 15:45:30 -0700 | [diff] [blame] | 795 | INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 796 | |
| 797 | close(fw_fd); |
| 798 | data_close_out: |
| 799 | close(data_fd); |
| 800 | loading_close_out: |
| 801 | close(loading_fd); |
| 802 | file_free_out: |
Brian Swetland | 02863b9 | 2010-09-19 03:36:39 -0700 | [diff] [blame] | 803 | free(file1); |
| 804 | free(file2); |
Ajay Dudani | 76c5889 | 2013-06-12 15:55:26 -0700 | [diff] [blame] | 805 | free(file3); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 806 | data_free_out: |
| 807 | free(data); |
| 808 | loading_free_out: |
| 809 | free(loading); |
| 810 | root_free_out: |
| 811 | free(root); |
| 812 | } |
| 813 | |
| 814 | static void handle_firmware_event(struct uevent *uevent) |
| 815 | { |
| 816 | pid_t pid; |
Colin Cross | 982a815 | 2010-06-03 12:21:01 -0700 | [diff] [blame] | 817 | int ret; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 818 | |
| 819 | if(strcmp(uevent->subsystem, "firmware")) |
| 820 | return; |
| 821 | |
| 822 | if(strcmp(uevent->action, "add")) |
| 823 | return; |
| 824 | |
| 825 | /* we fork, to avoid making large memory allocations in init proper */ |
| 826 | pid = fork(); |
| 827 | if (!pid) { |
| 828 | process_firmware_event(uevent); |
| 829 | exit(EXIT_SUCCESS); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | #define UEVENT_MSG_LEN 1024 |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 834 | void handle_device_fd() |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 835 | { |
Vernon Tang | 3f582e9 | 2011-04-25 13:08:17 +1000 | [diff] [blame] | 836 | char msg[UEVENT_MSG_LEN+2]; |
| 837 | int n; |
Nick Kralevich | 57de8b8 | 2011-05-11 14:58:24 -0700 | [diff] [blame] | 838 | while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) { |
Nick Kralevich | 5f5d5c8 | 2010-07-19 14:31:20 -0700 | [diff] [blame] | 839 | if(n >= UEVENT_MSG_LEN) /* overflow -- discard */ |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 840 | continue; |
| 841 | |
| 842 | msg[n] = '\0'; |
| 843 | msg[n+1] = '\0'; |
| 844 | |
Nick Kralevich | 5f5d5c8 | 2010-07-19 14:31:20 -0700 | [diff] [blame] | 845 | struct uevent uevent; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 846 | parse_event(msg, &uevent); |
| 847 | |
Stephen Smalley | e2eb69d | 2013-04-16 09:30:30 -0400 | [diff] [blame] | 848 | if (sehandle && selinux_status_updated() > 0) { |
| 849 | struct selabel_handle *sehandle2; |
| 850 | sehandle2 = selinux_android_file_context_handle(); |
| 851 | if (sehandle2) { |
| 852 | selabel_close(sehandle); |
| 853 | sehandle = sehandle2; |
| 854 | } |
| 855 | } |
| 856 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 857 | handle_device_event(&uevent); |
| 858 | handle_firmware_event(&uevent); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /* Coldboot walks parts of the /sys tree and pokes the uevent files |
| 863 | ** to cause the kernel to regenerate device add events that happened |
| 864 | ** before init's device manager was started |
| 865 | ** |
| 866 | ** We drain any pending events from the netlink socket every time |
| 867 | ** we poke another uevent file to make sure we don't overrun the |
| 868 | ** socket's buffer. |
| 869 | */ |
| 870 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 871 | static void do_coldboot(DIR *d) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 872 | { |
| 873 | struct dirent *de; |
| 874 | int dfd, fd; |
| 875 | |
| 876 | dfd = dirfd(d); |
| 877 | |
| 878 | fd = openat(dfd, "uevent", O_WRONLY); |
| 879 | if(fd >= 0) { |
| 880 | write(fd, "add\n", 4); |
| 881 | close(fd); |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 882 | handle_device_fd(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | while((de = readdir(d))) { |
| 886 | DIR *d2; |
| 887 | |
| 888 | if(de->d_type != DT_DIR || de->d_name[0] == '.') |
| 889 | continue; |
| 890 | |
| 891 | fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); |
| 892 | if(fd < 0) |
| 893 | continue; |
| 894 | |
| 895 | d2 = fdopendir(fd); |
| 896 | if(d2 == 0) |
| 897 | close(fd); |
| 898 | else { |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 899 | do_coldboot(d2); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 900 | closedir(d2); |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 905 | static void coldboot(const char *path) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 906 | { |
| 907 | DIR *d = opendir(path); |
| 908 | if(d) { |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 909 | do_coldboot(d); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 910 | closedir(d); |
| 911 | } |
| 912 | } |
| 913 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 914 | void device_init(void) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 915 | { |
| 916 | suseconds_t t0, t1; |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 917 | struct stat info; |
| 918 | int fd; |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 919 | |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 920 | sehandle = NULL; |
| 921 | if (is_selinux_enabled() > 0) { |
| 922 | sehandle = selinux_android_file_context_handle(); |
Stephen Smalley | e2eb69d | 2013-04-16 09:30:30 -0400 | [diff] [blame] | 923 | selinux_status_open(true); |
Stephen Smalley | ae6f3d7 | 2012-05-01 15:02:53 -0400 | [diff] [blame] | 924 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 925 | |
Andrew Boie | d562ca7 | 2012-12-04 15:47:20 -0800 | [diff] [blame] | 926 | /* is 256K enough? udev uses 16MB! */ |
| 927 | device_fd = uevent_open_socket(256*1024, true); |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 928 | if(device_fd < 0) |
| 929 | return; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 930 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 931 | fcntl(device_fd, F_SETFD, FD_CLOEXEC); |
| 932 | fcntl(device_fd, F_SETFL, O_NONBLOCK); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 933 | |
Colin Cross | f83d0b9 | 2010-04-21 12:04:20 -0700 | [diff] [blame] | 934 | if (stat(coldboot_done, &info) < 0) { |
| 935 | t0 = get_usecs(); |
| 936 | coldboot("/sys/class"); |
| 937 | coldboot("/sys/block"); |
| 938 | coldboot("/sys/devices"); |
| 939 | t1 = get_usecs(); |
| 940 | fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000); |
| 941 | close(fd); |
| 942 | log_event_print("coldboot %ld uS\n", ((long) (t1 - t0))); |
| 943 | } else { |
| 944 | log_event_print("skipping coldboot, already done\n"); |
| 945 | } |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 946 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 947 | |
Colin Cross | 0dd7ca6 | 2010-04-13 19:25:51 -0700 | [diff] [blame] | 948 | int get_device_fd() |
| 949 | { |
| 950 | return device_fd; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 951 | } |