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