blob: 9c07e99422de823cc8d7fd34ecc47ced1e2a3ee8 [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
Vernon Tang3f582e92011-04-25 13:08:17 +100037#include <cutils/uevent.h>
38
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070039#include "devices.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070040#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070041#include "log.h"
42#include "list.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
61static int open_uevent_socket(void)
62{
63 struct sockaddr_nl addr;
64 int sz = 64*1024; // XXX larger? udev uses 16MB!
Nick Kralevich5f5d5c82010-07-19 14:31:20 -070065 int on = 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066 int s;
67
68 memset(&addr, 0, sizeof(addr));
69 addr.nl_family = AF_NETLINK;
70 addr.nl_pid = getpid();
71 addr.nl_groups = 0xffffffff;
72
73 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
74 if(s < 0)
75 return -1;
76
77 setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
Nick Kralevich5f5d5c82010-07-19 14:31:20 -070078 setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079
80 if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
81 close(s);
82 return -1;
83 }
84
85 return s;
86}
87
88struct perms_ {
89 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070090 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070091 mode_t perm;
92 unsigned int uid;
93 unsigned int gid;
94 unsigned short prefix;
95};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070096
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070097struct perm_node {
98 struct perms_ dp;
99 struct listnode plist;
100};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700101
102static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -0700103static list_declare(dev_perms);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700105int add_dev_perms(const char *name, const char *attr,
106 mode_t perm, unsigned int uid, unsigned int gid,
107 unsigned short prefix) {
108 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700109 if (!node)
110 return -ENOMEM;
111
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700112 node->dp.name = strdup(name);
113 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700114 return -ENOMEM;
115
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700116 if (attr) {
117 node->dp.attr = strdup(attr);
118 if (!node->dp.attr)
119 return -ENOMEM;
120 }
121
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700122 node->dp.perm = perm;
123 node->dp.uid = uid;
124 node->dp.gid = gid;
125 node->dp.prefix = prefix;
126
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700127 if (attr)
128 list_add_tail(&sys_perms, &node->plist);
129 else
130 list_add_tail(&dev_perms, &node->plist);
131
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700132 return 0;
133}
134
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700135void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700136{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700137 char buf[512];
138 struct listnode *node;
139 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700140
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700141 /* upaths omit the "/sys" that paths in this list
142 * contain, so we add 4 when comparing...
143 */
144 list_for_each(node, &sys_perms) {
145 dp = &(node_to_item(node, struct perm_node, plist))->dp;
146 if (dp->prefix) {
147 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700148 continue;
149 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700150 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700151 continue;
152 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700153
154 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
155 return;
156
157 sprintf(buf,"/sys%s/%s", upath, dp->attr);
158 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
159 chown(buf, dp->uid, dp->gid);
160 chmod(buf, dp->perm);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700161 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700162}
163
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
165{
166 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700167 struct listnode *node;
168 struct perm_node *perm_node;
169 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700170
Colin Cross44b65d02010-04-20 14:32:50 -0700171 /* search the perms list in reverse so that ueventd.$hardware can
172 * override ueventd.rc
173 */
174 list_for_each_reverse(node, &dev_perms) {
175 perm_node = node_to_item(node, struct perm_node, plist);
176 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700177
Colin Cross44b65d02010-04-20 14:32:50 -0700178 if (dp->prefix) {
179 if (strncmp(path, dp->name, strlen(dp->name)))
180 continue;
181 } else {
182 if (strcmp(path, dp->name))
183 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700184 }
Colin Cross44b65d02010-04-20 14:32:50 -0700185 *uid = dp->uid;
186 *gid = dp->gid;
187 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700188 }
Colin Cross44b65d02010-04-20 14:32:50 -0700189 /* Default if nothing found. */
190 *uid = 0;
191 *gid = 0;
192 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193}
194
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700195static void make_device(const char *path,
196 const char *upath,
197 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198{
199 unsigned uid;
200 unsigned gid;
201 mode_t mode;
202 dev_t dev;
203
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700204 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Colin Cross17dcc5c2010-09-03 12:25:34 -0700205 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800206 /* Temporarily change egid to avoid race condition setting the gid of the
207 * device node. Unforunately changing the euid would prevent creation of
208 * some device nodes, so the uid has to be set with chown() and is still
209 * racy. Fixing the gid race at least fixed the issue with system_server
210 * opening dynamic input devices under the AID_INPUT gid. */
211 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700212 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800213 chown(path, uid, -1);
214 setegid(AID_ROOT);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700215}
216
Chuck Tuffli1e070842008-12-15 14:26:56 -0800217#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218
219static inline suseconds_t get_usecs(void)
220{
221 struct timeval tv;
222 gettimeofday(&tv, 0);
223 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
224}
225
226#define log_event_print(x...) INFO(x)
227
228#else
229
230#define log_event_print(fmt, args...) do { } while (0)
231#define get_usecs() 0
232
233#endif
234
235static void parse_event(const char *msg, struct uevent *uevent)
236{
237 uevent->action = "";
238 uevent->path = "";
239 uevent->subsystem = "";
240 uevent->firmware = "";
241 uevent->major = -1;
242 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700243 uevent->partition_name = NULL;
244 uevent->partition_num = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700245
246 /* currently ignoring SEQNUM */
247 while(*msg) {
248 if(!strncmp(msg, "ACTION=", 7)) {
249 msg += 7;
250 uevent->action = msg;
251 } else if(!strncmp(msg, "DEVPATH=", 8)) {
252 msg += 8;
253 uevent->path = msg;
254 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
255 msg += 10;
256 uevent->subsystem = msg;
257 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
258 msg += 9;
259 uevent->firmware = msg;
260 } else if(!strncmp(msg, "MAJOR=", 6)) {
261 msg += 6;
262 uevent->major = atoi(msg);
263 } else if(!strncmp(msg, "MINOR=", 6)) {
264 msg += 6;
265 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700266 } else if(!strncmp(msg, "PARTN=", 6)) {
267 msg += 6;
268 uevent->partition_num = atoi(msg);
269 } else if(!strncmp(msg, "PARTNAME=", 9)) {
270 msg += 9;
271 uevent->partition_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700272 }
273
274 /* advance to after the next \0 */
275 while(*msg++)
276 ;
277 }
278
279 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
280 uevent->action, uevent->path, uevent->subsystem,
281 uevent->firmware, uevent->major, uevent->minor);
282}
283
Benoit Gobyd2278632010-08-03 14:36:02 -0700284static char **get_character_device_symlinks(struct uevent *uevent)
285{
286 const char *parent;
287 char *slash;
288 char **links;
289 int link_num = 0;
290 int width;
291
292 if (strncmp(uevent->path, "/devices/platform/", 18))
293 return NULL;
294
295 links = malloc(sizeof(char *) * 2);
296 if (!links)
297 return NULL;
298 memset(links, 0, sizeof(char *) * 2);
299
300 /* skip "/devices/platform/<driver>" */
301 parent = strchr(uevent->path + 18, '/');
302 if (!*parent)
303 goto err;
304
305 if (!strncmp(parent, "/usb", 4)) {
306 /* skip root hub name and device. use device interface */
307 while (*++parent && *parent != '/');
308 if (*parent)
309 while (*++parent && *parent != '/');
310 if (!*parent)
311 goto err;
312 slash = strchr(++parent, '/');
313 if (!slash)
314 goto err;
315 width = slash - parent;
316 if (width <= 0)
317 goto err;
318
319 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
320 link_num++;
321 else
322 links[link_num] = NULL;
323 mkdir("/dev/usb", 0755);
324 }
325 else {
326 goto err;
327 }
328
329 return links;
330err:
331 free(links);
332 return NULL;
333}
334
Colin Crossb0ab94b2010-04-08 16:16:20 -0700335static char **parse_platform_block_device(struct uevent *uevent)
336{
337 const char *driver;
338 const char *path;
339 char *slash;
340 int width;
341 char buf[256];
342 char link_path[256];
343 int fd;
344 int link_num = 0;
345 int ret;
346 char *p;
347 unsigned int size;
348 struct stat info;
349
350 char **links = malloc(sizeof(char *) * 4);
351 if (!links)
352 return NULL;
353 memset(links, 0, sizeof(char *) * 4);
354
355 /* Drop "/devices/platform/" */
356 path = uevent->path;
357 driver = path + 18;
358 slash = strchr(driver, '/');
359 if (!slash)
360 goto err;
361 width = slash - driver;
362 if (width <= 0)
363 goto err;
364
365 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%.*s",
366 width, driver);
367
368 if (uevent->partition_name) {
369 p = strdup(uevent->partition_name);
370 sanitize(p);
371 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
372 link_num++;
373 else
374 links[link_num] = NULL;
375 free(p);
376 }
377
378 if (uevent->partition_num >= 0) {
379 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
380 link_num++;
381 else
382 links[link_num] = NULL;
383 }
384
385 slash = strrchr(path, '/');
386 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
387 link_num++;
388 else
389 links[link_num] = NULL;
390
391 return links;
392
393err:
394 free(links);
395 return NULL;
396}
397
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700398static void handle_device_event(struct uevent *uevent)
399{
400 char devpath[96];
Mike Lockwood93ac1552010-05-06 13:30:09 -0400401 int devpath_ready = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 char *base, *name;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700403 char **links = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700404 int block;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700405 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700406
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700407 if (!strcmp(uevent->action,"add"))
408 fixup_sys_perms(uevent->path);
409
410 /* if it's not a /dev device, nothing else to do */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700411 if((uevent->major < 0) || (uevent->minor < 0))
412 return;
413
414 /* do we have a name? */
415 name = strrchr(uevent->path, '/');
416 if(!name)
417 return;
418 name++;
419
420 /* too-long names would overrun our buffer */
421 if(strlen(name) > 64)
422 return;
423
424 /* are we block or char? where should we live? */
The Android Open Source Project35237d12008-12-17 18:08:08 -0800425 if(!strncmp(uevent->subsystem, "block", 5)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700426 block = 1;
427 base = "/dev/block/";
428 mkdir(base, 0755);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700429 if (!strncmp(uevent->path, "/devices/platform/", 18))
430 links = parse_platform_block_device(uevent);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431 } else {
432 block = 0;
433 /* this should probably be configurable somehow */
Mike Lockwood93ac1552010-05-06 13:30:09 -0400434 if (!strncmp(uevent->subsystem, "usb", 3)) {
435 if (!strcmp(uevent->subsystem, "usb")) {
436 /* This imitates the file system that would be created
437 * if we were using devfs instead.
438 * Minors are broken up into groups of 128, starting at "001"
439 */
440 int bus_id = uevent->minor / 128 + 1;
441 int device_id = uevent->minor % 128 + 1;
442 /* build directories */
443 mkdir("/dev/bus", 0755);
444 mkdir("/dev/bus/usb", 0755);
445 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
446 mkdir(devpath, 0755);
447 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
448 devpath_ready = 1;
449 } else {
450 /* ignore other USB events */
451 return;
452 }
453 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700454 base = "/dev/graphics/";
455 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800456 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700457 base = "/dev/oncrpc/";
458 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800459 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700460 base = "/dev/adsp/";
461 mkdir(base, 0755);
Iliyan Malchevfc0182e2009-05-01 10:25:05 -0700462 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
463 base = "/dev/msm_camera/";
464 mkdir(base, 0755);
465 } else if(!strncmp(uevent->subsystem, "input", 5)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700466 base = "/dev/input/";
467 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800468 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700469 base = "/dev/mtd/";
470 mkdir(base, 0755);
Xiaopeng Yang5bb44c82008-11-25 16:20:07 -0800471 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
472 base = "/dev/snd/";
473 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800474 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700475 !strncmp(name, "log_", 4)) {
476 base = "/dev/log/";
477 mkdir(base, 0755);
478 name += 4;
479 } else
480 base = "/dev/";
Benoit Gobyd2278632010-08-03 14:36:02 -0700481 links = get_character_device_symlinks(uevent);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700482 }
483
Mike Lockwood93ac1552010-05-06 13:30:09 -0400484 if (!devpath_ready)
485 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700486
487 if(!strcmp(uevent->action, "add")) {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700488 make_device(devpath, uevent->path, block, uevent->major, uevent->minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700489 if (links) {
490 for (i = 0; links[i]; i++)
491 make_link(devpath, links[i]);
492 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700493 }
494
495 if(!strcmp(uevent->action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700496 if (links) {
497 for (i = 0; links[i]; i++)
498 remove_link(devpath, links[i]);
499 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700500 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700501 }
502
503 if (links) {
504 for (i = 0; links[i]; i++)
505 free(links[i]);
506 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700507 }
508}
509
510static int load_firmware(int fw_fd, int loading_fd, int data_fd)
511{
512 struct stat st;
513 long len_to_copy;
514 int ret = 0;
515
516 if(fstat(fw_fd, &st) < 0)
517 return -1;
518 len_to_copy = st.st_size;
519
520 write(loading_fd, "1", 1); /* start transfer */
521
522 while (len_to_copy > 0) {
523 char buf[PAGE_SIZE];
524 ssize_t nr;
525
526 nr = read(fw_fd, buf, sizeof(buf));
527 if(!nr)
528 break;
529 if(nr < 0) {
530 ret = -1;
531 break;
532 }
533
534 len_to_copy -= nr;
535 while (nr > 0) {
536 ssize_t nw = 0;
537
538 nw = write(data_fd, buf + nw, nr);
539 if(nw <= 0) {
540 ret = -1;
541 goto out;
542 }
543 nr -= nw;
544 }
545 }
546
547out:
548 if(!ret)
549 write(loading_fd, "0", 1); /* successful end of transfer */
550 else
551 write(loading_fd, "-1", 2); /* abort transfer */
552
553 return ret;
554}
555
556static void process_firmware_event(struct uevent *uevent)
557{
Brian Swetland02863b92010-09-19 03:36:39 -0700558 char *root, *loading, *data, *file1 = NULL, *file2 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559 int l, loading_fd, data_fd, fw_fd;
560
561 log_event_print("firmware event { '%s', '%s' }\n",
562 uevent->path, uevent->firmware);
563
564 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
565 if (l == -1)
566 return;
567
568 l = asprintf(&loading, "%sloading", root);
569 if (l == -1)
570 goto root_free_out;
571
572 l = asprintf(&data, "%sdata", root);
573 if (l == -1)
574 goto loading_free_out;
575
Brian Swetland02863b92010-09-19 03:36:39 -0700576 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
577 if (l == -1)
578 goto data_free_out;
579
580 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700581 if (l == -1)
582 goto data_free_out;
583
584 loading_fd = open(loading, O_WRONLY);
585 if(loading_fd < 0)
586 goto file_free_out;
587
588 data_fd = open(data, O_WRONLY);
589 if(data_fd < 0)
590 goto loading_close_out;
591
Brian Swetland02863b92010-09-19 03:36:39 -0700592 fw_fd = open(file1, O_RDONLY);
593 if(fw_fd < 0) {
594 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800595 if (fw_fd < 0) {
596 write(loading_fd, "-1", 2);
Brian Swetland02863b92010-09-19 03:36:39 -0700597 goto data_close_out;
Benoit Goby609d8822010-11-09 18:10:24 -0800598 }
Brian Swetland02863b92010-09-19 03:36:39 -0700599 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700600
601 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland02863b92010-09-19 03:36:39 -0700602 log_event_print("firmware copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700603 else
Brian Swetland02863b92010-09-19 03:36:39 -0700604 log_event_print("firmware copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700605
606 close(fw_fd);
607data_close_out:
608 close(data_fd);
609loading_close_out:
610 close(loading_fd);
611file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700612 free(file1);
613 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700614data_free_out:
615 free(data);
616loading_free_out:
617 free(loading);
618root_free_out:
619 free(root);
620}
621
622static void handle_firmware_event(struct uevent *uevent)
623{
624 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700625 int status;
626 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700627
628 if(strcmp(uevent->subsystem, "firmware"))
629 return;
630
631 if(strcmp(uevent->action, "add"))
632 return;
633
634 /* we fork, to avoid making large memory allocations in init proper */
635 pid = fork();
636 if (!pid) {
637 process_firmware_event(uevent);
638 exit(EXIT_SUCCESS);
Colin Cross982a8152010-06-03 12:21:01 -0700639 } else {
640 do {
641 ret = waitpid(pid, &status, 0);
642 } while (ret == -1 && errno == EINTR);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700643 }
644}
645
646#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700647void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700648{
Vernon Tang3f582e92011-04-25 13:08:17 +1000649 char msg[UEVENT_MSG_LEN+2];
650 int n;
651 while ((n = uevent_checked_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700652 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700653 continue;
654
655 msg[n] = '\0';
656 msg[n+1] = '\0';
657
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700658 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700659 parse_event(msg, &uevent);
660
661 handle_device_event(&uevent);
662 handle_firmware_event(&uevent);
663 }
664}
665
666/* Coldboot walks parts of the /sys tree and pokes the uevent files
667** to cause the kernel to regenerate device add events that happened
668** before init's device manager was started
669**
670** We drain any pending events from the netlink socket every time
671** we poke another uevent file to make sure we don't overrun the
672** socket's buffer.
673*/
674
Colin Cross0dd7ca62010-04-13 19:25:51 -0700675static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700676{
677 struct dirent *de;
678 int dfd, fd;
679
680 dfd = dirfd(d);
681
682 fd = openat(dfd, "uevent", O_WRONLY);
683 if(fd >= 0) {
684 write(fd, "add\n", 4);
685 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700686 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700687 }
688
689 while((de = readdir(d))) {
690 DIR *d2;
691
692 if(de->d_type != DT_DIR || de->d_name[0] == '.')
693 continue;
694
695 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
696 if(fd < 0)
697 continue;
698
699 d2 = fdopendir(fd);
700 if(d2 == 0)
701 close(fd);
702 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700703 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700704 closedir(d2);
705 }
706 }
707}
708
Colin Cross0dd7ca62010-04-13 19:25:51 -0700709static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700710{
711 DIR *d = opendir(path);
712 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700713 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700714 closedir(d);
715 }
716}
717
Colin Cross0dd7ca62010-04-13 19:25:51 -0700718void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700719{
720 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700721 struct stat info;
722 int fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700723
Colin Cross0dd7ca62010-04-13 19:25:51 -0700724 device_fd = open_uevent_socket();
725 if(device_fd < 0)
726 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700727
Colin Cross0dd7ca62010-04-13 19:25:51 -0700728 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
729 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700730
Colin Crossf83d0b92010-04-21 12:04:20 -0700731 if (stat(coldboot_done, &info) < 0) {
732 t0 = get_usecs();
733 coldboot("/sys/class");
734 coldboot("/sys/block");
735 coldboot("/sys/devices");
736 t1 = get_usecs();
737 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
738 close(fd);
739 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
740 } else {
741 log_event_print("skipping coldboot, already done\n");
742 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700743}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700744
Colin Cross0dd7ca62010-04-13 19:25:51 -0700745int get_device_fd()
746{
747 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700748}