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