Do Not Merge: Fix out of bound read in libziparchive am: 6fdfd58fe1  -s ours am: 2b4a6de426 am: 556afe82f2  -s ours am: de0f0b8ecc am: 89054eb706 am: ebcba388f5 am: 1bc2862ac5  -s ours am: c800d82044 am: 497461adc5
am: 02e7225f78

Change-Id: I687fc898264c93bedb34076bfa6d981d5059f381
diff --git a/fs_mgr/fs_mgr_fstab.c b/fs_mgr/fs_mgr_fstab.c
index 21b4c74..9225d34 100644
--- a/fs_mgr/fs_mgr_fstab.c
+++ b/fs_mgr/fs_mgr_fstab.c
@@ -72,6 +72,7 @@
     { "recoveryonly",MF_RECOVERYONLY },
     { "swapprio=",   MF_SWAPPRIO },
     { "zramsize=",   MF_ZRAMSIZE },
+    { "verifyatboot", MF_VERIFYATBOOT },
     { "verify",      MF_VERIFY },
     { "noemulatedsd", MF_NOEMULATEDSD },
     { "notrim",       MF_NOTRIM },
diff --git a/fs_mgr/fs_mgr_priv.h b/fs_mgr/fs_mgr_priv.h
index 6d9492b..120ec5a 100644
--- a/fs_mgr/fs_mgr_priv.h
+++ b/fs_mgr/fs_mgr_priv.h
@@ -85,6 +85,7 @@
 #define MF_FORCEFDEORFBE 0x10000
 #define MF_LATEMOUNT    0x20000
 #define MF_NOFAIL       0x40000
+#define MF_VERIFYATBOOT 0x80000
 
 #define DM_BUF_SIZE 4096
 
diff --git a/fs_mgr/fs_mgr_verity.cpp b/fs_mgr/fs_mgr_verity.cpp
index 83f42a09..aa00520 100644
--- a/fs_mgr/fs_mgr_verity.cpp
+++ b/fs_mgr/fs_mgr_verity.cpp
@@ -31,6 +31,7 @@
 
 #include <android-base/file.h>
 #include <android-base/strings.h>
+#include <android-base/unique_fd.h>
 #include <cutils/properties.h>
 #include <logwrap/logwrap.h>
 
@@ -72,6 +73,8 @@
 #define VERITY_KMSG_RESTART "dm-verity device corrupted"
 #define VERITY_KMSG_BUFSIZE 1024
 
+#define READ_BUF_SIZE 4096
+
 #define __STRINGIFY(x) #x
 #define STRINGIFY(x) __STRINGIFY(x)
 
@@ -213,6 +216,16 @@
     return 0;
 }
 
+static int destroy_verity_device(struct dm_ioctl *io, char *name, int fd)
+{
+    verity_ioctl_init(io, name, 0);
+    if (ioctl(fd, DM_DEV_REMOVE, io)) {
+        ERROR("Error removing device mapping (%s)", strerror(errno));
+        return -1;
+    }
+    return 0;
+}
+
 static int get_verity_device_name(struct dm_ioctl *io, char *name, int fd, char **dev_name)
 {
     verity_ioctl_init(io, name, 0);
@@ -614,6 +627,30 @@
     return rc;
 }
 
+static int read_partition(const char *path, uint64_t size)
+{
+    char buf[READ_BUF_SIZE];
+    ssize_t size_read;
+    android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)));
+
+    if (fd == -1) {
+        ERROR("Failed to open %s: %s\n", path, strerror(errno));
+        return -errno;
+    }
+
+    while (size) {
+        size_read = TEMP_FAILURE_RETRY(read(fd, buf, READ_BUF_SIZE));
+        if (size_read == -1) {
+            ERROR("Error in reading partition %s: %s\n", path,
+                  strerror(errno));
+            return -errno;
+        }
+        size -= size_read;
+    }
+
+    return 0;
+}
+
 static int compare_last_signature(struct fstab_rec *fstab, int *match)
 {
     char tag[METADATA_TAG_MAX_LENGTH + 1];
@@ -796,7 +833,7 @@
     char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
     const char *mount_point;
     char propbuf[PROPERTY_VALUE_MAX];
-    char *status;
+    const char *status;
     int fd = -1;
     int i;
     int mode;
@@ -846,9 +883,13 @@
         verity_ioctl_init(io, mount_point, 0);
 
         if (ioctl(fd, DM_TABLE_STATUS, io)) {
-            ERROR("Failed to query DM_TABLE_STATUS for %s (%s)\n", mount_point,
-                strerror(errno));
-            continue;
+            if (fstab->recs[i].fs_mgr_flags & MF_VERIFYATBOOT) {
+                status = "V";
+            } else {
+                ERROR("Failed to query DM_TABLE_STATUS for %s (%s)\n", mount_point,
+                      strerror(errno));
+                continue;
+            }
         }
 
         status = &buffer[io->data_start + sizeof(struct dm_target_spec)];
@@ -912,6 +953,7 @@
     alignas(dm_ioctl) char buffer[DM_BUF_SIZE];
     struct dm_ioctl *io = (struct dm_ioctl *) buffer;
     char *mount_point = basename(fstab->mount_point);
+    bool verified_at_boot = false;
 
     if (fec_open(&f, fstab->blk_device, O_RDONLY, FEC_VERITY_DISABLE,
             FEC_DEFAULT_ROOTS) < 0) {
@@ -1045,10 +1087,26 @@
     // mark the underlying block device as read-only
     fs_mgr_set_blk_ro(fstab->blk_device);
 
+    // Verify the entire partition in one go
+    // If there is an error, allow it to mount as a normal verity partition.
+    if (fstab->fs_mgr_flags & MF_VERIFYATBOOT) {
+        INFO("Verifying partition %s at boot\n", fstab->blk_device);
+        int err = read_partition(verity_blk_name, verity.data_size);
+        if (!err) {
+            INFO("Verified verity partition %s at boot\n", fstab->blk_device);
+            verified_at_boot = true;
+        }
+    }
+
     // assign the new verity block device as the block device
-    free(fstab->blk_device);
-    fstab->blk_device = verity_blk_name;
-    verity_blk_name = 0;
+    if (!verified_at_boot) {
+        free(fstab->blk_device);
+        fstab->blk_device = verity_blk_name;
+        verity_blk_name = 0;
+    } else if (destroy_verity_device(io, mount_point, fd) < 0) {
+        ERROR("Failed to remove verity device %s\n", mount_point);
+        goto out;
+    }
 
     // make sure we've set everything up properly
     if (test_access(fstab->blk_device) < 0) {
diff --git a/healthd/Android.mk b/healthd/Android.mk
index a4469fc..1390962 100644
--- a/healthd/Android.mk
+++ b/healthd/Android.mk
@@ -19,12 +19,43 @@
 include $(BUILD_STATIC_LIBRARY)
 
 include $(CLEAR_VARS)
+ifeq ($(strip $(BOARD_CHARGER_ENABLE_SUSPEND)),true)
+LOCAL_CFLAGS += -DCHARGER_ENABLE_SUSPEND
+LOCAL_SHARED_LIBRARIES += libsuspend
+endif
+LOCAL_SRC_FILES := \
+    healthd_mode_android.cpp \
+    healthd_mode_charger.cpp \
+    AnimationParser.cpp \
+    BatteryPropertiesRegistrar.cpp \
+
+LOCAL_MODULE := libhealthd_internal
+LOCAL_C_INCLUDES := bootable/recovery
+LOCAL_EXPORT_C_INCLUDE_DIRS := \
+    $(LOCAL_PATH) \
+    $(LOCAL_PATH)/include \
+
+LOCAL_STATIC_LIBRARIES := \
+    libbatterymonitor \
+    libbatteryservice \
+    libbinder \
+    libminui \
+    libpng \
+    libz \
+    libutils \
+    libbase \
+    libcutils \
+    liblog \
+    libm \
+    libc \
+
+include $(BUILD_STATIC_LIBRARY)
+
+
+include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := \
-	healthd.cpp \
-	healthd_mode_android.cpp \
-	healthd_mode_charger.cpp \
-	BatteryPropertiesRegistrar.cpp
+    healthd.cpp \
 
 LOCAL_MODULE := healthd
 LOCAL_MODULE_TAGS := optional
@@ -42,9 +73,29 @@
 LOCAL_CFLAGS += -DCHARGER_ENABLE_SUSPEND
 endif
 
+ifneq ($(BOARD_PERIODIC_CHORES_INTERVAL_FAST),)
+LOCAL_CFLAGS += -DBOARD_PERIODIC_CHORES_INTERVAL_FAST=$(BOARD_PERIODIC_CHORES_INTERVAL_FAST)
+endif
+
+ifneq ($(BOARD_PERIODIC_CHORES_INTERVAL_SLOW),)
+LOCAL_CFLAGS += -DBOARD_PERIODIC_CHORES_INTERVAL_SLOW=$(BOARD_PERIODIC_CHORES_INTERVAL_SLOW)
+endif
 LOCAL_C_INCLUDES := bootable/recovery
 
-LOCAL_STATIC_LIBRARIES := libbatterymonitor libbatteryservice libbinder libminui libpng libz libutils libcutils liblog libm libc
+LOCAL_STATIC_LIBRARIES := \
+    libhealthd_internal \
+    libbatterymonitor \
+    libbatteryservice \
+    libbinder \
+    libminui \
+    libpng \
+    libz \
+    libutils \
+    libbase \
+    libcutils \
+    liblog \
+    libm \
+    libc
 
 ifeq ($(strip $(BOARD_CHARGER_ENABLE_SUSPEND)),true)
 LOCAL_STATIC_LIBRARIES += libsuspend
@@ -61,7 +112,7 @@
 
 define _add-charger-image
 include $$(CLEAR_VARS)
-LOCAL_MODULE := system_core_charger_$(notdir $(1))
+LOCAL_MODULE := system_core_charger_res_images_$(notdir $(1))
 LOCAL_MODULE_STEM := $(notdir $(1))
 _img_modules += $$(LOCAL_MODULE)
 LOCAL_SRC_FILES := $1
diff --git a/healthd/AnimationParser.cpp b/healthd/AnimationParser.cpp
new file mode 100644
index 0000000..864038b
--- /dev/null
+++ b/healthd/AnimationParser.cpp
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2016 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 "AnimationParser.h"
+
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+
+#include <cutils/klog.h>
+
+#include "animation.h"
+
+#define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
+#define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0)
+#define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
+
+namespace android {
+
+// Lines consisting of only whitespace or whitespace followed by '#' can be ignored.
+bool can_ignore_line(const char* str) {
+    for (int i = 0; str[i] != '\0' && str[i] != '#'; i++) {
+        if (!isspace(str[i])) return false;
+    }
+    return true;
+}
+
+bool remove_prefix(const std::string& line, const char* prefix, const char** rest) {
+    const char* str = line.c_str();
+    int start;
+    char c;
+
+    std::string format = base::StringPrintf(" %s%%n%%c", prefix);
+    if (sscanf(str, format.c_str(), &start, &c) != 1) {
+        return false;
+    }
+
+    *rest = &str[start];
+    return true;
+}
+
+bool parse_text_field(const char* in, animation::text_field* field) {
+    int* x = &field->pos_x;
+    int* y = &field->pos_y;
+    int* r = &field->color_r;
+    int* g = &field->color_g;
+    int* b = &field->color_b;
+    int* a = &field->color_a;
+
+    int start = 0, end = 0;
+
+    if (sscanf(in, "c c %d %d %d %d %n%*s%n", r, g, b, a, &start, &end) == 4) {
+        *x = CENTER_VAL;
+        *y = CENTER_VAL;
+    } else if (sscanf(in, "c %d %d %d %d %d %n%*s%n", y, r, g, b, a, &start, &end) == 5) {
+        *x = CENTER_VAL;
+    } else if (sscanf(in, "%d c %d %d %d %d %n%*s%n", x, r, g, b, a, &start, &end) == 5) {
+        *y = CENTER_VAL;
+    } else if (sscanf(in, "%d %d %d %d %d %d %n%*s%n", x, y, r, g, b, a, &start, &end) != 6) {
+        return false;
+    }
+
+    if (end == 0) return false;
+
+    field->font_file.assign(&in[start], end - start);
+
+    return true;
+}
+
+bool parse_animation_desc(const std::string& content, animation* anim) {
+    static constexpr const char* animation_prefix = "animation: ";
+    static constexpr const char* fail_prefix = "fail: ";
+    static constexpr const char* clock_prefix = "clock_display: ";
+    static constexpr const char* percent_prefix = "percent_display: ";
+    static constexpr const char* frame_prefix = "frame: ";
+
+    std::vector<animation::frame> frames;
+
+    for (const auto& line : base::Split(content, "\n")) {
+        animation::frame frame;
+        const char* rest;
+
+        if (can_ignore_line(line.c_str())) {
+            continue;
+        } else if (remove_prefix(line, animation_prefix, &rest)) {
+            int start = 0, end = 0;
+            if (sscanf(rest, "%d %d %n%*s%n", &anim->num_cycles, &anim->first_frame_repeats,
+                    &start, &end) != 2 ||
+                end == 0) {
+                LOGE("Bad animation format: %s\n", line.c_str());
+                return false;
+            } else {
+                anim->animation_file.assign(&rest[start], end - start);
+            }
+        } else if (remove_prefix(line, fail_prefix, &rest)) {
+            anim->fail_file.assign(rest);
+        } else if (remove_prefix(line, clock_prefix, &rest)) {
+            if (!parse_text_field(rest, &anim->text_clock)) {
+                LOGE("Bad clock_display format: %s\n", line.c_str());
+                return false;
+            }
+        } else if (remove_prefix(line, percent_prefix, &rest)) {
+            if (!parse_text_field(rest, &anim->text_percent)) {
+                LOGE("Bad percent_display format: %s\n", line.c_str());
+                return false;
+            }
+        } else if (sscanf(line.c_str(), " frame: %d %d %d",
+                &frame.disp_time, &frame.min_level, &frame.max_level) == 3) {
+            frames.push_back(std::move(frame));
+        } else {
+            LOGE("Malformed animation description line: %s\n", line.c_str());
+            return false;
+        }
+    }
+
+    if (anim->animation_file.empty() || frames.empty()) {
+        LOGE("Bad animation description. Provide the 'animation: ' line and at least one 'frame: ' "
+             "line.\n");
+        return false;
+    }
+
+    anim->num_frames = frames.size();
+    anim->frames = new animation::frame[frames.size()];
+    std::copy(frames.begin(), frames.end(), anim->frames);
+
+    return true;
+}
+
+}  // namespace android
diff --git a/healthd/AnimationParser.h b/healthd/AnimationParser.h
new file mode 100644
index 0000000..bc00845
--- /dev/null
+++ b/healthd/AnimationParser.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2016 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 HEALTHD_ANIMATION_PARSER_H
+#define HEALTHD_ANIMATION_PARSER_H
+
+#include "animation.h"
+
+namespace android {
+
+bool parse_animation_desc(const std::string& content, animation* anim);
+
+bool can_ignore_line(const char* str);
+bool remove_prefix(const std::string& str, const char* prefix, const char** rest);
+bool parse_text_field(const char* in, animation::text_field* field);
+}  // namespace android
+
+#endif // HEALTHD_ANIMATION_PARSER_H
diff --git a/healthd/animation.h b/healthd/animation.h
new file mode 100644
index 0000000..562b689
--- /dev/null
+++ b/healthd/animation.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2016 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 HEALTHD_ANIMATION_H
+#define HEALTHD_ANIMATION_H
+
+#include <inttypes.h>
+#include <string>
+
+struct GRSurface;
+struct GRFont;
+
+namespace android {
+
+#define CENTER_VAL INT_MAX
+
+struct animation {
+    struct frame {
+        int disp_time;
+        int min_level;
+        int max_level;
+
+        GRSurface* surface;
+    };
+
+    struct text_field {
+        std::string font_file;
+        int pos_x;
+        int pos_y;
+        int color_r;
+        int color_g;
+        int color_b;
+        int color_a;
+
+        GRFont* font;
+    };
+
+    std::string animation_file;
+    std::string fail_file;
+
+    text_field text_clock;
+    text_field text_percent;
+
+    bool run;
+
+    frame* frames;
+    int cur_frame;
+    int num_frames;
+    int first_frame_repeats;  // Number of times to repeat the first frame in the current cycle
+
+    int cur_cycle;
+    int num_cycles;  // Number of cycles to complete before blanking the screen
+
+    int cur_level;  // current battery level being animated (0-100)
+    int cur_status;  // current battery status - see BatteryService.h for BATTERY_STATUS_*
+};
+
+}
+
+#endif // HEALTHD_ANIMATION_H
diff --git a/healthd/healthd.cpp b/healthd/healthd.cpp
index 2caae78..1c144b3 100644
--- a/healthd/healthd.cpp
+++ b/healthd/healthd.cpp
@@ -35,9 +35,19 @@
 
 using namespace android;
 
-// Periodic chores intervals in seconds
-#define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (60 * 1)
-#define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (60 * 10)
+#ifndef BOARD_PERIODIC_CHORES_INTERVAL_FAST
+  // Periodic chores fast interval in seconds
+  #define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (60 * 1)
+#else
+  #define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (BOARD_PERIODIC_CHORES_INTERVAL_FAST)
+#endif
+
+#ifndef BOARD_PERIODIC_CHORES_INTERVAL_SLOW
+  // Periodic chores fast interval in seconds
+  #define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (60 * 10)
+#else
+  #define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (BOARD_PERIODIC_CHORES_INTERVAL_SLOW)
+#endif
 
 static struct healthd_config healthd_config = {
     .periodic_chores_interval_fast = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST,
@@ -280,12 +290,18 @@
 }
 
 static void healthd_mainloop(void) {
+    int nevents = 0;
     while (1) {
         struct epoll_event events[eventct];
-        int nevents;
         int timeout = awake_poll_interval;
         int mode_timeout;
 
+        /* Don't wait for first timer timeout to run periodic chores */
+        if (!nevents)
+            periodic_chores();
+
+        healthd_mode_ops->heartbeat();
+
         mode_timeout = healthd_mode_ops->preparetowait();
         if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout))
             timeout = mode_timeout;
@@ -301,11 +317,6 @@
             if (events[n].data.ptr)
                 (*(void (*)(int))events[n].data.ptr)(events[n].events);
         }
-
-        if (!nevents)
-            periodic_chores();
-
-        healthd_mode_ops->heartbeat();
     }
 
     return;
diff --git a/healthd/healthd_mode_charger.cpp b/healthd/healthd_mode_charger.cpp
index a6da704..ccf948b 100644
--- a/healthd/healthd_mode_charger.cpp
+++ b/healthd/healthd_mode_charger.cpp
@@ -30,6 +30,9 @@
 #include <time.h>
 #include <unistd.h>
 
+#include <android-base/file.h>
+#include <android-base/stringprintf.h>
+
 #include <sys/socket.h>
 #include <linux/netlink.h>
 
@@ -44,10 +47,14 @@
 #include <suspend/autosuspend.h>
 #endif
 
+#include "animation.h"
+#include "AnimationParser.h"
 #include "minui/minui.h"
 
 #include <healthd/healthd.h>
 
+using namespace android;
+
 char *locale;
 
 #ifndef max
@@ -67,8 +74,6 @@
 #define POWER_ON_KEY_TIME       (2 * MSEC_PER_SEC)
 #define UNPLUGGED_SHUTDOWN_TIME (10 * MSEC_PER_SEC)
 
-#define BATTERY_FULL_THRESH     95
-
 #define LAST_KMSG_PATH          "/proc/last_kmsg"
 #define LAST_KMSG_PSTORE_PATH   "/sys/fs/pstore/console-ramoops"
 #define LAST_KMSG_MAX_SZ        (32 * 1024)
@@ -77,34 +82,14 @@
 #define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0)
 #define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
 
+static constexpr const char* animation_desc_path = "/res/values/charger/animation.txt";
+
 struct key_state {
     bool pending;
     bool down;
     int64_t timestamp;
 };
 
-struct frame {
-    int disp_time;
-    int min_capacity;
-    bool level_only;
-
-    GRSurface* surface;
-};
-
-struct animation {
-    bool run;
-
-    struct frame *frames;
-    int cur_frame;
-    int num_frames;
-
-    int cur_cycle;
-    int num_cycles;
-
-    /* current capacity being animated */
-    int capacity;
-};
-
 struct charger {
     bool have_battery_state;
     bool charger_connected;
@@ -119,54 +104,83 @@
     int boot_min_cap;
 };
 
-static struct frame batt_anim_frames[] = {
+static const struct animation BASE_ANIMATION = {
+    .text_clock = {
+        .pos_x = 0,
+        .pos_y = 0,
+
+        .color_r = 255,
+        .color_g = 255,
+        .color_b = 255,
+        .color_a = 255,
+
+        .font = nullptr,
+    },
+    .text_percent = {
+        .pos_x = 0,
+        .pos_y = 0,
+
+        .color_r = 255,
+        .color_g = 255,
+        .color_b = 255,
+        .color_a = 255,
+    },
+
+    .run = false,
+
+    .frames = nullptr,
+    .cur_frame = 0,
+    .num_frames = 0,
+    .first_frame_repeats = 2,
+
+    .cur_cycle = 0,
+    .num_cycles = 3,
+
+    .cur_level = 0,
+    .cur_status = BATTERY_STATUS_UNKNOWN,
+};
+
+
+static struct animation::frame default_animation_frames[] = {
     {
         .disp_time = 750,
-        .min_capacity = 0,
-        .level_only = false,
+        .min_level = 0,
+        .max_level = 19,
         .surface = NULL,
     },
     {
         .disp_time = 750,
-        .min_capacity = 20,
-        .level_only = false,
+        .min_level = 0,
+        .max_level = 39,
         .surface = NULL,
     },
     {
         .disp_time = 750,
-        .min_capacity = 40,
-        .level_only = false,
+        .min_level = 0,
+        .max_level = 59,
         .surface = NULL,
     },
     {
         .disp_time = 750,
-        .min_capacity = 60,
-        .level_only = false,
+        .min_level = 0,
+        .max_level = 79,
         .surface = NULL,
     },
     {
         .disp_time = 750,
-        .min_capacity = 80,
-        .level_only = true,
+        .min_level = 80,
+        .max_level = 95,
         .surface = NULL,
     },
     {
         .disp_time = 750,
-        .min_capacity = BATTERY_FULL_THRESH,
-        .level_only = false,
+        .min_level = 0,
+        .max_level = 100,
         .surface = NULL,
     },
 };
 
-static struct animation battery_animation = {
-    .run = false,
-    .frames = batt_anim_frames,
-    .cur_frame = 0,
-    .num_frames = ARRAY_SIZE(batt_anim_frames),
-    .cur_cycle = 0,
-    .num_cycles = 3,
-    .capacity = 0,
-};
+static struct animation battery_animation = BASE_ANIMATION;
 
 static struct charger charger_state;
 static struct healthd_config *healthd_config;
@@ -257,13 +271,13 @@
 
 static int draw_text(const char *str, int x, int y)
 {
-    int str_len_px = gr_measure(str);
+    int str_len_px = gr_measure(gr_sys_font(), 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, 0);
+    gr_text(gr_sys_font(), x, y, str, 0);
 
     return y + char_height;
 }
@@ -273,8 +287,84 @@
     gr_color(0xa4, 0xc6, 0x39, 255);
 }
 
+// Negative x or y coordinates position the text away from the opposite edge that positive ones do.
+void determine_xy(const animation::text_field& field, const int length, int* x, int* y)
+{
+    *x = field.pos_x;
+    *y = field.pos_y;
+
+    int str_len_px = length * field.font->char_width;
+    if (field.pos_x == CENTER_VAL) {
+        *x = (gr_fb_width() - str_len_px) / 2;
+    } else if (field.pos_x >= 0) {
+        *x = field.pos_x;
+    } else {  // position from max edge
+        *x = gr_fb_width() + field.pos_x - str_len_px;
+    }
+
+    if (field.pos_y == CENTER_VAL) {
+        *y = (gr_fb_height() - field.font->char_height) / 2;
+    } else if (field.pos_y >= 0) {
+        *y = field.pos_y;
+    } else {  // position from max edge
+        *y = gr_fb_height() + field.pos_y - field.font->char_height;
+    }
+}
+
+static void draw_clock(const animation& anim)
+{
+    static constexpr char CLOCK_FORMAT[] = "%H:%M";
+    static constexpr int CLOCK_LENGTH = 6;
+
+    const animation::text_field& field = anim.text_clock;
+
+    if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) return;
+
+    time_t rawtime;
+    time(&rawtime);
+    struct tm* time_info = localtime(&rawtime);
+
+    char clock_str[CLOCK_LENGTH];
+    size_t length = strftime(clock_str, CLOCK_LENGTH, CLOCK_FORMAT, time_info);
+    if (length != CLOCK_LENGTH - 1) {
+        LOGE("Could not format time\n");
+        return;
+    }
+
+    int x, y;
+    determine_xy(field, length, &x, &y);
+
+    LOGV("drawing clock %s %d %d\n", clock_str, x, y);
+    gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
+    gr_text(field.font, x, y, clock_str, false);
+}
+
+static void draw_percent(const animation& anim)
+{
+    int cur_level = anim.cur_level;
+    if (anim.cur_status == BATTERY_STATUS_FULL) {
+        cur_level = 100;
+    }
+
+    if (cur_level <= 0) return;
+
+    const animation::text_field& field = anim.text_percent;
+    if (field.font == nullptr || field.font->char_width == 0 || field.font->char_height == 0) {
+        return;
+    }
+
+    std::string str = base::StringPrintf("%d%%", cur_level);
+
+    int x, y;
+    determine_xy(field, str.size(), &x, &y);
+
+    LOGV("drawing percent %s %d %d\n", str.c_str(), x, y);
+    gr_color(field.color_r, field.color_g, field.color_b, field.color_a);
+    gr_text(field.font, x, y, str.c_str(), false);
+}
+
 /* returns the last y-offset of where the surface ends */
-static int draw_surface_centered(struct charger* /*charger*/, GRSurface* surface)
+static int draw_surface_centered(GRSurface* surface)
 {
     int w;
     int h;
@@ -295,7 +385,7 @@
 {
     int y;
     if (charger->surf_unknown) {
-        draw_surface_centered(charger, charger->surf_unknown);
+        draw_surface_centered(charger->surf_unknown);
     } else {
         android_green();
         y = draw_text("Charging!", -1, -1);
@@ -303,17 +393,19 @@
     }
 }
 
-static void draw_battery(struct charger *charger)
+static void draw_battery(const struct charger* charger)
 {
-    struct animation *batt_anim = charger->batt_anim;
-    struct frame *frame = &batt_anim->frames[batt_anim->cur_frame];
+    const struct animation& anim = *charger->batt_anim;
+    const struct animation::frame& frame = anim.frames[anim.cur_frame];
 
-    if (batt_anim->num_frames != 0) {
-        draw_surface_centered(charger, frame->surface);
+    if (anim.num_frames != 0) {
+        draw_surface_centered(frame.surface);
         LOGV("drawing frame #%d min_cap=%d time=%d\n",
-             batt_anim->cur_frame, frame->min_capacity,
-             frame->disp_time);
+             anim.cur_frame, frame.min_level,
+             frame.disp_time);
     }
+    draw_clock(anim);
+    draw_percent(anim);
 }
 
 static void redraw_screen(struct charger *charger)
@@ -323,7 +415,7 @@
     clear_screen();
 
     /* try to display *something* */
-    if (batt_anim->capacity < 0 || batt_anim->num_frames == 0)
+    if (batt_anim->cur_level < 0 || batt_anim->num_frames == 0)
         draw_unknown(charger);
     else
         draw_battery(charger);
@@ -342,16 +434,33 @@
     anim->run = false;
 }
 
+static void init_status_display(struct animation* anim)
+{
+    int res;
+
+    if (!anim->text_clock.font_file.empty()) {
+        if ((res =
+                gr_init_font(anim->text_clock.font_file.c_str(), &anim->text_clock.font)) < 0) {
+            LOGE("Could not load time font (%d)\n", res);
+        }
+    }
+
+    if (!anim->text_percent.font_file.empty()) {
+        if ((res =
+                gr_init_font(anim->text_percent.font_file.c_str(), &anim->text_percent.font)) < 0) {
+            LOGE("Could not load percent font (%d)\n", res);
+        }
+    }
+}
+
 static void update_screen_state(struct charger *charger, int64_t now)
 {
     struct animation *batt_anim = charger->batt_anim;
     int disp_time;
 
-    if (!batt_anim->run || now < charger->next_screen_transition)
-        return;
+    if (!batt_anim->run || now < charger->next_screen_transition) return;
 
     if (!minui_inited) {
-
         if (healthd_config && healthd_config->screen_on) {
             if (!healthd_config->screen_on(batt_prop)) {
                 LOGV("[%" PRId64 "] leave screen off\n", now);
@@ -364,7 +473,8 @@
         }
 
         gr_init();
-        gr_font_size(&char_width, &char_height);
+        gr_font_size(gr_sys_font(), &char_width, &char_height);
+        init_status_display(batt_anim);
 
 #ifndef CHARGER_DISABLE_INIT_BLANK
         gr_fb_blank(true);
@@ -373,7 +483,7 @@
     }
 
     /* animation is over, blank screen and leave */
-    if (batt_anim->cur_cycle == batt_anim->num_cycles) {
+    if (batt_anim->num_cycles > 0 && batt_anim->cur_cycle == batt_anim->num_cycles) {
         reset_animation(batt_anim);
         charger->next_screen_transition = -1;
         gr_fb_blank(true);
@@ -389,21 +499,24 @@
     if (batt_anim->cur_frame == 0) {
 
         LOGV("[%" PRId64 "] animation starting\n", now);
-        if (batt_prop && batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) {
-            int i;
+        if (batt_prop) {
+            batt_anim->cur_level = batt_prop->batteryLevel;
+            batt_anim->cur_status = batt_prop->batteryStatus;
+            if (batt_prop->batteryLevel >= 0 && batt_anim->num_frames != 0) {
+                /* find first frame given current battery level */
+                for (int i = 0; i < batt_anim->num_frames; i++) {
+                    if (batt_anim->cur_level >= batt_anim->frames[i].min_level &&
+                        batt_anim->cur_level <= batt_anim->frames[i].max_level) {
+                        batt_anim->cur_frame = i;
+                        break;
+                    }
+                }
 
-            /* find first frame given current capacity */
-            for (i = 1; i < batt_anim->num_frames; i++) {
-                if (batt_prop->batteryLevel < batt_anim->frames[i].min_capacity)
-                    break;
+                // repeat the first frame first_frame_repeats times
+                disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time *
+                    batt_anim->first_frame_repeats;
             }
-            batt_anim->cur_frame = i - 1;
-
-            /* show the first frame for twice as long */
-            disp_time = batt_anim->frames[batt_anim->cur_frame].disp_time * 2;
         }
-        if (batt_prop)
-            batt_anim->capacity = batt_prop->batteryLevel;
     }
 
     /* unblank the screen  on first cycle */
@@ -416,8 +529,8 @@
     /* if we don't have anim frames, we only have one image, so just bump
      * the cycle counter and exit
      */
-    if (batt_anim->num_frames == 0 || batt_anim->capacity < 0) {
-        LOGV("[%" PRId64 "] animation missing or unknown battery status\n", now);
+    if (batt_anim->num_frames == 0 || batt_anim->cur_level < 0) {
+        LOGW("[%" PRId64 "] animation missing or unknown battery status\n", now);
         charger->next_screen_transition = now + BATTERY_UNKNOWN_TIME;
         batt_anim->cur_cycle++;
         return;
@@ -432,12 +545,11 @@
     if (charger->charger_connected) {
         batt_anim->cur_frame++;
 
-        /* if the frame is used for level-only, that is only show it when it's
-         * the current level, skip it during the animation.
-         */
         while (batt_anim->cur_frame < batt_anim->num_frames &&
-               batt_anim->frames[batt_anim->cur_frame].level_only)
+               (batt_anim->cur_level < batt_anim->frames[batt_anim->cur_frame].min_level ||
+                batt_anim->cur_level > batt_anim->frames[batt_anim->cur_frame].max_level)) {
             batt_anim->cur_frame++;
+        }
         if (batt_anim->cur_frame >= batt_anim->num_frames) {
             batt_anim->cur_cycle++;
             batt_anim->cur_frame = 0;
@@ -521,7 +633,7 @@
                     LOGW("[%" PRId64 "] booting from charger mode\n", now);
                     property_set("sys.boot_from_charger_mode", "1");
                 } else {
-                    if (charger->batt_anim->capacity >= charger->boot_min_cap) {
+                    if (charger->batt_anim->cur_level >= charger->boot_min_cap) {
                         LOGW("[%" PRId64 "] rebooting\n", now);
                         android_reboot(ANDROID_RB_RESTART, 0, 0);
                     } else {
@@ -672,6 +784,52 @@
         ev_dispatch();
 }
 
+animation* init_animation()
+{
+    bool parse_success;
+
+    std::string content;
+    if (base::ReadFileToString(animation_desc_path, &content)) {
+        parse_success = parse_animation_desc(content, &battery_animation);
+    } else {
+        LOGW("Could not open animation description at %s\n", animation_desc_path);
+        parse_success = false;
+    }
+
+    if (!parse_success) {
+        LOGW("Could not parse animation description. Using default animation.\n");
+        battery_animation = BASE_ANIMATION;
+        battery_animation.animation_file.assign("charger/battery_scale");
+        battery_animation.frames = default_animation_frames;
+        battery_animation.num_frames = ARRAY_SIZE(default_animation_frames);
+    }
+    if (battery_animation.fail_file.empty()) {
+        battery_animation.fail_file.assign("charger/battery_fail");
+    }
+
+    LOGV("Animation Description:\n");
+    LOGV("  animation: %d %d '%s' (%d)\n",
+        battery_animation.num_cycles, battery_animation.first_frame_repeats,
+        battery_animation.animation_file.c_str(), battery_animation.num_frames);
+    LOGV("  fail_file: '%s'\n", battery_animation.fail_file.c_str());
+    LOGV("  clock: %d %d %d %d %d %d '%s'\n",
+        battery_animation.text_clock.pos_x, battery_animation.text_clock.pos_y,
+        battery_animation.text_clock.color_r, battery_animation.text_clock.color_g,
+        battery_animation.text_clock.color_b, battery_animation.text_clock.color_a,
+        battery_animation.text_clock.font_file.c_str());
+    LOGV("  percent: %d %d %d %d %d %d '%s'\n",
+        battery_animation.text_percent.pos_x, battery_animation.text_percent.pos_y,
+        battery_animation.text_percent.color_r, battery_animation.text_percent.color_g,
+        battery_animation.text_percent.color_b, battery_animation.text_percent.color_a,
+        battery_animation.text_percent.font_file.c_str());
+    for (int i = 0; i < battery_animation.num_frames; i++) {
+        LOGV("  frame %.2d: %d %d %d\n", i, battery_animation.frames[i].disp_time,
+            battery_animation.frames[i].min_level, battery_animation.frames[i].max_level);
+    }
+
+    return &battery_animation;
+}
+
 void healthd_mode_charger_init(struct healthd_config* config)
 {
     int ret;
@@ -689,35 +847,39 @@
         healthd_register_event(epollfd, charger_event_handler, EVENT_WAKEUP_FD);
     }
 
-    ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
-    if (ret < 0) {
-        LOGE("Cannot load battery_fail image\n");
-        charger->surf_unknown = NULL;
-    }
+    struct animation* anim = init_animation();
+    charger->batt_anim = anim;
 
-    charger->batt_anim = &battery_animation;
+    ret = res_create_display_surface(anim->fail_file.c_str(), &charger->surf_unknown);
+    if (ret < 0) {
+        LOGE("Cannot load custom battery_fail image. Reverting to built in.\n");
+        ret = res_create_display_surface("charger/battery_fail", &charger->surf_unknown);
+        if (ret < 0) {
+            LOGE("Cannot load built in battery_fail image\n");
+            charger->surf_unknown = NULL;
+        }
+    }
 
     GRSurface** scale_frames;
     int scale_count;
     int scale_fps;  // Not in use (charger/battery_scale doesn't have FPS text
                     // chunk). We are using hard-coded frame.disp_time instead.
-    ret = res_create_multi_display_surface("charger/battery_scale", &scale_count, &scale_fps,
-                                           &scale_frames);
+    ret = res_create_multi_display_surface(anim->animation_file.c_str(),
+        &scale_count, &scale_fps, &scale_frames);
     if (ret < 0) {
         LOGE("Cannot load battery_scale image\n");
-        charger->batt_anim->num_frames = 0;
-        charger->batt_anim->num_cycles = 1;
-    } else if (scale_count != charger->batt_anim->num_frames) {
+        anim->num_frames = 0;
+        anim->num_cycles = 1;
+    } else if (scale_count != anim->num_frames) {
         LOGE("battery_scale image has unexpected frame count (%d, expected %d)\n",
-             scale_count, charger->batt_anim->num_frames);
-        charger->batt_anim->num_frames = 0;
-        charger->batt_anim->num_cycles = 1;
+             scale_count, anim->num_frames);
+        anim->num_frames = 0;
+        anim->num_cycles = 1;
     } else {
-        for (i = 0; i < charger->batt_anim->num_frames; i++) {
-            charger->batt_anim->frames[i].surface = scale_frames[i];
+        for (i = 0; i < anim->num_frames; i++) {
+            anim->frames[i].surface = scale_frames[i];
         }
     }
-
     ev_sync_key_state(set_key_callback, charger);
 
     charger->next_screen_transition = -1;
diff --git a/healthd/tests/Android.mk b/healthd/tests/Android.mk
new file mode 100644
index 0000000..87e8862
--- /dev/null
+++ b/healthd/tests/Android.mk
@@ -0,0 +1,21 @@
+# Copyright 2016 The Android Open Source Project
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
+    AnimationParser_test.cpp \
+
+LOCAL_MODULE := healthd_test
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_STATIC_LIBRARIES := \
+	libhealthd_internal \
+
+LOCAL_SHARED_LIBRARIES := \
+	liblog \
+	libbase \
+	libcutils \
+
+include $(BUILD_NATIVE_TEST)
diff --git a/healthd/tests/AnimationParser_test.cpp b/healthd/tests/AnimationParser_test.cpp
new file mode 100644
index 0000000..2fc3185
--- /dev/null
+++ b/healthd/tests/AnimationParser_test.cpp
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2016 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 "AnimationParser.h"
+
+#include <gtest/gtest.h>
+
+using namespace android;
+
+TEST(AnimationParserTest, Test_can_ignore_line) {
+    EXPECT_TRUE(can_ignore_line(""));
+    EXPECT_TRUE(can_ignore_line("     "));
+    EXPECT_TRUE(can_ignore_line("#"));
+    EXPECT_TRUE(can_ignore_line("   # comment"));
+
+    EXPECT_FALSE(can_ignore_line("text"));
+    EXPECT_FALSE(can_ignore_line("text # comment"));
+    EXPECT_FALSE(can_ignore_line("     text"));
+    EXPECT_FALSE(can_ignore_line("     text # comment"));
+}
+
+TEST(AnimationParserTest, Test_remove_prefix) {
+    static const char TEST_STRING[] = "abcdef";
+    const char* rest = nullptr;
+    EXPECT_FALSE(remove_prefix(TEST_STRING, "def", &rest));
+    // Ignore strings that only consist of the prefix
+    EXPECT_FALSE(remove_prefix(TEST_STRING, TEST_STRING, &rest));
+
+    EXPECT_TRUE(remove_prefix(TEST_STRING, "abc", &rest));
+    EXPECT_STREQ("def", rest);
+
+    EXPECT_TRUE(remove_prefix("  abcdef", "abc", &rest));
+    EXPECT_STREQ("def", rest);
+}
+
+TEST(AnimationParserTest, Test_parse_text_field) {
+    static const char TEST_FILE_NAME[] = "font_file";
+    static const int TEST_X = 3;
+    static const int TEST_Y = 6;
+    static const int TEST_R = 1;
+    static const int TEST_G = 2;
+    static const int TEST_B = 4;
+    static const int TEST_A = 8;
+
+    static const char TEST_XCENT_YCENT[] = "c c 1 2 4 8  font_file ";
+    static const char TEST_XCENT_YVAL[]  = "c 6 1 2 4 8  font_file ";
+    static const char TEST_XVAL_YCENT[]  = "3 c 1 2 4 8  font_file ";
+    static const char TEST_XVAL_YVAL[]   = "3 6 1 2 4 8  font_file ";
+    static const char TEST_BAD_MISSING[] = "c c 1 2 4 font_file";
+    static const char TEST_BAD_NO_FILE[] = "c c 1 2 4 8";
+
+    animation::text_field out;
+
+    EXPECT_TRUE(parse_text_field(TEST_XCENT_YCENT, &out));
+    EXPECT_EQ(CENTER_VAL, out.pos_x);
+    EXPECT_EQ(CENTER_VAL, out.pos_y);
+    EXPECT_EQ(TEST_R, out.color_r);
+    EXPECT_EQ(TEST_G, out.color_g);
+    EXPECT_EQ(TEST_B, out.color_b);
+    EXPECT_EQ(TEST_A, out.color_a);
+    EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str());
+
+    EXPECT_TRUE(parse_text_field(TEST_XCENT_YVAL, &out));
+    EXPECT_EQ(CENTER_VAL, out.pos_x);
+    EXPECT_EQ(TEST_Y, out.pos_y);
+    EXPECT_EQ(TEST_R, out.color_r);
+    EXPECT_EQ(TEST_G, out.color_g);
+    EXPECT_EQ(TEST_B, out.color_b);
+    EXPECT_EQ(TEST_A, out.color_a);
+    EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str());
+
+    EXPECT_TRUE(parse_text_field(TEST_XVAL_YCENT, &out));
+    EXPECT_EQ(TEST_X, out.pos_x);
+    EXPECT_EQ(CENTER_VAL, out.pos_y);
+    EXPECT_EQ(TEST_R, out.color_r);
+    EXPECT_EQ(TEST_G, out.color_g);
+    EXPECT_EQ(TEST_B, out.color_b);
+    EXPECT_EQ(TEST_A, out.color_a);
+    EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str());
+
+    EXPECT_TRUE(parse_text_field(TEST_XVAL_YVAL, &out));
+    EXPECT_EQ(TEST_X, out.pos_x);
+    EXPECT_EQ(TEST_Y, out.pos_y);
+    EXPECT_EQ(TEST_R, out.color_r);
+    EXPECT_EQ(TEST_G, out.color_g);
+    EXPECT_EQ(TEST_B, out.color_b);
+    EXPECT_EQ(TEST_A, out.color_a);
+    EXPECT_STREQ(TEST_FILE_NAME, out.font_file.c_str());
+
+    EXPECT_FALSE(parse_text_field(TEST_BAD_MISSING, &out));
+    EXPECT_FALSE(parse_text_field(TEST_BAD_NO_FILE, &out));
+}
+
+TEST(AnimationParserTest, Test_parse_animation_desc_basic) {
+    static const char TEST_ANIMATION[] = R"desc(
+        # Basic animation
+        animation: 5 1 test/animation_file
+        frame: 1000 0 100
+    )desc";
+    animation anim;
+
+    EXPECT_TRUE(parse_animation_desc(TEST_ANIMATION, &anim));
+}
+
+TEST(AnimationParserTest, Test_parse_animation_desc_bad_no_animation_line) {
+    static const char TEST_ANIMATION[] = R"desc(
+        # Bad animation
+        frame: 1000 90  10
+    )desc";
+    animation anim;
+
+    EXPECT_FALSE(parse_animation_desc(TEST_ANIMATION, &anim));
+}
+
+TEST(AnimationParserTest, Test_parse_animation_desc_bad_no_frame) {
+    static const char TEST_ANIMATION[] = R"desc(
+        # Bad animation
+        animation: 5 1 test/animation_file
+    )desc";
+    animation anim;
+
+    EXPECT_FALSE(parse_animation_desc(TEST_ANIMATION, &anim));
+}
+
+TEST(AnimationParserTest, Test_parse_animation_desc_bad_animation_line_format) {
+    static const char TEST_ANIMATION[] = R"desc(
+        # Bad animation
+        animation: 5 1
+        frame: 1000 90  10
+    )desc";
+    animation anim;
+
+    EXPECT_FALSE(parse_animation_desc(TEST_ANIMATION, &anim));
+}
+
+TEST(AnimationParserTest, Test_parse_animation_desc_full) {
+    static const char TEST_ANIMATION[] = R"desc(
+        # Full animation
+        animation: 5 1 test/animation_file
+        clock_display:    11 12 13 14 15 16 test/time_font
+        percent_display:  21 22 23 24 25 26 test/percent_font
+
+        frame: 10 20 30
+        frame: 40 50 60
+    )desc";
+    animation anim;
+
+    EXPECT_TRUE(parse_animation_desc(TEST_ANIMATION, &anim));
+
+    EXPECT_EQ(5, anim.num_cycles);
+    EXPECT_EQ(1, anim.first_frame_repeats);
+    EXPECT_STREQ("test/animation_file", anim.animation_file.c_str());
+
+    EXPECT_EQ(11, anim.text_clock.pos_x);
+    EXPECT_EQ(12, anim.text_clock.pos_y);
+    EXPECT_EQ(13, anim.text_clock.color_r);
+    EXPECT_EQ(14, anim.text_clock.color_g);
+    EXPECT_EQ(15, anim.text_clock.color_b);
+    EXPECT_EQ(16, anim.text_clock.color_a);
+    EXPECT_STREQ("test/time_font", anim.text_clock.font_file.c_str());
+
+    EXPECT_EQ(21, anim.text_percent.pos_x);
+    EXPECT_EQ(22, anim.text_percent.pos_y);
+    EXPECT_EQ(23, anim.text_percent.color_r);
+    EXPECT_EQ(24, anim.text_percent.color_g);
+    EXPECT_EQ(25, anim.text_percent.color_b);
+    EXPECT_EQ(26, anim.text_percent.color_a);
+    EXPECT_STREQ("test/percent_font", anim.text_percent.font_file.c_str());
+
+    EXPECT_EQ(2, anim.num_frames);
+
+    EXPECT_EQ(10, anim.frames[0].disp_time);
+    EXPECT_EQ(20, anim.frames[0].min_level);
+    EXPECT_EQ(30, anim.frames[0].max_level);
+
+    EXPECT_EQ(40, anim.frames[1].disp_time);
+    EXPECT_EQ(50, anim.frames[1].min_level);
+    EXPECT_EQ(60, anim.frames[1].max_level);
+}
diff --git a/init/init.cpp b/init/init.cpp
index 84da2b9..cd2d2e6 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -478,11 +478,17 @@
     if (qemu[0]) import_kernel_cmdline(true, import_kernel_nv);
 }
 
+static int property_enable_triggers_action(const std::vector<std::string>& args)
+{
+    /* Enable property triggers. */
+    property_triggers_enabled = 1;
+    return 0;
+}
+
 static int queue_property_triggers_action(const std::vector<std::string>& args)
 {
+    ActionManager::GetInstance().QueueBuiltinAction(property_enable_triggers_action, "enable_property_trigger");
     ActionManager::GetInstance().QueueAllPropertyTriggers();
-    /* enable property triggers */
-    property_triggers_enabled = 1;
     return 0;
 }