Merge changes from topic "repopick" into o8.1

* changes:
  We are a non-profit company, not an organization
  Added execution permission to repopick script
  repopick: Try to keep the changes sorted
  repopick: Allow to pick changes by URL
  repopick: Actually print the patchset number
  repopick: Ignore the trailing slash in change numbers
  tools: repopick: Correctly order changeset
  repopick: support auth'ing to gerrit and picking drafts
  Use secure https
  repopick: Support overriding the default commits count to check
  repopick: Add 'reset' option
  repopick: don't bail on drafts
  cm: build: Don't convert patchset to int if it's None
  cm: build: Fix cherry picking specific patchsets
  repopick: encode commit subjects in utf-8 for printing
  repopick: Avoid failing on commits lookup
  repopick: fix indentation derp
  Added repopick from LOS
diff --git a/build/core/sdllvm-lto-defs.mk b/build/core/sdllvm-lto-defs.mk
new file mode 100644
index 0000000..f965dd8
--- /dev/null
+++ b/build/core/sdllvm-lto-defs.mk
@@ -0,0 +1,35 @@
+ifeq ($(LOCAL_MODULE_CLASS), STATIC_LIBRARIES)
+# For STATIC_LIBRARIES we need to use SD LLVM's archiver and archiver flags.
+
+AR := $(SDCLANG_PATH)/llvm-ar
+
+ifeq ($(LOCAL_SDCLANG_2),true)
+AR := $(SDCLANG_PATH_2)/llvm-ar
+endif
+
+ARFLAGS := crsD
+
+# For 32 bit
+$(LOCAL_BUILT_MODULE) : $(combo_2nd_arch_prefix)TARGET_AR := $(AR)
+$(LOCAL_BUILT_MODULE) : $(combo_var_prefix)GLOBAL_ARFLAGS := $(ARFLAGS)
+
+# For 64 bit
+intermediates := $(call local-intermediates-dir,,$(LOCAL_2ND_ARCH_VAR_PREFIX))
+LOCAL_BUILT_MODULE_64 := $(intermediates)/$(my_built_module_stem)
+
+$(LOCAL_BUILT_MODULE_64) : TARGET_AR := $(AR)
+$(LOCAL_BUILT_MODULE_64) : TARGET_GLOBAL_ARFLAGS := $(ARFLAGS)
+
+else
+# For SHARED_LIBRARIES and EXECUTABLES we need to filter out flags not
+# needed/understood by SD LLVM's Linker.
+
+linked_module_32 := $(intermediates)/LINKED/$(my_built_module_stem)
+intermediates    := $(call local-intermediates-dir,,$(LOCAL_2ND_ARCH_VAR_PREFIX))
+linked_module_64 := $(intermediates)/LINKED/$(my_built_module_stem)
+
+FLAGS_TO_BE_FILTERED := -Wl,--icf=safe -Wl,--no-undefined-version -Wl,--fix-cortex-a53-843419 -fuse-ld=gold
+$(linked_module_32) : PRIVATE_TARGET_GLOBAL_LDFLAGS := $(filter-out $(FLAGS_TO_BE_FILTERED),$(PRIVATE_TARGET_GLOBAL_LDFLAGS))
+$(linked_module_64) : PRIVATE_TARGET_GLOBAL_LDFLAGS := $(filter-out $(FLAGS_TO_BE_FILTERED),$(PRIVATE_TARGET_GLOBAL_LDFLAGS))
+
+endif
diff --git a/build/envsetup.sh b/build/envsetup.sh
index 9ed3341..7813329 100644
--- a/build/envsetup.sh
+++ b/build/envsetup.sh
@@ -91,7 +91,7 @@
     proj="$(pwd -P | sed "s#$ANDROID_BUILD_TOP/##g")"
 
     if (echo "$proj" | egrep -q 'external|system|build|bionic|art|libcore|prebuilt|dalvik') ; then
-        pfx="android_"
+        pfx="platform_"
     fi
 
     project="${proj//\//_}"
@@ -160,7 +160,7 @@
 function bliss_push()
 {
     local branch ssh_name path_opt proj
-    branch="lp5.1"
+    branch="o8.1"
     ssh_name="bliss_review"
     path_opt=
 
@@ -176,7 +176,7 @@
     proj="${proj//\//_}"
 
     if (echo "$proj" | egrep -q 'external|system|build|bionic|art|libcore|prebuilt|dalvik') ; then
-        proj="android_$proj"
+        proj="platform_$proj"
     fi
 
     git $path_opt push "ssh://${ssh_name}/BlissRoms/$proj" "HEAD:refs/for/$branch"
@@ -197,6 +197,35 @@
     done |column
 }
 
+function mk_timer()
+{
+    local start_time=$(date +"%s")
+    $@
+    local ret=$?
+    local end_time=$(date +"%s")
+    local tdiff=$(($end_time-$start_time))
+    local hours=$(($tdiff / 3600 ))
+    local mins=$((($tdiff % 3600) / 60))
+    local secs=$(($tdiff % 60))
+    local ncolors=$(tput colors 2>/dev/null)
+    echo
+    if [ $ret -eq 0 ] ; then
+        echo -n "#### make completed successfully "
+    else
+        echo -n "#### make failed to build some targets "
+    fi
+    if [ $hours -gt 0 ] ; then
+        printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
+    elif [ $mins -gt 0 ] ; then
+        printf "(%02g:%02g (mm:ss))" $mins $secs
+    elif [ $secs -gt 0 ] ; then
+        printf "(%s seconds)" $secs
+    fi
+    echo " ####"
+    echo
+    return $ret
+}
+
 function brunch()
 {
     breakfast $*
@@ -286,3 +315,18 @@
     adb push $OUT/$* /$*
     adb reboot
 }
+
+# Enable SD-LLVM if available
+if [ -d $(gettop)/prebuilts/snapdragon-llvm/toolchains ]; then
+    case `uname -s` in
+        Darwin)
+            # Darwin is not supported yet
+            ;;
+        *)
+            export SDCLANG=true
+            export SDCLANG_PATH=$(gettop)/prebuilts/snapdragon-llvm/toolchains/llvm-Snapdragon_LLVM_for_Android_4.0/prebuilt/linux-x86_64/bin
+            export SDCLANG_PATH_2=$(gettop)/prebuilts/snapdragon-llvm/toolchains/llvm-Snapdragon_LLVM_for_Android_4.0/prebuilt/linux-x86_64/bin
+            export SDCLANG_LTO_DEFS=$(gettop)/vendor/lineage/build/core/sdllvm-lto-defs.mk
+            ;;
+    esac
+fi
diff --git a/build/soong/android/variable.go b/build/soong/android/variable.go
index e40b5ac..95df5d1 100644
--- a/build/soong/android/variable.go
+++ b/build/soong/android/variable.go
@@ -50,6 +50,15 @@
 	Target_shim_libs struct {
 		Cppflags []string
 	}
+	
+	Uses_qti_camera_device struct {
+		Cppflags []string
+		Shared_libs []string
+	}
+	
+	Uses_nvidia_enhancements struct {
+		Cppflags []string
+	}
 }
 
 type ProductVariables struct {
@@ -69,4 +78,6 @@
 	Target_uses_qsml      *bool `json:",omitempty"`
 	Target_uses_eigen       *bool `json:",omitempty"`
 	Target_shim_libs  *string `json:",omitempty"`
+	Uses_qti_camera_device  *bool `json:",omitempty"`
+	Uses_nvidia_enhancements  *bool `json:",omitempty"`
 }
diff --git a/build/soong/soong_config.mk b/build/soong/soong_config.mk
index 8846562..d4c1236 100644
--- a/build/soong/soong_config.mk
+++ b/build/soong/soong_config.mk
@@ -21,6 +21,8 @@
 	echo '    "TargetUsesProprietaryLibs":  $(if $(strip $(TARGET_USES_PROPRIETARY_LIBS)),true,false),';  \
 	echo '    "Target_uses_qsml":  $(if $(strip $(TARGET_USES_QSML)),true,false),';  \
 	echo '    "Target_uses_eigen":  $(if $(strip $(TARGET_USES_QSML)),false,true),';  \
+	echo '    "Uses_qti_camera_device": $(if $(filter true,$(TARGET_USES_QTI_CAMERA_DEVICE)),true,false),'; \
+	echo '    "Uses_nvidia_enhancements": $(if $(filter TRUE,$(NV_ANDROID_FRAMEWORK_ENHANCEMENTS)),true,false),'; \
 	echo '    "Target_shim_libs": "$(subst $(space),:,$(TARGET_LD_SHIM_LIBS))"'; \
 	echo '},'; \
 	echo '') > $(SOONG_VARIABLES_TMP)
diff --git a/build/tasks/dt_image.mk b/build/tasks/dt_image.mk
index 5390488..ddd4c98 100644
--- a/build/tasks/dt_image.mk
+++ b/build/tasks/dt_image.mk
@@ -8,7 +8,7 @@
 ifeq ($(strip $(BOARD_KERNEL_PREBUILT_DT)),)
 
 ifeq ($(strip $(TARGET_CUSTOM_DTBTOOL)),)
-DTBTOOL_NAME := dtbToolCM
+DTBTOOL_NAME := dtbToolLineage
 else
 DTBTOOL_NAME := $(TARGET_CUSTOM_DTBTOOL)
 endif
diff --git a/build/tasks/kernel.mk b/build/tasks/kernel.mk
index cc3572d..bdc5a3b 100644
--- a/build/tasks/kernel.mk
+++ b/build/tasks/kernel.mk
@@ -238,8 +238,8 @@
         # Find the clang-* directory containing the specified version
         KERNEL_CLANG_VERSION := $(shell find $(ANDROID_BUILD_TOP)/prebuilts/clang/host/$(HOST_OS)-x86/ -name AndroidVersion.txt -exec grep -l $(TARGET_KERNEL_CLANG_VERSION) "{}" \; | sed -e 's|/AndroidVersion.txt$$||g;s|^.*/||g')
     else
-        # Only set the latest version of clang if TARGET_KERNEL_CLANG_VERSION hasn't been set by the device config
-        KERNEL_CLANG_VERSION := $(shell ls -d $(ANDROID_BUILD_TOP)/prebuilts/clang/host/$(HOST_OS)-x86/clang-* | xargs -n 1 basename | tail -1)
+        # Use the default version of clang if TARGET_KERNEL_CLANG_VERSION hasn't been set by the device config
+        KERNEL_CLANG_VERSION := $(LLVM_PREBUILTS_VERSION)
     endif
     TARGET_KERNEL_CLANG_PATH ?= $(ANDROID_BUILD_TOP)/prebuilts/clang/host/$(HOST_OS)-x86/$(KERNEL_CLANG_VERSION)/bin
     ifeq ($(KERNEL_ARCH),arm64)
diff --git a/charger/Android.mk b/charger/Android.mk
index 78a3dcd..6ec2330 100644
--- a/charger/Android.mk
+++ b/charger/Android.mk
@@ -31,9 +31,6 @@
 ifneq ($(HEALTHD_BACKLIGHT_LEVEL),)
     LOCAL_CFLAGS += -DHEALTHD_BACKLIGHT_LEVEL=$(HEALTHD_BACKLIGHT_LEVEL)
 endif
-LOCAL_STATIC_LIBRARIES := \
-    libbase \
-    libminui
 include $(BUILD_STATIC_LIBRARY)
 
 include $(CLEAR_VARS)
diff --git a/charger/healthd_board_bliss.cpp b/charger/healthd_board_bliss.cpp
index 07e10e7..c2f42ea 100644
--- a/charger/healthd_board_bliss.cpp
+++ b/charger/healthd_board_bliss.cpp
@@ -416,4 +416,3 @@
     }
 
 }
-
diff --git a/charger/images/hdpi/bliss_battery_scale.png b/charger/images/hdpi/bliss_battery_scale.png
new file mode 100644
index 0000000..0052c0a
--- /dev/null
+++ b/charger/images/hdpi/bliss_battery_scale.png
Binary files differ
diff --git a/charger/images/ldpi/bliss_battery_scale.png b/charger/images/ldpi/bliss_battery_scale.png
new file mode 100644
index 0000000..b93c3af
--- /dev/null
+++ b/charger/images/ldpi/bliss_battery_scale.png
Binary files differ
diff --git a/charger/images/mdpi/bliss_battery_scale.png b/charger/images/mdpi/bliss_battery_scale.png
new file mode 100644
index 0000000..eb16a29
--- /dev/null
+++ b/charger/images/mdpi/bliss_battery_scale.png
Binary files differ
diff --git a/charger/images/xhdpi/bliss_battery_scale.png b/charger/images/xhdpi/bliss_battery_scale.png
new file mode 100644
index 0000000..078b2a6
--- /dev/null
+++ b/charger/images/xhdpi/bliss_battery_scale.png
Binary files differ
diff --git a/charger/images/xxhdpi/bliss_battery_scale.png b/charger/images/xxhdpi/bliss_battery_scale.png
new file mode 100644
index 0000000..2e6553e
--- /dev/null
+++ b/charger/images/xxhdpi/bliss_battery_scale.png
Binary files differ
diff --git a/charger/images/xxxhdpi/bliss_battery_scale.png b/charger/images/xxxhdpi/bliss_battery_scale.png
new file mode 100644
index 0000000..90c627d
--- /dev/null
+++ b/charger/images/xxxhdpi/bliss_battery_scale.png
Binary files differ
diff --git a/config/BoardConfigBliss.mk b/config/BoardConfigBliss.mk
index 7ed2c7b..59601c7 100644
--- a/config/BoardConfigBliss.mk
+++ b/config/BoardConfigBliss.mk
@@ -1,4 +1,9 @@
 # Charger
-ifneq ($(WITH_BLISS_CHARGER),false)
+ifeq ($(WITH_BLISS_CHARGER),true)
     BOARD_HAL_STATIC_LIBRARIES := libhealthd.bliss
 endif
+
+# QCOM HW crypto
+ifeq ($(TARGET_HW_DISK_ENCRYPTION),true)
+    TARGET_CRYPTFS_HW_PATH ?= vendor/qcom/opensource/cryptfs_hw
+endif
diff --git a/config/common.mk b/config/common.mk
index 4a86c1e..0d904c3 100644
--- a/config/common.mk
+++ b/config/common.mk
@@ -91,10 +91,13 @@
 PRODUCT_DEFAULT_PROPERTY_OVERRIDES += ro.adb.secure=1
 endif
 
-# Custom off-mode charger
-ifneq ($(WITH_BLISS_CHARGER),false)
+# Charger
 PRODUCT_PACKAGES += \
-    charger_res_images \
+    charger_res_images
+
+# Custom off-mode charger
+ifeq ($(WITH_BLISS_CHARGER),true)
+PRODUCT_PACKAGES += \
     bliss_charger_res_images \
     font_log.png \
     libhealthd.bliss
diff --git a/overlay/common/frameworks/base/core/res/res/drawable-hdpi/default_wallpaper.png b/overlay/common/frameworks/base/core/res/res/drawable-hdpi/default_wallpaper.png
new file mode 100644
index 0000000..75609bb
--- /dev/null
+++ b/overlay/common/frameworks/base/core/res/res/drawable-hdpi/default_wallpaper.png
Binary files differ
diff --git a/overlay/common/frameworks/base/core/res/res/drawable-mdpi/default_wallpaper.png b/overlay/common/frameworks/base/core/res/res/drawable-mdpi/default_wallpaper.png
new file mode 100644
index 0000000..c164cf3
--- /dev/null
+++ b/overlay/common/frameworks/base/core/res/res/drawable-mdpi/default_wallpaper.png
Binary files differ
diff --git a/overlay/common/frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.png b/overlay/common/frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.png
new file mode 100644
index 0000000..71eedb4
--- /dev/null
+++ b/overlay/common/frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.png
Binary files differ
diff --git a/overlay/common/frameworks/base/core/res/res/drawable-sw600dp-nodpi/default_wallpaper.png b/overlay/common/frameworks/base/core/res/res/drawable-sw600dp-nodpi/default_wallpaper.png
new file mode 100644
index 0000000..d34738a
--- /dev/null
+++ b/overlay/common/frameworks/base/core/res/res/drawable-sw600dp-nodpi/default_wallpaper.png
Binary files differ
diff --git a/overlay/common/frameworks/base/core/res/res/drawable-sw720dp-nodpi/default_wallpaper.png b/overlay/common/frameworks/base/core/res/res/drawable-sw720dp-nodpi/default_wallpaper.png
new file mode 100644
index 0000000..3079b4d
--- /dev/null
+++ b/overlay/common/frameworks/base/core/res/res/drawable-sw720dp-nodpi/default_wallpaper.png
Binary files differ
diff --git a/overlay/common/frameworks/base/core/res/res/drawable-xhdpi/default_wallpaper.png b/overlay/common/frameworks/base/core/res/res/drawable-xhdpi/default_wallpaper.png
new file mode 100644
index 0000000..99cc419
--- /dev/null
+++ b/overlay/common/frameworks/base/core/res/res/drawable-xhdpi/default_wallpaper.png
Binary files differ
diff --git a/overlay/common/frameworks/base/core/res/res/drawable-xxhdpi/default_wallpaper.png b/overlay/common/frameworks/base/core/res/res/drawable-xxhdpi/default_wallpaper.png
new file mode 100644
index 0000000..3d31e3a
--- /dev/null
+++ b/overlay/common/frameworks/base/core/res/res/drawable-xxhdpi/default_wallpaper.png
Binary files differ
diff --git a/overlay/common/frameworks/base/core/res/res/drawable-xxxhdpi/default_wallpaper.png b/overlay/common/frameworks/base/core/res/res/drawable-xxxhdpi/default_wallpaper.png
new file mode 100644
index 0000000..c8ad50b
--- /dev/null
+++ b/overlay/common/frameworks/base/core/res/res/drawable-xxxhdpi/default_wallpaper.png
Binary files differ
diff --git a/qcom/dtbtool/Android.mk b/qcom/dtbtool/Android.mk
new file mode 100644
index 0000000..604b7bc
--- /dev/null
+++ b/qcom/dtbtool/Android.mk
@@ -0,0 +1,17 @@
+ifeq ($(BOARD_KERNEL_SEPARATED_DT),true)
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
+	dtbtool.c
+
+LOCAL_CFLAGS += \
+	-Wall
+
+## Hybrid v1/v2 dtbTool. Use a different name to avoid conflicts with copies in device repos
+LOCAL_MODULE := dtbToolLineage
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_HOST_EXECUTABLE)
+endif
diff --git a/qcom/dtbtool/dtbtool.c b/qcom/dtbtool/dtbtool.c
new file mode 100644
index 0000000..08a1b8a
--- /dev/null
+++ b/qcom/dtbtool/dtbtool.c
@@ -0,0 +1,1054 @@
+/*
+ * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+       * Redistributions of source code must retain the above copyright
+         notice, this list of conditions and the following disclaimer.
+       * Redistributions in binary form must reproduce the above
+         copyright notice, this list of conditions and the following
+         disclaimer in the documentation and/or other materials provided
+         with the distribution.
+       * Neither the name of The Linux Foundation nor the names of its
+         contributors may be used to endorse or promote products derived
+         from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define _GNU_SOURCE
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <errno.h>
+#include <unistd.h>
+#include <limits.h>
+
+#define QCDT_MAGIC     "QCDT"  /* Master DTB magic */
+#define QCDT_VERSION   3       /* QCDT version */
+
+#define QCDT_DT_TAG    "qcom,msm-id = <"
+#define QCDT_BOARD_TAG "qcom,board-id = <"
+#define QCDT_PMIC_TAG  "qcom,pmic-id = <"
+
+
+#define PAGE_SIZE_DEF  2048
+#define PAGE_SIZE_MAX  (1024*1024)
+
+#define log_err(x...)  printf(x)
+#define log_info(x...) printf(x)
+#define log_dbg(x...)  { if (verbose) printf(x); }
+
+#define COPY_BLK       1024    /* File copy block size */
+
+#define RC_SUCCESS     0
+#define RC_ERROR       -1
+
+struct chipInfo_t {
+  uint32_t chipset;
+  uint32_t platform;
+  uint32_t subtype;
+  uint32_t revNum;
+  uint32_t pmic_model[4];
+  uint32_t dtb_size;
+  char     *dtb_file;
+  struct chipInfo_t *prev;
+  struct chipInfo_t *next;
+  struct chipInfo_t *master;
+  int      wroteDtb;
+  uint32_t master_offset;
+  struct chipInfo_t *t_next;
+};
+
+struct chipInfo_t *chip_list;
+
+struct chipId_t {
+  uint32_t chipset;
+  uint32_t revNum;
+  struct chipId_t *next;
+  struct chipId_t *t_next;
+};
+
+struct chipSt_t {
+  uint32_t platform;
+  uint32_t subtype;
+  struct chipSt_t *next;
+  struct chipSt_t *t_next;
+};
+
+struct chipPt_t {
+  uint32_t pmic0;
+  uint32_t pmic1;
+  uint32_t pmic2;
+  uint32_t pmic3;
+  struct chipPt_t *next;
+  struct chipPt_t *t_next;
+};
+
+char *input_dir;
+char *output_file;
+char *dtc_path;
+char *dt_tag = QCDT_DT_TAG;
+int   verbose;
+int   page_size = PAGE_SIZE_DEF;
+int   version_override = 0;
+
+void print_help()
+{
+    log_info("dtbTool version %d (kinda :) )\n", QCDT_VERSION);
+    log_info("dtbTool [options] -o <output file> <input DTB path>\n");
+    log_info("  options:\n");
+    log_info("  --output-file/-o     output file\n");
+    log_info("  --dtc-path/-p        path to dtc\n");
+    log_info("  --page-size/-s       page size in bytes\n");
+    log_info("  --dt-tag/-d          alternate QCDT_DT_TAG\n");
+    log_info("  --verbose/-v         verbose\n");
+    log_info("  --force-v2/-2        output dtb v2 format\n");
+    log_info("  --force-v3/-3        output dtb v3 format\n");
+    log_info("  --help/-h            this help screen\n");
+}
+
+int parse_commandline(int argc, char *const argv[])
+{
+    int c;
+
+    struct option long_options[] = {
+        {"output-file", 1, 0, 'o'},
+        {"dtc-path",    1, 0, 'p'},
+        {"page-size",   1, 0, 's'},
+        {"dt-tag",      1, 0, 'd'},
+        {"force-v2",    0, 0, '2'},
+        {"force-v3",    0, 0, '3'},
+        {"verbose",     0, 0, 'v'},
+        {"help",        0, 0, 'h'},
+        {0, 0, 0, 0}
+    };
+
+    while ((c = getopt_long(argc, argv, "-o:p:s:d:23vh", long_options, NULL))
+           != -1) {
+        switch (c) {
+        case 1:
+            if (!input_dir)
+                input_dir = optarg;
+            break;
+        case 'o':
+            output_file = optarg;
+            break;
+        case 'p':
+            dtc_path = optarg;
+            break;
+        case 's':
+            page_size = atoi(optarg);
+            if ((page_size <= 0) || (page_size > (PAGE_SIZE_MAX))) {
+                log_err("Invalid page size (> 0 and <=1MB\n");
+                return RC_ERROR;
+            }
+            break;
+        case 'd':
+            dt_tag = optarg;
+            break;
+        case '2':
+        case '3':
+            if (version_override != 0) {
+                log_err("A version output argument may only be passed once\n");
+                return RC_ERROR;
+            }
+            version_override = c - '0';
+            break;
+        case 'v':
+            verbose = 1;
+            break;
+        case 'h':
+        default:
+            return RC_ERROR;
+        }
+    }
+
+    if (!output_file) {
+        log_err("Output file must be specified\n");
+        return RC_ERROR;
+    }
+
+    if (!input_dir)
+        input_dir = "./";
+
+    if (!dtc_path)
+        dtc_path = "";
+
+    return RC_SUCCESS;
+}
+
+/* Unique entry sorted list add (by chipset->platform->rev) */
+int chip_add(struct chipInfo_t *c)
+{
+    struct chipInfo_t *x = chip_list;
+
+    if (!chip_list) {
+        chip_list = c;
+        c->next = NULL;
+        c->prev = NULL;
+        return RC_SUCCESS;
+    }
+
+    while (1) {
+        if ((c->chipset < x->chipset) ||
+            ((c->chipset == x->chipset) &&
+             ((c->platform < x->platform) ||
+              ((c->platform == x->platform) &&
+               ((c->subtype < x->subtype) ||
+                ((c->subtype == x->subtype) &&
+                 (c->revNum < x->revNum))))))) {
+            if (!x->prev) {
+                c->next = x;
+                c->prev = NULL;
+                x->prev = c;
+                chip_list = c;
+                break;
+            } else {
+                c->next = x;
+                c->prev = x->prev;
+                x->prev->next = c;
+                x->prev = c;
+                break;
+            }
+        }
+        if ((c->chipset == x->chipset) &&
+            (c->platform == x->platform) &&
+            (c->subtype == x->subtype) &&
+            (c->revNum == x->revNum) &&
+            (c->pmic_model[0] == x->pmic_model[0]) &&
+            (c->pmic_model[1] == x->pmic_model[1]) &&
+            (c->pmic_model[2] == x->pmic_model[2]) &&
+            (c->pmic_model[3] == x->pmic_model[3])) {
+            return RC_ERROR;  /* duplicate */
+        }
+        if (!x->next) {
+            c->prev = x;
+            c->next = NULL;
+            x->next = c;
+            break;
+        }
+        x = x->next;
+    }
+    return RC_SUCCESS;
+}
+
+void chip_deleteall()
+{
+    struct chipInfo_t *c = chip_list, *t;
+
+    while (c) {
+        t = c;
+        c = c->next;
+        if (t->dtb_file)
+            free(t->dtb_file);
+        free(t);
+    }
+}
+
+/*
+  For v1 Extract 'qcom,msm-id' parameter triplet from DTB
+      qcom,msm-id = <x y z>;
+
+  For v2 Extract 'qcom,msm-id', 'qcom,board-id' parameter double from DTB
+      qcom,msm-id = <x z> i.e chipset, revision number;
+      qcom,board-id = <y y'> i.e platform and sub-type;
+ */
+
+struct chipInfo_t *getChipInfo(const char *filename, int *num, uint32_t msmversion)
+{
+
+    const char str1[] = "dtc -I dtb -O dts \"";
+    const char str2[] = "\" 2>&1";
+    char *buf, *pos;
+    char *line = NULL;
+    size_t line_size;
+    FILE *pfile;
+    int llen;
+    struct chipInfo_t *chip = NULL, *tmp, *chip_t;
+    uint32_t data[3] = {0, 0, 0};
+    uint32_t data_st[2] = {0, 0};
+    uint32_t data_pt[4] = {0, 0, 0, 0};
+    char *tok, *sptr = NULL;
+    int i, entryValid, entryEnded;
+    int count = 0, count1 = 0, count2 = 0, count3 = 0;
+    int entryValidST, entryEndedST, entryValidDT, entryEndedDT, entryValidPT, entryEndedPT;
+    struct chipId_t *chipId = NULL, *cId = NULL, *tmp_id = NULL;
+    struct chipSt_t *chipSt = NULL, *cSt = NULL, *tmp_st = NULL;
+    struct chipPt_t *chipPt = NULL, *cPt = NULL, *tmp_pt = NULL;
+    struct chipId_t *chipId_tmp = NULL;
+    struct chipSt_t *chipSt_tmp = NULL;
+    struct chipPt_t *chipPt_tmp = NULL;
+
+    line_size = 1024;
+    line = (char *)malloc(line_size);
+    if (!line) {
+        log_err("Out of memory\n");
+        return NULL;
+    }
+
+    llen = sizeof(char) * (strlen(dtc_path) +
+                           strlen(str1) +
+                           strlen(str2) +
+                           strlen(filename) + 1);
+    buf = (char *)malloc(llen);
+    if (!buf) {
+        log_err("Out of memory\n");
+        free(line);
+        return NULL;
+    }
+
+    strncpy(buf, dtc_path, llen);
+    strncat(buf, str1, llen);
+    strncat(buf, filename, llen);
+    strncat(buf, str2, llen);
+
+    pfile = popen(buf, "r");
+    free(buf);
+
+    if (pfile == NULL) {
+        log_err("... skip, fail to decompile dtb\n");
+    } else {
+        /* Find "qcom,msm-id" */
+        while ((llen = getline(&line, &line_size, pfile)) != -1) {
+            if (msmversion == 1) {
+                if ((pos = strstr(line, dt_tag)) != NULL) {
+                    pos += strlen(dt_tag);
+
+                    entryEnded = 0;
+                    while (1) {
+                        entryValid = 1;
+                        for (i = 0; i < 3; i++) {
+                            tok = strtok_r(pos, " \t", &sptr);
+                            pos = NULL;
+                            if (tok != NULL) {
+                                if (*tok == '>') {
+                                    entryEnded = 1;
+                                    entryValid = 0;
+                                    break;
+                                }
+                                data[i] = strtoul(tok, NULL, 0);
+                            } else {
+                                data[i] = 0;
+                                entryValid = 0;
+                                entryEnded = 1;
+                            }
+                        }
+                        if (entryEnded) {
+                            free(line);
+                            pclose(pfile);
+                            *num = count;
+                            return chip;
+                        }
+                        if (entryValid) {
+                            tmp = (struct chipInfo_t *)
+                                      malloc(sizeof(struct chipInfo_t));
+                            if (!tmp) {
+                                log_err("Out of memory\n");
+                                break;
+                            }
+                            if (!chip) {
+                                chip = tmp;
+                                chip->t_next = NULL;
+                            } else {
+                                tmp->t_next = chip->t_next;
+                                chip->t_next = tmp;
+                            }
+                            tmp->chipset  = data[0];
+                            tmp->platform = data[1];
+                            tmp->subtype  = 0;
+                            tmp->revNum   = data[2];
+                            tmp->pmic_model[0] = 0;
+                            tmp->pmic_model[1] = 0;
+                            tmp->pmic_model[2] = 0;
+                            tmp->pmic_model[3] = 0;
+                            tmp->dtb_size = 0;
+                            tmp->dtb_file = NULL;
+                            tmp->master   = chip;
+                            tmp->wroteDtb = 0;
+                            tmp->master_offset = 0;
+                            count++;
+                        }
+                    }
+
+                    log_err("... skip, incorrect '%s' format\n", dt_tag);
+                    break;
+                }
+            } else if (msmversion == 2 || msmversion == 3) {
+                if ((pos = strstr(line, dt_tag)) != NULL) {
+                    pos += strlen(dt_tag);
+
+                    entryEndedDT = 0;
+                    for (;entryEndedDT < 1;) {
+                        entryValidDT = 1;
+                        for (i = 0; i < 2; i++) {
+                            tok = strtok_r(pos, " \t", &sptr);
+                            pos = NULL;
+                            if (tok != NULL) {
+                                if (*tok == '>') {
+                                    entryEndedDT = 1;
+                                    entryValidDT = 0;
+                                    break;
+                                }
+                                data_st[i] = strtoul(tok, NULL, 0);
+                            } else {
+                                data_st[i] = 0;
+                                entryValidDT = 0;
+                                entryEndedDT = 1;
+                            }
+                        }
+
+                        if (entryValidDT) {
+                            tmp_id = (struct chipId_t *)
+                                         malloc(sizeof(struct chipId_t));
+                            if (!tmp_id) {
+                                log_err("Out of memory\n");
+                                break;
+                            }
+                            if (!chipId) {
+                                chipId = tmp_id;
+                                cId = tmp_id;
+                                chipId->t_next = NULL;
+                            } else {
+                                tmp_id->t_next = chipId->t_next;
+                                chipId->t_next = tmp_id;
+                            }
+                            tmp_id->chipset = data_st[0];
+                            tmp_id->revNum= data_st[1];
+                            count1++;
+                        }
+                    }
+                }
+
+                if ((pos = strstr(line,QCDT_BOARD_TAG)) != NULL) {
+                    pos += strlen(QCDT_BOARD_TAG);
+                    entryEndedST = 0;
+                    for (;entryEndedST < 1;) {
+                        entryValidST = 1;
+                        for (i = 0; i < 2; i++) {
+                            tok = strtok_r(pos, " \t", &sptr);
+                            pos = NULL;
+                            if (tok != NULL) {
+                                if (*tok == '>') {
+                                    entryEndedST = 1;
+                                    entryValidST = 0;
+                                    break;
+                                }
+                                data_st[i] = strtoul(tok, NULL, 0);
+                            } else {
+                                data_st[i] = 0;
+                                entryValidST = 0;
+                                entryEndedST = 1;
+                            }
+                        }
+                        if (entryValidST) {
+                            tmp_st = (struct chipSt_t *)
+                                       malloc(sizeof(struct chipSt_t));
+                            if (!tmp_st) {
+                                log_err("Out of memory\n");
+                                break;
+                            }
+
+                            if (!chipSt) {
+                                chipSt = tmp_st;
+                                cSt = tmp_st;
+                                chipSt->t_next = NULL;
+                            } else {
+                                tmp_st->t_next = chipSt->t_next;
+                                chipSt->t_next = tmp_st;
+                            }
+
+                            tmp_st->platform = data_st[0];
+                            tmp_st->subtype= data_st[1];
+                            count2++;
+                        }
+                    }
+                }
+
+                if ((pos = strstr(line,QCDT_PMIC_TAG)) != NULL) {
+                    pos += strlen(QCDT_PMIC_TAG);
+                    entryEndedPT = 0;
+                    for (;entryEndedPT < 1;) {
+                        entryValidPT = 1;
+                        for (i = 0; i < 4; i++) {
+                            tok = strtok_r(pos, " \t", &sptr);
+                            pos = NULL;
+                            if (tok != NULL) {
+                                if (*tok == '>') {
+                                    entryEndedPT = 1;
+                                    entryValidPT = 0;
+                                    break;
+                                }
+                                data_pt[i] = strtoul(tok, NULL, 0);
+                            } else {
+                                data_pt[i] = 0;
+                                entryValidPT = 0;
+                                entryEndedPT = 1;
+                            }
+                        }
+                        if (entryValidPT) {
+                            tmp_pt = (struct chipPt_t *)
+                                       malloc(sizeof(struct chipPt_t));
+                            if (!tmp_pt) {
+                                log_err("Out of memory\n");
+                                break;
+                            }
+
+                            if (!chipPt) {
+                                chipPt = tmp_pt;
+                                cPt = tmp_pt;
+                                chipPt->t_next = NULL;
+                            } else {
+                                tmp_pt->t_next = chipPt->t_next;
+                                chipPt->t_next = tmp_pt;
+                            }
+
+                            tmp_pt->pmic0 = data_pt[0];
+                            tmp_pt->pmic1 = data_pt[1];
+                            tmp_pt->pmic2 = data_pt[2];
+                            tmp_pt->pmic3 = data_pt[3];
+                            count3++;
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    if (line)
+        free(line);
+
+    if (count1 == 0) {
+        log_err("... skip, incorrect '%s' format\n", dt_tag);
+        return NULL;
+    }
+    if (count2 == 0) {
+        log_err("... skip, incorrect '%s' format\n", QCDT_BOARD_TAG);
+        return NULL;
+    }
+    if (count3 == 0 && msmversion == 3) {
+        log_err("... skip, incorrect '%s' format\n", QCDT_PMIC_TAG);
+        return NULL;
+    }
+
+    tmp_st = cSt;
+    tmp_pt = cPt;
+    while (cId != NULL) {
+        while (cSt != NULL) {
+            if (msmversion == 3) {
+                while (cPt != NULL) {
+                    tmp = (struct chipInfo_t *)
+                        malloc(sizeof(struct chipInfo_t));
+                    if (!tmp) {
+                        log_err("Out of memory\n");
+                        break;
+                    }
+                    if (!chip) {
+                        chip = tmp;
+                        chip->t_next = NULL;
+                    } else {
+                        tmp->t_next = chip->t_next;
+                        chip->t_next = tmp;
+                    }
+
+                    tmp->chipset  = cId->chipset;
+                    tmp->platform = cSt->platform;
+                    tmp->revNum   = cId->revNum;
+                    tmp->subtype  = cSt->subtype;
+                    tmp->pmic_model[0] = cPt->pmic0;
+                    tmp->pmic_model[1] = cPt->pmic1;
+                    tmp->pmic_model[2] = cPt->pmic2;
+                    tmp->pmic_model[3] = cPt->pmic3;
+                    tmp->dtb_size = 0;
+                    tmp->dtb_file = NULL;
+                    tmp->master   = chip;
+                    tmp->wroteDtb = 0;
+                    tmp->master_offset = 0;
+                    cPt = cPt->t_next;
+                }
+                cPt = tmp_pt;
+            } else {
+                tmp = (struct chipInfo_t *)
+                    malloc(sizeof(struct chipInfo_t));
+                if (!tmp) {
+                    log_err("Out of memory\n");
+                    break;
+                }
+                if (!chip) {
+                    chip = tmp;
+                    chip->t_next = NULL;
+                } else {
+                    tmp->t_next = chip->t_next;
+                    chip->t_next = tmp;
+                }
+                tmp->chipset  = cId->chipset;
+                tmp->platform = cSt->platform;
+                tmp->revNum   = cId->revNum;
+                tmp->subtype  = cSt->subtype;
+                tmp->pmic_model[0] = 0;
+                tmp->pmic_model[1] = 0;
+                tmp->pmic_model[2] = 0;
+                tmp->pmic_model[3] = 0;
+                tmp->dtb_size = 0;
+                tmp->dtb_file = NULL;
+                tmp->master   = chip;
+                tmp->wroteDtb = 0;
+                tmp->master_offset = 0;
+            }
+            cSt = cSt->t_next;
+        }
+        cSt = tmp_st;
+        cId = cId->t_next;
+    }
+
+    if (msmversion == 2)
+        entryEndedPT = 1;
+
+    /* clear memory*/
+    pclose(pfile);
+    while (chipId) {
+        chipId_tmp = chipId;
+        chipId = chipId->t_next;
+        free(chipId_tmp);
+    }
+    while (chipSt) {
+        chipSt_tmp= chipSt;
+        chipSt = chipSt->t_next;
+        free(chipSt_tmp);
+    }
+
+    while (chipPt) {
+        chipPt_tmp= chipPt;
+        chipPt = chipPt->t_next;
+        free(chipPt_tmp);
+    }
+
+    if (entryEndedST  == 1 && entryEndedDT == 1 && entryEndedPT == 1) {
+        *num = count1;
+        return chip;
+    }
+
+    /* clear memory*/
+    while (chip) {
+        chip_t = chip;
+        chip = chip->next;
+        if (chip_t->dtb_file)
+            free(chip_t->dtb_file);
+        free(chip_t);
+    }
+    return NULL;
+}
+
+/* Get the version-id based on dtb files */
+uint32_t GetVersionInfo(const char *filename)
+{
+    const char str1[] = "dtc -I dtb -O dts \"";
+    const char str2[] = "\" 2>&1";
+    char *buf, *pos;
+    char *line = NULL;
+    size_t line_size;
+    FILE *pfile;
+    int llen;
+    uint32_t v = 1;
+
+    line_size = 1024;
+    line = (char *)malloc(line_size);
+    if (!line) {
+        log_err("Out of memory\n");
+        return 0;
+    }
+
+    llen = sizeof(char) * (strlen(dtc_path) +
+                           strlen(str1) +
+                           strlen(str2) +
+                           strlen(filename) + 1);
+    buf = (char *)malloc(llen);
+    if (!buf) {
+        log_err("Out of memory\n");
+        free(line);
+        return 0;
+    }
+
+    strncpy(buf, dtc_path, llen);
+    strncat(buf, str1, llen);
+    strncat(buf, filename, llen);
+    strncat(buf, str2, llen);
+
+    pfile = popen(buf, "r");
+    free(buf);
+
+    if (pfile == NULL) {
+        log_err("... skip, fail to decompile dtb\n");
+    } else {
+        /* Find the type of version */
+        while ((llen = getline(&line, &line_size, pfile)) != -1) {
+            if ((pos = strstr(line,QCDT_BOARD_TAG)) != NULL) {
+                v = 2;
+            }
+            if ((pos = strstr(line,QCDT_PMIC_TAG)) != NULL) {
+                v = 3;
+                break;
+            }
+        }
+    }
+
+    free(line);
+    log_info("Version:%d\n", v);
+
+    return v;
+}
+
+static int find_dtb(const char *path, uint32_t *version)
+{
+    struct dirent *dp;
+    int flen;
+    char *filename;
+    struct chipInfo_t *chip, *t_chip;
+    struct stat st;
+    int num;
+    int rc = RC_SUCCESS;
+    uint32_t msmversion = 0;
+    int dtb_count = 0;
+
+    DIR *dir = opendir(path);
+    if (!dir) {
+        log_err("Failed to open input directory '%s'\n", path);
+        return RC_ERROR;
+    }
+
+    /* Open the .dtb files in the specified path, decompile and
+       extract "qcom,msm-id" parameter
+     */
+    while ((dp = readdir(dir)) != NULL) {
+        if (dp->d_type == DT_UNKNOWN) {
+            struct stat statbuf;
+            char name[PATH_MAX];
+            snprintf(name, sizeof(name), "%s%s%s",
+                     path,
+                     (path[strlen(path) - 1] == '/' ? "" : "/"),
+                     dp->d_name);
+            if (!stat(name, &statbuf)) {
+                if (S_ISREG(statbuf.st_mode)) {
+                    dp->d_type = DT_REG;
+                } else if (S_ISDIR(statbuf.st_mode)) {
+                    dp->d_type = DT_DIR;
+                }
+            }
+        }
+
+        if (dp->d_type == DT_DIR) {
+            char name[PATH_MAX];
+            if (dp->d_name[0] == '.') {
+                continue;
+            }
+            snprintf(name, sizeof(name), "%s%s%s%s",
+                     path,
+                     (path[strlen(path) - 1] == '/' ? "" : "/"),
+                     dp->d_name,
+                     "/");
+            log_info("Searching subdir: %s ... \n", name);
+            dtb_count += find_dtb(name, version);
+        } else if (dp->d_type == DT_REG) {
+            flen = strlen(dp->d_name);
+            if ((flen > 4) &&
+                (strncmp(&dp->d_name[flen-4], ".dtb", 4) == 0)) {
+                log_info("Found file: %s ... \n", dp->d_name);
+
+                flen = strlen(path) + strlen(dp->d_name) + 1;
+                filename = (char *)malloc(flen);
+                if (!filename) {
+                    log_err("Out of memory\n");
+                    rc = RC_ERROR;
+                    break;
+                }
+                strncpy(filename, path, flen);
+                strncat(filename, dp->d_name, flen);
+
+                /* To identify the version number */
+                msmversion = GetVersionInfo(filename);
+                if (*version < msmversion) {
+                    *version = msmversion;
+                }
+
+                num = 1;
+                chip = getChipInfo(filename, &num, msmversion);
+
+                if (msmversion == 1) {
+                    if (!chip) {
+                        log_err("skip, failed to scan for '%s' tag\n", dt_tag);
+                        free(filename);
+                        continue;
+                    }
+                }
+                if (msmversion == 2) {
+                    if (!chip) {
+                        log_err("skip, failed to scan for '%s' or '%s' tag\n",
+                                dt_tag, QCDT_BOARD_TAG);
+                        free(filename);
+                        continue;
+                    }
+                }
+                if (msmversion == 3) {
+                    if (!chip) {
+                        log_err("skip, failed to scan for '%s', '%s' or '%s' tag\n",
+                                dt_tag, QCDT_BOARD_TAG, QCDT_PMIC_TAG);
+                        free(filename);
+                        continue;
+                    }
+                }
+
+                if ((stat(filename, &st) != 0) ||
+                    (st.st_size == 0)) {
+                    log_err("skip, failed to get DTB size\n");
+                    free(filename);
+                    continue;
+                }
+
+                log_info("chipset: %u, rev: %u, platform: %u, subtype: %u, pmic0: %u, pmic1: %u, pmic2: %u, pmic3: %u\n",
+                         chip->chipset, chip->revNum, chip->platform, chip->subtype,
+                         chip->pmic_model[0], chip->pmic_model[1], chip->pmic_model[2], chip->pmic_model[3]);
+
+                for (t_chip = chip->t_next; t_chip; t_chip = t_chip->t_next) {
+                    log_info("additional chipset: %u, rev: %u, platform: %u, subtype: %u, pmic0: %u, pmic1: %u, pmic2: %u, pmic3: %u\n",
+                             t_chip->chipset, t_chip->revNum, t_chip->platform, t_chip->subtype,
+                             t_chip->pmic_model[0], t_chip->pmic_model[1], t_chip->pmic_model[2], t_chip->pmic_model[3]);
+                }
+
+                rc = chip_add(chip);
+                if (rc != RC_SUCCESS) {
+                    log_err("... duplicate info, skipped\n");
+                    free(filename);
+                    continue;
+                }
+
+                dtb_count++;
+
+                chip->dtb_size = st.st_size +
+                                   (page_size - (st.st_size % page_size));
+                chip->dtb_file = filename;
+
+                for (t_chip = chip->t_next; t_chip; t_chip = t_chip->t_next) {
+                    rc = chip_add(t_chip);
+                    if (rc != RC_SUCCESS) {
+                        log_err("... duplicate info, skipped (chipset %u, rev: %u, platform: %u, subtype: %u\n",
+                             t_chip->chipset, t_chip->revNum, t_chip->platform, t_chip->subtype);
+                        continue;
+                    }
+                    dtb_count++;
+                }
+            }
+        }
+    }
+    closedir(dir);
+    return dtb_count;
+}
+
+/* Extract 'qcom,msm-id' 'qcom,board-id' parameter from DTB
+   v1 format:
+      qcom,msm-id = <x y z> [, <x2 y2 z2> ...];
+   v2 format:
+      qcom,msm-id = <x z> [, <x2 z2> ...;
+      qcom,board-id = <y y'> [, <y2 y2'> ...;
+   Fields:
+      x  = chipset
+      y  = platform
+      y' = subtype
+      z  = soc rev
+ */
+int main(int argc, char **argv)
+{
+    char buf[COPY_BLK];
+    struct chipInfo_t *chip;
+    FILE *pInputFile;
+    int padding;
+    uint8_t *filler = NULL;
+    int numBytesRead = 0;
+    int totBytesRead = 0;
+    int out_fd;
+    int rc = RC_SUCCESS;
+    int dtb_count = 0, dtb_offset = 0, entry_size;
+    size_t wrote = 0, expected = 0;
+    uint32_t dtb_size;
+    uint32_t version = 0;
+    char *filename;
+
+    log_info("DTB combiner:\n");
+
+    if (parse_commandline(argc, argv) != RC_SUCCESS) {
+        print_help();
+        return RC_ERROR;
+    }
+
+    log_info("  Input directory: '%s'\n", input_dir);
+    log_info("  Output file: '%s'\n", output_file);
+
+
+    filler = (uint8_t *)malloc(page_size);
+    if (!filler) {
+        log_err("Out of memory\n");
+        return RC_ERROR;
+    }
+    memset(filler, 0, page_size);
+
+    dtb_count = find_dtb(input_dir, &version);
+
+    log_info("=> Found %d unique DTB(s)\n", dtb_count);
+
+    if (!dtb_count)
+        goto cleanup;
+
+
+    /* Generate the master DTB file:
+
+       Simplify write error handling by just checking for actual vs
+       expected bytes written at the end.
+     */
+
+    log_info("\nGenerating master DTB... ");
+
+    out_fd = open(output_file, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
+    if (out_fd == -1) {
+        log_err("Cannot create '%s'\n", output_file);
+        rc = RC_ERROR;
+        goto cleanup;
+    }
+
+    if (version_override != 0) {
+        version = version_override;
+    }
+
+    if (version == 1) {
+        entry_size = 20;
+    } else if (version == 2) {
+        entry_size = 24;
+    } else {
+        entry_size = 40;
+    }
+
+    /* Write header info */
+    wrote += write(out_fd, QCDT_MAGIC, sizeof(uint8_t) * 4); /* magic */
+    wrote += write(out_fd, &version, sizeof(uint32_t));      /* version */
+    wrote += write(out_fd, (uint32_t *)&dtb_count, sizeof(uint32_t));
+                                                             /* #DTB */
+
+    /* Calculate offset of first DTB block */
+    dtb_offset = 12                       + /* header */
+                 (entry_size * dtb_count) + /* DTB table entries */
+                 4;                         /* end of table indicator */
+
+    /* Round up to page size */
+    padding = page_size - (dtb_offset % page_size);
+    dtb_offset += padding;
+    expected = dtb_offset;
+
+    /* Write index table:
+         chipset
+         platform
+         subtype (v2/v3 only)
+         soc rev
+         pmic model0 (v3 only)
+         pmic model1 (v3 only)
+         pmic model2 (v3 only)
+         pmic model3 (v3 only)
+         dtb offset
+         dtb size
+     */
+    for (chip = chip_list; chip; chip = chip->next) {
+        wrote += write(out_fd, &chip->chipset, sizeof(uint32_t));
+        wrote += write(out_fd, &chip->platform, sizeof(uint32_t));
+        if (version >= 2) {
+            wrote += write(out_fd, &chip->subtype, sizeof(uint32_t));
+        }
+        wrote += write(out_fd, &chip->revNum, sizeof(uint32_t));
+        if (version >= 3) {
+            wrote += write(out_fd, &chip->pmic_model[0], sizeof(uint32_t));
+            wrote += write(out_fd, &chip->pmic_model[1], sizeof(uint32_t));
+            wrote += write(out_fd, &chip->pmic_model[2], sizeof(uint32_t));
+            wrote += write(out_fd, &chip->pmic_model[3], sizeof(uint32_t));
+        }
+        if (chip->master->master_offset != 0) {
+            wrote += write(out_fd, &chip->master->master_offset, sizeof(uint32_t));
+        } else {
+            wrote += write(out_fd, &expected, sizeof(uint32_t));
+            chip->master->master_offset = expected;
+            expected += chip->master->dtb_size;
+        }
+        wrote += write(out_fd, &chip->master->dtb_size, sizeof(uint32_t));
+    }
+
+    rc = RC_SUCCESS;
+    wrote += write(out_fd, &rc, sizeof(uint32_t)); /* end of table indicator */
+    if (padding > 0)
+        wrote += write(out_fd, filler, padding);
+
+    /* Write DTB's */
+    for (chip = chip_list; chip; chip = chip->next) {
+        if (chip->master->wroteDtb) {
+            continue;
+        }
+
+        chip->master->wroteDtb = 1;
+        filename = chip->master->dtb_file;
+        dtb_size = chip->master->dtb_size;
+
+        log_dbg("\n (writing '%s' - %u bytes) ", filename, dtb_size);
+        pInputFile = fopen(filename, "r");
+        if (pInputFile != NULL) {
+            totBytesRead = 0;
+            while ((numBytesRead = fread(buf, 1, COPY_BLK, pInputFile)) > 0) {
+                wrote += write(out_fd, buf, numBytesRead);
+                totBytesRead += numBytesRead;
+            }
+            fclose(pInputFile);
+            padding = page_size - (totBytesRead % page_size);
+            if ((uint32_t)(totBytesRead + padding) != dtb_size) {
+                log_err("DTB size mismatch, please re-run: expected %d vs actual %d (%s)\n",
+                        dtb_size, totBytesRead + padding,
+                        filename);
+                rc = RC_ERROR;
+                break;
+            }
+            if (padding > 0)
+                wrote += write(out_fd, filler, padding);
+        } else {
+            log_err("failed to open DTB '%s'\n", filename);
+            rc = RC_ERROR;
+            break;
+        }
+    }
+    close(out_fd);
+
+    if (expected != wrote) {
+        log_err("error writing output file, please rerun: size mismatch %zu vs %zu\n",
+                expected, wrote);
+        rc = RC_ERROR;
+    } else
+        log_dbg("Total wrote %zu bytes\n", wrote);
+
+    if (rc != RC_SUCCESS)
+        unlink(output_file);
+    else
+        log_info("completed\n");
+
+cleanup:
+    free(filler);
+    chip_deleteall();
+    return rc;
+}
diff --git a/qcom/dtbtool/dtbtool.txt b/qcom/dtbtool/dtbtool.txt
new file mode 100644
index 0000000..1aa0be4
--- /dev/null
+++ b/qcom/dtbtool/dtbtool.txt
@@ -0,0 +1,235 @@
+Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
+
+Redistribution and use in source form and compiled forms (SGML, HTML,
+PDF, PostScript, RTF and so forth) with or without modification, are
+permitted provided that the following conditions are met:
+
+Redistributions in source form must retain the above copyright
+notice, this list of conditions and the following disclaimer as the
+first lines of this file unmodified.
+
+Redistributions in compiled form (transformed to other DTDs,
+converted to PDF, PostScript, RTF and other formats) must reproduce
+the above copyright notice, this list of conditions and the following
+disclaimer in the documentation and/or other materials provided with
+the distribution.
+
+THIS DOCUMENTATION IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND
+NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD
+DOCUMENTATION PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGE.
+
+
+Android - Table of Device Tree
+==============================
+
+0) Document revision
+   v1.0 - Initial version (dng)
+   v1.1 - Add v2 format to allow subtype (dng)
+
+1) Android boot image:
+----------------------
+1.1) Header:
+   1) Magic (8B)
+   2) kernel size (4B)
+   3) kernel addr (4B)
+   4) ramdisk size (4B)
+   5) ramdisk addr (4B)
+   6) 2ndary size (4B)
+   7) 2ndary addr (4B)
+   8) tags addr (4B)
+   9) page size (4B)
+  10) unused #1 (4B) (zero in standard Android)
+  11) unused #2 (4B) (zero in standard Android)
+  12) product name (16B)
+  13) kernel cmdline (512B)
+  14) id (8B)
+
+1.2) Layout:
+   A) header (as above - 1 page)
+   B) kernel (n pages)
+   C) ramdisk (m pages)
+   D) second stage (o pages)
+
+2) QC table of device tree
+--------------------------
+2.1) Changes:
+   i) use "unused #1, #2" members in existing boot image
+      header to point to new table of device tree
+      (#1 - size of QC table of DT)
+  ii) append table of device tree (described later)
+      after "D) second stage"
+
+2.2) Format:
+                                 size
+   x      +------------------+
+   |      | MAGIC ("QCDT")   |   4B
+   |      +------------------+
+ header   | VERSION          |   uint32 (version 3)
+   |      +------------------+
+   |      | num of DTBs      |   uint32 (number of DTB entries)
+   x      +------------------+
+   |      | platform id #1   |   uint32 (e.g. ID for MSM8974)
+   |      +------------------+
+   |      | variant id #1    |   uint32 (e.g. ID for CDP, MTP)
+   |      +------------------+
+   |      | subtype id #1    |   uint32 (e.g. ID for subtype) (QCDT v2)
+ device   +------------------+
+  #1      | soc rev #1       |   uint32 (e.g. MSM8974 v2)
+ entry    +------------------+
+   |      | pmic0 #1         |   uint32 (pmic0-> first smallest SID of existing pmic)
+   |      +------------------+
+   |      | pmic1 #1         |   uint32 (pmic1-> secondary smallest SID of existing pmic)
+   |      +------------------+
+   |      | pmic2 #1         |   uint32 (pmic2-> third smallest SID of existing pmic)
+   |      +------------------+
+   |      | pmic3 #1         |   uint32 (pmic3-> fourth smallest SID of existing pmic)
+   |      +------------------+
+   |      | offset #1        |   uint32 (byte offset from start/before MAGIC
+   |      +------------------+           to DTB entry)
+   |      | size #1          |   uint32 (size in bytes of DTB blob)
+   x      +------------------+
+   .              .
+   .              .  (repeat)
+   .              .
+
+   x      +------------------+
+   |      | platform id #Z   |   uint32 (e.g. ID for MSM8974)
+   |      +------------------+
+  device  | variant id #Z    |   uint32 (e.g. ID for CDP, MTP)
+  #Z      +------------------+
+  entry   | subtype id #Z    |   uint32 (e.g. ID for subtype) (QCDT v2)
+  (last)  +------------------+
+   |      | soc rev #Z       |   uint32 (e.g. MSM8974 v2)
+   |      +------------------+
+   |      | pmic0 #1         |   uint32 (pmic0-> first smallest SID of existing pmic)
+   |      +------------------+
+   |      | pmic1 #1         |   uint32 (pmic1-> secondary smallest SID of existing pmic)
+   |      +------------------+
+   |      | pmic2 #1         |   uint32 (pmic2-> third smallest SID of existing pmic)
+   |      +------------------+
+   |      | pmic3 #1         |   uint32 (pmic3-> fourth smallest SID of existing pmic)
+   |      +------------------+
+   |      | offset #Z        |   uint32 (byte offset from start/before MAGIC
+   x      +------------------+           to DTB entry)
+          | 0 ("zero")       |   uint32 (end of list delimiter)
+          +------------------+           to DTB entry)
+          | padding          |   variable length for next DTB to start on
+          +------------------+           page boundary
+          | DTB #1           |   variable (start is page aligned)
+          |                  |
+          |                  |
+          +------------------+
+          | padding          |   variable length for next DTB to start on
+          +------------------+           page boundary
+                  .
+                  .
+                  .
+
+          +------------------+
+          | DTB #Z (last)    |   variable (start is page aligned)
+          |                  |
+          |                  |
+          +------------------+
+
+3) Operations
+-------------
+3.1) Build-time:
+  1) Each DTS per device will add a "qcom,msm-id" entry
+     e.g. for msm8974-sim.dts, add
+            qcom,msm-id = <x y z>;
+          or
+            qcom,msm-id = <x z>;
+            qcom,board-id = <y y'>;
+          or
+            qcom,msm-id = <x z>;
+            qcom,board-id = <y y'>;
+            qcom,pmic-id = <a b c d>;
+     x  = ID for msm8974
+     y  = ID for CDP, MTP, etc.
+     y' = ID for subtype (assumed zero if absent)
+     z  = ID for soc revision
+     a = pmic0
+     b = pmic1
+     c = pmic2
+     d = pmic3
+     SBL populates the pmic entries always in ascending order of SID, so
+     pmic0-> SID0, pmic1-> SID1, pmic2-> SID2, pmic3-> SID3.
+     e.g. for qcom,pmic-id = <pmic0 pmic1 pmic2 pmic3>
+			Board X = MSM8994 + PM8994 + PMI8994            (Existing boards [ROW])
+			Board Y = MSM8994 + PM8994 + PMI8994 + PM8004   (Internal SS board variant)
+			Board Z = MSM8994 + PM8994 + PM8004             (Boards that SS will be making)
+
+		For all boards X, Y, and Z, PMICs have the following SIDs and REVID SUBTYPEs
+		(i.e. PMIC Model):
+			PM8994 -            SID 0 and 1; subtype = 9
+			PMI8994 -           SID 2 and 3; subtype = 10
+			PM8004 -            SID 4 and 5; subtype = 12
+
+		LK using SMEM PMIC info(1 as major and 0 as minor version for example):
+			Board X: qcom,pmic-id = <0x0109 0x010A  0x0 0x0>;
+			Board Y: qcom,pmic-id = <0x0109 0x010A 0x010C 0x0>;
+			Board Z: qcom,pmic-id = <0x0109 0x010C 0x0 0x0>;
+
+     The entry can optionally be an array:
+         qcom,msm-id = <x1 y1 z1>, <x2 y2 z2>, ...;
+       or
+         qcom,msm-id = <x1 z1>, <x2 z2>, ...;
+         qcom,board-id = <y1 y1'>, ...;
+        or
+         qcom,msm-id = <x1 z1>, <x2 z2>, ...;
+         qcom,board-id = <y1 y1'>, ...;
+         qcom,pmic-id = <a1 b1 c1 d1>, ...;
+     Note that qcom,msm-id, qcom,board-id and qcom,pmic-id are not matched pairs.
+  2) Kernel compile will generate the DTB
+  3) Android build will run a new tool (dtbTool)
+     a) scan the DTB output directory for all compiled DTB
+     b) decompile the DTB for "qcom,msm-id"/"qcom,board-id"/"qcom,pmic-id"
+     c) generate the QC table of device tree in sorted
+        order (platform, variant, subtype, soc rev, pmic0, pmic1, pmic2, pmic3)
+     d) modified mkbootimg will merge new table of DT
+
+3.2) Run-time:
+  1) LK bootloader will obtain platform id/variant/subtype/soc rev/major ver/minor ver
+     /pmic0/pmic1/pmic2/pmic3 info either from early bootloaders or via other means
+  2) LK bootloader will check entries #10 for non-zero
+     value (set to zero for standard boot.img).  If the
+     value is non-zero, refer to page section after
+     the "second stage" in the boot.img layout
+  3) Check QCDT magic
+  4) Check QCDT version (optional LK to handle multiple
+     QCDT version)
+  5) LK scans through the QCDT table to look for matching
+     entry.  Search order is:
+     1) msm ID exact match
+     2) Platform type exact match
+     3) subtype ID exact match
+     4) HLOS subtype exact match
+     5) Pmic0 model ID exact match
+     6) Pmic1 model ID exact match
+     7) Pmic2 model ID exact match
+     8) Pmic3 model ID exact match
+     9) foundry ID, look for exact match, if not found choose
+		device tree with foundry-id(0x0)
+     10) select the highest soc rev in QCDT that is
+        equal to or lower than the runtime detected soc rev
+     11) select the highest major&minor ver in QCDT that is
+        equal to or lower than the runtime detected major ver
+     12) select the highest pmic0 major&minor in QCDT that is
+        equal to or lower than the runtime detected pmic0
+     13) select the highest pmic1 major&minor in QCDT that is
+        equal to or lower than the runtime detected pmic1
+     14) select the highest pmic2 major&minor in QCDT that is
+        equal to or lower than the runtime detected pmic2
+     15) select the highest pmic3 major&minor in QCDT that is
+        equal to or lower than the runtime detected pmic3
+  6) Load the matching DTB blob to the tags addr
+  7) LK pass the correct DTB to the kernel
diff --git a/verity_tool/Android.bp b/verity_tool/Android.bp
new file mode 100644
index 0000000..0a2ecd8
--- /dev/null
+++ b/verity_tool/Android.bp
@@ -0,0 +1,28 @@
+// Copyright 2018 The LineageOS Project
+
+cc_library {
+    name: "libveritytool",
+    srcs: ["verity_tool.cpp"],
+    local_include_dirs: ["include"],
+    export_include_dirs: ["include"],
+    cflags: ["-Werror"],
+    shared_libs: [
+        "libbase",
+        "libcrypto",
+        "libcrypto_utils",
+        "libfec",
+    ],
+    static_libs: [
+        "libfs_mgr",
+    ],
+    whole_static_libs: [
+        "libavb_user",
+    ],
+}
+
+cc_binary {
+    name: "veritytool",
+    srcs: ["main.cpp"],
+    shared_libs: ["libveritytool"],
+    cflags: ["-Werror"],
+}
diff --git a/verity_tool/include/verity_tool.h b/verity_tool/include/verity_tool.h
new file mode 100644
index 0000000..25a6a7c
--- /dev/null
+++ b/verity_tool/include/verity_tool.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2018 The LineageOS 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.
+ */
+
+#pragma once
+
+#include <string>
+
+/*
+ * Return codes:
+ *
+ *    true: verity state set
+ *    false: verity state not set
+ */
+bool set_block_device_verity_enabled(const std::string& block_device,
+                                     bool enable);
+
+/*
+ * Return codes:
+ *
+ *    true: verity state set for all block devices
+ *    false: verity state not for set all block devices
+ */
+bool set_verity_enabled(bool enable);
diff --git a/verity_tool/main.cpp b/verity_tool/main.cpp
new file mode 100644
index 0000000..f5f026a
--- /dev/null
+++ b/verity_tool/main.cpp
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2018 The LineageOS 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 "verity_tool.h"
+
+#include <getopt.h>
+#include <stdio.h>
+#include <string.h>
+
+static void print_usage() {
+    printf("veritytool - toggle block device verification\n"
+           "    --help        show this help\n"
+           "    --enable      enable dm-verity\n"
+           "    --disable     disable dm-verity\n");
+}
+
+int main(int argc, char** argv) {
+    int c, rc;
+    int enable = 0;
+    bool flag_set = false;
+    struct option long_opts[] = {
+        {"disable", no_argument, &enable, 0},
+        {"enable", no_argument, &enable, 1},
+        {NULL, 0, NULL, 0},
+    };
+
+    while ((c = getopt_long(argc, argv, "de", long_opts, NULL)) != -1) {
+        switch (c) {
+            case 0:
+                flag_set = true;
+                break;
+            default:
+                print_usage();
+                exit(0);
+        }
+    }
+
+    if (!flag_set) {
+        print_usage();
+        exit(0);
+    }
+
+    if (!set_verity_enabled(enable)) {
+        printf("Error occurred in set_verity_enable\n");
+        exit(EXIT_FAILURE);
+    }
+
+    printf("Set verity mode to: %s\n", enable ? "enabled" : "disabled");
+    printf("Now reboot your device for settings to take effect\n");
+    return 0;
+}
diff --git a/verity_tool/verity_tool.cpp b/verity_tool/verity_tool.cpp
new file mode 100644
index 0000000..9575c4c
--- /dev/null
+++ b/verity_tool/verity_tool.cpp
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2014 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 "verity_tool.h"
+
+#include <android-base/logging.h>
+#include <android-base/properties.h>
+#include <fs_mgr.h>
+#include <fec/io.h>
+#include <libavb_user/libavb_user.h>
+
+#include <linux/fs.h>
+
+#include <errno.h>
+
+static int make_block_device_writable(const std::string& block_device) {
+    int fd = open(block_device.c_str(), O_RDONLY | O_CLOEXEC);
+    if (fd < 0) {
+        return -errno;
+    }
+
+    int OFF = 0;
+    int rc = ioctl(fd, BLKROSET, &OFF);
+    if (rc < 0) {
+        rc = -errno;
+        goto out;
+    }
+    rc = 0;
+out:
+    close(fd);
+    return rc;
+}
+
+/* Turn verity on/off */
+bool set_block_device_verity_enabled(const std::string& block_device,
+        bool enable) {
+    int rc = make_block_device_writable(block_device);
+    if (rc) {
+        LOG(ERROR) << "Could not make block device "
+                   << block_device << " writable:" << rc;
+        return false;
+    }
+
+    fec::io fh(block_device, O_RDWR);
+    if (!fh) {
+        PLOG(ERROR) << "Could not open block device " << block_device;
+        return false;
+    }
+
+    fec_verity_metadata metadata;
+    if (!fh.get_verity_metadata(metadata)) {
+        LOG(ERROR) << "Couldn't find verity metadata!";
+        return false;
+    }
+
+    if (!enable && metadata.disabled) {
+        LOG(ERROR) << "Verity already disabled on " << block_device;
+        return false;
+    }
+
+    if (enable && !metadata.disabled) {
+        LOG(WARNING) << "Verity already enabled on " << block_device;
+        return false;
+    }
+
+    if (!fh.set_verity_status(enable)) {
+        PLOG(ERROR) << "Could not set verity "
+                    << (enable ? "enabled" : "disabled")
+                    << " flag on device " << block_device;
+        return false;
+    }
+
+    LOG(DEBUG) << "Verity " << (enable ? "enabled" : "disabled")
+               << " on " << block_device;
+    return true;
+}
+
+/* Helper function to get A/B suffix, if any. If the device isn't
+ * using A/B the empty string is returned. Otherwise either "_a",
+ * "_b", ... is returned.
+ *
+ * Note that since sometime in O androidboot.slot_suffix is deprecated
+ * and androidboot.slot should be used instead. Since bootloaders may
+ * be out of sync with the OS, we check both and for extra safety
+ * prepend a leading underscore if there isn't one already.
+ */
+static std::string get_ab_suffix() {
+    std::string ab_suffix = android::base::GetProperty("ro.boot.slot_suffix", "");
+    if (ab_suffix.empty()) {
+        ab_suffix = android::base::GetProperty("ro.boot.slot", "");
+    }
+    if (ab_suffix.size() > 0 && ab_suffix[0] != '_') {
+        ab_suffix = std::string("_") + ab_suffix;
+    }
+    return ab_suffix;
+}
+
+/* Use AVB to turn verity on/off */
+static bool set_avb_verity_enabled_state(AvbOps* ops, bool enable_verity) {
+    std::string ab_suffix = get_ab_suffix();
+
+    bool verity_enabled;
+    if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) {
+        LOG(ERROR) << "Error getting verity state";
+        return false;
+    }
+
+    if ((verity_enabled && enable_verity) ||
+        (!verity_enabled && !enable_verity)) {
+        LOG(WARNING) << "verity is already "
+                     << verity_enabled ? "enabled" : "disabled";
+        return false;
+    }
+
+    if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) {
+        LOG(ERROR) << "Error setting verity";
+        return false;
+    }
+
+    LOG(DEBUG) << "Successfully " << (enable_verity ? "enabled" : "disabled")
+               << " verity";
+    return true;
+}
+
+bool set_verity_enabled(bool enable) {
+    bool rc = true;
+
+    // Do not allow changing verity on user builds
+    bool is_user = (android::base::GetProperty("ro.build.type", "") == "user");
+    if (is_user) {
+        LOG(ERROR) << "Cannot disable verity - USER BUILD";
+        return false;
+    }
+
+    // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by
+    // contract, androidboot.vbmeta.digest is set by the bootloader
+    // when using AVB).
+    bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty();
+
+    // If using AVB, dm-verity is used on any build so we want it to
+    // be possible to disable/enable on any build (except USER). For
+    // VB1.0 dm-verity is only enabled on certain builds.
+    if (using_avb) {
+        // Yep, the system is using AVB.
+        AvbOps* ops = avb_ops_user_new();
+        if (ops == nullptr) {
+            LOG(ERROR) << "Error getting AVB ops";
+            return false;
+        }
+        rc = set_avb_verity_enabled_state(ops, enable);
+        avb_ops_user_free(ops);
+    } else {
+        // Not using AVB - assume VB1.0.
+
+        // read all fstab entries at once from all sources
+        struct fstab* fstab = fs_mgr_read_fstab_default();
+        if (!fstab) {
+            LOG(ERROR) << "Failed to read fstab";
+            return false;
+        }
+
+        // Loop through entries looking for ones that vold manages.
+        for (int i = 0; i < fstab->num_entries; i++) {
+            if (fs_mgr_is_verified(&fstab->recs[i])) {
+                bool result = set_block_device_verity_enabled(
+                        fstab->recs[i].blk_device, enable);
+                if (!result) {
+                    // Warn, but continue if failure occurred
+                    LOG(WARNING) << "Failed to set state "
+                                 << (enable ? "enabled" : "disabled")
+                                 << " on " << fstab->recs[i].mount_point;
+                }
+                rc = rc && result;
+            }
+        }
+    }
+
+    return rc;
+}