blob: 69f5fc8ab86a7788b866a57a0e7a0b1a1761f293 [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);
454 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
455 link_num++;
456 else
457 links[link_num] = NULL;
458 free(p);
459 }
460
461 if (uevent->partition_num >= 0) {
462 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
463 link_num++;
464 else
465 links[link_num] = NULL;
466 }
467
Dima Zavinf395c922013-03-06 16:23:57 -0800468 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700469 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
470 link_num++;
471 else
472 links[link_num] = NULL;
473
474 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700475}
476
Colin Crosseb5ba832011-03-30 17:37:17 -0700477static void handle_device(const char *action, const char *devpath,
478 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700479{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700480 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700481
Colin Crosseb5ba832011-03-30 17:37:17 -0700482 if(!strcmp(action, "add")) {
483 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700484 if (links) {
485 for (i = 0; links[i]; i++)
486 make_link(devpath, links[i]);
487 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700488 }
489
Colin Crosseb5ba832011-03-30 17:37:17 -0700490 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700491 if (links) {
492 for (i = 0; links[i]; i++)
493 remove_link(devpath, links[i]);
494 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700495 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700496 }
497
498 if (links) {
499 for (i = 0; links[i]; i++)
500 free(links[i]);
501 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700502 }
503}
504
Colin Crossfadb85e2011-03-30 18:32:12 -0700505static void handle_platform_device_event(struct uevent *uevent)
506{
Dima Zavinf395c922013-03-06 16:23:57 -0800507 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700508
509 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800510 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700511 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800512 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700513}
514
Colin Crosseb5ba832011-03-30 17:37:17 -0700515static const char *parse_device_name(struct uevent *uevent, unsigned int len)
516{
517 const char *name;
518
519 /* if it's not a /dev device, nothing else to do */
520 if((uevent->major < 0) || (uevent->minor < 0))
521 return NULL;
522
523 /* do we have a name? */
524 name = strrchr(uevent->path, '/');
525 if(!name)
526 return NULL;
527 name++;
528
529 /* too-long names would overrun our buffer */
530 if(strlen(name) > len)
531 return NULL;
532
533 return name;
534}
535
536static void handle_block_device_event(struct uevent *uevent)
537{
538 const char *base = "/dev/block/";
539 const char *name;
540 char devpath[96];
541 char **links = NULL;
542
543 name = parse_device_name(uevent, 64);
544 if (!name)
545 return;
546
547 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500548 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700549
Dima Zavinf395c922013-03-06 16:23:57 -0800550 if (!strncmp(uevent->path, "/devices/", 9))
Colin Crosseb5ba832011-03-30 17:37:17 -0700551 links = parse_platform_block_device(uevent);
552
553 handle_device(uevent->action, devpath, uevent->path, 1,
554 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700555}
556
557static void handle_generic_device_event(struct uevent *uevent)
558{
559 char *base;
560 const char *name;
561 char devpath[96] = {0};
562 char **links = NULL;
563
564 name = parse_device_name(uevent, 64);
565 if (!name)
566 return;
567
568 if (!strncmp(uevent->subsystem, "usb", 3)) {
569 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700570 if (uevent->device_name) {
571 /*
572 * create device node provided by kernel if present
573 * see drivers/base/core.c
574 */
575 char *p = devpath;
576 snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
577 /* skip leading /dev/ */
578 p += 5;
579 /* build directories */
580 while (*p) {
581 if (*p == '/') {
582 *p = 0;
583 make_dir(devpath, 0755);
584 *p = '/';
585 }
586 p++;
587 }
588 }
589 else {
590 /* This imitates the file system that would be created
591 * if we were using devfs instead.
592 * Minors are broken up into groups of 128, starting at "001"
593 */
594 int bus_id = uevent->minor / 128 + 1;
595 int device_id = uevent->minor % 128 + 1;
596 /* build directories */
597 make_dir("/dev/bus", 0755);
598 make_dir("/dev/bus/usb", 0755);
599 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
600 make_dir(devpath, 0755);
601 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
602 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700603 } else {
604 /* ignore other USB events */
605 return;
606 }
607 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
608 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500609 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200610 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
611 base = "/dev/dri/";
612 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700613 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
614 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500615 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700616 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
617 base = "/dev/adsp/";
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, "msm_camera", 10)) {
620 base = "/dev/msm_camera/";
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, "input", 5)) {
623 base = "/dev/input/";
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, "mtd", 3)) {
626 base = "/dev/mtd/";
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, "sound", 5)) {
629 base = "/dev/snd/";
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, "misc", 4) &&
632 !strncmp(name, "log_", 4)) {
633 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500634 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700635 name += 4;
636 } else
637 base = "/dev/";
638 links = get_character_device_symlinks(uevent);
639
640 if (!devpath[0])
641 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
642
643 handle_device(uevent->action, devpath, uevent->path, 0,
644 uevent->major, uevent->minor, links);
645}
646
647static void handle_device_event(struct uevent *uevent)
648{
Colin Cross308bc522012-07-23 16:31:28 -0700649 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700650 fixup_sys_perms(uevent->path);
651
652 if (!strncmp(uevent->subsystem, "block", 5)) {
653 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700654 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
655 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700656 } else {
657 handle_generic_device_event(uevent);
658 }
659}
660
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700661static int load_firmware(int fw_fd, int loading_fd, int data_fd)
662{
663 struct stat st;
664 long len_to_copy;
665 int ret = 0;
666
667 if(fstat(fw_fd, &st) < 0)
668 return -1;
669 len_to_copy = st.st_size;
670
671 write(loading_fd, "1", 1); /* start transfer */
672
673 while (len_to_copy > 0) {
674 char buf[PAGE_SIZE];
675 ssize_t nr;
676
677 nr = read(fw_fd, buf, sizeof(buf));
678 if(!nr)
679 break;
680 if(nr < 0) {
681 ret = -1;
682 break;
683 }
684
685 len_to_copy -= nr;
686 while (nr > 0) {
687 ssize_t nw = 0;
688
689 nw = write(data_fd, buf + nw, nr);
690 if(nw <= 0) {
691 ret = -1;
692 goto out;
693 }
694 nr -= nw;
695 }
696 }
697
698out:
699 if(!ret)
700 write(loading_fd, "0", 1); /* successful end of transfer */
701 else
702 write(loading_fd, "-1", 2); /* abort transfer */
703
704 return ret;
705}
706
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700707static int is_booting(void)
708{
709 return access("/dev/.booting", F_OK) == 0;
710}
711
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700712static void process_firmware_event(struct uevent *uevent)
713{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700714 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700715 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700716 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700717
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700718 INFO("firmware: loading '%s' for '%s'\n",
719 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720
721 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
722 if (l == -1)
723 return;
724
725 l = asprintf(&loading, "%sloading", root);
726 if (l == -1)
727 goto root_free_out;
728
729 l = asprintf(&data, "%sdata", root);
730 if (l == -1)
731 goto loading_free_out;
732
Brian Swetland02863b92010-09-19 03:36:39 -0700733 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
734 if (l == -1)
735 goto data_free_out;
736
737 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700738 if (l == -1)
739 goto data_free_out;
740
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700741 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
742 if (l == -1)
743 goto data_free_out;
744
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700745 loading_fd = open(loading, O_WRONLY);
746 if(loading_fd < 0)
747 goto file_free_out;
748
749 data_fd = open(data, O_WRONLY);
750 if(data_fd < 0)
751 goto loading_close_out;
752
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700753try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700754 fw_fd = open(file1, O_RDONLY);
755 if(fw_fd < 0) {
756 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800757 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700758 fw_fd = open(file3, O_RDONLY);
759 if (fw_fd < 0) {
760 if (booting) {
761 /* If we're not fully booted, we may be missing
762 * filesystems needed for firmware, wait and retry.
763 */
764 usleep(100000);
765 booting = is_booting();
766 goto try_loading_again;
767 }
768 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
769 write(loading_fd, "-1", 2);
770 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700771 }
Benoit Goby609d8822010-11-09 18:10:24 -0800772 }
Brian Swetland02863b92010-09-19 03:36:39 -0700773 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700774
775 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700776 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700777 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700778 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700779
780 close(fw_fd);
781data_close_out:
782 close(data_fd);
783loading_close_out:
784 close(loading_fd);
785file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700786 free(file1);
787 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700788data_free_out:
789 free(data);
790loading_free_out:
791 free(loading);
792root_free_out:
793 free(root);
794}
795
796static void handle_firmware_event(struct uevent *uevent)
797{
798 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700799 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700800
801 if(strcmp(uevent->subsystem, "firmware"))
802 return;
803
804 if(strcmp(uevent->action, "add"))
805 return;
806
807 /* we fork, to avoid making large memory allocations in init proper */
808 pid = fork();
809 if (!pid) {
810 process_firmware_event(uevent);
811 exit(EXIT_SUCCESS);
812 }
813}
814
815#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700816void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700817{
Vernon Tang3f582e92011-04-25 13:08:17 +1000818 char msg[UEVENT_MSG_LEN+2];
819 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700820 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700821 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700822 continue;
823
824 msg[n] = '\0';
825 msg[n+1] = '\0';
826
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700827 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700828 parse_event(msg, &uevent);
829
830 handle_device_event(&uevent);
831 handle_firmware_event(&uevent);
832 }
833}
834
835/* Coldboot walks parts of the /sys tree and pokes the uevent files
836** to cause the kernel to regenerate device add events that happened
837** before init's device manager was started
838**
839** We drain any pending events from the netlink socket every time
840** we poke another uevent file to make sure we don't overrun the
841** socket's buffer.
842*/
843
Colin Cross0dd7ca62010-04-13 19:25:51 -0700844static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700845{
846 struct dirent *de;
847 int dfd, fd;
848
849 dfd = dirfd(d);
850
851 fd = openat(dfd, "uevent", O_WRONLY);
852 if(fd >= 0) {
853 write(fd, "add\n", 4);
854 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700855 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700856 }
857
858 while((de = readdir(d))) {
859 DIR *d2;
860
861 if(de->d_type != DT_DIR || de->d_name[0] == '.')
862 continue;
863
864 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
865 if(fd < 0)
866 continue;
867
868 d2 = fdopendir(fd);
869 if(d2 == 0)
870 close(fd);
871 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700872 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700873 closedir(d2);
874 }
875 }
876}
877
Colin Cross0dd7ca62010-04-13 19:25:51 -0700878static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700879{
880 DIR *d = opendir(path);
881 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700882 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700883 closedir(d);
884 }
885}
886
Colin Cross0dd7ca62010-04-13 19:25:51 -0700887void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700888{
889 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700890 struct stat info;
891 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700892
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400893 sehandle = NULL;
894 if (is_selinux_enabled() > 0) {
895 sehandle = selinux_android_file_context_handle();
896 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700897
Andrew Boied562ca72012-12-04 15:47:20 -0800898 /* is 256K enough? udev uses 16MB! */
899 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700900 if(device_fd < 0)
901 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700902
Colin Cross0dd7ca62010-04-13 19:25:51 -0700903 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
904 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700905
Colin Crossf83d0b92010-04-21 12:04:20 -0700906 if (stat(coldboot_done, &info) < 0) {
907 t0 = get_usecs();
908 coldboot("/sys/class");
909 coldboot("/sys/block");
910 coldboot("/sys/devices");
911 t1 = get_usecs();
912 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
913 close(fd);
914 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
915 } else {
916 log_event_print("skipping coldboot, already done\n");
917 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700918}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700919
Colin Cross0dd7ca62010-04-13 19:25:51 -0700920int get_device_fd()
921{
922 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700923}