blob: db229d36fa3b18dad2c9f663e382c8c92b2719d1 [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>
18#include <stdio.h>
19#include <stdlib.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <fcntl.h>
24#include <dirent.h>
25#include <unistd.h>
26#include <string.h>
27
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <linux/netlink.h>
31#include <private/android_filesystem_config.h>
32#include <sys/time.h>
33#include <asm/page.h>
Colin Cross982a8152010-06-03 12:21:01 -070034#include <sys/wait.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070036#include "devices.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070037#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070038#include "log.h"
39#include "list.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070042#define FIRMWARE_DIR1 "/etc/firmware"
43#define FIRMWARE_DIR2 "/vendor/firmware"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070044
Colin Cross0dd7ca62010-04-13 19:25:51 -070045static int device_fd = -1;
46
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047struct uevent {
48 const char *action;
49 const char *path;
50 const char *subsystem;
51 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070052 const char *partition_name;
53 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054 int major;
55 int minor;
56};
57
58static int open_uevent_socket(void)
59{
60 struct sockaddr_nl addr;
61 int sz = 64*1024; // XXX larger? udev uses 16MB!
Nick Kralevich5f5d5c82010-07-19 14:31:20 -070062 int on = 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063 int s;
64
65 memset(&addr, 0, sizeof(addr));
66 addr.nl_family = AF_NETLINK;
67 addr.nl_pid = getpid();
68 addr.nl_groups = 0xffffffff;
69
70 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
71 if(s < 0)
72 return -1;
73
74 setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
Nick Kralevich5f5d5c82010-07-19 14:31:20 -070075 setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076
77 if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
78 close(s);
79 return -1;
80 }
81
82 return s;
83}
84
85struct perms_ {
86 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070087 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070088 mode_t perm;
89 unsigned int uid;
90 unsigned int gid;
91 unsigned short prefix;
92};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070093
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070094struct perm_node {
95 struct perms_ dp;
96 struct listnode plist;
97};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070098
99static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -0700100static list_declare(dev_perms);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700102int add_dev_perms(const char *name, const char *attr,
103 mode_t perm, unsigned int uid, unsigned int gid,
104 unsigned short prefix) {
105 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700106 if (!node)
107 return -ENOMEM;
108
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700109 node->dp.name = strdup(name);
110 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700111 return -ENOMEM;
112
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700113 if (attr) {
114 node->dp.attr = strdup(attr);
115 if (!node->dp.attr)
116 return -ENOMEM;
117 }
118
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700119 node->dp.perm = perm;
120 node->dp.uid = uid;
121 node->dp.gid = gid;
122 node->dp.prefix = prefix;
123
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700124 if (attr)
125 list_add_tail(&sys_perms, &node->plist);
126 else
127 list_add_tail(&dev_perms, &node->plist);
128
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700129 return 0;
130}
131
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700132void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700133{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700134 char buf[512];
135 struct listnode *node;
136 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700138 /* upaths omit the "/sys" that paths in this list
139 * contain, so we add 4 when comparing...
140 */
141 list_for_each(node, &sys_perms) {
142 dp = &(node_to_item(node, struct perm_node, plist))->dp;
143 if (dp->prefix) {
144 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700145 continue;
146 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700147 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700148 continue;
149 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700150
151 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
152 return;
153
154 sprintf(buf,"/sys%s/%s", upath, dp->attr);
155 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
156 chown(buf, dp->uid, dp->gid);
157 chmod(buf, dp->perm);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700158 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700159}
160
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700161static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
162{
163 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700164 struct listnode *node;
165 struct perm_node *perm_node;
166 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700167
Colin Cross44b65d02010-04-20 14:32:50 -0700168 /* search the perms list in reverse so that ueventd.$hardware can
169 * override ueventd.rc
170 */
171 list_for_each_reverse(node, &dev_perms) {
172 perm_node = node_to_item(node, struct perm_node, plist);
173 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700174
Colin Cross44b65d02010-04-20 14:32:50 -0700175 if (dp->prefix) {
176 if (strncmp(path, dp->name, strlen(dp->name)))
177 continue;
178 } else {
179 if (strcmp(path, dp->name))
180 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700181 }
Colin Cross44b65d02010-04-20 14:32:50 -0700182 *uid = dp->uid;
183 *gid = dp->gid;
184 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700185 }
Colin Cross44b65d02010-04-20 14:32:50 -0700186 /* Default if nothing found. */
187 *uid = 0;
188 *gid = 0;
189 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190}
191
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700192static void make_device(const char *path,
193 const char *upath,
194 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195{
196 unsigned uid;
197 unsigned gid;
198 mode_t mode;
199 dev_t dev;
200
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700201 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Colin Cross17dcc5c2010-09-03 12:25:34 -0700202 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800203 /* Temporarily change egid to avoid race condition setting the gid of the
204 * device node. Unforunately changing the euid would prevent creation of
205 * some device nodes, so the uid has to be set with chown() and is still
206 * racy. Fixing the gid race at least fixed the issue with system_server
207 * opening dynamic input devices under the AID_INPUT gid. */
208 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700209 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800210 chown(path, uid, -1);
211 setegid(AID_ROOT);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700212}
213
Chuck Tuffli1e070842008-12-15 14:26:56 -0800214#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700215
216static inline suseconds_t get_usecs(void)
217{
218 struct timeval tv;
219 gettimeofday(&tv, 0);
220 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
221}
222
223#define log_event_print(x...) INFO(x)
224
225#else
226
227#define log_event_print(fmt, args...) do { } while (0)
228#define get_usecs() 0
229
230#endif
231
232static void parse_event(const char *msg, struct uevent *uevent)
233{
234 uevent->action = "";
235 uevent->path = "";
236 uevent->subsystem = "";
237 uevent->firmware = "";
238 uevent->major = -1;
239 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700240 uevent->partition_name = NULL;
241 uevent->partition_num = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242
243 /* currently ignoring SEQNUM */
244 while(*msg) {
245 if(!strncmp(msg, "ACTION=", 7)) {
246 msg += 7;
247 uevent->action = msg;
248 } else if(!strncmp(msg, "DEVPATH=", 8)) {
249 msg += 8;
250 uevent->path = msg;
251 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
252 msg += 10;
253 uevent->subsystem = msg;
254 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
255 msg += 9;
256 uevent->firmware = msg;
257 } else if(!strncmp(msg, "MAJOR=", 6)) {
258 msg += 6;
259 uevent->major = atoi(msg);
260 } else if(!strncmp(msg, "MINOR=", 6)) {
261 msg += 6;
262 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700263 } else if(!strncmp(msg, "PARTN=", 6)) {
264 msg += 6;
265 uevent->partition_num = atoi(msg);
266 } else if(!strncmp(msg, "PARTNAME=", 9)) {
267 msg += 9;
268 uevent->partition_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700269 }
270
271 /* advance to after the next \0 */
272 while(*msg++)
273 ;
274 }
275
276 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
277 uevent->action, uevent->path, uevent->subsystem,
278 uevent->firmware, uevent->major, uevent->minor);
279}
280
Benoit Gobyd2278632010-08-03 14:36:02 -0700281static char **get_character_device_symlinks(struct uevent *uevent)
282{
283 const char *parent;
284 char *slash;
285 char **links;
286 int link_num = 0;
287 int width;
288
289 if (strncmp(uevent->path, "/devices/platform/", 18))
290 return NULL;
291
292 links = malloc(sizeof(char *) * 2);
293 if (!links)
294 return NULL;
295 memset(links, 0, sizeof(char *) * 2);
296
297 /* skip "/devices/platform/<driver>" */
298 parent = strchr(uevent->path + 18, '/');
299 if (!*parent)
300 goto err;
301
302 if (!strncmp(parent, "/usb", 4)) {
303 /* skip root hub name and device. use device interface */
304 while (*++parent && *parent != '/');
305 if (*parent)
306 while (*++parent && *parent != '/');
307 if (!*parent)
308 goto err;
309 slash = strchr(++parent, '/');
310 if (!slash)
311 goto err;
312 width = slash - parent;
313 if (width <= 0)
314 goto err;
315
316 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
317 link_num++;
318 else
319 links[link_num] = NULL;
320 mkdir("/dev/usb", 0755);
321 }
322 else {
323 goto err;
324 }
325
326 return links;
327err:
328 free(links);
329 return NULL;
330}
331
Colin Crossb0ab94b2010-04-08 16:16:20 -0700332static char **parse_platform_block_device(struct uevent *uevent)
333{
334 const char *driver;
335 const char *path;
336 char *slash;
337 int width;
338 char buf[256];
339 char link_path[256];
340 int fd;
341 int link_num = 0;
342 int ret;
343 char *p;
344 unsigned int size;
345 struct stat info;
346
347 char **links = malloc(sizeof(char *) * 4);
348 if (!links)
349 return NULL;
350 memset(links, 0, sizeof(char *) * 4);
351
352 /* Drop "/devices/platform/" */
353 path = uevent->path;
354 driver = path + 18;
355 slash = strchr(driver, '/');
356 if (!slash)
357 goto err;
358 width = slash - driver;
359 if (width <= 0)
360 goto err;
361
362 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%.*s",
363 width, driver);
364
365 if (uevent->partition_name) {
366 p = strdup(uevent->partition_name);
367 sanitize(p);
368 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
369 link_num++;
370 else
371 links[link_num] = NULL;
372 free(p);
373 }
374
375 if (uevent->partition_num >= 0) {
376 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
377 link_num++;
378 else
379 links[link_num] = NULL;
380 }
381
382 slash = strrchr(path, '/');
383 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
384 link_num++;
385 else
386 links[link_num] = NULL;
387
388 return links;
389
390err:
391 free(links);
392 return NULL;
393}
394
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700395static void handle_device_event(struct uevent *uevent)
396{
397 char devpath[96];
Mike Lockwood93ac1552010-05-06 13:30:09 -0400398 int devpath_ready = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700399 char *base, *name;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700400 char **links = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700401 int block;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700402 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700403
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700404 if (!strcmp(uevent->action,"add"))
405 fixup_sys_perms(uevent->path);
406
407 /* if it's not a /dev device, nothing else to do */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700408 if((uevent->major < 0) || (uevent->minor < 0))
409 return;
410
411 /* do we have a name? */
412 name = strrchr(uevent->path, '/');
413 if(!name)
414 return;
415 name++;
416
417 /* too-long names would overrun our buffer */
418 if(strlen(name) > 64)
419 return;
420
421 /* are we block or char? where should we live? */
The Android Open Source Project35237d12008-12-17 18:08:08 -0800422 if(!strncmp(uevent->subsystem, "block", 5)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700423 block = 1;
424 base = "/dev/block/";
425 mkdir(base, 0755);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700426 if (!strncmp(uevent->path, "/devices/platform/", 18))
427 links = parse_platform_block_device(uevent);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700428 } else {
429 block = 0;
430 /* this should probably be configurable somehow */
Mike Lockwood93ac1552010-05-06 13:30:09 -0400431 if (!strncmp(uevent->subsystem, "usb", 3)) {
432 if (!strcmp(uevent->subsystem, "usb")) {
433 /* This imitates the file system that would be created
434 * if we were using devfs instead.
435 * Minors are broken up into groups of 128, starting at "001"
436 */
437 int bus_id = uevent->minor / 128 + 1;
438 int device_id = uevent->minor % 128 + 1;
439 /* build directories */
440 mkdir("/dev/bus", 0755);
441 mkdir("/dev/bus/usb", 0755);
442 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
443 mkdir(devpath, 0755);
444 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
445 devpath_ready = 1;
446 } else {
447 /* ignore other USB events */
448 return;
449 }
450 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700451 base = "/dev/graphics/";
452 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800453 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700454 base = "/dev/oncrpc/";
455 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800456 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700457 base = "/dev/adsp/";
458 mkdir(base, 0755);
Iliyan Malchevfc0182e2009-05-01 10:25:05 -0700459 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
460 base = "/dev/msm_camera/";
461 mkdir(base, 0755);
462 } else if(!strncmp(uevent->subsystem, "input", 5)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700463 base = "/dev/input/";
464 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800465 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700466 base = "/dev/mtd/";
467 mkdir(base, 0755);
Xiaopeng Yang5bb44c82008-11-25 16:20:07 -0800468 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
469 base = "/dev/snd/";
470 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800471 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700472 !strncmp(name, "log_", 4)) {
473 base = "/dev/log/";
474 mkdir(base, 0755);
475 name += 4;
476 } else
477 base = "/dev/";
Benoit Gobyd2278632010-08-03 14:36:02 -0700478 links = get_character_device_symlinks(uevent);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700479 }
480
Mike Lockwood93ac1552010-05-06 13:30:09 -0400481 if (!devpath_ready)
482 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700483
484 if(!strcmp(uevent->action, "add")) {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700485 make_device(devpath, uevent->path, block, uevent->major, uevent->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
492 if(!strcmp(uevent->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
507static int load_firmware(int fw_fd, int loading_fd, int data_fd)
508{
509 struct stat st;
510 long len_to_copy;
511 int ret = 0;
512
513 if(fstat(fw_fd, &st) < 0)
514 return -1;
515 len_to_copy = st.st_size;
516
517 write(loading_fd, "1", 1); /* start transfer */
518
519 while (len_to_copy > 0) {
520 char buf[PAGE_SIZE];
521 ssize_t nr;
522
523 nr = read(fw_fd, buf, sizeof(buf));
524 if(!nr)
525 break;
526 if(nr < 0) {
527 ret = -1;
528 break;
529 }
530
531 len_to_copy -= nr;
532 while (nr > 0) {
533 ssize_t nw = 0;
534
535 nw = write(data_fd, buf + nw, nr);
536 if(nw <= 0) {
537 ret = -1;
538 goto out;
539 }
540 nr -= nw;
541 }
542 }
543
544out:
545 if(!ret)
546 write(loading_fd, "0", 1); /* successful end of transfer */
547 else
548 write(loading_fd, "-1", 2); /* abort transfer */
549
550 return ret;
551}
552
553static void process_firmware_event(struct uevent *uevent)
554{
Brian Swetland02863b92010-09-19 03:36:39 -0700555 char *root, *loading, *data, *file1 = NULL, *file2 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700556 int l, loading_fd, data_fd, fw_fd;
557
558 log_event_print("firmware event { '%s', '%s' }\n",
559 uevent->path, uevent->firmware);
560
561 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
562 if (l == -1)
563 return;
564
565 l = asprintf(&loading, "%sloading", root);
566 if (l == -1)
567 goto root_free_out;
568
569 l = asprintf(&data, "%sdata", root);
570 if (l == -1)
571 goto loading_free_out;
572
Brian Swetland02863b92010-09-19 03:36:39 -0700573 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
574 if (l == -1)
575 goto data_free_out;
576
577 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700578 if (l == -1)
579 goto data_free_out;
580
581 loading_fd = open(loading, O_WRONLY);
582 if(loading_fd < 0)
583 goto file_free_out;
584
585 data_fd = open(data, O_WRONLY);
586 if(data_fd < 0)
587 goto loading_close_out;
588
Brian Swetland02863b92010-09-19 03:36:39 -0700589 fw_fd = open(file1, O_RDONLY);
590 if(fw_fd < 0) {
591 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800592 if (fw_fd < 0) {
593 write(loading_fd, "-1", 2);
Brian Swetland02863b92010-09-19 03:36:39 -0700594 goto data_close_out;
Benoit Goby609d8822010-11-09 18:10:24 -0800595 }
Brian Swetland02863b92010-09-19 03:36:39 -0700596 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700597
598 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland02863b92010-09-19 03:36:39 -0700599 log_event_print("firmware copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700600 else
Brian Swetland02863b92010-09-19 03:36:39 -0700601 log_event_print("firmware copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700602
603 close(fw_fd);
604data_close_out:
605 close(data_fd);
606loading_close_out:
607 close(loading_fd);
608file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700609 free(file1);
610 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700611data_free_out:
612 free(data);
613loading_free_out:
614 free(loading);
615root_free_out:
616 free(root);
617}
618
619static void handle_firmware_event(struct uevent *uevent)
620{
621 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700622 int status;
623 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700624
625 if(strcmp(uevent->subsystem, "firmware"))
626 return;
627
628 if(strcmp(uevent->action, "add"))
629 return;
630
631 /* we fork, to avoid making large memory allocations in init proper */
632 pid = fork();
633 if (!pid) {
634 process_firmware_event(uevent);
635 exit(EXIT_SUCCESS);
Colin Cross982a8152010-06-03 12:21:01 -0700636 } else {
637 do {
638 ret = waitpid(pid, &status, 0);
639 } while (ret == -1 && errno == EINTR);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700640 }
641}
642
643#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700644void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700645{
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700646 for(;;) {
647 char msg[UEVENT_MSG_LEN+2];
648 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
649 struct iovec iov = {msg, sizeof(msg)};
650 struct sockaddr_nl snl;
651 struct msghdr hdr = {&snl, sizeof(snl), &iov, 1, cred_msg, sizeof(cred_msg), 0};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700652
Nick Kralevichfad72042010-07-19 15:53:05 -0700653 ssize_t n = recvmsg(device_fd, &hdr, 0);
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700654 if (n <= 0) {
655 break;
656 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700657
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700658 if ((snl.nl_groups != 1) || (snl.nl_pid != 0)) {
659 /* ignoring non-kernel netlink multicast message */
660 continue;
661 }
662
663 struct cmsghdr * cmsg = CMSG_FIRSTHDR(&hdr);
664 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
665 /* no sender credentials received, ignore message */
666 continue;
667 }
668
669 struct ucred * cred = (struct ucred *)CMSG_DATA(cmsg);
670 if (cred->uid != 0) {
671 /* message from non-root user, ignore */
672 continue;
673 }
674
675 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700676 continue;
677
678 msg[n] = '\0';
679 msg[n+1] = '\0';
680
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700681 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700682 parse_event(msg, &uevent);
683
684 handle_device_event(&uevent);
685 handle_firmware_event(&uevent);
686 }
687}
688
689/* Coldboot walks parts of the /sys tree and pokes the uevent files
690** to cause the kernel to regenerate device add events that happened
691** before init's device manager was started
692**
693** We drain any pending events from the netlink socket every time
694** we poke another uevent file to make sure we don't overrun the
695** socket's buffer.
696*/
697
Colin Cross0dd7ca62010-04-13 19:25:51 -0700698static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700699{
700 struct dirent *de;
701 int dfd, fd;
702
703 dfd = dirfd(d);
704
705 fd = openat(dfd, "uevent", O_WRONLY);
706 if(fd >= 0) {
707 write(fd, "add\n", 4);
708 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700709 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700710 }
711
712 while((de = readdir(d))) {
713 DIR *d2;
714
715 if(de->d_type != DT_DIR || de->d_name[0] == '.')
716 continue;
717
718 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
719 if(fd < 0)
720 continue;
721
722 d2 = fdopendir(fd);
723 if(d2 == 0)
724 close(fd);
725 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700726 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700727 closedir(d2);
728 }
729 }
730}
731
Colin Cross0dd7ca62010-04-13 19:25:51 -0700732static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700733{
734 DIR *d = opendir(path);
735 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700736 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700737 closedir(d);
738 }
739}
740
Colin Cross0dd7ca62010-04-13 19:25:51 -0700741void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700742{
743 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700744 struct stat info;
745 int fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700746
Colin Cross0dd7ca62010-04-13 19:25:51 -0700747 device_fd = open_uevent_socket();
748 if(device_fd < 0)
749 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700750
Colin Cross0dd7ca62010-04-13 19:25:51 -0700751 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
752 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700753
Colin Crossf83d0b92010-04-21 12:04:20 -0700754 if (stat(coldboot_done, &info) < 0) {
755 t0 = get_usecs();
756 coldboot("/sys/class");
757 coldboot("/sys/block");
758 coldboot("/sys/devices");
759 t1 = get_usecs();
760 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
761 close(fd);
762 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
763 } else {
764 log_event_print("skipping coldboot, already done\n");
765 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700766}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700767
Colin Cross0dd7ca62010-04-13 19:25:51 -0700768int get_device_fd()
769{
770 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700771}