blob: e73efdf03b17feec87e96cf3cd7432ea8445b0dc [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
Colin Crossb0ab94b2010-04-08 16:16:20 -0700281static char **parse_platform_block_device(struct uevent *uevent)
282{
283 const char *driver;
284 const char *path;
285 char *slash;
286 int width;
287 char buf[256];
288 char link_path[256];
289 int fd;
290 int link_num = 0;
291 int ret;
292 char *p;
293 unsigned int size;
294 struct stat info;
295
296 char **links = malloc(sizeof(char *) * 4);
297 if (!links)
298 return NULL;
299 memset(links, 0, sizeof(char *) * 4);
300
301 /* Drop "/devices/platform/" */
302 path = uevent->path;
303 driver = path + 18;
304 slash = strchr(driver, '/');
305 if (!slash)
306 goto err;
307 width = slash - driver;
308 if (width <= 0)
309 goto err;
310
311 snprintf(link_path, sizeof(link_path), "/dev/block/platform/%.*s",
312 width, driver);
313
314 if (uevent->partition_name) {
315 p = strdup(uevent->partition_name);
316 sanitize(p);
317 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
318 link_num++;
319 else
320 links[link_num] = NULL;
321 free(p);
322 }
323
324 if (uevent->partition_num >= 0) {
325 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
326 link_num++;
327 else
328 links[link_num] = NULL;
329 }
330
331 slash = strrchr(path, '/');
332 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
333 link_num++;
334 else
335 links[link_num] = NULL;
336
337 return links;
338
339err:
340 free(links);
341 return NULL;
342}
343
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700344static void handle_device_event(struct uevent *uevent)
345{
346 char devpath[96];
Mike Lockwood93ac1552010-05-06 13:30:09 -0400347 int devpath_ready = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348 char *base, *name;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700349 char **links = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700350 int block;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700351 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700352
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700353 if (!strcmp(uevent->action,"add"))
354 fixup_sys_perms(uevent->path);
355
356 /* if it's not a /dev device, nothing else to do */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700357 if((uevent->major < 0) || (uevent->minor < 0))
358 return;
359
360 /* do we have a name? */
361 name = strrchr(uevent->path, '/');
362 if(!name)
363 return;
364 name++;
365
366 /* too-long names would overrun our buffer */
367 if(strlen(name) > 64)
368 return;
369
370 /* are we block or char? where should we live? */
The Android Open Source Project35237d12008-12-17 18:08:08 -0800371 if(!strncmp(uevent->subsystem, "block", 5)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372 block = 1;
373 base = "/dev/block/";
374 mkdir(base, 0755);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700375 if (!strncmp(uevent->path, "/devices/platform/", 18))
376 links = parse_platform_block_device(uevent);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377 } else {
378 block = 0;
379 /* this should probably be configurable somehow */
Mike Lockwood93ac1552010-05-06 13:30:09 -0400380 if (!strncmp(uevent->subsystem, "usb", 3)) {
381 if (!strcmp(uevent->subsystem, "usb")) {
382 /* This imitates the file system that would be created
383 * if we were using devfs instead.
384 * Minors are broken up into groups of 128, starting at "001"
385 */
386 int bus_id = uevent->minor / 128 + 1;
387 int device_id = uevent->minor % 128 + 1;
388 /* build directories */
389 mkdir("/dev/bus", 0755);
390 mkdir("/dev/bus/usb", 0755);
391 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
392 mkdir(devpath, 0755);
393 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
394 devpath_ready = 1;
395 } else {
396 /* ignore other USB events */
397 return;
398 }
399 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700400 base = "/dev/graphics/";
401 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800402 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700403 base = "/dev/oncrpc/";
404 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800405 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700406 base = "/dev/adsp/";
407 mkdir(base, 0755);
Iliyan Malchevfc0182e2009-05-01 10:25:05 -0700408 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
409 base = "/dev/msm_camera/";
410 mkdir(base, 0755);
411 } else if(!strncmp(uevent->subsystem, "input", 5)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700412 base = "/dev/input/";
413 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800414 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415 base = "/dev/mtd/";
416 mkdir(base, 0755);
Xiaopeng Yang5bb44c82008-11-25 16:20:07 -0800417 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
418 base = "/dev/snd/";
419 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800420 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700421 !strncmp(name, "log_", 4)) {
422 base = "/dev/log/";
423 mkdir(base, 0755);
424 name += 4;
425 } else
426 base = "/dev/";
427 }
428
Mike Lockwood93ac1552010-05-06 13:30:09 -0400429 if (!devpath_ready)
430 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431
432 if(!strcmp(uevent->action, "add")) {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700433 make_device(devpath, uevent->path, block, uevent->major, uevent->minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700434 if (links) {
435 for (i = 0; links[i]; i++)
436 make_link(devpath, links[i]);
437 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700438 }
439
440 if(!strcmp(uevent->action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700441 if (links) {
442 for (i = 0; links[i]; i++)
443 remove_link(devpath, links[i]);
444 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700445 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700446 }
447
448 if (links) {
449 for (i = 0; links[i]; i++)
450 free(links[i]);
451 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700452 }
453}
454
455static int load_firmware(int fw_fd, int loading_fd, int data_fd)
456{
457 struct stat st;
458 long len_to_copy;
459 int ret = 0;
460
461 if(fstat(fw_fd, &st) < 0)
462 return -1;
463 len_to_copy = st.st_size;
464
465 write(loading_fd, "1", 1); /* start transfer */
466
467 while (len_to_copy > 0) {
468 char buf[PAGE_SIZE];
469 ssize_t nr;
470
471 nr = read(fw_fd, buf, sizeof(buf));
472 if(!nr)
473 break;
474 if(nr < 0) {
475 ret = -1;
476 break;
477 }
478
479 len_to_copy -= nr;
480 while (nr > 0) {
481 ssize_t nw = 0;
482
483 nw = write(data_fd, buf + nw, nr);
484 if(nw <= 0) {
485 ret = -1;
486 goto out;
487 }
488 nr -= nw;
489 }
490 }
491
492out:
493 if(!ret)
494 write(loading_fd, "0", 1); /* successful end of transfer */
495 else
496 write(loading_fd, "-1", 2); /* abort transfer */
497
498 return ret;
499}
500
501static void process_firmware_event(struct uevent *uevent)
502{
Brian Swetland02863b92010-09-19 03:36:39 -0700503 char *root, *loading, *data, *file1 = NULL, *file2 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700504 int l, loading_fd, data_fd, fw_fd;
505
506 log_event_print("firmware event { '%s', '%s' }\n",
507 uevent->path, uevent->firmware);
508
509 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
510 if (l == -1)
511 return;
512
513 l = asprintf(&loading, "%sloading", root);
514 if (l == -1)
515 goto root_free_out;
516
517 l = asprintf(&data, "%sdata", root);
518 if (l == -1)
519 goto loading_free_out;
520
Brian Swetland02863b92010-09-19 03:36:39 -0700521 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
522 if (l == -1)
523 goto data_free_out;
524
525 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700526 if (l == -1)
527 goto data_free_out;
528
529 loading_fd = open(loading, O_WRONLY);
530 if(loading_fd < 0)
531 goto file_free_out;
532
533 data_fd = open(data, O_WRONLY);
534 if(data_fd < 0)
535 goto loading_close_out;
536
Brian Swetland02863b92010-09-19 03:36:39 -0700537 fw_fd = open(file1, O_RDONLY);
538 if(fw_fd < 0) {
539 fw_fd = open(file2, O_RDONLY);
540 if(fw_fd < 0)
541 goto data_close_out;
542 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700543
544 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland02863b92010-09-19 03:36:39 -0700545 log_event_print("firmware copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700546 else
Brian Swetland02863b92010-09-19 03:36:39 -0700547 log_event_print("firmware copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700548
549 close(fw_fd);
550data_close_out:
551 close(data_fd);
552loading_close_out:
553 close(loading_fd);
554file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700555 free(file1);
556 free(file2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700557data_free_out:
558 free(data);
559loading_free_out:
560 free(loading);
561root_free_out:
562 free(root);
563}
564
565static void handle_firmware_event(struct uevent *uevent)
566{
567 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700568 int status;
569 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700570
571 if(strcmp(uevent->subsystem, "firmware"))
572 return;
573
574 if(strcmp(uevent->action, "add"))
575 return;
576
577 /* we fork, to avoid making large memory allocations in init proper */
578 pid = fork();
579 if (!pid) {
580 process_firmware_event(uevent);
581 exit(EXIT_SUCCESS);
Colin Cross982a8152010-06-03 12:21:01 -0700582 } else {
583 do {
584 ret = waitpid(pid, &status, 0);
585 } while (ret == -1 && errno == EINTR);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700586 }
587}
588
589#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700590void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700591{
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700592 for(;;) {
593 char msg[UEVENT_MSG_LEN+2];
594 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
595 struct iovec iov = {msg, sizeof(msg)};
596 struct sockaddr_nl snl;
597 struct msghdr hdr = {&snl, sizeof(snl), &iov, 1, cred_msg, sizeof(cred_msg), 0};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700598
Nick Kralevichfad72042010-07-19 15:53:05 -0700599 ssize_t n = recvmsg(device_fd, &hdr, 0);
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700600 if (n <= 0) {
601 break;
602 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700603
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700604 if ((snl.nl_groups != 1) || (snl.nl_pid != 0)) {
605 /* ignoring non-kernel netlink multicast message */
606 continue;
607 }
608
609 struct cmsghdr * cmsg = CMSG_FIRSTHDR(&hdr);
610 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
611 /* no sender credentials received, ignore message */
612 continue;
613 }
614
615 struct ucred * cred = (struct ucred *)CMSG_DATA(cmsg);
616 if (cred->uid != 0) {
617 /* message from non-root user, ignore */
618 continue;
619 }
620
621 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700622 continue;
623
624 msg[n] = '\0';
625 msg[n+1] = '\0';
626
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700627 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700628 parse_event(msg, &uevent);
629
630 handle_device_event(&uevent);
631 handle_firmware_event(&uevent);
632 }
633}
634
635/* Coldboot walks parts of the /sys tree and pokes the uevent files
636** to cause the kernel to regenerate device add events that happened
637** before init's device manager was started
638**
639** We drain any pending events from the netlink socket every time
640** we poke another uevent file to make sure we don't overrun the
641** socket's buffer.
642*/
643
Colin Cross0dd7ca62010-04-13 19:25:51 -0700644static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700645{
646 struct dirent *de;
647 int dfd, fd;
648
649 dfd = dirfd(d);
650
651 fd = openat(dfd, "uevent", O_WRONLY);
652 if(fd >= 0) {
653 write(fd, "add\n", 4);
654 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700655 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700656 }
657
658 while((de = readdir(d))) {
659 DIR *d2;
660
661 if(de->d_type != DT_DIR || de->d_name[0] == '.')
662 continue;
663
664 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
665 if(fd < 0)
666 continue;
667
668 d2 = fdopendir(fd);
669 if(d2 == 0)
670 close(fd);
671 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700672 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700673 closedir(d2);
674 }
675 }
676}
677
Colin Cross0dd7ca62010-04-13 19:25:51 -0700678static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700679{
680 DIR *d = opendir(path);
681 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700682 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700683 closedir(d);
684 }
685}
686
Colin Cross0dd7ca62010-04-13 19:25:51 -0700687void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700688{
689 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700690 struct stat info;
691 int fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700692
Colin Cross0dd7ca62010-04-13 19:25:51 -0700693 device_fd = open_uevent_socket();
694 if(device_fd < 0)
695 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700696
Colin Cross0dd7ca62010-04-13 19:25:51 -0700697 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
698 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700699
Colin Crossf83d0b92010-04-21 12:04:20 -0700700 if (stat(coldboot_done, &info) < 0) {
701 t0 = get_usecs();
702 coldboot("/sys/class");
703 coldboot("/sys/block");
704 coldboot("/sys/devices");
705 t1 = get_usecs();
706 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
707 close(fd);
708 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
709 } else {
710 log_event_print("skipping coldboot, already done\n");
711 }
Colin Cross0dd7ca62010-04-13 19:25:51 -0700712}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700713
Colin Cross0dd7ca62010-04-13 19:25:51 -0700714int get_device_fd()
715{
716 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700717}