Jelajahi Sumber

Merge "mtk bootctrl" into android-11

SmallPP420 4 tahun lalu
induk
melakukan
36803ba1f8
38 mengubah file dengan 2573 tambahan dan 537 penghapusan
  1. 4 0
      BoardConfig.mk
  2. 54 0
      bootctrl/1.0/Android.mk
  3. 185 0
      bootctrl/1.0/NOTICE
  4. 660 0
      bootctrl/1.0/bootctrl.cpp
  5. 88 0
      bootctrl/1.0/bootctrl.h
  6. 62 0
      bootctrl/2.0/Android.mk
  7. 62 0
      bootctrl/2.0/Android.mk.txt
  8. 202 0
      bootctrl/2.0/NOTICE
  9. 114 0
      bootctrl/2.0/avb/libavb/avb_crc32.c
  10. 41 0
      bootctrl/2.0/avb/libavb/avb_util.c
  11. 43 0
      bootctrl/2.0/avb/libavb/avb_util.h
  12. 773 0
      bootctrl/2.0/bootctrl.cpp
  13. 157 0
      bootctrl/2.0/bootctrl.h
  14. 48 0
      bootctrl/Android.mk
  15. 44 0
      device.mk
  16. 6 0
      recovery/root/boot-hal-service.rc
  17. 16 10
      recovery/root/init.recovery.mt6761.rc
  18. 0 526
      recovery/root/system/manifest.xml
  19. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/020f0000000000000000000000000000.drbin
  20. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/020f0000000000000000000000000000.tlbin
  21. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/030b0000000000000000000000000000.drbin
  22. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/030b0000000000000000000000000000.tlbin
  23. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/05120000000000000000000000000000.drbin
  24. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/05120000000000000000000000000000.tlbin
  25. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/06090000000000000000000000000000.drbin
  26. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/06090000000000000000000000000000.tlbin
  27. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/07010000000000000000000000000000.tlbin
  28. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/0706000000000000000000000000004d.tlbin
  29. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/07061000000000000000000000000000.tlbin
  30. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/07150000000000000000000000000000.drbin
  31. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/07150000000000000000000000000000.tlbin
  32. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/07170000000000000000000000000000.drbin
  33. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/07170000000000000000000000000000.tlbin
  34. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/08030000000000000000000000000000.tabin
  35. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/08110000000000000000000000000000.tabin
  36. TEMPAT SAMPAH
      recovery/root/vendor/app/mcRegistry/09190000000000000000000000000000.tlbin
  37. TEMPAT SAMPAH
      recovery/root/vendor/bin/hw/android.hardware.boot@1.1-service
  38. 14 1
      twrp.dependencies

+ 4 - 0
BoardConfig.mk

@@ -40,6 +40,10 @@ TARGET_SUPPORTS_64_BIT_APPS := true
 TARGET_BOARD_PLATFORM := mt6761
 BOARD_USES_MTK_HARDWARE := true
 
+#A/B
+AB_OTA_UPDATER := true
+TW_INCLUDE_REPACKTOOLS := true
+
 # Kernel
 BOARD_KERNEL_CMDLINE := bootopt=64S3,32N2,64N2
 TARGET_PREBUILT_KERNEL := $(DEVICE_PATH)/prebuilt/Image.gz

+ 54 - 0
bootctrl/1.0/Android.mk

@@ -0,0 +1,54 @@
+# Copyright (C) 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.
+
+# HAL Shared library the for libhardware.
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := bootctrl.$(MTK_PLATFORM_DIR)
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_PROPRIETARY_MODULE := true
+LOCAL_MODULE_OWNER := mtk
+LOCAL_SRC_FILES := bootctrl.cpp
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_C_INCLUDES += \
+    system/core/fs_mgr/include_fstab/fstab \
+    device/mediatek/common/kernel-headers \
+    bootable/recovery/bootloader_message/include
+
+LOCAL_HEADER_LIBRARIES := libhardware_headers libsystem_headers
+LOCAL_SHARED_LIBRARIES += liblog libbase libcutils
+LOCAL_STATIC_LIBRARIES += libfstab
+
+include $(BUILD_SHARED_LIBRARY)
+
+# Static library for the update_engine_sideload from recovery.
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := bootctrl.$(MTK_PLATFORM_DIR)
+LOCAL_MODULE_OWNER := mtk
+LOCAL_SRC_FILES := bootctrl.cpp
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_C_INCLUDES += \
+    system/core/fs_mgr/include_fstab/fstab \
+    device/mediatek/common/kernel-headers \
+    bootable/recovery/bootloader_message/include
+
+LOCAL_HEADER_LIBRARIES := libhardware_headers libsystem_headers
+LOCAL_SHARED_LIBRARIES += liblog libbase libcutils
+LOCAL_STATIC_LIBRARIES += libfstab
+
+include $(BUILD_STATIC_LIBRARY)

+ 185 - 0
bootctrl/1.0/NOTICE

@@ -0,0 +1,185 @@
+This MediaTek software package contains software with the following notices and under the following licenses:
+
+==============================================================================================================
+
+Copyright (C) 2015 Intel Corporation
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+
+

+ 660 - 0
bootctrl/1.0/bootctrl.cpp

@@ -0,0 +1,660 @@
+/* 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.
+ *
+ * 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 "bootctrl.h"
+#if !defined(ARCH_X86)
+#include <linux/scsi/ufs/ufs-mtk-ioctl.h>
+#include <linux/mmc/mmc-mtk-ioctl.h>
+#include <sys/ioctl.h>
+#endif
+#include <fstab.h>
+#include <bootloader_message/bootloader_message.h>
+
+// Debug for update_engine_sideload
+//#define ALOGE printf
+//#define ALOGI printf
+
+using android::fs_mgr::Fstab;
+using android::fs_mgr::GetEntryForMountPoint;
+using android::fs_mgr::ReadDefaultFstab;
+
+static char *blk_dev_path = NULL;
+
+static int bootctrl_read_metadata(boot_ctrl_t *bctrl)
+{
+    int fd, err;
+    ssize_t sz, size;
+    char *buf = (char *)bctrl;
+
+    fd = open(blk_dev_path, O_RDONLY);
+    if (fd < 0) {
+        err = errno;
+        ALOGE("%s Error opening metadata file: %s\n", __func__ ,strerror(errno));
+        return -err;
+    }
+    if (lseek(fd, OFFSETOF_SLOT_SUFFIX, SEEK_SET) < 0) {
+        err = errno;
+        ALOGE("%s Error seeking to metadata offset: %s\n", __func__ ,strerror(errno));
+        close(fd);
+        return -err;
+    }
+    size = sizeof(boot_ctrl_t);
+
+    do {
+        sz = read(fd, buf, size);
+        if (sz == 0) {
+            break;
+        } else if (sz < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            err = -errno;
+            ALOGE("%s Error reading metadata file\n", __func__);
+            close(fd);
+            return err;
+        }
+        size -= sz;
+        buf += sz;
+    } while(size > 0);
+
+    close(fd);
+
+    /* Check bootctrl magic number */
+    if (bctrl->magic != BOOTCTRL_MAGIC) {
+        ALOGE("metadata is not initialised or corrupted.\n");
+        return -EIO;
+    }
+    return 0;
+}
+
+static int bootctrl_write_metadata(boot_ctrl_t *bctrl)
+{
+    int fd, err;
+    ssize_t sz, size;
+    char *buf = (char *)bctrl;
+
+    fd = open(blk_dev_path, O_RDWR);
+    if (fd < 0) {
+        err = errno;
+        ALOGE("%s Error opening metadata file: %s\n", __func__,strerror(errno));
+        return -err;
+    }
+
+    if (lseek(fd, OFFSETOF_SLOT_SUFFIX, SEEK_SET) < 0) {
+        err = errno;
+        ALOGE("%s Error seeking to metadata offset: %s\n", __func__ ,strerror(errno));
+        close(fd);
+        return -err;
+    }
+    size = sizeof(boot_ctrl_t);
+    do {
+        sz = write(fd, buf, size);
+        if (sz == 0) {
+            break;
+        } else if (sz < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            err = -errno;
+            ALOGE("%s Error Writing metadata file\n",__func__);
+            close(fd);
+            return err;
+        }
+        size -= sz;
+        buf += sz;
+    } while(size > 0);
+
+    close(fd);
+    return 0;
+}
+
+void bootctrl_init(boot_control_module_t *module __unused)
+{
+    static Fstab fstab;
+    ALOGI("boot control HAL init");
+
+    if(blk_dev_path == NULL) {
+        /* Initial read fstab */
+        if(!ReadDefaultFstab(&fstab)) {
+            ALOGE("bootctrl read fstab fail\n");
+            return;
+        }
+
+        auto v = GetEntryForMountPoint(&fstab, "/misc");
+        if (v == nullptr) {
+            LOG(ERROR)<<"failed to get block device path by mount point";
+            return;
+         }
+        blk_dev_path = strdup(v->blk_device.c_str());
+    }
+
+    ALOGI("%s misc blk device path = %s\n", __func__ ,blk_dev_path);
+}
+
+unsigned bootctrl_get_number_slots(boot_control_module_t *module __unused)
+{
+    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 += sizeof(SLOT_SUFFIX_STR);
+    slot = (*str == 'a') ? 0 : 1;
+    free(ptr);
+    close(fd);
+
+    return slot;
+}
+
+static int mmc_read_extcsd(int fd, __u8 *ext_csd)
+{
+    int ret = 0;
+    struct mmc_ioc_cmd mmc_ioctl_cmd;
+
+    memset(ext_csd, 0, sizeof(__u8) * 512);
+    memset(&mmc_ioctl_cmd, 0, sizeof(mmc_ioctl_cmd));
+    mmc_ioctl_cmd.blocks = 1;
+    mmc_ioctl_cmd.blksz = 512;
+    mmc_ioctl_cmd.opcode = MMC_SEND_EXT_CSD;
+    mmc_ioctl_cmd.flags = MMC_CMD_ADTC | MMC_RSP_R1;
+    mmc_ioc_cmd_set_data(mmc_ioctl_cmd, ext_csd);
+
+    ret = ioctl(fd, MMC_IOC_CMD, &mmc_ioctl_cmd);
+
+    if (ret)
+        ALOGE("ioctl error, mmc_read_extcsd fail, ret = %d\n", ret);
+    return ret;
+}
+
+static int mmc_switch_bootpart(int fd, __u8 *ext_csd, __u8 bootpart)
+{
+    int ret = 0;
+    struct mmc_ioc_cmd mmc_ioctl_cmd;
+    __u8 val;
+
+    val = (ext_csd[EXT_CSD_PART_CONFIG] & ~(0x38)) | (bootpart << 3);
+    memset(&mmc_ioctl_cmd, 0, sizeof(mmc_ioctl_cmd));
+    mmc_ioctl_cmd.opcode = MMC_SWITCH;
+
+    mmc_ioctl_cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24)
+        | (EXT_CSD_PART_CONFIG << 16)
+        | val << 8
+        | EXT_CSD_CMD_SET_NORMAL;
+    mmc_ioctl_cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
+    mmc_ioc_cmd_set_data(mmc_ioctl_cmd, ext_csd);
+
+    ret = ioctl(fd, MMC_IOC_CMD, &mmc_ioctl_cmd);
+    if (ret)
+        ALOGE("ioctl error, mmc_switch_bootpart fail ret = %d\n", ret);
+    return ret;
+}
+
+static int emmc_set_active_boot_part(int bootpart)
+{
+    __u8 ext_csd[512];
+    __u8 cur_bootpart;
+    int ret;
+    int fd= open("/dev/block/mmcblk0", O_RDWR);
+    if (fd >= 0) {
+        ret = mmc_read_extcsd(fd, ext_csd);
+        if (ret) {
+            ALOGE("Could not read EXT_CSD, error=%d\n", ret);
+            close(fd);
+            return 1;
+        }
+
+        /* check current boot part */
+        cur_bootpart = (ext_csd[EXT_CSD_PART_CONFIG] >> 3) & 0x7;
+        if (cur_bootpart == bootpart)
+        {
+            ALOGI("Current boot part is boot%d, no neeed switch\n", cur_bootpart);
+        } else {
+            ALOGI("Current boot part is boot%d, need switch to %d\n",cur_bootpart, bootpart);
+            ret = mmc_switch_bootpart(fd, ext_csd, bootpart);
+                if(ret) {
+                    ALOGE("Could not switch boot part, error=%d\n", ret);
+                    close(fd);
+                    return 1;
+                }
+        }
+        close(fd);
+        return 0;
+        } else {
+            ALOGE("open /dev/block/mmcblk0 fail\n");
+            return 1;
+        }
+}
+
+int ufs_set_active_boot_part(int boot)
+{
+    struct ufs_ioctl_query_data idata;
+    unsigned char buf[1];
+    int fd, ret = 0;
+
+    fd = open("/dev/block/sdc", O_RDWR);
+
+    if (fd < 0) {
+        printf("%s: open device failed, err: %d\n", __func__, fd);
+        ret = -1;
+        goto out;
+    }
+
+    buf[0] = boot;           /* 1: BootLU A, 2: BootLU B */
+
+    idata.opcode = UPIU_QUERY_OPCODE_WRITE_ATTR;
+    idata.idn = QUERY_ATTR_IDN_BOOT_LUN_EN;
+    idata.idx = 0;
+    idata.buf_ptr = &buf[0];
+    idata.buf_byte = 1;
+
+    ret = ioctl(fd, UFS_IOCTL_QUERY, &idata);
+    if(ret < 0)
+        printf("ufs_set boot_part fail: %s\n", strerror(errno));
+out:
+    if(fd >= 0)
+        close(fd);
+    return ret;
+}
+
+static int get_boot_type(void) {
+    int fd;
+    size_t s;
+    char boot_type[4] = {'0'};
+
+    fd = open("/sys/class/BOOT/BOOT/boot/boot_type", O_RDONLY);
+    if (fd < 0) {
+        ALOGE("fail to open: %s\n", "/sys/class/BOOT/BOOT/boot/boot_type");
+        return -1;
+    }
+
+    s = read(fd, boot_type, sizeof(boot_type) - 1);
+    close(fd);
+
+    if (s <= 0) {
+        ALOGE("could not read boot type sys file\n");
+        return -1;
+    }
+
+    boot_type[s] = '\0';
+
+    return atoi(boot_type);
+}
+
+static int set_ota_result(int result, int offset) {
+
+    if (blk_dev_path == NULL) {
+      printf("Error: blk_dev_path is NULL\n");
+      return -1;
+    }
+
+    int dev = -1;
+    int count;
+
+    dev = open(blk_dev_path, O_WRONLY | O_SYNC);
+    if (dev < 0)  {
+        printf("Can't open %s\n(%s)\n", blk_dev_path, strerror(errno));
+        return -1;
+    }
+
+    if (lseek(dev, offset, SEEK_SET) == -1) {
+        printf("Failed seeking %s\n(%s)\n", blk_dev_path, strerror(errno));
+        close(dev);
+        return -1;
+    }
+
+    count = write(dev, &result, sizeof(result));
+    if (count != sizeof(result)) {
+        printf("Failed writing %s\n(%s)\n", blk_dev_path, strerror(errno));
+        close(dev);
+        return -1;
+    }
+    if (close(dev) != 0) {
+        printf("Failed closing %s\n(%s)\n", blk_dev_path, strerror(errno));
+        return -1;
+    }
+    sync();
+    return 1;
+}
+
+
+static int SetOTAResultForDMVerity(void) {
+
+    int ota_result_offset, ret = -1;
+    ota_result_offset = sizeof(bootloader_message_ab);
+    ret = set_ota_result(1, ota_result_offset);
+    return ret;
+}
+
+int switch_pl_boot_part(unsigned slot)
+{
+    int ret = 0;
+    int boot_part = 0;
+
+    /* slot 0 is A , slot 1 is B */
+    if (slot >= 2) {
+        ALOGE("%s Wrong Slot value %u\n", __func__ , slot);
+        return -1;
+    }
+
+    if(slot)
+      boot_part = 2;
+    else
+      boot_part = 1;
+
+    int mt_boot_type = get_boot_type();
+
+    if(mt_boot_type  == -1) {
+        ALOGI("Get boot type failed then use default device type\n");
+        mt_boot_type = FS_TYPE_EMMC;
+    }
+
+    /* EMMC */
+    if(mt_boot_type == FS_TYPE_EMMC) {
+        ALOGI("emmc_set_active_boot_part\n");
+        ret = emmc_set_active_boot_part(boot_part);
+        if(ret) {
+            ALOGE("emmc set boot_part fail\n");
+            return -1;
+        }
+        return 1;
+    }
+
+    /* UFS */
+    if(mt_boot_type == FS_TYPE_UFS) {
+        ALOGI ("boot_type is UFS\n");
+        ret = ufs_set_active_boot_part(boot_part);
+        if(ret) {
+            ALOGE("ufs set boot_part fail\n");
+            return -1;
+        }
+        return 1;
+    }
+    ALOGE("Un support phone type\n");
+    return -1;
+}
+
+uint32_t bootctrl_get_current_slot(boot_control_module_t *module __unused)
+{
+    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 __unused)
+{
+    ALOGI("boot control bootctrl_mark_boot_successful\n");
+    int ret;
+    uint32_t slot = 0;
+    boot_ctrl_t metadata;
+    slot_metadata_t *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.slot_info[slot];
+    slotp->successful_boot = 1;
+    slotp->retry_count = 3;
+
+    return bootctrl_write_metadata(&metadata);
+}
+
+int bootctrl_set_active_boot_slot(boot_control_module_t *module __unused,
+    unsigned slot)
+{
+    ALOGI("boot control bootctrl_set_active_boot_slot , slot is %d\n", slot);
+    int ret, slot2;
+    boot_ctrl_t metadata;
+    slot_metadata_t *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 prioority and reset retry count */
+    slotp = &metadata.slot_info[slot];
+    slotp->successful_boot = 0;
+    slotp->priority = 7;
+    slotp->retry_count = 3;
+    slotp->normal_boot = 1;
+
+    /* Set lower priority to another slot */
+    slot2 = (slot == 0) ? 1 : 0;
+    slotp = &metadata.slot_info[slot2];
+    if (slotp->priority >= 7) {
+        slotp->priority = 6;
+    }
+    slotp->retry_count = 3;
+    ret = bootctrl_write_metadata(&metadata);
+    if (ret < 0) {
+        return ret;
+    }
+
+    ret = switch_pl_boot_part(slot);
+    if (ret < 0) {
+        ALOGE("bootctrl_set_active_boot_slot switch boot part fail\n");
+        return ret;
+    }
+
+    ret = SetOTAResultForDMVerity();
+    if (ret < 0) {
+        ALOGE("bootctrl_set_active_boot_slot SetOTAResultForDMVerity fail\n");
+        return ret;
+    }
+    return 0;
+}
+
+int bootctrl_set_slot_as_unbootable(boot_control_module_t *module __unused,
+    unsigned slot)
+{
+    ALOGI("boot control bootctrl_set_slot_as_unbootable\n");
+    int ret;
+    boot_ctrl_t metadata;
+    slot_metadata_t *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 retry_count */
+    slotp = &metadata.slot_info[slot];
+    slotp->successful_boot = 0;
+    slotp->priority = 0;
+    slotp->retry_count = 0;
+    ret = bootctrl_write_metadata(&metadata);
+    if (ret < 0) {
+        return ret;
+    }
+
+    return 0;
+}
+
+int bootctrl_is_slot_bootable(boot_control_module_t *module __unused,
+    unsigned slot)
+{
+    ALOGI("boot control bootctrl_is_slot_bootable\n");
+    int ret;
+    boot_ctrl_t 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.slot_info[slot].priority != 0);
+}
+
+int bootctrl_get_bootup_status(boot_control_module_t *module __unused,
+    unsigned slot)
+{
+    ALOGI("bootctrl bootctrl_get_bootup_status\n");
+    int ret = -1;
+    slot_metadata_t *slotp;
+    boot_ctrl_t 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.slot_info[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 __unused,
+    unsigned slot)
+{
+    ALOGI("boot control bootctrl_get_suffix\n");
+    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,
+    .isSlotMarkedSuccessful = bootctrl_get_bootup_status,
+    .getSuffix            = bootctrl_get_suffix,
+};
+

+ 88 - 0
bootctrl/1.0/bootctrl.h

@@ -0,0 +1,88 @@
+/* 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.
+ *
+ * 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 OFFSETOF_SLOT_SUFFIX 2048
+
+#define BOOTCTRL_MAGIC 0x19191100
+#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
+
+enum mt_device_type {
+     FS_TYPE_MTD = 0,
+     FS_TYPE_EMMC,
+     FS_TYPE_UFS,
+     FS_TYPE_MNTL,
+     FS_TYPE_UNKNOWN,
+     FS_TYPE_INIT
+};
+
+typedef struct slot_metadata_vendor{
+    uint8_t priority : 3;
+    uint8_t retry_count : 3;
+    uint8_t successful_boot : 1;
+    uint8_t normal_boot : 1;
+} slot_metadata_t;
+
+typedef struct boot_ctrl {
+    /* Magic for identification */
+    uint32_t magic;
+
+    /* Version of struct. */
+    uint8_t version;
+
+    /* Information about each slot. */
+    slot_metadata_t slot_info[2];
+
+    uint8_t recovery_retry_count;
+} boot_ctrl_t;
+#endif /* _BOOTCTRL_H_ */

+ 62 - 0
bootctrl/2.0/Android.mk

@@ -0,0 +1,62 @@
+# Copyright (C) 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.
+
+# HAL Shared library the for libhardware.
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := bootctrl.$(MTK_PLATFORM_DIR)
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_PROPRIETARY_MODULE := true
+LOCAL_MODULE_OWNER := mtk
+LOCAL_SRC_FILES := \
+    bootctrl.cpp \
+    avb/libavb/avb_util.c \
+    avb/libavb/avb_crc32.c
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_C_INCLUDES += \
+    system/core/fs_mgr/include_fstab/fstab \
+    device/mediatek/common/kernel-headers \
+    bootable/recovery/bootloader_message/include
+
+LOCAL_HEADER_LIBRARIES := libhardware_headers libsystem_headers
+LOCAL_SHARED_LIBRARIES += liblog libbase libcutils
+LOCAL_STATIC_LIBRARIES += libfstab
+
+include $(BUILD_SHARED_LIBRARY)
+
+# Static library for the update_engine_sideload from recovery.
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := bootctrl.$(MTK_PLATFORM_DIR)
+LOCAL_MODULE_OWNER := mtk
+LOCAL_SRC_FILES := \
+    bootctrl.cpp \
+    avb/libavb/avb_util.c \
+    avb/libavb/avb_crc32.c
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_C_INCLUDES += \
+    system/core/fs_mgr/include_fstab/fstab \
+    device/mediatek/common/kernel-headers \
+    bootable/recovery/bootloader_message/include
+
+LOCAL_HEADER_LIBRARIES := libhardware_headers libsystem_headers
+LOCAL_SHARED_LIBRARIES += liblog libbase libcutils
+LOCAL_STATIC_LIBRARIES += libfstab
+
+include $(BUILD_STATIC_LIBRARY)

+ 62 - 0
bootctrl/2.0/Android.mk.txt

@@ -0,0 +1,62 @@
+# Copyright (C) 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.
+
+# HAL Shared library the for libhardware.
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := bootctrl.$(MTK_PLATFORM_DIR)
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_PROPRIETARY_MODULE := true
+LOCAL_MODULE_OWNER := mtk
+LOCAL_SRC_FILES := \
+    bootctrl.cpp \
+    avb/libavb/avb_util.c \
+    avb/libavb/avb_crc32.c
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_C_INCLUDES += \
+    system/core/fs_mgr/include_fstab/fstab \
+    device/mediatek/common/kernel-headers \
+    bootable/recovery/bootloader_message/include
+
+LOCAL_HEADER_LIBRARIES := libhardware_headers libsystem_headers
+LOCAL_SHARED_LIBRARIES += liblog libbase libcutils
+LOCAL_STATIC_LIBRARIES += libfstab
+
+include $(BUILD_SHARED_LIBRARY)
+
+# Static library for the update_engine_sideload from recovery.
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := bootctrl.$(MTK_PLATFORM_DIR)
+LOCAL_MODULE_OWNER := mtk
+LOCAL_SRC_FILES := \
+    bootctrl.cpp \
+    avb/libavb/avb_util.c \
+    avb/libavb/avb_crc32.c
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_C_INCLUDES += \
+    system/core/fs_mgr/include_fstab/fstab \
+    device/mediatek/common/kernel-headers \
+    bootable/recovery/bootloader_message/include
+
+LOCAL_HEADER_LIBRARIES := libhardware_headers libsystem_headers
+LOCAL_SHARED_LIBRARIES += liblog libbase libcutils
+LOCAL_STATIC_LIBRARIES += libfstab
+
+include $(BUILD_STATIC_LIBRARY)

+ 202 - 0
bootctrl/2.0/NOTICE

@@ -0,0 +1,202 @@
+This MediaTek software package contains software with the following notices and under the following licenses:
+
+==============================================================================================================
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+
+
+
+COPYRIGHT (C) 1986 Gary S. Brown.
+
+The MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
+persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
+Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+
+

+ 114 - 0
bootctrl/2.0/avb/libavb/avb_crc32.c

@@ -0,0 +1,114 @@
+/*-
+ *  COPYRIGHT (C) 1986 Gary S. Brown.  You may use this program, or
+ *  code or tables extracted from it, as desired without restriction.
+ */
+
+/*
+ *  First, the polynomial itself and its table of feedback terms.  The
+ *  polynomial is
+ *  X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
+ *
+ *  Note that we take it "backwards" and put the highest-order term in
+ *  the lowest-order bit.  The X^32 term is "implied"; the LSB is the
+ *  X^31 term, etc.  The X^0 term (usually shown as "+1") results in
+ *  the MSB being 1
+ *
+ *  Note that the usual hardware shift register implementation, which
+ *  is what we're using (we're merely optimizing it by doing eight-bit
+ *  chunks at a time) shifts bits into the lowest-order term.  In our
+ *  implementation, that means shifting towards the right.  Why do we
+ *  do it this way?  Because the calculated CRC must be transmitted in
+ *  order from highest-order term to lowest-order term.  UARTs transmit
+ *  characters in order from LSB to MSB.  By storing the CRC this way
+ *  we hand it to the UART in the order low-byte to high-byte; the UART
+ *  sends each low-bit to hight-bit; and the result is transmission bit
+ *  by bit from highest- to lowest-order term without requiring any bit
+ *  shuffling on our part.  Reception works similarly
+ *
+ *  The feedback terms table consists of 256, 32-bit entries.  Notes
+ *
+ *      The table can be generated at runtime if desired; code to do so
+ *      is shown later.  It might not be obvious, but the feedback
+ *      terms simply represent the results of eight shift/xor opera
+ *      tions for all combinations of data and CRC register values
+ *
+ *      The values must be right-shifted by eight bits by the "updcrc
+ *      logic; the shift must be unsigned (bring in zeroes).  On some
+ *      hardware you could probably optimize the shift in assembler by
+ *      using byte-swap instructions
+ *      polynomial $edb88320
+ *
+ *
+ * CRC32 code derived from work by Gary S. Brown.
+ */
+
+//#include "avb_sysdeps.h"
+#include "avb_util.h"
+
+/* Code taken from FreeBSD 8 */
+
+static uint32_t iavb_crc32_tab[] = {
+    0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
+    0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
+    0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
+    0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
+    0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
+    0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
+    0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
+    0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
+    0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
+    0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
+    0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
+    0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
+    0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
+    0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
+    0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
+    0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
+    0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
+    0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
+    0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
+    0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+    0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
+    0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
+    0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
+    0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
+    0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
+    0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
+    0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
+    0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
+    0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
+    0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
+    0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
+    0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
+    0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
+    0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
+    0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
+    0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
+    0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
+    0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
+    0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
+    0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+    0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
+    0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
+    0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
+
+/*
+ * A function that calculates the CRC-32 based on the table above is
+ * given below for documentation purposes. An equivalent implementation
+ * of this function that's actually used in the kernel can be found
+ * in sys/libkern.h, where it can be inlined.
+ */
+
+static uint32_t iavb_crc32(uint32_t crc_in, const uint8_t* buf, int size) {
+  const uint8_t* p = buf;
+  uint32_t crc;
+
+  crc = crc_in ^ ~0U;
+  while (size--)
+    crc = iavb_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
+  return crc ^ ~0U;
+}
+
+uint32_t avb_crc32(const uint8_t* buf, size_t size) {
+  return iavb_crc32(0, buf, size);
+}

+ 41 - 0
bootctrl/2.0/avb/libavb/avb_util.c

@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "avb_util.h"
+
+//#include <stdarg.h>
+
+/* Converts a 32-bit unsigned integer from host to big-endian byte order. */
+uint32_t avb_htobe32(uint32_t in) {
+  union {
+    uint32_t word;
+    uint8_t bytes[4];
+  } ret;
+  ret.bytes[0] = (in >> 24) & 0xff;
+  ret.bytes[1] = (in >> 16) & 0xff;
+  ret.bytes[2] = (in >> 8) & 0xff;
+  ret.bytes[3] = in & 0xff;
+  return ret.word;
+}
+

+ 43 - 0
bootctrl/2.0/avb/libavb/avb_util.h

@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef AVB_UTIL_H_
+#define AVB_UTIL_H_
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+/* Converts a 32-bit unsigned integer from host to big-endian byte order. */
+uint32_t avb_htobe32(uint32_t in);
+
+/* Calculates the CRC-32 for data in |buf| of size |buf_size|. */
+uint32_t avb_crc32(const uint8_t* buf, size_t buf_size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* AVB_UTIL_H_ */

+ 773 - 0
bootctrl/2.0/bootctrl.cpp

@@ -0,0 +1,773 @@
+/* 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.
+ *
+ * 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 "bootctrl.h"
+#if !defined(ARCH_X86)
+#include <linux/scsi/ufs/ufs-mtk-ioctl.h>
+#include <linux/mmc/mmc-mtk-ioctl.h>
+#include <sys/ioctl.h>
+#endif
+#include <fstab.h>
+#include "avb/libavb/avb_util.h"
+#include <bootloader_message/bootloader_message.h>
+
+// Debug for update_engine_sideload
+//#define ALOGE printf
+//#define ALOGI printf
+
+using android::fs_mgr::Fstab;
+using android::fs_mgr::GetEntryForMountPoint;
+using android::fs_mgr::ReadDefaultFstab;
+
+static char *blk_dev_path = NULL;
+
+static int bootctrl_read_metadata(AvbABData *bctrl)
+{
+    int fd, err;
+    ssize_t sz, size;
+    char *buf = (char *)bctrl;
+
+    fd = open(blk_dev_path, O_RDONLY);
+    if (fd < 0) {
+        err = errno;
+        ALOGE("%s Error opening metadata file: %s\n", __func__ ,strerror(errno));
+        return -err;
+    }
+    if (lseek(fd, OFFSETOF_SLOT_SUFFIX, SEEK_SET) < 0) {
+        err = errno;
+        ALOGE("%s Error seeking to metadata offset: %s\n", __func__ ,strerror(errno));
+        close(fd);
+        return -err;
+    }
+    size = sizeof(AvbABData);
+
+    do {
+        sz = read(fd, buf, size);
+        if (sz == 0) {
+            break;
+        } else if (sz < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            err = -errno;
+            ALOGE("%s Error reading metadata file\n", __func__);
+            close(fd);
+            return err;
+        }
+        size -= sz;
+        buf += sz;
+    } while(size > 0);
+
+    close(fd);
+
+    /* Check bootctrl magic number */
+    if (memcmp(bctrl->magic , AVB_AB_MAGIC, AVB_AB_MAGIC_LEN) != 0) {
+        ALOGE("metadata is not initialised or corrupted.\n");
+        return -EIO;
+    }
+    return 0;
+}
+
+static int bootctrl_read_metadata_offset(boot_ctrl_t *bctrl,int offset)
+{
+    int fd, err;
+    ssize_t sz, size;
+    char *buf = (char *)bctrl;
+
+    fd = open(blk_dev_path, O_RDONLY);
+    if (fd < 0) {
+        err = errno;
+        ALOGE("%s Error opening metadata file: %s\n", __func__ ,strerror(errno));
+        return -err;
+    }
+    if (lseek(fd, OFFSETOF_SLOT_SUFFIX + offset, SEEK_SET) < 0) {
+        err = errno;
+        ALOGE("%s Error seeking to metadata offset: %s\n", __func__ ,strerror(errno));
+        close(fd);
+        return -err;
+    }
+    size = sizeof(boot_ctrl_t);
+
+    do {
+        sz = read(fd, buf, size);
+        if (sz == 0) {
+            break;
+        } else if (sz < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            err = -errno;
+            ALOGE("%s Error reading metadata file\n", __func__);
+            close(fd);
+            return err;
+        }
+        size -= sz;
+        buf += sz;
+    } while(size > 0);
+
+    close(fd);
+
+    /* Check bootctrl magic number */
+    if (bctrl->magic == BOOTCTRL_MAGIC) {
+        ALOGE("backup exist, need restore.\n");
+        return 1;
+    }
+    return 0;
+}
+static int bootctrl_write_metadata_bootctrv1(boot_ctrl_t *bctrl)
+{
+    int fd, err;
+    ssize_t sz, size;
+    char *buf = (char *)bctrl;
+
+    fd = open(blk_dev_path, O_RDWR);
+    if (fd < 0) {
+        err = errno;
+        ALOGE("%s Error opening metadata file: %s\n", __func__,strerror(errno));
+        return -err;
+    }
+
+    if (lseek(fd, OFFSETOF_SLOT_SUFFIX, SEEK_SET) < 0) {
+        err = errno;
+        ALOGE("%s Error seeking to metadata offset: %s\n", __func__ ,strerror(errno));
+        close(fd);
+        return -err;
+    }
+    size = sizeof(boot_ctrl_t);
+    do {
+        sz = write(fd, buf, size);
+        if (sz == 0) {
+            break;
+        } else if (sz < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            err = -errno;
+            ALOGE("%s Error Writing metadata file\n",__func__);
+            close(fd);
+            return err;
+        }
+        size -= sz;
+        buf += sz;
+    } while(size > 0);
+
+    close(fd);
+    return 0;
+}
+
+
+static int bootctrl_write_metadata(AvbABData *bctrl)
+{
+    int fd, err;
+    ssize_t sz, size;
+    char *buf = (char *)bctrl;
+
+    fd = open(blk_dev_path, O_RDWR);
+    if (fd < 0) {
+        err = errno;
+        ALOGE("%s Error opening metadata file: %s\n", __func__,strerror(errno));
+        return -err;
+    }
+
+    if (lseek(fd, OFFSETOF_SLOT_SUFFIX, SEEK_SET) < 0) {
+        err = errno;
+        ALOGE("%s Error seeking to metadata offset: %s\n", __func__ ,strerror(errno));
+        close(fd);
+        return -err;
+    }
+    size = sizeof(AvbABData);
+    bctrl->crc32 = avb_htobe32(
+             avb_crc32((const uint8_t*)bctrl, sizeof(AvbABData) - sizeof(uint32_t)));
+    do {
+        sz = write(fd, buf, size);
+        if (sz == 0) {
+            break;
+        } else if (sz < 0) {
+            if (errno == EINTR) {
+                continue;
+            }
+            err = -errno;
+            ALOGE("%s Error Writing metadata file\n",__func__);
+            close(fd);
+            return err;
+        }
+        size -= sz;
+        buf += sz;
+    } while(size > 0);
+
+    close(fd);
+    return 0;
+}
+
+void bootctrl_init(boot_control_module_t *module __unused)
+{
+    static Fstab fstab;
+    ALOGI("boot control HAL init");
+
+    if(blk_dev_path == NULL) {
+        /* Initial read fstab */
+        if(!ReadDefaultFstab(&fstab)) {
+            ALOGE("bootctrl read fstab fail\n");
+            return;
+        }
+
+        auto v = GetEntryForMountPoint(&fstab, "/misc");
+        if (v == nullptr) {
+            LOG(ERROR)<<"failed to get block device path by mount point";
+            return;
+        }
+
+        blk_dev_path = strdup(v->blk_device.c_str());
+    }
+
+    ALOGI("%s misc blk device path = %s\n", __func__ ,blk_dev_path);
+}
+
+unsigned bootctrl_get_number_slots(boot_control_module_t *module __unused)
+{
+    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 += sizeof(SLOT_SUFFIX_STR);
+    slot = (*str == 'a') ? 0 : 1;
+    free(ptr);
+    close(fd);
+
+    return slot;
+}
+
+static int mmc_read_extcsd(int fd, __u8 *ext_csd)
+{
+    int ret = 0;
+    struct mmc_ioc_cmd mmc_ioctl_cmd;
+
+    memset(ext_csd, 0, sizeof(__u8) * 512);
+    memset(&mmc_ioctl_cmd, 0, sizeof(mmc_ioctl_cmd));
+    mmc_ioctl_cmd.blocks = 1;
+    mmc_ioctl_cmd.blksz = 512;
+    mmc_ioctl_cmd.opcode = MMC_SEND_EXT_CSD;
+    mmc_ioctl_cmd.flags = MMC_CMD_ADTC | MMC_RSP_R1;
+    mmc_ioc_cmd_set_data(mmc_ioctl_cmd, ext_csd);
+
+    ret = ioctl(fd, MMC_IOC_CMD, &mmc_ioctl_cmd);
+
+    if (ret)
+        ALOGE("ioctl error, mmc_read_extcsd fail, ret = %d\n", ret);
+    return ret;
+}
+
+static int mmc_switch_bootpart(int fd, __u8 *ext_csd, __u8 bootpart)
+{
+    int ret = 0;
+    struct mmc_ioc_cmd mmc_ioctl_cmd;
+    __u8 val;
+
+    val = (ext_csd[EXT_CSD_PART_CONFIG] & ~(0x38)) | (bootpart << 3);
+    memset(&mmc_ioctl_cmd, 0, sizeof(mmc_ioctl_cmd));
+    mmc_ioctl_cmd.opcode = MMC_SWITCH;
+
+    mmc_ioctl_cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24)
+        | (EXT_CSD_PART_CONFIG << 16)
+        | val << 8
+        | EXT_CSD_CMD_SET_NORMAL;
+    mmc_ioctl_cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
+    mmc_ioc_cmd_set_data(mmc_ioctl_cmd, ext_csd);
+
+    ret = ioctl(fd, MMC_IOC_CMD, &mmc_ioctl_cmd);
+    if (ret)
+        ALOGE("ioctl error, mmc_switch_bootpart fail ret = %d\n", ret);
+    return ret;
+}
+
+static int emmc_set_active_boot_part(int bootpart)
+{
+    __u8 ext_csd[512];
+    __u8 cur_bootpart;
+    int ret;
+    int fd= open("/dev/block/mmcblk0", O_RDWR);
+    if (fd >= 0) {
+        ret = mmc_read_extcsd(fd, ext_csd);
+        if (ret) {
+            ALOGE("Could not read EXT_CSD, error=%d\n", ret);
+            close(fd);
+            return 1;
+        }
+
+        /* check current boot part */
+        cur_bootpart = (ext_csd[EXT_CSD_PART_CONFIG] >> 3) & 0x7;
+        if (cur_bootpart == bootpart)
+        {
+            ALOGI("Current boot part is boot%d, no neeed switch\n", cur_bootpart);
+            printf("Current boot part is boot%d, no neeed switch\n", cur_bootpart);
+        } else {
+            ALOGI("Current boot part is boot%d, need switch to %d\n",cur_bootpart, bootpart);
+            printf("Current boot part is boot%d, need switch to %d\n",cur_bootpart, bootpart);
+            ret = mmc_switch_bootpart(fd, ext_csd, bootpart);
+                if(ret) {
+                    ALOGE("Could not switch boot part, error=%d\n", ret);
+                    close(fd);
+                    return 1;
+                }
+        }
+        close(fd);
+        return 0;
+        } else {
+            ALOGE("open /dev/block/mmcblk0 fail\n");
+            return 1;
+        }
+}
+
+static int ufs_set_active_boot_part(int boot)
+{
+    struct ufs_ioctl_query_data idata;
+    unsigned char buf[1];
+    int fd, ret = 0;
+
+    fd = open("/dev/block/sdc", O_RDWR);
+
+    if (fd < 0) {
+        printf("%s: open device failed, err: %d\n", __func__, fd);
+        ret = -1;
+        goto out;
+    }
+
+    buf[0] = boot;           /* 1: BootLU A, 2: BootLU B */
+
+    idata.opcode = UPIU_QUERY_OPCODE_WRITE_ATTR;
+    idata.idn = QUERY_ATTR_IDN_BOOT_LUN_EN;
+    idata.idx = 0;
+    idata.buf_ptr = &buf[0];
+    idata.buf_byte = 1;
+
+    ret = ioctl(fd, UFS_IOCTL_QUERY, &idata);
+    if(ret < 0)
+        printf("ufs_set boot_part fail: %s\n", strerror(errno));
+out:
+    if(fd >= 0)
+        close(fd);
+    return ret;
+}
+
+static int get_boot_type(void) {
+    int fd;
+    size_t s;
+    char boot_type[4] = {'0'};
+
+    fd = open("/sys/class/BOOT/BOOT/boot/boot_type", O_RDONLY);
+    if (fd < 0) {
+        ALOGE("fail to open: %s\n", "/sys/class/BOOT/BOOT/boot/boot_type");
+        return -1;
+    }
+
+    s = read(fd, boot_type, sizeof(boot_type) - 1);
+    close(fd);
+
+    if (s <= 0) {
+        ALOGE("could not read boot type sys file\n");
+        return -1;
+    }
+
+    boot_type[s] = '\0';
+
+    return atoi(boot_type);
+}
+
+static int set_ota_result(int result, int offset) {
+
+    if (blk_dev_path == NULL) {
+      printf("Error: blk_dev_path is NULL\n");
+      return -1;
+    }
+
+    int dev = -1;
+    int count;
+
+    dev = open(blk_dev_path, O_WRONLY | O_SYNC);
+    if (dev < 0)  {
+        printf("Can't open %s\n(%s)\n", blk_dev_path, strerror(errno));
+        return -1;
+    }
+
+    if (lseek(dev, offset, SEEK_SET) == -1) {
+        printf("Failed seeking %s\n(%s)\n", blk_dev_path, strerror(errno));
+        close(dev);
+        return -1;
+    }
+
+    count = write(dev, &result, sizeof(result));
+    if (count != sizeof(result)) {
+        printf("Failed writing %s\n(%s)\n", blk_dev_path, strerror(errno));
+        close(dev);
+        return -1;
+    }
+    if (close(dev) != 0) {
+        printf("Failed closing %s\n(%s)\n", blk_dev_path, strerror(errno));
+        return -1;
+    }
+    sync();
+    return 1;
+}
+
+
+static int SetOTAResultForDMVerity(void) {
+
+    int ota_result_offset, ret = -1;
+    ota_result_offset = sizeof(bootloader_message_ab);
+    ret = set_ota_result(1, ota_result_offset);
+    return ret;
+}
+
+
+int switch_pl_boot_part(unsigned slot)
+{
+    int ret = 0;
+    int boot_part = 0;
+    boot_ctrl_t metadata;    
+    slot_metadata_t *slotp;
+
+    /* slot 0 is A , slot 1 is B */
+    if (slot >= 2) {
+        ALOGE("%s Wrong Slot value %u\n", __func__ , slot);
+        return -1;
+    }
+
+    if(slot)
+      boot_part = 2;
+    else
+      boot_part = 1;
+
+    int mt_boot_type = get_boot_type();
+
+    if(mt_boot_type  == -1) {
+        ALOGI("Get boot type failed then use default device type\n");
+        mt_boot_type = FS_TYPE_EMMC;
+    }
+    if(slot ==1)
+    {
+        //restore backup bootctrv1
+        if(bootctrl_read_metadata_offset(&metadata,32))
+        {
+            slotp = &metadata.slot_info[1];
+            slotp->successful_boot = 0;
+            slotp->priority = 7;
+            slotp->retry_count = 3;
+            slotp->normal_boot = 1;
+            slotp = &metadata.slot_info[0];
+            slotp->successful_boot = 0;
+            slotp->priority = 0;
+            slotp->retry_count = 0;
+            slotp->normal_boot = 0;
+            bootctrl_write_metadata_bootctrv1(&metadata);
+        }
+    }
+
+    /* EMMC */
+    if(mt_boot_type == FS_TYPE_EMMC) {
+        ALOGI("emmc_set_active_boot_part\n");
+        ret = emmc_set_active_boot_part(boot_part);
+        if(ret) {
+            ALOGE("emmc set boot_part fail\n");
+            return -1;
+        }
+        return 1;
+    }
+
+    /* UFS */
+    if(mt_boot_type == FS_TYPE_UFS) {
+        ALOGI ("boot_type is UFS\n");
+        ret = ufs_set_active_boot_part(boot_part);
+        if(ret) {
+            ALOGE("ufs set boot_part fail\n");
+            return -1;
+        }
+        return 1;
+    }
+    ALOGE("Un support phone type\n");
+    return -1;
+}
+
+uint32_t bootctrl_get_current_slot(boot_control_module_t *module __unused)
+{
+    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 __unused)
+{
+    ALOGI("boot control bootctrl_mark_boot_successful\n");
+    int ret;
+    uint32_t 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 __unused,
+    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;
+    }
+
+    ret = switch_pl_boot_part(slot);
+    if (ret < 0) {
+        ALOGE("bootctrl_set_active_boot_slot switch boot part fail\n");
+        return ret;
+    }
+
+    ret = SetOTAResultForDMVerity();
+    if (ret < 0) {
+        ALOGE("bootctrl_set_active_boot_slot SetOTAResultForDMVerity fail\n");
+        return ret;
+    }
+
+    return 0;
+}
+
+int bootctrl_set_slot_as_unbootable(boot_control_module_t *module __unused,
+    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 __unused,
+    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 __unused,
+    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 __unused,
+    unsigned slot)
+{
+    ALOGI("boot control bootctrl_get_suffix\n");
+    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,
+    .isSlotMarkedSuccessful = bootctrl_get_bootup_status,
+    .getSuffix            = bootctrl_get_suffix,
+};
+

+ 157 - 0
bootctrl/2.0/bootctrl.h

@@ -0,0 +1,157 @@
+/* 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.
+ *
+ * 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 OFFSETOF_SLOT_SUFFIX 2048
+
+#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
+
+enum mt_device_type {
+     FS_TYPE_MTD = 0,
+     FS_TYPE_EMMC,
+     FS_TYPE_UFS,
+     FS_TYPE_MNTL,
+     FS_TYPE_UNKNOWN,
+     FS_TYPE_INIT
+};
+
+/* AVB20 */
+/* Magic for the A/B struct when serialized. */
+#define BOOTCTRL_MAGIC 0x19191100
+
+#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
+
+/* 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;
+
+  /* Reserved for future use. */
+  uint8_t reserved[1];
+
+} 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;
+
+typedef struct slot_metadata_vendor{
+    uint8_t priority : 3;
+    uint8_t retry_count : 3;
+    uint8_t successful_boot : 1;
+    uint8_t normal_boot : 1;
+} slot_metadata_t;
+
+typedef struct boot_ctrl {
+    /* Magic for identification */
+    uint32_t magic;
+
+    /* Version of struct. */
+    uint8_t version;
+
+    /* Information about each slot. */
+    slot_metadata_t slot_info[2];
+
+    uint8_t recovery_retry_count;
+} boot_ctrl_t;
+
+
+#endif /* _BOOTCTRL_H_ */

+ 48 - 0
bootctrl/Android.mk

@@ -0,0 +1,48 @@
+# 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.
+#
+# MediaTek Inc. (C) 2010. 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.
+
+
+### ============================================================================
+### platform bootctrl HAL
+### ============================================================================
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+ifeq ($(strip $(MTK_BOOTCTRL_VERSION)), 1.0)
+MTK_BOOTCTRL_VERSION=1.0
+else
+MTK_BOOTCTRL_VERSION=2.0
+endif
+include $(LOCAL_PATH)/$(MTK_BOOTCTRL_VERSION)/Android.mk
+

+ 44 - 0
device.mk

@@ -2,3 +2,47 @@ LOCAL_PATH := device/nokia/WSP_sprout
 
 PRODUCT_COPY_FILES += $(call find-copy-subdir-files,*,$(LOCAL_PATH)/recovery/root,recovery/root) \
         $(LOCAL_PATH)/prebuilt/dtb.img:dtb.img
+
+# A/B
+AB_OTA_PARTITIONS += \
+    boot \
+    custom \
+    dtbo \
+    elabel \
+    lk \
+    md1img \
+    preloader \
+    scp \
+    spmfw \
+    sspm \
+    system \
+    tee \
+    vbmeta \
+    vendor \
+
+AB_OTA_POSTINSTALL_CONFIG += \
+    RUN_POSTINSTALL_system=true \
+    POSTINSTALL_PATH_system=system/bin/otapreopt_script \
+    FILESYSTEM_TYPE_system=ext4 \
+    POSTINSTALL_OPTIONAL_system=true
+
+# Boot control HAL
+PRODUCT_PACKAGES += \
+    android.hardware.boot@1.1-impl \
+    android.hardware.boot@1.1-service
+
+PRODUCT_PACKAGES += \
+    bootctrl.mt6761
+
+PRODUCT_STATIC_BOOT_CONTROL_HAL := \
+    bootctrl.mt6761 \
+    libgptutils \
+    libz \
+    libcutils
+
+PRODUCT_PACKAGES += \
+    otapreopt_script \
+    cppreopts.sh \
+    update_engine \
+    update_verifier \
+    update_engine_sideload

+ 6 - 0
recovery/root/boot-hal-service.rc

@@ -0,0 +1,6 @@
+service vendor-boot-hal-1-1 /vendor/bin/hw/android.hardware.boot@1.1-service
+    interface android.hardware.boot@1.0::IBootControl default
+    interface android.hardware.boot@1.1::IBootControl default
+    class early_hal
+    user root
+    group root

+ 16 - 10
recovery/root/init.recovery.mt6761.rc

@@ -1,8 +1,10 @@
 import /init.recovery.trustonic.rc
+import /boot-hal-service.rc
 
 on fs
+    exec u:r:recovery:s0 root root -- /system/bin/cp -arf /vendor/app/mcRegistry/. /odm/vendor/app/mcRegistry/
+
     install_keyring
-    mkdir /mnt/vendor/persist 0777 system system
     setprop crypto.ready 1
 
     write /sys/class/power_supply/usb/otg_switch 1
@@ -22,11 +24,13 @@ service keystore /system/bin/keystore /tmp/misc/keystore
     seclabel u:r:recovery:s0
 
 on property:hwservicemanager.ready=true
+    start hal_cryptoeng_oppo
     start mobicore
     start keymaster-3-0
     start gatekeeper-1-0
 
 on property:ro.crypto.state=unsupported
+    stop hal_cryptoeng_oppo
     stop mobicore
     stop keymaster-3-0
     stop gatekeeper-1-0
@@ -34,6 +38,7 @@ on property:ro.crypto.state=unsupported
     stop hwservicemanager
 
 on property:ro.crypto.state=unencrypted
+    stop hal_cryptoeng_oppo
     stop mobicore
     stop keymaster-3-0
     stop gatekeeper-1-0
@@ -47,13 +52,20 @@ on post-fs-data
 
 on boot
     start health-hal-2-1
-
-service gatekeeper-1-0 /vendor/bin/hw/android.hardware.gatekeeper@1.0-service
+    vendor-boot-hal-1-1
+    
+service gatekeeper-1-0 /system/bin/hw/android.hardware.gatekeeper@1.0-service
     user root
     group root
     disabled
     seclabel u:r:recovery:s0
 
+service hal_cryptoeng_oppo /vendor/bin/hw/vendor.oplus.hardware.cryptoeng@1.0-service
+    user root
+    group root input sdcard_rw sdcard_r
+    disabled
+    seclabel u:r:recovery:s0
+
 service keymaster-3-0 /vendor/bin/hw/android.hardware.keymaster@3.0-service.trustonic
     user root
     group root drmrpc
@@ -61,6 +73,7 @@ service keymaster-3-0 /vendor/bin/hw/android.hardware.keymaster@3.0-service.trus
     seclabel u:r:recovery:s0
 
 on property:enable.fbe.service=0
+    stop hal_cryptoeng_oppo
     stop mobicore
     stop servicemanager
     stop hwservicemanager
@@ -77,13 +90,6 @@ on fs && property:ro.debuggable=0
     # set charging free due to it wait for USB activation
     start adbd
 
-on property:twrp.decrypt.done=true
-    stop mobicore
-    stop servicemanager
-    stop hwservicemanager
-    stop keymaster-4-0
-    stop gatekeeper-1-0
-
 on post-fs
     # Support A/B feature for emmc boot region
     symlink /dev/block/sda /dev/block/mmcblk0boot0

+ 0 - 526
recovery/root/system/manifest.xml

@@ -1,526 +0,0 @@
-<!--
-    Input:
-        manifest_target_level_p.xml
-        manifest_sensor_hidl_v2.xml
-        manifest_audio.xml
-        manifest_dsds.xml
-        manifest_radioconfig.xml
-        manifest_bgservice.xml
-        manifest_wifi.xml
-        manifest_videotelephony.xml
-        manifest_secure_element_dsds.xml
-        manifest_mtkcodecservice.xml
-        manifest_km3.xml
-        manifest_tee.xml
-        manifest_trustonic.xml
-        manifest_boot.xml
-        manifest_netdagent.xml
-        manifest_pq.xml
-        manifest_mmservice.xml
-        manifest_gnss.xml
-        manifest.xml
--->
-<manifest version="2.0" type="device" target-level="3">
-    <hal format="hidl">
-        <name>android.hardware.audio</name>
-        <transport>hwbinder</transport>
-        <version>6.0</version>
-        <interface>
-            <name>IDevicesFactory</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@6.0::IDevicesFactory/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.audio.effect</name>
-        <transport>hwbinder</transport>
-        <version>6.0</version>
-        <interface>
-            <name>IEffectsFactory</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@6.0::IEffectsFactory/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.bluetooth</name>
-        <transport>hwbinder</transport>
-        <version>1.0</version>
-        <interface>
-            <name>IBluetoothHci</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.0::IBluetoothHci/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.bluetooth.audio</name>
-        <transport>hwbinder</transport>
-        <version>2.0</version>
-        <interface>
-            <name>IBluetoothAudioProvidersFactory</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@2.0::IBluetoothAudioProvidersFactory/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.boot</name>
-        <transport>hwbinder</transport>
-        <version>1.0</version>
-        <interface>
-            <name>IBootControl</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.0::IBootControl/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.camera.provider</name>
-        <transport>hwbinder</transport>
-        <version>2.6</version>
-        <interface>
-            <name>ICameraProvider</name>
-            <instance>internal/0</instance>
-        </interface>
-        <fqname>@2.6::ICameraProvider/internal/0</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.configstore</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <interface>
-            <name>ISurfaceFlingerConfigs</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.1::ISurfaceFlingerConfigs/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.gatekeeper</name>
-        <transport>hwbinder</transport>
-        <version>1.0</version>
-        <interface>
-            <name>IGatekeeper</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.0::IGatekeeper/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.gnss</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <version>2.1</version>
-        <interface>
-            <name>IGnss</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.1::IGnss/default</fqname>
-        <fqname>@2.1::IGnss/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.graphics.allocator</name>
-        <transport>hwbinder</transport>
-        <version>4.0</version>
-        <interface>
-            <name>IAllocator</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@4.0::IAllocator/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.graphics.composer</name>
-        <transport>hwbinder</transport>
-        <version>2.1</version>
-        <interface>
-            <name>IComposer</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@2.1::IComposer/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.graphics.mapper</name>
-        <transport arch="32+64">passthrough</transport>
-        <version>4.0</version>
-        <interface>
-            <name>IMapper</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@4.0::IMapper/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.keymaster</name>
-        <transport>hwbinder</transport>
-        <version>3.0</version>
-        <interface>
-            <name>IKeymasterDevice</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@3.0::IKeymasterDevice/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.media.omx</name>
-        <transport>hwbinder</transport>
-        <version>1.0</version>
-        <interface>
-            <name>IOmx</name>
-            <instance>default</instance>
-        </interface>
-        <interface>
-            <name>IOmxStore</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.0::IOmx/default</fqname>
-        <fqname>@1.0::IOmxStore/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.memtrack</name>
-        <transport>hwbinder</transport>
-        <version>1.0</version>
-        <interface>
-            <name>IMemtrack</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.0::IMemtrack/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.radio</name>
-        <transport>hwbinder</transport>
-        <version>1.2</version>
-        <interface>
-            <name>IRadio</name>
-            <instance>em1</instance>
-            <instance>em2</instance>
-            <instance>imsAospSlot1</instance>
-            <instance>imsAospSlot2</instance>
-            <instance>se1</instance>
-            <instance>se2</instance>
-            <instance>slot1</instance>
-            <instance>slot2</instance>
-        </interface>
-        <interface>
-            <name>ISap</name>
-            <instance>slot1</instance>
-            <instance>slot2</instance>
-        </interface>
-        <fqname>@1.2::IRadio/em1</fqname>
-        <fqname>@1.2::IRadio/em2</fqname>
-        <fqname>@1.2::IRadio/imsAospSlot1</fqname>
-        <fqname>@1.2::IRadio/imsAospSlot2</fqname>
-        <fqname>@1.2::IRadio/se1</fqname>
-        <fqname>@1.2::IRadio/se2</fqname>
-        <fqname>@1.2::IRadio/slot1</fqname>
-        <fqname>@1.2::IRadio/slot2</fqname>
-        <fqname>@1.2::ISap/slot1</fqname>
-        <fqname>@1.2::ISap/slot2</fqname>
-        <fqname>@1.5::IRadio/em1</fqname>
-        <fqname>@1.5::IRadio/em2</fqname>
-        <fqname>@1.5::IRadio/imsAospSlot1</fqname>
-        <fqname>@1.5::IRadio/imsAospSlot2</fqname>
-        <fqname>@1.5::IRadio/se1</fqname>
-        <fqname>@1.5::IRadio/se2</fqname>
-        <fqname>@1.5::IRadio/slot1</fqname>
-        <fqname>@1.5::IRadio/slot2</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.radio.config</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <interface>
-            <name>IRadioConfig</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.1::IRadioConfig/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.renderscript</name>
-        <transport arch="32+64">passthrough</transport>
-        <version>1.0</version>
-        <interface>
-            <name>IDevice</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.0::IDevice/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.secure_element</name>
-        <transport>hwbinder</transport>
-        <version>1.2</version>
-        <interface>
-            <name>ISecureElement</name>
-            <instance>SIM1</instance>
-            <instance>SIM2</instance>
-        </interface>
-        <fqname>@1.2::ISecureElement/SIM1</fqname>
-        <fqname>@1.2::ISecureElement/SIM2</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.sensors</name>
-        <transport>hwbinder</transport>
-        <version>2.0</version>
-        <interface>
-            <name>ISensors</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@2.0::ISensors/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.thermal</name>
-        <transport>hwbinder</transport>
-        <version>1.0</version>
-        <version>2.0</version>
-        <interface>
-            <name>IThermal</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.0::IThermal/default</fqname>
-        <fqname>@2.0::IThermal/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.usb</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <interface>
-            <name>IUsb</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.1::IUsb/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>android.hardware.usb.gadget</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <interface>
-            <name>IUsbGadget</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.1::IUsbGadget/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.atci</name>
-        <transport>hwbinder</transport>
-        <version>1.0</version>
-        <interface>
-            <name>IAtcid</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.0::IAtcid/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.bluetooth.audio</name>
-        <transport>hwbinder</transport>
-        <version>2.1</version>
-        <interface>
-            <name>IBluetoothAudioProvidersFactory</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@2.1::IBluetoothAudioProvidersFactory/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.camera.bgservice</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <interface>
-            <name>IBGService</name>
-            <instance>internal/0</instance>
-        </interface>
-        <fqname>@1.1::IBGService/internal/0</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.keymaster_attestation</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <interface>
-            <name>IKeymasterDevice</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.1::IKeymasterDevice/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.lbs</name>
-        <transport>hwbinder</transport>
-        <version>1.0</version>
-        <interface>
-            <name>ILbs</name>
-            <instance>AgpsDebugInterface</instance>
-            <instance>AgpsInterface</instance>
-            <instance>mtk_agps2framework</instance>
-            <instance>mtk_debugService2mnld</instance>
-            <instance>mtk_framework2agps</instance>
-            <instance>mtk_lbs_log_v2s</instance>
-            <instance>mtk_lppe_socket_agps</instance>
-            <instance>mtk_lppe_socket_bt</instance>
-            <instance>mtk_lppe_socket_ipaddr</instance>
-            <instance>mtk_lppe_socket_lbs</instance>
-            <instance>mtk_lppe_socket_network</instance>
-            <instance>mtk_lppe_socket_sensor</instance>
-            <instance>mtk_lppe_socket_wlan</instance>
-            <instance>mtk_meta2mnld</instance>
-            <instance>mtk_mnld2debugService</instance>
-            <instance>mtk_mnld2mtklogger</instance>
-            <instance>mtk_mnld2nlputils</instance>
-            <instance>mtk_mtklogger2mnld</instance>
-        </interface>
-        <fqname>@1.0::ILbs/AgpsDebugInterface</fqname>
-        <fqname>@1.0::ILbs/AgpsInterface</fqname>
-        <fqname>@1.0::ILbs/mtk_agps2framework</fqname>
-        <fqname>@1.0::ILbs/mtk_debugService2mnld</fqname>
-        <fqname>@1.0::ILbs/mtk_framework2agps</fqname>
-        <fqname>@1.0::ILbs/mtk_lbs_log_v2s</fqname>
-        <fqname>@1.0::ILbs/mtk_lppe_socket_agps</fqname>
-        <fqname>@1.0::ILbs/mtk_lppe_socket_bt</fqname>
-        <fqname>@1.0::ILbs/mtk_lppe_socket_ipaddr</fqname>
-        <fqname>@1.0::ILbs/mtk_lppe_socket_lbs</fqname>
-        <fqname>@1.0::ILbs/mtk_lppe_socket_network</fqname>
-        <fqname>@1.0::ILbs/mtk_lppe_socket_sensor</fqname>
-        <fqname>@1.0::ILbs/mtk_lppe_socket_wlan</fqname>
-        <fqname>@1.0::ILbs/mtk_meta2mnld</fqname>
-        <fqname>@1.0::ILbs/mtk_mnld2debugService</fqname>
-        <fqname>@1.0::ILbs/mtk_mnld2mtklogger</fqname>
-        <fqname>@1.0::ILbs/mtk_mnld2nlputils</fqname>
-        <fqname>@1.0::ILbs/mtk_mtklogger2mnld</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.mms</name>
-        <transport>hwbinder</transport>
-        <version>1.5</version>
-        <interface>
-            <name>IMms</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.5::IMms/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.mtkcodecservice</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <interface>
-            <name>IMtkCodecService</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.1::IMtkCodecService/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.mtkpower</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <interface>
-            <name>IMtkPerf</name>
-            <instance>default</instance>
-        </interface>
-        <interface>
-            <name>IMtkPower</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.1::IMtkPerf/default</fqname>
-        <fqname>@1.1::IMtkPower/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.mtkradioex</name>
-        <transport>hwbinder</transport>
-        <version>2.0</version>
-        <interface>
-            <name>IMtkRadioEx</name>
-            <instance>imsSlot1</instance>
-            <instance>imsSlot2</instance>
-            <instance>mtkAssist1</instance>
-            <instance>mtkAssist2</instance>
-            <instance>mtkCap1</instance>
-            <instance>mtkCap2</instance>
-            <instance>mtkEm1</instance>
-            <instance>mtkEm2</instance>
-            <instance>mtkRcs1</instance>
-            <instance>mtkRcs2</instance>
-            <instance>mtkSe1</instance>
-            <instance>mtkSe2</instance>
-            <instance>mtkSlot1</instance>
-            <instance>mtkSlot2</instance>
-            <instance>mtkSmartRatSwitch1</instance>
-            <instance>mtkSmartRatSwitch2</instance>
-        </interface>
-        <fqname>@2.0::IMtkRadioEx/imsSlot1</fqname>
-        <fqname>@2.0::IMtkRadioEx/imsSlot2</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkAssist1</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkAssist2</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkCap1</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkCap2</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkEm1</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkEm2</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkRcs1</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkRcs2</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkSe1</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkSe2</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkSlot1</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkSlot2</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkSmartRatSwitch1</fqname>
-        <fqname>@2.0::IMtkRadioEx/mtkSmartRatSwitch2</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.netdagent</name>
-        <transport>hwbinder</transport>
-        <version>1.0</version>
-        <interface>
-            <name>INetdagent</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.0::INetdagent/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.nvram</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <interface>
-            <name>INvram</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.1::INvram/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.pq</name>
-        <transport>hwbinder</transport>
-        <version>2.6</version>
-        <interface>
-            <name>IPictureQuality</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@2.6::IPictureQuality/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.videotelephony</name>
-        <transport>hwbinder</transport>
-        <version>1.0</version>
-        <interface>
-            <name>IVideoTelephony</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.0::IVideoTelephony/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.mediatek.hardware.wifi.supplicant</name>
-        <transport>hwbinder</transport>
-        <version>2.0</version>
-        <interface>
-            <name>ISupplicant</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@2.0::ISupplicant/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.trustonic.tee</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <interface>
-            <name>ITee</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.1::ITee/default</fqname>
-    </hal>
-    <hal format="hidl">
-        <name>vendor.trustonic.tee.tui</name>
-        <transport>hwbinder</transport>
-        <version>1.0</version>
-        <interface>
-            <name>ITui</name>
-            <instance>default</instance>
-        </interface>
-        <fqname>@1.0::ITui/default</fqname>
-    </hal>
-    <sepolicy>
-        <version>30.0</version>
-    </sepolicy>
-</manifest>

TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/020f0000000000000000000000000000.drbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/020f0000000000000000000000000000.tlbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/030b0000000000000000000000000000.drbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/030b0000000000000000000000000000.tlbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/05120000000000000000000000000000.drbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/05120000000000000000000000000000.tlbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/06090000000000000000000000000000.drbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/06090000000000000000000000000000.tlbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/07010000000000000000000000000000.tlbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/0706000000000000000000000000004d.tlbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/07061000000000000000000000000000.tlbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/07150000000000000000000000000000.drbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/07150000000000000000000000000000.tlbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/07170000000000000000000000000000.drbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/07170000000000000000000000000000.tlbin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/08030000000000000000000000000000.tabin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/08110000000000000000000000000000.tabin


TEMPAT SAMPAH
recovery/root/vendor/app/mcRegistry/09190000000000000000000000000000.tlbin


TEMPAT SAMPAH
recovery/root/vendor/bin/hw/android.hardware.boot@1.1-service


+ 14 - 1
twrp.dependencies

@@ -1 +1,14 @@
-[]
+[
+  {
+    "remote": "github",
+    "repository": "SmallPP420/device_mediatek_common",
+    "target_path": "device/mediatek/common",
+    "branch": "R"
+  }
+  {
+    "remote": "github",
+    "repository": "SmallPP420/avb_sys_fst",
+    "target_path": "system/core/fs_mgr/include_fstab/fstab",
+    "branch": "main"
+  }
+]