merge in ics-release history after reset to master
diff --git a/charger/Android.mk b/charger/Android.mk
new file mode 100644
index 0000000..75e78d5
--- /dev/null
+++ b/charger/Android.mk
@@ -0,0 +1,20 @@
+# Copyright 2011 The Android Open Source Project
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
+	charger.c
+
+LOCAL_MODULE := charger
+LOCAL_MODULE_TAGS := optional
+LOCAL_FORCE_STATIC_EXECUTABLE := true
+LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)
+LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
+
+LOCAL_C_INCLUDES := bootable/recovery
+
+LOCAL_STATIC_LIBRARIES := libminui libpixelflinger_static libpng
+LOCAL_STATIC_LIBRARIES += libz libstdc++ libcutils libc
+
+include $(BUILD_EXECUTABLE)
diff --git a/charger/charger.c b/charger/charger.c
new file mode 100644
index 0000000..5397487
--- /dev/null
+++ b/charger/charger.c
@@ -0,0 +1,726 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#define DEBUG_UEVENTS
+#define CHARGER_KLOG_LEVEL 6
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/input.h>
+#include <linux/netlink.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/poll.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <cutils/android_reboot.h>
+#include <cutils/klog.h>
+#include <cutils/list.h>
+#include <cutils/uevent.h>
+
+#include "minui/minui.h"
+
+#ifndef max
+#define max(a,b) ((a) > (b) ? (a) : (b))
+#endif
+
+#ifndef min
+#define min(a,b) ((a) < (b) ? (a) : (b))
+#endif
+
+#define MSEC_PER_SEC            (1000LL)
+#define NSEC_PER_MSEC           (1000000LL)
+
+#define SCREEN_ON_TIME          (5 * MSEC_PER_SEC)
+#define POWER_ON_KEY_TIME       (5 * MSEC_PER_SEC)
+#define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
+
+#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
+#define LOGI(x...) do { KLOG_INFO("charger", x); } while (0)
+#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
+
+struct key_state {
+    bool pending;
+    bool down;
+    int64_t timestamp;
+};
+
+struct power_supply {
+    struct listnode list;
+    char name[256];
+    char type[32];
+    bool online;
+    bool valid;
+};
+
+struct charger {
+    int64_t next_screen_transition;
+    int64_t next_key_check;
+    int64_t next_pwr_check;
+    bool screen_on;
+
+    struct key_state keys[KEY_MAX + 1];
+    gr_surface surf_charging;
+    int uevent_fd;
+
+    struct listnode supplies;
+    int num_supplies;
+    int num_supplies_online;
+
+    struct power_supply *battery;
+};
+
+struct uevent {
+    const char *action;
+    const char *path;
+    const char *subsystem;
+    const char *ps_name;
+    const char *ps_type;
+    const char *ps_online;
+};
+
+static int char_width;
+static int char_height;
+
+struct charger charger_state;
+
+/* current time in milliseconds */
+static int64_t curr_time_ms(void)
+{
+    struct timespec tm;
+    clock_gettime(CLOCK_MONOTONIC, &tm);
+    return tm.tv_sec * MSEC_PER_SEC + (tm.tv_nsec / NSEC_PER_MSEC);
+}
+
+static void clear_screen(void)
+{
+    gr_color(0, 0, 0, 255);
+    gr_fill(0, 0, gr_fb_width(), gr_fb_height());
+};
+
+static int read_file(const char *path, char *buf, size_t sz)
+{
+    int fd;
+    size_t cnt;
+
+    fd = open(path, O_RDONLY, 0);
+    if (fd < 0)
+        goto err;
+
+    cnt = read(fd, buf, sz - 1);
+    if (cnt <= 0)
+        goto err;
+    buf[cnt] = '\0';
+    if (buf[cnt - 1] == '\n') {
+        cnt--;
+        buf[cnt] = '\0';
+    }
+
+    close(fd);
+    return cnt;
+
+err:
+    if (fd >= 0)
+        close(fd);
+    return -1;
+}
+
+static int read_file_int(const char *path, int *val)
+{
+    char buf[32];
+    int ret;
+    int tmp;
+    char *end;
+
+    ret = read_file(path, buf, sizeof(buf));
+    if (ret < 0)
+        return -1;
+
+    tmp = strtol(buf, &end, 0);
+    if (end == buf ||
+        ((end < buf+sizeof(buf)) && (*end != '\n' && *end != '\0')))
+        goto err;
+
+    *val = tmp;
+    return 0;
+
+err:
+    return -1;
+}
+
+static struct power_supply *find_supply(struct charger *charger,
+                                        const char *name)
+{
+    struct listnode *node;
+    struct power_supply *supply;
+
+    list_for_each(node, &charger->supplies) {
+        supply = node_to_item(node, struct power_supply, list);
+        if (!strncmp(name, supply->name, sizeof(supply->name)))
+            return supply;
+    }
+    return NULL;
+}
+
+static struct power_supply *add_supply(struct charger *charger,
+                                       const char *name, const char *type,
+                                       bool online)
+{
+    struct power_supply *supply;
+
+    supply = calloc(1, sizeof(struct power_supply));
+    if (!supply)
+        return NULL;
+
+    strlcpy(supply->name, name, sizeof(supply->name));
+    strlcpy(supply->type, type, sizeof(supply->type));
+    supply->online = online;
+    list_add_tail(&charger->supplies, &supply->list);
+    charger->num_supplies++;
+    LOGV("... added %s %s %d\n", supply->name, supply->type, online);
+    return supply;
+}
+
+static void remove_supply(struct charger *charger, struct power_supply *supply)
+{
+    if (!supply)
+        return;
+    list_remove(&supply->list);
+    charger->num_supplies--;
+    free(supply);
+}
+
+static void parse_uevent(const char *msg, struct uevent *uevent)
+{
+    uevent->action = "";
+    uevent->path = "";
+    uevent->subsystem = "";
+    uevent->ps_name = "";
+    uevent->ps_online = "";
+    uevent->ps_type = "";
+
+    /* currently ignoring SEQNUM */
+    while (*msg) {
+#ifdef DEBUG_UEVENTS
+        LOGV("uevent str: %s\n", msg);
+#endif
+        if (!strncmp(msg, "ACTION=", 7)) {
+            msg += 7;
+            uevent->action = msg;
+        } else if (!strncmp(msg, "DEVPATH=", 8)) {
+            msg += 8;
+            uevent->path = msg;
+        } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
+            msg += 10;
+            uevent->subsystem = msg;
+        } else if (!strncmp(msg, "POWER_SUPPLY_NAME=", 18)) {
+            msg += 18;
+            uevent->ps_name = msg;
+        } else if (!strncmp(msg, "POWER_SUPPLY_ONLINE=", 20)) {
+            msg += 20;
+            uevent->ps_online = msg;
+        } else if (!strncmp(msg, "POWER_SUPPLY_TYPE=", 18)) {
+            msg += 18;
+            uevent->ps_type = msg;
+        }
+
+        /* advance to after the next \0 */
+        while (*msg++)
+            ;
+    }
+
+    LOGV("event { '%s', '%s', '%s', '%s', '%s', '%s' }\n",
+         uevent->action, uevent->path, uevent->subsystem,
+         uevent->ps_name, uevent->ps_type, uevent->ps_online);
+}
+
+static void process_ps_uevent(struct charger *charger, struct uevent *uevent)
+{
+    int online;
+    char ps_type[32];
+    struct power_supply *supply = NULL;
+    int i;
+    bool was_online = false;
+    bool battery = false;
+
+    if (uevent->ps_type[0] == '\0') {
+        char *path;
+        int ret;
+
+        if (uevent->path[0] == '\0')
+            return;
+        ret = asprintf(&path, "/sys/%s/type", uevent->path);
+        if (ret <= 0)
+            return;
+        ret = read_file(path, ps_type, sizeof(ps_type));
+        free(path);
+        if (ret < 0)
+            return;
+    } else {
+        strlcpy(ps_type, uevent->ps_type, sizeof(ps_type));
+    }
+
+    if (!strncmp(ps_type, "Battery", 7))
+        battery = true;
+
+    online = atoi(uevent->ps_online);
+    supply = find_supply(charger, uevent->ps_name);
+    if (supply) {
+        was_online = supply->online;
+        supply->online = online;
+    }
+
+    if (!strcmp(uevent->action, "add")) {
+        if (!supply) {
+            supply = add_supply(charger, uevent->ps_name, ps_type, online);
+            if (!supply) {
+                LOGE("cannot add supply '%s' (%s %d)\n", uevent->ps_name,
+                     uevent->ps_type, online);
+                return;
+            }
+            /* only pick up the first battery for now */
+            if (battery && !charger->battery)
+                charger->battery = supply;
+        } else {
+            LOGE("supply '%s' already exists..\n", uevent->ps_name);
+        }
+    } else if (!strcmp(uevent->action, "remove")) {
+        if (supply) {
+            if (charger->battery == supply)
+                charger->battery = NULL;
+            remove_supply(charger, supply);
+            supply = NULL;
+        }
+    } else if (!strcmp(uevent->action, "change")) {
+        if (!supply) {
+            LOGE("power supply '%s' not found ('%s' %d)\n",
+                 uevent->ps_name, ps_type, online);
+            return;
+        }
+    } else {
+        return;
+    }
+
+    /* allow battery to be managed in the supply list but make it not
+     * contribute to online power supplies. */
+    if (!battery) {
+        if (was_online && !online)
+            charger->num_supplies_online--;
+        else if (supply && !was_online && online)
+            charger->num_supplies_online++;
+    }
+
+    LOGI("power supply %s (%s) %s (action=%s num_online=%d num_supplies=%d)\n",
+         uevent->ps_name, ps_type, battery ? "" : online ? "online" : "offline",
+         uevent->action, charger->num_supplies_online, charger->num_supplies);
+}
+
+static void process_uevent(struct charger *charger, struct uevent *uevent)
+{
+    if (!strcmp(uevent->subsystem, "power_supply"))
+        process_ps_uevent(charger, uevent);
+}
+
+#define UEVENT_MSG_LEN  1024
+static int handle_uevent_fd(struct charger *charger, int fd)
+{
+    char msg[UEVENT_MSG_LEN+2];
+    int n;
+
+    if (fd < 0)
+        return -1;
+
+    while (true) {
+        struct uevent uevent;
+
+        n = uevent_kernel_multicast_recv(fd, msg, UEVENT_MSG_LEN);
+        if (n <= 0)
+            break;
+        if (n >= UEVENT_MSG_LEN)   /* overflow -- discard */
+            continue;
+
+        msg[n] = '\0';
+        msg[n+1] = '\0';
+
+        parse_uevent(msg, &uevent);
+        process_uevent(charger, &uevent);
+    }
+
+    return 0;
+}
+
+static int uevent_callback(int fd, short revents, void *data)
+{
+    struct charger *charger = data;
+
+    if (!(revents & POLLIN))
+        return -1;
+    return handle_uevent_fd(charger, fd);
+}
+
+/* force the kernel to regenerate the change events for the existing
+ * devices, if valid */
+static void do_coldboot(struct charger *charger, DIR *d, const char *event,
+                        bool follow_links, int max_depth)
+{
+    struct dirent *de;
+    int dfd, fd;
+
+    dfd = dirfd(d);
+
+    fd = openat(dfd, "uevent", O_WRONLY);
+    if (fd >= 0) {
+        write(fd, event, strlen(event));
+        close(fd);
+        handle_uevent_fd(charger, charger->uevent_fd);
+    }
+
+    while ((de = readdir(d)) && max_depth > 0) {
+        DIR *d2;
+
+        LOGV("looking at '%s'\n", de->d_name);
+
+        if ((de->d_type != DT_DIR && !(de->d_type == DT_LNK && follow_links)) ||
+           de->d_name[0] == '.') {
+            LOGV("skipping '%s' type %d (depth=%d follow=%d)\n",
+                 de->d_name, de->d_type, max_depth, follow_links);
+            continue;
+        }
+        LOGV("can descend into '%s'\n", de->d_name);
+
+        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
+        if (fd < 0) {
+            LOGE("cannot openat %d '%s' (%d: %s)\n", dfd, de->d_name,
+                 errno, strerror(errno));
+            continue;
+        }
+
+        d2 = fdopendir(fd);
+        if (d2 == 0)
+            close(fd);
+        else {
+            LOGV("opened '%s'\n", de->d_name);
+            do_coldboot(charger, d2, event, follow_links, max_depth - 1);
+            closedir(d2);
+        }
+    }
+}
+
+static void coldboot(struct charger *charger, const char *path,
+                     const char *event)
+{
+    char str[256];
+
+    LOGV("doing coldboot '%s' in '%s'\n", event, path);
+    DIR *d = opendir(path);
+    if (d) {
+        snprintf(str, sizeof(str), "%s\n", event);
+        do_coldboot(charger, d, str, true, 1);
+        closedir(d);
+    }
+}
+
+static int draw_text(const char *str, int x, int y)
+{
+    int str_len_px = gr_measure(str);
+
+    if (x < 0)
+        x = (gr_fb_width() - str_len_px) / 2;
+    if (y < 0)
+        y = (gr_fb_height() - char_height) / 2;
+    gr_text(x, y, str);
+
+    return y + char_height;
+}
+
+static void android_green(void)
+{
+    gr_color(0xa4, 0xc6, 0x39, 255);
+}
+
+static void redraw_screen(struct charger *charger)
+{
+    int surf_height;
+    int surf_width;
+    int x;
+    int y = 0;
+    int batt_cap;
+    int ret;
+    char cap_string[128];
+    char cap_path[256];
+
+    clear_screen();
+
+    if (charger->surf_charging) {
+        surf_width = gr_get_width(charger->surf_charging);
+        surf_height = gr_get_height(charger->surf_charging);
+        x = (gr_fb_width() - surf_width) / 2 ;
+        y = (gr_fb_height() - surf_height) / 2 ;
+
+        gr_blit(charger->surf_charging, 0, 0,
+                surf_width, surf_height,
+                x, y);
+        y += surf_height;
+    } else {
+        android_green();
+        y = draw_text("Charging!", -1, -1);
+    }
+
+    cap_string[0] = '\0';
+    if (charger->battery) {
+        ret = snprintf(cap_path, sizeof(cap_path),
+                       "/sys/class/power_supply/%s/capacity",
+                       charger->battery->name);
+        if (ret <= 0)
+            goto done;
+        ret = read_file_int(cap_path, &batt_cap);
+        if (ret >= 0)
+            snprintf(cap_string, sizeof(cap_string), "%d/100", batt_cap);
+    }
+
+    if (cap_string[0] == '\0')
+        snprintf(cap_string, sizeof(cap_string), "?\?/100");
+
+    y += 25;
+    android_green();
+    draw_text(cap_string, -1, y);
+
+done:
+    gr_flip();
+}
+
+static void update_screen_state(struct charger *charger, int64_t now,
+                                bool force)
+{
+    if (!force && ((now < charger->next_screen_transition) ||
+                   (charger->next_screen_transition == -1)))
+        return;
+
+    if (!charger->screen_on)
+        charger->next_screen_transition = now + SCREEN_ON_TIME;
+    else
+        charger->next_screen_transition = -1;
+    charger->screen_on = !charger->screen_on;
+
+    gr_fb_blank(!charger->screen_on);
+    if (charger->screen_on)
+        redraw_screen(charger);
+    LOGV("[%lld] screen %s\n", now, charger->screen_on ? "on" : "off");
+}
+
+static void update_input_state(struct charger *charger,
+                               struct input_event *ev,
+                               int64_t now)
+{
+    int down = !!ev->value;
+
+    if (ev->type != EV_KEY || ev->code > KEY_MAX)
+        return;
+
+    /* only record the down even timestamp, as the amount
+     * of time the key spent not being pressed is not useful */
+    if (down)
+        charger->keys[ev->code].timestamp = now;
+    charger->keys[ev->code].down = down;
+    charger->keys[ev->code].pending = true;
+    if (down) {
+        LOGV("[%lld] key[%d] down\n", now, ev->code);
+    } else {
+        int64_t duration = now - charger->keys[ev->code].timestamp;
+        int64_t secs = duration / 1000;
+        int64_t msecs = duration - secs * 1000;
+        LOGV("[%lld] key[%d] up (was down for %lld.%lldsec)\n", now,
+            ev->code, secs, msecs);
+    }
+}
+
+static void set_next_key_check(struct charger *charger,
+                               struct key_state *key,
+                               int64_t timeout)
+{
+    int64_t then = key->timestamp + timeout;
+
+    if (charger->next_key_check == -1 || then < charger->next_key_check)
+        charger->next_key_check = then;
+}
+
+static void process_key(struct charger *charger, int code, int64_t now)
+{
+    struct key_state *key = &charger->keys[code];
+    int64_t next_key_check;
+
+    if (code == KEY_POWER) {
+        if (key->down) {
+            int64_t reboot_timeout = key->timestamp + POWER_ON_KEY_TIME;
+            if (now >= reboot_timeout) {
+                LOGI("[%lld] rebooting\n", now);
+                android_reboot(ANDROID_RB_RESTART, 0, 0);
+            } else {
+                /* if the key is pressed but timeout hasn't expired,
+                 * make sure we wake up at the right-ish time to check
+                 */
+                set_next_key_check(charger, key, POWER_ON_KEY_TIME);
+            }
+        } else {
+            /* if the power key got released, force screen state cycle */
+            if (key->pending)
+                update_screen_state(charger, now, true);
+        }
+    }
+
+    key->pending = false;
+}
+
+static void handle_input_state(struct charger *charger, int64_t now)
+{
+    process_key(charger, KEY_POWER, now);
+
+    if (charger->next_key_check != -1 && now > charger->next_key_check)
+        charger->next_key_check = -1;
+}
+
+static void handle_power_supply_state(struct charger *charger, int64_t now)
+{
+    if (charger->num_supplies_online == 0) {
+        if (charger->next_pwr_check == -1) {
+            charger->next_pwr_check = now + UNPLUGGED_SHUTDOWN_TIME;
+            LOGI("[%lld] device unplugged: shutting down in %lld (@ %lld)\n",
+                 now, UNPLUGGED_SHUTDOWN_TIME, charger->next_pwr_check);
+        } else if (now >= charger->next_pwr_check) {
+            LOGI("[%lld] shutting down\n", now);
+            android_reboot(ANDROID_RB_POWEROFF, 0, 0);
+        } else {
+            /* otherwise we already have a shutdown timer scheduled */
+        }
+    } else {
+        /* online supply present, reset shutdown timer if set */
+        if (charger->next_pwr_check != -1) {
+            LOGI("[%lld] device plugged in: shutdown cancelled\n", now);
+            update_screen_state(charger, now, true);
+        }
+        charger->next_pwr_check = -1;
+    }
+}
+
+static void wait_next_event(struct charger *charger, int64_t now)
+{
+    int64_t next_event = INT64_MAX;
+    int64_t timeout;
+    struct input_event ev;
+    int ret;
+
+    LOGV("[%lld] next screen: %lld next key: %lld next pwr: %lld\n", now,
+         charger->next_screen_transition, charger->next_key_check,
+         charger->next_pwr_check);
+
+    /* TODO: right now it's just screen on/off and keys, but later I'm sure
+     *       there will be animations */
+    if (charger->next_screen_transition != -1)
+        next_event = charger->next_screen_transition;
+    if (charger->next_key_check != -1 && charger->next_key_check < next_event)
+        next_event = charger->next_key_check;
+    if (charger->next_pwr_check != -1 && charger->next_pwr_check < next_event)
+        next_event = charger->next_pwr_check;
+
+    if (next_event != -1 && next_event != INT64_MAX)
+        timeout = max(0, next_event - now);
+    else
+        timeout = -1;
+    LOGV("[%lld] blocking (%lld)\n", now, timeout);
+    ret = ev_wait((int)timeout);
+    if (!ret)
+        ev_dispatch();
+}
+
+static int input_callback(int fd, short revents, void *data)
+{
+    struct charger *charger = data;
+    struct input_event ev;
+    int ret;
+
+    ret = ev_get_input(fd, revents, &ev);
+    if (ret)
+        return -1;
+    update_input_state(charger, &ev, curr_time_ms());
+    return 0;
+}
+
+static void event_loop(struct charger *charger)
+{
+    int ret;
+
+    while (true) {
+        int64_t now = curr_time_ms();
+
+        LOGV("[%lld] event_loop()\n", now);
+        handle_input_state(charger, now);
+        update_screen_state(charger, now, false);
+        handle_power_supply_state(charger, now);
+
+        wait_next_event(charger, now);
+    }
+}
+
+int main(int argc, char **argv)
+{
+    int ret;
+    struct charger *charger = &charger_state;
+    int64_t now = curr_time_ms() - 1;
+    int fd;
+
+    list_init(&charger->supplies);
+
+    klog_init();
+    klog_set_level(CHARGER_KLOG_LEVEL);
+
+    gr_init();
+    gr_font_size(&char_width, &char_height);
+
+    ev_init(input_callback, charger);
+
+    fd = uevent_open_socket(64*1024, true);
+    if (fd >= 0) {
+        fcntl(fd, F_SETFL, O_NONBLOCK);
+        ev_add_fd(fd, uevent_callback, charger);
+    }
+    charger->uevent_fd = fd;
+    coldboot(charger, "/sys/class/power_supply", "add");
+
+    ret = res_create_surface("charging", &charger->surf_charging);
+    if (ret < 0) {
+        LOGE("Cannot load image\n");
+        charger->surf_charging = NULL;
+    }
+
+    gr_fb_blank(true);
+
+    charger->next_screen_transition = now - 1;
+    charger->next_key_check = -1;
+    charger->next_pwr_check = -1;
+    charger->screen_on = false;
+
+    event_loop(charger);
+
+    return 0;
+}
diff --git a/charger/images/charging.png b/charger/images/charging.png
new file mode 100644
index 0000000..94ce4b1
--- /dev/null
+++ b/charger/images/charging.png
Binary files differ
diff --git a/include/cutils/klog.h b/include/cutils/klog.h
new file mode 100644
index 0000000..1335543
--- /dev/null
+++ b/include/cutils/klog.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _CUTILS_KLOG_H_
+#define _CUTILS_KLOG_H_
+
+void klog_init(void);
+void klog_set_level(int level);
+void klog_close(void);
+void klog_write(int level, const char *fmt, ...)
+    __attribute__ ((format(printf, 2, 3)));
+
+#define KLOG_ERROR(tag,x...)   klog_write(3, "<3>" tag ": " x)
+#define KLOG_WARNING(tag,x...) klog_write(4, "<4>" tag ": " x)
+#define KLOG_NOTICE(tag,x...)  klog_write(5, "<5>" tag ": " x)
+#define KLOG_INFO(tag,x...)    klog_write(6, "<6>" tag ": " x)
+#define KLOG_DEBUG(tag,x...)   klog_write(7, "<7>" tag ": " x)
+
+#define KLOG_DEFAULT_LEVEL  3  /* messages <= this level are logged */
+
+#endif
diff --git a/init/list.h b/include/cutils/list.h
similarity index 96%
rename from init/list.h
rename to include/cutils/list.h
index 7b9ef32..eb5a3c8 100644
--- a/init/list.h
+++ b/include/cutils/list.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef _INIT_LIST_H_
-#define _INIT_LIST_H_
+#ifndef _CUTILS_LIST_H_
+#define _CUTILS_LIST_H_
 
 #include <stddef.h>
 
diff --git a/include/cutils/uevent.h b/include/cutils/uevent.h
index 5f5e6ca..4ebc300 100644
--- a/include/cutils/uevent.h
+++ b/include/cutils/uevent.h
@@ -17,12 +17,14 @@
 #ifndef __CUTILS_UEVENT_H
 #define __CUTILS_UEVENT_H
 
+#include <stdbool.h>
 #include <sys/socket.h>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+int uevent_open_socket(int buf_sz, bool passcred);
 ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length);
 
 #ifdef __cplusplus
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index fc71a1e..048864c 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -213,6 +213,7 @@
     { 00750, AID_ROOT,      AID_SHELL,     "sbin/*" },
     { 00755, AID_ROOT,      AID_ROOT,      "bin/*" },
     { 00750, AID_ROOT,      AID_SHELL,     "init*" },
+    { 00750, AID_ROOT,      AID_SHELL,     "charger*" },
     { 00644, AID_ROOT,      AID_ROOT,       0 },
 };
 
diff --git a/init/builtins.c b/init/builtins.c
index 06ef96d..eccda3f 100644
--- a/init/builtins.c
+++ b/init/builtins.c
@@ -663,7 +663,7 @@
 
 int do_loglevel(int nargs, char **args) {
     if (nargs == 2) {
-        log_set_level(atoi(args[1]));
+        klog_set_level(atoi(args[1]));
         return 0;
     }
     return -1;
diff --git a/init/devices.c b/init/devices.c
index 60659ce..a2f84aa 100644
--- a/init/devices.c
+++ b/init/devices.c
@@ -34,12 +34,12 @@
 #include <asm/page.h>
 #include <sys/wait.h>
 
+#include <cutils/list.h>
 #include <cutils/uevent.h>
 
 #include "devices.h"
 #include "util.h"
 #include "log.h"
-#include "list.h"
 
 #define SYSFS_PREFIX    "/sys"
 #define FIRMWARE_DIR1   "/etc/firmware"
@@ -58,33 +58,6 @@
     int minor;
 };
 
-static int open_uevent_socket(void)
-{
-    struct sockaddr_nl addr;
-    int sz = 64*1024; // XXX larger? udev uses 16MB!
-    int on = 1;
-    int s;
-
-    memset(&addr, 0, sizeof(addr));
-    addr.nl_family = AF_NETLINK;
-    addr.nl_pid = getpid();
-    addr.nl_groups = 0xffffffff;
-
-    s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
-    if(s < 0)
-        return -1;
-
-    setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
-    setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
-
-    if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-        close(s);
-        return -1;
-    }
-
-    return s;
-}
-
 struct perms_ {
     char *name;
     char *attr;
@@ -847,7 +820,8 @@
     struct stat info;
     int fd;
 
-    device_fd = open_uevent_socket();
+    /* is 64K enough? udev uses 16MB! */
+    device_fd = uevent_open_socket(64*1024, true);
     if(device_fd < 0)
         return;
 
diff --git a/init/init.c b/init/init.c
index ef42e02..5058b17 100755
--- a/init/init.c
+++ b/init/init.c
@@ -33,6 +33,7 @@
 #include <sys/un.h>
 #include <libgen.h>
 
+#include <cutils/list.h>
 #include <cutils/sockets.h>
 #include <cutils/iosched_policy.h>
 #include <private/android_filesystem_config.h>
@@ -42,7 +43,6 @@
 
 #include "devices.h"
 #include "init.h"
-#include "list.h"
 #include "log.h"
 #include "property_service.h"
 #include "bootchart.h"
@@ -724,7 +724,7 @@
          * talk to the outside world.
          */
     open_devnull_stdio();
-    log_init();
+    klog_init();
     
     INFO("reading config file\n");
     init_parse_config_file("/init.rc");
@@ -744,20 +744,27 @@
     queue_builtin_action(console_init_action, "console_init");
     queue_builtin_action(set_init_properties_action, "set_init_properties");
 
-        /* execute all the boot actions to get us started */
+    /* execute all the boot actions to get us started */
     action_for_each_trigger("init", action_add_queue_tail);
-    action_for_each_trigger("early-fs", action_add_queue_tail);
-    action_for_each_trigger("fs", action_add_queue_tail);
-    action_for_each_trigger("post-fs", action_add_queue_tail);
-    action_for_each_trigger("post-fs-data", action_add_queue_tail);
+
+    /* skip mounting filesystems in charger mode */
+    if (strcmp(bootmode, "charger") != 0) {
+        action_for_each_trigger("early-fs", action_add_queue_tail);
+        action_for_each_trigger("fs", action_add_queue_tail);
+        action_for_each_trigger("post-fs", action_add_queue_tail);
+        action_for_each_trigger("post-fs-data", action_add_queue_tail);
+    }
 
     queue_builtin_action(property_service_init_action, "property_service_init");
     queue_builtin_action(signal_init_action, "signal_init");
     queue_builtin_action(check_startup_action, "check_startup");
 
-    /* execute all the boot actions to get us started */
-    action_for_each_trigger("early-boot", action_add_queue_tail);
-    action_for_each_trigger("boot", action_add_queue_tail);
+    if (!strcmp(bootmode, "charger")) {
+        action_for_each_trigger("charger", action_add_queue_tail);
+    } else {
+        action_for_each_trigger("early-boot", action_add_queue_tail);
+        action_for_each_trigger("boot", action_add_queue_tail);
+    }
 
         /* run all property triggers based on current state of the properties */
     queue_builtin_action(queue_property_triggers_action, "queue_propety_triggers");
diff --git a/init/init.h b/init/init.h
index 05cdfaa..2d98c7c 100644
--- a/init/init.h
+++ b/init/init.h
@@ -17,7 +17,7 @@
 #ifndef _INIT_INIT_H
 #define _INIT_INIT_H
 
-#include "list.h"
+#include <cutils/list.h>
 
 #include <sys/stat.h>
 
diff --git a/init/init_parser.c b/init/init_parser.c
index d9d3084..fa813b9 100644
--- a/init/init_parser.c
+++ b/init/init_parser.c
@@ -27,11 +27,11 @@
 #include "parser.h"
 #include "init_parser.h"
 #include "log.h"
-#include "list.h"
 #include "property_service.h"
 #include "util.h"
 
 #include <cutils/iosched_policy.h>
+#include <cutils/list.h>
 
 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
 #include <sys/_system_properties.h>
diff --git a/init/log.h b/init/log.h
index 3d93965..4aac3df 100644
--- a/init/log.h
+++ b/init/log.h
@@ -17,17 +17,12 @@
 #ifndef _INIT_LOG_H_
 #define _INIT_LOG_H_
 
-void log_init(void);
-void log_set_level(int level);
-void log_close(void);
-void log_write(int level, const char *fmt, ...)
-    __attribute__ ((format(printf, 2, 3)));
+#include <cutils/klog.h>
 
-#define ERROR(x...)   log_write(3, "<3>init: " x)
-#define NOTICE(x...)  log_write(5, "<5>init: " x)
-#define INFO(x...)    log_write(6, "<6>init: " x)
+#define ERROR(x...)   KLOG_ERROR("init", x)
+#define NOTICE(x...)  KLOG_NOTICE("init", x)
+#define INFO(x...)    KLOG_INFO("init", x)
 
-#define LOG_DEFAULT_LEVEL  3  /* messages <= this level are logged */
 #define LOG_UEVENTS        0  /* log uevent messages if 1. verbose */
 
 #endif
diff --git a/init/parser.c b/init/parser.c
index 3c2ec00..48e7aec 100644
--- a/init/parser.c
+++ b/init/parser.c
@@ -3,7 +3,6 @@
 #include <string.h>
 
 #include "parser.h"
-#include "list.h"
 #include "log.h"
 
 #define RAW(x...) log_write(6, x)
diff --git a/init/signal_handler.c b/init/signal_handler.c
index f89d058..b170132 100644
--- a/init/signal_handler.c
+++ b/init/signal_handler.c
@@ -24,9 +24,9 @@
 #include <sys/wait.h>
 #include <cutils/sockets.h>
 #include <cutils/android_reboot.h>
+#include <cutils/list.h>
 
 #include "init.h"
-#include "list.h"
 #include "util.h"
 #include "log.h"
 
diff --git a/init/ueventd.c b/init/ueventd.c
index 1328d19..ddf42be 100644
--- a/init/ueventd.c
+++ b/init/ueventd.c
@@ -47,7 +47,7 @@
     signal(SIGCHLD, SIG_IGN);
 
     open_devnull_stdio();
-    log_init();
+    klog_init();
 
     INFO("starting ueventd\n");
 
diff --git a/init/ueventd_parser.c b/init/ueventd_parser.c
index 0dd8b4d..3e60df5 100644
--- a/init/ueventd_parser.c
+++ b/init/ueventd_parser.c
@@ -22,7 +22,6 @@
 #include "ueventd_parser.h"
 #include "parser.h"
 #include "log.h"
-#include "list.h"
 #include "util.h"
 
 static void parse_line_device(struct parse_state *state, int nargs, char **args);
diff --git a/init/util.c b/init/util.c
index d8ec88e..fd4bee2 100755
--- a/init/util.c
+++ b/init/util.c
@@ -34,45 +34,8 @@
 #include <private/android_filesystem_config.h>
 
 #include "log.h"
-#include "list.h"
 #include "util.h"
 
-static int log_fd = -1;
-/* Inital log level before init.rc is parsed and this this is reset. */
-static int log_level = LOG_DEFAULT_LEVEL;
-
-
-void log_set_level(int level) {
-    log_level = level;
-}
-
-void log_init(void)
-{
-    static const char *name = "/dev/__kmsg__";
-    if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
-        log_fd = open(name, O_WRONLY);
-        fcntl(log_fd, F_SETFD, FD_CLOEXEC);
-        unlink(name);
-    }
-}
-
-#define LOG_BUF_MAX 512
-
-void log_write(int level, const char *fmt, ...)
-{
-    char buf[LOG_BUF_MAX];
-    va_list ap;
-    
-    if (level > log_level) return;
-    if (log_fd < 0) return;
-    
-    va_start(ap, fmt);
-    vsnprintf(buf, LOG_BUF_MAX, fmt, ap);
-    buf[LOG_BUF_MAX - 1] = 0;
-    va_end(ap);
-    write(log_fd, buf, strlen(buf));
-}
-
 /*
  * android_name_to_id - returns the integer uid/gid associated with the given
  * name, or -1U on error.
@@ -192,26 +155,6 @@
     return 0;
 }
 
-void list_init(struct listnode *node)
-{
-    node->next = node;
-    node->prev = node;
-}
-
-void list_add_tail(struct listnode *head, struct listnode *item)
-{
-    item->next = head;
-    item->prev = head->prev;
-    head->prev->next = item;
-    head->prev = item;
-}
-
-void list_remove(struct listnode *item)
-{
-    item->next->prev = item->prev;
-    item->prev->next = item->next;
-}
-
 #define MAX_MTD_PARTITIONS 16
 
 static struct {
diff --git a/libcutils/Android.mk b/libcutils/Android.mk
index 0c4e235..effaae0 100644
--- a/libcutils/Android.mk
+++ b/libcutils/Android.mk
@@ -39,6 +39,7 @@
 	config_utils.c \
 	cpu_info.c \
 	load_file.c \
+	list.c \
 	open_memstream.c \
 	strdup16to8.c \
 	strdup8to16.c \
@@ -97,7 +98,7 @@
 # ========================================================
 include $(CLEAR_VARS)
 LOCAL_MODULE := libcutils
-LOCAL_SRC_FILES := $(commonSources) ashmem-dev.c mq.c android_reboot.c partition_utils.c uevent.c qtaguid.c
+LOCAL_SRC_FILES := $(commonSources) ashmem-dev.c mq.c android_reboot.c partition_utils.c uevent.c qtaguid.c klog.c
 
 ifeq ($(TARGET_ARCH),arm)
 LOCAL_SRC_FILES += arch-arm/memset32.S
diff --git a/libcutils/klog.c b/libcutils/klog.c
new file mode 100644
index 0000000..b586a57
--- /dev/null
+++ b/libcutils/klog.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <cutils/klog.h>
+
+static int klog_fd = -1;
+static int klog_level = KLOG_DEFAULT_LEVEL;
+
+void klog_set_level(int level) {
+    klog_level = level;
+}
+
+void klog_init(void)
+{
+    static const char *name = "/dev/__kmsg__";
+    if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
+        klog_fd = open(name, O_WRONLY);
+        fcntl(klog_fd, F_SETFD, FD_CLOEXEC);
+        unlink(name);
+    }
+}
+
+#define LOG_BUF_MAX 512
+
+void klog_write(int level, const char *fmt, ...)
+{
+    char buf[LOG_BUF_MAX];
+    va_list ap;
+
+    if (level > klog_level) return;
+    if (klog_fd < 0) return;
+
+    va_start(ap, fmt);
+    vsnprintf(buf, LOG_BUF_MAX, fmt, ap);
+    buf[LOG_BUF_MAX - 1] = 0;
+    va_end(ap);
+    write(klog_fd, buf, strlen(buf));
+}
diff --git a/libcutils/list.c b/libcutils/list.c
new file mode 100644
index 0000000..e13452d
--- /dev/null
+++ b/libcutils/list.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <cutils/list.h>
+
+void list_init(struct listnode *node)
+{
+    node->next = node;
+    node->prev = node;
+}
+
+void list_add_tail(struct listnode *head, struct listnode *item)
+{
+    item->next = head;
+    item->prev = head->prev;
+    head->prev->next = item;
+    head->prev = item;
+}
+
+void list_remove(struct listnode *item)
+{
+    item->next->prev = item->prev;
+    item->prev->next = item->next;
+}
diff --git a/libcutils/uevent.c b/libcutils/uevent.c
index 320f8d1..4add29c 100644
--- a/libcutils/uevent.c
+++ b/libcutils/uevent.c
@@ -17,7 +17,12 @@
 #include <cutils/uevent.h>
 
 #include <errno.h>
+#include <stdbool.h>
+#include <string.h>
 #include <strings.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
 
 #include <linux/netlink.h>
 
@@ -68,3 +73,29 @@
     errno = EIO;
     return -1;
 }
+
+int uevent_open_socket(int buf_sz, bool passcred)
+{
+    struct sockaddr_nl addr;
+    int on = passcred;
+    int s;
+
+    memset(&addr, 0, sizeof(addr));
+    addr.nl_family = AF_NETLINK;
+    addr.nl_pid = getpid();
+    addr.nl_groups = 0xffffffff;
+
+    s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
+    if(s < 0)
+        return -1;
+
+    setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &buf_sz, sizeof(buf_sz));
+    setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
+
+    if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+        close(s);
+        return -1;
+    }
+
+    return s;
+}
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 8d6bf97..a5a0bc0 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -263,6 +263,9 @@
 on nonencrypted
     class_start late_start
 
+on charger
+    class_start charger
+
 on property:vold.decrypt=trigger_reset_main
     class_reset main