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