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