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