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