blob: a805237e9bae8b2265de7931cb564e4b617452d6 [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>
34
35#include "init.h"
36#include "devices.h"
37
38#define CMDLINE_PREFIX "/dev"
39#define SYSFS_PREFIX "/sys"
40#define FIRMWARE_DIR "/etc/firmware"
41#define MAX_QEMU_PERM 6
42
43struct uevent {
44 const char *action;
45 const char *path;
46 const char *subsystem;
47 const char *firmware;
48 int major;
49 int minor;
50};
51
52static int open_uevent_socket(void)
53{
54 struct sockaddr_nl addr;
55 int sz = 64*1024; // XXX larger? udev uses 16MB!
56 int s;
57
58 memset(&addr, 0, sizeof(addr));
59 addr.nl_family = AF_NETLINK;
60 addr.nl_pid = getpid();
61 addr.nl_groups = 0xffffffff;
62
63 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
64 if(s < 0)
65 return -1;
66
67 setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
68
69 if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
70 close(s);
71 return -1;
72 }
73
74 return s;
75}
76
77struct perms_ {
78 char *name;
79 mode_t perm;
80 unsigned int uid;
81 unsigned int gid;
82 unsigned short prefix;
83};
84static struct perms_ devperms[] = {
85 { "/dev/null", 0666, AID_ROOT, AID_ROOT, 0 },
86 { "/dev/zero", 0666, AID_ROOT, AID_ROOT, 0 },
87 { "/dev/full", 0666, AID_ROOT, AID_ROOT, 0 },
88 { "/dev/ptmx", 0666, AID_ROOT, AID_ROOT, 0 },
89 { "/dev/tty", 0666, AID_ROOT, AID_ROOT, 0 },
90 { "/dev/random", 0666, AID_ROOT, AID_ROOT, 0 },
91 { "/dev/urandom", 0666, AID_ROOT, AID_ROOT, 0 },
92 { "/dev/ashmem", 0666, AID_ROOT, AID_ROOT, 0 },
93 { "/dev/binder", 0666, AID_ROOT, AID_ROOT, 0 },
94
95 /* logger should be world writable (for logging) but not readable */
96 { "/dev/log/", 0662, AID_ROOT, AID_LOG, 1 },
97
98 /* these should not be world writable */
99 { "/dev/android_adb", 0660, AID_ADB, AID_ADB, 0 },
100 { "/dev/android_adb_enable", 0660, AID_ADB, AID_ADB, 0 },
101 { "/dev/ttyMSM0", 0660, AID_BLUETOOTH, AID_BLUETOOTH, 0 },
102 { "/dev/alarm", 0664, AID_SYSTEM, AID_RADIO, 0 },
103 { "/dev/tty0", 0666, AID_ROOT, AID_SYSTEM, 0 },
104 { "/dev/graphics/", 0660, AID_ROOT, AID_GRAPHICS, 1 },
105 { "/dev/hw3d", 0660, AID_SYSTEM, AID_GRAPHICS, 0 },
106 { "/dev/input/", 0660, AID_ROOT, AID_INPUT, 1 },
107 { "/dev/eac", 0660, AID_ROOT, AID_AUDIO, 0 },
108 { "/dev/cam", 0660, AID_ROOT, AID_CAMERA, 0 },
109 { "/dev/pmem", 0660, AID_SYSTEM, AID_GRAPHICS, 0 },
110 { "/dev/pmem_gpu", 0660, AID_SYSTEM, AID_GRAPHICS, 1 },
111 { "/dev/pmem_adsp", 0660, AID_SYSTEM, AID_AUDIO, 1 },
112 { "/dev/pmem_camera", 0660, AID_SYSTEM, AID_CAMERA, 1 },
113 { "/dev/oncrpc/", 0660, AID_ROOT, AID_SYSTEM, 1 },
114 { "/dev/adsp/", 0660, AID_SYSTEM, AID_AUDIO, 1 },
115 { "/dev/mt9t013", 0660, AID_SYSTEM, AID_SYSTEM, 0 },
116 { "/dev/akm8976_daemon",0640, AID_COMPASS, AID_SYSTEM, 0 },
117 { "/dev/akm8976_aot", 0640, AID_COMPASS, AID_SYSTEM, 0 },
118 { "/dev/akm8976_pffd", 0640, AID_COMPASS, AID_SYSTEM, 0 },
119 { "/dev/msm_pcm_out", 0660, AID_SYSTEM, AID_AUDIO, 1 },
120 { "/dev/msm_pcm_in", 0660, AID_SYSTEM, AID_AUDIO, 1 },
121 { "/dev/msm_pcm_ctl", 0660, AID_SYSTEM, AID_AUDIO, 1 },
122 { "/dev/msm_mp3", 0660, AID_SYSTEM, AID_AUDIO, 1 },
Iliyan Malchev3088ff82008-10-29 16:08:18 -0700123 { "/dev/msm_snd", 0660, AID_SYSTEM, AID_AUDIO, 1 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700124 { "/dev/smd0", 0640, AID_RADIO, AID_RADIO, 0 },
125 { "/dev/qmi", 0640, AID_RADIO, AID_RADIO, 0 },
126 { "/dev/qmi0", 0640, AID_RADIO, AID_RADIO, 0 },
127 { "/dev/qmi1", 0640, AID_RADIO, AID_RADIO, 0 },
128 { "/dev/qmi2", 0640, AID_RADIO, AID_RADIO, 0 },
129 { "/dev/htc-acoustic", 0640, AID_RADIO, AID_RADIO, 0 },
130 { NULL, 0, 0, 0, 0 },
131};
132
133/* devperms_partners list and perm_node are for hardware specific /dev entries */
134struct perm_node {
135 struct perms_ dp;
136 struct listnode plist;
137};
138list_declare(devperms_partners);
139
140/*
141 * Permission override when in emulator mode, must be parsed before
142 * system properties is initalized.
143 */
144static int qemu_perm_count;
145static struct perms_ qemu_perms[MAX_QEMU_PERM + 1];
146
147int add_devperms_partners(const char *name, mode_t perm, unsigned int uid,
148 unsigned int gid, unsigned short prefix) {
149 int size;
150 struct perm_node *node = malloc(sizeof (struct perm_node));
151 if (!node)
152 return -ENOMEM;
153
154 size = strlen(name) + 1;
155 if ((node->dp.name = malloc(size)) == NULL)
156 return -ENOMEM;
157
158 memcpy(node->dp.name, name, size);
159 node->dp.perm = perm;
160 node->dp.uid = uid;
161 node->dp.gid = gid;
162 node->dp.prefix = prefix;
163
164 list_add_tail(&devperms_partners, &node->plist);
165 return 0;
166}
167
168void qemu_init(void) {
169 qemu_perm_count = 0;
170 memset(&qemu_perms, 0, sizeof(qemu_perms));
171}
172
173static int qemu_perm(const char* name, mode_t perm, unsigned int uid,
174 unsigned int gid, unsigned short prefix)
175{
176 char *buf;
177 if (qemu_perm_count == MAX_QEMU_PERM)
178 return -ENOSPC;
179
180 buf = malloc(strlen(name) + 1);
181 if (!buf)
182 return -errno;
183
184 strlcpy(buf, name, strlen(name) + 1);
185 qemu_perms[qemu_perm_count].name = buf;
186 qemu_perms[qemu_perm_count].perm = perm;
187 qemu_perms[qemu_perm_count].uid = uid;
188 qemu_perms[qemu_perm_count].gid = gid;
189 qemu_perms[qemu_perm_count].prefix = prefix;
190
191 qemu_perm_count++;
192 return 0;
193}
194
195/* Permission overrides for emulator that are parsed from /proc/cmdline. */
196void qemu_cmdline(const char* name, const char *value)
197{
198 char *buf;
199 if (!strcmp(name, "android.ril")) {
200 /* cmd line params currently assume /dev/ prefix */
201 if (asprintf(&buf, CMDLINE_PREFIX"/%s", value) == -1) {
202 return;
203 }
204 INFO("nani- buf:: %s\n", buf);
205 qemu_perm(buf, 0660, AID_RADIO, AID_ROOT, 0);
206 }
207}
208
209static int get_device_perm_inner(struct perms_ *perms, const char *path,
210 unsigned *uid, unsigned *gid, mode_t *perm)
211{
212 int i;
213 for(i = 0; perms[i].name; i++) {
214
215 if(perms[i].prefix) {
216 if(strncmp(path, perms[i].name, strlen(perms[i].name)))
217 continue;
218 } else {
219 if(strcmp(path, perms[i].name))
220 continue;
221 }
222 *uid = perms[i].uid;
223 *gid = perms[i].gid;
224 *perm = perms[i].perm;
225 return 0;
226 }
227 return -1;
228}
229
230/* First checks for emulator specific permissions specified in /proc/cmdline. */
231static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
232{
233 mode_t perm;
234
235 if (get_device_perm_inner(qemu_perms, path, uid, gid, &perm) == 0) {
236 return perm;
237 } else if (get_device_perm_inner(devperms, path, uid, gid, &perm) == 0) {
238 return perm;
239 } else {
240 struct listnode *node;
241 struct perm_node *perm_node;
242 struct perms_ *dp;
243
244 /* Check partners list. */
245 list_for_each(node, &devperms_partners) {
246 perm_node = node_to_item(node, struct perm_node, plist);
247 dp = &perm_node->dp;
248
249 if (dp->prefix) {
250 if (strncmp(path, dp->name, strlen(dp->name)))
251 continue;
252 } else {
253 if (strcmp(path, dp->name))
254 continue;
255 }
256 /* Found perm in partner list. */
257 *uid = dp->uid;
258 *gid = dp->gid;
259 return dp->perm;
260 }
261 /* Default if nothing found. */
262 *uid = 0;
263 *gid = 0;
264 return 0600;
265 }
266}
267
268static void make_device(const char *path, int block, int major, int minor)
269{
270 unsigned uid;
271 unsigned gid;
272 mode_t mode;
273 dev_t dev;
274
275 if(major > 255 || minor > 255)
276 return;
277
278 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
279 dev = (major << 8) | minor;
280 mknod(path, mode, dev);
281 chown(path, uid, gid);
282}
283
284#ifdef LOG_UEVENTS
285
286static inline suseconds_t get_usecs(void)
287{
288 struct timeval tv;
289 gettimeofday(&tv, 0);
290 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
291}
292
293#define log_event_print(x...) INFO(x)
294
295#else
296
297#define log_event_print(fmt, args...) do { } while (0)
298#define get_usecs() 0
299
300#endif
301
302static void parse_event(const char *msg, struct uevent *uevent)
303{
304 uevent->action = "";
305 uevent->path = "";
306 uevent->subsystem = "";
307 uevent->firmware = "";
308 uevent->major = -1;
309 uevent->minor = -1;
310
311 /* currently ignoring SEQNUM */
312 while(*msg) {
313 if(!strncmp(msg, "ACTION=", 7)) {
314 msg += 7;
315 uevent->action = msg;
316 } else if(!strncmp(msg, "DEVPATH=", 8)) {
317 msg += 8;
318 uevent->path = msg;
319 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
320 msg += 10;
321 uevent->subsystem = msg;
322 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
323 msg += 9;
324 uevent->firmware = msg;
325 } else if(!strncmp(msg, "MAJOR=", 6)) {
326 msg += 6;
327 uevent->major = atoi(msg);
328 } else if(!strncmp(msg, "MINOR=", 6)) {
329 msg += 6;
330 uevent->minor = atoi(msg);
331 }
332
333 /* advance to after the next \0 */
334 while(*msg++)
335 ;
336 }
337
338 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
339 uevent->action, uevent->path, uevent->subsystem,
340 uevent->firmware, uevent->major, uevent->minor);
341}
342
343static void handle_device_event(struct uevent *uevent)
344{
345 char devpath[96];
346 char *base, *name;
347 int block;
348
349 /* if it's not a /dev device, nothing to do */
350 if((uevent->major < 0) || (uevent->minor < 0))
351 return;
352
353 /* do we have a name? */
354 name = strrchr(uevent->path, '/');
355 if(!name)
356 return;
357 name++;
358
359 /* too-long names would overrun our buffer */
360 if(strlen(name) > 64)
361 return;
362
363 /* are we block or char? where should we live? */
364 if(!strncmp(uevent->path, "/block", 6)) {
365 block = 1;
366 base = "/dev/block/";
367 mkdir(base, 0755);
368 } else {
369 block = 0;
370 /* this should probably be configurable somehow */
371 if(!strncmp(uevent->path, "/class/graphics/", 16)) {
372 base = "/dev/graphics/";
373 mkdir(base, 0755);
374 } else if (!strncmp(uevent->path, "/class/oncrpc/", 14)) {
375 base = "/dev/oncrpc/";
376 mkdir(base, 0755);
377 } else if (!strncmp(uevent->path, "/class/adsp/", 12)) {
378 base = "/dev/adsp/";
379 mkdir(base, 0755);
380 } else if(!strncmp(uevent->path, "/class/input/", 13)) {
381 base = "/dev/input/";
382 mkdir(base, 0755);
383 } else if(!strncmp(uevent->path, "/class/mtd/", 11)) {
384 base = "/dev/mtd/";
385 mkdir(base, 0755);
386 } else if(!strncmp(uevent->path, "/class/misc/", 12) &&
387 !strncmp(name, "log_", 4)) {
388 base = "/dev/log/";
389 mkdir(base, 0755);
390 name += 4;
391 } else
392 base = "/dev/";
393 }
394
395 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
396
397 if(!strcmp(uevent->action, "add")) {
398 make_device(devpath, block, uevent->major, uevent->minor);
399 return;
400 }
401
402 if(!strcmp(uevent->action, "remove")) {
403 unlink(devpath);
404 return;
405 }
406}
407
408static int load_firmware(int fw_fd, int loading_fd, int data_fd)
409{
410 struct stat st;
411 long len_to_copy;
412 int ret = 0;
413
414 if(fstat(fw_fd, &st) < 0)
415 return -1;
416 len_to_copy = st.st_size;
417
418 write(loading_fd, "1", 1); /* start transfer */
419
420 while (len_to_copy > 0) {
421 char buf[PAGE_SIZE];
422 ssize_t nr;
423
424 nr = read(fw_fd, buf, sizeof(buf));
425 if(!nr)
426 break;
427 if(nr < 0) {
428 ret = -1;
429 break;
430 }
431
432 len_to_copy -= nr;
433 while (nr > 0) {
434 ssize_t nw = 0;
435
436 nw = write(data_fd, buf + nw, nr);
437 if(nw <= 0) {
438 ret = -1;
439 goto out;
440 }
441 nr -= nw;
442 }
443 }
444
445out:
446 if(!ret)
447 write(loading_fd, "0", 1); /* successful end of transfer */
448 else
449 write(loading_fd, "-1", 2); /* abort transfer */
450
451 return ret;
452}
453
454static void process_firmware_event(struct uevent *uevent)
455{
456 char *root, *loading, *data, *file;
457 int l, loading_fd, data_fd, fw_fd;
458
459 log_event_print("firmware event { '%s', '%s' }\n",
460 uevent->path, uevent->firmware);
461
462 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
463 if (l == -1)
464 return;
465
466 l = asprintf(&loading, "%sloading", root);
467 if (l == -1)
468 goto root_free_out;
469
470 l = asprintf(&data, "%sdata", root);
471 if (l == -1)
472 goto loading_free_out;
473
474 l = asprintf(&file, FIRMWARE_DIR"/%s", uevent->firmware);
475 if (l == -1)
476 goto data_free_out;
477
478 loading_fd = open(loading, O_WRONLY);
479 if(loading_fd < 0)
480 goto file_free_out;
481
482 data_fd = open(data, O_WRONLY);
483 if(data_fd < 0)
484 goto loading_close_out;
485
486 fw_fd = open(file, O_RDONLY);
487 if(fw_fd < 0)
488 goto data_close_out;
489
490 if(!load_firmware(fw_fd, loading_fd, data_fd))
491 log_event_print("firmware copy success { '%s', '%s' }\n", root, file);
492 else
493 log_event_print("firmware copy failure { '%s', '%s' }\n", root, file);
494
495 close(fw_fd);
496data_close_out:
497 close(data_fd);
498loading_close_out:
499 close(loading_fd);
500file_free_out:
501 free(file);
502data_free_out:
503 free(data);
504loading_free_out:
505 free(loading);
506root_free_out:
507 free(root);
508}
509
510static void handle_firmware_event(struct uevent *uevent)
511{
512 pid_t pid;
513
514 if(strcmp(uevent->subsystem, "firmware"))
515 return;
516
517 if(strcmp(uevent->action, "add"))
518 return;
519
520 /* we fork, to avoid making large memory allocations in init proper */
521 pid = fork();
522 if (!pid) {
523 process_firmware_event(uevent);
524 exit(EXIT_SUCCESS);
525 }
526}
527
528#define UEVENT_MSG_LEN 1024
529void handle_device_fd(int fd)
530{
531 char msg[UEVENT_MSG_LEN+2];
532 int n;
533
534 while((n = recv(fd, msg, UEVENT_MSG_LEN, 0)) > 0) {
535 struct uevent uevent;
536
537 if(n == UEVENT_MSG_LEN) /* overflow -- discard */
538 continue;
539
540 msg[n] = '\0';
541 msg[n+1] = '\0';
542
543 parse_event(msg, &uevent);
544
545 handle_device_event(&uevent);
546 handle_firmware_event(&uevent);
547 }
548}
549
550/* Coldboot walks parts of the /sys tree and pokes the uevent files
551** to cause the kernel to regenerate device add events that happened
552** before init's device manager was started
553**
554** We drain any pending events from the netlink socket every time
555** we poke another uevent file to make sure we don't overrun the
556** socket's buffer.
557*/
558
559static void do_coldboot(int event_fd, DIR *d)
560{
561 struct dirent *de;
562 int dfd, fd;
563
564 dfd = dirfd(d);
565
566 fd = openat(dfd, "uevent", O_WRONLY);
567 if(fd >= 0) {
568 write(fd, "add\n", 4);
569 close(fd);
570 handle_device_fd(event_fd);
571 }
572
573 while((de = readdir(d))) {
574 DIR *d2;
575
576 if(de->d_type != DT_DIR || de->d_name[0] == '.')
577 continue;
578
579 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
580 if(fd < 0)
581 continue;
582
583 d2 = fdopendir(fd);
584 if(d2 == 0)
585 close(fd);
586 else {
587 do_coldboot(event_fd, d2);
588 closedir(d2);
589 }
590 }
591}
592
593static void coldboot(int event_fd, const char *path)
594{
595 DIR *d = opendir(path);
596 if(d) {
597 do_coldboot(event_fd, d);
598 closedir(d);
599 }
600}
601
602int device_init(void)
603{
604 suseconds_t t0, t1;
605 int fd;
606
607 fd = open_uevent_socket();
608 if(fd < 0)
609 return -1;
610
611 fcntl(fd, F_SETFD, FD_CLOEXEC);
612 fcntl(fd, F_SETFL, O_NONBLOCK);
613
614 t0 = get_usecs();
615 coldboot(fd, "/sys/class");
616 coldboot(fd, "/sys/block");
617 coldboot(fd, "/sys/devices");
618 t1 = get_usecs();
619
620 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
621
622 return fd;
623}