blob: b07a1a6a1255f9f6a148ec3f77ae6d7aa3e03cdd [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;
86 int name_len;
87 struct listnode list;
88};
89
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070090static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070091static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070092static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070093
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070094int add_dev_perms(const char *name, const char *attr,
95 mode_t perm, unsigned int uid, unsigned int gid,
96 unsigned short prefix) {
97 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070098 if (!node)
99 return -ENOMEM;
100
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700101 node->dp.name = strdup(name);
102 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700103 return -ENOMEM;
104
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700105 if (attr) {
106 node->dp.attr = strdup(attr);
107 if (!node->dp.attr)
108 return -ENOMEM;
109 }
110
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700111 node->dp.perm = perm;
112 node->dp.uid = uid;
113 node->dp.gid = gid;
114 node->dp.prefix = prefix;
115
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700116 if (attr)
117 list_add_tail(&sys_perms, &node->plist);
118 else
119 list_add_tail(&dev_perms, &node->plist);
120
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700121 return 0;
122}
123
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700124void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700125{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700126 char buf[512];
127 struct listnode *node;
128 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700129
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700130 /* upaths omit the "/sys" that paths in this list
131 * contain, so we add 4 when comparing...
132 */
133 list_for_each(node, &sys_perms) {
134 dp = &(node_to_item(node, struct perm_node, plist))->dp;
135 if (dp->prefix) {
136 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137 continue;
138 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700139 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700140 continue;
141 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700142
143 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
144 return;
145
146 sprintf(buf,"/sys%s/%s", upath, dp->attr);
147 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
148 chown(buf, dp->uid, dp->gid);
149 chmod(buf, dp->perm);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700150 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700151}
152
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700153static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
154{
155 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700156 struct listnode *node;
157 struct perm_node *perm_node;
158 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700159
Colin Cross44b65d02010-04-20 14:32:50 -0700160 /* search the perms list in reverse so that ueventd.$hardware can
161 * override ueventd.rc
162 */
163 list_for_each_reverse(node, &dev_perms) {
164 perm_node = node_to_item(node, struct perm_node, plist);
165 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700166
Colin Cross44b65d02010-04-20 14:32:50 -0700167 if (dp->prefix) {
168 if (strncmp(path, dp->name, strlen(dp->name)))
169 continue;
170 } else {
171 if (strcmp(path, dp->name))
172 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173 }
Colin Cross44b65d02010-04-20 14:32:50 -0700174 *uid = dp->uid;
175 *gid = dp->gid;
176 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700177 }
Colin Cross44b65d02010-04-20 14:32:50 -0700178 /* Default if nothing found. */
179 *uid = 0;
180 *gid = 0;
181 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700182}
183
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700184static void make_device(const char *path,
185 const char *upath,
186 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700187{
188 unsigned uid;
189 unsigned gid;
190 mode_t mode;
191 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500192 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700195
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500196 if (sehandle) {
197 selabel_lookup(sehandle, &secontext, path, mode);
198 setfscreatecon(secontext);
199 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700200
Colin Cross17dcc5c2010-09-03 12:25:34 -0700201 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800202 /* Temporarily change egid to avoid race condition setting the gid of the
203 * device node. Unforunately changing the euid would prevent creation of
204 * some device nodes, so the uid has to be set with chown() and is still
205 * racy. Fixing the gid race at least fixed the issue with system_server
206 * opening dynamic input devices under the AID_INPUT gid. */
207 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700208 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800209 chown(path, uid, -1);
210 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700211
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500212 if (secontext) {
213 freecon(secontext);
214 setfscreatecon(NULL);
215 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700216}
217
Colin Crossfadb85e2011-03-30 18:32:12 -0700218static void add_platform_device(const char *name)
219{
220 int name_len = strlen(name);
221 struct listnode *node;
222 struct platform_node *bus;
223
224 list_for_each_reverse(node, &platform_names) {
225 bus = node_to_item(node, struct platform_node, list);
226 if ((bus->name_len < name_len) &&
227 (name[bus->name_len] == '/') &&
228 !strncmp(name, bus->name, bus->name_len))
229 /* subdevice of an existing platform, ignore it */
230 return;
231 }
232
233 INFO("adding platform device %s\n", name);
234
235 bus = calloc(1, sizeof(struct platform_node));
236 bus->name = strdup(name);
237 bus->name_len = name_len;
238 list_add_tail(&platform_names, &bus->list);
239}
240
241/*
242 * given a name that may start with a platform device, find the length of the
243 * platform device prefix. If it doesn't start with a platform device, return
244 * 0.
245 */
246static const char *find_platform_device(const char *name)
247{
248 int name_len = strlen(name);
249 struct listnode *node;
250 struct platform_node *bus;
251
252 list_for_each_reverse(node, &platform_names) {
253 bus = node_to_item(node, struct platform_node, list);
254 if ((bus->name_len < name_len) &&
255 (name[bus->name_len] == '/') &&
256 !strncmp(name, bus->name, bus->name_len))
257 return bus->name;
258 }
259
260 return NULL;
261}
262
263static void remove_platform_device(const char *name)
264{
265 struct listnode *node;
266 struct platform_node *bus;
267
268 list_for_each_reverse(node, &platform_names) {
269 bus = node_to_item(node, struct platform_node, list);
270 if (!strcmp(name, bus->name)) {
271 INFO("removing platform device %s\n", name);
272 free(bus->name);
273 list_remove(node);
274 free(bus);
275 return;
276 }
277 }
278}
279
Chuck Tuffli1e070842008-12-15 14:26:56 -0800280#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281
282static inline suseconds_t get_usecs(void)
283{
284 struct timeval tv;
285 gettimeofday(&tv, 0);
286 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
287}
288
289#define log_event_print(x...) INFO(x)
290
291#else
292
293#define log_event_print(fmt, args...) do { } while (0)
294#define get_usecs() 0
295
296#endif
297
298static void parse_event(const char *msg, struct uevent *uevent)
299{
300 uevent->action = "";
301 uevent->path = "";
302 uevent->subsystem = "";
303 uevent->firmware = "";
304 uevent->major = -1;
305 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700306 uevent->partition_name = NULL;
307 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700308 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700309
310 /* currently ignoring SEQNUM */
311 while(*msg) {
312 if(!strncmp(msg, "ACTION=", 7)) {
313 msg += 7;
314 uevent->action = msg;
315 } else if(!strncmp(msg, "DEVPATH=", 8)) {
316 msg += 8;
317 uevent->path = msg;
318 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
319 msg += 10;
320 uevent->subsystem = msg;
321 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
322 msg += 9;
323 uevent->firmware = msg;
324 } else if(!strncmp(msg, "MAJOR=", 6)) {
325 msg += 6;
326 uevent->major = atoi(msg);
327 } else if(!strncmp(msg, "MINOR=", 6)) {
328 msg += 6;
329 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700330 } else if(!strncmp(msg, "PARTN=", 6)) {
331 msg += 6;
332 uevent->partition_num = atoi(msg);
333 } else if(!strncmp(msg, "PARTNAME=", 9)) {
334 msg += 9;
335 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700336 } else if(!strncmp(msg, "DEVNAME=", 8)) {
337 msg += 8;
338 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700339 }
340
Wei Zhongf97b8872012-03-23 14:15:34 -0700341 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700342 while(*msg++)
343 ;
344 }
345
346 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
347 uevent->action, uevent->path, uevent->subsystem,
348 uevent->firmware, uevent->major, uevent->minor);
349}
350
Benoit Gobyd2278632010-08-03 14:36:02 -0700351static char **get_character_device_symlinks(struct uevent *uevent)
352{
353 const char *parent;
354 char *slash;
355 char **links;
356 int link_num = 0;
357 int width;
358
359 if (strncmp(uevent->path, "/devices/platform/", 18))
360 return NULL;
361
362 links = malloc(sizeof(char *) * 2);
363 if (!links)
364 return NULL;
365 memset(links, 0, sizeof(char *) * 2);
366
367 /* skip "/devices/platform/<driver>" */
368 parent = strchr(uevent->path + 18, '/');
369 if (!*parent)
370 goto err;
371
372 if (!strncmp(parent, "/usb", 4)) {
373 /* skip root hub name and device. use device interface */
374 while (*++parent && *parent != '/');
375 if (*parent)
376 while (*++parent && *parent != '/');
377 if (!*parent)
378 goto err;
379 slash = strchr(++parent, '/');
380 if (!slash)
381 goto err;
382 width = slash - parent;
383 if (width <= 0)
384 goto err;
385
386 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
387 link_num++;
388 else
389 links[link_num] = NULL;
390 mkdir("/dev/usb", 0755);
391 }
392 else {
393 goto err;
394 }
395
396 return links;
397err:
398 free(links);
399 return NULL;
400}
401
Colin Crossb0ab94b2010-04-08 16:16:20 -0700402static char **parse_platform_block_device(struct uevent *uevent)
403{
Colin Crossfadb85e2011-03-30 18:32:12 -0700404 const char *device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700405 const char *path;
406 char *slash;
407 int width;
408 char buf[256];
409 char link_path[256];
410 int fd;
411 int link_num = 0;
412 int ret;
413 char *p;
414 unsigned int size;
415 struct stat info;
416
417 char **links = malloc(sizeof(char *) * 4);
418 if (!links)
419 return NULL;
420 memset(links, 0, sizeof(char *) * 4);
421
422 /* Drop "/devices/platform/" */
423 path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700424 device = path + 18;
425 device = find_platform_device(device);
426 if (!device)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700427 goto err;
428
Colin Crossfadb85e2011-03-30 18:32:12 -0700429 INFO("found platform device %s\n", device);
430
431 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700432
433 if (uevent->partition_name) {
434 p = strdup(uevent->partition_name);
435 sanitize(p);
436 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
437 link_num++;
438 else
439 links[link_num] = NULL;
440 free(p);
441 }
442
443 if (uevent->partition_num >= 0) {
444 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
445 link_num++;
446 else
447 links[link_num] = NULL;
448 }
449
450 slash = strrchr(path, '/');
451 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
452 link_num++;
453 else
454 links[link_num] = NULL;
455
456 return links;
457
458err:
459 free(links);
460 return NULL;
461}
462
Colin Crosseb5ba832011-03-30 17:37:17 -0700463static void handle_device(const char *action, const char *devpath,
464 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700465{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700466 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700467
Colin Crosseb5ba832011-03-30 17:37:17 -0700468 if(!strcmp(action, "add")) {
469 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700470 if (links) {
471 for (i = 0; links[i]; i++)
472 make_link(devpath, links[i]);
473 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700474 }
475
Colin Crosseb5ba832011-03-30 17:37:17 -0700476 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700477 if (links) {
478 for (i = 0; links[i]; i++)
479 remove_link(devpath, links[i]);
480 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700481 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700482 }
483
484 if (links) {
485 for (i = 0; links[i]; i++)
486 free(links[i]);
487 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700488 }
489}
490
Colin Crossfadb85e2011-03-30 18:32:12 -0700491static void handle_platform_device_event(struct uevent *uevent)
492{
493 const char *name = uevent->path + 18; /* length of /devices/platform/ */
494
495 if (!strcmp(uevent->action, "add"))
496 add_platform_device(name);
497 else if (!strcmp(uevent->action, "remove"))
498 remove_platform_device(name);
499}
500
Colin Crosseb5ba832011-03-30 17:37:17 -0700501static const char *parse_device_name(struct uevent *uevent, unsigned int len)
502{
503 const char *name;
504
505 /* if it's not a /dev device, nothing else to do */
506 if((uevent->major < 0) || (uevent->minor < 0))
507 return NULL;
508
509 /* do we have a name? */
510 name = strrchr(uevent->path, '/');
511 if(!name)
512 return NULL;
513 name++;
514
515 /* too-long names would overrun our buffer */
516 if(strlen(name) > len)
517 return NULL;
518
519 return name;
520}
521
522static void handle_block_device_event(struct uevent *uevent)
523{
524 const char *base = "/dev/block/";
525 const char *name;
526 char devpath[96];
527 char **links = NULL;
528
529 name = parse_device_name(uevent, 64);
530 if (!name)
531 return;
532
533 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500534 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700535
536 if (!strncmp(uevent->path, "/devices/platform/", 18))
537 links = parse_platform_block_device(uevent);
538
539 handle_device(uevent->action, devpath, uevent->path, 1,
540 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700541}
542
543static void handle_generic_device_event(struct uevent *uevent)
544{
545 char *base;
546 const char *name;
547 char devpath[96] = {0};
548 char **links = NULL;
549
550 name = parse_device_name(uevent, 64);
551 if (!name)
552 return;
553
554 if (!strncmp(uevent->subsystem, "usb", 3)) {
555 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700556 if (uevent->device_name) {
557 /*
558 * create device node provided by kernel if present
559 * see drivers/base/core.c
560 */
561 char *p = devpath;
562 snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
563 /* skip leading /dev/ */
564 p += 5;
565 /* build directories */
566 while (*p) {
567 if (*p == '/') {
568 *p = 0;
569 make_dir(devpath, 0755);
570 *p = '/';
571 }
572 p++;
573 }
574 }
575 else {
576 /* This imitates the file system that would be created
577 * if we were using devfs instead.
578 * Minors are broken up into groups of 128, starting at "001"
579 */
580 int bus_id = uevent->minor / 128 + 1;
581 int device_id = uevent->minor % 128 + 1;
582 /* build directories */
583 make_dir("/dev/bus", 0755);
584 make_dir("/dev/bus/usb", 0755);
585 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
586 make_dir(devpath, 0755);
587 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
588 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700589 } else {
590 /* ignore other USB events */
591 return;
592 }
593 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
594 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500595 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200596 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
597 base = "/dev/dri/";
598 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700599 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
600 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500601 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700602 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
603 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500604 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700605 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
606 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500607 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700608 } else if(!strncmp(uevent->subsystem, "input", 5)) {
609 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500610 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700611 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
612 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500613 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700614 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
615 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500616 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700617 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
618 !strncmp(name, "log_", 4)) {
619 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500620 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700621 name += 4;
622 } else
623 base = "/dev/";
624 links = get_character_device_symlinks(uevent);
625
626 if (!devpath[0])
627 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
628
629 handle_device(uevent->action, devpath, uevent->path, 0,
630 uevent->major, uevent->minor, links);
631}
632
633static void handle_device_event(struct uevent *uevent)
634{
Colin Cross308bc522012-07-23 16:31:28 -0700635 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700636 fixup_sys_perms(uevent->path);
637
638 if (!strncmp(uevent->subsystem, "block", 5)) {
639 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700640 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
641 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700642 } else {
643 handle_generic_device_event(uevent);
644 }
645}
646
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700647static int load_firmware(int fw_fd, int loading_fd, int data_fd)
648{
649 struct stat st;
650 long len_to_copy;
651 int ret = 0;
652
653 if(fstat(fw_fd, &st) < 0)
654 return -1;
655 len_to_copy = st.st_size;
656
657 write(loading_fd, "1", 1); /* start transfer */
658
659 while (len_to_copy > 0) {
660 char buf[PAGE_SIZE];
661 ssize_t nr;
662
663 nr = read(fw_fd, buf, sizeof(buf));
664 if(!nr)
665 break;
666 if(nr < 0) {
667 ret = -1;
668 break;
669 }
670
671 len_to_copy -= nr;
672 while (nr > 0) {
673 ssize_t nw = 0;
674
675 nw = write(data_fd, buf + nw, nr);
676 if(nw <= 0) {
677 ret = -1;
678 goto out;
679 }
680 nr -= nw;
681 }
682 }
683
684out:
685 if(!ret)
686 write(loading_fd, "0", 1); /* successful end of transfer */
687 else
688 write(loading_fd, "-1", 2); /* abort transfer */
689
690 return ret;
691}
692
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700693static int is_booting(void)
694{
695 return access("/dev/.booting", F_OK) == 0;
696}
697
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700698static void process_firmware_event(struct uevent *uevent)
699{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700700 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700701 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700702 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700703
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700704 INFO("firmware: loading '%s' for '%s'\n",
705 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700706
707 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
708 if (l == -1)
709 return;
710
711 l = asprintf(&loading, "%sloading", root);
712 if (l == -1)
713 goto root_free_out;
714
715 l = asprintf(&data, "%sdata", root);
716 if (l == -1)
717 goto loading_free_out;
718
Brian Swetland02863b92010-09-19 03:36:39 -0700719 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
720 if (l == -1)
721 goto data_free_out;
722
723 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700724 if (l == -1)
725 goto data_free_out;
726
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700727 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
728 if (l == -1)
729 goto data_free_out;
730
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700731 loading_fd = open(loading, O_WRONLY);
732 if(loading_fd < 0)
733 goto file_free_out;
734
735 data_fd = open(data, O_WRONLY);
736 if(data_fd < 0)
737 goto loading_close_out;
738
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700739try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700740 fw_fd = open(file1, O_RDONLY);
741 if(fw_fd < 0) {
742 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800743 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700744 fw_fd = open(file3, O_RDONLY);
745 if (fw_fd < 0) {
746 if (booting) {
747 /* If we're not fully booted, we may be missing
748 * filesystems needed for firmware, wait and retry.
749 */
750 usleep(100000);
751 booting = is_booting();
752 goto try_loading_again;
753 }
754 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
755 write(loading_fd, "-1", 2);
756 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700757 }
Benoit Goby609d8822010-11-09 18:10:24 -0800758 }
Brian Swetland02863b92010-09-19 03:36:39 -0700759 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700760
761 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700762 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700763 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700764 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700765
766 close(fw_fd);
767data_close_out:
768 close(data_fd);
769loading_close_out:
770 close(loading_fd);
771file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700772 free(file1);
773 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700774data_free_out:
775 free(data);
776loading_free_out:
777 free(loading);
778root_free_out:
779 free(root);
780}
781
782static void handle_firmware_event(struct uevent *uevent)
783{
784 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700785 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700786
787 if(strcmp(uevent->subsystem, "firmware"))
788 return;
789
790 if(strcmp(uevent->action, "add"))
791 return;
792
793 /* we fork, to avoid making large memory allocations in init proper */
794 pid = fork();
795 if (!pid) {
796 process_firmware_event(uevent);
797 exit(EXIT_SUCCESS);
798 }
799}
800
801#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700802void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803{
Vernon Tang3f582e92011-04-25 13:08:17 +1000804 char msg[UEVENT_MSG_LEN+2];
805 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700806 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700807 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700808 continue;
809
810 msg[n] = '\0';
811 msg[n+1] = '\0';
812
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700813 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700814 parse_event(msg, &uevent);
815
816 handle_device_event(&uevent);
817 handle_firmware_event(&uevent);
818 }
819}
820
821/* Coldboot walks parts of the /sys tree and pokes the uevent files
822** to cause the kernel to regenerate device add events that happened
823** before init's device manager was started
824**
825** We drain any pending events from the netlink socket every time
826** we poke another uevent file to make sure we don't overrun the
827** socket's buffer.
828*/
829
Colin Cross0dd7ca62010-04-13 19:25:51 -0700830static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700831{
832 struct dirent *de;
833 int dfd, fd;
834
835 dfd = dirfd(d);
836
837 fd = openat(dfd, "uevent", O_WRONLY);
838 if(fd >= 0) {
839 write(fd, "add\n", 4);
840 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700841 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700842 }
843
844 while((de = readdir(d))) {
845 DIR *d2;
846
847 if(de->d_type != DT_DIR || de->d_name[0] == '.')
848 continue;
849
850 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
851 if(fd < 0)
852 continue;
853
854 d2 = fdopendir(fd);
855 if(d2 == 0)
856 close(fd);
857 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700858 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700859 closedir(d2);
860 }
861 }
862}
863
Colin Cross0dd7ca62010-04-13 19:25:51 -0700864static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700865{
866 DIR *d = opendir(path);
867 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700868 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700869 closedir(d);
870 }
871}
872
Colin Cross0dd7ca62010-04-13 19:25:51 -0700873void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700874{
875 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700876 struct stat info;
877 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700878
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400879 sehandle = NULL;
880 if (is_selinux_enabled() > 0) {
881 sehandle = selinux_android_file_context_handle();
882 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700883
Andrew Boied562ca72012-12-04 15:47:20 -0800884 /* is 256K enough? udev uses 16MB! */
885 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700886 if(device_fd < 0)
887 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700888
Colin Cross0dd7ca62010-04-13 19:25:51 -0700889 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
890 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700891
Colin Crossf83d0b92010-04-21 12:04:20 -0700892 if (stat(coldboot_done, &info) < 0) {
893 t0 = get_usecs();
894 coldboot("/sys/class");
895 coldboot("/sys/block");
896 coldboot("/sys/devices");
897 t1 = get_usecs();
898 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
899 close(fd);
900 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
901 } else {
902 log_event_print("skipping coldboot, already done\n");
903 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700904}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700905
Colin Cross0dd7ca62010-04-13 19:25:51 -0700906int get_device_fd()
907{
908 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700909}