blob: e25034c749342a36aa64a1de441b1ca5b8efc4c0 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
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 Baillyb93e5812010-11-17 11:47:23 -080018#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070019#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 Smalleye46f9d52012-01-13 08:48:47 -050032
Stephen Smalleye46f9d52012-01-13 08:48:47 -050033#include <selinux/selinux.h>
34#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040035#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070037#include <private/android_filesystem_config.h>
38#include <sys/time.h>
39#include <asm/page.h>
Colin Cross982a8152010-06-03 12:21:01 -070040#include <sys/wait.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
Dima Zavinda04c522011-09-01 17:09:44 -070042#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100043#include <cutils/uevent.h>
44
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070045#include "devices.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070046#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070047#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070048
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070050#define FIRMWARE_DIR1 "/etc/firmware"
51#define FIRMWARE_DIR2 "/vendor/firmware"
Iliyan Malchev029d44e2012-06-08 10:42:26 -070052#define FIRMWARE_DIR3 "/firmware/image"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053
Stephen Smalleye096e362012-06-11 13:37:39 -040054extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050055
Colin Cross0dd7ca62010-04-13 19:25:51 -070056static int device_fd = -1;
57
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058struct uevent {
59 const char *action;
60 const char *path;
61 const char *subsystem;
62 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070063 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070064 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070065 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066 int major;
67 int minor;
68};
69
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070struct perms_ {
71 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070072 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073 mode_t perm;
74 unsigned int uid;
75 unsigned int gid;
76 unsigned short prefix;
77};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079struct perm_node {
80 struct perms_ dp;
81 struct listnode plist;
82};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070083
Colin Crossfadb85e2011-03-30 18:32:12 -070084struct platform_node {
85 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080086 char *path;
87 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070088 struct listnode list;
89};
90
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070091static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070092static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070093static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070094
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070095int 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 Project4f6e8d72008-10-21 07:00:00 -070099 if (!node)
100 return -ENOMEM;
101
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700102 node->dp.name = strdup(name);
103 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104 return -ENOMEM;
105
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700106 if (attr) {
107 node->dp.attr = strdup(attr);
108 if (!node->dp.attr)
109 return -ENOMEM;
110 }
111
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112 node->dp.perm = perm;
113 node->dp.uid = uid;
114 node->dp.gid = gid;
115 node->dp.prefix = prefix;
116
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700117 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 Project4f6e8d72008-10-21 07:00:00 -0700122 return 0;
123}
124
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700125void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700126{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700127 char buf[512];
128 struct listnode *node;
129 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700130
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700131 /* 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 Project4f6e8d72008-10-21 07:00:00 -0700138 continue;
139 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700140 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700141 continue;
142 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700143
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 Project4f6e8d72008-10-21 07:00:00 -0700151 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700152}
153
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700154static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
155{
156 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700157 struct listnode *node;
158 struct perm_node *perm_node;
159 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700160
Colin Cross44b65d02010-04-20 14:32:50 -0700161 /* 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 Project4f6e8d72008-10-21 07:00:00 -0700167
Colin Cross44b65d02010-04-20 14:32:50 -0700168 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 Project4f6e8d72008-10-21 07:00:00 -0700174 }
Colin Cross44b65d02010-04-20 14:32:50 -0700175 *uid = dp->uid;
176 *gid = dp->gid;
177 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700178 }
Colin Cross44b65d02010-04-20 14:32:50 -0700179 /* Default if nothing found. */
180 *uid = 0;
181 *gid = 0;
182 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700183}
184
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700185static void make_device(const char *path,
186 const char *upath,
187 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700188{
189 unsigned uid;
190 unsigned gid;
191 mode_t mode;
192 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500193 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700196
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500197 if (sehandle) {
198 selabel_lookup(sehandle, &secontext, path, mode);
199 setfscreatecon(secontext);
200 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700201
Colin Cross17dcc5c2010-09-03 12:25:34 -0700202 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800203 /* 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 Project4f6e8d72008-10-21 07:00:00 -0700209 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800210 chown(path, uid, -1);
211 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700212
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500213 if (secontext) {
214 freecon(secontext);
215 setfscreatecon(NULL);
216 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700217}
218
Dima Zavinf395c922013-03-06 16:23:57 -0800219static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700220{
Dima Zavinf395c922013-03-06 16:23:57 -0800221 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700222 struct listnode *node;
223 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800224 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 Crossfadb85e2011-03-30 18:32:12 -0700231
232 list_for_each_reverse(node, &platform_names) {
233 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800234 if ((bus->path_len < path_len) &&
235 (path[bus->path_len] == '/') &&
236 !strncmp(path, bus->path, bus->path_len))
Colin Crossfadb85e2011-03-30 18:32:12 -0700237 /* subdevice of an existing platform, ignore it */
238 return;
239 }
240
Dima Zavinf395c922013-03-06 16:23:57 -0800241 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700242
243 bus = calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800244 bus->path = strdup(path);
245 bus->path_len = path_len;
246 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700247 list_add_tail(&platform_names, &bus->list);
248}
249
250/*
Dima Zavinf395c922013-03-06 16:23:57 -0800251 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700252 * platform device prefix. If it doesn't start with a platform device, return
253 * 0.
254 */
Dima Zavinf395c922013-03-06 16:23:57 -0800255static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700256{
Dima Zavinf395c922013-03-06 16:23:57 -0800257 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700258 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 Zavinf395c922013-03-06 16:23:57 -0800263 if ((bus->path_len < path_len) &&
264 (path[bus->path_len] == '/') &&
265 !strncmp(path, bus->path, bus->path_len))
266 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700267 }
268
269 return NULL;
270}
271
Dima Zavinf395c922013-03-06 16:23:57 -0800272static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700273{
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 Zavinf395c922013-03-06 16:23:57 -0800279 if (!strcmp(path, bus->path)) {
280 INFO("removing platform device %s\n", bus->name);
281 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700282 list_remove(node);
283 free(bus);
284 return;
285 }
286 }
287}
288
Chuck Tuffli1e070842008-12-15 14:26:56 -0800289#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700290
291static 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
307static 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 Crossb0ab94b2010-04-08 16:16:20 -0700315 uevent->partition_name = NULL;
316 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700317 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700318
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 Crossb0ab94b2010-04-08 16:16:20 -0700339 } 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 Zhongf97b8872012-03-23 14:15:34 -0700345 } else if(!strncmp(msg, "DEVNAME=", 8)) {
346 msg += 8;
347 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348 }
349
Wei Zhongf97b8872012-03-23 14:15:34 -0700350 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700351 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 Gobyd2278632010-08-03 14:36:02 -0700360static 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 Zavinf395c922013-03-06 16:23:57 -0800367 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700368
Dima Zavinf395c922013-03-06 16:23:57 -0800369 pdev = find_platform_device(uevent->path);
370 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700371 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 Zavinf395c922013-03-06 16:23:57 -0800379 parent = strchr(uevent->path + pdev->path_len, '/');
Benoit Gobyd2278632010-08-03 14:36:02 -0700380 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;
408err:
409 free(links);
410 return NULL;
411}
412
Colin Crossb0ab94b2010-04-08 16:16:20 -0700413static char **parse_platform_block_device(struct uevent *uevent)
414{
Colin Crossfadb85e2011-03-30 18:32:12 -0700415 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800416 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700417 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 Zavinf395c922013-03-06 16:23:57 -0800428 pdev = find_platform_device(uevent->path);
429 if (!pdev)
430 return NULL;
431 device = pdev->name;
432
Colin Crossb0ab94b2010-04-08 16:16:20 -0700433 char **links = malloc(sizeof(char *) * 4);
434 if (!links)
435 return NULL;
436 memset(links, 0, sizeof(char *) * 4);
437
Colin Crossfadb85e2011-03-30 18:32:12 -0700438 INFO("found platform device %s\n", device);
439
440 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700441
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 Zavinf395c922013-03-06 16:23:57 -0800459 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700460 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 Crossb0ab94b2010-04-08 16:16:20 -0700466}
467
Colin Crosseb5ba832011-03-30 17:37:17 -0700468static 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 Project4f6e8d72008-10-21 07:00:00 -0700470{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700471 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700472
Colin Crosseb5ba832011-03-30 17:37:17 -0700473 if(!strcmp(action, "add")) {
474 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700475 if (links) {
476 for (i = 0; links[i]; i++)
477 make_link(devpath, links[i]);
478 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700479 }
480
Colin Crosseb5ba832011-03-30 17:37:17 -0700481 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700482 if (links) {
483 for (i = 0; links[i]; i++)
484 remove_link(devpath, links[i]);
485 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700486 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700487 }
488
489 if (links) {
490 for (i = 0; links[i]; i++)
491 free(links[i]);
492 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700493 }
494}
495
Colin Crossfadb85e2011-03-30 18:32:12 -0700496static void handle_platform_device_event(struct uevent *uevent)
497{
Dima Zavinf395c922013-03-06 16:23:57 -0800498 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700499
500 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800501 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700502 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800503 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700504}
505
Colin Crosseb5ba832011-03-30 17:37:17 -0700506static 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
527static 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 Smalleye46f9d52012-01-13 08:48:47 -0500539 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700540
Dima Zavinf395c922013-03-06 16:23:57 -0800541 if (!strncmp(uevent->path, "/devices/", 9))
Colin Crosseb5ba832011-03-30 17:37:17 -0700542 links = parse_platform_block_device(uevent);
543
544 handle_device(uevent->action, devpath, uevent->path, 1,
545 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700546}
547
548static 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 Zhongf97b8872012-03-23 14:15:34 -0700561 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 Crosseb5ba832011-03-30 17:37:17 -0700594 } else {
595 /* ignore other USB events */
596 return;
597 }
598 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
599 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500600 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200601 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
602 base = "/dev/dri/";
603 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700604 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
605 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500606 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700607 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
608 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500609 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700610 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
611 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500612 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700613 } else if(!strncmp(uevent->subsystem, "input", 5)) {
614 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500615 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700616 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
617 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500618 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700619 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
620 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500621 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700622 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
623 !strncmp(name, "log_", 4)) {
624 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500625 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700626 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
638static void handle_device_event(struct uevent *uevent)
639{
Colin Cross308bc522012-07-23 16:31:28 -0700640 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700641 fixup_sys_perms(uevent->path);
642
643 if (!strncmp(uevent->subsystem, "block", 5)) {
644 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700645 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
646 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700647 } else {
648 handle_generic_device_event(uevent);
649 }
650}
651
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700652static 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
689out:
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 Swetland8d48c8e2011-03-24 15:45:30 -0700698static int is_booting(void)
699{
700 return access("/dev/.booting", F_OK) == 0;
701}
702
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700703static void process_firmware_event(struct uevent *uevent)
704{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700705 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700706 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700707 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700708
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700709 INFO("firmware: loading '%s' for '%s'\n",
710 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700711
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 Swetland02863b92010-09-19 03:36:39 -0700724 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 Project4f6e8d72008-10-21 07:00:00 -0700729 if (l == -1)
730 goto data_free_out;
731
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700732 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
733 if (l == -1)
734 goto data_free_out;
735
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700736 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 Swetland8d48c8e2011-03-24 15:45:30 -0700744try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700745 fw_fd = open(file1, O_RDONLY);
746 if(fw_fd < 0) {
747 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800748 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700749 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 Swetland8d48c8e2011-03-24 15:45:30 -0700762 }
Benoit Goby609d8822010-11-09 18:10:24 -0800763 }
Brian Swetland02863b92010-09-19 03:36:39 -0700764 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700765
766 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700767 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700768 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700769 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700770
771 close(fw_fd);
772data_close_out:
773 close(data_fd);
774loading_close_out:
775 close(loading_fd);
776file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700777 free(file1);
778 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700779data_free_out:
780 free(data);
781loading_free_out:
782 free(loading);
783root_free_out:
784 free(root);
785}
786
787static void handle_firmware_event(struct uevent *uevent)
788{
789 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700790 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700791
792 if(strcmp(uevent->subsystem, "firmware"))
793 return;
794
795 if(strcmp(uevent->action, "add"))
796 return;
797
798 /* we fork, to avoid making large memory allocations in init proper */
799 pid = fork();
800 if (!pid) {
801 process_firmware_event(uevent);
802 exit(EXIT_SUCCESS);
803 }
804}
805
806#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700807void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700808{
Vernon Tang3f582e92011-04-25 13:08:17 +1000809 char msg[UEVENT_MSG_LEN+2];
810 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700811 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700812 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813 continue;
814
815 msg[n] = '\0';
816 msg[n+1] = '\0';
817
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700818 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700819 parse_event(msg, &uevent);
820
821 handle_device_event(&uevent);
822 handle_firmware_event(&uevent);
823 }
824}
825
826/* Coldboot walks parts of the /sys tree and pokes the uevent files
827** to cause the kernel to regenerate device add events that happened
828** before init's device manager was started
829**
830** We drain any pending events from the netlink socket every time
831** we poke another uevent file to make sure we don't overrun the
832** socket's buffer.
833*/
834
Colin Cross0dd7ca62010-04-13 19:25:51 -0700835static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700836{
837 struct dirent *de;
838 int dfd, fd;
839
840 dfd = dirfd(d);
841
842 fd = openat(dfd, "uevent", O_WRONLY);
843 if(fd >= 0) {
844 write(fd, "add\n", 4);
845 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700846 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700847 }
848
849 while((de = readdir(d))) {
850 DIR *d2;
851
852 if(de->d_type != DT_DIR || de->d_name[0] == '.')
853 continue;
854
855 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
856 if(fd < 0)
857 continue;
858
859 d2 = fdopendir(fd);
860 if(d2 == 0)
861 close(fd);
862 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700863 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700864 closedir(d2);
865 }
866 }
867}
868
Colin Cross0dd7ca62010-04-13 19:25:51 -0700869static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700870{
871 DIR *d = opendir(path);
872 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700873 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700874 closedir(d);
875 }
876}
877
Colin Cross0dd7ca62010-04-13 19:25:51 -0700878void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700879{
880 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700881 struct stat info;
882 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700883
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400884 sehandle = NULL;
885 if (is_selinux_enabled() > 0) {
886 sehandle = selinux_android_file_context_handle();
887 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700888
Andrew Boied562ca72012-12-04 15:47:20 -0800889 /* is 256K enough? udev uses 16MB! */
890 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700891 if(device_fd < 0)
892 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700893
Colin Cross0dd7ca62010-04-13 19:25:51 -0700894 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
895 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700896
Colin Crossf83d0b92010-04-21 12:04:20 -0700897 if (stat(coldboot_done, &info) < 0) {
898 t0 = get_usecs();
899 coldboot("/sys/class");
900 coldboot("/sys/block");
901 coldboot("/sys/devices");
902 t1 = get_usecs();
903 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
904 close(fd);
905 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
906 } else {
907 log_event_print("skipping coldboot, already done\n");
908 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700909}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700910
Colin Cross0dd7ca62010-04-13 19:25:51 -0700911int get_device_fd()
912{
913 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700914}