blob: a2f84aa3c9393c490019498123e86ff39ed23051 [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>
32#include <private/android_filesystem_config.h>
33#include <sys/time.h>
34#include <asm/page.h>
Colin Cross982a8152010-06-03 12:21:01 -070035#include <sys/wait.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070036
Dima Zavinda04c522011-09-01 17:09:44 -070037#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100038#include <cutils/uevent.h>
39
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040#include "devices.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070041#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070042#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070044#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070045#define FIRMWARE_DIR1 "/etc/firmware"
46#define FIRMWARE_DIR2 "/vendor/firmware"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047
Colin Cross0dd7ca62010-04-13 19:25:51 -070048static int device_fd = -1;
49
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050struct uevent {
51 const char *action;
52 const char *path;
53 const char *subsystem;
54 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070055 const char *partition_name;
56 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057 int major;
58 int minor;
59};
60
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061struct perms_ {
62 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070063 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064 mode_t perm;
65 unsigned int uid;
66 unsigned int gid;
67 unsigned short prefix;
68};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070struct perm_node {
71 struct perms_ dp;
72 struct listnode plist;
73};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070074
Colin Crossfadb85e2011-03-30 18:32:12 -070075struct platform_node {
76 char *name;
77 int name_len;
78 struct listnode list;
79};
80
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070081static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070082static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070083static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070085int add_dev_perms(const char *name, const char *attr,
86 mode_t perm, unsigned int uid, unsigned int gid,
87 unsigned short prefix) {
88 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070089 if (!node)
90 return -ENOMEM;
91
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070092 node->dp.name = strdup(name);
93 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070094 return -ENOMEM;
95
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070096 if (attr) {
97 node->dp.attr = strdup(attr);
98 if (!node->dp.attr)
99 return -ENOMEM;
100 }
101
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102 node->dp.perm = perm;
103 node->dp.uid = uid;
104 node->dp.gid = gid;
105 node->dp.prefix = prefix;
106
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700107 if (attr)
108 list_add_tail(&sys_perms, &node->plist);
109 else
110 list_add_tail(&dev_perms, &node->plist);
111
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112 return 0;
113}
114
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700115void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700116{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700117 char buf[512];
118 struct listnode *node;
119 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700120
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700121 /* upaths omit the "/sys" that paths in this list
122 * contain, so we add 4 when comparing...
123 */
124 list_for_each(node, &sys_perms) {
125 dp = &(node_to_item(node, struct perm_node, plist))->dp;
126 if (dp->prefix) {
127 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700128 continue;
129 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700130 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131 continue;
132 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700133
134 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
135 return;
136
137 sprintf(buf,"/sys%s/%s", upath, dp->attr);
138 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
139 chown(buf, dp->uid, dp->gid);
140 chmod(buf, dp->perm);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700141 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700142}
143
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700144static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
145{
146 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700147 struct listnode *node;
148 struct perm_node *perm_node;
149 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700150
Colin Cross44b65d02010-04-20 14:32:50 -0700151 /* search the perms list in reverse so that ueventd.$hardware can
152 * override ueventd.rc
153 */
154 list_for_each_reverse(node, &dev_perms) {
155 perm_node = node_to_item(node, struct perm_node, plist);
156 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700157
Colin Cross44b65d02010-04-20 14:32:50 -0700158 if (dp->prefix) {
159 if (strncmp(path, dp->name, strlen(dp->name)))
160 continue;
161 } else {
162 if (strcmp(path, dp->name))
163 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164 }
Colin Cross44b65d02010-04-20 14:32:50 -0700165 *uid = dp->uid;
166 *gid = dp->gid;
167 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700168 }
Colin Cross44b65d02010-04-20 14:32:50 -0700169 /* Default if nothing found. */
170 *uid = 0;
171 *gid = 0;
172 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173}
174
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700175static void make_device(const char *path,
176 const char *upath,
177 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700178{
179 unsigned uid;
180 unsigned gid;
181 mode_t mode;
182 dev_t dev;
183
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700184 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Colin Cross17dcc5c2010-09-03 12:25:34 -0700185 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800186 /* Temporarily change egid to avoid race condition setting the gid of the
187 * device node. Unforunately changing the euid would prevent creation of
188 * some device nodes, so the uid has to be set with chown() and is still
189 * racy. Fixing the gid race at least fixed the issue with system_server
190 * opening dynamic input devices under the AID_INPUT gid. */
191 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800193 chown(path, uid, -1);
194 setegid(AID_ROOT);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195}
196
Colin Crossfadb85e2011-03-30 18:32:12 -0700197static void add_platform_device(const char *name)
198{
199 int name_len = strlen(name);
200 struct listnode *node;
201 struct platform_node *bus;
202
203 list_for_each_reverse(node, &platform_names) {
204 bus = node_to_item(node, struct platform_node, list);
205 if ((bus->name_len < name_len) &&
206 (name[bus->name_len] == '/') &&
207 !strncmp(name, bus->name, bus->name_len))
208 /* subdevice of an existing platform, ignore it */
209 return;
210 }
211
212 INFO("adding platform device %s\n", name);
213
214 bus = calloc(1, sizeof(struct platform_node));
215 bus->name = strdup(name);
216 bus->name_len = name_len;
217 list_add_tail(&platform_names, &bus->list);
218}
219
220/*
221 * given a name that may start with a platform device, find the length of the
222 * platform device prefix. If it doesn't start with a platform device, return
223 * 0.
224 */
225static const char *find_platform_device(const char *name)
226{
227 int name_len = strlen(name);
228 struct listnode *node;
229 struct platform_node *bus;
230
231 list_for_each_reverse(node, &platform_names) {
232 bus = node_to_item(node, struct platform_node, list);
233 if ((bus->name_len < name_len) &&
234 (name[bus->name_len] == '/') &&
235 !strncmp(name, bus->name, bus->name_len))
236 return bus->name;
237 }
238
239 return NULL;
240}
241
242static void remove_platform_device(const char *name)
243{
244 struct listnode *node;
245 struct platform_node *bus;
246
247 list_for_each_reverse(node, &platform_names) {
248 bus = node_to_item(node, struct platform_node, list);
249 if (!strcmp(name, bus->name)) {
250 INFO("removing platform device %s\n", name);
251 free(bus->name);
252 list_remove(node);
253 free(bus);
254 return;
255 }
256 }
257}
258
Chuck Tuffli1e070842008-12-15 14:26:56 -0800259#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700260
261static inline suseconds_t get_usecs(void)
262{
263 struct timeval tv;
264 gettimeofday(&tv, 0);
265 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
266}
267
268#define log_event_print(x...) INFO(x)
269
270#else
271
272#define log_event_print(fmt, args...) do { } while (0)
273#define get_usecs() 0
274
275#endif
276
277static void parse_event(const char *msg, struct uevent *uevent)
278{
279 uevent->action = "";
280 uevent->path = "";
281 uevent->subsystem = "";
282 uevent->firmware = "";
283 uevent->major = -1;
284 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700285 uevent->partition_name = NULL;
286 uevent->partition_num = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287
288 /* currently ignoring SEQNUM */
289 while(*msg) {
290 if(!strncmp(msg, "ACTION=", 7)) {
291 msg += 7;
292 uevent->action = msg;
293 } else if(!strncmp(msg, "DEVPATH=", 8)) {
294 msg += 8;
295 uevent->path = msg;
296 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
297 msg += 10;
298 uevent->subsystem = msg;
299 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
300 msg += 9;
301 uevent->firmware = msg;
302 } else if(!strncmp(msg, "MAJOR=", 6)) {
303 msg += 6;
304 uevent->major = atoi(msg);
305 } else if(!strncmp(msg, "MINOR=", 6)) {
306 msg += 6;
307 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700308 } else if(!strncmp(msg, "PARTN=", 6)) {
309 msg += 6;
310 uevent->partition_num = atoi(msg);
311 } else if(!strncmp(msg, "PARTNAME=", 9)) {
312 msg += 9;
313 uevent->partition_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314 }
315
316 /* advance to after the next \0 */
317 while(*msg++)
318 ;
319 }
320
321 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
322 uevent->action, uevent->path, uevent->subsystem,
323 uevent->firmware, uevent->major, uevent->minor);
324}
325
Benoit Gobyd2278632010-08-03 14:36:02 -0700326static char **get_character_device_symlinks(struct uevent *uevent)
327{
328 const char *parent;
329 char *slash;
330 char **links;
331 int link_num = 0;
332 int width;
333
334 if (strncmp(uevent->path, "/devices/platform/", 18))
335 return NULL;
336
337 links = malloc(sizeof(char *) * 2);
338 if (!links)
339 return NULL;
340 memset(links, 0, sizeof(char *) * 2);
341
342 /* skip "/devices/platform/<driver>" */
343 parent = strchr(uevent->path + 18, '/');
344 if (!*parent)
345 goto err;
346
347 if (!strncmp(parent, "/usb", 4)) {
348 /* skip root hub name and device. use device interface */
349 while (*++parent && *parent != '/');
350 if (*parent)
351 while (*++parent && *parent != '/');
352 if (!*parent)
353 goto err;
354 slash = strchr(++parent, '/');
355 if (!slash)
356 goto err;
357 width = slash - parent;
358 if (width <= 0)
359 goto err;
360
361 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
362 link_num++;
363 else
364 links[link_num] = NULL;
365 mkdir("/dev/usb", 0755);
366 }
367 else {
368 goto err;
369 }
370
371 return links;
372err:
373 free(links);
374 return NULL;
375}
376
Colin Crossb0ab94b2010-04-08 16:16:20 -0700377static char **parse_platform_block_device(struct uevent *uevent)
378{
Colin Crossfadb85e2011-03-30 18:32:12 -0700379 const char *device;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700380 const char *path;
381 char *slash;
382 int width;
383 char buf[256];
384 char link_path[256];
385 int fd;
386 int link_num = 0;
387 int ret;
388 char *p;
389 unsigned int size;
390 struct stat info;
391
392 char **links = malloc(sizeof(char *) * 4);
393 if (!links)
394 return NULL;
395 memset(links, 0, sizeof(char *) * 4);
396
397 /* Drop "/devices/platform/" */
398 path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700399 device = path + 18;
400 device = find_platform_device(device);
401 if (!device)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700402 goto err;
403
Colin Crossfadb85e2011-03-30 18:32:12 -0700404 INFO("found platform device %s\n", device);
405
406 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700407
408 if (uevent->partition_name) {
409 p = strdup(uevent->partition_name);
410 sanitize(p);
411 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
412 link_num++;
413 else
414 links[link_num] = NULL;
415 free(p);
416 }
417
418 if (uevent->partition_num >= 0) {
419 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
420 link_num++;
421 else
422 links[link_num] = NULL;
423 }
424
425 slash = strrchr(path, '/');
426 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
427 link_num++;
428 else
429 links[link_num] = NULL;
430
431 return links;
432
433err:
434 free(links);
435 return NULL;
436}
437
Colin Crosseb5ba832011-03-30 17:37:17 -0700438static void handle_device(const char *action, const char *devpath,
439 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700440{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700441 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700442
Colin Crosseb5ba832011-03-30 17:37:17 -0700443 if(!strcmp(action, "add")) {
444 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700445 if (links) {
446 for (i = 0; links[i]; i++)
447 make_link(devpath, links[i]);
448 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700449 }
450
Colin Crosseb5ba832011-03-30 17:37:17 -0700451 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700452 if (links) {
453 for (i = 0; links[i]; i++)
454 remove_link(devpath, links[i]);
455 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700456 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700457 }
458
459 if (links) {
460 for (i = 0; links[i]; i++)
461 free(links[i]);
462 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700463 }
464}
465
Colin Crossfadb85e2011-03-30 18:32:12 -0700466static void handle_platform_device_event(struct uevent *uevent)
467{
468 const char *name = uevent->path + 18; /* length of /devices/platform/ */
469
470 if (!strcmp(uevent->action, "add"))
471 add_platform_device(name);
472 else if (!strcmp(uevent->action, "remove"))
473 remove_platform_device(name);
474}
475
Colin Crosseb5ba832011-03-30 17:37:17 -0700476static const char *parse_device_name(struct uevent *uevent, unsigned int len)
477{
478 const char *name;
479
480 /* if it's not a /dev device, nothing else to do */
481 if((uevent->major < 0) || (uevent->minor < 0))
482 return NULL;
483
484 /* do we have a name? */
485 name = strrchr(uevent->path, '/');
486 if(!name)
487 return NULL;
488 name++;
489
490 /* too-long names would overrun our buffer */
491 if(strlen(name) > len)
492 return NULL;
493
494 return name;
495}
496
497static void handle_block_device_event(struct uevent *uevent)
498{
499 const char *base = "/dev/block/";
500 const char *name;
501 char devpath[96];
502 char **links = NULL;
503
504 name = parse_device_name(uevent, 64);
505 if (!name)
506 return;
507
508 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
509 mkdir(base, 0755);
510
511 if (!strncmp(uevent->path, "/devices/platform/", 18))
512 links = parse_platform_block_device(uevent);
513
514 handle_device(uevent->action, devpath, uevent->path, 1,
515 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700516}
517
518static void handle_generic_device_event(struct uevent *uevent)
519{
520 char *base;
521 const char *name;
522 char devpath[96] = {0};
523 char **links = NULL;
524
525 name = parse_device_name(uevent, 64);
526 if (!name)
527 return;
528
529 if (!strncmp(uevent->subsystem, "usb", 3)) {
530 if (!strcmp(uevent->subsystem, "usb")) {
531 /* This imitates the file system that would be created
532 * if we were using devfs instead.
533 * Minors are broken up into groups of 128, starting at "001"
534 */
535 int bus_id = uevent->minor / 128 + 1;
536 int device_id = uevent->minor % 128 + 1;
537 /* build directories */
538 mkdir("/dev/bus", 0755);
539 mkdir("/dev/bus/usb", 0755);
540 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
541 mkdir(devpath, 0755);
542 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
543 } else {
544 /* ignore other USB events */
545 return;
546 }
547 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
548 base = "/dev/graphics/";
549 mkdir(base, 0755);
550 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
551 base = "/dev/oncrpc/";
552 mkdir(base, 0755);
553 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
554 base = "/dev/adsp/";
555 mkdir(base, 0755);
556 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
557 base = "/dev/msm_camera/";
558 mkdir(base, 0755);
559 } else if(!strncmp(uevent->subsystem, "input", 5)) {
560 base = "/dev/input/";
561 mkdir(base, 0755);
562 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
563 base = "/dev/mtd/";
564 mkdir(base, 0755);
565 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
566 base = "/dev/snd/";
567 mkdir(base, 0755);
568 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
569 !strncmp(name, "log_", 4)) {
570 base = "/dev/log/";
571 mkdir(base, 0755);
572 name += 4;
573 } else
574 base = "/dev/";
575 links = get_character_device_symlinks(uevent);
576
577 if (!devpath[0])
578 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
579
580 handle_device(uevent->action, devpath, uevent->path, 0,
581 uevent->major, uevent->minor, links);
582}
583
584static void handle_device_event(struct uevent *uevent)
585{
586 if (!strcmp(uevent->action,"add"))
587 fixup_sys_perms(uevent->path);
588
589 if (!strncmp(uevent->subsystem, "block", 5)) {
590 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700591 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
592 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700593 } else {
594 handle_generic_device_event(uevent);
595 }
596}
597
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700598static int load_firmware(int fw_fd, int loading_fd, int data_fd)
599{
600 struct stat st;
601 long len_to_copy;
602 int ret = 0;
603
604 if(fstat(fw_fd, &st) < 0)
605 return -1;
606 len_to_copy = st.st_size;
607
608 write(loading_fd, "1", 1); /* start transfer */
609
610 while (len_to_copy > 0) {
611 char buf[PAGE_SIZE];
612 ssize_t nr;
613
614 nr = read(fw_fd, buf, sizeof(buf));
615 if(!nr)
616 break;
617 if(nr < 0) {
618 ret = -1;
619 break;
620 }
621
622 len_to_copy -= nr;
623 while (nr > 0) {
624 ssize_t nw = 0;
625
626 nw = write(data_fd, buf + nw, nr);
627 if(nw <= 0) {
628 ret = -1;
629 goto out;
630 }
631 nr -= nw;
632 }
633 }
634
635out:
636 if(!ret)
637 write(loading_fd, "0", 1); /* successful end of transfer */
638 else
639 write(loading_fd, "-1", 2); /* abort transfer */
640
641 return ret;
642}
643
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700644static int is_booting(void)
645{
646 return access("/dev/.booting", F_OK) == 0;
647}
648
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700649static void process_firmware_event(struct uevent *uevent)
650{
Brian Swetland02863b92010-09-19 03:36:39 -0700651 char *root, *loading, *data, *file1 = NULL, *file2 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700652 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700653 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700654
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700655 INFO("firmware: loading '%s' for '%s'\n",
656 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700657
658 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
659 if (l == -1)
660 return;
661
662 l = asprintf(&loading, "%sloading", root);
663 if (l == -1)
664 goto root_free_out;
665
666 l = asprintf(&data, "%sdata", root);
667 if (l == -1)
668 goto loading_free_out;
669
Brian Swetland02863b92010-09-19 03:36:39 -0700670 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
671 if (l == -1)
672 goto data_free_out;
673
674 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700675 if (l == -1)
676 goto data_free_out;
677
678 loading_fd = open(loading, O_WRONLY);
679 if(loading_fd < 0)
680 goto file_free_out;
681
682 data_fd = open(data, O_WRONLY);
683 if(data_fd < 0)
684 goto loading_close_out;
685
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700686try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700687 fw_fd = open(file1, O_RDONLY);
688 if(fw_fd < 0) {
689 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800690 if (fw_fd < 0) {
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700691 if (booting) {
692 /* If we're not fully booted, we may be missing
693 * filesystems needed for firmware, wait and retry.
694 */
695 usleep(100000);
696 booting = is_booting();
697 goto try_loading_again;
698 }
699 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
Benoit Goby609d8822010-11-09 18:10:24 -0800700 write(loading_fd, "-1", 2);
Brian Swetland02863b92010-09-19 03:36:39 -0700701 goto data_close_out;
Benoit Goby609d8822010-11-09 18:10:24 -0800702 }
Brian Swetland02863b92010-09-19 03:36:39 -0700703 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700704
705 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700706 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700707 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700708 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700709
710 close(fw_fd);
711data_close_out:
712 close(data_fd);
713loading_close_out:
714 close(loading_fd);
715file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700716 free(file1);
717 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700718data_free_out:
719 free(data);
720loading_free_out:
721 free(loading);
722root_free_out:
723 free(root);
724}
725
726static void handle_firmware_event(struct uevent *uevent)
727{
728 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700729 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700730
731 if(strcmp(uevent->subsystem, "firmware"))
732 return;
733
734 if(strcmp(uevent->action, "add"))
735 return;
736
737 /* we fork, to avoid making large memory allocations in init proper */
738 pid = fork();
739 if (!pid) {
740 process_firmware_event(uevent);
741 exit(EXIT_SUCCESS);
742 }
743}
744
745#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700746void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700747{
Vernon Tang3f582e92011-04-25 13:08:17 +1000748 char msg[UEVENT_MSG_LEN+2];
749 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700750 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700751 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700752 continue;
753
754 msg[n] = '\0';
755 msg[n+1] = '\0';
756
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700757 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700758 parse_event(msg, &uevent);
759
760 handle_device_event(&uevent);
761 handle_firmware_event(&uevent);
762 }
763}
764
765/* Coldboot walks parts of the /sys tree and pokes the uevent files
766** to cause the kernel to regenerate device add events that happened
767** before init's device manager was started
768**
769** We drain any pending events from the netlink socket every time
770** we poke another uevent file to make sure we don't overrun the
771** socket's buffer.
772*/
773
Colin Cross0dd7ca62010-04-13 19:25:51 -0700774static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700775{
776 struct dirent *de;
777 int dfd, fd;
778
779 dfd = dirfd(d);
780
781 fd = openat(dfd, "uevent", O_WRONLY);
782 if(fd >= 0) {
783 write(fd, "add\n", 4);
784 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700785 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700786 }
787
788 while((de = readdir(d))) {
789 DIR *d2;
790
791 if(de->d_type != DT_DIR || de->d_name[0] == '.')
792 continue;
793
794 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
795 if(fd < 0)
796 continue;
797
798 d2 = fdopendir(fd);
799 if(d2 == 0)
800 close(fd);
801 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700802 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803 closedir(d2);
804 }
805 }
806}
807
Colin Cross0dd7ca62010-04-13 19:25:51 -0700808static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700809{
810 DIR *d = opendir(path);
811 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700812 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813 closedir(d);
814 }
815}
816
Colin Cross0dd7ca62010-04-13 19:25:51 -0700817void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700818{
819 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700820 struct stat info;
821 int fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700822
Dima Zavin2d55e022011-08-31 18:07:36 -0700823 /* is 64K enough? udev uses 16MB! */
824 device_fd = uevent_open_socket(64*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700825 if(device_fd < 0)
826 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827
Colin Cross0dd7ca62010-04-13 19:25:51 -0700828 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
829 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700830
Colin Crossf83d0b92010-04-21 12:04:20 -0700831 if (stat(coldboot_done, &info) < 0) {
832 t0 = get_usecs();
833 coldboot("/sys/class");
834 coldboot("/sys/block");
835 coldboot("/sys/devices");
836 t1 = get_usecs();
837 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
838 close(fd);
839 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
840 } else {
841 log_event_print("skipping coldboot, already done\n");
842 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700843}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700844
Colin Cross0dd7ca62010-04-13 19:25:51 -0700845int get_device_fd()
846{
847 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700848}