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