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"
54029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev#define FIRMWARE_DIR3   "/firmware/image"
554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
56e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
57e096e36e50b4b66638ebc4d3c09c2ee35f538dfaStephen Smalleyextern struct selabel_handle *sehandle;
58e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
59e46f9d510db9351682cf17c49115110870147335Stephen Smalley
600dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic int device_fd = -1;
610dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross
624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct uevent {
634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *action;
644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *path;
654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *subsystem;
664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    const char *firmware;
67b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    const char *partition_name;
68f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong    const char *device_name;
69b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int partition_num;
704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int major;
714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int minor;
724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perms_ {
754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    char *name;
76bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    char *attr;
774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t perm;
784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned int uid;
794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned int gid;
804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned short prefix;
814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstruct perm_node {
844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct perms_ dp;
854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct listnode plist;
864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project};
87bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
88fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstruct platform_node {
89fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    char *name;
90fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    int name_len;
91fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode list;
92fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross};
93fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
94bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic list_declare(sys_perms);
9544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Crossstatic list_declare(dev_perms);
96fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic list_declare(platform_names);
974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
98bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandint add_dev_perms(const char *name, const char *attr,
99bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                  mode_t perm, unsigned int uid, unsigned int gid,
100bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                  unsigned short prefix) {
101bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct perm_node *node = calloc(1, sizeof(*node));
1024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!node)
1034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -ENOMEM;
1044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
105bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    node->dp.name = strdup(name);
106bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (!node->dp.name)
1074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -ENOMEM;
1084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
109bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (attr) {
110bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        node->dp.attr = strdup(attr);
111bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if (!node->dp.attr)
112bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            return -ENOMEM;
113bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    }
114bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
1154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.perm = perm;
1164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.uid = uid;
1174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.gid = gid;
1184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    node->dp.prefix = prefix;
1194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
120bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    if (attr)
121bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        list_add_tail(&sys_perms, &node->plist);
122bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    else
123bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        list_add_tail(&dev_perms, &node->plist);
124bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
1254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return 0;
1264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
128bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandvoid fixup_sys_perms(const char *upath)
1294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
130bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    char buf[512];
131bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct listnode *node;
132bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    struct perms_ *dp;
1334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
134bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        /* upaths omit the "/sys" that paths in this list
135bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland         * contain, so we add 4 when comparing...
136bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland         */
137bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland    list_for_each(node, &sys_perms) {
138bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        dp = &(node_to_item(node, struct perm_node, plist))->dp;
139bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if (dp->prefix) {
140bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
1414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                continue;
1424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else {
143bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            if (strcmp(upath, dp->name + 4))
1444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                continue;
1454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
146bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
147bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
148bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland            return;
149bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland
150bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        sprintf(buf,"/sys%s/%s", upath, dp->attr);
151bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
152bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        chown(buf, dp->uid, dp->gid);
153bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland        chmod(buf, dp->perm);
1544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
1554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
1574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
1584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t perm;
16044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct listnode *node;
16144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct perm_node *perm_node;
16244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    struct perms_ *dp;
16344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
16444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    /* search the perms list in reverse so that ueventd.$hardware can
16544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross     * override ueventd.rc
16644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross     */
16744b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    list_for_each_reverse(node, &dev_perms) {
16844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        perm_node = node_to_item(node, struct perm_node, plist);
16944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        dp = &perm_node->dp;
17044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross
17144b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        if (dp->prefix) {
17244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            if (strncmp(path, dp->name, strlen(dp->name)))
17344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross                continue;
17444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        } else {
17544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross            if (strcmp(path, dp->name))
17644b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross                continue;
1774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
17844b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        *uid = dp->uid;
17944b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        *gid = dp->gid;
18044b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross        return dp->perm;
1814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
18244b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    /* Default if nothing found. */
18344b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    *uid = 0;
18444b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    *gid = 0;
18544b65d047cc39baf30e21bfd8dd438f6bc1f77f5Colin Cross    return 0600;
1864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
1874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
188bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetlandstatic void make_device(const char *path,
189bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                        const char *upath,
190bc57d4ce925a62f14c28c55e0ff28af1114f12beBrian Swetland                        int block, int major, int minor)
1914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
1924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned uid;
1934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    unsigned gid;
1944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode_t mode;
1954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    dev_t dev;
196e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
197e46f9d510db9351682cf17c49115110870147335Stephen Smalley    char *secontext = NULL;
198e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
1994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
201e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
202e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (sehandle) {
203e46f9d510db9351682cf17c49115110870147335Stephen Smalley        selabel_lookup(sehandle, &secontext, path, mode);
204e46f9d510db9351682cf17c49115110870147335Stephen Smalley        setfscreatecon(secontext);
205e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
206e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
20717dcc5c57dcffb919f3de65ff7a0134ffa3bd874Colin Cross    dev = makedev(major, minor);
2086405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    /* Temporarily change egid to avoid race condition setting the gid of the
2096405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * device node. Unforunately changing the euid would prevent creation of
2106405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * some device nodes, so the uid has to be set with chown() and is still
2116405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * racy. Fixing the gid race at least fixed the issue with system_server
2126405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly     * opening dynamic input devices under the AID_INPUT gid. */
2136405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    setegid(gid);
2144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    mknod(path, mode, dev);
2156405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    chown(path, uid, -1);
2166405c6953fa02d41d9f6377f4cdb947604f481c4Nick Pelly    setegid(AID_ROOT);
217e46f9d510db9351682cf17c49115110870147335Stephen Smalley#ifdef HAVE_SELINUX
218e46f9d510db9351682cf17c49115110870147335Stephen Smalley    if (secontext) {
219e46f9d510db9351682cf17c49115110870147335Stephen Smalley        freecon(secontext);
220e46f9d510db9351682cf17c49115110870147335Stephen Smalley        setfscreatecon(NULL);
221e46f9d510db9351682cf17c49115110870147335Stephen Smalley    }
222e46f9d510db9351682cf17c49115110870147335Stephen Smalley#endif
223e46f9d510db9351682cf17c49115110870147335Stephen Smalley}
224e46f9d510db9351682cf17c49115110870147335Stephen Smalley
225fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void add_platform_device(const char *name)
226fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
227fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    int name_len = strlen(name);
228fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
229fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
230fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
231fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
232fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
233fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        if ((bus->name_len < name_len) &&
234fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                (name[bus->name_len] == '/') &&
235fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                !strncmp(name, bus->name, bus->name_len))
236fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            /* subdevice of an existing platform, ignore it */
237fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            return;
238fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
239fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
240fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    INFO("adding platform device %s\n", name);
241fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
242fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    bus = calloc(1, sizeof(struct platform_node));
243fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    bus->name = strdup(name);
244fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    bus->name_len = name_len;
245fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_add_tail(&platform_names, &bus->list);
246fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
247fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
248fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross/*
249fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * given a name that may start with a platform device, find the length of the
250fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * platform device prefix.  If it doesn't start with a platform device, return
251fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross * 0.
252fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross */
253fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic const char *find_platform_device(const char *name)
254fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
255fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    int name_len = strlen(name);
256fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
257fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
258fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
259fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
260fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
261fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        if ((bus->name_len < name_len) &&
262fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                (name[bus->name_len] == '/') &&
263fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross                !strncmp(name, bus->name, bus->name_len))
264fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            return bus->name;
265fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
266fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
267fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    return NULL;
268fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
269fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
270fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void remove_platform_device(const char *name)
271fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
272fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct listnode *node;
273fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    struct platform_node *bus;
274fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
275fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    list_for_each_reverse(node, &platform_names) {
276fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        bus = node_to_item(node, struct platform_node, list);
277fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        if (!strcmp(name, bus->name)) {
278fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            INFO("removing platform device %s\n", name);
279fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            free(bus->name);
280fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            list_remove(node);
281fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            free(bus);
282fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross            return;
283fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        }
284fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    }
285fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
286fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
2871e070846f8908dff15086efa12fbba01ab7eae15Chuck Tuffli#if LOG_UEVENTS
2884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic inline suseconds_t get_usecs(void)
2904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
2914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct timeval tv;
2924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    gettimeofday(&tv, 0);
2934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
2944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
2954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(x...) INFO(x)
2974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
2984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#else
2994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define log_event_print(fmt, args...)   do { } while (0)
3014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define get_usecs()                     0
3024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#endif
3044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void parse_event(const char *msg, struct uevent *uevent)
3064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
3074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->action = "";
3084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->path = "";
3094f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->subsystem = "";
3104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->firmware = "";
3114f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->major = -1;
3124f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    uevent->minor = -1;
313b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    uevent->partition_name = NULL;
314b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    uevent->partition_num = -1;
315f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong    uevent->device_name = NULL;
3164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        /* currently ignoring SEQNUM */
3184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while(*msg) {
3194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(!strncmp(msg, "ACTION=", 7)) {
3204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 7;
3214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->action = msg;
3224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "DEVPATH=", 8)) {
3234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 8;
3244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->path = msg;
3254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
3264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 10;
3274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->subsystem = msg;
3284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "FIRMWARE=", 9)) {
3294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 9;
3304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->firmware = msg;
3314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "MAJOR=", 6)) {
3324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 6;
3334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->major = atoi(msg);
3344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        } else if(!strncmp(msg, "MINOR=", 6)) {
3354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            msg += 6;
3364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            uevent->minor = atoi(msg);
337b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        } else if(!strncmp(msg, "PARTN=", 6)) {
338b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            msg += 6;
339b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            uevent->partition_num = atoi(msg);
340b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        } else if(!strncmp(msg, "PARTNAME=", 9)) {
341b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            msg += 9;
342b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            uevent->partition_name = msg;
343f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong        } else if(!strncmp(msg, "DEVNAME=", 8)) {
344f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong            msg += 8;
345f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong            uevent->device_name = msg;
3464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
3474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
348f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong        /* advance to after the next \0 */
3494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        while(*msg++)
3504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ;
3514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
3524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
3534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
3544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                    uevent->action, uevent->path, uevent->subsystem,
3554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                    uevent->firmware, uevent->major, uevent->minor);
3564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
3574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
358d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobystatic char **get_character_device_symlinks(struct uevent *uevent)
359d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby{
360d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    const char *parent;
361d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    char *slash;
362d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    char **links;
363d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    int link_num = 0;
364d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    int width;
365d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
366d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (strncmp(uevent->path, "/devices/platform/", 18))
367d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        return NULL;
368d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
369d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    links = malloc(sizeof(char *) * 2);
370d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (!links)
371d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        return NULL;
372d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    memset(links, 0, sizeof(char *) * 2);
373d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
374d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    /* skip "/devices/platform/<driver>" */
375d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    parent = strchr(uevent->path + 18, '/');
376d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (!*parent)
377d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        goto err;
378d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
379d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    if (!strncmp(parent, "/usb", 4)) {
380d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        /* skip root hub name and device. use device interface */
381d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        while (*++parent && *parent != '/');
382d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (*parent)
383d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            while (*++parent && *parent != '/');
384d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (!*parent)
385d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
386d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        slash = strchr(++parent, '/');
387d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (!slash)
388d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
389d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        width = slash - parent;
390d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (width <= 0)
391d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            goto err;
392d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
393d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
394d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            link_num++;
395d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        else
396d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby            links[link_num] = NULL;
397d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        mkdir("/dev/usb", 0755);
398d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    }
399d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    else {
400d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby        goto err;
401d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    }
402d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
403d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    return links;
404d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Gobyerr:
405d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    free(links);
406d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby    return NULL;
407d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby}
408d2278638d6b37717b51d622929eb4c8f6e16cb0cBenoit Goby
409b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crossstatic char **parse_platform_block_device(struct uevent *uevent)
410b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross{
411fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    const char *device;
412b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    const char *path;
413b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char *slash;
414b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int width;
415b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char buf[256];
416b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char link_path[256];
417b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int fd;
418b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int link_num = 0;
419b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int ret;
420b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char *p;
421b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    unsigned int size;
422b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    struct stat info;
423b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
424b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    char **links = malloc(sizeof(char *) * 4);
425b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (!links)
426b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        return NULL;
427b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    memset(links, 0, sizeof(char *) * 4);
428b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
429b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    /* Drop "/devices/platform/" */
430b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    path = uevent->path;
431fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    device = path + 18;
432fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    device = find_platform_device(device);
433fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    if (!device)
434b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        goto err;
435b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
436fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    INFO("found platform device %s\n", device);
437fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
438fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
439b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
440b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (uevent->partition_name) {
441b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        p = strdup(uevent->partition_name);
442b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        sanitize(p);
443b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
444b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            link_num++;
445b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        else
446b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            links[link_num] = NULL;
447b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        free(p);
448b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
449b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
450b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (uevent->partition_num >= 0) {
451b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
452b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            link_num++;
453b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        else
454b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            links[link_num] = NULL;
455b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
456b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
457b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    slash = strrchr(path, '/');
458b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
459b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        link_num++;
460b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    else
461b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        links[link_num] = NULL;
462b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
463b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    return links;
464b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
465b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Crosserr:
466b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    free(links);
467b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    return NULL;
468b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross}
469b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
470eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device(const char *action, const char *devpath,
471eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        const char *path, int block, int major, int minor, char **links)
4724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
473b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    int i;
4744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
475eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!strcmp(action, "add")) {
476eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        make_device(devpath, path, block, major, minor);
477b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (links) {
478b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            for (i = 0; links[i]; i++)
479b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross                make_link(devpath, links[i]);
480b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        }
4814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
4824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
483eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!strcmp(action, "remove")) {
484b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        if (links) {
485b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            for (i = 0; links[i]; i++)
486b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross                remove_link(devpath, links[i]);
487b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        }
4884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        unlink(devpath);
489b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    }
490b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross
491b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross    if (links) {
492b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        for (i = 0; links[i]; i++)
493b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross            free(links[i]);
494b0ab94b7d5a888f0b6920b156e5c6a075fa0741aColin Cross        free(links);
4954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
4964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
4974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
498fadb85e3bd333653014df5fcc931d844dc8c3630Colin Crossstatic void handle_platform_device_event(struct uevent *uevent)
499fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross{
500fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    const char *name = uevent->path + 18; /* length of /devices/platform/ */
501fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
502fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    if (!strcmp(uevent->action, "add"))
503fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        add_platform_device(name);
504fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    else if (!strcmp(uevent->action, "remove"))
505fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        remove_platform_device(name);
506fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross}
507fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross
508eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic const char *parse_device_name(struct uevent *uevent, unsigned int len)
509eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
510eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
511eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
512eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* if it's not a /dev device, nothing else to do */
513eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if((uevent->major < 0) || (uevent->minor < 0))
514eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
515eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
516eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* do we have a name? */
517eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = strrchr(uevent->path, '/');
518eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(!name)
519eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
520eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name++;
521eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
522eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    /* too-long names would overrun our buffer */
523eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if(strlen(name) > len)
524eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return NULL;
525eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
526eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    return name;
527eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
528eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
529eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_block_device_event(struct uevent *uevent)
530eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
531eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *base = "/dev/block/";
532eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
533eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char devpath[96];
534eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char **links = NULL;
535eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
536eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = parse_device_name(uevent, 64);
537eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!name)
538eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return;
539eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
540eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    snprintf(devpath, sizeof(devpath), "%s%s", base, name);
541e46f9d510db9351682cf17c49115110870147335Stephen Smalley    make_dir(base, 0755);
542eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
543eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strncmp(uevent->path, "/devices/platform/", 18))
544eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        links = parse_platform_block_device(uevent);
545eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
546eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    handle_device(uevent->action, devpath, uevent->path, 1,
547eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross            uevent->major, uevent->minor, links);
548eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
549eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
550eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_generic_device_event(struct uevent *uevent)
551eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
552eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char *base;
553eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    const char *name;
554eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char devpath[96] = {0};
555eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    char **links = NULL;
556eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
557eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    name = parse_device_name(uevent, 64);
558eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!name)
559eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        return;
560eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
561eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strncmp(uevent->subsystem, "usb", 3)) {
562eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         if (!strcmp(uevent->subsystem, "usb")) {
563f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong            if (uevent->device_name) {
564f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                /*
565f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 * create device node provided by kernel if present
566f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 * see drivers/base/core.c
567f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 */
568f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                char *p = devpath;
569f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
570f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                /* skip leading /dev/ */
571f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                p += 5;
572f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                /* build directories */
573f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                while (*p) {
574f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                    if (*p == '/') {
575f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                        *p = 0;
576f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                        make_dir(devpath, 0755);
577f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                        *p = '/';
578f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                    }
579f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                    p++;
580f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                }
581f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong             }
582f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong             else {
583f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 /* This imitates the file system that would be created
584f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                  * if we were using devfs instead.
585f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                  * Minors are broken up into groups of 128, starting at "001"
586f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                  */
587f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 int bus_id = uevent->minor / 128 + 1;
588f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 int device_id = uevent->minor % 128 + 1;
589f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 /* build directories */
590f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 make_dir("/dev/bus", 0755);
591f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 make_dir("/dev/bus/usb", 0755);
592f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
593f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 make_dir(devpath, 0755);
594f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong                 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
595f97b887848cc64e7d88d27f198b7721c9f2f8ec8Wei Zhong             }
596eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         } else {
597eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             /* ignore other USB events */
598eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             return;
599eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         }
600eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
601eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/graphics/";
602e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
603e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski     } else if (!strncmp(uevent->subsystem, "drm", 3)) {
604e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski         base = "/dev/dri/";
605e6f8d45da8c61d67dc9fdc1cec53f40594043817Lukasz Anaczkowski         make_dir(base, 0755);
606eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
607eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/oncrpc/";
608e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
609eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
610eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/adsp/";
611e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
612eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
613eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/msm_camera/";
614e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
615eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "input", 5)) {
616eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/input/";
617e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
618eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
619eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/mtd/";
620e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
621eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "sound", 5)) {
622eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/snd/";
623e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
624eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else if(!strncmp(uevent->subsystem, "misc", 4) &&
625eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross                 !strncmp(name, "log_", 4)) {
626eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/log/";
627e46f9d510db9351682cf17c49115110870147335Stephen Smalley         make_dir(base, 0755);
628eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         name += 4;
629eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     } else
630eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         base = "/dev/";
631eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     links = get_character_device_symlinks(uevent);
632eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
633eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     if (!devpath[0])
634eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross         snprintf(devpath, sizeof(devpath), "%s%s", base, name);
635eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
636eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross     handle_device(uevent->action, devpath, uevent->path, 0,
637eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross             uevent->major, uevent->minor, links);
638eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
639eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
640eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Crossstatic void handle_device_event(struct uevent *uevent)
641eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross{
642308bc52d33afa1b7b470e14fa8478045456cbb20Colin Cross    if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
643eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        fixup_sys_perms(uevent->path);
644eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
645eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    if (!strncmp(uevent->subsystem, "block", 5)) {
646eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        handle_block_device_event(uevent);
647fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross    } else if (!strncmp(uevent->subsystem, "platform", 8)) {
648fadb85e3bd333653014df5fcc931d844dc8c3630Colin Cross        handle_platform_device_event(uevent);
649eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    } else {
650eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross        handle_generic_device_event(uevent);
651eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross    }
652eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross}
653eb5ba8306522acdbf3a63ca847e5e6b990d842b9Colin Cross
6544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic int load_firmware(int fw_fd, int loading_fd, int data_fd)
6554f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
6564f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct stat st;
6574f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    long len_to_copy;
6584f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int ret = 0;
6594f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6604f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(fstat(fw_fd, &st) < 0)
6614f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return -1;
6624f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    len_to_copy = st.st_size;
6634f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6644f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    write(loading_fd, "1", 1);  /* start transfer */
6654f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6664f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while (len_to_copy > 0) {
6674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        char buf[PAGE_SIZE];
6684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        ssize_t nr;
6694f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        nr = read(fw_fd, buf, sizeof(buf));
6714f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(!nr)
6724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            break;
6734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(nr < 0) {
6744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ret = -1;
6754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            break;
6764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
6774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        len_to_copy -= nr;
6794f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        while (nr > 0) {
6804f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            ssize_t nw = 0;
6814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            nw = write(data_fd, buf + nw, nr);
6834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            if(nw <= 0) {
6844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                ret = -1;
6854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project                goto out;
6864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            }
6874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            nr -= nw;
6884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        }
6894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
6904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectout:
6924f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!ret)
6934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(loading_fd, "0", 1);  /* successful end of transfer */
6944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    else
6954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(loading_fd, "-1", 2); /* abort transfer */
6964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
6974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    return ret;
6984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
6994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7008d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandstatic int is_booting(void)
7018d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland{
7028d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    return access("/dev/.booting", F_OK) == 0;
7038d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland}
7048d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland
7054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void process_firmware_event(struct uevent *uevent)
7064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
707029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev    char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
7084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int l, loading_fd, data_fd, fw_fd;
7098d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    int booting = is_booting();
7104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7118d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland    INFO("firmware: loading '%s' for '%s'\n",
7128d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland         uevent->firmware, uevent->path);
7134f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7144f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
7154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
7174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&loading, "%sloading", root);
7194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7204f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto root_free_out;
7214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    l = asprintf(&data, "%sdata", root);
7234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto loading_free_out;
7254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
72602863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
72702863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    if (l == -1)
72802863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland        goto data_free_out;
72902863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland
73002863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
7314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (l == -1)
7324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto data_free_out;
7334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
734029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev    l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
735029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev    if (l == -1)
736029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev        goto data_free_out;
737029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev
7384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    loading_fd = open(loading, O_WRONLY);
7394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(loading_fd < 0)
7404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto file_free_out;
7414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    data_fd = open(data, O_WRONLY);
7434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(data_fd < 0)
7444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        goto loading_close_out;
7454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7468d48c8e45724c7103f0ace7885d339e49399908bBrian Swetlandtry_loading_again:
74702863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    fw_fd = open(file1, O_RDONLY);
74802863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    if(fw_fd < 0) {
74902863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland        fw_fd = open(file2, O_RDONLY);
750609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby        if (fw_fd < 0) {
751029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev            fw_fd = open(file3, O_RDONLY);
752029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev            if (fw_fd < 0) {
753029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev                if (booting) {
754029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev                        /* If we're not fully booted, we may be missing
755029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev                         * filesystems needed for firmware, wait and retry.
756029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev                         */
757029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev                    usleep(100000);
758029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev                    booting = is_booting();
759029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev                    goto try_loading_again;
760029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev                }
761029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev                INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
762029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev                write(loading_fd, "-1", 2);
763029d44e6aef334d2f0f03cbc9a5609cddc1b23e3Iliyan Malchev                goto data_close_out;
7648d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland            }
765609d8828d3b8266a80606bf902d1294296962cf3Benoit Goby        }
76602863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    }
7674f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7684f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(!load_firmware(fw_fd, loading_fd, data_fd))
7698d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland        INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
7704f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    else
7718d48c8e45724c7103f0ace7885d339e49399908bBrian Swetland        INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
7724f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7734f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(fw_fd);
7744f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_close_out:
7754f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(data_fd);
7764f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_close_out:
7774f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    close(loading_fd);
7784f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectfile_free_out:
77902863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    free(file1);
78002863b957cfbfc302d3136ed78c0cba86accacd8Brian Swetland    free(file2);
7814f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectdata_free_out:
7824f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(data);
7834f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectloading_free_out:
7844f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(loading);
7854f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectroot_free_out:
7864f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    free(root);
7874f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
7884f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7894f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Projectstatic void handle_firmware_event(struct uevent *uevent)
7904f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
7914f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    pid_t pid;
792982a815c10cc28707d81c46112fc45b1c10df3beColin Cross    int ret;
7934f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7944f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(strcmp(uevent->subsystem, "firmware"))
7954f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
7964f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
7974f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(strcmp(uevent->action, "add"))
7984f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        return;
7994f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8004f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    /* we fork, to avoid making large memory allocations in init proper */
8014f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    pid = fork();
8024f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if (!pid) {
8034f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        process_firmware_event(uevent);
8044f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        exit(EXIT_SUCCESS);
8054f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8064f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
8074f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8084f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project#define UEVENT_MSG_LEN  1024
8090dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossvoid handle_device_fd()
8104f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
8113f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang    char msg[UEVENT_MSG_LEN+2];
8123f582e92f07d358f3d0941b86407b39e2e7e67ccVernon Tang    int n;
81357de8b8f27f887ca8084671df777a4ac199ba647Nick Kralevich    while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
8145f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        if(n >= UEVENT_MSG_LEN)   /* overflow -- discard */
8154f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project            continue;
8164f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8174f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        msg[n] = '\0';
8184f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        msg[n+1] = '\0';
8194f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8205f5d5c8cef10f28950fa108a8bd86d55f11b7ef4Nick Kralevich        struct uevent uevent;
8214f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        parse_event(msg, &uevent);
8224f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8234f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        handle_device_event(&uevent);
8244f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        handle_firmware_event(&uevent);
8254f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8264f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project}
8274f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8284f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project/* Coldboot walks parts of the /sys tree and pokes the uevent files
8294f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** to cause the kernel to regenerate device add events that happened
8304f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** before init's device manager was started
8314f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project**
8324f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** We drain any pending events from the netlink socket every time
8334f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** we poke another uevent file to make sure we don't overrun the
8344f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project** socket's buffer.
8354f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project*/
8364f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8370dd7ca6e87abb689700c5e3a816a949b03b1154bColin Crossstatic void do_coldboot(DIR *d)
8384f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project{
8394f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    struct dirent *de;
8404f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    int dfd, fd;
8414f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8424f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    dfd = dirfd(d);
8434f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8444f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    fd = openat(dfd, "uevent", O_WRONLY);
8454f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    if(fd >= 0) {
8464f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        write(fd, "add\n", 4);
8474f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        close(fd);
8480dd7ca6e87abb689700c5e3a816a949b03b1154bColin Cross        handle_device_fd();
8494f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    }
8504f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8514f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project    while((de = readdir(d))) {
8524f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        DIR *d2;
8534f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project
8544f6e8d7a00cbeda1e70cc15be9c4af1018bdad5The Android Open Source Project        if(de->d_type != DT_DIR || de->d_name[0] == '.')
8554f6e8d7a00cbeda1e70cc