devices.cpp revision ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473
14f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/*
24f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
34f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *
44f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
54f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * you may not use this file except in compliance with the License.
64f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * You may obtain a copy of the License at
74f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *
84f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
94f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project *
104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * See the License for the specific language governing permissions and
144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project * limitations under the License.
154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project */
164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <errno.h>
18b93e5812faffd3b6c5fb349072413aace31918d8Olivier Bailly#include <stddef.h>
194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdio.h>
204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <stdlib.h>
214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/stat.h>
224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/types.h>
234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <fcntl.h>
254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <dirent.h>
264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <unistd.h>
274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <string.h>
284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/socket.h>
304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/un.h>
314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <linux/netlink.h>
32e46f9d510db9351682cf17c49115110870147335Stephen Smalley
33e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
34e46f9d510db9351682cf17c49115110870147335Stephen Smalley#include <selinux/selinux.h>
35e46f9d510db9351682cf17c49115110870147335Stephen Smalley#include <selinux/label.h>
36ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley#include <selinux/android.h>
37e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
38e46f9d510db9351682cf17c49115110870147335Stephen Smalley
394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <private/android_filesystem_config.h>
404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <sys/time.h>
414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include <asm/page.h>
42982a815c10cc28707d81c46112fc45b1c10df3beColin Cross#include <sys/wait.h>
434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
44da04c52ab1036048520fca265cf02b61dca789e0Dima Zavin#include <cutils/list.h>
453f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang#include <cutils/uevent.h>
463f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang
474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#include "devices.h"
48b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross#include "util.h"
49ed8a7d84428ec945c48b6b53dc5a3a18fabaf683Colin Cross#include "log.h"
504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define SYSFS_PREFIX    "/sys"
5202863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland#define FIRMWARE_DIR1   "/etc/firmware"
5302863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland#define FIRMWARE_DIR2   "/vendor/firmware"
544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
55e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
56e096e36e50b4b66638ebc4d3c09c2ee35f538dfaStephen Smalleyextern struct selabel_handle *sehandle;
57e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
58e46f9d510db9351682cf17c49115110870147335Stephen Smalley
590dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic int device_fd = -1;
600dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross
614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct uevent {
624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *action;
634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *path;
644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *subsystem;
654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *firmware;
66b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    const char *partition_name;
67f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong    const char *device_name;
68b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int partition_num;
694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int major;
704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int minor;
714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perms_ {
744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    char *name;
75bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    char *attr;
764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t perm;
774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned int uid;
784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned int gid;
794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned short prefix;
804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perm_node {
834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct perms_ dp;
844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct listnode plist;
854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
86bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
87fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstruct platform_node {
88fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    char *name;
89fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    int name_len;
90fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode list;
91fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross};
92fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
93bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic list_declare(sys_perms);
9444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic list_declare(dev_perms);
95fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic list_declare(platform_names);
964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
97bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandint add_dev_perms(const char *name, const char *attr,
98bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                  mode_t perm, unsigned int uid, unsigned int gid,
99bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                  unsigned short prefix) {
100bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct perm_node *node = calloc(1, sizeof(*node));
1014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!node)
1024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -ENOMEM;
1034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
104bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    node->dp.name = strdup(name);
105bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (!node->dp.name)
1064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -ENOMEM;
1074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
108bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (attr) {
109bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        node->dp.attr = strdup(attr);
110bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if (!node->dp.attr)
111bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            return -ENOMEM;
112bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    }
113bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
1144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.perm = perm;
1154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.uid = uid;
1164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.gid = gid;
1174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.prefix = prefix;
1184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
119bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (attr)
120bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        list_add_tail(&sys_perms, &node->plist);
121bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    else
122bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        list_add_tail(&dev_perms, &node->plist);
123bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
1244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
1254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
127bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandvoid fixup_sys_perms(const char *upath)
1284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
129bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    char buf[512];
130bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct listnode *node;
131bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct perms_ *dp;
1324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
133bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        /* upaths omit the "/sys" that paths in this list
134bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland         * contain, so we add 4 when comparing...
135bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland         */
136bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    list_for_each(node, &sys_perms) {
137bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        dp = &(node_to_item(node, struct perm_node, plist))->dp;
138bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if (dp->prefix) {
139bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
1404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                continue;
1414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else {
142bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            if (strcmp(upath, dp->name + 4))
1434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                continue;
1444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
145bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
146bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
147bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            return;
148bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
149bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        sprintf(buf,"/sys%s/%s", upath, dp->attr);
150bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
151bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        chown(buf, dp->uid, dp->gid);
152bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        chmod(buf, dp->perm);
1534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
1544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
1574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t perm;
15944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct listnode *node;
16044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct perm_node *perm_node;
16144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct perms_ *dp;
16244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
16344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    /* search the perms list in reverse so that ueventd.$hardware can
16444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross     * override ueventd.rc
16544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross     */
16644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    list_for_each_reverse(node, &dev_perms) {
16744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        perm_node = node_to_item(node, struct perm_node, plist);
16844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        dp = &perm_node->dp;
16944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
17044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        if (dp->prefix) {
17144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            if (strncmp(path, dp->name, strlen(dp->name)))
17244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross                continue;
17344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        } else {
17444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            if (strcmp(path, dp->name))
17544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross                continue;
1764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
17744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        *uid = dp->uid;
17844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        *gid = dp->gid;
17944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        return dp->perm;
1804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
18144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    /* Default if nothing found. */
18244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    *uid = 0;
18344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    *gid = 0;
18444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    return 0600;
1854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
187bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic void make_device(const char *path,
188bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                        const char *upath,
189bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                        int block, int major, int minor)
1904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned uid;
1924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned gid;
1934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t mode;
1944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    dev_t dev;
195e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
196e46f9d510db9351682cf17c49115110870147335Stephen Smalley    char *secontext = NULL;
197e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
1984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
200e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
201e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (sehandle) {
202e46f9d510db9351682cf17c49115110870147335Stephen Smalley        selabel_lookup(sehandle, &secontext, path, mode);
203e46f9d510db9351682cf17c49115110870147335Stephen Smalley        setfscreatecon(secontext);
204e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
205e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
20617dcc5c57dcffb919f3de65ff7a0134ffa3bd874Colin Cross    dev = makedev(major, minor);
2076405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    /* Temporarily change egid to avoid race condition setting the gid of the
2086405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * device node. Unforunately changing the euid would prevent creation of
2096405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * some device nodes, so the uid has to be set with chown() and is still
2106405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * racy. Fixing the gid race at least fixed the issue with system_server
2116405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * opening dynamic input devices under the AID_INPUT gid. */
2126405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    setegid(gid);
2134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mknod(path, mode, dev);
2146405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    chown(path, uid, -1);
2156405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    setegid(AID_ROOT);
216e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
217e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (secontext) {
218e46f9d510db9351682cf17c49115110870147335Stephen Smalley        freecon(secontext);
219e46f9d510db9351682cf17c49115110870147335Stephen Smalley        setfscreatecon(NULL);
220e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
221e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
222e46f9d510db9351682cf17c49115110870147335Stephen Smalley}
223e46f9d510db9351682cf17c49115110870147335Stephen Smalley
224fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void add_platform_device(const char *name)
225fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
226fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    int name_len = strlen(name);
227fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
228fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
229fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
230fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
231fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
232fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        if ((bus->name_len < name_len) &&
233fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                (name[bus->name_len] == '/') &&
234fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                !strncmp(name, bus->name, bus->name_len))
235fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            /* subdevice of an existing platform, ignore it */
236fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            return;
237fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
238fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
239fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    INFO("adding platform device %s\n", name);
240fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
241fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    bus = calloc(1, sizeof(struct platform_node));
242fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    bus->name = strdup(name);
243fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    bus->name_len = name_len;
244fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_add_tail(&platform_names, &bus->list);
245fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
246fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
247fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross/*
248fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * given a name that may start with a platform device, find the length of the
249fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * platform device prefix.  If it doesn't start with a platform device, return
250fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * 0.
251fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross */
252fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic const char *find_platform_device(const char *name)
253fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
254fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    int name_len = strlen(name);
255fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
256fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
257fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
258fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
259fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
260fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        if ((bus->name_len < name_len) &&
261fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                (name[bus->name_len] == '/') &&
262fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                !strncmp(name, bus->name, bus->name_len))
263fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            return bus->name;
264fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
265fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
266fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    return NULL;
267fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
268fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
269fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void remove_platform_device(const char *name)
270fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
271fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
272fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
273fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
274fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
275fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
276fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        if (!strcmp(name, bus->name)) {
277fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            INFO("removing platform device %s\n", name);
278fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            free(bus->name);
279fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            list_remove(node);
280fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            free(bus);
281fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            return;
282fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        }
283fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
284fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
285fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
2861e070846f8908dff15086efa12fbba01ab7eae15Chuck Tuffli#if LOG_UEVENTS
2874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic inline suseconds_t get_usecs(void)
2894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct timeval tv;
2914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    gettimeofday(&tv, 0);
2924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
2934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(x...) INFO(x)
2964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#else
2984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(fmt, args...)   do { } while (0)
3004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define get_usecs()                     0
3014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#endif
3034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void parse_event(const char *msg, struct uevent *uevent)
3054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
3064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->action = "";
3074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->path = "";
3084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->subsystem = "";
3094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->firmware = "";
3104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->major = -1;
3114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->minor = -1;
312b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    uevent->partition_name = NULL;
313b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    uevent->partition_num = -1;
314f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong    uevent->device_name = NULL;
3154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        /* currently ignoring SEQNUM */
3174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while(*msg) {
3184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(!strncmp(msg, "ACTION=", 7)) {
3194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 7;
3204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->action = msg;
3214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "DEVPATH=", 8)) {
3224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 8;
3234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->path = msg;
3244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
3254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 10;
3264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->subsystem = msg;
3274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "FIRMWARE=", 9)) {
3284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 9;
3294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->firmware = msg;
3304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "MAJOR=", 6)) {
3314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 6;
3324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->major = atoi(msg);
3334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "MINOR=", 6)) {
3344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 6;
3354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->minor = atoi(msg);
336b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        } else if(!strncmp(msg, "PARTN=", 6)) {
337b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            msg += 6;
338b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            uevent->partition_num = atoi(msg);
339b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        } else if(!strncmp(msg, "PARTNAME=", 9)) {
340b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            msg += 9;
341b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            uevent->partition_name = msg;
342f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong        } else if(!strncmp(msg, "DEVNAME=", 8)) {
343f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong            msg += 8;
344f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong            uevent->device_name = msg;
3454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
3464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
347f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong        /* advance to after the next \0 */
3484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        while(*msg++)
3494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ;
3504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
3514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
3534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                    uevent->action, uevent->path, uevent->subsystem,
3544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                    uevent->firmware, uevent->major, uevent->minor);
3554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
3564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
357d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobystatic char **get_character_device_symlinks(struct uevent *uevent)
358d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby{
359d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    const char *parent;
360d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    char *slash;
361d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    char **links;
362d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    int link_num = 0;
363d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    int width;
364d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
365d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (strncmp(uevent->path, "/devices/platform/", 18))
366d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        return NULL;
367d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
368d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    links = malloc(sizeof(char *) * 2);
369d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (!links)
370d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        return NULL;
371d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    memset(links, 0, sizeof(char *) * 2);
372d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
373d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    /* skip "/devices/platform/<driver>" */
374d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    parent = strchr(uevent->path + 18, '/');
375d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (!*parent)
376d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        goto err;
377d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
378d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (!strncmp(parent, "/usb", 4)) {
379d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        /* skip root hub name and device. use device interface */
380d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        while (*++parent && *parent != '/');
381d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (*parent)
382d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            while (*++parent && *parent != '/');
383d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (!*parent)
384d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
385d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        slash = strchr(++parent, '/');
386d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (!slash)
387d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
388d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        width = slash - parent;
389d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (width <= 0)
390d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
391d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
392d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
393d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            link_num++;
394d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        else
395d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            links[link_num] = NULL;
396d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        mkdir("/dev/usb", 0755);
397d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    }
398d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    else {
399d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        goto err;
400d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    }
401d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
402d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    return links;
403d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobyerr:
404d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    free(links);
405d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    return NULL;
406d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby}
407d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
408b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crossstatic char **parse_platform_block_device(struct uevent *uevent)
409b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross{
410fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    const char *device;
411b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    const char *path;
412b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char *slash;
413b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int width;
414b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char buf[256];
415b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char link_path[256];
416b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int fd;
417b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int link_num = 0;
418b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int ret;
419b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char *p;
420b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    unsigned int size;
421b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    struct stat info;
422b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
423b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char **links = malloc(sizeof(char *) * 4);
424b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (!links)
425b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        return NULL;
426b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    memset(links, 0, sizeof(char *) * 4);
427b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
428b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    /* Drop "/devices/platform/" */
429b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    path = uevent->path;
430fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    device = path + 18;
431fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    device = find_platform_device(device);
432fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    if (!device)
433b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        goto err;
434b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
435fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    INFO("found platform device %s\n", device);
436fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
437fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
438b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
439b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (uevent->partition_name) {
440b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        p = strdup(uevent->partition_name);
441b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        sanitize(p);
442b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
443b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            link_num++;
444b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        else
445b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            links[link_num] = NULL;
446b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        free(p);
447b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
448b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
449b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (uevent->partition_num >= 0) {
450b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
451b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            link_num++;
452b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        else
453b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            links[link_num] = NULL;
454b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
455b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
456b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    slash = strrchr(path, '/');
457b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
458b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        link_num++;
459b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    else
460b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        links[link_num] = NULL;
461b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
462b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    return links;
463b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
464b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crosserr:
465b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    free(links);
466b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    return NULL;
467b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross}
468b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
469eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device(const char *action, const char *devpath,
470eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        const char *path, int block, int major, int minor, char **links)
4714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
472b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int i;
4734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
474eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!strcmp(action, "add")) {
475eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        make_device(devpath, path, block, major, minor);
476b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (links) {
477b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            for (i = 0; links[i]; i++)
478b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross                make_link(devpath, links[i]);
479b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        }
4804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
4814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
482eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!strcmp(action, "remove")) {
483b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (links) {
484b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            for (i = 0; links[i]; i++)
485b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross                remove_link(devpath, links[i]);
486b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        }
4874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        unlink(devpath);
488b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
489b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
490b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (links) {
491b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        for (i = 0; links[i]; i++)
492b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            free(links[i]);
493b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        free(links);
4944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
4954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
4964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
497fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void handle_platform_device_event(struct uevent *uevent)
498fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
499fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    const char *name = uevent->path + 18; /* length of /devices/platform/ */
500fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
501fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    if (!strcmp(uevent->action, "add"))
502fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        add_platform_device(name);
503fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    else if (!strcmp(uevent->action, "remove"))
504fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        remove_platform_device(name);
505fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
506fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
507eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic const char *parse_device_name(struct uevent *uevent, unsigned int len)
508eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
509eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
510eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
511eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* if it's not a /dev device, nothing else to do */
512eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if((uevent->major < 0) || (uevent->minor < 0))
513eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
514eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
515eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* do we have a name? */
516eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = strrchr(uevent->path, '/');
517eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!name)
518eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
519eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name++;
520eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
521eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* too-long names would overrun our buffer */
522eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(strlen(name) > len)
523eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
524eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
525eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    return name;
526eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
527eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
528eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_block_device_event(struct uevent *uevent)
529eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
530eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *base = "/dev/block/";
531eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
532eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char devpath[96];
533eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char **links = NULL;
534eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
535eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = parse_device_name(uevent, 64);
536eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!name)
537eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return;
538eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
539eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    snprintf(devpath, sizeof(devpath), "%s%s", base, name);
540e46f9d510db9351682cf17c49115110870147335Stephen Smalley    make_dir(base, 0755);
541eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
542eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strncmp(uevent->path, "/devices/platform/", 18))
543eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        links = parse_platform_block_device(uevent);
544eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
545eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    handle_device(uevent->action, devpath, uevent->path, 1,
546eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross            uevent->major, uevent->minor, links);
547eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
548eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
549eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_generic_device_event(struct uevent *uevent)
550eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
551eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char *base;
552eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
553eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char devpath[96] = {0};
554eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char **links = NULL;
555eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
556eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = parse_device_name(uevent, 64);
557eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!name)
558eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return;
559eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
560eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strncmp(uevent->subsystem, "usb", 3)) {
561eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         if (!strcmp(uevent->subsystem, "usb")) {
562f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong            if (uevent->device_name) {
563f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                /*
564f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 * create device node provided by kernel if present
565f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 * see drivers/base/core.c
566f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 */
567f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                char *p = devpath;
568f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
569f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                /* skip leading /dev/ */
570f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                p += 5;
571f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                /* build directories */
572f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                while (*p) {
573f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                    if (*p == '/') {
574f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                        *p = 0;
575f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                        make_dir(devpath, 0755);
576f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                        *p = '/';
577f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                    }
578f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                    p++;
579f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                }
580f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong             }
581f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong             else {
582f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 /* This imitates the file system that would be created
583f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                  * if we were using devfs instead.
584f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                  * Minors are broken up into groups of 128, starting at "001"
585f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                  */
586f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 int bus_id = uevent->minor / 128 + 1;
587f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 int device_id = uevent->minor % 128 + 1;
588f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 /* build directories */
589f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 make_dir("/dev/bus", 0755);
590f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 make_dir("/dev/bus/usb", 0755);
591f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
592f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 make_dir(devpath, 0755);
593f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
594f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong             }
595eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         } else {
596eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             /* ignore other USB events */
597eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             return;
598eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         }
599eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
600eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/graphics/";
601e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
602eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
603eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/oncrpc/";
604e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
605eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
606eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/adsp/";
607e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
608eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
609eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/msm_camera/";
610e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
611eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "input", 5)) {
612eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/input/";
613e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
614eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
615eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/mtd/";
616e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
617eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "sound", 5)) {
618eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/snd/";
619e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
620eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "misc", 4) &&
621eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross                 !strncmp(name, "log_", 4)) {
622eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/log/";
623e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
624eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         name += 4;
625eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else
626eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/";
627eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     links = get_character_device_symlinks(uevent);
628eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
629eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     if (!devpath[0])
630eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         snprintf(devpath, sizeof(devpath), "%s%s", base, name);
631eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
632eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     handle_device(uevent->action, devpath, uevent->path, 0,
633eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             uevent->major, uevent->minor, links);
634eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
635eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
636eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device_event(struct uevent *uevent)
637eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
638eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strcmp(uevent->action,"add"))
639eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        fixup_sys_perms(uevent->path);
640eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
641eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strncmp(uevent->subsystem, "block", 5)) {
642eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        handle_block_device_event(uevent);
643fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    } else if (!strncmp(uevent->subsystem, "platform", 8)) {
644fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        handle_platform_device_event(uevent);
645eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    } else {
646eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        handle_generic_device_event(uevent);
647eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    }
648eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
649eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
6504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int load_firmware(int fw_fd, int loading_fd, int data_fd)
6514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
6524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct stat st;
6534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    long len_to_copy;
6544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int ret = 0;
6554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(fstat(fw_fd, &st) < 0)
6574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -1;
6584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    len_to_copy = st.st_size;
6594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    write(loading_fd, "1", 1);  /* start transfer */
6614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while (len_to_copy > 0) {
6634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        char buf[PAGE_SIZE];
6644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        ssize_t nr;
6654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        nr = read(fw_fd, buf, sizeof(buf));
6674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(!nr)
6684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            break;
6694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(nr < 0) {
6704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ret = -1;
6714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            break;
6724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
6734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        len_to_copy -= nr;
6754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        while (nr > 0) {
6764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ssize_t nw = 0;
6774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            nw = write(data_fd, buf + nw, nr);
6794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            if(nw <= 0) {
6804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                ret = -1;
6814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                goto out;
6824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            }
6834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            nr -= nw;
6844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
6854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
6864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectout:
6884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!ret)
6894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(loading_fd, "0", 1);  /* successful end of transfer */
6904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    else
6914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(loading_fd, "-1", 2); /* abort transfer */
6924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return ret;
6944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
6954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6968d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandstatic int is_booting(void)
6978d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland{
6988d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    return access("/dev/.booting", F_OK) == 0;
6998d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland}
7008d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland
7014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void process_firmware_event(struct uevent *uevent)
7024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
70302863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    char *root, *loading, *data, *file1 = NULL, *file2 = NULL;
7044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int l, loading_fd, data_fd, fw_fd;
7058d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    int booting = is_booting();
7064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7078d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    INFO("firmware: loading '%s' for '%s'\n",
7088d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland         uevent->firmware, uevent->path);
7094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
7114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
7134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&loading, "%sloading", root);
7154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto root_free_out;
7174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&data, "%sdata", root);
7194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto loading_free_out;
7214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
72202863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
72302863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    if (l == -1)
72402863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland        goto data_free_out;
72502863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland
72602863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
7274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto data_free_out;
7294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    loading_fd = open(loading, O_WRONLY);
7314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(loading_fd < 0)
7324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto file_free_out;
7334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    data_fd = open(data, O_WRONLY);
7354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(data_fd < 0)
7364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto loading_close_out;
7374f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7388d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandtry_loading_again:
73902863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    fw_fd = open(file1, O_RDONLY);
74002863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    if(fw_fd < 0) {
74102863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland        fw_fd = open(file2, O_RDONLY);
742609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby        if (fw_fd < 0) {
7438d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland            if (booting) {
7448d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                    /* If we're not fully booted, we may be missing
7458d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                     * filesystems needed for firmware, wait and retry.
7468d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                     */
7478d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                usleep(100000);
7488d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                booting = is_booting();
7498d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland                goto try_loading_again;
7508d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland            }
7518d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland            INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
752609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby            write(loading_fd, "-1", 2);
75302863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland            goto data_close_out;
754609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby        }
75502863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    }
7564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!load_firmware(fw_fd, loading_fd, data_fd))
7588d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland        INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
7594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    else
7608d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland        INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
7614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(fw_fd);
7634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_close_out:
7644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(data_fd);
7654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_close_out:
7664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(loading_fd);
7674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectfile_free_out:
76802863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    free(file1);
76902863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    free(file2);
7704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_free_out:
7714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(data);
7724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_free_out:
7734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(loading);
7744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectroot_free_out:
7754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(root);
7764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
7774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_firmware_event(struct uevent *uevent)
7794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
7804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    pid_t pid;
781982a815c10cc28707d81c46112fc45b1c10df3beColin Cross    int ret;
7824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(strcmp(uevent->subsystem, "firmware"))
7844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
7854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(strcmp(uevent->action, "add"))
7874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
7884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    /* we fork, to avoid making large memory allocations in init proper */
7904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    pid = fork();
7914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!pid) {
7924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        process_firmware_event(uevent);
7934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        exit(EXIT_SUCCESS);
7944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
7954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
7964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define UEVENT_MSG_LEN  1024
7980dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossvoid handle_device_fd()
7994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
8003f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang    char msg[UEVENT_MSG_LEN+2];
8013f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang    int n;
80257de8b8f27f887ca8084671df777a4ac199ba647Nick Kralevich    while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
8035f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        if(n >= UEVENT_MSG_LEN)   /* overflow -- discard */
8044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
8054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        msg[n] = '\0';
8074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        msg[n+1] = '\0';
8084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8095f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        struct uevent uevent;
8104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        parse_event(msg, &uevent);
8114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        handle_device_event(&uevent);
8134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        handle_firmware_event(&uevent);
8144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
8164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/* Coldboot walks parts of the /sys tree and pokes the uevent files
8184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** to cause the kernel to regenerate device add events that happened
8194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** before init's device manager was started
8204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project**
8214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** We drain any pending events from the netlink socket every time
8224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** we poke another uevent file to make sure we don't overrun the
8234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** socket's buffer.
8244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project*/
8254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8260dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void do_coldboot(DIR *d)
8274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
8284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct dirent *de;
8294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int dfd, fd;
8304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    dfd = dirfd(d);
8324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    fd = openat(dfd, "uevent", O_WRONLY);
8344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(fd >= 0) {
8354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(fd, "add\n", 4);
8364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        close(fd);
8370dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        handle_device_fd();
8384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while((de = readdir(d))) {
8414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        DIR *d2;
8424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(de->d_type != DT_DIR || de->d_name[0] == '.')
8444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
8454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
8474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(fd < 0)
8484f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
8494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        d2 = fdopendir(fd);
8514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(d2 == 0)
8524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            close(fd);
8534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        else {
8540dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross            do_coldboot(d2);
8554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            closedir(d2);
8564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
8574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
8594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8600dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void coldboot(const char *path)
8614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
8624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    DIR *d = opendir(path);
8634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(d) {
8640dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        do_coldboot(d);
8654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        closedir(d);
8664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
8684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8690dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossvoid device_init(void)
8704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
8714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    suseconds_t t0, t1;
872f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    struct stat info;
873f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    int fd;
874e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
875ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley    sehandle = NULL;
876ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley    if (is_selinux_enabled() > 0) {
877ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley        sehandle = selinux_android_file_context_handle();
878ae6f3d7c05070f7e0e56fe0056c8923c6ee2f473Stephen Smalley    }
879e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
8802d55e02d0f3c27f0c99ab889ab7b73126280a21cDima Zavin    /* is 64K enough? udev uses 16MB! */
8812d55e02d0f3c27f0c99ab889ab7b73126280a21cDima Zavin    device_fd = uevent_open_socket(64*1024, true);
8820dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    if(device_fd < 0)
8830dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        return;
8844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8850dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    fcntl(device_fd, F_SETFD, FD_CLOEXEC);
8860dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    fcntl(device_fd, F_SETFL, O_NONBLOCK);
8874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
888f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    if (stat(coldboot_done, &info) < 0) {
889f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        t0 = get_usecs();
890f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/class");
891f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/block");
892f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        coldboot("/sys/devices");
893f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        t1 = get_usecs();
894f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
895f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        close(fd);
896f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
897f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    } else {
898f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross        log_event_print("skipping coldboot, already done\n");
899f83d0b9af5cbe4440cc41ceaa8a7806a13c86282Colin Cross    }
9000dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross}
9014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
9020dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossint get_device_fd()
9030dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross{
9040dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross    return device_fd;
9054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
906