[Feature]add MT2731_MP2_MR2_SVN388 baseline version

Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/meta/meta-mediatek/recipes-support/update-verifier/files/Makefile.am b/meta/meta-mediatek/recipes-support/update-verifier/files/Makefile.am
new file mode 100644
index 0000000..b26b317
--- /dev/null
+++ b/meta/meta-mediatek/recipes-support/update-verifier/files/Makefile.am
@@ -0,0 +1,31 @@
+# update_verifier.bin
+bin_PROGRAMS = update_verifier
+
+
+update_verifier_SOURCES = \
+	otautil/rangeset.cpp \
+	update_verifier/update_verifier_main.cpp \
+	update_verifier/update_verifier.cpp
+
+update_verifier_CFLAGS = \
+   -Wall \
+   -Werror \
+   -std=c++11
+
+INCLUDES = \
+	-I$(top_srcdir)/otautil/include \
+	-I$(top_srcdir)/update_verifier/include
+
+AM_CPPFLAGS = \
+	-I$(top_srcdir)/otautil \
+	-I$(top_srcdir)/update_verifier
+
+update_verifier_CPPFLAGS = \
+	-Ibootctrl \
+	-I$(top_srcdir)
+
+if NAND
+update_verifier_LDADD = -llog -lpthread -lbase -lbootctrl -lnandapi
+else
+update_verifier_LDADD = -llog -lpthread -lbase -lbootctrl
+endif
diff --git a/meta/meta-mediatek/recipes-support/update-verifier/files/configure.ac b/meta/meta-mediatek/recipes-support/update-verifier/files/configure.ac
new file mode 100644
index 0000000..ca5e7e1
--- /dev/null
+++ b/meta/meta-mediatek/recipes-support/update-verifier/files/configure.ac
@@ -0,0 +1,14 @@
+AC_INIT(update-verifier, 1.0)
+AM_INIT_AUTOMAKE(foreign)
+
+# check boot type
+AC_ARG_ENABLE(nand, AC_HELP_STRING([--enable-nand],
+                                [enable nand support]),
+                                        [enable_nand=${enableval}])
+
+AM_CONDITIONAL(NAND, test "$enable_nand" = "yes")
+
+AM_MAINTAINER_MODE
+AC_PROG_CXX
+LT_INIT
+AC_OUTPUT([Makefile])
diff --git a/meta/meta-mediatek/recipes-support/update-verifier/files/update-verifier.patch b/meta/meta-mediatek/recipes-support/update-verifier/files/update-verifier.patch
new file mode 100644
index 0000000..3150c93
--- /dev/null
+++ b/meta/meta-mediatek/recipes-support/update-verifier/files/update-verifier.patch
@@ -0,0 +1,317 @@
+diff --git a/update_verifier/include/update_verifier/update_verifier.h b/update_verifier/include/update_verifier/update_verifier.h
+index 16b394e98..c700a170b 100644
+--- a/update_verifier/include/update_verifier/update_verifier.h
++++ b/update_verifier/include/update_verifier/update_verifier.h
+@@ -22,3 +22,4 @@ int update_verifier(int argc, char** argv);
+ 
+ // Exposed for testing purpose.
+ bool verify_image(const std::string& care_map_name);
++std::string GetProperty(const char* strProperity);
+diff --git a/update_verifier/update_verifier.cpp b/update_verifier/update_verifier.cpp
+index 92d931371..45b12cb9f 100644
+--- a/update_verifier/update_verifier.cpp
++++ b/update_verifier/update_verifier.cpp
+@@ -34,9 +34,6 @@
+  * The current slot will be marked as having booted successfully if the
+  * verifier reaches the end after the verification.
+  */
+-
+-#include "update_verifier/update_verifier.h"
+-
+ #include <dirent.h>
+ #include <errno.h>
+ #include <fcntl.h>
+@@ -52,20 +49,22 @@
+ #include <android-base/file.h>
+ #include <android-base/logging.h>
+ #include <android-base/parseint.h>
+-#include <android-base/properties.h>
+ #include <android-base/strings.h>
+ #include <android-base/unique_fd.h>
+-#include <android/hardware/boot/1.0/IBootControl.h>
+-#include <cutils/android_reboot.h>
++#include <hardware/boot_control.h>
++#include <hardware/hardware.h>
++#include <sys/reboot.h>
+ 
+ #include "otautil/rangeset.h"
++#include "update_verifier/update_verifier.h"
+ 
+-using android::sp;
+-using android::hardware::boot::V1_0::IBootControl;
+-using android::hardware::boot::V1_0::BoolResult;
+-using android::hardware::boot::V1_0::CommandResult;
++using std::string;
+ 
+ // Find directories in format of "/sys/block/dm-X".
++
++extern const hw_module_t HAL_MODULE_INFO_SYM;
++boot_control_module_t* module;
++
+ static int dm_name_filter(const dirent* de) {
+   if (android::base::StartsWith(de->d_name, "dm-")) {
+     return 1;
+@@ -73,6 +72,72 @@ static int dm_name_filter(const dirent* de) {
+   return 0;
+ }
+ 
++#define COMMAND_LINE_PATH "/proc/cmdline"
++#define COMMAND_LINE_SIZE 2048
++
++std::string GetProperty(const char* strProperity)
++{
++    int fd, err, slot;
++    ssize_t size = COMMAND_LINE_SIZE, sz;
++    char *buf, *ptr;
++    char *str;
++    std::string res;
++
++    fd = open(COMMAND_LINE_PATH, O_RDONLY);
++    if (fd < 0) {
++        err = -errno;
++        LOG(ERROR) << "error reading commandline\n";
++        return NULL;
++    }
++    ptr = buf = (char *)malloc(size);
++    if (!buf) {
++        err = -errno;
++        LOG(ERROR) << "Error allocating memory";
++        close(fd);
++        return NULL;
++    }
++    do {
++        sz = read(fd, buf, size);
++        if (sz == 0) {
++            break;
++        } else if (sz < 0) {
++            if (errno == EINTR) {
++                continue;
++            }
++            err = -errno;
++			      LOG(ERROR) << "Error reading file";
++            free(ptr);
++            close(fd);
++            return NULL;
++        }
++        size -= sz;
++        buf += sz;
++    } while(size > 0);
++    
++    str = strstr((char *)ptr, strProperity);
++    if (!str) {
++        err = -EIO;
++        LOG(ERROR) << "Cannot find %s in kernel commandline" <<  strProperity;
++        free(ptr);
++        close(fd);
++        return NULL;
++    }
++    str += strlen(strProperity);
++    while(1){
++        str++;
++        if(((*str == ' ') || (*str == '\0' ) || (*str == '\n'))){
++            break;
++		    }else{
++			      res.push_back(*str);
++		    }
++    }
++    LOG(INFO) << "Find kernel commandline" << res;
++    
++    free(ptr);
++    close(fd);
++    return res;
++}
++
+ static bool read_blocks(const std::string& partition, const std::string& range_str) {
+   if (partition != "system" && partition != "vendor" && partition != "product") {
+     LOG(ERROR) << "Invalid partition name \"" << partition << "\"";
+@@ -103,6 +168,7 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
+       PLOG(WARNING) << "Failed to read " << path;
+     } else {
+       std::string dm_block_name = android::base::Trim(content);
++      	LOG(ERROR) << "dm_block_name namelist[n]->d_name " << dm_block_name << namelist[n]->d_name;
+ #ifdef BOARD_AVB_ENABLE
+       // AVB is using 'vroot' for the root block device but we're expecting 'system'.
+       if (dm_block_name == "vroot") {
+@@ -135,7 +201,7 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
+     LOG(ERROR) << "Error parsing RangeSet string " << range_str;
+     return false;
+   }
+-
++  
+   // RangeSet::Split() splits the ranges into multiple groups with same number of blocks (except for
+   // the last group).
+   size_t thread_num = std::thread::hardware_concurrency() ?: 4;
+@@ -147,13 +213,27 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
+       android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dm_block_device.c_str(), O_RDONLY)));
+       if (fd.get() == -1) {
+         PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition;
+-        return false;
++        //return false;
++        std::string path = "/sys/class/block/dm-0/dev";
++        std::string content;
++        if (!android::base::ReadFileToString(path, &content)) {
++             PLOG(WARNING) << "Failed to read " << path;
++        } else {
++            std::string dev_name = android::base::Trim(content);
++            	dm_block_device = DEV_PATH + dev_name;
++        }
+       }
+-
++      fd.reset(open(dm_block_device.c_str(), O_RDONLY));
++      if (fd.get() == -1) {
++      	  PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition;
++          return false;
++      }
++      
+       static constexpr size_t kBlockSize = 4096;
+       std::vector<uint8_t> buf(1024 * kBlockSize);
+ 
+       size_t block_count = 0;
++
+       for (const auto& range : group) {
+         size_t range_start = range.first;
+         size_t range_end = range.second;
+@@ -161,7 +241,7 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
+           PLOG(ERROR) << "lseek to " << range_start << " failed";
+           return false;
+         }
+-
++        PLOG(ERROR) << "range start " << range_start << " range_end " << range_end;
+         size_t remain = (range_end - range_start) * kBlockSize;
+         while (remain > 0) {
+           size_t to_read = std::min(remain, 1024 * kBlockSize);
+@@ -173,7 +253,7 @@ static bool read_blocks(const std::string& partition, const std::string& range_s
+         }
+         block_count += (range_end - range_start);
+       }
+-      LOG(INFO) << "Finished reading " << block_count << " blocks on " << dm_block_device;
++      LOG(ERROR) << "Finished reading " << block_count << " blocks on " << dm_block_device ;
+       return true;
+     };
+ 
+@@ -238,10 +318,13 @@ bool verify_image(const std::string& care_map_name) {
+ }
+ 
+ static int reboot_device() {
+-  if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) == -1) {
+-    LOG(ERROR) << "Failed to reboot.";
+-    return -1;
+-  }
++  //if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) == -1) {
++  //  LOG(ERROR) << "Failed to reboot.";
++  //  return -1;
++  //}
++  //system("reboot");
++  reboot(RB_AUTOBOOT);
++  
+   while (true) pause();
+ }
+ 
+@@ -250,23 +333,45 @@ int update_verifier(int argc, char** argv) {
+     LOG(INFO) << "Started with arg " << i << ": " << argv[i];
+   }
+ 
+-  sp<IBootControl> module = IBootControl::getService();
++  /*************    Bootctrl Init  Start  *************/
++  //sp<IBootControl> module = IBootControl::getService();
++  const hw_module_t* hw_module;
++  int ret;
++
++  // For update_engine_sideload, we simulate the hw_get_module() by accessing it
++  // from the current process directly.
++  hw_module = &HAL_MODULE_INFO_SYM;
++  ret = 0;
++  if (!hw_module ||
++      strcmp(BOOT_CONTROL_HARDWARE_MODULE_ID, hw_module->id) != 0) {
++    ret = -EINVAL;
++  }
++  if (ret != 0) {
++    LOG(ERROR) << "Error loading boot_control HAL implementation.";
++    return false;
++  }
++
++  module = reinterpret_cast<boot_control_module_t*>(
++      const_cast<hw_module_t*>(hw_module));
++  module->init(module);
++
+   if (module == nullptr) {
+     LOG(ERROR) << "Error getting bootctrl module.";
+     return reboot_device();
+   }
++  /*************    Bootctrl Init  End  *************/
+ 
+-  uint32_t current_slot = module->getCurrentSlot();
+-  BoolResult is_successful = module->isSlotMarkedSuccessful(current_slot);
++  uint32_t current_slot = module->getCurrentSlot(module);
++  //bool is_successful = module->isSlotMarkedSuccessful(current_slot);
++  bool is_successful = module->isSlotMarkedSuccessful(module, current_slot);
+   LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful="
+-            << static_cast<int32_t>(is_successful);
++            << is_successful;
+ 
+-  if (is_successful == BoolResult::FALSE) {
++  if (is_successful == false) {
+     // The current slot has not booted successfully.
+-
+ #if defined(PRODUCT_SUPPORTS_VERITY) || defined(BOARD_AVB_ENABLE)
+     bool skip_verification = false;
+-    std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
++    std::string verity_mode = GetProperty("androidboot.veritymode");
+     if (verity_mode.empty()) {
+       // With AVB it's possible to disable verification entirely and
+       // in this case ro.boot.veritymode is empty.
+@@ -290,7 +395,7 @@ int update_verifier(int argc, char** argv) {
+     }
+ 
+     if (!skip_verification) {
+-      static constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt";
++	    static constexpr auto CARE_MAP_FILE = "/var/lib/update_engine/care_map.txt";
+       if (!verify_image(CARE_MAP_FILE)) {
+         LOG(ERROR) << "Failed to verify all blocks in care map file.";
+         return reboot_device();
+@@ -300,10 +405,10 @@ int update_verifier(int argc, char** argv) {
+     LOG(WARNING) << "dm-verity not enabled; marking without verification.";
+ #endif
+ 
+-    CommandResult cr;
+-    module->markBootSuccessful([&cr](CommandResult result) { cr = result; });
+-    if (!cr.success) {
+-      LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg;
++    //bool cr = module->markBootSuccessful([&cr](CommandResult result) { cr = result; });
++    int cr = module->markBootSuccessful(module);
++    if (cr < 0) {
++      LOG(ERROR) << "Error marking booted successfully: " << cr;
+       return reboot_device();
+     }
+     LOG(INFO) << "Marked slot " << current_slot << " as booted successfully.";
+diff --git a/update_verifier/update_verifier_main.cpp b/update_verifier/update_verifier_main.cpp
+index a86203bfb..c22582341 100644
+--- a/update_verifier/update_verifier_main.cpp
++++ b/update_verifier/update_verifier_main.cpp
+@@ -19,12 +19,15 @@
+ #include <string>
+ 
+ #include <android-base/logging.h>
+-#include <android-base/properties.h>
++//#include <android-base/properties.h>
+ 
+ #include "update_verifier/update_verifier.h"
+ 
+ int main(int argc, char** argv) {
+-  std::string s = android::base::GetProperty("ro.boot.slot_suffix", "");
++  //std::string s = android::base::GetProperty("ro.boot.slot_suffix", "");
++  
++  
++  std::string s = GetProperty("androidboot.slot");
+ 
+   if (s.empty()) {
+     return 0;  // non-A/B update device, so we quit
+@@ -32,7 +35,7 @@ int main(int argc, char** argv) {
+ 
+   // Set up update_verifier logging to be written to kmsg; because we may not have Logd during
+   // boot time.
+-  android::base::InitLogging(argv, &android::base::KernelLogger);
++  android::base::InitLogging(argv, android::base::StderrLogger);
+ 
+   return update_verifier(argc, argv);
+ }
diff --git a/meta/meta-mediatek/recipes-support/update-verifier/files/update_verifier.service b/meta/meta-mediatek/recipes-support/update-verifier/files/update_verifier.service
new file mode 100644
index 0000000..732bc33
--- /dev/null
+++ b/meta/meta-mediatek/recipes-support/update-verifier/files/update_verifier.service
@@ -0,0 +1,10 @@
+[Unit]

+Description=update_verifier

+

+[Service]

+ExecStart=/usr/bin/update_verifier

+Type=simple

+

+[Install]

+Alias=upgrade

+WantedBy=multi-user.target
\ No newline at end of file
diff --git a/meta/meta-mediatek/recipes-support/update-verifier/update-verifier.bb b/meta/meta-mediatek/recipes-support/update-verifier/update-verifier.bb
new file mode 100644
index 0000000..7f6751e
--- /dev/null
+++ b/meta/meta-mediatek/recipes-support/update-verifier/update-verifier.bb
@@ -0,0 +1,42 @@
+inherit pkgconfig autotools
+DESCRIPTION = "ab update verifier"
+
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://NOTICE;md5=9645f39e9db895a4aa6e02cb57294595"
+
+DEPENDS += "bootctrl platform-libs-common ${@bb.utils.contains("BOOTDEV_TYPE", "nand", "nandapi", "", d)}"
+
+INHIBIT_PACKAGE_STRIP = "1"
+inherit systemd
+
+FILESEXTRAPATHS_prepend := "${THISDIR}:"
+SRC_URI = " \
+	git://android.googlesource.com/platform/bootable/recovery;name=platform/bootable/recovery;destsuffix=platform/bootable/recovery \
+	file://update-verifier.patch \
+	file://Makefile.am \
+	file://configure.ac \
+	file://update_verifier.service \
+"
+
+SRCREV_platform/bootable/recovery = "896de5423db1ec3824797e11199a08be20b02d1d"
+
+S = "${WORKDIR}/platform/bootable/recovery"
+
+CPPFLAGS += "${@bb.utils.contains("BOARD_AVB_ENABLE","true", "-DBOARD_AVB_ENABLE","",d)}"
+
+SYSTEMD_PACKAGES = "${PN}"
+SYSTEMD_SERVICE_${PN} = "update_verifier.service"
+FILES_${PN} += "${systemd_unitdir}/system/update_verifier.service"
+
+EXTRA_OECONF = "${@bb.utils.contains("BOOTDEV_TYPE", "nand", "--enable-nand=yes", "", d)}"
+
+do_configure_prepend() {
+	install -m 0755 ${WORKDIR}/Makefile.am ${S}/Makefile.am
+	install -m 0755 ${WORKDIR}/configure.ac ${S}/configure.ac
+	install -m 0755 ${WORKDIR}/update_verifier.service ${S}/update_verifier.service
+}
+
+do_install_append() {
+	install -d ${D}${systemd_unitdir}/system
+	install -m 0755 ${S}/update_verifier.service ${D}${systemd_unitdir}/system
+}