blob: 3119e8eca6539d16c0e61217f1c1af1d4dd9351c [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
Mark Salyzyn154f4602014-02-20 14:59:07 -08002 * Copyright (C) 2007-2014 The Android Open Source Project
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080018#include <stddef.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070019#include <stdio.h>
20#include <stdlib.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
24#include <fcntl.h>
25#include <dirent.h>
26#include <unistd.h>
27#include <string.h>
28
29#include <sys/socket.h>
30#include <sys/un.h>
31#include <linux/netlink.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050032
Stephen Smalleye46f9d52012-01-13 08:48:47 -050033#include <selinux/selinux.h>
34#include <selinux/label.h>
Stephen Smalleyae6f3d72012-05-01 15:02:53 -040035#include <selinux/android.h>
Stephen Smalleye2eb69d2013-04-16 09:30:30 -040036#include <selinux/avc.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050037
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038#include <private/android_filesystem_config.h>
39#include <sys/time.h>
Colin Cross982a8152010-06-03 12:21:01 -070040#include <sys/wait.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
Dima Zavinda04c522011-09-01 17:09:44 -070042#include <cutils/list.h>
Vernon Tang3f582e92011-04-25 13:08:17 +100043#include <cutils/uevent.h>
44
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070045#include "devices.h"
Greg Hackmann3312aa82013-11-18 15:24:40 -080046#include "ueventd_parser.h"
Colin Crossb0ab94b2010-04-08 16:16:20 -070047#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070048#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070049
Mark Salyzyn154f4602014-02-20 14:59:07 -080050#define UNUSED __attribute__((__unused__))
51
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052#define SYSFS_PREFIX "/sys"
Brian Swetland02863b92010-09-19 03:36:39 -070053#define FIRMWARE_DIR1 "/etc/firmware"
54#define FIRMWARE_DIR2 "/vendor/firmware"
Iliyan Malchev029d44e2012-06-08 10:42:26 -070055#define FIRMWARE_DIR3 "/firmware/image"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056
Stephen Smalleye096e362012-06-11 13:37:39 -040057extern struct selabel_handle *sehandle;
Stephen Smalleye46f9d52012-01-13 08:48:47 -050058
Colin Cross0dd7ca62010-04-13 19:25:51 -070059static int device_fd = -1;
60
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061struct uevent {
62 const char *action;
63 const char *path;
64 const char *subsystem;
65 const char *firmware;
Colin Crossb0ab94b2010-04-08 16:16:20 -070066 const char *partition_name;
Wei Zhongf97b8872012-03-23 14:15:34 -070067 const char *device_name;
Colin Crossb0ab94b2010-04-08 16:16:20 -070068 int partition_num;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069 int major;
70 int minor;
71};
72
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073struct perms_ {
74 char *name;
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070075 char *attr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076 mode_t perm;
77 unsigned int uid;
78 unsigned int gid;
79 unsigned short prefix;
80};
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082struct perm_node {
83 struct perms_ dp;
84 struct listnode plist;
85};
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070086
Colin Crossfadb85e2011-03-30 18:32:12 -070087struct platform_node {
88 char *name;
Dima Zavinf395c922013-03-06 16:23:57 -080089 char *path;
90 int path_len;
Colin Crossfadb85e2011-03-30 18:32:12 -070091 struct listnode list;
92};
93
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070094static list_declare(sys_perms);
Colin Cross44b65d02010-04-20 14:32:50 -070095static list_declare(dev_perms);
Colin Crossfadb85e2011-03-30 18:32:12 -070096static list_declare(platform_names);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070097
Brian Swetlandbc57d4c2010-10-26 15:09:43 -070098int add_dev_perms(const char *name, const char *attr,
99 mode_t perm, unsigned int uid, unsigned int gid,
100 unsigned short prefix) {
101 struct perm_node *node = calloc(1, sizeof(*node));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102 if (!node)
103 return -ENOMEM;
104
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700105 node->dp.name = strdup(name);
106 if (!node->dp.name)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107 return -ENOMEM;
108
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700109 if (attr) {
110 node->dp.attr = strdup(attr);
111 if (!node->dp.attr)
112 return -ENOMEM;
113 }
114
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700115 node->dp.perm = perm;
116 node->dp.uid = uid;
117 node->dp.gid = gid;
118 node->dp.prefix = prefix;
119
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700120 if (attr)
121 list_add_tail(&sys_perms, &node->plist);
122 else
123 list_add_tail(&dev_perms, &node->plist);
124
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700125 return 0;
126}
127
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700128void fixup_sys_perms(const char *upath)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700129{
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700130 char buf[512];
131 struct listnode *node;
132 struct perms_ *dp;
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400133 char *secontext;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700134
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700135 /* upaths omit the "/sys" that paths in this list
136 * contain, so we add 4 when comparing...
137 */
138 list_for_each(node, &sys_perms) {
139 dp = &(node_to_item(node, struct perm_node, plist))->dp;
140 if (dp->prefix) {
141 if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700142 continue;
143 } else {
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700144 if (strcmp(upath, dp->name + 4))
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700145 continue;
146 }
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700147
148 if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
149 return;
150
151 sprintf(buf,"/sys%s/%s", upath, dp->attr);
152 INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
153 chown(buf, dp->uid, dp->gid);
154 chmod(buf, dp->perm);
Stephen Smalley5f7b0172013-04-03 13:15:01 -0400155 if (sehandle) {
156 secontext = NULL;
157 selabel_lookup(sehandle, &secontext, buf, 0);
158 if (secontext) {
159 setfilecon(buf, secontext);
160 freecon(secontext);
161 }
162 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700163 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164}
165
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700166static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
167{
168 mode_t perm;
Colin Cross44b65d02010-04-20 14:32:50 -0700169 struct listnode *node;
170 struct perm_node *perm_node;
171 struct perms_ *dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700172
Colin Cross44b65d02010-04-20 14:32:50 -0700173 /* search the perms list in reverse so that ueventd.$hardware can
174 * override ueventd.rc
175 */
176 list_for_each_reverse(node, &dev_perms) {
177 perm_node = node_to_item(node, struct perm_node, plist);
178 dp = &perm_node->dp;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700179
Colin Cross44b65d02010-04-20 14:32:50 -0700180 if (dp->prefix) {
181 if (strncmp(path, dp->name, strlen(dp->name)))
182 continue;
183 } else {
184 if (strcmp(path, dp->name))
185 continue;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700186 }
Colin Cross44b65d02010-04-20 14:32:50 -0700187 *uid = dp->uid;
188 *gid = dp->gid;
189 return dp->perm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190 }
Colin Cross44b65d02010-04-20 14:32:50 -0700191 /* Default if nothing found. */
192 *uid = 0;
193 *gid = 0;
194 return 0600;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195}
196
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700197static void make_device(const char *path,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800198 const char *upath UNUSED,
Brian Swetlandbc57d4c2010-10-26 15:09:43 -0700199 int block, int major, int minor)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700200{
201 unsigned uid;
202 unsigned gid;
203 mode_t mode;
204 dev_t dev;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500205 char *secontext = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700208
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500209 if (sehandle) {
210 selabel_lookup(sehandle, &secontext, path, mode);
211 setfscreatecon(secontext);
212 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700213
Colin Cross17dcc5c2010-09-03 12:25:34 -0700214 dev = makedev(major, minor);
Nick Pelly6405c692010-01-21 18:13:39 -0800215 /* Temporarily change egid to avoid race condition setting the gid of the
216 * device node. Unforunately changing the euid would prevent creation of
217 * some device nodes, so the uid has to be set with chown() and is still
218 * racy. Fixing the gid race at least fixed the issue with system_server
219 * opening dynamic input devices under the AID_INPUT gid. */
220 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700221 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800222 chown(path, uid, -1);
223 setegid(AID_ROOT);
Kenny Rootb5982bf2012-10-16 23:07:05 -0700224
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500225 if (secontext) {
226 freecon(secontext);
227 setfscreatecon(NULL);
228 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229}
230
Dima Zavinf395c922013-03-06 16:23:57 -0800231static void add_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700232{
Dima Zavinf395c922013-03-06 16:23:57 -0800233 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700234 struct listnode *node;
235 struct platform_node *bus;
Dima Zavinf395c922013-03-06 16:23:57 -0800236 const char *name = path;
237
238 if (!strncmp(path, "/devices/", 9)) {
239 name += 9;
240 if (!strncmp(name, "platform/", 9))
241 name += 9;
242 }
Colin Crossfadb85e2011-03-30 18:32:12 -0700243
244 list_for_each_reverse(node, &platform_names) {
245 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800246 if ((bus->path_len < path_len) &&
247 (path[bus->path_len] == '/') &&
248 !strncmp(path, bus->path, bus->path_len))
Colin Crossfadb85e2011-03-30 18:32:12 -0700249 /* subdevice of an existing platform, ignore it */
250 return;
251 }
252
Dima Zavinf395c922013-03-06 16:23:57 -0800253 INFO("adding platform device %s (%s)\n", name, path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700254
255 bus = calloc(1, sizeof(struct platform_node));
Dima Zavinf395c922013-03-06 16:23:57 -0800256 bus->path = strdup(path);
257 bus->path_len = path_len;
258 bus->name = bus->path + (name - path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700259 list_add_tail(&platform_names, &bus->list);
260}
261
262/*
Dima Zavinf395c922013-03-06 16:23:57 -0800263 * given a path that may start with a platform device, find the length of the
Colin Crossfadb85e2011-03-30 18:32:12 -0700264 * platform device prefix. If it doesn't start with a platform device, return
265 * 0.
266 */
Dima Zavinf395c922013-03-06 16:23:57 -0800267static struct platform_node *find_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700268{
Dima Zavinf395c922013-03-06 16:23:57 -0800269 int path_len = strlen(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700270 struct listnode *node;
271 struct platform_node *bus;
272
273 list_for_each_reverse(node, &platform_names) {
274 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800275 if ((bus->path_len < path_len) &&
276 (path[bus->path_len] == '/') &&
277 !strncmp(path, bus->path, bus->path_len))
278 return bus;
Colin Crossfadb85e2011-03-30 18:32:12 -0700279 }
280
281 return NULL;
282}
283
Dima Zavinf395c922013-03-06 16:23:57 -0800284static void remove_platform_device(const char *path)
Colin Crossfadb85e2011-03-30 18:32:12 -0700285{
286 struct listnode *node;
287 struct platform_node *bus;
288
289 list_for_each_reverse(node, &platform_names) {
290 bus = node_to_item(node, struct platform_node, list);
Dima Zavinf395c922013-03-06 16:23:57 -0800291 if (!strcmp(path, bus->path)) {
292 INFO("removing platform device %s\n", bus->name);
293 free(bus->path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700294 list_remove(node);
295 free(bus);
296 return;
297 }
298 }
299}
300
Andrew Boiea885d042013-09-13 17:41:20 -0700301/* Given a path that may start with a PCI device, populate the supplied buffer
302 * with the PCI domain/bus number and the peripheral ID and return 0.
303 * If it doesn't start with a PCI device, or there is some error, return -1 */
304static int find_pci_device_prefix(const char *path, char *buf, ssize_t buf_sz)
305{
306 const char *start, *end;
307
308 if (strncmp(path, "/devices/pci", 12))
309 return -1;
310
311 /* Beginning of the prefix is the initial "pci" after "/devices/" */
312 start = path + 9;
313
314 /* End of the prefix is two path '/' later, capturing the domain/bus number
315 * and the peripheral ID. Example: pci0000:00/0000:00:1f.2 */
316 end = strchr(start, '/');
317 if (!end)
318 return -1;
319 end = strchr(end + 1, '/');
320 if (!end)
321 return -1;
322
323 /* Make sure we have enough room for the string plus null terminator */
324 if (end - start + 1 > buf_sz)
325 return -1;
326
327 strncpy(buf, start, end - start);
328 buf[end - start] = '\0';
329 return 0;
330}
331
Chuck Tuffli1e070842008-12-15 14:26:56 -0800332#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700333
334static inline suseconds_t get_usecs(void)
335{
336 struct timeval tv;
337 gettimeofday(&tv, 0);
338 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
339}
340
341#define log_event_print(x...) INFO(x)
342
343#else
344
345#define log_event_print(fmt, args...) do { } while (0)
346#define get_usecs() 0
347
348#endif
349
350static void parse_event(const char *msg, struct uevent *uevent)
351{
352 uevent->action = "";
353 uevent->path = "";
354 uevent->subsystem = "";
355 uevent->firmware = "";
356 uevent->major = -1;
357 uevent->minor = -1;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700358 uevent->partition_name = NULL;
359 uevent->partition_num = -1;
Wei Zhongf97b8872012-03-23 14:15:34 -0700360 uevent->device_name = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361
362 /* currently ignoring SEQNUM */
363 while(*msg) {
364 if(!strncmp(msg, "ACTION=", 7)) {
365 msg += 7;
366 uevent->action = msg;
367 } else if(!strncmp(msg, "DEVPATH=", 8)) {
368 msg += 8;
369 uevent->path = msg;
370 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
371 msg += 10;
372 uevent->subsystem = msg;
373 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
374 msg += 9;
375 uevent->firmware = msg;
376 } else if(!strncmp(msg, "MAJOR=", 6)) {
377 msg += 6;
378 uevent->major = atoi(msg);
379 } else if(!strncmp(msg, "MINOR=", 6)) {
380 msg += 6;
381 uevent->minor = atoi(msg);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700382 } else if(!strncmp(msg, "PARTN=", 6)) {
383 msg += 6;
384 uevent->partition_num = atoi(msg);
385 } else if(!strncmp(msg, "PARTNAME=", 9)) {
386 msg += 9;
387 uevent->partition_name = msg;
Wei Zhongf97b8872012-03-23 14:15:34 -0700388 } else if(!strncmp(msg, "DEVNAME=", 8)) {
389 msg += 8;
390 uevent->device_name = msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700391 }
392
Wei Zhongf97b8872012-03-23 14:15:34 -0700393 /* advance to after the next \0 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700394 while(*msg++)
395 ;
396 }
397
398 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
399 uevent->action, uevent->path, uevent->subsystem,
400 uevent->firmware, uevent->major, uevent->minor);
401}
402
Benoit Gobyd2278632010-08-03 14:36:02 -0700403static char **get_character_device_symlinks(struct uevent *uevent)
404{
405 const char *parent;
406 char *slash;
407 char **links;
408 int link_num = 0;
409 int width;
Dima Zavinf395c922013-03-06 16:23:57 -0800410 struct platform_node *pdev;
Benoit Gobyd2278632010-08-03 14:36:02 -0700411
Dima Zavinf395c922013-03-06 16:23:57 -0800412 pdev = find_platform_device(uevent->path);
413 if (!pdev)
Benoit Gobyd2278632010-08-03 14:36:02 -0700414 return NULL;
415
416 links = malloc(sizeof(char *) * 2);
417 if (!links)
418 return NULL;
419 memset(links, 0, sizeof(char *) * 2);
420
421 /* skip "/devices/platform/<driver>" */
Dima Zavinf395c922013-03-06 16:23:57 -0800422 parent = strchr(uevent->path + pdev->path_len, '/');
Benoit Gobyd2278632010-08-03 14:36:02 -0700423 if (!*parent)
424 goto err;
425
426 if (!strncmp(parent, "/usb", 4)) {
427 /* skip root hub name and device. use device interface */
428 while (*++parent && *parent != '/');
429 if (*parent)
430 while (*++parent && *parent != '/');
431 if (!*parent)
432 goto err;
433 slash = strchr(++parent, '/');
434 if (!slash)
435 goto err;
436 width = slash - parent;
437 if (width <= 0)
438 goto err;
439
440 if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
441 link_num++;
442 else
443 links[link_num] = NULL;
444 mkdir("/dev/usb", 0755);
445 }
446 else {
447 goto err;
448 }
449
450 return links;
451err:
452 free(links);
453 return NULL;
454}
455
Andrew Boiea885d042013-09-13 17:41:20 -0700456static char **get_block_device_symlinks(struct uevent *uevent)
Colin Crossb0ab94b2010-04-08 16:16:20 -0700457{
Colin Crossfadb85e2011-03-30 18:32:12 -0700458 const char *device;
Dima Zavinf395c922013-03-06 16:23:57 -0800459 struct platform_node *pdev;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700460 char *slash;
Andrew Boiea885d042013-09-13 17:41:20 -0700461 const char *type;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700462 int width;
463 char buf[256];
464 char link_path[256];
465 int fd;
466 int link_num = 0;
467 int ret;
468 char *p;
469 unsigned int size;
470 struct stat info;
471
Dima Zavinf395c922013-03-06 16:23:57 -0800472 pdev = find_platform_device(uevent->path);
Andrew Boiea885d042013-09-13 17:41:20 -0700473 if (pdev) {
474 device = pdev->name;
475 type = "platform";
476 } else if (!find_pci_device_prefix(uevent->path, buf, sizeof(buf))) {
477 device = buf;
478 type = "pci";
479 } else {
Dima Zavinf395c922013-03-06 16:23:57 -0800480 return NULL;
Andrew Boiea885d042013-09-13 17:41:20 -0700481 }
Dima Zavinf395c922013-03-06 16:23:57 -0800482
Colin Crossb0ab94b2010-04-08 16:16:20 -0700483 char **links = malloc(sizeof(char *) * 4);
484 if (!links)
485 return NULL;
486 memset(links, 0, sizeof(char *) * 4);
487
Andrew Boiea885d042013-09-13 17:41:20 -0700488 INFO("found %s device %s\n", type, device);
Colin Crossfadb85e2011-03-30 18:32:12 -0700489
Andrew Boiea885d042013-09-13 17:41:20 -0700490 snprintf(link_path, sizeof(link_path), "/dev/block/%s/%s", type, device);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700491
492 if (uevent->partition_name) {
493 p = strdup(uevent->partition_name);
494 sanitize(p);
Johan Redestig93ca79b2012-04-18 16:41:19 +0200495 if (strcmp(uevent->partition_name, p))
496 NOTICE("Linking partition '%s' as '%s'\n", uevent->partition_name, p);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700497 if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
498 link_num++;
499 else
500 links[link_num] = NULL;
501 free(p);
502 }
503
504 if (uevent->partition_num >= 0) {
505 if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
506 link_num++;
507 else
508 links[link_num] = NULL;
509 }
510
Dima Zavinf395c922013-03-06 16:23:57 -0800511 slash = strrchr(uevent->path, '/');
Colin Crossb0ab94b2010-04-08 16:16:20 -0700512 if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
513 link_num++;
514 else
515 links[link_num] = NULL;
516
517 return links;
Colin Crossb0ab94b2010-04-08 16:16:20 -0700518}
519
Colin Crosseb5ba832011-03-30 17:37:17 -0700520static void handle_device(const char *action, const char *devpath,
521 const char *path, int block, int major, int minor, char **links)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700522{
Colin Crossb0ab94b2010-04-08 16:16:20 -0700523 int i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700524
Colin Crosseb5ba832011-03-30 17:37:17 -0700525 if(!strcmp(action, "add")) {
526 make_device(devpath, path, block, major, minor);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700527 if (links) {
528 for (i = 0; links[i]; i++)
529 make_link(devpath, links[i]);
530 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700531 }
532
Colin Crosseb5ba832011-03-30 17:37:17 -0700533 if(!strcmp(action, "remove")) {
Colin Crossb0ab94b2010-04-08 16:16:20 -0700534 if (links) {
535 for (i = 0; links[i]; i++)
536 remove_link(devpath, links[i]);
537 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700538 unlink(devpath);
Colin Crossb0ab94b2010-04-08 16:16:20 -0700539 }
540
541 if (links) {
542 for (i = 0; links[i]; i++)
543 free(links[i]);
544 free(links);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700545 }
546}
547
Colin Crossfadb85e2011-03-30 18:32:12 -0700548static void handle_platform_device_event(struct uevent *uevent)
549{
Dima Zavinf395c922013-03-06 16:23:57 -0800550 const char *path = uevent->path;
Colin Crossfadb85e2011-03-30 18:32:12 -0700551
552 if (!strcmp(uevent->action, "add"))
Dima Zavinf395c922013-03-06 16:23:57 -0800553 add_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700554 else if (!strcmp(uevent->action, "remove"))
Dima Zavinf395c922013-03-06 16:23:57 -0800555 remove_platform_device(path);
Colin Crossfadb85e2011-03-30 18:32:12 -0700556}
557
Colin Crosseb5ba832011-03-30 17:37:17 -0700558static const char *parse_device_name(struct uevent *uevent, unsigned int len)
559{
560 const char *name;
561
562 /* if it's not a /dev device, nothing else to do */
563 if((uevent->major < 0) || (uevent->minor < 0))
564 return NULL;
565
566 /* do we have a name? */
567 name = strrchr(uevent->path, '/');
568 if(!name)
569 return NULL;
570 name++;
571
572 /* too-long names would overrun our buffer */
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800573 if(strlen(name) > len) {
574 ERROR("DEVPATH=%s exceeds %u-character limit on filename; ignoring event\n",
575 name, len);
Colin Crosseb5ba832011-03-30 17:37:17 -0700576 return NULL;
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800577 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700578
579 return name;
580}
581
582static void handle_block_device_event(struct uevent *uevent)
583{
584 const char *base = "/dev/block/";
585 const char *name;
586 char devpath[96];
587 char **links = NULL;
588
589 name = parse_device_name(uevent, 64);
590 if (!name)
591 return;
592
593 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500594 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700595
Dima Zavinf395c922013-03-06 16:23:57 -0800596 if (!strncmp(uevent->path, "/devices/", 9))
Andrew Boiea885d042013-09-13 17:41:20 -0700597 links = get_block_device_symlinks(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700598
599 handle_device(uevent->action, devpath, uevent->path, 1,
600 uevent->major, uevent->minor, links);
Colin Crosseb5ba832011-03-30 17:37:17 -0700601}
602
Greg Hackmann3312aa82013-11-18 15:24:40 -0800603#define DEVPATH_LEN 96
604
605static bool assemble_devpath(char *devpath, const char *dirname,
606 const char *devname)
607{
608 int s = snprintf(devpath, DEVPATH_LEN, "%s/%s", dirname, devname);
609 if (s < 0) {
610 ERROR("failed to assemble device path (%s); ignoring event\n",
611 strerror(errno));
612 return false;
613 } else if (s >= DEVPATH_LEN) {
614 ERROR("%s/%s exceeds %u-character limit on path; ignoring event\n",
615 dirname, devname, DEVPATH_LEN);
616 return false;
617 }
618 return true;
619}
620
621static void mkdir_recursive_for_devpath(const char *devpath)
622{
623 char dir[DEVPATH_LEN];
624 char *slash;
625
626 strcpy(dir, devpath);
627 slash = strrchr(dir, '/');
628 *slash = '\0';
629 mkdir_recursive(dir, 0755);
630}
631
Mark Salyzyn154f4602014-02-20 14:59:07 -0800632static inline void __attribute__((__deprecated__)) kernel_logger()
633{
634 INFO("kernel logger is deprecated\n");
635}
636
Colin Crosseb5ba832011-03-30 17:37:17 -0700637static void handle_generic_device_event(struct uevent *uevent)
638{
639 char *base;
640 const char *name;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800641 char devpath[DEVPATH_LEN] = {0};
Colin Crosseb5ba832011-03-30 17:37:17 -0700642 char **links = NULL;
643
644 name = parse_device_name(uevent, 64);
645 if (!name)
646 return;
647
Greg Hackmann3312aa82013-11-18 15:24:40 -0800648 struct ueventd_subsystem *subsystem =
649 ueventd_subsystem_find_by_name(uevent->subsystem);
650
651 if (subsystem) {
652 const char *devname;
653
654 switch (subsystem->devname_src) {
655 case DEVNAME_UEVENT_DEVNAME:
656 devname = uevent->device_name;
657 break;
658
659 case DEVNAME_UEVENT_DEVPATH:
660 devname = name;
661 break;
662
663 default:
664 ERROR("%s subsystem's devpath option is not set; ignoring event\n",
665 uevent->subsystem);
666 return;
667 }
668
669 if (!assemble_devpath(devpath, subsystem->dirname, devname))
670 return;
671 mkdir_recursive_for_devpath(devpath);
672 } else if (!strncmp(uevent->subsystem, "usb", 3)) {
Colin Crosseb5ba832011-03-30 17:37:17 -0700673 if (!strcmp(uevent->subsystem, "usb")) {
Wei Zhongf97b8872012-03-23 14:15:34 -0700674 if (uevent->device_name) {
Greg Hackmann3312aa82013-11-18 15:24:40 -0800675 if (!assemble_devpath(devpath, "/dev", uevent->device_name))
Greg Hackmannf6e009e2013-11-21 13:26:48 -0800676 return;
Greg Hackmann3312aa82013-11-18 15:24:40 -0800677 mkdir_recursive_for_devpath(devpath);
Wei Zhongf97b8872012-03-23 14:15:34 -0700678 }
679 else {
680 /* This imitates the file system that would be created
681 * if we were using devfs instead.
682 * Minors are broken up into groups of 128, starting at "001"
683 */
684 int bus_id = uevent->minor / 128 + 1;
685 int device_id = uevent->minor % 128 + 1;
686 /* build directories */
687 make_dir("/dev/bus", 0755);
688 make_dir("/dev/bus/usb", 0755);
689 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
690 make_dir(devpath, 0755);
691 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
692 }
Colin Crosseb5ba832011-03-30 17:37:17 -0700693 } else {
694 /* ignore other USB events */
695 return;
696 }
697 } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
698 base = "/dev/graphics/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500699 make_dir(base, 0755);
Lukasz Anaczkowskie6f8d452011-09-28 15:30:07 +0200700 } else if (!strncmp(uevent->subsystem, "drm", 3)) {
701 base = "/dev/dri/";
702 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700703 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
704 base = "/dev/oncrpc/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500705 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700706 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
707 base = "/dev/adsp/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500708 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700709 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
710 base = "/dev/msm_camera/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500711 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700712 } else if(!strncmp(uevent->subsystem, "input", 5)) {
713 base = "/dev/input/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500714 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700715 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
716 base = "/dev/mtd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500717 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700718 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
719 base = "/dev/snd/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500720 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700721 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
722 !strncmp(name, "log_", 4)) {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800723 kernel_logger();
Colin Crosseb5ba832011-03-30 17:37:17 -0700724 base = "/dev/log/";
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500725 make_dir(base, 0755);
Colin Crosseb5ba832011-03-30 17:37:17 -0700726 name += 4;
727 } else
728 base = "/dev/";
729 links = get_character_device_symlinks(uevent);
730
731 if (!devpath[0])
732 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
733
734 handle_device(uevent->action, devpath, uevent->path, 0,
735 uevent->major, uevent->minor, links);
736}
737
738static void handle_device_event(struct uevent *uevent)
739{
Ruchi Kandoi75b287b2014-04-29 19:14:37 -0700740 if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change") || !strcmp(uevent->action, "online"))
Colin Crosseb5ba832011-03-30 17:37:17 -0700741 fixup_sys_perms(uevent->path);
742
743 if (!strncmp(uevent->subsystem, "block", 5)) {
744 handle_block_device_event(uevent);
Colin Crossfadb85e2011-03-30 18:32:12 -0700745 } else if (!strncmp(uevent->subsystem, "platform", 8)) {
746 handle_platform_device_event(uevent);
Colin Crosseb5ba832011-03-30 17:37:17 -0700747 } else {
748 handle_generic_device_event(uevent);
749 }
750}
751
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700752static int load_firmware(int fw_fd, int loading_fd, int data_fd)
753{
754 struct stat st;
755 long len_to_copy;
756 int ret = 0;
757
758 if(fstat(fw_fd, &st) < 0)
759 return -1;
760 len_to_copy = st.st_size;
761
762 write(loading_fd, "1", 1); /* start transfer */
763
764 while (len_to_copy > 0) {
765 char buf[PAGE_SIZE];
766 ssize_t nr;
767
768 nr = read(fw_fd, buf, sizeof(buf));
769 if(!nr)
770 break;
771 if(nr < 0) {
772 ret = -1;
773 break;
774 }
775
776 len_to_copy -= nr;
777 while (nr > 0) {
778 ssize_t nw = 0;
779
780 nw = write(data_fd, buf + nw, nr);
781 if(nw <= 0) {
782 ret = -1;
783 goto out;
784 }
785 nr -= nw;
786 }
787 }
788
789out:
790 if(!ret)
791 write(loading_fd, "0", 1); /* successful end of transfer */
792 else
793 write(loading_fd, "-1", 2); /* abort transfer */
794
795 return ret;
796}
797
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700798static int is_booting(void)
799{
800 return access("/dev/.booting", F_OK) == 0;
801}
802
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803static void process_firmware_event(struct uevent *uevent)
804{
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700805 char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806 int l, loading_fd, data_fd, fw_fd;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700807 int booting = is_booting();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700808
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700809 INFO("firmware: loading '%s' for '%s'\n",
810 uevent->firmware, uevent->path);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811
812 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
813 if (l == -1)
814 return;
815
816 l = asprintf(&loading, "%sloading", root);
817 if (l == -1)
818 goto root_free_out;
819
820 l = asprintf(&data, "%sdata", root);
821 if (l == -1)
822 goto loading_free_out;
823
Brian Swetland02863b92010-09-19 03:36:39 -0700824 l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
825 if (l == -1)
826 goto data_free_out;
827
828 l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829 if (l == -1)
830 goto data_free_out;
831
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700832 l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
833 if (l == -1)
834 goto data_free_out;
835
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700836 loading_fd = open(loading, O_WRONLY);
837 if(loading_fd < 0)
838 goto file_free_out;
839
840 data_fd = open(data, O_WRONLY);
841 if(data_fd < 0)
842 goto loading_close_out;
843
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700844try_loading_again:
Brian Swetland02863b92010-09-19 03:36:39 -0700845 fw_fd = open(file1, O_RDONLY);
846 if(fw_fd < 0) {
847 fw_fd = open(file2, O_RDONLY);
Benoit Goby609d8822010-11-09 18:10:24 -0800848 if (fw_fd < 0) {
Iliyan Malchev029d44e2012-06-08 10:42:26 -0700849 fw_fd = open(file3, O_RDONLY);
850 if (fw_fd < 0) {
851 if (booting) {
852 /* If we're not fully booted, we may be missing
853 * filesystems needed for firmware, wait and retry.
854 */
855 usleep(100000);
856 booting = is_booting();
857 goto try_loading_again;
858 }
859 INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
860 write(loading_fd, "-1", 2);
861 goto data_close_out;
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700862 }
Benoit Goby609d8822010-11-09 18:10:24 -0800863 }
Brian Swetland02863b92010-09-19 03:36:39 -0700864 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700865
866 if(!load_firmware(fw_fd, loading_fd, data_fd))
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700867 INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700868 else
Brian Swetland8d48c8e2011-03-24 15:45:30 -0700869 INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700870
871 close(fw_fd);
872data_close_out:
873 close(data_fd);
874loading_close_out:
875 close(loading_fd);
876file_free_out:
Brian Swetland02863b92010-09-19 03:36:39 -0700877 free(file1);
878 free(file2);
Ajay Dudani76c58892013-06-12 15:55:26 -0700879 free(file3);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700880data_free_out:
881 free(data);
882loading_free_out:
883 free(loading);
884root_free_out:
885 free(root);
886}
887
888static void handle_firmware_event(struct uevent *uevent)
889{
890 pid_t pid;
Colin Cross982a8152010-06-03 12:21:01 -0700891 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700892
893 if(strcmp(uevent->subsystem, "firmware"))
894 return;
895
896 if(strcmp(uevent->action, "add"))
897 return;
898
899 /* we fork, to avoid making large memory allocations in init proper */
900 pid = fork();
901 if (!pid) {
902 process_firmware_event(uevent);
903 exit(EXIT_SUCCESS);
904 }
905}
906
907#define UEVENT_MSG_LEN 1024
Colin Cross0dd7ca62010-04-13 19:25:51 -0700908void handle_device_fd()
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700909{
Vernon Tang3f582e92011-04-25 13:08:17 +1000910 char msg[UEVENT_MSG_LEN+2];
911 int n;
Nick Kralevich57de8b82011-05-11 14:58:24 -0700912 while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700913 if(n >= UEVENT_MSG_LEN) /* overflow -- discard */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700914 continue;
915
916 msg[n] = '\0';
917 msg[n+1] = '\0';
918
Nick Kralevich5f5d5c82010-07-19 14:31:20 -0700919 struct uevent uevent;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700920 parse_event(msg, &uevent);
921
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400922 if (sehandle && selinux_status_updated() > 0) {
923 struct selabel_handle *sehandle2;
924 sehandle2 = selinux_android_file_context_handle();
925 if (sehandle2) {
926 selabel_close(sehandle);
927 sehandle = sehandle2;
928 }
929 }
930
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700931 handle_device_event(&uevent);
932 handle_firmware_event(&uevent);
933 }
934}
935
936/* Coldboot walks parts of the /sys tree and pokes the uevent files
937** to cause the kernel to regenerate device add events that happened
938** before init's device manager was started
939**
940** We drain any pending events from the netlink socket every time
941** we poke another uevent file to make sure we don't overrun the
942** socket's buffer.
943*/
944
Colin Cross0dd7ca62010-04-13 19:25:51 -0700945static void do_coldboot(DIR *d)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700946{
947 struct dirent *de;
948 int dfd, fd;
949
950 dfd = dirfd(d);
951
952 fd = openat(dfd, "uevent", O_WRONLY);
953 if(fd >= 0) {
954 write(fd, "add\n", 4);
955 close(fd);
Colin Cross0dd7ca62010-04-13 19:25:51 -0700956 handle_device_fd();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700957 }
958
959 while((de = readdir(d))) {
960 DIR *d2;
961
962 if(de->d_type != DT_DIR || de->d_name[0] == '.')
963 continue;
964
965 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
966 if(fd < 0)
967 continue;
968
969 d2 = fdopendir(fd);
970 if(d2 == 0)
971 close(fd);
972 else {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700973 do_coldboot(d2);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700974 closedir(d2);
975 }
976 }
977}
978
Colin Cross0dd7ca62010-04-13 19:25:51 -0700979static void coldboot(const char *path)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700980{
981 DIR *d = opendir(path);
982 if(d) {
Colin Cross0dd7ca62010-04-13 19:25:51 -0700983 do_coldboot(d);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700984 closedir(d);
985 }
986}
987
Colin Cross0dd7ca62010-04-13 19:25:51 -0700988void device_init(void)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700989{
990 suseconds_t t0, t1;
Colin Crossf83d0b92010-04-21 12:04:20 -0700991 struct stat info;
992 int fd;
Kenny Rootb5982bf2012-10-16 23:07:05 -0700993
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400994 sehandle = NULL;
995 if (is_selinux_enabled() > 0) {
996 sehandle = selinux_android_file_context_handle();
Stephen Smalleye2eb69d2013-04-16 09:30:30 -0400997 selinux_status_open(true);
Stephen Smalleyae6f3d72012-05-01 15:02:53 -0400998 }
Kenny Rootb5982bf2012-10-16 23:07:05 -0700999
Andrew Boied562ca72012-12-04 15:47:20 -08001000 /* is 256K enough? udev uses 16MB! */
1001 device_fd = uevent_open_socket(256*1024, true);
Colin Cross0dd7ca62010-04-13 19:25:51 -07001002 if(device_fd < 0)
1003 return;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001004
Colin Cross0dd7ca62010-04-13 19:25:51 -07001005 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
1006 fcntl(device_fd, F_SETFL, O_NONBLOCK);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001007
Colin Crossf83d0b92010-04-21 12:04:20 -07001008 if (stat(coldboot_done, &info) < 0) {
1009 t0 = get_usecs();
1010 coldboot("/sys/class");
1011 coldboot("/sys/block");
1012 coldboot("/sys/devices");
1013 t1 = get_usecs();
1014 fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
1015 close(fd);
1016 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
1017 } else {
1018 log_event_print("skipping coldboot, already done\n");
1019 }
Colin Cross0dd7ca62010-04-13 19:25:51 -07001020}
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001021
Colin Cross0dd7ca62010-04-13 19:25:51 -07001022int get_device_fd()
1023{
1024 return device_fd;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001025}