blob: 41ab85ea7346faed9c210022164b4d416a559836 [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;
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400130 char *secontext;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700132 /* upaths omit the "/sys" that paths in this list
133 * contain, so we add 4 when comparing...
134 */
135 list_for_each(node, &sys_perms) {
136 dp = &(node_to_item(node, struct perm_node, plist))->dp;
137 if (dp->prefix) {
138 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139 continue;
140 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700141 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700142 continue;
143 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700144
145 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
146 return;
147
148 sprintf(buf,"/sys%s/%s", upath, dp->attr);
149 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
150 chown(buf, dp->uid, dp->gid);
151 chmod(buf, dp->perm);
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400152 if (sehandle) {
153 secontext = NULL;
154 selabel_lookup(sehandle, &secontext, buf, 0);
155 if (secontext) {
156 setfilecon(buf, secontext);
157 freecon(secontext);
158 }
159 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700160 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700161}
162
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700163static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
164{
165 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700166 struct listnode *node;
167 struct perm_node *perm_node;
168 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700169
Colin Cross44b65d02010-04-20 14:32:50 -0700170 /* search the perms list in reverse so that ueventd.$hardware can
171 * override ueventd.rc
172 */
173 list_for_each_reverse(node, &dev_perms) {
174 perm_node = node_to_item(node, struct perm_node, plist);
175 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700176
Colin Cross44b65d02010-04-20 14:32:50 -0700177 if (dp->prefix) {
178 if (strncmp(path, dp->name, strlen(dp->name)))
179 continue;
180 } else {
181 if (strcmp(path, dp->name))
182 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700183 }
Colin Cross44b65d02010-04-20 14:32:50 -0700184 *uid = dp->uid;
185 *gid = dp->gid;
186 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700187 }
Colin Cross44b65d02010-04-20 14:32:50 -0700188 /* Default if nothing found. */
189 *uid = 0;
190 *gid = 0;
191 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192}
193
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700194static void make_device(const char *path,
195 const char *upath,
196 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197{
198 unsigned uid;
199 unsigned gid;
200 mode_t mode;
201 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500202 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700203
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700204 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700205
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500206 if (sehandle) {
207 selabel_lookup(sehandle, &secontext, path, mode);
208 setfscreatecon(secontext);
209 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700210
Colin Cross17dcc5c2010-09-03 12:25:34 -0700211 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800212 /* Temporarily change egid to avoid race condition setting the gid of the
213 * device node. Unforunately changing the euid would prevent creation of
214 * some device nodes, so the uid has to be set with chown() and is still
215 * racy. Fixing the gid race at least fixed the issue with system_server
216 * opening dynamic input devices under the AID_INPUT gid. */
217 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800219 chown(path, uid, -1);
220 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700221
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500222 if (secontext) {
223 freecon(secontext);
224 setfscreatecon(NULL);
225 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226}
227
Dima Zavinf395c922013-03-06 16:23:57 -0800228static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700229{
Dima Zavinf395c922013-03-06 16:23:57 -0800230 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700231 struct listnode *node;
232 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800233 const char *name = path;
234
235 if (!strncmp(path, "/devices/", 9)) {
236 name += 9;
237 if (!strncmp(name, "platform/", 9))
238 name += 9;
239 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700240
241 list_for_each_reverse(node, &platform_names) {
242 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800243 if ((bus->path_len < path_len) &&
244 (path[bus->path_len] == '/') &&
245 !strncmp(path, bus->path, bus->path_len))
Colin Crossfadb85e2011-03-30 18:32:12 -0700246 /* subdevice of an existing platform, ignore it */
247 return;
248 }
249
Dima Zavinf395c922013-03-06 16:23:57 -0800250 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700251
252 bus = calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800253 bus->path = strdup(path);
254 bus->path_len = path_len;
255 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700256 list_add_tail(&platform_names, &bus->list);
257}
258
259/*
Dima Zavinf395c922013-03-06 16:23:57 -0800260 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700261 * platform device prefix. If it doesn't start with a platform device, return
262 * 0.
263 */
Dima Zavinf395c922013-03-06 16:23:57 -0800264static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700265{
Dima Zavinf395c922013-03-06 16:23:57 -0800266 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700267 struct listnode *node;
268 struct platform_node *bus;
269
270 list_for_each_reverse(node, &platform_names) {
271 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800272 if ((bus->path_len < path_len) &&
273 (path[bus->path_len] == '/') &&
274 !strncmp(path, bus->path, bus->path_len))
275 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700276 }
277
278 return NULL;
279}
280
Dima Zavinf395c922013-03-06 16:23:57 -0800281static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700282{
283 struct listnode *node;
284 struct platform_node *bus;
285
286 list_for_each_reverse(node, &platform_names) {
287 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800288 if (!strcmp(path, bus->path)) {
289 INFO("removing platform device %s\n", bus->name);
290 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700291 list_remove(node);
292 free(bus);
293 return;
294 }
295 }
296}
297
Chuck Tuffli1e070842008-12-15 14:26:56 -0800298#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700299
300static inline suseconds_t get_usecs(void)
301{
302 struct timeval tv;
303 gettimeofday(&tv, 0);
304 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
305}
306
307#define log_event_print(x...) INFO(x)
308
309#else
310
311#define log_event_print(fmt, args...) do { } while (0)
312#define get_usecs() 0
313
314#endif
315
316static void parse_event(const char *msg, struct uevent *uevent)
317{
318 uevent->action = "";
319 uevent->path = "";
320 uevent->subsystem = "";
321 uevent->firmware = "";
322 uevent->major = -1;
323 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700324 uevent->partition_name = NULL;
325 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700326 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327
328 /* currently ignoring SEQNUM */
329 while(*msg) {
330 if(!strncmp(msg, "ACTION=", 7)) {
331 msg += 7;
332 uevent->action = msg;
333 } else if(!strncmp(msg, "DEVPATH=", 8)) {
334 msg += 8;
335 uevent->path = msg;
336 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
337 msg += 10;
338 uevent->subsystem = msg;
339 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
340 msg += 9;
341 uevent->firmware = msg;
342 } else if(!strncmp(msg, "MAJOR=", 6)) {
343 msg += 6;
344 uevent->major = atoi(msg);
345 } else if(!strncmp(msg, "MINOR=", 6)) {
346 msg += 6;
347 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700348 } else if(!strncmp(msg, "PARTN=", 6)) {
349 msg += 6;
350 uevent->partition_num = atoi(msg);
351 } else if(!strncmp(msg, "PARTNAME=", 9)) {
352 msg += 9;
353 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700354 } else if(!strncmp(msg, "DEVNAME=", 8)) {
355 msg += 8;
356 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700357 }
358
Wei Zhongf97b8872012-03-23 14:15:34 -0700359 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360 while(*msg++)
361 ;
362 }
363
364 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
365 uevent->action, uevent->path, uevent->subsystem,
366 uevent->firmware, uevent->major, uevent->minor);
367}
368
Benoit Gobyd2278632010-08-03 14:36:02 -0700369static char **get_character_device_symlinks(struct uevent *uevent)
370{
371 const char *parent;
372 char *slash;
373 char **links;
374 int link_num = 0;
375 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800376 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700377
Dima Zavinf395c922013-03-06 16:23:57 -0800378 pdev = find_platform_device(uevent->path);
379 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700380 return NULL;
381
382 links = malloc(sizeof(char *) * 2);
383 if (!links)
384 return NULL;
385 memset(links, 0, sizeof(char *) * 2);
386
387 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800388 parent = strchr(uevent->path + pdev->path_len, '/');
Benoit Gobyd2278632010-08-03 14:36:02 -0700389 if (!*parent)
390 goto err;
391
392 if (!strncmp(parent, "/usb", 4)) {
393 /* skip root hub name and device. use device interface */
394 while (*++parent && *parent != '/');
395 if (*parent)
396 while (*++parent && *parent != '/');
397 if (!*parent)
398 goto err;
399 slash = strchr(++parent, '/');
400 if (!slash)
401 goto err;
402 width = slash - parent;
403 if (width <= 0)
404 goto err;
405
406 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
407 link_num++;
408 else
409 links[link_num] = NULL;
410 mkdir("/dev/usb", 0755);
411 }
412 else {
413 goto err;
414 }
415
416 return links;
417err:
418 free(links);
419 return NULL;
420}
421
Colin Crossb0ab94b2010-04-08 16:16:20 -0700422static char **parse_platform_block_device(struct uevent *uevent)
423{
Colin Crossfadb85e2011-03-30 18:32:12 -0700424 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800425 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700426 char *slash;
427 int width;
428 char buf[256];
429 char link_path[256];
430 int fd;
431 int link_num = 0;
432 int ret;
433 char *p;
434 unsigned int size;
435 struct stat info;
436
Dima Zavinf395c922013-03-06 16:23:57 -0800437 pdev = find_platform_device(uevent->path);
438 if (!pdev)
439 return NULL;
440 device = pdev->name;
441
Colin Crossb0ab94b2010-04-08 16:16:20 -0700442 char **links = malloc(sizeof(char *) * 4);
443 if (!links)
444 return NULL;
445 memset(links, 0, sizeof(char *) * 4);
446
Colin Crossfadb85e2011-03-30 18:32:12 -0700447 INFO("found platform device %s\n", device);
448
449 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700450
451 if (uevent->partition_name) {
452 p = strdup(uevent->partition_name);
453 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200454 if (strcmp(uevent->partition_name, p))
455 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700456 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
457 link_num++;
458 else
459 links[link_num] = NULL;
460 free(p);
461 }
462
463 if (uevent->partition_num >= 0) {
464 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
465 link_num++;
466 else
467 links[link_num] = NULL;
468 }
469
Dima Zavinf395c922013-03-06 16:23:57 -0800470 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700471 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
472 link_num++;
473 else
474 links[link_num] = NULL;
475
476 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700477}
478
Colin Crosseb5ba832011-03-30 17:37:17 -0700479static void handle_device(const char *action, const char *devpath,
480 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700481{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700482 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700483
Colin Crosseb5ba832011-03-30 17:37:17 -0700484 if(!strcmp(action, "add")) {
485 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700486 if (links) {
487 for (i = 0; links[i]; i++)
488 make_link(devpath, links[i]);
489 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700490 }
491
Colin Crosseb5ba832011-03-30 17:37:17 -0700492 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700493 if (links) {
494 for (i = 0; links[i]; i++)
495 remove_link(devpath, links[i]);
496 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700497 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700498 }
499
500 if (links) {
501 for (i = 0; links[i]; i++)
502 free(links[i]);
503 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700504 }
505}
506
Colin Crossfadb85e2011-03-30 18:32:12 -0700507static void handle_platform_device_event(struct uevent *uevent)
508{
Dima Zavinf395c922013-03-06 16:23:57 -0800509 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700510
511 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800512 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700513 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800514 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700515}
516
Colin Crosseb5ba832011-03-30 17:37:17 -0700517static const char *parse_device_name(struct uevent *uevent, unsigned int len)
518{
519 const char *name;
520
521 /* if it's not a /dev device, nothing else to do */
522 if((uevent->major < 0) || (uevent->minor < 0))
523 return NULL;
524
525 /* do we have a name? */
526 name = strrchr(uevent->path, '/');
527 if(!name)
528 return NULL;
529 name++;
530
531 /* too-long names would overrun our buffer */
532 if(strlen(name) > len)
533 return NULL;
534
535 return name;
536}
537
538static void handle_block_device_event(struct uevent *uevent)
539{
540 const char *base = "/dev/block/";
541 const char *name;
542 char devpath[96];
543 char **links = NULL;
544
545 name = parse_device_name(uevent, 64);
546 if (!name)
547 return;
548
549 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500550 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700551
Dima Zavinf395c922013-03-06 16:23:57 -0800552 if (!strncmp(uevent->path, "/devices/", 9))
Colin Crosseb5ba832011-03-30 17:37:17 -0700553 links = parse_platform_block_device(uevent);
554
555 handle_device(uevent->action, devpath, uevent->path, 1,
556 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700557}
558
559static void handle_generic_device_event(struct uevent *uevent)
560{
561 char *base;
562 const char *name;
563 char devpath[96] = {0};
564 char **links = NULL;
565
566 name = parse_device_name(uevent, 64);
567 if (!name)
568 return;
569
570 if (!strncmp(uevent->subsystem, "usb", 3)) {
571 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700572 if (uevent->device_name) {
573 /*
574 * create device node provided by kernel if present
575 * see drivers/base/core.c
576 */
577 char *p = devpath;
578 snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
579 /* skip leading /dev/ */
580 p += 5;
581 /* build directories */
582 while (*p) {
583 if (*p == '/') {
584 *p = 0;
585 make_dir(devpath, 0755);
586 *p = '/';
587 }
588 p++;
589 }
590 }
591 else {
592 /* This imitates the file system that would be created
593 * if we were using devfs instead.
594 * Minors are broken up into groups of 128, starting at "001"
595 */
596 int bus_id = uevent->minor / 128 + 1;
597 int device_id = uevent->minor % 128 + 1;
598 /* build directories */
599 make_dir("/dev/bus", 0755);
600 make_dir("/dev/bus/usb", 0755);
601 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
602 make_dir(devpath, 0755);
603 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
604 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700605 } else {
606 /* ignore other USB events */
607 return;
608 }
609 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
610 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500611 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200612 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
613 base = "/dev/dri/";
614 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700615 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
616 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500617 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700618 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
619 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500620 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700621 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
622 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500623 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700624 } else if(!strncmp(uevent->subsystem, "input", 5)) {
625 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500626 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700627 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
628 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500629 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700630 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
631 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500632 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700633 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
634 !strncmp(name, "log_", 4)) {
635 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500636 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700637 name += 4;
638 } else
639 base = "/dev/";
640 links = get_character_device_symlinks(uevent);
641
642 if (!devpath[0])
643 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
644
645 handle_device(uevent->action, devpath, uevent->path, 0,
646 uevent->major, uevent->minor, links);
647}
648
649static void handle_device_event(struct uevent *uevent)
650{
Colin Cross308bc522012-07-23 16:31:28 -0700651 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700652 fixup_sys_perms(uevent->path);
653
654 if (!strncmp(uevent->subsystem, "block", 5)) {
655 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700656 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
657 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700658 } else {
659 handle_generic_device_event(uevent);
660 }
661}
662
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700663static int load_firmware(int fw_fd, int loading_fd, int data_fd)
664{
665 struct stat st;
666 long len_to_copy;
667 int ret = 0;
668
669 if(fstat(fw_fd, &st) < 0)
670 return -1;
671 len_to_copy = st.st_size;
672
673 write(loading_fd, "1", 1); /* start transfer */
674
675 while (len_to_copy > 0) {
676 char buf[PAGE_SIZE];
677 ssize_t nr;
678
679 nr = read(fw_fd, buf, sizeof(buf));
680 if(!nr)
681 break;
682 if(nr < 0) {
683 ret = -1;
684 break;
685 }
686
687 len_to_copy -= nr;
688 while (nr > 0) {
689 ssize_t nw = 0;
690
691 nw = write(data_fd, buf + nw, nr);
692 if(nw <= 0) {
693 ret = -1;
694 goto out;
695 }
696 nr -= nw;
697 }
698 }
699
700out:
701 if(!ret)
702 write(loading_fd, "0", 1); /* successful end of transfer */
703 else
704 write(loading_fd, "-1", 2); /* abort transfer */
705
706 return ret;
707}
708
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700709static int is_booting(void)
710{
711 return access("/dev/.booting", F_OK) == 0;
712}
713
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700714static void process_firmware_event(struct uevent *uevent)
715{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700716 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700717 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700718 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700719
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700720 INFO("firmware: loading '%s' for '%s'\n",
721 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700722
723 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
724 if (l == -1)
725 return;
726
727 l = asprintf(&loading, "%sloading", root);
728 if (l == -1)
729 goto root_free_out;
730
731 l = asprintf(&data, "%sdata", root);
732 if (l == -1)
733 goto loading_free_out;
734
Brian Swetland02863b92010-09-19 03:36:39 -0700735 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
736 if (l == -1)
737 goto data_free_out;
738
739 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700740 if (l == -1)
741 goto data_free_out;
742
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700743 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
744 if (l == -1)
745 goto data_free_out;
746
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700747 loading_fd = open(loading, O_WRONLY);
748 if(loading_fd < 0)
749 goto file_free_out;
750
751 data_fd = open(data, O_WRONLY);
752 if(data_fd < 0)
753 goto loading_close_out;
754
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700755try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700756 fw_fd = open(file1, O_RDONLY);
757 if(fw_fd < 0) {
758 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800759 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700760 fw_fd = open(file3, O_RDONLY);
761 if (fw_fd < 0) {
762 if (booting) {
763 /* If we're not fully booted, we may be missing
764 * filesystems needed for firmware, wait and retry.
765 */
766 usleep(100000);
767 booting = is_booting();
768 goto try_loading_again;
769 }
770 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
771 write(loading_fd, "-1", 2);
772 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700773 }
Benoit Goby609d8822010-11-09 18:10:24 -0800774 }
Brian Swetland02863b92010-09-19 03:36:39 -0700775 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700776
777 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700778 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700779 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700780 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700781
782 close(fw_fd);
783data_close_out:
784 close(data_fd);
785loading_close_out:
786 close(loading_fd);
787file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700788 free(file1);
789 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700790data_free_out:
791 free(data);
792loading_free_out:
793 free(loading);
794root_free_out:
795 free(root);
796}
797
798static void handle_firmware_event(struct uevent *uevent)
799{
800 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700801 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700802
803 if(strcmp(uevent->subsystem, "firmware"))
804 return;
805
806 if(strcmp(uevent->action, "add"))
807 return;
808
809 /* we fork, to avoid making large memory allocations in init proper */
810 pid = fork();
811 if (!pid) {
812 process_firmware_event(uevent);
813 exit(EXIT_SUCCESS);
814 }
815}
816
817#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700818void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700819{
Vernon Tang3f582e92011-04-25 13:08:17 +1000820 char msg[UEVENT_MSG_LEN+2];
821 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700822 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700823 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824 continue;
825
826 msg[n] = '\0';
827 msg[n+1] = '\0';
828
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700829 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700830 parse_event(msg, &uevent);
831
832 handle_device_event(&uevent);
833 handle_firmware_event(&uevent);
834 }
835}
836
837/* Coldboot walks parts of the /sys tree and pokes the uevent files
838** to cause the kernel to regenerate device add events that happened
839** before init's device manager was started
840**
841** We drain any pending events from the netlink socket every time
842** we poke another uevent file to make sure we don't overrun the
843** socket's buffer.
844*/
845
Colin Cross0dd7ca62010-04-13 19:25:51 -0700846static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700847{
848 struct dirent *de;
849 int dfd, fd;
850
851 dfd = dirfd(d);
852
853 fd = openat(dfd, "uevent", O_WRONLY);
854 if(fd >= 0) {
855 write(fd, "add\n", 4);
856 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700857 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858 }
859
860 while((de = readdir(d))) {
861 DIR *d2;
862
863 if(de->d_type != DT_DIR || de->d_name[0] == '.')
864 continue;
865
866 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
867 if(fd < 0)
868 continue;
869
870 d2 = fdopendir(fd);
871 if(d2 == 0)
872 close(fd);
873 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700874 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700875 closedir(d2);
876 }
877 }
878}
879
Colin Cross0dd7ca62010-04-13 19:25:51 -0700880static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700881{
882 DIR *d = opendir(path);
883 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700884 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700885 closedir(d);
886 }
887}
888
Colin Cross0dd7ca62010-04-13 19:25:51 -0700889void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700890{
891 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700892 struct stat info;
893 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700894
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400895 sehandle = NULL;
896 if (is_selinux_enabled() > 0) {
897 sehandle = selinux_android_file_context_handle();
898 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700899
Andrew Boied562ca72012-12-04 15:47:20 -0800900 /* is 256K enough? udev uses 16MB! */
901 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700902 if(device_fd < 0)
903 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700904
Colin Cross0dd7ca62010-04-13 19:25:51 -0700905 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
906 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700907
Colin Crossf83d0b92010-04-21 12:04:20 -0700908 if (stat(coldboot_done, &info) < 0) {
909 t0 = get_usecs();
910 coldboot("/sys/class");
911 coldboot("/sys/block");
912 coldboot("/sys/devices");
913 t1 = get_usecs();
914 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
915 close(fd);
916 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
917 } else {
918 log_event_print("skipping coldboot, already done\n");
919 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700920}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700921
Colin Cross0dd7ca62010-04-13 19:25:51 -0700922int get_device_fd()
923{
924 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700925}