Merge "init: Set ADDR_COMPAT_LAYOUT before spawning processes." into jb-mr1-dev
diff --git a/fastboot/engine.c b/fastboot/engine.c
index 8d7b68a..7a55260 100644
--- a/fastboot/engine.c
+++ b/fastboot/engine.c
@@ -144,6 +144,39 @@
     { "ext4", generate_ext4_image, cleanup_image }
 };
 
+/* Return true if this partition is supported by the fastboot format command.
+ * It is also used to determine if we should first erase a partition before
+ * flashing it with an ext4 filesystem.  See needs_erase()
+ *
+ * Not all devices report the filesystem type, so don't report any errors,
+ * just return false.
+ */
+int fb_format_supported(usb_handle *usb, const char *partition)
+{
+    char response[FB_RESPONSE_SZ+1];
+    struct generator *generator = NULL;
+    int status;
+    unsigned int i;
+
+    status = fb_getvar(usb, response, "partition-type:%s", partition);
+    if (status) {
+        return 0;
+    }
+
+    for (i = 0; i < ARRAY_SIZE(generators); i++) {
+        if (!strncmp(generators[i].fs_type, response, FB_RESPONSE_SZ)) {
+            generator = &generators[i];
+            break;
+        }
+    }
+
+    if (generator) {
+        return 1;
+    }
+
+    return 0;
+}
+
 static int cb_default(Action *a, int status, char *resp)
 {
     if (status) {
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index 06a6a13..3de6d7d 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -287,7 +287,10 @@
             "  help                                     show this help message\n"
             "\n"
             "options:\n"
-            "  -w                                       erase userdata and cache\n"
+            "  -w                                       erase userdata and cache (and format\n"
+            "                                           if supported by partition type)\n"
+            "  -u                                       do not first erase partition before\n"
+            "                                           formatting\n"
             "  -s <specific device>                     specify device serial number\n"
             "                                           or path to device port\n"
             "  -l                                       with \"devices\", lists device paths\n"
@@ -562,6 +565,18 @@
     return 0;
 }
 
+/* Until we get lazy inode table init working in make_ext4fs, we need to
+ * erase partitions of type ext4 before flashing a filesystem so no stale
+ * inodes are left lying around.  Otherwise, e2fsck gets very upset.
+ */
+static int needs_erase(const char *part)
+{
+    /* The function fb_format_supported() currently returns the value
+     * we want, so just call it.
+     */
+     return fb_format_supported(usb, part);
+}
+
 void do_flash(usb_handle *usb, const char *pname, const char *fname)
 {
     int64_t sz64;
@@ -597,7 +612,7 @@
     fb_queue_command("signature", "installing signature");
 }
 
-void do_update(char *fn)
+void do_update(char *fn, int erase_first)
 {
     void *zdata;
     unsigned zsize;
@@ -635,17 +650,26 @@
     data = unzip_file(zip, "boot.img", &sz);
     if (data == 0) die("update package missing boot.img");
     do_update_signature(zip, "boot.sig");
+    if (erase_first && needs_erase("boot")) {
+        fb_queue_erase("boot");
+    }
     fb_queue_flash("boot", data, sz);
 
     data = unzip_file(zip, "recovery.img", &sz);
     if (data != 0) {
         do_update_signature(zip, "recovery.sig");
+        if (erase_first && needs_erase("recovery")) {
+            fb_queue_erase("recovery");
+        }
         fb_queue_flash("recovery", data, sz);
     }
 
     data = unzip_file(zip, "system.img", &sz);
     if (data == 0) die("update package missing system.img");
     do_update_signature(zip, "system.sig");
+    if (erase_first && needs_erase("system")) {
+        fb_queue_erase("system");
+    }
     fb_queue_flash("system", data, sz);
 }
 
@@ -667,7 +691,7 @@
     fb_queue_command("signature", "installing signature");
 }
 
-void do_flashall(void)
+void do_flashall(int erase_first)
 {
     char *fname;
     void *data;
@@ -687,12 +711,18 @@
     data = load_file(fname, &sz);
     if (data == 0) die("could not load boot.img: %s", strerror(errno));
     do_send_signature(fname);
+    if (erase_first && needs_erase("boot")) {
+        fb_queue_erase("boot");
+    }
     fb_queue_flash("boot", data, sz);
 
     fname = find_item("recovery", product);
     data = load_file(fname, &sz);
     if (data != 0) {
         do_send_signature(fname);
+        if (erase_first && needs_erase("recovery")) {
+            fb_queue_erase("recovery");
+        }
         fb_queue_flash("recovery", data, sz);
     }
 
@@ -700,6 +730,9 @@
     data = load_file(fname, &sz);
     if (data == 0) die("could not load system.img: %s", strerror(errno));
     do_send_signature(fname);
+    if (erase_first && needs_erase("system")) {
+        fb_queue_erase("system");
+    }
     fb_queue_flash("system", data, sz);
 }
 
@@ -770,6 +803,7 @@
     int wants_wipe = 0;
     int wants_reboot = 0;
     int wants_reboot_bootloader = 0;
+    int erase_first = 1;
     void *data;
     unsigned sz;
     unsigned page_size = 2048;
@@ -782,7 +816,7 @@
     serial = getenv("ANDROID_SERIAL");
 
     while (1) {
-        c = getopt_long(argc, argv, "wb:n:s:S:lp:c:i:m:h", &longopts, NULL);
+        c = getopt_long(argc, argv, "wub:n:s:S:lp:c:i:m:h", &longopts, NULL);
         if (c < 0) {
             break;
         }
@@ -791,6 +825,9 @@
         case 'w':
             wants_wipe = 1;
             break;
+        case 'u':
+            erase_first = 0;
+            break;
         case 'b':
             base_addr = strtoul(optarg, 0, 16);
             break;
@@ -864,10 +901,18 @@
             skip(2);
         } else if(!strcmp(*argv, "erase")) {
             require(2);
+
+            if (fb_format_supported(usb, argv[1])) {
+                fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
+            }
+
             fb_queue_erase(argv[1]);
             skip(2);
         } else if(!strcmp(*argv, "format")) {
             require(2);
+            if (erase_first && needs_erase(argv[1])) {
+                fb_queue_erase(argv[1]);
+            }
             fb_queue_format(argv[1], 0);
             skip(2);
         } else if(!strcmp(*argv, "signature")) {
@@ -915,6 +960,9 @@
                 skip(2);
             }
             if (fname == 0) die("cannot determine image filename for '%s'", pname);
+            if (erase_first && needs_erase(pname)) {
+                fb_queue_erase(pname);
+            }
             do_flash(usb, pname, fname);
         } else if(!strcmp(*argv, "flash:raw")) {
             char *pname = argv[1];
@@ -932,14 +980,14 @@
             fb_queue_flash(pname, data, sz);
         } else if(!strcmp(*argv, "flashall")) {
             skip(1);
-            do_flashall();
+            do_flashall(erase_first);
             wants_reboot = 1;
         } else if(!strcmp(*argv, "update")) {
             if (argc > 1) {
-                do_update(argv[1]);
+                do_update(argv[1], erase_first);
                 skip(2);
             } else {
-                do_update("update.zip");
+                do_update("update.zip", erase_first);
                 skip(1);
             }
             wants_reboot = 1;
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
index b4cf195..c1b2964 100644
--- a/fastboot/fastboot.h
+++ b/fastboot/fastboot.h
@@ -45,7 +45,8 @@
 
 /* engine.c - high level command queue engine */
 int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...);
-void fb_queue_flash(const char *ptn, void *data, unsigned sz);;
+int fb_format_supported(usb_handle *usb, const char *partition);
+void fb_queue_flash(const char *ptn, void *data, unsigned sz);
 void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz);
 void fb_queue_erase(const char *ptn);
 void fb_queue_format(const char *ptn, int skip_if_not_supported);
diff --git a/include/private/android_filesystem_config.h b/include/private/android_filesystem_config.h
index 8f4956c..7e34da8 100644
--- a/include/private/android_filesystem_config.h
+++ b/include/private/android_filesystem_config.h
@@ -76,6 +76,7 @@
 #define AID_NET_ADMIN     3005  /* can configure interfaces and routing tables. */
 #define AID_NET_BW_STATS  3006  /* read bandwidth statistics */
 #define AID_NET_BW_ACCT   3007  /* change bandwidth statistics accounting */
+#define AID_NET_BT_STACK  3008  /* bluetooth: access config files */
 
 #define AID_MISC          9998  /* access to misc storage */
 #define AID_NOBODY        9999
@@ -122,6 +123,7 @@
     { "diag",      AID_DIAG, },
     { "net_bt_admin", AID_NET_BT_ADMIN, },
     { "net_bt",    AID_NET_BT, },
+    { "net_bt_stack",    AID_NET_BT_STACK, },
     { "sdcard_r",  AID_SDCARD_R, },
     { "sdcard_rw", AID_SDCARD_RW, },
     { "media_rw",  AID_MEDIA_RW, },
diff --git a/include/system/window.h b/include/system/window.h
index 1c168db..4698fb3 100644
--- a/include/system/window.h
+++ b/include/system/window.h
@@ -18,6 +18,7 @@
 #define SYSTEM_CORE_INCLUDE_ANDROID_WINDOW_H
 
 #include <cutils/native_handle.h>
+#include <errno.h>
 #include <limits.h>
 #include <stdint.h>
 #include <string.h>
@@ -824,17 +825,7 @@
  */
 static inline int native_window_dequeue_buffer_and_wait(ANativeWindow *anw,
         struct ANativeWindowBuffer** anb) {
-    int fenceFd = -1;
-    int err = anw->dequeueBuffer(anw, anb, &fenceFd);
-    if (err == 0 && fenceFd != -1) {
-        err = sync_wait(fenceFd, UINT_MAX);
-        close(fenceFd);
-        if (err != 0) {
-            anw->cancelBuffer(anw, *anb, -1);
-            *anb = NULL;
-        }
-    }
-    return err;
+    return anw->dequeueBuffer_DEPRECATED(anw, anb);
 }
 
 
diff --git a/rootdir/init.rc b/rootdir/init.rc
index e9abadd..106cae5 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -195,7 +195,7 @@
     # create basic filesystem structure
     mkdir /data/misc 01771 system misc
     mkdir /data/misc/adb 02750 system shell
-    mkdir /data/misc/bluedroid 0770 bluetooth bluetooth
+    mkdir /data/misc/bluedroid 0770 bluetooth net_bt_stack
     mkdir /data/misc/bluetooth 0770 system system
     mkdir /data/misc/keystore 0700 keystore keystore
     mkdir /data/misc/keychain 0771 system system