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