Code drop from //branches/cupcake/...@124589
diff --git a/Android.mk b/Android.mk
index b085568..816d143 100644
--- a/Android.mk
+++ b/Android.mk
@@ -4,6 +4,7 @@
 commands_recovery_local_path := $(LOCAL_PATH)
 
 ifneq ($(TARGET_SIMULATOR),true)
+ifeq ($(TARGET_ARCH),arm)
 
 LOCAL_SRC_FILES := \
 	recovery.c \
@@ -49,9 +50,11 @@
 
 include $(commands_recovery_local_path)/minui/Android.mk
 
+endif   # TARGET_ARCH == arm
 endif	# !TARGET_SIMULATOR
 
 include $(commands_recovery_local_path)/amend/Android.mk
 include $(commands_recovery_local_path)/minzip/Android.mk
 include $(commands_recovery_local_path)/mtdutils/Android.mk
+include $(commands_recovery_local_path)/tools/Android.mk
 commands_recovery_local_path :=
diff --git a/minui/graphics.c b/minui/graphics.c
index 4f3026a..05e8763 100644
--- a/minui/graphics.c
+++ b/minui/graphics.c
@@ -57,24 +57,27 @@
     void *bits;
     
     fd = open("/dev/graphics/fb0", O_RDWR);
-    if(fd < 0) {
+    if (fd < 0) {
         perror("cannot open fb0");
         return -1;
     }
 
-    if(ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
+    if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
         perror("failed to get fb0 info");
+        close(fd);
         return -1;
     }
 
-    if(ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
+    if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) {
         perror("failed to get fb0 info");
+        close(fd);
         return -1;
     }
 
     bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
-    if(bits == MAP_FAILED) {
+    if (bits == MAP_FAILED) {
         perror("failed to mmap framebuffer");
+        close(fd);
         return -1;
     }
 
@@ -99,24 +102,14 @@
 
 static void set_active_framebuffer(unsigned n)
 {
-    if(n > 1) return;
+    if (n > 1) return;
     vi.yres_virtual = vi.yres * 2;
     vi.yoffset = n * vi.yres;
-    if(ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
-        fprintf(stderr,"active fb swap failed!\n");
+    if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) {
+        perror("active fb swap failed");
     }
 }
 
-static void dumpinfo(struct fb_var_screeninfo *vi)
-{
-    fprintf(stderr,"vi.xres = %d\n", vi->xres);
-    fprintf(stderr,"vi.yres = %d\n", vi->yres);
-    fprintf(stderr,"vi.xresv = %d\n", vi->xres_virtual);
-    fprintf(stderr,"vi.yresv = %d\n", vi->yres_virtual);
-    fprintf(stderr,"vi.xoff = %d\n", vi->xoffset);
-    fprintf(stderr,"vi.yoff = %d\n", vi->yoffset);
-}
-
 void gr_flip(void)
 {
     GGLContext *gl = gr_context;
@@ -163,7 +156,7 @@
     
     while((off = *s++)) {
         off -= 32;
-        if(off < 96) {
+        if (off < 96) {
             gl->texCoord2i(gl, (off * font->cwidth) - x, 0 - y);
             gl->recti(gl, x, y, x + font->cwidth, y + font->cheight);
         }
@@ -240,31 +233,29 @@
 
 int gr_init(void)
 {
-    GGLContext *gl;
-    int fd;
-
     gglInit(&gr_context);
-    gl = gr_context;
+    GGLContext *gl = gr_context;
 
     gr_init_font();
-
-    fd = open("/dev/tty0", O_RDWR | O_SYNC);
-    if(fd < 0) return -1;
-
-    if(ioctl(fd, KDSETMODE, (void*) KD_GRAPHICS)) {
-        close(fd);
+    gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
+    if (gr_vt_fd < 0) {
+        // This is non-fatal; post-Cupcake kernels don't have tty0.
+        perror("can't open /dev/tty0");
+    } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
+        // However, if we do open tty0, we expect the ioctl to work.
+        perror("failed KDSETMODE to KD_GRAPHICS on tty0");
+        gr_exit();
         return -1;
     }
 
     gr_fb_fd = get_framebuffer(gr_framebuffer);
-    
-    if(gr_fb_fd < 0) {
-        ioctl(fd, KDSETMODE, (void*) KD_TEXT);
-        close(fd);
+    if (gr_fb_fd < 0) {
+        gr_exit();
         return -1;
     }
 
-    gr_vt_fd = fd;
+    fprintf(stderr, "framebuffer: fd %d (%d x %d)\n",
+            gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height);
 
         /* start with 0 as front (displayed) and 1 as back (drawing) */
     gr_active_fb = 0;
diff --git a/mtdutils/Android.mk b/mtdutils/Android.mk
index c75eb01..1a75423 100644
--- a/mtdutils/Android.mk
+++ b/mtdutils/Android.mk
@@ -1,4 +1,5 @@
 ifneq ($(TARGET_SIMULATOR),true)
+ifeq ($(TARGET_ARCH),arm)
 
 LOCAL_PATH := $(call my-dir)
 include $(CLEAR_VARS)
@@ -18,4 +19,5 @@
 LOCAL_SHARED_LIBRARIES := libcutils libc
 include $(BUILD_EXECUTABLE)
 
+endif	# TARGET_ARCH == arm
 endif	# !TARGET_SIMULATOR
diff --git a/res/images/icon_firmware_install.bmp b/res/images/icon_firmware_install.bmp
index 7096bf9..b0f5f95 100644
--- a/res/images/icon_firmware_install.bmp
+++ b/res/images/icon_firmware_install.bmp
Binary files differ
diff --git a/res/images/icon_installing.bmp b/res/images/icon_installing.bmp
index 2287170..fff99fd 100644
--- a/res/images/icon_installing.bmp
+++ b/res/images/icon_installing.bmp
Binary files differ
diff --git a/tools/Android.mk b/tools/Android.mk
new file mode 100644
index 0000000..6571161
--- /dev/null
+++ b/tools/Android.mk
@@ -0,0 +1 @@
+include $(all-subdir-makefiles)
diff --git a/tools/ota/Android.mk b/tools/ota/Android.mk
index 43e2135..b7a57d6 100644
--- a/tools/ota/Android.mk
+++ b/tools/ota/Android.mk
@@ -18,3 +18,25 @@
 LOCAL_MODULE := make-update-script
 LOCAL_SRC_FILES := make-update-script.c
 include $(BUILD_HOST_EXECUTABLE)
+
+ifneq ($(TARGET_SIMULATOR),true)
+
+include $(CLEAR_VARS)
+LOCAL_FORCE_STATIC_EXECUTABLE := true
+LOCAL_MODULE := add-property-tag
+LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+LOCAL_MODULE_TAGS := debug
+LOCAL_SRC_FILES := add-property-tag.c
+LOCAL_STATIC_LIBRARIES := libc
+include $(BUILD_EXECUTABLE)
+
+include $(CLEAR_VARS)
+LOCAL_FORCE_STATIC_EXECUTABLE := true
+LOCAL_MODULE := check-lost+found
+LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
+LOCAL_MODULE_TAGS := debug
+LOCAL_SRC_FILES := check-lost+found.c
+LOCAL_STATIC_LIBRARIES := libcutils libc
+include $(BUILD_EXECUTABLE)
+
+endif  # !TARGET_SIMULATOR
diff --git a/tools/ota/add-property-tag.c b/tools/ota/add-property-tag.c
new file mode 100644
index 0000000..5277edd
--- /dev/null
+++ b/tools/ota/add-property-tag.c
@@ -0,0 +1,141 @@
+/*
+ * 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 <ctype.h>
+#include <errno.h>
+#include <getopt.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * Append a tag to a property value in a .prop file if it isn't already there.
+ * Normally used to modify build properties to record incremental updates.
+ */
+
+// Return nonzero if the tag should be added to this line.
+int should_tag(const char *line, const char *propname) {
+    const char *prop = strstr(line, propname);
+    if (prop == NULL) return 0;
+
+    // Make sure this is actually the property name (not an accidental hit)
+    const char *ptr;
+    for (ptr = line; ptr < prop && isspace(*ptr); ++ptr) ;
+    if (ptr != prop) return 0;  // Must be at the beginning of the line
+
+    for (ptr += strlen(propname); *ptr != '\0' && isspace(*ptr); ++ptr) ;
+    return (*ptr == '=');  // Must be followed by a '='
+}
+
+// Remove existing tags from the line, return the following number (if any)
+int remove_tag(char *line, const char *tag) {
+    char *pos = strstr(line, tag);
+    if (pos == NULL) return 0;
+
+    char *end;
+    int num = strtoul(pos + strlen(tag), &end, 10);
+    strcpy(pos, end);
+    return num;
+}
+
+// Write line to output with the tag added, adding a number (if >0)
+void write_tagged(FILE *out, const char *line, const char *tag, int number) {
+    const char *end = line + strlen(line);
+    while (end > line && isspace(end[-1])) --end;
+    if (number > 0) {
+        fprintf(out, "%.*s%s%d%s", end - line, line, tag, number, end);
+    } else {
+        fprintf(out, "%.*s%s%s", end - line, line, tag, end);
+    }
+}
+
+int main(int argc, char **argv) {
+    const char *filename = "/system/build.prop";
+    const char *propname = "ro.build.fingerprint";
+    const char *tag = NULL;
+    int do_remove = 0, do_number = 0;
+
+    int opt;
+    while ((opt = getopt(argc, argv, "f:p:rn")) != -1) {
+        switch (opt) {
+        case 'f': filename = optarg; break;
+        case 'p': propname = optarg; break;
+        case 'r': do_remove = 1; break;
+        case 'n': do_number = 1; break;
+        case '?': return 2;
+        }
+    }
+
+    if (argc != optind + 1) {
+        fprintf(stderr,
+            "usage: add-property-tag [flags] tag-to-add\n"
+            "flags: -f /dir/file.prop (default /system/build.prop)\n"
+            "       -p prop.name (default ro.build.fingerprint)\n"
+            "       -r (if set, remove the tag rather than adding it)\n"
+            "       -n (if set, add and increment a number after the tag)\n");
+        return 2;
+    }
+
+    tag = argv[optind];
+    FILE *input = fopen(filename, "r");
+    if (input == NULL) {
+        fprintf(stderr, "can't read %s: %s\n", filename, strerror(errno));
+        return 1;
+    }
+
+    char tmpname[PATH_MAX];
+    snprintf(tmpname, sizeof(tmpname), "%s.tmp", filename);
+    FILE *output = fopen(tmpname, "w");
+    if (output == NULL) {
+        fprintf(stderr, "can't write %s: %s\n", tmpname, strerror(errno));
+        return 1;
+    }
+
+    int found = 0;
+    char line[4096];
+    while (fgets(line, sizeof(line), input)) {
+        if (!should_tag(line, propname)) {
+            fputs(line, output);  // Pass through unmodified
+        } else {
+            found = 1;
+            int number = remove_tag(line, tag);
+            if (do_remove) {
+                fputs(line, output);  // Remove the tag but don't re-add it
+            } else {
+                write_tagged(output, line, tag, number + do_number);
+            }
+        }
+    }
+
+    fclose(input);
+    fclose(output);
+
+    if (!found) {
+        fprintf(stderr, "property %s not found in %s\n", propname, filename);
+        remove(tmpname);
+        return 1;
+    }
+
+    if (rename(tmpname, filename)) {
+        fprintf(stderr, "can't rename %s to %s: %s\n",
+            tmpname, filename, strerror(errno));
+        remove(tmpname);
+        return 1;
+    }
+
+    return 0;
+}
diff --git a/tools/ota/check-lost+found.c b/tools/ota/check-lost+found.c
new file mode 100644
index 0000000..f856275
--- /dev/null
+++ b/tools/ota/check-lost+found.c
@@ -0,0 +1,144 @@
+/*
+ * 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 <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/klog.h>
+#include <sys/reboot.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <time.h>
+
+#include "private/android_filesystem_config.h"
+
+// Sentinel file used to track whether we've forced a reboot
+static const char *kMarkerFile = "/data/misc/check-lost+found-rebooted-2";
+
+// Output file in tombstones directory (first 8K will be uploaded)
+static const char *kOutputDir = "/data/tombstones";
+static const char *kOutputFile = "/data/tombstones/check-lost+found-log";
+
+// Partitions to check
+static const char *kPartitions[] = { "/system", "/data", "/cache", NULL };
+
+/*
+ * 1. If /data/misc/forced-reboot is missing, touch it & force "unclean" boot.
+ * 2. Write a log entry with the number of files in lost+found directories.
+ */
+
+int main(int argc, char **argv) {
+    mkdir(kOutputDir, 0755);
+    chown(kOutputDir, AID_SYSTEM, AID_SYSTEM);
+    FILE *out = fopen(kOutputFile, "a");
+    if (out == NULL) {
+        fprintf(stderr, "Can't write %s: %s\n", kOutputFile, strerror(errno));
+        return 1;
+    }
+
+    // Note: only the first 8K of log will be uploaded, so be terse.
+    time_t start = time(NULL);
+    fprintf(out, "*** check-lost+found ***\nStarted: %s", ctime(&start));
+
+    struct stat st;
+    if (stat(kMarkerFile, &st)) {
+        // No reboot marker -- need to force an unclean reboot.
+        // But first, try to create the marker file.  If that fails,
+        // skip the reboot, so we don't get caught in an infinite loop.
+
+        int fd = open(kMarkerFile, O_WRONLY|O_CREAT, 0444);
+        if (fd >= 0 && close(fd) == 0) {
+            fprintf(out, "Wrote %s, rebooting\n", kMarkerFile);
+            fflush(out);
+            sync();  // Make sure the marker file is committed to disk
+
+            // If possible, dirty each of these partitions before rebooting,
+            // to make sure the filesystem has to do a scan on mount.
+            int i;
+            for (i = 0; kPartitions[i] != NULL; ++i) {
+                char fn[PATH_MAX];
+                snprintf(fn, sizeof(fn), "%s/%s", kPartitions[i], "dirty");
+                fd = open(fn, O_WRONLY|O_CREAT, 0444);
+                if (fd >= 0) {  // Don't sweat it if we can't write the file.
+                    write(fd, fn, sizeof(fn));  // write, you know, some data
+                    close(fd);
+                    unlink(fn);
+                }
+            }
+
+            reboot(RB_AUTOBOOT);  // reboot immediately, with dirty filesystems
+            fprintf(out, "Reboot failed?!\n");
+            exit(1);
+        } else {
+            fprintf(out, "Can't write %s: %s\n", kMarkerFile, strerror(errno));
+        }
+    } else {
+        fprintf(out, "Found %s\n", kMarkerFile);
+    }
+
+    int i;
+    for (i = 0; kPartitions[i] != NULL; ++i) {
+        char fn[PATH_MAX];
+        snprintf(fn, sizeof(fn), "%s/%s", kPartitions[i], "lost+found");
+        DIR *dir = opendir(fn);
+        if (dir == NULL) {
+            fprintf(out, "Can't open %s: %s\n", fn, strerror(errno));
+        } else {
+            int count = 0;
+            struct dirent *ent;
+            while ((ent = readdir(dir))) {
+                if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, ".."))
+                    ++count;
+            }
+            closedir(dir);
+            if (count > 0) {
+                fprintf(out, "OMGZ FOUND %d FILES IN %s\n", count, fn);
+            } else {
+                fprintf(out, "%s is clean\n", fn);
+            }
+        }
+    }
+
+    char dmesg[131073];
+    int len = klogctl(KLOG_READ_ALL, dmesg, sizeof(dmesg) - 1);
+    if (len < 0) {
+        fprintf(out, "Can't read kernel log: %s\n", strerror(errno));
+    } else {  // To conserve space, only write lines with certain keywords
+        fprintf(out, "--- Kernel log ---\n");
+        dmesg[len] = '\0';
+        char *saveptr, *line;
+        int in_yaffs = 0;
+        for (line = strtok_r(dmesg, "\n", &saveptr); line != NULL;
+             line = strtok_r(NULL, "\n", &saveptr)) {
+            if (strstr(line, "yaffs: dev is")) in_yaffs = 1;
+
+            if (in_yaffs ||
+                    strstr(line, "yaffs") ||
+                    strstr(line, "mtd") ||
+                    strstr(line, "msm_nand")) {
+                fprintf(out, "%s\n", line);
+            }
+
+            if (strstr(line, "yaffs_read_super: isCheckpointed")) in_yaffs = 0;
+        }
+    }
+
+    return 0;
+}
diff --git a/tools/ota/make-update-script.c b/tools/ota/make-update-script.c
index 0fb7ed0..225dc52 100644
--- a/tools/ota/make-update-script.c
+++ b/tools/ota/make-update-script.c
@@ -171,7 +171,7 @@
     printf("assert compatible_with(\"0.2\") == \"true\"\n");
 
     // if known, make sure the device name is correct
-    const char *device = getenv("TARGET_PRODUCT");
+    const char *device = getenv("TARGET_DEVICE");
     if (device != NULL) {
         printf("assert getprop(\"ro.product.device\") == \"%s\" || "
                 "getprop(\"ro.build.product\") == \"%s\"\n", device, device);
diff --git a/verifier.c b/verifier.c
index 67a4f39..1180ae8 100644
--- a/verifier.c
+++ b/verifier.c
@@ -126,13 +126,15 @@
             strncpy(sfName, rsaName.str, rsaName.len - sizeof(rsa) + 1);
             strcpy(sfName + rsaName.len - sizeof(rsa) + 1, sf);
             const ZipEntry *sfEntry = mzFindZipEntry(pArchive, sfName);
-            free(sfName);
 
             if (sfEntry == NULL) {
                 LOGW("Missing signature file %s\n", sfName);
+                free(sfName);
                 continue;
             }
 
+            free(sfName);
+
             uint8_t sfDigest[SHA_DIGEST_SIZE];
             if (!digestEntry(pArchive, sfEntry, NULL, 0, sfDigest)) continue;