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