Merge "Make logd more aggressive when scanning for the position from which to resume logging." into pi-dev
diff --git a/fastboot/Android.mk b/fastboot/Android.mk
index f5bcc26..944b00b 100644
--- a/fastboot/Android.mk
+++ b/fastboot/Android.mk
@@ -22,7 +22,8 @@
 
 LOCAL_C_INCLUDES := \
   $(LOCAL_PATH)/../adb \
-  $(LOCAL_PATH)/../mkbootimg \
+
+LOCAL_HEADER_LIBRARIES := bootimg_headers
 
 LOCAL_SRC_FILES := \
     bootimg_utils.cpp \
diff --git a/fastboot/bootimg_utils.cpp b/fastboot/bootimg_utils.cpp
index 62a26b3..23443ec 100644
--- a/fastboot/bootimg_utils.cpp
+++ b/fastboot/bootimg_utils.cpp
@@ -34,26 +34,25 @@
 #include <stdlib.h>
 #include <string.h>
 
-void bootimg_set_cmdline(boot_img_hdr* h, const char* cmdline) {
+void bootimg_set_cmdline(boot_img_hdr_v1* h, const char* cmdline) {
     if (strlen(cmdline) >= sizeof(h->cmdline)) die("command line too large: %zu", strlen(cmdline));
     strcpy(reinterpret_cast<char*>(h->cmdline), cmdline);
 }
 
-boot_img_hdr* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset,
-                        void* ramdisk, int64_t ramdisk_size, off_t ramdisk_offset,
-                        void* second, int64_t second_size, off_t second_offset,
-                        size_t page_size, size_t base, off_t tags_offset,
-                        int64_t* bootimg_size)
-{
+boot_img_hdr_v1* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset, void* ramdisk,
+                           int64_t ramdisk_size, off_t ramdisk_offset, void* second,
+                           int64_t second_size, off_t second_offset, size_t page_size, size_t base,
+                           off_t tags_offset, uint32_t header_version, int64_t* bootimg_size) {
     size_t page_mask = page_size - 1;
 
+    int64_t header_actual = sizeof(boot_img_hdr_v1) & (~page_mask);
     int64_t kernel_actual = (kernel_size + page_mask) & (~page_mask);
     int64_t ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask);
     int64_t second_actual = (second_size + page_mask) & (~page_mask);
 
-    *bootimg_size = page_size + kernel_actual + ramdisk_actual + second_actual;
+    *bootimg_size = header_actual + kernel_actual + ramdisk_actual + second_actual;
 
-    boot_img_hdr* hdr = reinterpret_cast<boot_img_hdr*>(calloc(*bootimg_size, 1));
+    boot_img_hdr_v1* hdr = reinterpret_cast<boot_img_hdr_v1*>(calloc(*bootimg_size, 1));
     if (hdr == nullptr) {
         return hdr;
     }
@@ -71,9 +70,13 @@
 
     hdr->page_size =    page_size;
 
+    if (header_version) {
+        hdr->header_version = header_version;
+        hdr->header_size = sizeof(boot_img_hdr_v1);
+    }
+
     memcpy(hdr->magic + page_size, kernel, kernel_size);
     memcpy(hdr->magic + page_size + kernel_actual, ramdisk, ramdisk_size);
     memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual, second, second_size);
-
     return hdr;
 }
diff --git a/fastboot/bootimg_utils.h b/fastboot/bootimg_utils.h
index fcc8662..d3993f5 100644
--- a/fastboot/bootimg_utils.h
+++ b/fastboot/bootimg_utils.h
@@ -33,11 +33,10 @@
 #include <inttypes.h>
 #include <sys/types.h>
 
-void bootimg_set_cmdline(boot_img_hdr* h, const char* cmdline);
-boot_img_hdr* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset,
-                        void* ramdisk, int64_t ramdisk_size, off_t ramdisk_offset,
-                        void* second, int64_t second_size, off_t second_offset,
-                        size_t page_size, size_t base, off_t tags_offset,
-                        int64_t* bootimg_size);
+void bootimg_set_cmdline(boot_img_hdr_v1* h, const char* cmdline);
+boot_img_hdr_v1* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset, void* ramdisk,
+                           int64_t ramdisk_size, off_t ramdisk_offset, void* second,
+                           int64_t second_size, off_t second_offset, size_t page_size, size_t base,
+                           off_t tags_offset, uint32_t header_version, int64_t* bootimg_size);
 
 #endif
diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp
index 237f081..0ce3855 100644
--- a/fastboot/fastboot.cpp
+++ b/fastboot/fastboot.cpp
@@ -435,6 +435,9 @@
 #endif
             "  --unbuffered                             Do not buffer input or output.\n"
             "  --version                                Display version.\n"
+            "  --header-version                         Set boot image header version while\n"
+            "                                           using flash:raw and boot commands to \n"
+            "                                           to create a boot image.\n"
             "  -h, --help                               show this message.\n"
         );
     // clang-format off
@@ -443,17 +446,23 @@
 
 static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
                                  const std::string& second_stage, int64_t* sz,
-                                 const char* cmdline) {
+                                 const char* cmdline, uint32_t header_version) {
     int64_t ksize;
     void* kdata = load_file(kernel.c_str(), &ksize);
     if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
 
     // Is this actually a boot image?
-    if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr))) {
+    if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
         die("cannot load '%s': too short", kernel.c_str());
     }
     if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
-        if (cmdline) bootimg_set_cmdline(reinterpret_cast<boot_img_hdr*>(kdata), cmdline);
+        if (cmdline) bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), cmdline);
+        uint32_t header_version_existing =
+                reinterpret_cast<boot_img_hdr_v1*>(kdata)->header_version;
+        if (header_version != header_version_existing) {
+            die("header version mismatch, expected: %" PRIu32 " found %" PRIu32 "",
+                header_version, header_version_existing);
+        }
 
         if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
 
@@ -477,13 +486,13 @@
 
     fprintf(stderr,"creating boot image...\n");
     int64_t bsize = 0;
-    void* bdata = mkbootimg(kdata, ksize, kernel_offset,
+    boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, kernel_offset,
                       rdata, rsize, ramdisk_offset,
                       sdata, ssize, second_offset,
-                      page_size, base_addr, tags_offset, &bsize);
+                      page_size, base_addr, tags_offset, header_version, &bsize);
     if (bdata == nullptr) die("failed to create boot.img");
 
-    if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
+    if (cmdline) bootimg_set_cmdline(bdata, cmdline);
     fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
     *sz = bsize;
 
@@ -1500,6 +1509,7 @@
     bool erase_first = true;
     bool set_fbe_marker = false;
     void *data;
+    uint32_t header_version = 0;
     int64_t sz;
     int longindex;
     std::string slot_override;
@@ -1525,6 +1535,7 @@
         {"skip-reboot", no_argument, 0, 0},
         {"disable-verity", no_argument, 0, 0},
         {"disable-verification", no_argument, 0, 0},
+        {"header-version", required_argument, 0, 0},
 #if !defined(_WIN32)
         {"wipe-and-use-fbe", no_argument, 0, 0},
 #endif
@@ -1617,6 +1628,8 @@
                 wants_wipe = true;
                 set_fbe_marker = true;
 #endif
+            } else if (strcmp("header-version", longopts[longindex].name) == 0) {
+                header_version = strtoul(optarg, nullptr, 0);
             } else {
                 fprintf(stderr, "Internal error in options processing for %s\n",
                     longopts[longindex].name);
@@ -1749,7 +1762,7 @@
             std::string second_stage;
             if (!args.empty()) second_stage = next_arg(&args);
 
-            data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
+            data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline, header_version);
             fb_queue_download("boot.img", data, sz);
             fb_queue_command("boot", "booting");
         } else if (command == "flash") {
@@ -1778,7 +1791,7 @@
             std::string second_stage;
             if (!args.empty()) second_stage = next_arg(&args);
 
-            data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
+            data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline, header_version);
             auto flashraw = [&](const std::string& partition) {
                 fb_queue_flash(partition, data, sz);
             };
diff --git a/fs_mgr/fs_mgr_format.cpp b/fs_mgr/fs_mgr_format.cpp
index cbd8ffa..63a6839 100644
--- a/fs_mgr/fs_mgr_format.cpp
+++ b/fs_mgr/fs_mgr_format.cpp
@@ -114,10 +114,19 @@
     }
 
     std::string size_str = std::to_string(dev_sz / 4096);
+    // clang-format off
     const char* const args[] = {
-        "/system/bin/make_f2fs", "-d1", "-f",
-        "-O", "encrypt", "-O", "quota",
-        fs_blkdev, size_str.c_str(), nullptr};
+        "/system/bin/make_f2fs",
+        "-d1",
+        "-f",
+        "-O", "encrypt",
+        "-O", "quota",
+        "-w", "4096",
+        fs_blkdev,
+        size_str.c_str(),
+        nullptr
+    };
+    // clang-format on
 
     return android_fork_execvp_ext(arraysize(args), const_cast<char**>(args), NULL, true,
                                    LOG_KLOG, true, nullptr, nullptr, 0);
diff --git a/fs_mgr/fs_mgr_fstab.cpp b/fs_mgr/fs_mgr_fstab.cpp
index 9b7405a..9d504d6 100644
--- a/fs_mgr/fs_mgr_fstab.cpp
+++ b/fs_mgr/fs_mgr_fstab.cpp
@@ -114,17 +114,21 @@
 #define EM_ICE          2
 #define EM_AES_256_CTS  3
 #define EM_AES_256_HEH  4
+#define EM_SPECK_128_256_XTS 5
+#define EM_SPECK_128_256_CTS 6
 
 static const struct flag_list file_contents_encryption_modes[] = {
     {"aes-256-xts", EM_AES_256_XTS},
+    {"speck128/256-xts", EM_SPECK_128_256_XTS},
     {"software", EM_AES_256_XTS}, /* alias for backwards compatibility */
-    {"ice", EM_ICE}, /* hardware-specific inline cryptographic engine */
+    {"ice", EM_ICE},              /* hardware-specific inline cryptographic engine */
     {0, 0},
 };
 
 static const struct flag_list file_names_encryption_modes[] = {
     {"aes-256-cts", EM_AES_256_CTS},
     {"aes-256-heh", EM_AES_256_HEH},
+    {"speck128/256-cts", EM_SPECK_128_256_CTS},
     {0, 0},
 };
 
diff --git a/init/Android.bp b/init/Android.bp
index 31c8efb..70a4ac6 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -127,9 +127,7 @@
         "watchdogd.cpp",
     ],
     whole_static_libs: ["libcap"],
-    include_dirs: [
-        "system/core/mkbootimg",
-    ],
+    header_libs: ["bootimg_headers"],
     proto: {
         type: "lite",
         export_proto_headers: true,
diff --git a/libsystem/include/system/graphics-base-v1.0.h b/libsystem/include/system/graphics-base-v1.0.h
index 987a39d..44913cc 100644
--- a/libsystem/include/system/graphics-base-v1.0.h
+++ b/libsystem/include/system/graphics-base-v1.0.h
@@ -1,6 +1,6 @@
 // This file is autogenerated by hidl-gen. Do not edit manually.
 // Source: android.hardware.graphics.common@1.0
-// Root: android.hardware:hardware/interfaces
+// Location: hardware/interfaces/graphics/common/1.0/
 
 #ifndef HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_0_EXPORTED_CONSTANTS_H_
 #define HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_0_EXPORTED_CONSTANTS_H_
diff --git a/libsystem/include/system/graphics-base-v1.1.h b/libsystem/include/system/graphics-base-v1.1.h
index 12d01c1..f95b9ba 100644
--- a/libsystem/include/system/graphics-base-v1.1.h
+++ b/libsystem/include/system/graphics-base-v1.1.h
@@ -1,6 +1,6 @@
 // This file is autogenerated by hidl-gen. Do not edit manually.
 // Source: android.hardware.graphics.common@1.1
-// Root: android.hardware:hardware/interfaces
+// Location: hardware/interfaces/graphics/common/1.1/
 
 #ifndef HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_1_EXPORTED_CONSTANTS_H_
 #define HIDL_GENERATED_ANDROID_HARDWARE_GRAPHICS_COMMON_V1_1_EXPORTED_CONSTANTS_H_
@@ -24,8 +24,23 @@
         281411584,  // ((STANDARD_BT2020 | TRANSFER_SMPTE_170M) | RANGE_LIMITED)
     HAL_DATASPACE_BT2020_ITU_PQ =
         298188800,  // ((STANDARD_BT2020 | TRANSFER_ST2084) | RANGE_LIMITED)
+    HAL_DATASPACE_BT2020_ITU_HLG = 302383104,  // ((STANDARD_BT2020 | TRANSFER_HLG) | RANGE_LIMITED)
+    HAL_DATASPACE_BT2020_HLG = 168165376,      // ((STANDARD_BT2020 | TRANSFER_HLG) | RANGE_FULL)
 } android_dataspace_v1_1_t;
 
+typedef enum {
+    HAL_COLOR_MODE_BT2020 = 10,
+    HAL_COLOR_MODE_BT2100_PQ = 11,
+    HAL_COLOR_MODE_BT2100_HLG = 12,
+} android_color_mode_v1_1_t;
+
+typedef enum {
+    HAL_RENDER_INTENT_COLORIMETRIC = 0,
+    HAL_RENDER_INTENT_ENHANCE = 1,
+    HAL_RENDER_INTENT_TONE_MAP_COLORIMETRIC = 2,
+    HAL_RENDER_INTENT_TONE_MAP_ENHANCE = 3,
+} android_render_intent_v1_1_t;
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/mkbootimg/Android.bp b/mkbootimg/Android.bp
new file mode 100644
index 0000000..b494346
--- /dev/null
+++ b/mkbootimg/Android.bp
@@ -0,0 +1,32 @@
+// Copyright 2012 The Android Open Source Project
+
+cc_library_headers {
+    name: "libmkbootimg_abi_headers",
+    vendor_available: true,
+    export_include_dirs: ["include"],
+}
+
+cc_library_headers {
+    name: "bootimg_headers",
+    vendor_available: true,
+    export_include_dirs: ["include/bootimg"],
+    host_supported: true,
+    target: {
+        windows: {
+            enabled: true,
+        },
+    },
+}
+
+cc_library {
+    name: "libmkbootimg_abi_check",
+    vendor_available: true,
+    vndk: {
+        enabled: true,
+    },
+    srcs: [
+        "mkbootimg_dummy.cpp",
+    ],
+    header_libs: ["libmkbootimg_abi_headers"],
+    export_header_lib_headers: ["libmkbootimg_abi_headers"],
+}
diff --git a/mkbootimg/include/abi_check/mkbootimg_abi_check.h b/mkbootimg/include/abi_check/mkbootimg_abi_check.h
new file mode 100644
index 0000000..d478aba
--- /dev/null
+++ b/mkbootimg/include/abi_check/mkbootimg_abi_check.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2018 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 <bootimg/bootimg.h>
+
+// This header has been created for the following reaons:
+//    1) In order for a change in a user defined type to be classified as API /
+//       ABI breaking, it needs to be referenced by an 'exported interface'
+//       (in this case the function mkbootimg_dummy).
+//    2) Since 'mkbootimg_dummy' needs to be exported, we need to have it
+//       exposed through a public header.
+//    3) It is desirable not to pollute bootimg.h with interfaces which are not
+//       'used' in reality by on device binaries. Furthermore, bootimg.h might
+//       be exported by a library in the future, so we must avoid polluting it.
+void mkbootimg_dummy(boot_img_hdr*);
diff --git a/mkbootimg/bootimg.h b/mkbootimg/include/bootimg/bootimg.h
similarity index 91%
rename from mkbootimg/bootimg.h
rename to mkbootimg/include/bootimg/bootimg.h
index 1be8c22..4311b46 100644
--- a/mkbootimg/bootimg.h
+++ b/mkbootimg/include/bootimg/bootimg.h
@@ -2,16 +2,16 @@
 **
 ** Copyright 2007, 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 
+** 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 
+**     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 
+** 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.
 */
 
@@ -34,17 +34,17 @@
 struct boot_img_hdr_v0 {
     uint8_t magic[BOOT_MAGIC_SIZE];
 
-    uint32_t kernel_size;  /* size in bytes */
-    uint32_t kernel_addr;  /* physical load addr */
+    uint32_t kernel_size; /* size in bytes */
+    uint32_t kernel_addr; /* physical load addr */
 
     uint32_t ramdisk_size; /* size in bytes */
     uint32_t ramdisk_addr; /* physical load addr */
 
-    uint32_t second_size;  /* size in bytes */
-    uint32_t second_addr;  /* physical load addr */
+    uint32_t second_size; /* size in bytes */
+    uint32_t second_addr; /* physical load addr */
 
-    uint32_t tags_addr;    /* physical addr for kernel tags */
-    uint32_t page_size;    /* flash page size we assume */
+    uint32_t tags_addr; /* physical addr for kernel tags */
+    uint32_t page_size; /* flash page size we assume */
     /*
      * version for the boot image header.
      */
diff --git a/mkbootimg/mkbootimg_dummy.cpp b/mkbootimg/mkbootimg_dummy.cpp
new file mode 100644
index 0000000..410d379
--- /dev/null
+++ b/mkbootimg/mkbootimg_dummy.cpp
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2018 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 <abi_check/mkbootimg_abi_check.h>
+
+void mkbootimg_dummy(boot_img_hdr* hdr) {
+    // TODO: Hack to trigger abi checks, remove this.
+    if (hdr) {
+        hdr--;
+    }
+}
diff --git a/storaged/include/storaged_info.h b/storaged/include/storaged_info.h
index 88a53de..9c3d0e7 100644
--- a/storaged/include/storaged_info.h
+++ b/storaged/include/storaged_info.h
@@ -38,6 +38,7 @@
 class storage_info_t {
   protected:
     FRIEND_TEST(storaged_test, storage_info_t);
+    FRIEND_TEST(storaged_test, storage_info_t_proto);
     // emmc lifetime
     uint16_t eol;                   // pre-eol (end of life) information
     uint16_t lifetime_a;            // device life time estimation (type A)
diff --git a/storaged/storaged_info.cpp b/storaged/storaged_info.cpp
index 055f375..5605f66 100644
--- a/storaged/storaged_info.cpp
+++ b/storaged/storaged_info.cpp
@@ -157,11 +157,14 @@
         return;
     }
 
-    recent_perf.erase(recent_perf.begin() + nr_samples,
-                      recent_perf.end());
+    if (nr_samples < recent_perf.size()) {
+        recent_perf.erase(recent_perf.begin() + nr_samples, recent_perf.end());
+    }
 
-    uint32_t daily_avg_bw = accumulate(recent_perf.begin(),
-        recent_perf.begin() + nr_samples, 0) / nr_samples;
+    uint32_t daily_avg_bw = 0;
+    if (!recent_perf.empty()) {
+        daily_avg_bw = accumulate(recent_perf.begin(), recent_perf.end(), 0) / recent_perf.size();
+    }
 
     day_start_tp = tp - chrono::seconds(duration_cast<chrono::seconds>(
         tp.time_since_epoch()).count() % DAY_TO_SEC);
@@ -176,6 +179,7 @@
         return;
     }
 
+    DCHECK(nr_days > 0);
     uint32_t week_avg_bw = accumulate(daily_perf.begin(),
         daily_perf.begin() + nr_days, 0) / nr_days;
 
diff --git a/storaged/tests/storaged_test.cpp b/storaged/tests/storaged_test.cpp
index d1fa9ed..ec47b65 100644
--- a/storaged/tests/storaged_test.cpp
+++ b/storaged/tests/storaged_test.cpp
@@ -416,6 +416,31 @@
     }
 }
 
+TEST(storaged_test, storage_info_t_proto) {
+    storage_info_t si;
+    si.day_start_tp = {};
+
+    IOPerfHistory proto;
+    proto.set_nr_samples(10);
+    proto.set_day_start_sec(0);
+    si.load_perf_history_proto(proto);
+
+    // Skip ahead > 1 day, with no data points in the previous day.
+    time_point<system_clock> stp;
+    stp += hours(36);
+    si.update_perf_history(100, stp);
+
+    vector<int> history = si.get_perf_history();
+    EXPECT_EQ(history.size(), 63UL);
+    EXPECT_EQ(history[0], 1);
+    EXPECT_EQ(history[1], 7);
+    EXPECT_EQ(history[2], 52);
+    EXPECT_EQ(history[3], 100);
+    for (size_t i = 4; i < history.size(); i++) {
+        EXPECT_EQ(history[i], 0);
+    }
+}
+
 TEST(storaged_test, uid_monitor) {
     uid_monitor uidm;