[Feature]add MT2731_MP2_MR2_SVN388 baseline version

Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/support/bootctrl/Makefile.am b/src/support/bootctrl/Makefile.am
new file mode 100644
index 0000000..900a7f8
--- /dev/null
+++ b/src/support/bootctrl/Makefile.am
@@ -0,0 +1,3 @@
+SUBDIRS = \
+  include \
+  mediatek/bootctrl
\ No newline at end of file
diff --git a/src/support/bootctrl/configure.ac b/src/support/bootctrl/configure.ac
new file mode 100644
index 0000000..ddac88c
--- /dev/null
+++ b/src/support/bootctrl/configure.ac
@@ -0,0 +1,11 @@
+AC_INIT([bootctrl], [1.0])
+AM_INIT_AUTOMAKE([foreign subdir-objects])
+AC_PROG_CXX
+AM_PROG_AS
+LT_INIT
+AC_CONFIG_FILES([
+  Makefile
+  include/Makefile
+  mediatek/bootctrl/Makefile
+])
+AC_OUTPUT
diff --git a/src/support/bootctrl/include/Makefile.am b/src/support/bootctrl/include/Makefile.am
new file mode 100644
index 0000000..41115f0
--- /dev/null
+++ b/src/support/bootctrl/include/Makefile.am
@@ -0,0 +1,3 @@
+nobase_include_HEADERS = \
+	hardware/boot_control.h \
+	hardware/hardware.h
diff --git a/src/support/bootctrl/mediatek/bootctrl/Makefile.am b/src/support/bootctrl/mediatek/bootctrl/Makefile.am
new file mode 100644
index 0000000..676c581
--- /dev/null
+++ b/src/support/bootctrl/mediatek/bootctrl/Makefile.am
@@ -0,0 +1,12 @@
+lib_LIBRARIES = libbootctrl.a
+
+libbootctrl_a_SOURCES = \
+	bootctrl.cpp \
+	avb/libavb/avb_util.c \
+	avb/libavb/avb_crc32.c
+
+libbootctrl_a_CFLAGS = -Wall \
+		       -Werror
+
+libbootctrl_a_CPPFLAGS = \
+	-I$(top_srcdir)/include
diff --git a/src/support/bootctrl/mediatek/bootctrl/bootctrl.cpp b/src/support/bootctrl/mediatek/bootctrl/bootctrl.cpp
new file mode 100644
index 0000000..8f0e948
--- /dev/null
+++ b/src/support/bootctrl/mediatek/bootctrl/bootctrl.cpp
@@ -0,0 +1,530 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * SPDX-License-Identifier: MediaTekProprietary
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <hardware/hardware.h>
+#include <hardware/boot_control.h>
+//#include <android-base/logging.h>
+//#include <cutils/properties.h>
+#define LOG_TAG "bootctrlHAL"
+//#include <log/log.h>
+//#include <utils/Log.h>
+
+#include "bootctrl.h"
+#if !defined(ARCH_X86)
+#include "sd_misc.h"
+#endif
+//#include <fstab/fstab.h>
+#include "avb/libavb/avb_util.h"
+
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+extern "C"{
+    #include "mtk_device_wrap.h"
+}
+#endif
+
+#define FSTAB_SUPPORT 0
+// Debug for update_engine_sideload
+#define ALOGE printf
+#define ALOGI printf
+static struct fstab* fstab = NULL;
+static char *blk_dev_path = "/dev/disk/by-partlabel/misc";
+#if FSTAB_SUPPORT
+static void free_fstab(void)
+{
+    fs_mgr_free_fstab(fstab);
+}
+
+
+static char *get_device_path(const char *mount_point)
+{
+    struct fstab_rec *rec = NULL;
+    char *source = NULL;
+
+    rec = fs_mgr_get_entry_for_mount_point(fstab, mount_point);
+    if (!rec) {
+        ALOGE("%s failed to get entry for %s \n", __func__ , mount_point);
+        return NULL;
+    }
+
+    source = strdup(rec->blk_device);
+    return source;
+}
+#endif
+static int bootctrl_read_metadata(AvbABData *bctrl)
+{
+    int fd, err;
+    ssize_t sz, size;
+    char *buf = (char *)bctrl;
+
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+    fd = mtk_device_wrap_open(blk_dev_path, O_RDONLY);
+#else
+    fd = open(blk_dev_path, O_RDONLY);
+#endif
+
+    if (fd < 0) {
+        err = errno;
+        ALOGE("%s Error opening metadata file: %s\n", __func__ ,strerror(errno));
+        return -err;
+    }
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+    if (mtk_device_wrap_seek(fd, OFFSETOF_SLOT_SUFFIX, SEEK_SET) < 0)
+#else
+    if (lseek(fd, OFFSETOF_SLOT_SUFFIX, SEEK_SET) < 0)
+#endif
+    {
+        err = errno;
+        ALOGE("%s Error seeking to metadata offset: %s\n", __func__ ,strerror(errno));
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+        mtk_device_wrap_close(fd);
+#else
+        close(fd);
+#endif
+        return -err;
+    }
+    size = sizeof(AvbABData);
+    do {
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+        sz = mtk_device_wrap_read(fd, buf, size);
+#else
+        sz = read(fd, buf, size);
+#endif
+        if (sz == 0) {
+            break;
+        } else if (sz < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            err = -errno;
+            ALOGE("%s Error reading metadata file\n", __func__);
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+            mtk_device_wrap_close(fd);
+#else
+            close(fd);
+#endif
+            return err;
+        }
+        size -= sz;
+        buf += sz;
+    } while(size > 0);
+
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+    mtk_device_wrap_close(fd);
+#else
+    close(fd);
+#endif
+
+    /* Check bootctrl magic number */
+    if (memcmp(bctrl->magic , AVB_AB_MAGIC, AVB_AB_MAGIC_LEN) != 0) {
+        ALOGE("metadata is not initialised or corrupted %s.\n", bctrl->magic);
+        return -EIO;
+    }
+    return 0;
+}
+
+static int bootctrl_write_metadata(AvbABData *bctrl)
+{
+    int fd, err;
+    ssize_t sz, size;
+    char *buf = (char *)bctrl;
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+    char *tmpbuf = NULL;
+#endif
+
+    ALOGI("Enter in bootctrl: bootctrl_write_metadata \n");
+
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+    fd = mtk_device_wrap_open(blk_dev_path, O_RDWR);
+#else
+    fd = open(blk_dev_path, O_RDWR);
+#endif
+    if (fd < 0) {
+        err = errno;
+        ALOGE("%s Error opening metadata file: %s\n", __func__,strerror(errno));
+        return -err;
+    }
+
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+    if (mtk_device_wrap_seek(fd, OFFSETOF_SLOT_SUFFIX, SEEK_SET) < 0)
+#else
+    if (lseek(fd, OFFSETOF_SLOT_SUFFIX, SEEK_SET) < 0)
+#endif
+    {
+        err = errno;
+        ALOGE("%s Error seeking to metadata offset: %s\n", __func__ ,strerror(errno));
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+        mtk_device_wrap_close(fd);
+#else
+        close(fd);
+#endif
+        return -err;
+    }
+
+    bctrl->crc32 = avb_htobe32(
+             avb_crc32((const uint8_t*)bctrl, sizeof(AvbABData) - sizeof(uint32_t)));
+
+    size = sizeof(AvbABData);
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+    tmpbuf = (char *)malloc(sizeof(char)* OFFSETOF_SLOT_SUFFIX);
+    if (tmpbuf == NULL){
+        ALOGE("Error Writing metadata: malloc tmpbuf failed\n");
+        mtk_device_wrap_close(fd);
+        err = -errno;
+        return err;
+    }
+#endif
+
+    do {
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+        memset(tmpbuf, 0xFF, OFFSETOF_SLOT_SUFFIX);
+        if (mtk_device_wrap_seek(fd, 0, SEEK_SET) < 0){
+            err = errno;
+            ALOGE("%s Error seeking to metadata offset: %s\n", __func__ ,strerror(errno));
+            mtk_device_wrap_close(fd);
+            free(tmpbuf);
+            return -err;
+        }
+        if (mtk_device_wrap_write_force(fd, tmpbuf, OFFSETOF_SLOT_SUFFIX) < 0){
+            err = -errno;
+            ALOGE("%s Error Writing metadata file\n",__func__);
+            mtk_device_wrap_close(fd);
+            free(tmpbuf);
+            return err;
+        }
+        memcpy(tmpbuf, bctrl, size);
+        sz = mtk_device_wrap_write_force(fd, tmpbuf, sizeof(char)* OFFSETOF_SLOT_SUFFIX);
+#else
+        sz = write(fd, buf, size);
+#endif
+        if (sz == 0) {
+            break;
+        } else if (sz < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            err = -errno;
+            ALOGE("%s Error Writing metadata file\n",__func__);
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+            mtk_device_wrap_close(fd);
+            free(tmpbuf);
+#else
+            close(fd);
+#endif
+            return err;
+        }
+        size -= sz;
+        buf += sz;
+    } while(size > 0);
+
+#if MTK_BOOTDEV_TYPE == BOOTDEV_TYPE_NAND
+    free(tmpbuf);
+    mtk_device_wrap_close(fd);
+#else
+    close(fd);
+#endif
+
+    return 0;
+}
+
+void bootctrl_init(boot_control_module_t  *module )
+{
+    ALOGI("boot control HAL init");
+
+    if(blk_dev_path == NULL) {
+#if FSTAB_SUPPORT
+        /* Initial read fstab */
+        fstab = fs_mgr_read_fstab_default();
+        if (!fstab) {
+            ALOGE("failed to read default fstab");
+        }
+        blk_dev_path = get_device_path("/misc");
+
+        /* Free fstab */
+        free_fstab();
+#endif
+    }
+
+    ALOGI("%s misc blk device path = %s\n", __func__ ,blk_dev_path);
+}
+
+unsigned bootctrl_get_number_slots(boot_control_module_t  *module )
+{
+    return 2;
+}
+
+int bootctrl_get_active_slot()
+{
+    int fd, err, slot;
+    ssize_t size = COMMAND_LINE_SIZE, sz;
+    char *buf, *ptr;
+    char *str;
+
+    fd = open(COMMAND_LINE_PATH, O_RDONLY);
+
+    if (fd < 0) {
+        err = -errno;
+        ALOGE("%s error reading commandline\n", __func__);
+        return err;
+    }
+    ptr = buf = (char *)malloc(size);
+    if (!buf) {
+        err = -errno;
+        ALOGE("%s Error allocating memory\n", __func__);
+        close(fd);
+        return err;
+    }
+    do {
+        sz = read(fd, buf, size);
+        if (sz == 0) {
+            break;
+        } else if (sz < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            err = -errno;
+            ALOGE("%s Error reading file\n",__func__);
+            free(ptr);
+            close(fd);
+            return err;
+        }
+        size -= sz;
+        buf += sz;
+    } while(size > 0);
+    str = strstr((char *)ptr, SLOT_SUFFIX_STR);
+    if (!str) {
+        err = -EIO;
+        ALOGE("%s cannot find %s in kernel commandline.\n", __func__ , SLOT_SUFFIX_STR);
+        free(ptr);
+        close(fd);
+        return err;
+    }
+    str += strlen(SLOT_SUFFIX_STR) + 1;
+    //str += strlen(SLOT_SUFFIX_STR);
+    slot = (*str == 'a') ? 0 : 1;
+    free(ptr);
+    close(fd);
+    return slot;
+}
+
+
+uint32_t bootctrl_get_current_slot(boot_control_module_t  *module )
+{
+    ALOGI("boot control bootctrl_get_current_slot\n");
+
+    uint32_t slot = 0;
+
+    slot = bootctrl_get_active_slot();
+
+    ALOGI("bootctrl_get_current_slot %d\n", slot);
+    return slot;
+}
+
+int bootctrl_mark_boot_successful(boot_control_module_t  *module )
+{
+    ALOGI("boot control bootctrl_mark_boot_successful\n");
+    int ret;
+    int slot = 0;
+    AvbABData metadata;
+    AvbABSlotData *slotp;
+
+    ret = bootctrl_read_metadata(&metadata);
+    if (ret < 0) {
+        return ret;
+    }
+
+    slot = bootctrl_get_active_slot();
+    if (slot < 0) {
+        ALOGE("bootctrl_mark_boot_successful fail , slot = \n");
+        return slot;
+    }
+    slotp = &metadata.slots[slot];
+    slotp->successful_boot = 1;
+    slotp->tries_remaining = 0;
+
+    return bootctrl_write_metadata(&metadata);
+}
+
+int bootctrl_set_active_boot_slot(boot_control_module_t  *module ,
+    unsigned slot)
+{
+    ALOGI("boot control bootctrl_set_active_boot_slot , slot is %d\n", slot);
+    int ret, slot2;
+    AvbABData metadata;
+    AvbABSlotData *slotp;
+
+    if (slot >= 2) {
+        ALOGE("%s Wrong Slot value %u\n", __func__ , slot);
+        return -EINVAL;
+    }
+    ret = bootctrl_read_metadata(&metadata);
+    if (ret < 0) {
+        return ret;
+    }
+    /* Set highest priority and reset retry count */
+    slotp = &metadata.slots[slot];
+    slotp->successful_boot = 0;
+    slotp->priority = AVB_AB_MAX_PRIORITY;
+    slotp->tries_remaining = AVB_AB_MAX_TRIES_REMAINING;
+
+    /* Ensure other slot doesn't have as high a priority. */
+    slot2 = (slot == 0) ? 1 : 0;
+    slotp = &metadata.slots[slot2];
+    if(slotp->priority == AVB_AB_MAX_PRIORITY)
+        slotp->priority = AVB_AB_MAX_PRIORITY - 1;
+    ret = bootctrl_write_metadata(&metadata);
+    if (ret < 0) {
+        return ret;
+    }
+
+    return 0;
+}
+
+int bootctrl_set_slot_as_unbootable(boot_control_module_t  *module ,
+    unsigned slot)
+{
+    ALOGI("boot control bootctrl_set_slot_as_unbootable\n");
+    int ret;
+    AvbABData metadata;
+    AvbABSlotData *slotp;
+
+    if (slot >= 2) {
+        ALOGE("%s Wrong Slot value %u\n", __func__ , slot);
+        return -EINVAL;
+    }
+    ret = bootctrl_read_metadata(&metadata);
+    if (ret < 0) {
+        return ret;
+    }
+    /* Set zero to priority ,successful_boot and tries_remaining */
+    slotp = &metadata.slots[slot];
+    slotp->successful_boot = 0;
+    slotp->priority = 0;
+    slotp->tries_remaining = 0;
+    ret = bootctrl_write_metadata(&metadata);
+    if (ret < 0) {
+        return ret;
+    }
+
+    return 0;
+}
+
+int bootctrl_is_slot_bootable(boot_control_module_t  *module ,
+    unsigned slot)
+{
+    ALOGI("boot control bootctrl_is_slot_bootable\n");
+    int ret;
+    AvbABData metadata;
+
+    /* slot 0 is A , slot 1 is B */
+    if (slot >= 2) {
+        ALOGE("%s Wrong slot value %u\n", __func__,slot);
+        return -EINVAL;
+    }
+    ret = bootctrl_read_metadata(&metadata);
+    if (ret < 0) {
+        return ret;
+    }
+
+    return (metadata.slots[slot].priority != 0);
+}
+
+int bootctrl_get_bootup_status(boot_control_module_t  *module ,
+    unsigned slot)
+{
+    ALOGI("bootctrl bootctrl_get_bootup_status\n");
+    int ret = -1;
+    AvbABSlotData *slotp;
+    AvbABData metadata;
+
+    if(slot >= 2) {
+        ALOGE("%s Wrong slot value %u\n", __func__,slot);
+        return -1;
+    }
+
+    ret = bootctrl_read_metadata(&metadata);
+    if (ret < 0) {
+        return ret;
+    }
+
+    slotp = &metadata.slots[slot];
+
+    ALOGI("bootctrl bootctrl_get_bootup_status = %d\n", slotp->successful_boot);
+    return slotp->successful_boot;
+}
+
+const char *bootctrl_get_suffix(boot_control_module_t  *module ,
+    unsigned slot)
+{
+    ALOGI("boot control bootctrl_get_suffix %d\n",slot);
+    static const char* suffix[2] = {BOOTCTRL_SUFFIX_A, BOOTCTRL_SUFFIX_B};
+    if (slot >= 2)
+        return NULL;
+    return suffix[slot];
+}
+
+static struct hw_module_methods_t bootctrl_methods = {
+    .open  = NULL,
+};
+
+/* Boot Control Module implementation */
+boot_control_module_t HAL_MODULE_INFO_SYM = {
+    .common = {
+        .tag                 = HARDWARE_MODULE_TAG,
+        .module_api_version  = BOOT_CONTROL_MODULE_API_VERSION_0_1,
+        .hal_api_version     = HARDWARE_HAL_API_VERSION,
+        .id                  = BOOT_CONTROL_HARDWARE_MODULE_ID,
+        .name                = "boot_control HAL",
+        .author              = "Mediatek Corporation",
+        .methods             = &bootctrl_methods,
+    },
+    .init                 = bootctrl_init,
+    .getNumberSlots       = bootctrl_get_number_slots,
+    .getCurrentSlot       = bootctrl_get_current_slot,
+    .markBootSuccessful   =  bootctrl_mark_boot_successful,
+    .setActiveBootSlot    = bootctrl_set_active_boot_slot,
+    .setSlotAsUnbootable  = bootctrl_set_slot_as_unbootable,
+    .isSlotBootable       = bootctrl_is_slot_bootable,
+	  .getSuffix            = bootctrl_get_suffix,
+    .isSlotMarkedSuccessful = bootctrl_get_bootup_status,
+};
diff --git a/src/support/bootctrl/mediatek/bootctrl/bootctrl.h b/src/support/bootctrl/mediatek/bootctrl/bootctrl.h
new file mode 100644
index 0000000..b2a5c51
--- /dev/null
+++ b/src/support/bootctrl/mediatek/bootctrl/bootctrl.h
@@ -0,0 +1,131 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * SPDX-License-Identifier: MediaTekProprietary
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+
+/* THE HAL BOOTCTRL HEADER MUST BE IN SYNC WITH THE UBOOT BOOTCTRL HEADER */
+
+#ifndef _BOOTCTRL_H_
+#define _BOOTCTRL_H_
+
+#include <stdint.h>
+
+/* struct boot_ctrl occupies the slot_suffix field of
+ * struct bootloader_message */
+
+#define BOOTCTRL_SUFFIX_A           "_a"
+#define BOOTCTRL_SUFFIX_B           "_b"
+#define BOOT_CONTROL_VERSION    1
+
+#define BOOTCTRL_PROPERTY "ro.boot.slot_suffix"
+#define SLOT_SUFFIX_STR "androidboot.slot_suffix="
+#define COMMAND_LINE_PATH "/proc/cmdline"
+#define COMMAND_LINE_SIZE 2048
+
+/* AVB20 */
+/* Magic for the A/B struct when serialized. */
+#define AVB_AB_MAGIC "\0AB0"
+#define AVB_AB_MAGIC_LEN 4
+
+/* Versioning for the on-disk A/B metadata - keep in sync with avbtool. */
+#define AVB_AB_MAJOR_VERSION 1
+#define AVB_AB_MINOR_VERSION 0
+
+/* Size of AvbABData struct. */
+#define AVB_AB_DATA_SIZE 32
+
+/* Maximum values for slot data */
+#define AVB_AB_MAX_PRIORITY 15
+#define AVB_AB_MAX_TRIES_REMAINING 7
+
+#define BOOTDEV_TYPE_NAND 1
+#define BOOTDEV_TYPE_EMMC 2
+
+#define OFFSETOF_SLOT_SUFFIX 2048
+
+/* Struct used for recording per-slot metadata.
+ *
+ * When serialized, data is stored in network byte-order.
+ */
+typedef struct AvbABSlotData {
+  /* Slot priority. Valid values range from 0 to AVB_AB_MAX_PRIORITY,
+   * both inclusive with 1 being the lowest and AVB_AB_MAX_PRIORITY
+   * being the highest. The special value 0 is used to indicate the
+   * slot is unbootable.
+   */
+  uint8_t priority;
+
+  /* Number of times left attempting to boot this slot ranging from 0
+   * to AVB_AB_MAX_TRIES_REMAINING.
+   */
+  uint8_t tries_remaining;
+
+  /* Non-zero if this slot has booted successfully, 0 otherwise. */
+  uint8_t successful_boot;
+  /* For lk anti rollback*/
+  uint8_t efuse_write;
+  uint8_t bl_ver;
+  /* Reserved for future use. */
+  uint8_t reserved[3];
+
+} AvbABSlotData;
+
+/* Struct used for recording A/B metadata.
+ *
+ * When serialized, data is stored in network byte-order.
+ */
+typedef struct AvbABData {
+  /* Magic number used for identification - see AVB_AB_MAGIC. */
+  uint8_t magic[AVB_AB_MAGIC_LEN];
+
+  /* Version of on-disk struct - see AVB_AB_{MAJOR,MINOR}_VERSION. */
+  uint8_t version_major;
+  uint8_t version_minor;
+
+  /* Padding to ensure |slots| field start eight bytes in. */
+  uint8_t reserved1[2];
+
+  /* Per-slot metadata. */
+  AvbABSlotData slots[2];
+
+  /* Reserved for future use. */
+  uint8_t reserved2[12];
+
+  /* CRC32 of all 28 bytes preceding this field. */
+  uint32_t crc32;
+} AvbABData;
+#endif /* _BOOTCTRL_H_ */
diff --git a/src/support/bootctrl/mediatek/bootctrl/sd_misc.h b/src/support/bootctrl/mediatek/bootctrl/sd_misc.h
new file mode 100644
index 0000000..8dfc5d3
--- /dev/null
+++ b/src/support/bootctrl/mediatek/bootctrl/sd_misc.h
@@ -0,0 +1,239 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein is
+ * confidential and proprietary to MediaTek Inc. and/or its licensors. Without
+ * the prior written permission of MediaTek inc. and/or its licensors, any
+ * reproduction, modification, use or disclosure of MediaTek Software, and
+ * information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ *
+ * SPDX-License-Identifier: MediaTekProprietary
+ * MediaTek Inc. (C) 2016. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
+ * ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
+ * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
+ * NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
+ * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
+ * INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
+ * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
+ * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
+ * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
+ * SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
+ * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
+ * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
+ * RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
+ * MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
+ * CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek
+ * Software") have been modified by MediaTek Inc. All revisions are subject to
+ * any receiver's applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef SD_MISC_H
+#define SD_MISC_H
+
+#ifdef __KERNEL__
+#include <linux/bitops.h>
+#include <linux/mmc/host.h>
+#include <linux/mmc/card.h>
+#include <linux/mmc/core.h>
+#include <linux/mmc/mmc.h>
+#include <linux/mmc/sd.h>
+#endif
+
+
+
+struct msdc_ioctl {
+    int opcode;
+    int host_num;
+    int iswrite;
+    int trans_type;
+    unsigned int total_size;
+    unsigned int address;
+    unsigned int *buffer;
+    int cmd_pu_driving;
+    int cmd_pd_driving;
+    int dat_pu_driving;
+    int dat_pd_driving;
+    int clk_pu_driving;
+    int clk_pd_driving;
+    int ds_pu_driving;
+    int ds_pd_driving;
+    int rst_pu_driving;
+    int rst_pd_driving;
+    int clock_freq;
+    int partition;
+    int hopping_bit;
+    int hopping_time;
+    int result;
+    int sd30_mode;
+    int sd30_max_current;
+    int sd30_drive;
+    int sd30_power_control;
+};
+
+/**************for msdc_ssc***********************/
+#define AUDPLL_CTL_REG12                   (0xF0007070)
+#define AUDPLL_CTL_REG01                   (0xF00071E0)
+#define AUDPLL_CTL_REG02                   (0xF100000C)
+
+#define AUDPLL_TSEL_MASK                   (1792)   /* MASK = 00000111 00000000 */
+#define AUDPLL_TSEL_RESULT1                (0)  /* REG = 00000000 00000000   30.5us */
+#define AUDPLL_TSEL_RESULT2                (256)    /* REG = 00000001 00000000   61.0us */
+#define AUDPLL_TSEL_RESULT3                (512)    /* REG = 00000010 00000000   122.1us */
+#define AUDPLL_TSEL_RESULT4                (768)    /* REG = 00000011 00000000   244.1us */
+#define AUDPLL_TSEL_RESULT5                (1024)   /* REG = 00000100 00000000   448.3us */
+
+#define AUDPLL_BSEL_MASK                   (7)  /* MASK = 00000000 00000111 */
+#define AUDPLL_BSEL_RESULT0                (0)  /* REG = 00000000 00000000   REG init val */
+#define AUDPLL_BSEL_RESULT1                (1)  /* REG = 00000000 00000001   2.26MHz */
+#define AUDPLL_BSEL_RESULT2                (2)  /* REG = 00000000 00000010   4.52MHz */
+#define AUDPLL_BSEL_RESULT3                (4)  /* REG = 00000000 00000100   9.04MHz */
+
+#define SET_HOP_BIT_NONE                   (0)
+#define SET_HOP_BIT1                       (1)
+#define SET_HOP_BIT2                       (2)
+#define SET_HOP_BIT3                       (3)
+
+#define SET_HOP_TIME0                      (0)
+#define SET_HOP_TIME1                      (1)
+#define SET_HOP_TIME2                      (2)
+#define SET_HOP_TIME3                      (3)
+#define SET_HOP_TIME4                      (4)
+
+
+/**************for msdc_ssc***********************/
+
+#define MSDC_DRIVING_SETTING                (0)
+#define MSDC_CLOCK_FREQUENCY                (1)
+#define MSDC_SINGLE_READ_WRITE              (2)
+#define MSDC_MULTIPLE_READ_WRITE            (3)
+#define MSDC_GET_CID                        (4)
+#define MSDC_GET_CSD                        (5)
+#define MSDC_GET_EXCSD                      (6)
+#define MSDC_ERASE_PARTITION                (7)
+#define MSDC_HOPPING_SETTING                (8)
+
+#define MSDC_REINIT_SDCARD            _IOW('r', 9, int)
+
+#define MSDC_SD30_MODE_SWITCH               (10)
+#define MSDC_GET_BOOTPART                   (11)
+#define MSDC_SET_BOOTPART                   (12)
+#define MSDC_GET_PARTSIZE                   (13)
+#define MSDC_ERASE_SELECTED_AREA            (0x20)
+#define MSDC_CARD_DUNM_FUNC                 (0xff)
+
+typedef enum {
+    USER_PARTITION = 0,
+    BOOT_PARTITION_1,
+    BOOT_PARTITION_2,
+    RPMB_PARTITION,
+    GP_PARTITION_1,
+    GP_PARTITION_2,
+    GP_PARTITION_3,
+    GP_PARTITION_4,
+} PARTITON_ACCESS_T;
+
+typedef enum {
+    SDHC_HIGHSPEED = 0, /* 0 Host supports HS mode */
+    UHS_SDR12,      /* 1 Host supports UHS SDR12 mode */
+    UHS_SDR25,      /* 2 Host supports UHS SDR25 mode */
+    UHS_SDR50,      /* 3 Host supports UHS SDR50 mode */
+    UHS_SDR104,     /* 4 Host supports UHS SDR104 mode */
+    UHS_DDR50,      /* 5 Host supports UHS DDR50 mode */
+} SD3_MODE;
+
+typedef enum {
+    DRIVER_TYPE_A = 0,  /* 0 Host supports Driver Type A */
+    DRIVER_TYPE_B,      /* 1 Host supports Driver Type B */
+    DRIVER_TYPE_C,      /* 2 Host supports Driver Type C */
+    DRIVER_TYPE_D,      /* 3 Host supports Driver Type D */
+} SD3_DRIVE;
+
+typedef enum {
+    MAX_CURRENT_200 = 0,    /* 0 Host max current limit is 200mA */
+    MAX_CURRENT_400,    /* 1 Host max current limit is 400mA */
+    MAX_CURRENT_600,    /* 2 Host max current limit is 600mA */
+    MAX_CURRENT_800,    /* 3 Host max current limit is 800mA */
+} SD3_MAX_CURRENT;
+
+typedef enum {
+    SDXC_NO_POWER_CONTROL = 0,  /* 0  Host not supports >150mA current at 3.3V /3.0V/1.8V */
+    SDXC_POWER_CONTROL, /* 1 Host supports >150mA current at 3.3V /3.0V/1.8V */
+} SD3_POWER_CONTROL;
+
+typedef enum {
+    DUMP_INTO_BOOT_CARD_IPANIC = 0,
+    DUMP_INTO_BOOT_CARD_KDUMP = 1,
+    DUMP_INTO_EXTERN_CARD = 2,
+} DUMP_STORAGE_TYPE;
+
+typedef enum {
+    EMMC_CARD_BOOT = 0,
+    SD_CARD_BOOT,
+    EMMC_CARD,
+    SD_CARD,
+} STORAGE_TPYE;
+
+#define EXT_CSD_BOOT_SIZE_MULT           (226) /* R */
+#define EXT_CSD_HC_ERASE_GRP_SIZE        (224) /* RO */
+#define EXT_CSD_HC_WP_GPR_SIZE           (221) /* RO */
+#define EXT_CSD_RPMB_SIZE_MULT           (168) /* R */
+#define EXT_CSD_GP1_SIZE_MULT            (143) /* R/W 3 bytes */
+#define EXT_CSD_GP2_SIZE_MULT            (146) /* R/W 3 bytes */
+#define EXT_CSD_GP3_SIZE_MULT            (149) /* R/W 3 bytes */
+#define EXT_CSD_GP4_SIZE_MULT            (152) /* R/W 3 bytes */
+#define EXT_CSD_PART_CFG                 (179) /* R/W/E & R/W/E_P */
+#define EXT_CSD_CACHE_FLUSH              (32)
+#define EXT_CSD_CACHE_CTRL               (33)
+#define CAPACITY_2G                      (2 * 1024 * 1024 * 1024ULL)
+
+typedef enum {
+    EMMC_BOOT_NO_EN = 0,
+    EMMC_BOOT1_EN,
+    EMMC_BOOT2_EN,
+    EMMC_BOOT_USER = 7,
+    EMMC_BOOT_END
+} BOOT_PARTITION_EN;
+
+#ifdef CONFIG_MTK_GPT_SCHEME_SUPPORT
+typedef enum {
+    EMMC_PART_UNKNOWN=0
+    ,EMMC_PART_BOOT1
+    ,EMMC_PART_BOOT2
+    ,EMMC_PART_RPMB
+    ,EMMC_PART_GP1
+    ,EMMC_PART_GP2
+    ,EMMC_PART_GP3
+    ,EMMC_PART_GP4
+    ,EMMC_PART_USER
+    ,EMMC_PART_END
+} Region;
+#endif
+
+typedef enum {
+    CARD_INFO = 0,
+    DISK_INFO,
+    EMMC_USER_CAPACITY,
+    EMMC_CAPACITY,
+    EMMC_RESERVE,
+} GET_STORAGE_INFO;
+
+struct storage_info {
+    struct mmc_card *card;
+    struct gendisk *disk;
+    unsigned long long emmc_user_capacity;
+    unsigned long long emmc_capacity;
+    int emmc_reserve;
+};
+
+int msdc_get_info(STORAGE_TPYE storage_type, GET_STORAGE_INFO info_type, struct storage_info *info);
+
+#endif              /* end of SD_MISC_H */