blob: af88c5f09ea89dfb60f3c34718c26eb540378218 [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 Smalleye2eb69d2013-04-16 09:30:30 -040036#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050037
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038#include <private/android_filesystem_config.h>
39#include <sys/time.h>
40#include <asm/page.h>
Colin Cross982a8152010-06-03 12:21:01 -070041#include <sys/wait.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042
Dima Zavinda04c522011-09-01 17:09:44 -070043#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100044#include <cutils/uevent.h>
45
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070046#include "devices.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070047#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070048#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070051#define FIRMWARE_DIR1 "/etc/firmware"
52#define FIRMWARE_DIR2 "/vendor/firmware"
Iliyan Malchev029d44e2012-06-08 10:42:26 -070053#define FIRMWARE_DIR3 "/firmware/image"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054
Stephen Smalleye096e362012-06-11 13:37:39 -040055extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050056
Colin Cross0dd7ca62010-04-13 19:25:51 -070057static int device_fd = -1;
58
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059struct uevent {
60 const char *action;
61 const char *path;
62 const char *subsystem;
63 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070064 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070065 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070066 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067 int major;
68 int minor;
69};
70
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071struct perms_ {
72 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070073 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074 mode_t perm;
75 unsigned int uid;
76 unsigned int gid;
77 unsigned short prefix;
78};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080struct perm_node {
81 struct perms_ dp;
82 struct listnode plist;
83};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070084
Colin Crossfadb85e2011-03-30 18:32:12 -070085struct platform_node {
86 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080087 char *path;
88 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070089 struct listnode list;
90};
91
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070092static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070093static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070094static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070095
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070096int add_dev_perms(const char *name, const char *attr,
97 mode_t perm, unsigned int uid, unsigned int gid,
98 unsigned short prefix) {
99 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700100 if (!node)
101 return -ENOMEM;
102
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700103 node->dp.name = strdup(name);
104 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700105 return -ENOMEM;
106
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700107 if (attr) {
108 node->dp.attr = strdup(attr);
109 if (!node->dp.attr)
110 return -ENOMEM;
111 }
112
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700113 node->dp.perm = perm;
114 node->dp.uid = uid;
115 node->dp.gid = gid;
116 node->dp.prefix = prefix;
117
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700118 if (attr)
119 list_add_tail(&sys_perms, &node->plist);
120 else
121 list_add_tail(&dev_perms, &node->plist);
122
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700123 return 0;
124}
125
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700126void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700128 char buf[512];
129 struct listnode *node;
130 struct perms_ *dp;
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400131 char *secontext;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700132
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700133 /* upaths omit the "/sys" that paths in this list
134 * contain, so we add 4 when comparing...
135 */
136 list_for_each(node, &sys_perms) {
137 dp = &(node_to_item(node, struct perm_node, plist))->dp;
138 if (dp->prefix) {
139 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700140 continue;
141 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700142 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700143 continue;
144 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700145
146 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
147 return;
148
149 sprintf(buf,"/sys%s/%s", upath, dp->attr);
150 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
151 chown(buf, dp->uid, dp->gid);
152 chmod(buf, dp->perm);
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400153 if (sehandle) {
154 secontext = NULL;
155 selabel_lookup(sehandle, &secontext, buf, 0);
156 if (secontext) {
157 setfilecon(buf, secontext);
158 freecon(secontext);
159 }
160 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700161 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700162}
163
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
165{
166 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700167 struct listnode *node;
168 struct perm_node *perm_node;
169 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700170
Colin Cross44b65d02010-04-20 14:32:50 -0700171 /* search the perms list in reverse so that ueventd.$hardware can
172 * override ueventd.rc
173 */
174 list_for_each_reverse(node, &dev_perms) {
175 perm_node = node_to_item(node, struct perm_node, plist);
176 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700177
Colin Cross44b65d02010-04-20 14:32:50 -0700178 if (dp->prefix) {
179 if (strncmp(path, dp->name, strlen(dp->name)))
180 continue;
181 } else {
182 if (strcmp(path, dp->name))
183 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700184 }
Colin Cross44b65d02010-04-20 14:32:50 -0700185 *uid = dp->uid;
186 *gid = dp->gid;
187 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700188 }
Colin Cross44b65d02010-04-20 14:32:50 -0700189 /* Default if nothing found. */
190 *uid = 0;
191 *gid = 0;
192 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193}
194
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700195static void make_device(const char *path,
196 const char *upath,
197 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198{
199 unsigned uid;
200 unsigned gid;
201 mode_t mode;
202 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500203 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700204
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700206
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500207 if (sehandle) {
208 selabel_lookup(sehandle, &secontext, path, mode);
209 setfscreatecon(secontext);
210 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700211
Colin Cross17dcc5c2010-09-03 12:25:34 -0700212 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800213 /* Temporarily change egid to avoid race condition setting the gid of the
214 * device node. Unforunately changing the euid would prevent creation of
215 * some device nodes, so the uid has to be set with chown() and is still
216 * racy. Fixing the gid race at least fixed the issue with system_server
217 * opening dynamic input devices under the AID_INPUT gid. */
218 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800220 chown(path, uid, -1);
221 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700222
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500223 if (secontext) {
224 freecon(secontext);
225 setfscreatecon(NULL);
226 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227}
228
Dima Zavinf395c922013-03-06 16:23:57 -0800229static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700230{
Dima Zavinf395c922013-03-06 16:23:57 -0800231 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700232 struct listnode *node;
233 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800234 const char *name = path;
235
236 if (!strncmp(path, "/devices/", 9)) {
237 name += 9;
238 if (!strncmp(name, "platform/", 9))
239 name += 9;
240 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700241
242 list_for_each_reverse(node, &platform_names) {
243 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800244 if ((bus->path_len < path_len) &&
245 (path[bus->path_len] == '/') &&
246 !strncmp(path, bus->path, bus->path_len))
Colin Crossfadb85e2011-03-30 18:32:12 -0700247 /* subdevice of an existing platform, ignore it */
248 return;
249 }
250
Dima Zavinf395c922013-03-06 16:23:57 -0800251 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700252
253 bus = calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800254 bus->path = strdup(path);
255 bus->path_len = path_len;
256 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700257 list_add_tail(&platform_names, &bus->list);
258}
259
260/*
Dima Zavinf395c922013-03-06 16:23:57 -0800261 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700262 * platform device prefix. If it doesn't start with a platform device, return
263 * 0.
264 */
Dima Zavinf395c922013-03-06 16:23:57 -0800265static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700266{
Dima Zavinf395c922013-03-06 16:23:57 -0800267 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700268 struct listnode *node;
269 struct platform_node *bus;
270
271 list_for_each_reverse(node, &platform_names) {
272 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800273 if ((bus->path_len < path_len) &&
274 (path[bus->path_len] == '/') &&
275 !strncmp(path, bus->path, bus->path_len))
276 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700277 }
278
279 return NULL;
280}
281
Dima Zavinf395c922013-03-06 16:23:57 -0800282static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700283{
284 struct listnode *node;
285 struct platform_node *bus;
286
287 list_for_each_reverse(node, &platform_names) {
288 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800289 if (!strcmp(path, bus->path)) {
290 INFO("removing platform device %s\n", bus->name);
291 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700292 list_remove(node);
293 free(bus);
294 return;
295 }
296 }
297}
298
Chuck Tuffli1e070842008-12-15 14:26:56 -0800299#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300
301static inline suseconds_t get_usecs(void)
302{
303 struct timeval tv;
304 gettimeofday(&tv, 0);
305 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
306}
307
308#define log_event_print(x...) INFO(x)
309
310#else
311
312#define log_event_print(fmt, args...) do { } while (0)
313#define get_usecs() 0
314
315#endif
316
317static void parse_event(const char *msg, struct uevent *uevent)
318{
319 uevent->action = "";
320 uevent->path = "";
321 uevent->subsystem = "";
322 uevent->firmware = "";
323 uevent->major = -1;
324 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700325 uevent->partition_name = NULL;
326 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700327 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328
329 /* currently ignoring SEQNUM */
330 while(*msg) {
331 if(!strncmp(msg, "ACTION=", 7)) {
332 msg += 7;
333 uevent->action = msg;
334 } else if(!strncmp(msg, "DEVPATH=", 8)) {
335 msg += 8;
336 uevent->path = msg;
337 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
338 msg += 10;
339 uevent->subsystem = msg;
340 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
341 msg += 9;
342 uevent->firmware = msg;
343 } else if(!strncmp(msg, "MAJOR=", 6)) {
344 msg += 6;
345 uevent->major = atoi(msg);
346 } else if(!strncmp(msg, "MINOR=", 6)) {
347 msg += 6;
348 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700349 } else if(!strncmp(msg, "PARTN=", 6)) {
350 msg += 6;
351 uevent->partition_num = atoi(msg);
352 } else if(!strncmp(msg, "PARTNAME=", 9)) {
353 msg += 9;
354 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700355 } else if(!strncmp(msg, "DEVNAME=", 8)) {
356 msg += 8;
357 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358 }
359
Wei Zhongf97b8872012-03-23 14:15:34 -0700360 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361 while(*msg++)
362 ;
363 }
364
365 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
366 uevent->action, uevent->path, uevent->subsystem,
367 uevent->firmware, uevent->major, uevent->minor);
368}
369
Benoit Gobyd2278632010-08-03 14:36:02 -0700370static char **get_character_device_symlinks(struct uevent *uevent)
371{
372 const char *parent;
373 char *slash;
374 char **links;
375 int link_num = 0;
376 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800377 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700378
Dima Zavinf395c922013-03-06 16:23:57 -0800379 pdev = find_platform_device(uevent->path);
380 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700381 return NULL;
382
383 links = malloc(sizeof(char *) * 2);
384 if (!links)
385 return NULL;
386 memset(links, 0, sizeof(char *) * 2);
387
388 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800389 parent = strchr(uevent->path + pdev->path_len, '/');
Benoit Gobyd2278632010-08-03 14:36:02 -0700390 if (!*parent)
391 goto err;
392
393 if (!strncmp(parent, "/usb", 4)) {
394 /* skip root hub name and device. use device interface */
395 while (*++parent && *parent != '/');
396 if (*parent)
397 while (*++parent && *parent != '/');
398 if (!*parent)
399 goto err;
400 slash = strchr(++parent, '/');
401 if (!slash)
402 goto err;
403 width = slash - parent;
404 if (width <= 0)
405 goto err;
406
407 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
408 link_num++;
409 else
410 links[link_num] = NULL;
411 mkdir("/dev/usb", 0755);
412 }
413 else {
414 goto err;
415 }
416
417 return links;
418err:
419 free(links);
420 return NULL;
421}
422
Colin Crossb0ab94b2010-04-08 16:16:20 -0700423static char **parse_platform_block_device(struct uevent *uevent)
424{
Colin Crossfadb85e2011-03-30 18:32:12 -0700425 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800426 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700427 char *slash;
428 int width;
429 char buf[256];
430 char link_path[256];
431 int fd;
432 int link_num = 0;
433 int ret;
434 char *p;
435 unsigned int size;
436 struct stat info;
437
Dima Zavinf395c922013-03-06 16:23:57 -0800438 pdev = find_platform_device(uevent->path);
439 if (!pdev)
440 return NULL;
441 device = pdev->name;
442
Colin Crossb0ab94b2010-04-08 16:16:20 -0700443 char **links = malloc(sizeof(char *) * 4);
444 if (!links)
445 return NULL;
446 memset(links, 0, sizeof(char *) * 4);
447
Colin Crossfadb85e2011-03-30 18:32:12 -0700448 INFO("found platform device %s\n", device);
449
450 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700451
452 if (uevent->partition_name) {
453 p = strdup(uevent->partition_name);
454 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200455 if (strcmp(uevent->partition_name, p))
456 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700457 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
458 link_num++;
459 else
460 links[link_num] = NULL;
461 free(p);
462 }
463
464 if (uevent->partition_num >= 0) {
465 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
466 link_num++;
467 else
468 links[link_num] = NULL;
469 }
470
Dima Zavinf395c922013-03-06 16:23:57 -0800471 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700472 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
473 link_num++;
474 else
475 links[link_num] = NULL;
476
477 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700478}
479
Colin Crosseb5ba832011-03-30 17:37:17 -0700480static void handle_device(const char *action, const char *devpath,
481 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700482{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700483 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700484
Colin Crosseb5ba832011-03-30 17:37:17 -0700485 if(!strcmp(action, "add")) {
486 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700487 if (links) {
488 for (i = 0; links[i]; i++)
489 make_link(devpath, links[i]);
490 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700491 }
492
Colin Crosseb5ba832011-03-30 17:37:17 -0700493 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700494 if (links) {
495 for (i = 0; links[i]; i++)
496 remove_link(devpath, links[i]);
497 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700498 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700499 }
500
501 if (links) {
502 for (i = 0; links[i]; i++)
503 free(links[i]);
504 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700505 }
506}
507
Colin Crossfadb85e2011-03-30 18:32:12 -0700508static void handle_platform_device_event(struct uevent *uevent)
509{
Dima Zavinf395c922013-03-06 16:23:57 -0800510 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700511
512 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800513 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700514 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800515 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700516}
517
Colin Crosseb5ba832011-03-30 17:37:17 -0700518static const char *parse_device_name(struct uevent *uevent, unsigned int len)
519{
520 const char *name;
521
522 /* if it's not a /dev device, nothing else to do */
523 if((uevent->major < 0) || (uevent->minor < 0))
524 return NULL;
525
526 /* do we have a name? */
527 name = strrchr(uevent->path, '/');
528 if(!name)
529 return NULL;
530 name++;
531
532 /* too-long names would overrun our buffer */
533 if(strlen(name) > len)
534 return NULL;
535
536 return name;
537}
538
539static void handle_block_device_event(struct uevent *uevent)
540{
541 const char *base = "/dev/block/";
542 const char *name;
543 char devpath[96];
544 char **links = NULL;
545
546 name = parse_device_name(uevent, 64);
547 if (!name)
548 return;
549
550 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500551 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700552
Dima Zavinf395c922013-03-06 16:23:57 -0800553 if (!strncmp(uevent->path, "/devices/", 9))
Colin Crosseb5ba832011-03-30 17:37:17 -0700554 links = parse_platform_block_device(uevent);
555
556 handle_device(uevent->action, devpath, uevent->path, 1,
557 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700558}
559
560static void handle_generic_device_event(struct uevent *uevent)
561{
562 char *base;
563 const char *name;
564 char devpath[96] = {0};
565 char **links = NULL;
566
567 name = parse_device_name(uevent, 64);
568 if (!name)
569 return;
570
571 if (!strncmp(uevent->subsystem, "usb", 3)) {
572 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700573 if (uevent->device_name) {
574 /*
575 * create device node provided by kernel if present
576 * see drivers/base/core.c
577 */
578 char *p = devpath;
579 snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
580 /* skip leading /dev/ */
581 p += 5;
582 /* build directories */
583 while (*p) {
584 if (*p == '/') {
585 *p = 0;
586 make_dir(devpath, 0755);
587 *p = '/';
588 }
589 p++;
590 }
591 }
592 else {
593 /* This imitates the file system that would be created
594 * if we were using devfs instead.
595 * Minors are broken up into groups of 128, starting at "001"
596 */
597 int bus_id = uevent->minor / 128 + 1;
598 int device_id = uevent->minor % 128 + 1;
599 /* build directories */
600 make_dir("/dev/bus", 0755);
601 make_dir("/dev/bus/usb", 0755);
602 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
603 make_dir(devpath, 0755);
604 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
605 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700606 } else {
607 /* ignore other USB events */
608 return;
609 }
610 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
611 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500612 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200613 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
614 base = "/dev/dri/";
615 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700616 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
617 base = "/dev/oncrpc/";
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, "adsp", 4)) {
620 base = "/dev/adsp/";
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, "msm_camera", 10)) {
623 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500624 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700625 } else if(!strncmp(uevent->subsystem, "input", 5)) {
626 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500627 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700628 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
629 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500630 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700631 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
632 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500633 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700634 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
635 !strncmp(name, "log_", 4)) {
636 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500637 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700638 name += 4;
639 } else
640 base = "/dev/";
641 links = get_character_device_symlinks(uevent);
642
643 if (!devpath[0])
644 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
645
646 handle_device(uevent->action, devpath, uevent->path, 0,
647 uevent->major, uevent->minor, links);
648}
649
650static void handle_device_event(struct uevent *uevent)
651{
Colin Cross308bc522012-07-23 16:31:28 -0700652 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700653 fixup_sys_perms(uevent->path);
654
655 if (!strncmp(uevent->subsystem, "block", 5)) {
656 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700657 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
658 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700659 } else {
660 handle_generic_device_event(uevent);
661 }
662}
663
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700664static int load_firmware(int fw_fd, int loading_fd, int data_fd)
665{
666 struct stat st;
667 long len_to_copy;
668 int ret = 0;
669
670 if(fstat(fw_fd, &st) < 0)
671 return -1;
672 len_to_copy = st.st_size;
673
674 write(loading_fd, "1", 1); /* start transfer */
675
676 while (len_to_copy > 0) {
677 char buf[PAGE_SIZE];
678 ssize_t nr;
679
680 nr = read(fw_fd, buf, sizeof(buf));
681 if(!nr)
682 break;
683 if(nr < 0) {
684 ret = -1;
685 break;
686 }
687
688 len_to_copy -= nr;
689 while (nr > 0) {
690 ssize_t nw = 0;
691
692 nw = write(data_fd, buf + nw, nr);
693 if(nw <= 0) {
694 ret = -1;
695 goto out;
696 }
697 nr -= nw;
698 }
699 }
700
701out:
702 if(!ret)
703 write(loading_fd, "0", 1); /* successful end of transfer */
704 else
705 write(loading_fd, "-1", 2); /* abort transfer */
706
707 return ret;
708}
709
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700710static int is_booting(void)
711{
712 return access("/dev/.booting", F_OK) == 0;
713}
714
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700715static void process_firmware_event(struct uevent *uevent)
716{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700717 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700718 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700719 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700721 INFO("firmware: loading '%s' for '%s'\n",
722 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700723
724 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
725 if (l == -1)
726 return;
727
728 l = asprintf(&loading, "%sloading", root);
729 if (l == -1)
730 goto root_free_out;
731
732 l = asprintf(&data, "%sdata", root);
733 if (l == -1)
734 goto loading_free_out;
735
Brian Swetland02863b92010-09-19 03:36:39 -0700736 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
737 if (l == -1)
738 goto data_free_out;
739
740 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700741 if (l == -1)
742 goto data_free_out;
743
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700744 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
745 if (l == -1)
746 goto data_free_out;
747
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700748 loading_fd = open(loading, O_WRONLY);
749 if(loading_fd < 0)
750 goto file_free_out;
751
752 data_fd = open(data, O_WRONLY);
753 if(data_fd < 0)
754 goto loading_close_out;
755
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700756try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700757 fw_fd = open(file1, O_RDONLY);
758 if(fw_fd < 0) {
759 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800760 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700761 fw_fd = open(file3, O_RDONLY);
762 if (fw_fd < 0) {
763 if (booting) {
764 /* If we're not fully booted, we may be missing
765 * filesystems needed for firmware, wait and retry.
766 */
767 usleep(100000);
768 booting = is_booting();
769 goto try_loading_again;
770 }
771 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
772 write(loading_fd, "-1", 2);
773 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700774 }
Benoit Goby609d8822010-11-09 18:10:24 -0800775 }
Brian Swetland02863b92010-09-19 03:36:39 -0700776 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700777
778 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700779 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700780 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700781 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700782
783 close(fw_fd);
784data_close_out:
785 close(data_fd);
786loading_close_out:
787 close(loading_fd);
788file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700789 free(file1);
790 free(file2);
Ajay Dudani76c58892013-06-12 15:55:26 -0700791 free(file3);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700792data_free_out:
793 free(data);
794loading_free_out:
795 free(loading);
796root_free_out:
797 free(root);
798}
799
800static void handle_firmware_event(struct uevent *uevent)
801{
802 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700803 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700804
805 if(strcmp(uevent->subsystem, "firmware"))
806 return;
807
808 if(strcmp(uevent->action, "add"))
809 return;
810
811 /* we fork, to avoid making large memory allocations in init proper */
812 pid = fork();
813 if (!pid) {
814 process_firmware_event(uevent);
815 exit(EXIT_SUCCESS);
816 }
817}
818
819#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700820void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700821{
Vernon Tang3f582e92011-04-25 13:08:17 +1000822 char msg[UEVENT_MSG_LEN+2];
823 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700824 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700825 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700826 continue;
827
828 msg[n] = '\0';
829 msg[n+1] = '\0';
830
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700831 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700832 parse_event(msg, &uevent);
833
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400834 if (sehandle && selinux_status_updated() > 0) {
835 struct selabel_handle *sehandle2;
836 sehandle2 = selinux_android_file_context_handle();
837 if (sehandle2) {
838 selabel_close(sehandle);
839 sehandle = sehandle2;
840 }
841 }
842
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700843 handle_device_event(&uevent);
844 handle_firmware_event(&uevent);
845 }
846}
847
848/* Coldboot walks parts of the /sys tree and pokes the uevent files
849** to cause the kernel to regenerate device add events that happened
850** before init's device manager was started
851**
852** We drain any pending events from the netlink socket every time
853** we poke another uevent file to make sure we don't overrun the
854** socket's buffer.
855*/
856
Colin Cross0dd7ca62010-04-13 19:25:51 -0700857static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858{
859 struct dirent *de;
860 int dfd, fd;
861
862 dfd = dirfd(d);
863
864 fd = openat(dfd, "uevent", O_WRONLY);
865 if(fd >= 0) {
866 write(fd, "add\n", 4);
867 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700868 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700869 }
870
871 while((de = readdir(d))) {
872 DIR *d2;
873
874 if(de->d_type != DT_DIR || de->d_name[0] == '.')
875 continue;
876
877 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
878 if(fd < 0)
879 continue;
880
881 d2 = fdopendir(fd);
882 if(d2 == 0)
883 close(fd);
884 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700885 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700886 closedir(d2);
887 }
888 }
889}
890
Colin Cross0dd7ca62010-04-13 19:25:51 -0700891static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700892{
893 DIR *d = opendir(path);
894 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700895 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700896 closedir(d);
897 }
898}
899
Colin Cross0dd7ca62010-04-13 19:25:51 -0700900void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700901{
902 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700903 struct stat info;
904 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700905
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400906 sehandle = NULL;
907 if (is_selinux_enabled() > 0) {
908 sehandle = selinux_android_file_context_handle();
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400909 selinux_status_open(true);
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400910 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700911
Andrew Boied562ca72012-12-04 15:47:20 -0800912 /* is 256K enough? udev uses 16MB! */
913 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700914 if(device_fd < 0)
915 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700916
Colin Cross0dd7ca62010-04-13 19:25:51 -0700917 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
918 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700919
Colin Crossf83d0b92010-04-21 12:04:20 -0700920 if (stat(coldboot_done, &info) < 0) {
921 t0 = get_usecs();
922 coldboot("/sys/class");
923 coldboot("/sys/block");
924 coldboot("/sys/devices");
925 t1 = get_usecs();
926 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
927 close(fd);
928 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
929 } else {
930 log_event_print("skipping coldboot, already done\n");
931 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700932}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700933
Colin Cross0dd7ca62010-04-13 19:25:51 -0700934int get_device_fd()
935{
936 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700937}