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