Переглянути джерело

Initial commit

*Based on Sony Discovery
Aayush Gupta 7 роки тому
коміт
e80669c371
42 змінених файлів з 3168 додано та 0 видалено
  1. 30 0
      Android.mk
  2. 18 0
      AndroidProducts.mk
  3. 93 0
      BoardConfig.mk
  4. 29 0
      README.md
  5. 30 0
      bootctrl/Android.mk
  6. 26 0
      bootctrl/NOTICE
  7. 670 0
      bootctrl/boot_control.cpp
  8. 68 0
      omni_PL2.mk
  9. BIN
      prebuilt/Image.gz-dtb
  10. 1 0
      recovery/Android.mk
  11. 46 0
      recovery/gpt-utils/Android.mk
  12. 1533 0
      recovery/gpt-utils/gpt-utils.cpp
  13. 192 0
      recovery/gpt-utils/gpt-utils.h
  14. 58 0
      recovery/gpt-utils/scsi/ufs/ioctl.h
  15. 71 0
      recovery/gpt-utils/scsi/ufs/ufs.h
  16. 17 0
      recovery/root/etc/recovery.fstab
  17. 13 0
      recovery/root/etc/twrp.fstab
  18. 100 0
      recovery/root/init.recovery.qcom.rc
  19. 117 0
      recovery/root/init.recovery.usb.rc
  20. BIN
      recovery/root/sbin/android.hardware.gatekeeper@1.0-service-qti
  21. BIN
      recovery/root/sbin/android.hardware.keymaster@3.0-service-qti
  22. 36 0
      recovery/root/sbin/prepdecrypt.sh
  23. BIN
      recovery/root/sbin/qseecomd
  24. BIN
      recovery/root/vendor/lib64/android.hardware.boot@1.0.so
  25. BIN
      recovery/root/vendor/lib64/android.hardware.confirmationui@1.0.so
  26. BIN
      recovery/root/vendor/lib64/android.hardware.weaver@1.0.so
  27. BIN
      recovery/root/vendor/lib64/hw/android.hardware.gatekeeper@1.0-impl-qti.so
  28. BIN
      recovery/root/vendor/lib64/hw/android.hardware.keymaster@3.0-impl-qti.so
  29. BIN
      recovery/root/vendor/lib64/hw/bootctrl.qcom.so
  30. BIN
      recovery/root/vendor/lib64/libGPreqcancel.so
  31. BIN
      recovery/root/vendor/lib64/libGPreqcancel_svc.so
  32. BIN
      recovery/root/vendor/lib64/libQSEEComAPI.so
  33. BIN
      recovery/root/vendor/lib64/libdiag.so
  34. BIN
      recovery/root/vendor/lib64/libdrmfs.so
  35. BIN
      recovery/root/vendor/lib64/libdrmtime.so
  36. BIN
      recovery/root/vendor/lib64/libgptutils.so
  37. BIN
      recovery/root/vendor/lib64/libkeymasterdeviceutils.so
  38. BIN
      recovery/root/vendor/lib64/libqisl.so
  39. BIN
      recovery/root/vendor/lib64/librpmb.so
  40. BIN
      recovery/root/vendor/lib64/libssd.so
  41. BIN
      recovery/root/vendor/lib64/libtime_genoff.so
  42. 20 0
      recovery/root/vendor/manifest.xml

+ 30 - 0
Android.mk

@@ -0,0 +1,30 @@
+#
+# Copyright 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.
+#
+
+# This contains the module build definitions for the hardware-specific
+# components for this device.
+#
+# As much as possible, those components should be built unconditionally,
+# with device-specific names to avoid collisions, to avoid device-specific
+# bitrot and build breakages. Building a component unconditionally does
+# *not* include it on all devices, so it is safe even with hardware-specific
+# components.
+
+LOCAL_PATH := $(call my-dir)
+
+ifneq ($(filter PL2,$(TARGET_DEVICE)),)
+include $(call all-makefiles-under,$(LOCAL_PATH))
+endif

+ 18 - 0
AndroidProducts.mk

@@ -0,0 +1,18 @@
+#
+# Copyright 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.
+#
+
+PRODUCT_MAKEFILES := \
+    $(LOCAL_DIR)/omni_PL2.mk

+ 93 - 0
BoardConfig.mk

@@ -0,0 +1,93 @@
+#
+# Copyright 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.
+#
+
+# This contains the module build definitions for the hardware-specific
+# components for this device.
+#
+# As much as possible, those components should be built unconditionally,
+# with device-specific names to avoid collisions, to avoid device-specific
+# bitrot and build breakages. Building a component unconditionally does
+# *not* include it on all devices, so it is safe even with hardware-specific
+# components.
+
+LOCAL_PATH := device/nokia/PL2
+TARGET_SPECIFIC_HEADER_PATH := $(LOCAL_PATH)/include
+
+# Architecture
+TARGET_ARCH := arm64
+TARGET_ARCH_VARIANT := armv8-a
+TARGET_CPU_ABI := arm64-v8a
+TARGET_CPU_ABI2 :=
+TARGET_CPU_VARIANT := generic
+
+TARGET_2ND_ARCH := arm
+TARGET_2ND_ARCH_VARIANT := armv7-a-neon
+TARGET_2ND_CPU_ABI := armeabi-v7a
+TARGET_2ND_CPU_ABI2 := armeabi
+TARGET_2ND_CPU_VARIANT := cortex-a53
+
+# Bootloader
+TARGET_BOOTLOADER_BOARD_NAME := sdm660
+TARGET_NO_BOOTLOADER := true
+
+# Kernel
+BOARD_KERNEL_CMDLINE := console=ttyMSM0,115200,n8 androidboot.console=ttyMSM0 earlycon=msm_serial_dm,0xc170000 androidboot.hardware=qcom msm_rtb.filter=0x37 ehci-hcd.park=3 lpm_levels.sleep_disabled=1 sched_enable_hmp=1 sched_enable_power_aware=1 service_locator.enable=1 swiotlb=1 androidboot.configfs=true androidboot.usbcontroller=a800000.dwc3
+BOARD_KERNEL_CMDLINE     += skip_override androidboot.selinux=permissive
+BOARD_KERNEL_BASE        := 0x00000000
+BOARD_KERNEL_OFFSET      := 0x00008000
+BOARD_KERNEL_PAGESIZE    := 4096
+BOARD_KERNEL_TAGS_OFFSET := 0x00000100
+BOARD_RAMDISK_OFFSET     := 0x01000000
+BOARD_SECOND_OFFSET      := 0x00f00000
+TARGET_PREBUILT_KERNEL := $(LOCAL_PATH)/prebuilt/Image.gz-dtb
+
+# Platform
+TARGET_BOARD_PLATFORM := sdm660
+TARGET_BOARD_PLATFORM_GPU := qcom-adreno508
+
+# Encryption
+PLATFORM_SECURITY_PATCH := 2025-12-31
+
+# Partitions
+BOARD_BOOTIMAGE_PARTITION_SIZE := 67108864
+BOARD_SYSTEMIMAGE_PARTITION_SIZE := 3036676096
+BOARD_USERDATAIMAGE_PARTITION_SIZE := 21474820096
+BOARD_BUILD_SYSTEM_ROOT_IMAGE := true
+BOARD_FLASH_BLOCK_SIZE := 131072 # (BOARD_KERNEL_PAGESIZE * 64)
+
+# Recovery
+BOARD_HAS_LARGE_FILESYSTEM := true
+BOARD_HAS_NO_SELECT_BUTTON := true
+TARGET_NO_KERNEL := false
+TARGET_RECOVERY_PIXEL_FORMAT := "RGBX_8888"
+TARGET_USERIMAGES_USE_EXT4 := true
+
+# TWRP specific build flags
+RECOVERY_SDCARD_ON_DATA := true
+TARGET_RECOVERY_QCOM_RTC_FIX := true
+TW_BRIGHTNESS_PATH := "/sys/class/leds/lcd-backlight/brightness"
+TW_MAX_BRIGHTNESS := 100
+TW_DEFAULT_BRIGHTNESS := "30"
+TW_EXTRA_LANGUAGES := true
+TW_INCLUDE_CRYPTO := true
+TW_INCLUDE_NTFS_3G := true
+TW_INPUT_BLACKLIST := "hbtp_vm"
+TW_SCREEN_BLANK_ON_BOOT := true
+TW_THEME := portrait_hdpi
+TW_USE_TOOLBOX := true
+
+# Workaround for error copying vendor files to recovery ramdisk
+TARGET_COPY_OUT_VENDOR := system/vendor

+ 29 - 0
README.md

@@ -0,0 +1,29 @@
+# Device Tree for Nokia 6.1 (PL2)
+
+The Nokia 6 (codenamed _"PL2"_) is a mid-range smartphone from Nokia.
+It was released in April 2018.
+
+| Basic                   | Spec Sheet                                                                                                                     |
+| -----------------------:|:------------------------------------------------------------------------------------------------------------------------------ |
+| CPU                     | Octa-core 2.2 GHz Cortex-A53                                                                                                   |
+| Chipset                 | Qualcomm SDM630 Snapdragon 660                                                                                                 |
+| GPU                     | Adreno 630                                                                                                                     |
+| Memory                  | 3/4 GB RAM                                                                                                                     |
+| Shipped Android Version | 8.1                                                                                                                            |
+| Storage                 | 32/64 GB                                                                                                                       |
+| Battery                 | Non-removable Li-Po 3000 mAh battery                                                                                           |
+| Display                 | 1080 x 1920 pixels, 16:9 ratio (~403 ppi density)                                                                              |
+| Camera (Back)           | 16 MP, f/2.0, 27mm (wide), 1.0µm, PDAF, Zeiss optics, dual-LED dual-tone flash, panorama, HDR, 2160p@30fps, 1080p@30fps        |
+| Camera (Front)          | 8 MP, f/2.0, 1/4", 1.12µm, 1080p@30fps                                                                                         |
+
+![Nokia 6.1](https://cdn2.gsmarena.com/vv/pics/nokia/nokia-6-2018-1.jpg)
+
+## Build instructions
+
+```
+# Compiling
+$ . build/envsetup.sh
+$ lunch omni_PL2-eng
+$ make -jx recoveryimage //replace x in -jx with number of cores you want to allot for compilation
+
+```

+ 30 - 0
bootctrl/Android.mk

@@ -0,0 +1,30 @@
+ifeq ($(AB_OTA_UPDATER),true)
+# TODO:  Find a better way to separate build configs for ADP vs non-ADP devices
+ifneq ($(BOARD_IS_AUTOMOTIVE),true)
+ifneq ($(filter msm8996 msm8998 sdm660 sdm845,$(TARGET_BOARD_PLATFORM)),)
+LOCAL_PATH := $(call my-dir)
+
+# HAL Shared library for the target. Used by libhardware.
+include $(CLEAR_VARS)
+LOCAL_CFLAGS += -Wall -Werror
+LOCAL_SHARED_LIBRARIES += liblog libgptutils libcutils
+LOCAL_HEADER_LIBRARIES := libhardware_headers libsystem_headers
+LOCAL_SRC_FILES := boot_control.cpp
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_MODULE := bootctrl.$(TARGET_BOARD_PLATFORM)
+LOCAL_MODULE_OWNER := qcom
+LOCAL_PROPRIETARY_MODULE := true
+include $(BUILD_SHARED_LIBRARY)
+
+# Static library for the target. Used by update_engine_sideload from recovery.
+include $(CLEAR_VARS)
+LOCAL_CFLAGS += -Wall -Werror
+LOCAL_SHARED_LIBRARIES += liblog libgptutils libcutils
+LOCAL_HEADER_LIBRARIES := libhardware_headers libsystem_headers
+LOCAL_SRC_FILES := boot_control.cpp
+LOCAL_MODULE := bootctrl.$(TARGET_BOARD_PLATFORM)
+include $(BUILD_STATIC_LIBRARY)
+
+endif
+endif
+endif

+ 26 - 0
bootctrl/NOTICE

@@ -0,0 +1,26 @@
+Copyright (c) 2016, 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.

+ 670 - 0
bootctrl/boot_control.cpp

@@ -0,0 +1,670 @@
+/*
+ * Copyright (c) 2016, 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.
+ */
+#include <map>
+#include <list>
+#include <string>
+#include <vector>
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include <errno.h>
+#define LOG_TAG "bootcontrolhal"
+#include <log/log.h>
+#include <hardware/boot_control.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <cutils/properties.h>
+#include "../recovery/gpt-utils/gpt-utils.h"
+
+#define BOOTDEV_DIR "/dev/block/bootdevice/by-name"
+#define BOOT_IMG_PTN_NAME "boot"
+#define LUN_NAME_END_LOC 14
+#define BOOT_SLOT_PROP "ro.boot.slot_suffix"
+
+#define SLOT_ACTIVE 1
+#define SLOT_INACTIVE 2
+#define UPDATE_SLOT(pentry, guid, slot_state) ({ \
+		memcpy(pentry, guid, TYPE_GUID_SIZE); \
+		if (slot_state == SLOT_ACTIVE)\
+			*(pentry + AB_FLAG_OFFSET) = AB_SLOT_ACTIVE_VAL; \
+		else if (slot_state == SLOT_INACTIVE) \
+		*(pentry + AB_FLAG_OFFSET)  = (*(pentry + AB_FLAG_OFFSET)& \
+			~AB_PARTITION_ATTR_SLOT_ACTIVE); \
+		})
+
+using namespace std;
+const char *slot_suffix_arr[] = {
+	AB_SLOT_A_SUFFIX,
+	AB_SLOT_B_SUFFIX,
+	NULL};
+
+enum part_attr_type {
+	ATTR_SLOT_ACTIVE = 0,
+	ATTR_BOOT_SUCCESSFUL,
+	ATTR_UNBOOTABLE,
+};
+
+void boot_control_init(struct boot_control_module *module)
+{
+	if (!module) {
+		ALOGE("Invalid argument passed to %s", __func__);
+		return;
+	}
+	return;
+}
+
+//Get the value of one of the attribute fields for a partition.
+static int get_partition_attribute(char *partname,
+		enum part_attr_type part_attr)
+{
+	struct gpt_disk *disk = NULL;
+	uint8_t *pentry = NULL;
+	int retval = -1;
+	uint8_t *attr = NULL;
+	if (!partname)
+		goto error;
+	disk = gpt_disk_alloc();
+	if (!disk) {
+		ALOGE("%s: Failed to alloc disk struct", __func__);
+		goto error;
+	}
+	if (gpt_disk_get_disk_info(partname, disk)) {
+		ALOGE("%s: Failed to get disk info", __func__);
+		goto error;
+	}
+	pentry = gpt_disk_get_pentry(disk, partname, PRIMARY_GPT);
+	if (!pentry) {
+		ALOGE("%s: pentry does not exist in disk struct",
+				__func__);
+		goto error;
+	}
+	attr = pentry + AB_FLAG_OFFSET;
+	if (part_attr == ATTR_SLOT_ACTIVE)
+		retval = !!(*attr & AB_PARTITION_ATTR_SLOT_ACTIVE);
+	else if (part_attr == ATTR_BOOT_SUCCESSFUL)
+		retval = !!(*attr & AB_PARTITION_ATTR_BOOT_SUCCESSFUL);
+	else if (part_attr == ATTR_UNBOOTABLE)
+		retval = !!(*attr & AB_PARTITION_ATTR_UNBOOTABLE);
+	else
+		retval = -1;
+	gpt_disk_free(disk);
+	return retval;
+error:
+	if (disk)
+		gpt_disk_free(disk);
+	return retval;
+}
+
+//Set a particular attribute for all the partitions in a
+//slot
+static int update_slot_attribute(const char *slot,
+		enum part_attr_type ab_attr)
+{
+	unsigned int i = 0;
+	char buf[PATH_MAX];
+	struct stat st;
+	struct gpt_disk *disk = NULL;
+	uint8_t *pentry = NULL;
+	uint8_t *pentry_bak = NULL;
+	int rc = -1;
+	uint8_t *attr = NULL;
+	uint8_t *attr_bak = NULL;
+	char partName[MAX_GPT_NAME_SIZE + 1] = {0};
+	const char ptn_list[][MAX_GPT_NAME_SIZE] = { AB_PTN_LIST };
+	int slot_name_valid = 0;
+	if (!slot) {
+		ALOGE("%s: Invalid argument", __func__);
+		goto error;
+	}
+	for (i = 0; slot_suffix_arr[i] != NULL; i++)
+	{
+		if (!strncmp(slot, slot_suffix_arr[i],
+					strlen(slot_suffix_arr[i])))
+				slot_name_valid = 1;
+	}
+	if (!slot_name_valid) {
+		ALOGE("%s: Invalid slot name", __func__);
+		goto error;
+	}
+	for (i=0; i < ARRAY_SIZE(ptn_list); i++) {
+		memset(buf, '\0', sizeof(buf));
+		//Check if A/B versions of this ptn exist
+		snprintf(buf, sizeof(buf) - 1,
+                                        "%s/%s%s",
+                                        BOOT_DEV_DIR,
+                                        ptn_list[i],
+					AB_SLOT_A_SUFFIX
+					);
+		if (stat(buf, &st)) {
+			//partition does not have _a version
+			continue;
+		}
+		memset(buf, '\0', sizeof(buf));
+		snprintf(buf, sizeof(buf) - 1,
+                                        "%s/%s%s",
+                                        BOOT_DEV_DIR,
+                                        ptn_list[i],
+					AB_SLOT_B_SUFFIX
+					);
+		if (stat(buf, &st)) {
+			//partition does not have _a version
+			continue;
+		}
+		memset(partName, '\0', sizeof(partName));
+		snprintf(partName,
+				sizeof(partName) - 1,
+				"%s%s",
+				ptn_list[i],
+				slot);
+		disk = gpt_disk_alloc();
+		if (!disk) {
+			ALOGE("%s: Failed to alloc disk struct",
+					__func__);
+			goto error;
+		}
+		rc = gpt_disk_get_disk_info(partName, disk);
+		if (rc != 0) {
+			ALOGE("%s: Failed to get disk info for %s",
+					__func__,
+					partName);
+			goto error;
+		}
+		pentry = gpt_disk_get_pentry(disk, partName, PRIMARY_GPT);
+		pentry_bak = gpt_disk_get_pentry(disk, partName, SECONDARY_GPT);
+		if (!pentry || !pentry_bak) {
+			ALOGE("%s: Failed to get pentry/pentry_bak for %s",
+					__func__,
+					partName);
+			goto error;
+		}
+		attr = pentry + AB_FLAG_OFFSET;
+		attr_bak = pentry_bak + AB_FLAG_OFFSET;
+		if (ab_attr == ATTR_BOOT_SUCCESSFUL) {
+			*attr = (*attr) | AB_PARTITION_ATTR_BOOT_SUCCESSFUL;
+			*attr_bak = (*attr_bak) |
+				AB_PARTITION_ATTR_BOOT_SUCCESSFUL;
+		} else if (ab_attr == ATTR_UNBOOTABLE) {
+			*attr = (*attr) | AB_PARTITION_ATTR_UNBOOTABLE;
+			*attr_bak = (*attr_bak) | AB_PARTITION_ATTR_UNBOOTABLE;
+		} else if (ab_attr == ATTR_SLOT_ACTIVE) {
+			*attr = (*attr) | AB_PARTITION_ATTR_SLOT_ACTIVE;
+			*attr_bak = (*attr) | AB_PARTITION_ATTR_SLOT_ACTIVE;
+		} else {
+			ALOGE("%s: Unrecognized attr", __func__);
+			goto error;
+		}
+		if (gpt_disk_update_crc(disk)) {
+			ALOGE("%s: Failed to update crc for %s",
+					__func__,
+					partName);
+			goto error;
+		}
+		if (gpt_disk_commit(disk)) {
+			ALOGE("%s: Failed to write back entry for %s",
+					__func__,
+					partName);
+			goto error;
+		}
+		gpt_disk_free(disk);
+		disk = NULL;
+	}
+	return 0;
+error:
+	if (disk)
+		gpt_disk_free(disk);
+	return -1;
+}
+
+unsigned get_number_slots(struct boot_control_module *module)
+{
+	struct dirent *de = NULL;
+	DIR *dir_bootdev = NULL;
+	unsigned slot_count = 0;
+	if (!module) {
+		ALOGE("%s: Invalid argument", __func__);
+		goto error;
+	}
+	dir_bootdev = opendir(BOOTDEV_DIR);
+	if (!dir_bootdev) {
+		ALOGE("%s: Failed to open bootdev dir (%s)",
+				__func__,
+				strerror(errno));
+		goto error;
+	}
+	while ((de = readdir(dir_bootdev))) {
+		if (de->d_name[0] == '.')
+			continue;
+		if (!strncmp(de->d_name, BOOT_IMG_PTN_NAME,
+					strlen(BOOT_IMG_PTN_NAME)))
+			slot_count++;
+	}
+	closedir(dir_bootdev);
+	return slot_count;
+error:
+	if (dir_bootdev)
+		closedir(dir_bootdev);
+	return 0;
+}
+
+unsigned get_current_slot(struct boot_control_module *module)
+{
+	uint32_t num_slots = 0;
+	char bootSlotProp[PROPERTY_VALUE_MAX] = {'\0'};
+	unsigned i = 0;
+	if (!module) {
+		ALOGE("%s: Invalid argument", __func__);
+		goto error;
+	}
+	num_slots = get_number_slots(module);
+	if (num_slots <= 1) {
+		//Slot 0 is the only slot around.
+		return 0;
+	}
+	property_get(BOOT_SLOT_PROP, bootSlotProp, "N/A");
+	if (!strncmp(bootSlotProp, "N/A", strlen("N/A"))) {
+		ALOGE("%s: Unable to read boot slot property",
+				__func__);
+		goto error;
+	}
+	//Iterate through a list of partitons named as boot+suffix
+	//and see which one is currently active.
+	for (i = 0; slot_suffix_arr[i] != NULL ; i++) {
+		if (!strncmp(bootSlotProp,
+					slot_suffix_arr[i],
+					strlen(slot_suffix_arr[i])))
+				return i;
+	}
+error:
+	//The HAL spec requires that we return a number between
+	//0 to num_slots - 1. Since something went wrong here we
+	//are just going to return the default slot.
+	return 0;
+}
+
+static int boot_control_check_slot_sanity(struct boot_control_module *module,
+		unsigned slot)
+{
+	if (!module)
+		return -1;
+	uint32_t num_slots = get_number_slots(module);
+	if ((num_slots < 1) || (slot > num_slots - 1)) {
+		ALOGE("Invalid slot number");
+		return -1;
+	}
+	return 0;
+
+}
+
+int mark_boot_successful(struct boot_control_module *module)
+{
+	unsigned cur_slot = 0;
+	if (!module) {
+		ALOGE("%s: Invalid argument", __func__);
+		goto error;
+	}
+	cur_slot = get_current_slot(module);
+	if (update_slot_attribute(slot_suffix_arr[cur_slot],
+				ATTR_BOOT_SUCCESSFUL)) {
+		goto error;
+	}
+	return 0;
+error:
+	ALOGE("%s: Failed to mark boot successful", __func__);
+	return -1;
+}
+
+const char *get_suffix(struct boot_control_module *module, unsigned slot)
+{
+	if (boot_control_check_slot_sanity(module, slot) != 0)
+		return NULL;
+	else
+		return slot_suffix_arr[slot];
+}
+
+
+//Return a gpt disk structure representing the disk that holds
+//partition.
+static struct gpt_disk* boot_ctl_get_disk_info(char *partition)
+{
+	struct gpt_disk *disk = NULL;
+	if (!partition)
+		return NULL;
+	disk = gpt_disk_alloc();
+	if (!disk) {
+		ALOGE("%s: Failed to alloc disk",
+				__func__);
+		goto error;
+	}
+	if (gpt_disk_get_disk_info(partition, disk)) {
+		ALOGE("failed to get disk info for %s",
+				partition);
+		goto error;
+	}
+	return disk;
+error:
+	if (disk)
+		gpt_disk_free(disk);
+	return NULL;
+}
+
+//The argument here is a vector of partition names(including the slot suffix)
+//that lie on a single disk
+static int boot_ctl_set_active_slot_for_partitions(vector<string> part_list,
+		unsigned slot)
+{
+	char buf[PATH_MAX] = {0};
+	struct gpt_disk *disk = NULL;
+	char slotA[MAX_GPT_NAME_SIZE + 1] = {0};
+	char slotB[MAX_GPT_NAME_SIZE + 1] = {0};
+	char active_guid[TYPE_GUID_SIZE + 1] = {0};
+	char inactive_guid[TYPE_GUID_SIZE + 1] = {0};
+	//Pointer to the partition entry of current 'A' partition
+	uint8_t *pentryA = NULL;
+	uint8_t *pentryA_bak = NULL;
+	//Pointer to partition entry of current 'B' partition
+	uint8_t *pentryB = NULL;
+	uint8_t *pentryB_bak = NULL;
+	struct stat st;
+	vector<string>::iterator partition_iterator;
+
+	for (partition_iterator = part_list.begin();
+			partition_iterator != part_list.end();
+			partition_iterator++) {
+		//Chop off the slot suffix from the partition name to
+		//make the string easier to work with.
+		string prefix = *partition_iterator;
+		if (prefix.size() < (strlen(AB_SLOT_A_SUFFIX) + 1)) {
+			ALOGE("Invalid partition name: %s", prefix.c_str());
+			goto error;
+		}
+		prefix.resize(prefix.size() - strlen(AB_SLOT_A_SUFFIX));
+		//Check if A/B versions of this ptn exist
+		snprintf(buf, sizeof(buf) - 1, "%s/%s%s", BOOT_DEV_DIR,
+				prefix.c_str(),
+				AB_SLOT_A_SUFFIX);
+		if (stat(buf, &st))
+			continue;
+		memset(buf, '\0', sizeof(buf));
+		snprintf(buf, sizeof(buf) - 1, "%s/%s%s", BOOT_DEV_DIR,
+				prefix.c_str(),
+				AB_SLOT_B_SUFFIX);
+		if (stat(buf, &st))
+			continue;
+		memset(slotA, 0, sizeof(slotA));
+		memset(slotB, 0, sizeof(slotA));
+		snprintf(slotA, sizeof(slotA) - 1, "%s%s", prefix.c_str(),
+				AB_SLOT_A_SUFFIX);
+		snprintf(slotB, sizeof(slotB) - 1,"%s%s", prefix.c_str(),
+				AB_SLOT_B_SUFFIX);
+		//Get the disk containing the partitions that were passed in.
+		//All partitions passed in must lie on the same disk.
+		if (!disk) {
+			disk = boot_ctl_get_disk_info(slotA);
+			if (!disk)
+				goto error;
+		}
+		//Get partition entry for slot A & B from the primary
+		//and backup tables.
+		pentryA = gpt_disk_get_pentry(disk, slotA, PRIMARY_GPT);
+		pentryA_bak = gpt_disk_get_pentry(disk, slotA, SECONDARY_GPT);
+		pentryB = gpt_disk_get_pentry(disk, slotB, PRIMARY_GPT);
+		pentryB_bak = gpt_disk_get_pentry(disk, slotB, SECONDARY_GPT);
+		if ( !pentryA || !pentryA_bak || !pentryB || !pentryB_bak) {
+			//None of these should be NULL since we have already
+			//checked for A & B versions earlier.
+			ALOGE("Slot pentries for %s not found.",
+					prefix.c_str());
+			goto error;
+		}
+		memset(active_guid, '\0', sizeof(active_guid));
+		memset(inactive_guid, '\0', sizeof(inactive_guid));
+		if (get_partition_attribute(slotA, ATTR_SLOT_ACTIVE) == 1) {
+			//A is the current active slot
+			memcpy((void*)active_guid, (const void*)pentryA,
+					TYPE_GUID_SIZE);
+			memcpy((void*)inactive_guid,(const void*)pentryB,
+					TYPE_GUID_SIZE);
+		} else if (get_partition_attribute(slotB,
+					ATTR_SLOT_ACTIVE) == 1) {
+			//B is the current active slot
+			memcpy((void*)active_guid, (const void*)pentryB,
+					TYPE_GUID_SIZE);
+			memcpy((void*)inactive_guid, (const void*)pentryA,
+					TYPE_GUID_SIZE);
+		} else {
+			ALOGE("Both A & B are inactive..Aborting");
+			goto error;
+		}
+		if (!strncmp(slot_suffix_arr[slot], AB_SLOT_A_SUFFIX,
+					strlen(AB_SLOT_A_SUFFIX))){
+			//Mark A as active in primary table
+			UPDATE_SLOT(pentryA, active_guid, SLOT_ACTIVE);
+			//Mark A as active in backup table
+			UPDATE_SLOT(pentryA_bak, active_guid, SLOT_ACTIVE);
+			//Mark B as inactive in primary table
+			UPDATE_SLOT(pentryB, inactive_guid, SLOT_INACTIVE);
+			//Mark B as inactive in backup table
+			UPDATE_SLOT(pentryB_bak, inactive_guid, SLOT_INACTIVE);
+		} else if (!strncmp(slot_suffix_arr[slot], AB_SLOT_B_SUFFIX,
+					strlen(AB_SLOT_B_SUFFIX))){
+			//Mark B as active in primary table
+			UPDATE_SLOT(pentryB, active_guid, SLOT_ACTIVE);
+			//Mark B as active in backup table
+			UPDATE_SLOT(pentryB_bak, active_guid, SLOT_ACTIVE);
+			//Mark A as inavtive in primary table
+			UPDATE_SLOT(pentryA, inactive_guid, SLOT_INACTIVE);
+			//Mark A as inactive in backup table
+			UPDATE_SLOT(pentryA_bak, inactive_guid, SLOT_INACTIVE);
+		} else {
+			//Something has gone terribly terribly wrong
+			ALOGE("%s: Unknown slot suffix!", __func__);
+			goto error;
+		}
+		if (disk) {
+			if (gpt_disk_update_crc(disk) != 0) {
+				ALOGE("%s: Failed to update gpt_disk crc",
+						__func__);
+				goto error;
+			}
+		}
+	}
+	//write updated content to disk
+	if (disk) {
+		if (gpt_disk_commit(disk)) {
+			ALOGE("Failed to commit disk entry");
+			goto error;
+		}
+		gpt_disk_free(disk);
+	}
+	return 0;
+
+error:
+	if (disk)
+		gpt_disk_free(disk);
+	return -1;
+}
+
+int set_active_boot_slot(struct boot_control_module *module, unsigned slot)
+{
+	map<string, vector<string>> ptn_map;
+	vector<string> ptn_vec;
+	const char ptn_list[][MAX_GPT_NAME_SIZE] = { AB_PTN_LIST };
+	uint32_t i;
+	int rc = -1;
+	int is_ufs = gpt_utils_is_ufs_device();
+	map<string, vector<string>>::iterator map_iter;
+	vector<string>::iterator string_iter;
+
+	if (boot_control_check_slot_sanity(module, slot)) {
+		ALOGE("%s: Bad arguments", __func__);
+		goto error;
+	}
+	//The partition list just contains prefixes(without the _a/_b) of the
+	//partitions that support A/B. In order to get the layout we need the
+	//actual names. To do this we append the slot suffix to every member
+	//in the list.
+	for (i = 0; i < ARRAY_SIZE(ptn_list); i++) {
+		//XBL is handled differrently for ufs devices so ignore it
+		if (is_ufs && !strncmp(ptn_list[i], PTN_XBL, strlen(PTN_XBL)))
+				continue;
+		//The partition list will be the list of _a partitions
+		string cur_ptn = ptn_list[i];
+		cur_ptn.append(AB_SLOT_A_SUFFIX);
+		ptn_vec.push_back(cur_ptn);
+
+	}
+	//The partition map gives us info in the following format:
+	// [path_to_block_device_1]--><partitions on device 1>
+	// [path_to_block_device_2]--><partitions on device 2>
+	// ...
+	// ...
+	// eg:
+	// [/dev/block/sdb]---><system, boot, rpm, tz,....>
+	if (gpt_utils_get_partition_map(ptn_vec, ptn_map)) {
+		ALOGE("%s: Failed to get partition map",
+				__func__);
+		goto error;
+	}
+	for (map_iter = ptn_map.begin(); map_iter != ptn_map.end(); map_iter++){
+		if (map_iter->second.size() < 1)
+			continue;
+		boot_ctl_set_active_slot_for_partitions(map_iter->second, slot);
+	}
+	if (is_ufs) {
+		if (!strncmp(slot_suffix_arr[slot], AB_SLOT_A_SUFFIX,
+					strlen(AB_SLOT_A_SUFFIX))){
+			//Set xbl_a as the boot lun
+			rc = gpt_utils_set_xbl_boot_partition(NORMAL_BOOT);
+		} else if (!strncmp(slot_suffix_arr[slot], AB_SLOT_B_SUFFIX,
+					strlen(AB_SLOT_B_SUFFIX))){
+			//Set xbl_b as the boot lun
+			rc = gpt_utils_set_xbl_boot_partition(BACKUP_BOOT);
+		} else {
+			//Something has gone terribly terribly wrong
+			ALOGE("%s: Unknown slot suffix!", __func__);
+			goto error;
+		}
+		if (rc) {
+			ALOGE("%s: Failed to switch xbl boot partition",
+					__func__);
+			goto error;
+		}
+	}
+	return 0;
+error:
+	return -1;
+}
+
+int set_slot_as_unbootable(struct boot_control_module *module, unsigned slot)
+{
+	if (boot_control_check_slot_sanity(module, slot) != 0) {
+		ALOGE("%s: Argument check failed", __func__);
+		goto error;
+	}
+	if (update_slot_attribute(slot_suffix_arr[slot],
+				ATTR_UNBOOTABLE)) {
+		goto error;
+	}
+	return 0;
+error:
+	ALOGE("%s: Failed to mark slot unbootable", __func__);
+	return -1;
+}
+
+int is_slot_bootable(struct boot_control_module *module, unsigned slot)
+{
+	int attr = 0;
+	char bootPartition[MAX_GPT_NAME_SIZE + 1] = {0};
+
+	if (boot_control_check_slot_sanity(module, slot) != 0) {
+		ALOGE("%s: Argument check failed", __func__);
+		goto error;
+	}
+	snprintf(bootPartition,
+			sizeof(bootPartition) - 1, "boot%s",
+			slot_suffix_arr[slot]);
+	attr = get_partition_attribute(bootPartition, ATTR_UNBOOTABLE);
+	if (attr >= 0)
+		return !attr;
+error:
+	return -1;
+}
+
+int is_slot_marked_successful(struct boot_control_module *module, unsigned slot)
+{
+	int attr = 0;
+	char bootPartition[MAX_GPT_NAME_SIZE + 1] = {0};
+
+	if (boot_control_check_slot_sanity(module, slot) != 0) {
+		ALOGE("%s: Argument check failed", __func__);
+		goto error;
+	}
+	snprintf(bootPartition,
+			sizeof(bootPartition) - 1,
+			"boot%s", slot_suffix_arr[slot]);
+	attr = get_partition_attribute(bootPartition, ATTR_BOOT_SUCCESSFUL);
+	if (attr >= 0)
+		return attr;
+error:
+	return -1;
+}
+
+static hw_module_methods_t boot_control_module_methods = {
+	.open = NULL,
+};
+
+boot_control_module_t HAL_MODULE_INFO_SYM = {
+	.common = {
+		.tag = HARDWARE_MODULE_TAG,
+		.module_api_version = 1,
+		.hal_api_version = 0,
+		.id = BOOT_CONTROL_HARDWARE_MODULE_ID,
+		.name = "Boot control HAL",
+		.author = "Code Aurora Forum",
+		.methods = &boot_control_module_methods,
+	},
+	.init = boot_control_init,
+	.getNumberSlots = get_number_slots,
+	.getCurrentSlot = get_current_slot,
+	.markBootSuccessful = mark_boot_successful,
+	.setActiveBootSlot = set_active_boot_slot,
+	.setSlotAsUnbootable = set_slot_as_unbootable,
+	.isSlotBootable = is_slot_bootable,
+	.getSuffix = get_suffix,
+	.isSlotMarkedSuccessful = is_slot_marked_successful,
+};
+#ifdef __cplusplus
+}
+#endif

+ 68 - 0
omni_PL2.mk

@@ -0,0 +1,68 @@
+#
+# Copyright 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.
+#
+
+# Release name
+PRODUCT_RELEASE_NAME := PL2
+
+$(call inherit-product, build/target/product/embedded.mk)
+
+# Inherit from our custom product configuration
+$(call inherit-product, vendor/omni/config/common.mk)
+
+# A/B updater
+AB_OTA_UPDATER := true
+
+AB_OTA_PARTITIONS += \
+    boot \
+    system
+
+AB_OTA_POSTINSTALL_CONFIG += \
+    RUN_POSTINSTALL_system=true \
+    POSTINSTALL_PATH_system=system/bin/otapreopt_script \
+    FILESYSTEM_TYPE_system=ext4 \
+    POSTINSTALL_OPTIONAL_system=true
+
+PRODUCT_PACKAGES += \
+    otapreopt_script \
+    update_engine \
+    update_engine_sideload \
+    update_verifier
+
+# The following modules are included in debuggable builds only.
+PRODUCT_PACKAGES_DEBUG += \
+    bootctl \
+    update_engine_client
+
+# Boot control HAL
+PRODUCT_PACKAGES += \
+    bootctrl.sdm660
+
+PRODUCT_STATIC_BOOT_CONTROL_HAL := \
+    bootctrl.sdm660 \
+    libcutils \
+    libgptutils \
+    libz
+
+# Time Zone data for recovery
+PRODUCT_COPY_FILES += \
+    system/timezone/output_data/iana/tzdata:recovery/root/system/usr/share/zoneinfo/tzdata
+
+## Device identifier. This must come after all inclusions
+PRODUCT_NAME := omni_PL2
+PRODUCT_DEVICE := PL2
+PRODUCT_MANUFACTURER := FIH
+PRODUCT_BRAND := Nokia
+PRODUCT_MODEL := Nokia 6.1

BIN
prebuilt/Image.gz-dtb


+ 1 - 0
recovery/Android.mk

@@ -0,0 +1 @@
+include $(all-subdir-makefiles)

+ 46 - 0
recovery/gpt-utils/Android.mk

@@ -0,0 +1,46 @@
+#
+# Copyright 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := gpt-utils.cpp
+ifeq ($(TARGET_COMPILE_WITH_MSM_KERNEL),true)
+LOCAL_C_INCLUDES += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
+LOCAL_ADDITIONAL_DEPENDENCIES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
+endif
+LOCAL_SHARED_LIBRARIES := liblog libz
+LOCAL_MODULE := libgptutils
+LOCAL_MODULE_OWNER := qti
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := gpt-utils.cpp
+ifeq ($(TARGET_COMPILE_WITH_MSM_KERNEL),true)
+LOCAL_C_INCLUDES += $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
+LOCAL_ADDITIONAL_DEPENDENCIES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
+endif
+LOCAL_SHARED_LIBRARIES += liblog libcutils libz
+LOCAL_EXPORT_HEADER_LIBRARY_HEADERS := libgptutils_headers
+LOCAL_MODULE := libgptutils
+LOCAL_MODULE_OWNER := qti
+LOCAL_PROPRIETARY_MODULE := true
+include $(BUILD_SHARED_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libgptutils_headers
+LOCAL_EXPORT_C_INCLUDE_DIRS:=$(LOCAL_PATH)
+include $(BUILD_HEADER_LIBRARY)

+ 1533 - 0
recovery/gpt-utils/gpt-utils.cpp

@@ -0,0 +1,1533 @@
+/*
+ * Copyright (c) 2013,2016, 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 _LARGEFILE64_SOURCE /* enable lseek64() */
+
+/******************************************************************************
+ * INCLUDE SECTION
+ ******************************************************************************/
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <scsi/ufs/ioctl.h>
+#include <scsi/ufs/ufs.h>
+#include <unistd.h>
+#include <linux/fs.h>
+#include <limits.h>
+#include <dirent.h>
+#include <linux/kernel.h>
+#include <asm/byteorder.h>
+#include <map>
+#include <vector>
+#include <string>
+#define LOG_TAG "gpt-utils"
+#include <cutils/log.h>
+#include <cutils/properties.h>
+#include "gpt-utils.h"
+#include <endian.h>
+#include <zlib.h>
+
+
+/******************************************************************************
+ * DEFINE SECTION
+ ******************************************************************************/
+#define BLK_DEV_FILE    "/dev/block/mmcblk0"
+/* list the names of the backed-up partitions to be swapped */
+/* extension used for the backup partitions - tzbak, abootbak, etc. */
+#define BAK_PTN_NAME_EXT    "bak"
+#define XBL_PRIMARY         "/dev/block/bootdevice/by-name/xbl"
+#define XBL_BACKUP          "/dev/block/bootdevice/by-name/xblbak"
+#define XBL_AB_PRIMARY      "/dev/block/bootdevice/by-name/xbl_a"
+#define XBL_AB_SECONDARY    "/dev/block/bootdevice/by-name/xbl_b"
+/* GPT defines */
+#define MAX_LUNS                    26
+//Size of the buffer that needs to be passed to the UFS ioctl
+#define UFS_ATTR_DATA_SIZE          32
+//This will allow us to get the root lun path from the path to the partition.
+//i.e: from /dev/block/sdaXXX get /dev/block/sda. The assumption here is that
+//the boot critical luns lie between sda to sdz which is acceptable because
+//only user added external disks,etc would lie beyond that limit which do not
+//contain partitions that interest us here.
+#define PATH_TRUNCATE_LOC (sizeof("/dev/block/sda") - 1)
+
+//From /dev/block/sda get just sda
+#define LUN_NAME_START_LOC (sizeof("/dev/block/") - 1)
+#define BOOT_LUN_A_ID 1
+#define BOOT_LUN_B_ID 2
+/******************************************************************************
+ * MACROS
+ ******************************************************************************/
+
+
+#define GET_4_BYTES(ptr)    ((uint32_t) *((uint8_t *)(ptr)) | \
+        ((uint32_t) *((uint8_t *)(ptr) + 1) << 8) | \
+        ((uint32_t) *((uint8_t *)(ptr) + 2) << 16) | \
+        ((uint32_t) *((uint8_t *)(ptr) + 3) << 24))
+
+#define GET_8_BYTES(ptr)    ((uint64_t) *((uint8_t *)(ptr)) | \
+        ((uint64_t) *((uint8_t *)(ptr) + 1) << 8) | \
+        ((uint64_t) *((uint8_t *)(ptr) + 2) << 16) | \
+        ((uint64_t) *((uint8_t *)(ptr) + 3) << 24) | \
+        ((uint64_t) *((uint8_t *)(ptr) + 4) << 32) | \
+        ((uint64_t) *((uint8_t *)(ptr) + 5) << 40) | \
+        ((uint64_t) *((uint8_t *)(ptr) + 6) << 48) | \
+        ((uint64_t) *((uint8_t *)(ptr) + 7) << 56))
+
+#define PUT_4_BYTES(ptr, y)   *((uint8_t *)(ptr)) = (y) & 0xff; \
+        *((uint8_t *)(ptr) + 1) = ((y) >> 8) & 0xff; \
+        *((uint8_t *)(ptr) + 2) = ((y) >> 16) & 0xff; \
+        *((uint8_t *)(ptr) + 3) = ((y) >> 24) & 0xff;
+
+/******************************************************************************
+ * TYPES
+ ******************************************************************************/
+using namespace std;
+enum gpt_state {
+    GPT_OK = 0,
+    GPT_BAD_SIGNATURE,
+    GPT_BAD_CRC
+};
+//List of LUN's containing boot critical images.
+//Required in the case of UFS devices
+struct update_data {
+     char lun_list[MAX_LUNS][PATH_MAX];
+     uint32_t num_valid_entries;
+};
+
+/******************************************************************************
+ * FUNCTIONS
+ ******************************************************************************/
+/**
+ *  ==========================================================================
+ *
+ *  \brief  Read/Write len bytes from/to block dev
+ *
+ *  \param [in] fd      block dev file descriptor (returned from open)
+ *  \param [in] rw      RW flag: 0 - read, != 0 - write
+ *  \param [in] offset  block dev offset [bytes] - RW start position
+ *  \param [in] buf     Pointer to the buffer containing the data
+ *  \param [in] len     RW size in bytes. Buf must be at least that big
+ *
+ *  \return  0 on success
+ *
+ *  ==========================================================================
+ */
+static int blk_rw(int fd, int rw, int64_t offset, uint8_t *buf, unsigned len)
+{
+    int r;
+
+    if (lseek64(fd, offset, SEEK_SET) < 0) {
+        fprintf(stderr, "block dev lseek64 %lld failed: %s\n", offset,
+                strerror(errno));
+        return -1;
+    }
+
+    if (rw)
+        r = write(fd, buf, len);
+    else
+        r = read(fd, buf, len);
+
+    if (r < 0)
+        fprintf(stderr, "block dev %s failed: %s\n", rw ? "write" : "read",
+                strerror(errno));
+    else
+        r = 0;
+
+    return r;
+}
+
+
+
+/**
+ *  ==========================================================================
+ *
+ *  \brief  Search within GPT for partition entry with the given name
+ *  or it's backup twin (name-bak).
+ *
+ *  \param [in] ptn_name        Partition name to seek
+ *  \param [in] pentries_start  Partition entries array start pointer
+ *  \param [in] pentries_end    Partition entries array end pointer
+ *  \param [in] pentry_size     Single partition entry size [bytes]
+ *
+ *  \return  First partition entry pointer that matches the name or NULL
+ *
+ *  ==========================================================================
+ */
+static uint8_t *gpt_pentry_seek(const char *ptn_name,
+                                const uint8_t *pentries_start,
+                                const uint8_t *pentries_end,
+                                uint32_t pentry_size)
+{
+    char *pentry_name;
+    unsigned len = strlen(ptn_name);
+
+    for (pentry_name = (char *) (pentries_start + PARTITION_NAME_OFFSET);
+         pentry_name < (char *) pentries_end; pentry_name += pentry_size) {
+        char name8[MAX_GPT_NAME_SIZE];
+        unsigned i;
+
+        /* Partition names in GPT are UTF-16 - ignoring UTF-16 2nd byte */
+        for (i = 0; i < sizeof(name8) / 2; i++)
+            name8[i] = pentry_name[i * 2];
+        if (!strncmp(ptn_name, name8, len))
+            if (name8[len] == 0 || !strcmp(&name8[len], BAK_PTN_NAME_EXT))
+                return (uint8_t *) (pentry_name - PARTITION_NAME_OFFSET);
+    }
+
+    return NULL;
+}
+
+
+
+/**
+ *  ==========================================================================
+ *
+ *  \brief  Swaps boot chain in GPT partition entries array
+ *
+ *  \param [in] pentries_start  Partition entries array start
+ *  \param [in] pentries_end    Partition entries array end
+ *  \param [in] pentry_size     Single partition entry size
+ *
+ *  \return  0 on success, 1 if no backup partitions found
+ *
+ *  ==========================================================================
+ */
+static int gpt_boot_chain_swap(const uint8_t *pentries_start,
+                                const uint8_t *pentries_end,
+                                uint32_t pentry_size)
+{
+    const char ptn_swap_list[][MAX_GPT_NAME_SIZE] = { PTN_SWAP_LIST };
+
+    int backup_not_found = 1;
+    unsigned i;
+
+    for (i = 0; i < ARRAY_SIZE(ptn_swap_list); i++) {
+        uint8_t *ptn_entry;
+        uint8_t *ptn_bak_entry;
+        uint8_t ptn_swap[PTN_ENTRY_SIZE];
+        //Skip the xbl partition on UFS devices. That is handled
+        //seperately.
+        if (gpt_utils_is_ufs_device() && !strncmp(ptn_swap_list[i],
+                                PTN_XBL,
+                                strlen(PTN_XBL)))
+            continue;
+
+        ptn_entry = gpt_pentry_seek(ptn_swap_list[i], pentries_start,
+                        pentries_end, pentry_size);
+        if (ptn_entry == NULL)
+            continue;
+
+        ptn_bak_entry = gpt_pentry_seek(ptn_swap_list[i],
+                        ptn_entry + pentry_size, pentries_end, pentry_size);
+        if (ptn_bak_entry == NULL) {
+            fprintf(stderr, "'%s' partition not backup - skip safe update\n",
+                    ptn_swap_list[i]);
+            continue;
+        }
+
+        /* swap primary <-> backup partition entries */
+        memcpy(ptn_swap, ptn_entry, PTN_ENTRY_SIZE);
+        memcpy(ptn_entry, ptn_bak_entry, PTN_ENTRY_SIZE);
+        memcpy(ptn_bak_entry, ptn_swap, PTN_ENTRY_SIZE);
+        backup_not_found = 0;
+    }
+
+    return backup_not_found;
+}
+
+
+
+/**
+ *  ==========================================================================
+ *
+ *  \brief  Sets secondary GPT boot chain
+ *
+ *  \param [in] fd    block dev file descriptor
+ *  \param [in] boot  Boot chain to switch to
+ *
+ *  \return  0 on success
+ *
+ *  ==========================================================================
+ */
+static int gpt2_set_boot_chain(int fd, enum boot_chain boot)
+{
+    int64_t  gpt2_header_offset;
+    uint64_t pentries_start_offset;
+    uint32_t gpt_header_size;
+    uint32_t pentry_size;
+    uint32_t pentries_array_size;
+
+    uint8_t *gpt_header = NULL;
+    uint8_t  *pentries = NULL;
+    uint32_t crc;
+    uint32_t blk_size = 0;
+    int r;
+
+    if (ioctl(fd, BLKSSZGET, &blk_size) != 0) {
+            fprintf(stderr, "Failed to get GPT device block size: %s\n",
+                            strerror(errno));
+            r = -1;
+            goto EXIT;
+    }
+    gpt_header = (uint8_t*)malloc(blk_size);
+    if (!gpt_header) {
+            fprintf(stderr, "Failed to allocate memory to hold GPT block\n");
+            r = -1;
+            goto EXIT;
+    }
+    gpt2_header_offset = lseek64(fd, 0, SEEK_END) - blk_size;
+    if (gpt2_header_offset < 0) {
+        fprintf(stderr, "Getting secondary GPT header offset failed: %s\n",
+                strerror(errno));
+        r = -1;
+        goto EXIT;
+    }
+
+    /* Read primary GPT header from block dev */
+    r = blk_rw(fd, 0, blk_size, gpt_header, blk_size);
+
+    if (r) {
+            fprintf(stderr, "Failed to read primary GPT header from blk dev\n");
+            goto EXIT;
+    }
+    pentries_start_offset =
+        GET_8_BYTES(gpt_header + PENTRIES_OFFSET) * blk_size;
+    pentry_size = GET_4_BYTES(gpt_header + PENTRY_SIZE_OFFSET);
+    pentries_array_size =
+        GET_4_BYTES(gpt_header + PARTITION_COUNT_OFFSET) * pentry_size;
+
+    pentries = (uint8_t *) calloc(1, pentries_array_size);
+    if (pentries == NULL) {
+        fprintf(stderr,
+                    "Failed to alloc memory for GPT partition entries array\n");
+        r = -1;
+        goto EXIT;
+    }
+    /* Read primary GPT partititon entries array from block dev */
+    r = blk_rw(fd, 0, pentries_start_offset, pentries, pentries_array_size);
+    if (r)
+        goto EXIT;
+
+    crc = crc32(0, pentries, pentries_array_size);
+    if (GET_4_BYTES(gpt_header + PARTITION_CRC_OFFSET) != crc) {
+        fprintf(stderr, "Primary GPT partition entries array CRC invalid\n");
+        r = -1;
+        goto EXIT;
+    }
+
+    /* Read secondary GPT header from block dev */
+    r = blk_rw(fd, 0, gpt2_header_offset, gpt_header, blk_size);
+    if (r)
+        goto EXIT;
+
+    gpt_header_size = GET_4_BYTES(gpt_header + HEADER_SIZE_OFFSET);
+    pentries_start_offset =
+        GET_8_BYTES(gpt_header + PENTRIES_OFFSET) * blk_size;
+
+    if (boot == BACKUP_BOOT) {
+        r = gpt_boot_chain_swap(pentries, pentries + pentries_array_size,
+                                pentry_size);
+        if (r)
+            goto EXIT;
+    }
+
+    crc = crc32(0, pentries, pentries_array_size);
+    PUT_4_BYTES(gpt_header + PARTITION_CRC_OFFSET, crc);
+
+    /* header CRC is calculated with this field cleared */
+    PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, 0);
+    crc = crc32(0, gpt_header, gpt_header_size);
+    PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, crc);
+
+    /* Write the modified GPT header back to block dev */
+    r = blk_rw(fd, 1, gpt2_header_offset, gpt_header, blk_size);
+    if (!r)
+        /* Write the modified GPT partititon entries array back to block dev */
+        r = blk_rw(fd, 1, pentries_start_offset, pentries,
+                    pentries_array_size);
+
+EXIT:
+    if(gpt_header)
+            free(gpt_header);
+    if (pentries)
+            free(pentries);
+    return r;
+}
+
+/**
+ *  ==========================================================================
+ *
+ *  \brief  Checks GPT state (header signature and CRC)
+ *
+ *  \param [in] fd      block dev file descriptor
+ *  \param [in] gpt     GPT header to be checked
+ *  \param [out] state  GPT header state
+ *
+ *  \return  0 on success
+ *
+ *  ==========================================================================
+ */
+static int gpt_get_state(int fd, enum gpt_instance gpt, enum gpt_state *state)
+{
+    int64_t gpt_header_offset;
+    uint32_t gpt_header_size;
+    uint8_t  *gpt_header = NULL;
+    uint32_t crc;
+    uint32_t blk_size = 0;
+
+    *state = GPT_OK;
+
+    if (ioctl(fd, BLKSSZGET, &blk_size) != 0) {
+            fprintf(stderr, "Failed to get GPT device block size: %s\n",
+                            strerror(errno));
+            goto error;
+    }
+    gpt_header = (uint8_t*)malloc(blk_size);
+    if (!gpt_header) {
+            fprintf(stderr, "gpt_get_state:Failed to alloc memory for header\n");
+            goto error;
+    }
+    if (gpt == PRIMARY_GPT)
+        gpt_header_offset = blk_size;
+    else {
+        gpt_header_offset = lseek64(fd, 0, SEEK_END) - blk_size;
+        if (gpt_header_offset < 0) {
+            fprintf(stderr, "gpt_get_state:Seek to end of GPT part fail\n");
+            goto error;
+        }
+    }
+
+    if (blk_rw(fd, 0, gpt_header_offset, gpt_header, blk_size)) {
+        fprintf(stderr, "gpt_get_state: blk_rw failed\n");
+        goto error;
+    }
+    if (memcmp(gpt_header, GPT_SIGNATURE, sizeof(GPT_SIGNATURE)))
+        *state = GPT_BAD_SIGNATURE;
+    gpt_header_size = GET_4_BYTES(gpt_header + HEADER_SIZE_OFFSET);
+
+    crc = GET_4_BYTES(gpt_header + HEADER_CRC_OFFSET);
+    /* header CRC is calculated with this field cleared */
+    PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, 0);
+    if (crc32(0, gpt_header, gpt_header_size) != crc)
+        *state = GPT_BAD_CRC;
+    free(gpt_header);
+    return 0;
+error:
+    if (gpt_header)
+            free(gpt_header);
+    return -1;
+}
+
+
+
+/**
+ *  ==========================================================================
+ *
+ *  \brief  Sets GPT header state (used to corrupt and fix GPT signature)
+ *
+ *  \param [in] fd     block dev file descriptor
+ *  \param [in] gpt    GPT header to be checked
+ *  \param [in] state  GPT header state to set (GPT_OK or GPT_BAD_SIGNATURE)
+ *
+ *  \return  0 on success
+ *
+ *  ==========================================================================
+ */
+static int gpt_set_state(int fd, enum gpt_instance gpt, enum gpt_state state)
+{
+    int64_t gpt_header_offset;
+    uint32_t gpt_header_size;
+    uint8_t  *gpt_header = NULL;
+    uint32_t crc;
+    uint32_t blk_size = 0;
+
+    if (ioctl(fd, BLKSSZGET, &blk_size) != 0) {
+            fprintf(stderr, "Failed to get GPT device block size: %s\n",
+                            strerror(errno));
+            goto error;
+    }
+    gpt_header = (uint8_t*)malloc(blk_size);
+    if (!gpt_header) {
+            fprintf(stderr, "Failed to alloc memory for gpt header\n");
+            goto error;
+    }
+    if (gpt == PRIMARY_GPT)
+        gpt_header_offset = blk_size;
+    else {
+        gpt_header_offset = lseek64(fd, 0, SEEK_END) - blk_size;
+        if (gpt_header_offset < 0) {
+            fprintf(stderr, "Failed to seek to end of GPT device\n");
+            goto error;
+        }
+    }
+    if (blk_rw(fd, 0, gpt_header_offset, gpt_header, blk_size)) {
+        fprintf(stderr, "Failed to r/w gpt header\n");
+        goto error;
+    }
+    if (state == GPT_OK)
+        memcpy(gpt_header, GPT_SIGNATURE, sizeof(GPT_SIGNATURE));
+    else if (state == GPT_BAD_SIGNATURE)
+        *gpt_header = 0;
+    else {
+        fprintf(stderr, "gpt_set_state: Invalid state\n");
+        goto error;
+    }
+
+    gpt_header_size = GET_4_BYTES(gpt_header + HEADER_SIZE_OFFSET);
+
+    /* header CRC is calculated with this field cleared */
+    PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, 0);
+    crc = crc32(0, gpt_header, gpt_header_size);
+    PUT_4_BYTES(gpt_header + HEADER_CRC_OFFSET, crc);
+
+    if (blk_rw(fd, 1, gpt_header_offset, gpt_header, blk_size)) {
+        fprintf(stderr, "gpt_set_state: blk write failed\n");
+        goto error;
+    }
+    return 0;
+error:
+    if(gpt_header)
+           free(gpt_header);
+    return -1;
+}
+
+int get_scsi_node_from_bootdevice(const char *bootdev_path,
+                char *sg_node_path,
+                size_t buf_size)
+{
+        char sg_dir_path[PATH_MAX] = {0};
+        char real_path[PATH_MAX] = {0};
+        DIR *scsi_dir = NULL;
+        struct dirent *de;
+        int node_found = 0;
+        if (!bootdev_path || !sg_node_path) {
+                fprintf(stderr, "%s : invalid argument\n",
+                                 __func__);
+                goto error;
+        }
+        if (readlink(bootdev_path, real_path, sizeof(real_path) - 1) < 0) {
+                        fprintf(stderr, "failed to resolve link for %s(%s)\n",
+                                        bootdev_path,
+                                        strerror(errno));
+                        goto error;
+        }
+        if(strlen(real_path) < PATH_TRUNCATE_LOC + 1){
+            fprintf(stderr, "Unrecognized path :%s:\n",
+                           real_path);
+            goto error;
+        }
+        //For the safe side in case there are additional partitions on
+        //the XBL lun we truncate the name.
+        real_path[PATH_TRUNCATE_LOC] = '\0';
+        if(strlen(real_path) < LUN_NAME_START_LOC + 1){
+            fprintf(stderr, "Unrecognized truncated path :%s:\n",
+                           real_path);
+            goto error;
+        }
+        //This will give us /dev/block/sdb/device/scsi_generic
+        //which contains a file sgY whose name gives us the path
+        //to /dev/sgY which we return
+        snprintf(sg_dir_path, sizeof(sg_dir_path) - 1,
+                        "/sys/block/%s/device/scsi_generic",
+                        &real_path[LUN_NAME_START_LOC]);
+        scsi_dir = opendir(sg_dir_path);
+        if (!scsi_dir) {
+                fprintf(stderr, "%s : Failed to open %s(%s)\n",
+                                __func__,
+                                sg_dir_path,
+                                strerror(errno));
+                goto error;
+        }
+        while((de = readdir(scsi_dir))) {
+                if (de->d_name[0] == '.')
+                        continue;
+                else if (!strncmp(de->d_name, "sg", 2)) {
+                          snprintf(sg_node_path,
+                                        buf_size -1,
+                                        "/dev/%s",
+                                        de->d_name);
+                          fprintf(stderr, "%s:scsi generic node is :%s:\n",
+                                          __func__,
+                                          sg_node_path);
+                          node_found = 1;
+                          break;
+                }
+        }
+        if(!node_found) {
+                fprintf(stderr,"%s: Unable to locate scsi generic node\n",
+                               __func__);
+                goto error;
+        }
+        closedir(scsi_dir);
+        return 0;
+error:
+        if (scsi_dir)
+                closedir(scsi_dir);
+        return -1;
+}
+
+int set_boot_lun(char *sg_dev, uint8_t boot_lun_id)
+{
+        int fd = -1;
+        int rc;
+        struct ufs_ioctl_query_data *data = NULL;
+        size_t ioctl_data_size = sizeof(struct ufs_ioctl_query_data) + UFS_ATTR_DATA_SIZE;
+
+        data = (struct ufs_ioctl_query_data*)malloc(ioctl_data_size);
+        if (!data) {
+                fprintf(stderr, "%s: Failed to alloc query data struct\n",
+                                __func__);
+                goto error;
+        }
+        memset(data, 0, ioctl_data_size);
+        data->opcode = UPIU_QUERY_OPCODE_WRITE_ATTR;
+        data->idn = QUERY_ATTR_IDN_BOOT_LU_EN;
+        data->buf_size = UFS_ATTR_DATA_SIZE;
+        data->buffer[0] = boot_lun_id;
+        fd = open(sg_dev, O_RDWR);
+        if (fd < 0) {
+                fprintf(stderr, "%s: Failed to open %s(%s)\n",
+                                __func__,
+                                sg_dev,
+                                strerror(errno));
+                goto error;
+        }
+        rc = ioctl(fd, UFS_IOCTL_QUERY, data);
+        if (rc) {
+                fprintf(stderr, "%s: UFS query ioctl failed(%s)\n",
+                                __func__,
+                                strerror(errno));
+                goto error;
+        }
+        close(fd);
+        free(data);
+        return 0;
+error:
+        if (fd >= 0)
+                close(fd);
+        if (data)
+                free(data);
+        return -1;
+}
+
+//Swtich betwieen using either the primary or the backup
+//boot LUN for boot. This is required since UFS boot partitions
+//cannot have a backup GPT which is what we use for failsafe
+//updates of the other 'critical' partitions. This function will
+//not be invoked for emmc targets and on UFS targets is only required
+//to be invoked for XBL.
+//
+//The algorithm to do this is as follows:
+//- Find the real block device(eg: /dev/block/sdb) that corresponds
+//  to the /dev/block/bootdevice/by-name/xbl(bak) symlink
+//
+//- Once we have the block device 'node' name(sdb in the above example)
+//  use this node to to locate the scsi generic device that represents
+//  it by checking the file /sys/block/sdb/device/scsi_generic/sgY
+//
+//- Once we locate sgY we call the query ioctl on /dev/sgy to switch
+//the boot lun to either LUNA or LUNB
+int gpt_utils_set_xbl_boot_partition(enum boot_chain chain)
+{
+        struct stat st;
+        ///sys/block/sdX/device/scsi_generic/
+        char sg_dev_node[PATH_MAX] = {0};
+        uint8_t boot_lun_id = 0;
+        const char *boot_dev = NULL;
+
+        if (chain == BACKUP_BOOT) {
+                boot_lun_id = BOOT_LUN_B_ID;
+                if (!stat(XBL_BACKUP, &st))
+                        boot_dev = XBL_BACKUP;
+                else if (!stat(XBL_AB_SECONDARY, &st))
+                        boot_dev = XBL_AB_SECONDARY;
+                else {
+                        fprintf(stderr, "%s: Failed to locate secondary xbl\n",
+                                        __func__);
+                        goto error;
+                }
+        } else if (chain == NORMAL_BOOT) {
+                boot_lun_id = BOOT_LUN_A_ID;
+                if (!stat(XBL_PRIMARY, &st))
+                        boot_dev = XBL_PRIMARY;
+                else if (!stat(XBL_AB_PRIMARY, &st))
+                        boot_dev = XBL_AB_PRIMARY;
+                else {
+                        fprintf(stderr, "%s: Failed to locate primary xbl\n",
+                                        __func__);
+                        goto error;
+                }
+        } else {
+                fprintf(stderr, "%s: Invalid boot chain id\n", __func__);
+                goto error;
+        }
+        //We need either both xbl and xblbak or both xbl_a and xbl_b to exist at
+        //the same time. If not the current configuration is invalid.
+        if((stat(XBL_PRIMARY, &st) ||
+                                stat(XBL_BACKUP, &st)) &&
+                        (stat(XBL_AB_PRIMARY, &st) ||
+                         stat(XBL_AB_SECONDARY, &st))) {
+                fprintf(stderr, "%s:primary/secondary XBL prt not found(%s)\n",
+                                __func__,
+                                strerror(errno));
+                goto error;
+        }
+        fprintf(stderr, "%s: setting %s lun as boot lun\n",
+                        __func__,
+                        boot_dev);
+        if (get_scsi_node_from_bootdevice(boot_dev,
+                                sg_dev_node,
+                                sizeof(sg_dev_node))) {
+                fprintf(stderr, "%s: Failed to get scsi node path for xblbak\n",
+                                __func__);
+                goto error;
+        }
+        if (set_boot_lun(sg_dev_node, boot_lun_id)) {
+                fprintf(stderr, "%s: Failed to set xblbak as boot partition\n",
+                                __func__);
+                goto error;
+        }
+        return 0;
+error:
+        return -1;
+}
+
+int gpt_utils_is_ufs_device()
+{
+    char bootdevice[PROPERTY_VALUE_MAX] = {0};
+    property_get("ro.boot.bootdevice", bootdevice, "N/A");
+    if (strlen(bootdevice) < strlen(".ufshc") + 1)
+        return 0;
+    return (!strncmp(&bootdevice[strlen(bootdevice) - strlen(".ufshc")],
+                            ".ufshc",
+                            sizeof(".ufshc")));
+}
+//dev_path is the path to the block device that contains the GPT image that
+//needs to be updated. This would be the device which holds one or more critical
+//boot partitions and their backups. In the case of EMMC this function would
+//be invoked only once on /dev/block/mmcblk1 since it holds the GPT image
+//containing all the partitions For UFS devices it could potentially be
+//invoked multiple times, once for each LUN containing critical image(s) and
+//their backups
+int prepare_partitions(enum boot_update_stage stage, const char *dev_path)
+{
+    int r = 0;
+    int fd = -1;
+    int is_ufs = gpt_utils_is_ufs_device();
+    enum gpt_state gpt_prim, gpt_second;
+    enum boot_update_stage internal_stage;
+    struct stat xbl_partition_stat;
+    struct stat ufs_dir_stat;
+
+    if (!dev_path) {
+        fprintf(stderr, "%s: Invalid dev_path\n",
+                        __func__);
+        r = -1;
+        goto EXIT;
+    }
+    fd = open(dev_path, O_RDWR);
+    if (fd < 0) {
+        fprintf(stderr, "%s: Opening '%s' failed: %s\n",
+                        __func__,
+                       BLK_DEV_FILE,
+                       strerror(errno));
+        r = -1;
+        goto EXIT;
+    }
+    r = gpt_get_state(fd, PRIMARY_GPT, &gpt_prim) ||
+        gpt_get_state(fd, SECONDARY_GPT, &gpt_second);
+    if (r) {
+        fprintf(stderr, "%s: Getting GPT headers state failed\n",
+                        __func__);
+        goto EXIT;
+    }
+
+    /* These 2 combinations are unexpected and unacceptable */
+    if (gpt_prim == GPT_BAD_CRC || gpt_second == GPT_BAD_CRC) {
+        fprintf(stderr, "%s: GPT headers CRC corruption detected, aborting\n",
+                        __func__);
+        r = -1;
+        goto EXIT;
+    }
+    if (gpt_prim == GPT_BAD_SIGNATURE && gpt_second == GPT_BAD_SIGNATURE) {
+        fprintf(stderr, "%s: Both GPT headers corrupted, aborting\n",
+                        __func__);
+        r = -1;
+        goto EXIT;
+    }
+
+    /* Check internal update stage according GPT headers' state */
+    if (gpt_prim == GPT_OK && gpt_second == GPT_OK)
+        internal_stage = UPDATE_MAIN;
+    else if (gpt_prim == GPT_BAD_SIGNATURE)
+        internal_stage = UPDATE_BACKUP;
+    else if (gpt_second == GPT_BAD_SIGNATURE)
+        internal_stage = UPDATE_FINALIZE;
+    else {
+        fprintf(stderr, "%s: Abnormal GPTs state: primary (%d), secondary (%d), "
+                "aborting\n", __func__, gpt_prim, gpt_second);
+        r = -1;
+        goto EXIT;
+    }
+
+    /* Stage already set - ready for update, exitting */
+    if ((int) stage == (int) internal_stage - 1)
+        goto EXIT;
+    /* Unexpected stage given */
+    if (stage != internal_stage) {
+        r = -1;
+        goto EXIT;
+    }
+
+    switch (stage) {
+    case UPDATE_MAIN:
+            if (is_ufs) {
+                if(stat(XBL_PRIMARY, &xbl_partition_stat)||
+                                stat(XBL_BACKUP, &xbl_partition_stat)){
+                        //Non fatal error. Just means this target does not
+                        //use XBL but relies on sbl whose update is handled
+                        //by the normal methods.
+                        fprintf(stderr, "%s: xbl part not found(%s).Assuming sbl in use\n",
+                                        __func__,
+                                        strerror(errno));
+                } else {
+                        //Switch the boot lun so that backup boot LUN is used
+                        r = gpt_utils_set_xbl_boot_partition(BACKUP_BOOT);
+                        if(r){
+                                fprintf(stderr, "%s: Failed to set xbl backup partition as boot\n",
+                                                __func__);
+                                goto EXIT;
+                        }
+                }
+        }
+        //Fix up the backup GPT table so that it actually points to
+        //the backup copy of the boot critical images
+        fprintf(stderr, "%s: Preparing for primary partition update\n",
+                        __func__);
+        r = gpt2_set_boot_chain(fd, BACKUP_BOOT);
+        if (r) {
+            if (r < 0)
+                fprintf(stderr,
+                                "%s: Setting secondary GPT to backup boot failed\n",
+                                __func__);
+            /* No backup partitions - do not corrupt GPT, do not flag error */
+            else
+                r = 0;
+            goto EXIT;
+        }
+        //corrupt the primary GPT so that the backup(which now points to
+        //the backup boot partitions is used)
+        r = gpt_set_state(fd, PRIMARY_GPT, GPT_BAD_SIGNATURE);
+        if (r) {
+            fprintf(stderr, "%s: Corrupting primary GPT header failed\n",
+                            __func__);
+            goto EXIT;
+        }
+        break;
+    case UPDATE_BACKUP:
+        if (is_ufs) {
+                if(stat(XBL_PRIMARY, &xbl_partition_stat)||
+                                stat(XBL_BACKUP, &xbl_partition_stat)){
+                        //Non fatal error. Just means this target does not
+                        //use XBL but relies on sbl whose update is handled
+                        //by the normal methods.
+                        fprintf(stderr, "%s: xbl part not found(%s).Assuming sbl in use\n",
+                                        __func__,
+                                        strerror(errno));
+                } else {
+                        //Switch the boot lun so that backup boot LUN is used
+                        r = gpt_utils_set_xbl_boot_partition(NORMAL_BOOT);
+                        if(r) {
+                                fprintf(stderr, "%s: Failed to set xbl backup partition as boot\n",
+                                                __func__);
+                                goto EXIT;
+                        }
+                }
+        }
+        //Fix the primary GPT header so that is used
+        fprintf(stderr, "%s: Preparing for backup partition update\n",
+                        __func__);
+        r = gpt_set_state(fd, PRIMARY_GPT, GPT_OK);
+        if (r) {
+            fprintf(stderr, "%s: Fixing primary GPT header failed\n",
+                             __func__);
+            goto EXIT;
+        }
+        //Corrupt the scondary GPT header
+        r = gpt_set_state(fd, SECONDARY_GPT, GPT_BAD_SIGNATURE);
+        if (r) {
+            fprintf(stderr, "%s: Corrupting secondary GPT header failed\n",
+                            __func__);
+            goto EXIT;
+        }
+        break;
+    case UPDATE_FINALIZE:
+        //Undo the changes we had made in the UPDATE_MAIN stage so that the
+        //primary/backup GPT headers once again point to the same set of
+        //partitions
+        fprintf(stderr, "%s: Finalizing partitions\n",
+                        __func__);
+        r = gpt2_set_boot_chain(fd, NORMAL_BOOT);
+        if (r < 0) {
+            fprintf(stderr, "%s: Setting secondary GPT to normal boot failed\n",
+                            __func__);
+            goto EXIT;
+        }
+
+        r = gpt_set_state(fd, SECONDARY_GPT, GPT_OK);
+        if (r) {
+            fprintf(stderr, "%s: Fixing secondary GPT header failed\n",
+                            __func__);
+            goto EXIT;
+        }
+        break;
+    default:;
+    }
+
+EXIT:
+    if (fd >= 0) {
+       fsync(fd);
+       close(fd);
+    }
+    return r;
+}
+
+int add_lun_to_update_list(char *lun_path, struct update_data *dat)
+{
+        uint32_t i = 0;
+        struct stat st;
+        if (!lun_path || !dat){
+                fprintf(stderr, "%s: Invalid data",
+                                __func__);
+                return -1;
+        }
+        if (stat(lun_path, &st)) {
+                fprintf(stderr, "%s: Unable to access %s. Skipping adding to list",
+                                __func__,
+                                lun_path);
+                return -1;
+        }
+        if (dat->num_valid_entries == 0) {
+                fprintf(stderr, "%s: Copying %s into lun_list[%d]\n",
+                                __func__,
+                                lun_path,
+                                i);
+                strlcpy(dat->lun_list[0], lun_path,
+                                PATH_MAX * sizeof(char));
+                dat->num_valid_entries = 1;
+        } else {
+                for (i = 0; (i < dat->num_valid_entries) &&
+                                (dat->num_valid_entries < MAX_LUNS - 1); i++) {
+                        //Check if the current LUN is not already part
+                        //of the lun list
+                        if (!strncmp(lun_path,dat->lun_list[i],
+                                                strlen(dat->lun_list[i]))) {
+                                //LUN already in list..Return
+                                return 0;
+                        }
+                }
+                fprintf(stderr, "%s: Copying %s into lun_list[%d]\n",
+                                __func__,
+                                lun_path,
+                                dat->num_valid_entries);
+                //Add LUN path lun list
+                strlcpy(dat->lun_list[dat->num_valid_entries], lun_path,
+                                PATH_MAX * sizeof(char));
+                dat->num_valid_entries++;
+        }
+        return 0;
+}
+
+int prepare_boot_update(enum boot_update_stage stage)
+{
+        int r, fd;
+        int is_ufs = gpt_utils_is_ufs_device();
+        struct stat ufs_dir_stat;
+        struct update_data data;
+        int rcode = 0;
+        uint32_t i = 0;
+        int is_error = 0;
+        const char ptn_swap_list[][MAX_GPT_NAME_SIZE] = { PTN_SWAP_LIST };
+        //Holds /dev/block/bootdevice/by-name/*bak entry
+        char buf[PATH_MAX] = {0};
+        //Holds the resolved path of the symlink stored in buf
+        char real_path[PATH_MAX] = {0};
+
+        if (!is_ufs) {
+                //emmc device. Just pass in path to mmcblk0
+                return prepare_partitions(stage, BLK_DEV_FILE);
+        } else {
+                //Now we need to find the list of LUNs over
+                //which the boot critical images are spread
+                //and set them up for failsafe updates.To do
+                //this we find out where the symlinks for the
+                //each of the paths under
+                ///dev/block/bootdevice/by-name/PTN_SWAP_LIST
+                //actually point to.
+                fprintf(stderr, "%s: Running on a UFS device\n",
+                                __func__);
+                memset(&data, '\0', sizeof(struct update_data));
+                for (i=0; i < ARRAY_SIZE(ptn_swap_list); i++) {
+                        //XBL on UFS does not follow the convention
+                        //of being loaded based on well known GUID'S.
+                        //We take care of switching the UFS boot LUN
+                        //explicitly later on.
+                        if (!strncmp(ptn_swap_list[i],
+                                                PTN_XBL,
+                                                strlen(PTN_XBL)))
+                                continue;
+                        snprintf(buf, sizeof(buf),
+                                        "%s/%sbak",
+                                        BOOT_DEV_DIR,
+                                        ptn_swap_list[i]);
+                        if (stat(buf, &ufs_dir_stat)) {
+                                continue;
+                        }
+                        if (readlink(buf, real_path, sizeof(real_path) - 1) < 0)
+                        {
+                                fprintf(stderr, "%s: readlink error. Skipping %s",
+                                                __func__,
+                                                strerror(errno));
+                        } else {
+                              if(strlen(real_path) < PATH_TRUNCATE_LOC + 1){
+                                    fprintf(stderr, "Unknown path.Skipping :%s:\n",
+                                                real_path);
+                                } else {
+                                    real_path[PATH_TRUNCATE_LOC] = '\0';
+                                    add_lun_to_update_list(real_path, &data);
+                                }
+                        }
+                        memset(buf, '\0', sizeof(buf));
+                        memset(real_path, '\0', sizeof(real_path));
+                }
+                for (i=0; i < data.num_valid_entries; i++) {
+                        fprintf(stderr, "%s: Preparing %s for update stage %d\n",
+                                        __func__,
+                                        data.lun_list[i],
+                                        stage);
+                        rcode = prepare_partitions(stage, data.lun_list[i]);
+                        if (rcode != 0)
+                        {
+                                fprintf(stderr, "%s: Failed to prepare %s.Continuing..\n",
+                                                __func__,
+                                                data.lun_list[i]);
+                                is_error = 1;
+                        }
+                }
+        }
+        if (is_error)
+                return -1;
+        return 0;
+}
+
+//Given a parttion name(eg: rpm) get the path to the block device that
+//represents the GPT disk the partition resides on. In the case of emmc it
+//would be the default emmc dev(/dev/block/mmcblk0). In the case of UFS we look
+//through the /dev/block/bootdevice/by-name/ tree for partname, and resolve
+//the path to the LUN from there.
+static int get_dev_path_from_partition_name(const char *partname,
+                char *buf,
+                size_t buflen)
+{
+        struct stat st;
+        char path[PATH_MAX] = {0};
+        if (!partname || !buf || buflen < ((PATH_TRUNCATE_LOC) + 1)) {
+                ALOGE("%s: Invalid argument", __func__);
+                goto error;
+        }
+        if (gpt_utils_is_ufs_device()) {
+                //Need to find the lun that holds partition partname
+                snprintf(path, sizeof(path),
+                                "%s/%s",
+                                BOOT_DEV_DIR,
+                                partname);
+                if (stat(path, &st)) {
+                        goto error;
+                }
+                if (readlink(path, buf, buflen) < 0)
+                {
+                        goto error;
+                } else {
+                        buf[PATH_TRUNCATE_LOC] = '\0';
+                }
+        } else {
+                snprintf(buf, buflen, BLK_DEV_FILE);
+        }
+        return 0;
+
+error:
+        return -1;
+}
+
+int gpt_utils_get_partition_map(vector<string>& ptn_list,
+                map<string, vector<string>>& partition_map) {
+        char devpath[PATH_MAX] = {'\0'};
+        map<string, vector<string>>::iterator it;
+        if (ptn_list.size() < 1) {
+                fprintf(stderr, "%s: Invalid ptn list\n", __func__);
+                goto error;
+        }
+        //Go through the passed in list
+        for (uint32_t i = 0; i < ptn_list.size(); i++)
+        {
+                //Key in the map is the path to the device that holds the
+                //partition
+                if (get_dev_path_from_partition_name(ptn_list[i].c_str(),
+                                devpath,
+                                sizeof(devpath))) {
+                        //Not necessarily an error. The partition may just
+                        //not be present.
+                        continue;
+                }
+                string path = devpath;
+                it = partition_map.find(path);
+                if (it != partition_map.end()) {
+                        it->second.push_back(ptn_list[i]);
+                } else {
+                        vector<string> str_vec;
+                        str_vec.push_back( ptn_list[i]);
+                        partition_map.insert(pair<string, vector<string>>
+                                        (path, str_vec));
+                }
+                memset(devpath, '\0', sizeof(devpath));
+        }
+        return 0;
+error:
+        return -1;
+}
+
+//Get the block size of the disk represented by decsriptor fd
+static uint32_t gpt_get_block_size(int fd)
+{
+        uint32_t block_size = 0;
+        if (fd < 0) {
+                ALOGE("%s: invalid descriptor",
+                                __func__);
+                goto error;
+        }
+        if (ioctl(fd, BLKSSZGET, &block_size) != 0) {
+                ALOGE("%s: Failed to get GPT dev block size : %s",
+                                __func__,
+                                strerror(errno));
+                goto error;
+        }
+        return block_size;
+error:
+        return 0;
+}
+
+//Write the GPT header present in the passed in buffer back to the
+//disk represented by fd
+static int gpt_set_header(uint8_t *gpt_header, int fd,
+                enum gpt_instance instance)
+{
+        uint32_t block_size = 0;
+        off64_t gpt_header_offset = 0;
+        if (!gpt_header || fd < 0) {
+                ALOGE("%s: Invalid arguments",
+                                __func__);
+                goto error;
+        }
+        block_size = gpt_get_block_size(fd);
+        if (block_size == 0) {
+                ALOGE("%s: Failed to get block size", __func__);
+                goto error;
+        }
+        if (instance == PRIMARY_GPT)
+                gpt_header_offset = block_size;
+        else
+                gpt_header_offset = lseek64(fd, 0, SEEK_END) - block_size;
+        if (gpt_header_offset <= 0) {
+                ALOGE("%s: Failed to get gpt header offset",__func__);
+                goto error;
+        }
+        if (blk_rw(fd, 1, gpt_header_offset, gpt_header, block_size)) {
+                ALOGE("%s: Failed to write back GPT header", __func__);
+                goto error;
+        }
+        return 0;
+error:
+        return -1;
+}
+
+//Read out the GPT header for the disk that contains the partition partname
+static uint8_t* gpt_get_header(const char *partname, enum gpt_instance instance)
+{
+        uint8_t* hdr = NULL;
+        char devpath[PATH_MAX] = {0};
+        int64_t hdr_offset = 0;
+        uint32_t block_size = 0;
+        int fd = -1;
+        if (!partname) {
+                ALOGE("%s: Invalid partition name", __func__);
+                goto error;
+        }
+        if (get_dev_path_from_partition_name(partname, devpath, sizeof(devpath))
+                        != 0) {
+                ALOGE("%s: Failed to resolve path for %s",
+                                __func__,
+                                partname);
+                goto error;
+        }
+        fd = open(devpath, O_RDWR);
+        if (fd < 0) {
+                ALOGE("%s: Failed to open %s : %s",
+                                __func__,
+                                devpath,
+                                strerror(errno));
+                goto error;
+        }
+        block_size = gpt_get_block_size(fd);
+        if (block_size == 0)
+        {
+                ALOGE("%s: Failed to get gpt block size for %s",
+                                __func__,
+                                partname);
+                goto error;
+        }
+
+        hdr = (uint8_t*)malloc(block_size);
+        if (!hdr) {
+                ALOGE("%s: Failed to allocate memory for gpt header",
+                                __func__);
+        }
+        if (instance == PRIMARY_GPT)
+                hdr_offset = block_size;
+        else {
+                hdr_offset = lseek64(fd, 0, SEEK_END) - block_size;
+        }
+        if (hdr_offset < 0) {
+                ALOGE("%s: Failed to get gpt header offset",
+                                __func__);
+                goto error;
+        }
+        if (blk_rw(fd, 0, hdr_offset, hdr, block_size)) {
+                ALOGE("%s: Failed to read GPT header from device",
+                                __func__);
+                goto error;
+        }
+        close(fd);
+        return hdr;
+error:
+        if (fd >= 0)
+                close(fd);
+        if (hdr)
+                free(hdr);
+        return NULL;
+}
+
+//Returns the partition entry array based on the
+//passed in buffer which contains the gpt header.
+//The fd here is the descriptor for the 'disk' which
+//holds the partition
+static uint8_t* gpt_get_pentry_arr(uint8_t *hdr, int fd)
+{
+        uint64_t pentries_start = 0;
+        uint32_t pentry_size = 0;
+        uint32_t block_size = 0;
+        uint32_t pentries_arr_size = 0;
+        uint8_t *pentry_arr = NULL;
+        int rc = 0;
+        if (!hdr) {
+                ALOGE("%s: Invalid header", __func__);
+                goto error;
+        }
+        if (fd < 0) {
+                ALOGE("%s: Invalid fd", __func__);
+                goto error;
+        }
+        block_size = gpt_get_block_size(fd);
+        if (!block_size) {
+                ALOGE("%s: Failed to get gpt block size for",
+                                __func__);
+                goto error;
+        }
+        pentries_start = GET_8_BYTES(hdr + PENTRIES_OFFSET) * block_size;
+        pentry_size = GET_4_BYTES(hdr + PENTRY_SIZE_OFFSET);
+        pentries_arr_size =
+                GET_4_BYTES(hdr + PARTITION_COUNT_OFFSET) * pentry_size;
+        pentry_arr = (uint8_t*)calloc(1, pentries_arr_size);
+        if (!pentry_arr) {
+                ALOGE("%s: Failed to allocate memory for partition array",
+                                __func__);
+                goto error;
+        }
+        rc = blk_rw(fd, 0,
+                        pentries_start,
+                        pentry_arr,
+                        pentries_arr_size);
+        if (rc) {
+                ALOGE("%s: Failed to read partition entry array",
+                                __func__);
+                goto error;
+        }
+        return pentry_arr;
+error:
+        if (pentry_arr)
+                free(pentry_arr);
+        return NULL;
+}
+
+static int gpt_set_pentry_arr(uint8_t *hdr, int fd, uint8_t* arr)
+{
+        uint32_t block_size = 0;
+        uint64_t pentries_start = 0;
+        uint32_t pentry_size = 0;
+        uint32_t pentries_arr_size = 0;
+        int rc = 0;
+        if (!hdr || fd < 0 || !arr) {
+                ALOGE("%s: Invalid argument", __func__);
+                goto error;
+        }
+        block_size = gpt_get_block_size(fd);
+        if (!block_size) {
+                ALOGE("%s: Failed to get gpt block size for",
+                                __func__);
+                goto error;
+        }
+        pentries_start = GET_8_BYTES(hdr + PENTRIES_OFFSET) * block_size;
+        pentry_size = GET_4_BYTES(hdr + PENTRY_SIZE_OFFSET);
+        pentries_arr_size =
+                GET_4_BYTES(hdr + PARTITION_COUNT_OFFSET) * pentry_size;
+        rc = blk_rw(fd, 1,
+                        pentries_start,
+                        arr,
+                        pentries_arr_size);
+        if (rc) {
+                ALOGE("%s: Failed to read partition entry array",
+                                __func__);
+                goto error;
+        }
+        return 0;
+error:
+        return -1;
+}
+
+
+
+//Allocate a handle used by calls to the "gpt_disk" api's
+struct gpt_disk * gpt_disk_alloc()
+{
+        struct gpt_disk *disk;
+        disk = (struct gpt_disk *)malloc(sizeof(struct gpt_disk));
+        if (!disk) {
+                ALOGE("%s: Failed to allocate memory", __func__);
+                goto end;
+        }
+        memset(disk, 0, sizeof(struct gpt_disk));
+end:
+        return disk;
+}
+
+//Free previously allocated/initialized handle
+void gpt_disk_free(struct gpt_disk *disk)
+{
+        if (!disk)
+                return;
+        if (disk->hdr)
+                free(disk->hdr);
+        if (disk->hdr_bak)
+                free(disk->hdr_bak);
+        if (disk->pentry_arr)
+                free(disk->pentry_arr);
+        if (disk->pentry_arr_bak)
+                free(disk->pentry_arr_bak);
+        free(disk);
+        return;
+}
+
+//fills up the passed in gpt_disk struct with information about the
+//disk represented by path dev. Returns 0 on success and -1 on error.
+int gpt_disk_get_disk_info(const char *dev, struct gpt_disk *dsk)
+{
+        struct gpt_disk *disk = NULL;
+        int fd = -1;
+        uint32_t gpt_header_size = 0;
+
+        if (!dsk || !dev) {
+                ALOGE("%s: Invalid arguments", __func__);
+                goto error;
+        }
+        disk = dsk;
+        disk->hdr = gpt_get_header(dev, PRIMARY_GPT);
+        if (!disk->hdr) {
+                ALOGE("%s: Failed to get primary header", __func__);
+                goto error;
+        }
+        gpt_header_size = GET_4_BYTES(disk->hdr + HEADER_SIZE_OFFSET);
+        disk->hdr_crc = crc32(0, disk->hdr, gpt_header_size);
+        disk->hdr_bak = gpt_get_header(dev, PRIMARY_GPT);
+        if (!disk->hdr_bak) {
+                ALOGE("%s: Failed to get backup header", __func__);
+                goto error;
+        }
+        disk->hdr_bak_crc = crc32(0, disk->hdr_bak, gpt_header_size);
+
+        //Descriptor for the block device. We will use this for further
+        //modifications to the partition table
+        if (get_dev_path_from_partition_name(dev,
+                                disk->devpath,
+                                sizeof(disk->devpath)) != 0) {
+                ALOGE("%s: Failed to resolve path for %s",
+                                __func__,
+                                dev);
+                goto error;
+        }
+        fd = open(disk->devpath, O_RDWR);
+        if (fd < 0) {
+                ALOGE("%s: Failed to open %s: %s",
+                                __func__,
+                                disk->devpath,
+                                strerror(errno));
+                goto error;
+        }
+        disk->pentry_arr = gpt_get_pentry_arr(disk->hdr, fd);
+        if (!disk->pentry_arr) {
+                ALOGE("%s: Failed to obtain partition entry array",
+                                __func__);
+                goto error;
+        }
+        disk->pentry_arr_bak = gpt_get_pentry_arr(disk->hdr_bak, fd);
+        if (!disk->pentry_arr_bak) {
+                ALOGE("%s: Failed to obtain backup partition entry array",
+                                __func__);
+                goto error;
+        }
+        disk->pentry_size = GET_4_BYTES(disk->hdr + PENTRY_SIZE_OFFSET);
+        disk->pentry_arr_size =
+                GET_4_BYTES(disk->hdr + PARTITION_COUNT_OFFSET) *
+                disk->pentry_size;
+        disk->pentry_arr_crc = GET_4_BYTES(disk->hdr + PARTITION_CRC_OFFSET);
+        disk->pentry_arr_bak_crc = GET_4_BYTES(disk->hdr_bak +
+                        PARTITION_CRC_OFFSET);
+        disk->block_size = gpt_get_block_size(fd);
+        close(fd);
+        disk->is_initialized = GPT_DISK_INIT_MAGIC;
+        return 0;
+error:
+        if (fd >= 0)
+                close(fd);
+        return -1;
+}
+
+//Get pointer to partition entry from a allocated gpt_disk structure
+uint8_t* gpt_disk_get_pentry(struct gpt_disk *disk,
+                const char *partname,
+                enum gpt_instance instance)
+{
+        uint8_t *ptn_arr = NULL;
+        if (!disk || !partname || disk->is_initialized != GPT_DISK_INIT_MAGIC) {
+                ALOGE("%s: Invalid argument",__func__);
+                goto error;
+        }
+        ptn_arr = (instance == PRIMARY_GPT) ?
+                disk->pentry_arr : disk->pentry_arr_bak;
+        return (gpt_pentry_seek(partname, ptn_arr,
+                        ptn_arr + disk->pentry_arr_size ,
+                        disk->pentry_size));
+error:
+        return NULL;
+}
+
+//Update CRC values for the various components of the gpt_disk
+//structure. This function should be called after any of the fields
+//have been updated before the structure contents are written back to
+//disk.
+int gpt_disk_update_crc(struct gpt_disk *disk)
+{
+        uint32_t gpt_header_size = 0;
+        if (!disk || (disk->is_initialized != GPT_DISK_INIT_MAGIC)) {
+                ALOGE("%s: invalid argument", __func__);
+                goto error;
+        }
+        //Recalculate the CRC of the primary partiton array
+        disk->pentry_arr_crc = crc32(0,
+                        disk->pentry_arr,
+                        disk->pentry_arr_size);
+        //Recalculate the CRC of the backup partition array
+        disk->pentry_arr_bak_crc = crc32(0,
+                        disk->pentry_arr_bak,
+                        disk->pentry_arr_size);
+        //Update the partition CRC value in the primary GPT header
+        PUT_4_BYTES(disk->hdr + PARTITION_CRC_OFFSET, disk->pentry_arr_crc);
+        //Update the partition CRC value in the backup GPT header
+        PUT_4_BYTES(disk->hdr_bak + PARTITION_CRC_OFFSET,
+                        disk->pentry_arr_bak_crc);
+        //Update the CRC value of the primary header
+        gpt_header_size = GET_4_BYTES(disk->hdr + HEADER_SIZE_OFFSET);
+        //Header CRC is calculated with its own CRC field set to 0
+        PUT_4_BYTES(disk->hdr + HEADER_CRC_OFFSET, 0);
+        PUT_4_BYTES(disk->hdr_bak + HEADER_CRC_OFFSET, 0);
+        disk->hdr_crc = crc32(0, disk->hdr, gpt_header_size);
+        disk->hdr_bak_crc = crc32(0, disk->hdr_bak, gpt_header_size);
+        PUT_4_BYTES(disk->hdr + HEADER_CRC_OFFSET, disk->hdr_crc);
+        PUT_4_BYTES(disk->hdr_bak + HEADER_CRC_OFFSET, disk->hdr_bak_crc);
+        return 0;
+error:
+        return -1;
+}
+
+//Write the contents of struct gpt_disk back to the actual disk
+int gpt_disk_commit(struct gpt_disk *disk)
+{
+        int fd = -1;
+        if (!disk || (disk->is_initialized != GPT_DISK_INIT_MAGIC)){
+                ALOGE("%s: Invalid args", __func__);
+                goto error;
+        }
+        fd = open(disk->devpath, O_RDWR);
+        if (fd < 0) {
+                ALOGE("%s: Failed to open %s: %s",
+                                __func__,
+                                disk->devpath,
+                                strerror(errno));
+                goto error;
+        }
+        //Write the primary header
+        if(gpt_set_header(disk->hdr, fd, PRIMARY_GPT) != 0) {
+                ALOGE("%s: Failed to update primary GPT header",
+                                __func__);
+                goto error;
+        }
+        //Write back the primary partition array
+        if (gpt_set_pentry_arr(disk->hdr, fd, disk->pentry_arr)) {
+                ALOGE("%s: Failed to write primary GPT partition arr",
+                                __func__);
+                goto error;
+        }
+        close(fd);
+        return 0;
+error:
+        if (fd >= 0)
+                close(fd);
+        return -1;
+}

+ 192 - 0
recovery/gpt-utils/gpt-utils.h

@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2013,2016, 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.
+ */
+
+#ifndef __GPT_UTILS_H__
+#define __GPT_UTILS_H__
+#include <vector>
+#include <string>
+#include <map>
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include <unistd.h>
+#include <stdlib.h>
+/******************************************************************************
+ * GPT HEADER DEFINES
+ ******************************************************************************/
+#define GPT_SIGNATURE               "EFI PART"
+#define HEADER_SIZE_OFFSET          12
+#define HEADER_CRC_OFFSET           16
+#define PRIMARY_HEADER_OFFSET       24
+#define BACKUP_HEADER_OFFSET        32
+#define FIRST_USABLE_LBA_OFFSET     40
+#define LAST_USABLE_LBA_OFFSET      48
+#define PENTRIES_OFFSET             72
+#define PARTITION_COUNT_OFFSET      80
+#define PENTRY_SIZE_OFFSET          84
+#define PARTITION_CRC_OFFSET        88
+
+#define TYPE_GUID_OFFSET            0
+#define TYPE_GUID_SIZE              16
+#define PTN_ENTRY_SIZE              128
+#define UNIQUE_GUID_OFFSET          16
+#define FIRST_LBA_OFFSET            32
+#define LAST_LBA_OFFSET             40
+#define ATTRIBUTE_FLAG_OFFSET       48
+#define PARTITION_NAME_OFFSET       56
+#define MAX_GPT_NAME_SIZE           72
+
+/******************************************************************************
+ * AB RELATED DEFINES
+ ******************************************************************************/
+//Bit 48 onwords in the attribute field are the ones where we are allowed to
+//store our AB attributes.
+#define AB_FLAG_OFFSET (ATTRIBUTE_FLAG_OFFSET + 6)
+#define GPT_DISK_INIT_MAGIC 0xABCD
+#define AB_PARTITION_ATTR_SLOT_ACTIVE (0x1<<2)
+#define AB_PARTITION_ATTR_BOOT_SUCCESSFUL (0x1<<6)
+#define AB_PARTITION_ATTR_UNBOOTABLE (0x1<<7)
+#define AB_SLOT_ACTIVE_VAL              0x3F
+#define AB_SLOT_INACTIVE_VAL            0x0
+#define AB_SLOT_ACTIVE                  1
+#define AB_SLOT_INACTIVE                0
+#define AB_SLOT_A_SUFFIX                "_a"
+#define AB_SLOT_B_SUFFIX                "_b"
+#define PTN_XBL                         "xbl"
+#define PTN_SWAP_LIST                   PTN_XBL, "sbl1", "rpm", "tz", "aboot", "hyp", "lksecapp", "keymaster", "cmnlib", "cmnlib32", "cmnlib64", "pmic", "apdp", "devcfg", "hosd", "keystore", "msadp", "abl", "mdtpsecapp", "mdtp"
+#define AB_PTN_LIST PTN_SWAP_LIST, "boot", "system", "vendor", "modem", "bluetooth", "dsp"
+#define BOOT_DEV_DIR    "/dev/block/bootdevice/by-name"
+
+/******************************************************************************
+ * HELPER MACROS
+ ******************************************************************************/
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+/******************************************************************************
+ * TYPES
+ ******************************************************************************/
+enum boot_update_stage {
+	UPDATE_MAIN = 1,
+	UPDATE_BACKUP,
+	UPDATE_FINALIZE
+};
+
+enum gpt_instance {
+	PRIMARY_GPT = 0,
+	SECONDARY_GPT
+};
+
+enum boot_chain {
+	NORMAL_BOOT = 0,
+	BACKUP_BOOT
+};
+
+struct gpt_disk {
+	//GPT primary header
+	uint8_t *hdr;
+	//primary header crc
+	uint32_t hdr_crc;
+	//GPT backup header
+	uint8_t *hdr_bak;
+	//backup header crc
+	uint32_t hdr_bak_crc;
+	//Partition entries array
+	uint8_t *pentry_arr;
+	//Partition entries array for backup table
+	uint8_t *pentry_arr_bak;
+	//Size of the pentry array
+	uint32_t pentry_arr_size;
+	//Size of each element in the pentry array
+	uint32_t pentry_size;
+	//CRC of the partition entry array
+	uint32_t pentry_arr_crc;
+	//CRC of the backup partition entry array
+	uint32_t pentry_arr_bak_crc;
+	//Path to block dev representing the disk
+	char devpath[PATH_MAX];
+	//Block size of disk
+	uint32_t block_size;
+	uint32_t is_initialized;
+};
+
+/******************************************************************************
+ * FUNCTION PROTOTYPES
+ ******************************************************************************/
+int prepare_boot_update(enum boot_update_stage stage);
+//GPT disk methods
+struct gpt_disk* gpt_disk_alloc();
+//Free previously allocated gpt_disk struct
+void gpt_disk_free(struct gpt_disk *disk);
+//Get the details of the disk holding the partition whose name
+//is passed in via dev
+int gpt_disk_get_disk_info(const char *dev, struct gpt_disk *disk);
+
+//Get pointer to partition entry from a allocated gpt_disk structure
+uint8_t* gpt_disk_get_pentry(struct gpt_disk *disk,
+		const char *partname,
+		enum gpt_instance instance);
+
+//Update the crc fields of the modified disk structure
+int gpt_disk_update_crc(struct gpt_disk *disk);
+
+//Write the contents of struct gpt_disk back to the actual disk
+int gpt_disk_commit(struct gpt_disk *disk);
+
+//Return if the current device is UFS based or not
+int gpt_utils_is_ufs_device();
+
+//Swtich betwieen using either the primary or the backup
+//boot LUN for boot. This is required since UFS boot partitions
+//cannot have a backup GPT which is what we use for failsafe
+//updates of the other 'critical' partitions. This function will
+//not be invoked for emmc targets and on UFS targets is only required
+//to be invoked for XBL.
+//
+//The algorithm to do this is as follows:
+//- Find the real block device(eg: /dev/block/sdb) that corresponds
+//  to the /dev/block/bootdevice/by-name/xbl(bak) symlink
+//
+//- Once we have the block device 'node' name(sdb in the above example)
+//  use this node to to locate the scsi generic device that represents
+//  it by checking the file /sys/block/sdb/device/scsi_generic/sgY
+//
+//- Once we locate sgY we call the query ioctl on /dev/sgy to switch
+//the boot lun to either LUNA or LUNB
+int gpt_utils_set_xbl_boot_partition(enum boot_chain chain);
+
+//Given a vector of partition names as a input and a reference to a map,
+//populate the map to indicate which physical disk each of the partitions
+//sits on. The key in the map is the path to the block device where the
+//partiton lies and the value is a vector of strings indicating which of
+//the passed in partiton names sits on that device.
+int gpt_utils_get_partition_map(std::vector<std::string>& partition_list,
+                std::map<std::string,std::vector<std::string>>& partition_map);
+#ifdef __cplusplus
+}
+#endif
+#endif /* __GPT_UTILS_H__ */

+ 58 - 0
recovery/gpt-utils/scsi/ufs/ioctl.h

@@ -0,0 +1,58 @@
+#ifndef UAPI_UFS_IOCTL_H_
+#define UAPI_UFS_IOCTL_H_
+
+#include <linux/types.h>
+
+/*
+ *  IOCTL opcode for ufs queries has the following opcode after
+ *  SCSI_IOCTL_GET_PCI
+ */
+#define UFS_IOCTL_QUERY			0x5388
+#define UFS_IOCTL_GETSN			0x5389
+
+/**
+ * struct ufs_ioctl_query_data - used to transfer data to and from user via ioctl
+ * @opcode: type of data to query (descriptor/attribute/flag)
+ * @idn: id of the data structure
+ * @buf_size: number of allocated bytes/data size on return
+ * @buffer: data location
+ *
+ * Received: buffer and buf_size (available space for transfered data)
+ * Submitted: opcode, idn, length, buf_size
+ */
+struct ufs_ioctl_query_data {
+	/*
+	 * User should select one of the opcode defined in "enum query_opcode".
+	 * Please check include/uapi/scsi/ufs/ufs.h for the definition of it.
+	 * Note that only UPIU_QUERY_OPCODE_READ_DESC,
+	 * UPIU_QUERY_OPCODE_READ_ATTR & UPIU_QUERY_OPCODE_READ_FLAG are
+	 * supported as of now. All other query_opcode would be considered
+	 * invalid.
+	 * As of now only read query operations are supported.
+	 */
+	__u32 opcode;
+	/*
+	 * User should select one of the idn from "enum flag_idn" or "enum
+	 * attr_idn" or "enum desc_idn" based on whether opcode above is
+	 * attribute, flag or descriptor.
+	 * Please check include/uapi/scsi/ufs/ufs.h for the definition of it.
+	 */
+	__u8 idn;
+	/*
+	 * User should specify the size of the buffer (buffer[0] below) where
+	 * it wants to read the query data (attribute/flag/descriptor).
+	 * As we might end up reading less data then what is specified in
+	 * buf_size. So we are updating buf_size to what exactly we have read.
+	 */
+	__u16 buf_size;
+	/*
+	 * placeholder for the start of the data buffer where kernel will copy
+	 * the query data (attribute/flag/descriptor) read from the UFS device
+	 * Note:
+	 * For Read/Write Attribute you will have to allocate 4 bytes
+	 * For Read/Write Flag you will have to allocate 1 byte
+	 */
+	__u8 buffer[0];
+};
+
+#endif /* UAPI_UFS_IOCTL_H_ */

+ 71 - 0
recovery/gpt-utils/scsi/ufs/ufs.h

@@ -0,0 +1,71 @@
+#ifndef UAPI_UFS_H_
+#define UAPI_UFS_H_
+
+#define MAX_QUERY_IDN	0x12
+
+/* Flag idn for Query Requests*/
+enum flag_idn {
+	QUERY_FLAG_IDN_FDEVICEINIT		= 0x01,
+	QUERY_FLAG_IDN_PERMANENT_WPE		= 0x02,
+	QUERY_FLAG_IDN_PWR_ON_WPE		= 0x03,
+	QUERY_FLAG_IDN_BKOPS_EN			= 0x04,
+	QUERY_FLAG_IDN_RESERVED1		= 0x05,
+	QUERY_FLAG_IDN_PURGE_ENABLE		= 0x06,
+	QUERY_FLAG_IDN_RESERVED2		= 0x07,
+	QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL      = 0x08,
+	QUERY_FLAG_IDN_BUSY_RTC			= 0x09,
+};
+
+/* Attribute idn for Query requests */
+enum attr_idn {
+	QUERY_ATTR_IDN_BOOT_LU_EN		= 0x00,
+	QUERY_ATTR_IDN_RESERVED			= 0x01,
+	QUERY_ATTR_IDN_POWER_MODE		= 0x02,
+	QUERY_ATTR_IDN_ACTIVE_ICC_LVL		= 0x03,
+	QUERY_ATTR_IDN_OOO_DATA_EN		= 0x04,
+	QUERY_ATTR_IDN_BKOPS_STATUS		= 0x05,
+	QUERY_ATTR_IDN_PURGE_STATUS		= 0x06,
+	QUERY_ATTR_IDN_MAX_DATA_IN		= 0x07,
+	QUERY_ATTR_IDN_MAX_DATA_OUT		= 0x08,
+	QUERY_ATTR_IDN_DYN_CAP_NEEDED		= 0x09,
+	QUERY_ATTR_IDN_REF_CLK_FREQ		= 0x0A,
+	QUERY_ATTR_IDN_CONF_DESC_LOCK		= 0x0B,
+	QUERY_ATTR_IDN_MAX_NUM_OF_RTT		= 0x0C,
+	QUERY_ATTR_IDN_EE_CONTROL		= 0x0D,
+	QUERY_ATTR_IDN_EE_STATUS		= 0x0E,
+	QUERY_ATTR_IDN_SECONDS_PASSED		= 0x0F,
+	QUERY_ATTR_IDN_CNTX_CONF		= 0x10,
+	QUERY_ATTR_IDN_CORR_PRG_BLK_NUM		= 0x11,
+};
+
+#define QUERY_ATTR_IDN_BOOT_LU_EN_MAX	0x02
+
+/* Descriptor idn for Query requests */
+enum desc_idn {
+	QUERY_DESC_IDN_DEVICE		= 0x0,
+	QUERY_DESC_IDN_CONFIGURAION	= 0x1,
+	QUERY_DESC_IDN_UNIT		= 0x2,
+	QUERY_DESC_IDN_RFU_0		= 0x3,
+	QUERY_DESC_IDN_INTERCONNECT	= 0x4,
+	QUERY_DESC_IDN_STRING		= 0x5,
+	QUERY_DESC_IDN_RFU_1		= 0x6,
+	QUERY_DESC_IDN_GEOMETRY		= 0x7,
+	QUERY_DESC_IDN_POWER		= 0x8,
+	QUERY_DESC_IDN_RFU_2		= 0x9,
+	QUERY_DESC_IDN_MAX,
+};
+
+/* UTP QUERY Transaction Specific Fields OpCode */
+enum query_opcode {
+	UPIU_QUERY_OPCODE_NOP		= 0x0,
+	UPIU_QUERY_OPCODE_READ_DESC	= 0x1,
+	UPIU_QUERY_OPCODE_WRITE_DESC	= 0x2,
+	UPIU_QUERY_OPCODE_READ_ATTR	= 0x3,
+	UPIU_QUERY_OPCODE_WRITE_ATTR	= 0x4,
+	UPIU_QUERY_OPCODE_READ_FLAG	= 0x5,
+	UPIU_QUERY_OPCODE_SET_FLAG	= 0x6,
+	UPIU_QUERY_OPCODE_CLEAR_FLAG	= 0x7,
+	UPIU_QUERY_OPCODE_TOGGLE_FLAG	= 0x8,
+	UPIU_QUERY_OPCODE_MAX,
+};
+#endif /* UAPI_UFS_H_ */

+ 17 - 0
recovery/root/etc/recovery.fstab

@@ -0,0 +1,17 @@
+# Android fstab file.
+# The filesystem that contains the filesystem checker binary (typically /system) cannot
+# specify MF_CHECK, and must come before any filesystems that do specify MF_CHECK
+
+/dev/block/bootdevice/by-name/system       /            ext4    ro,barrier=1                                                  wait,recoveryonly,slotselect
+/dev/block/bootdevice/by-name/oem          /oem         ext4    ro,barrier=1                                                  wait,recoveryonly,slotselect
+/dev/block/bootdevice/by-name/oem          /odm         ext4    ro,barrier=1                                                  wait,recoveryonly,slotselect
+/dev/block/bootdevice/by-name/userdata     /data        ext4    noatime,nosuid,nodev,barrier=1,data=ordered,noauto_da_alloc,errors=panic wait,check,formattable,encryptable=footer
+/dev/block/bootdevice/by-name/frp          /persistent  emmc    defaults                                                      defaults
+/dev/block/bootdevice/by-name/dsp          /dsp         ext4    nosuid,nodev,barrier=1,data=ordered,nodelalloc,errors=panic   wait,notrim,slotselect
+/dev/block/bootdevice/by-name/misc         /misc        emmc    defaults                                                      defaults
+/dev/block/bootdevice/by-name/modem        /firmware    vfat    ro,shortname=lower,uid=1000,gid=1000,dmask=227,fmask=337      wait,slotselect
+/dev/block/bootdevice/by-name/bluetooth    /bt_firmware vfat    ro,shortname=lower,uid=1002,gid=3002,dmask=227,fmask=337      wait,slotselect
+/dev/block/bootdevice/by-name/persist      /persist     ext4    nosuid,nodev,barrier=1,data=ordered,nodelalloc,errors=panic   wait,notrim
+
+/devices/soc/c084000.sdhci/mmc_host/mmc*                       auto         auto    nosuid,nodev                              voldmanaged=sdcard1:auto,encryptable=userdata
+/devices/soc/a800000.ssusb/a800000.dwc3/xhci-hcd.0.auto/usb*   auto         auto    defaults                                  voldmanaged=usb:auto

+ 13 - 0
recovery/root/etc/twrp.fstab

@@ -0,0 +1,13 @@
+# mount point	fstype	device					device2				flags
+/boot		emmc	/dev/block/bootdevice/by-name/boot					flags=slotselect
+/system		ext4	/dev/block/bootdevice/by-name/system					flags=slotselect
+/system_image	emmc	/dev/block/bootdevice/by-name/system					flags=slotselect
+/vendor		ext4	/dev/block/bootdevice/by-name/vendor					flags=backup=1;slotselect
+/vendor_image	emmc	/dev/block/bootdevice/by-name/vendor					flags=backup=1;slotselect
+/data		ext4	/dev/block/bootdevice/by-name/userdata
+/firmware	vfat	/dev/block/bootdevice/by-name/modem					flags=display="Firmware";mounttodecrypt;slotselect;fsflags=ro
+/misc		emmc	/dev/block/bootdevice/by-name/misc
+
+# Removable storage
+/sdcard1	auto	/dev/block/mmcblk1p1			/dev/block/mmcblk1		flags=display="MicroSD";storage;wipeingui;removable
+/usb_otg	vfat	/dev/block/sda1				/dev/block/sda			flags=display="USB-OTG";storage;wipeingui;removable

+ 100 - 0
recovery/root/init.recovery.qcom.rc

@@ -0,0 +1,100 @@
+on early-init
+    mkdir /firmware 0771 system system
+    mount debugfs debugfs /sys/kernel/debug
+    chmod 0755 /sys/kernel/debug
+
+on fs
+    wait /dev/block/platform/soc/${ro.boot.bootdevice}
+    symlink /dev/block/platform/soc/${ro.boot.bootdevice} /dev/block/bootdevice
+
+    chmod 0660 /dev/qseecom
+    chown system drmrpc /dev/qseecom
+    chmod 0664 /dev/ion
+    chown system system /dev/ion
+
+    # IMPORTANT!
+    # This needs to be supported by the init binary!
+    install_keyring
+
+on init
+    # Mount cgroup for cpu accounting
+    mount cgroup none /acct cpuacct
+    mkdir /acct/uid
+
+    start hwservicemanager
+
+on boot
+    setprop sys.usb.config adb
+    start prepdecrypt
+
+on property:crypto.ready=1
+    start sbin_qseecomd
+    start servicemanager
+
+    # Wait for qseecomd to finish
+    wait_for_prop sys.listeners.registered true
+
+    start gatekeeper-1-0
+    start keymaster-3-0
+
+on property:crypto.ready=0
+    stop keymaster-3-0
+    stop gatekeeper-1-0
+    stop servicemanager
+    stop sbin_qseecomd
+
+service hwservicemanager /sbin/hwservicemanager
+    user root
+    group root
+    disabled
+    onrestart setprop hwservicemanager.ready false
+    seclabel u:r:recovery:s0
+
+service servicemanager /sbin/servicemanager
+    disabled
+    user root
+    group root readproc
+    seclabel u:r:recovery:s0
+    writepid /dev/cpuset/system-background/tasks
+
+service keymaster-3-0 /sbin/android.hardware.keymaster@3.0-service-qti
+    user root
+    group root
+    disabled
+    seclabel u:r:recovery:s0
+
+service gatekeeper-1-0 /sbin/android.hardware.gatekeeper@1.0-service-qti
+    user root
+    group root
+    disabled
+    seclabel u:r:recovery:s0
+
+service sbin_qseecomd /sbin/qseecomd
+    disabled
+    user root
+    group root
+    seclabel u:r:recovery:s0
+
+service prepdecrypt /sbin/prepdecrypt.sh
+    oneshot
+    disabled
+    user root
+    group root
+    seclabel u:r:recovery:s0
+
+# Handled by TWRP
+service keystore /sbin/keystore /tmp/misc/keystore
+    disabled
+    oneshot
+    user root
+    group root drmrpc readproc
+    seclabel u:r:recovery:s0
+    writepid /dev/cpuset/foreground/tasks
+
+# Handled by TWRP
+service keystore_auth /sbin/keystore_auth
+    disabled
+    oneshot
+    user system
+    group root
+    seclabel u:r:recovery:s0

+ 117 - 0
recovery/root/init.recovery.usb.rc

@@ -0,0 +1,117 @@
+# Copyright 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.
+#
+
+on boot
+    mkdir /dev/usb-ffs 0770 shell shell
+    mkdir /dev/usb-ffs/adb 0770 shell shell
+    mount configfs none /config
+    mkdir /config/usb_gadget/g1 0770 shell shell
+    mkdir /config/usb_gadget/g1/strings/0x409 0770 shell shell
+
+    write /config/usb_gadget/g1/bcdUSB 0x0200
+    write /config/usb_gadget/g1/os_desc/use 1
+    write /config/usb_gadget/g1/strings/0x409/serialnumber ${ro.serialno}
+    write /config/usb_gadget/g1/strings/0x409/manufacturer ${ro.product.manufacturer}
+    write /config/usb_gadget/g1/strings/0x409/product ${ro.product.model}
+
+    mkdir /config/usb_gadget/g1/functions/mass_storage.0
+    mkdir /config/usb_gadget/g1/functions/mtp.gs0
+    mkdir /config/usb_gadget/g1/functions/ptp.gs1
+    mkdir /config/usb_gadget/g1/functions/accessory.gs2
+    mkdir /config/usb_gadget/g1/functions/audio_source.gs2
+    mkdir /config/usb_gadget/g1/functions/audio_source.gs3
+    mkdir /config/usb_gadget/g1/functions/midi.gs5
+    mkdir /config/usb_gadget/g1/functions/ffs.adb
+    mkdir /config/usb_gadget/g1/functions/diag.diag
+    mkdir /config/usb_gadget/g1/functions/cser.dun.0
+    mkdir /config/usb_gadget/g1/functions/cser.nmea.1
+    mkdir /config/usb_gadget/g1/functions/gsi.rmnet
+    mkdir /config/usb_gadget/g1/functions/gsi.rndis
+    mkdir /config/usb_gadget/g1/functions/gsi.dpl
+    mkdir /config/usb_gadget/g1/functions/qdss.qdss
+    mkdir /config/usb_gadget/g1/functions/rndis_bam.rndis
+    mkdir /config/usb_gadget/g1/functions/rmnet_bam.rmnet
+    mkdir /config/usb_gadget/g1/functions/rmnet_bam.dpl
+    mkdir /config/usb_gadget/g1/functions/ncm.0
+    mkdir /config/usb_gadget/g1/functions/ccid.ccid
+    mkdir /config/usb_gadget/g1/configs/b.1 0770 shell shell
+    mkdir /config/usb_gadget/g1/configs/b.1/strings/0x409 0770 shell shell
+    write /config/usb_gadget/g1/os_desc/b_vendor_code 0x1
+    write /config/usb_gadget/g1/os_desc/qw_sign "MSFT100"
+
+    symlink /config/usb_gadget/g1/configs/b.1 /config/usb_gadget/g1/os_desc/b.1
+
+    mount functionfs adb /dev/usb-ffs/adb uid=2000,gid=2000
+    write /sys/class/android_usb/android0/f_ffs/aliases adb
+
+    setprop sys.usb.configfs 1
+    setprop sys.usb.rndis.func.name "rndis_bam"
+    setprop sys.usb.rmnet.func.name "rmnet_bam"
+
+# USB compositions
+on property:sys.usb.config=none
+    write /config/usb_gadget/g1/UDC "none"
+    stop adbd
+    setprop sys.usb.ffs.ready 0
+    write /config/usb_gadget/g1/bDeviceClass 0
+    write /config/usb_gadget/g1/bDeviceSubClass 0
+    write /config/usb_gadget/g1/bDeviceProtocol 0
+    setprop sys.usb.state ${sys.usb.config}
+
+on property:sys.usb.config=adb
+    start adbd
+
+on property:sys.usb.ffs.ready=1 && property:sys.usb.config=adb
+    write /config/usb_gadget/g1/configs/b.1/strings/0x409/configuration "adb"
+    rm /config/usb_gadget/g1/configs/b.1/f1
+    rm /config/usb_gadget/g1/configs/b.1/f2
+    rm /config/usb_gadget/g1/configs/b.1/f3
+    rm /config/usb_gadget/g1/configs/b.1/f4
+    rm /config/usb_gadget/g1/configs/b.1/f5
+    write /config/usb_gadget/g1/idVendor 0x0fce
+    write /config/usb_gadget/g1/idProduct 0x7${ro.usb.pid_suffix}
+    symlink /config/usb_gadget/g1/functions/ffs.adb /config/usb_gadget/g1/configs/b.1/f1
+    write /config/usb_gadget/g1/UDC ${sys.usb.controller}
+    setprop sys.usb.state ${sys.usb.config}
+
+on property:sys.usb.config=mtp
+    write /config/usb_gadget/g1/configs/b.1/strings/0x409/configuration "mtp"
+    rm /config/usb_gadget/g1/configs/b.1/f1
+    rm /config/usb_gadget/g1/configs/b.1/f2
+    rm /config/usb_gadget/g1/configs/b.1/f3
+    rm /config/usb_gadget/g1/configs/b.1/f4
+    rm /config/usb_gadget/g1/configs/b.1/f5
+    write /config/usb_gadget/g1/idVendor 0x0fce
+    write /config/usb_gadget/g1/idProduct 0x7${ro.usb.pid_suffix}
+    symlink /config/usb_gadget/g1/functions/mtp.gs0 /config/usb_gadget/g1/configs/b.1/f1
+    write /config/usb_gadget/g1/UDC ${sys.usb.controller}
+    setprop sys.usb.state ${sys.usb.config}
+
+on property:sys.usb.config=mtp,adb
+    start adbd
+
+on property:sys.usb.ffs.ready=1 && property:sys.usb.config=mtp,adb
+    write /config/usb_gadget/g1/configs/b.1/strings/0x409/configuration "mtp_adb"
+    rm /config/usb_gadget/g1/configs/b.1/f1
+    rm /config/usb_gadget/g1/configs/b.1/f2
+    rm /config/usb_gadget/g1/configs/b.1/f3
+    rm /config/usb_gadget/g1/configs/b.1/f4
+    rm /config/usb_gadget/g1/configs/b.1/f5
+    write /config/usb_gadget/g1/idVendor 0x0fce
+    write /config/usb_gadget/g1/idProduct 0x7${ro.usb.pid_suffix}
+    symlink /config/usb_gadget/g1/functions/mtp.gs0 /config/usb_gadget/g1/configs/b.1/f1
+    symlink /config/usb_gadget/g1/functions/ffs.adb /config/usb_gadget/g1/configs/b.1/f2
+    write /config/usb_gadget/g1/UDC ${sys.usb.controller}
+    setprop sys.usb.state ${sys.usb.config}

BIN
recovery/root/sbin/android.hardware.gatekeeper@1.0-service-qti


BIN
recovery/root/sbin/android.hardware.keymaster@3.0-service-qti


+ 36 - 0
recovery/root/sbin/prepdecrypt.sh

@@ -0,0 +1,36 @@
+#!/sbin/sh
+
+finish() {
+    umount /s
+
+    setprop crypto.ready 1
+
+    exit 0
+}
+
+suffix=$(getprop ro.boot.slot_suffix)
+if [ -z "$suffix" ]; then
+    suf=$(getprop ro.boot.slot)
+    suffix="_$suf"
+fi
+
+syspath="/dev/block/bootdevice/by-name/system$suffix"
+mkdir /s
+mount -t ext4 -o ro "$syspath" /s
+
+is_fastboot_twrp=$(getprop ro.boot.fastboot)
+if [ ! -z "$is_fastboot_twrp" ]; then
+    setprop ro.build.version.release "$(getprop ro.build.version.release_orig)"
+    setprop ro.build.version.security_patch "$(getprop ro.build.version.security_patch_orig)"
+    finish
+fi
+
+if [ -f /s/system/build.prop ]; then
+    setprop ro.build.version.release "$(grep -i 'ro.build.version.release' /s/system/build.prop | cut -f2 -d'=')"
+    setprop ro.build.version.security_patch "$(grep -i 'ro.build.version.security_patch' /s/system/build.prop  | cut -f2 -d'=')"
+    finish
+fi
+
+setprop ro.build.version.release "$(getprop ro.build.version.release_orig)"
+setprop ro.build.version.security_patch "$(getprop ro.build.version.security_patch_orig)"
+finish

BIN
recovery/root/sbin/qseecomd


BIN
recovery/root/vendor/lib64/android.hardware.boot@1.0.so


BIN
recovery/root/vendor/lib64/android.hardware.confirmationui@1.0.so


BIN
recovery/root/vendor/lib64/android.hardware.weaver@1.0.so


BIN
recovery/root/vendor/lib64/hw/android.hardware.gatekeeper@1.0-impl-qti.so


BIN
recovery/root/vendor/lib64/hw/android.hardware.keymaster@3.0-impl-qti.so


BIN
recovery/root/vendor/lib64/hw/bootctrl.qcom.so


BIN
recovery/root/vendor/lib64/libGPreqcancel.so


BIN
recovery/root/vendor/lib64/libGPreqcancel_svc.so


BIN
recovery/root/vendor/lib64/libQSEEComAPI.so


BIN
recovery/root/vendor/lib64/libdiag.so


BIN
recovery/root/vendor/lib64/libdrmfs.so


BIN
recovery/root/vendor/lib64/libdrmtime.so


BIN
recovery/root/vendor/lib64/libgptutils.so


BIN
recovery/root/vendor/lib64/libkeymasterdeviceutils.so


BIN
recovery/root/vendor/lib64/libqisl.so


BIN
recovery/root/vendor/lib64/librpmb.so


BIN
recovery/root/vendor/lib64/libssd.so


BIN
recovery/root/vendor/lib64/libtime_genoff.so


+ 20 - 0
recovery/root/vendor/manifest.xml

@@ -0,0 +1,20 @@
+<manifest version="1.0" type="device">
+    <hal format="hidl">
+        <name>android.hardware.gatekeeper</name>
+        <transport>hwbinder</transport>
+        <version>1.0</version>
+        <interface>
+            <name>IGatekeeper</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
+    <hal format="hidl">
+        <name>android.hardware.keymaster</name>
+        <transport>hwbinder</transport>
+        <version>3.0</version>
+        <interface>
+            <name>IKeymasterDevice</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
+</manifest>