Merge remote-tracking branch master into MHB

* master:
  app: aboot: move display shutdown before uninit
  platform: msm8996: Add QUSB ref clock
  platform: msm_shared: Update SIGDET_LVL for qmp phy
  platform: msm_shared: Update phy sequence for qusb
  platform: msm_shared: Add support for pmic type API
  dev: pmi8994: Add support to read battery voltage from sram
  app: aboot: Add support for tests framework

Change-Id: I78dd0fb4e2006328dbf1449f102dca779d60f164
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index 4911344..5067181 100644
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -92,6 +92,7 @@
 #include "secapp_loader.h"
 #include <menu_keys_detect.h>
 #include <display_menu.h>
+#include "fastboot_test.h"
 
 extern  bool target_use_signed_kernel(void);
 extern void platform_uninit(void);
@@ -192,6 +193,7 @@
 static char *target_boot_params = NULL;
 static bool boot_reason_alarm;
 static bool devinfo_present = true;
+bool boot_into_fastboot = false;
 
 /* Assuming unauthorized kernel image by default */
 static int auth_kernel_img = 0;
@@ -1108,7 +1110,11 @@
 		device.is_unlocked,
 		device.is_tampered);
 
-	if(target_use_signed_kernel() && (!device.is_unlocked))
+	/* Change the condition a little bit to include the test framework support.
+	 * We would never reach this point if device is in fastboot mode, even if we did
+	 * that means we are in test mode, so execute kernel authentication part for the
+	 * tests */
+	if((target_use_signed_kernel() && (!device.is_unlocked)) || boot_into_fastboot)
 	{
 		offset = imagesize_actual;
 		if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
@@ -1125,6 +1131,9 @@
 		}
 
 		verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
+		/* The purpose of our test is done here */
+		if (boot_into_fastboot && auth_kernel_img)
+			return 0;
 	} else {
 		second_actual  = ROUND_TO_PAGE(hdr->second_size,  page_mask);
 		#ifdef TZ_SAVE_KERNEL_HASH
@@ -3439,6 +3448,9 @@
 						{"oem disable-charger-screen", cmd_oem_disable_charger_screen},
 						{"oem off-mode-charge", cmd_oem_off_mode_charger},
 						{"oem select-display-panel", cmd_oem_select_display_panel},
+#if UNITTEST_FW_SUPPORT
+						{"oem run-tests", cmd_oem_runtests},
+#endif
 #endif
 						};
 
@@ -3492,7 +3504,6 @@
 void aboot_init(const struct app_descriptor *app)
 {
 	unsigned reboot_mode = 0;
-	bool boot_into_fastboot = false;
 
 	/* Setup page size information for nv storage */
 	if (target_is_emmc_boot())
diff --git a/app/aboot/fastboot_test.c b/app/aboot/fastboot_test.c
new file mode 100644
index 0000000..f32435c
--- /dev/null
+++ b/app/aboot/fastboot_test.c
@@ -0,0 +1,92 @@
+/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+    * Neither the name of The Linux Foundation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#include <stdlib.h>
+#include <debug.h>
+#include "devinfo.h"
+#include "fastboot.h"
+#include "fastboot_test.h"
+#include <app/tests.h>
+#if USE_RPMB_FOR_DEVINFO
+#include <rpmb.h>
+#endif
+/* main function that calls into the tests */
+
+extern void ramdump_table_map();
+extern void kauth_test();
+
+void cmd_oem_runtests()
+{
+	dprintf(INFO, "Running LK tests ... \n");
+#if LPAE
+	ramdump_table_map();
+#endif
+
+#if USE_RPMB_FOR_DEVINFO
+	dprintf(INFO, "Running RPMB test case, please make sure RPMB key is provisioned ...\n");
+	struct device_info *write_info = memalign(PAGE_SIZE, 4096);
+	struct device_info *read_info = memalign(PAGE_SIZE, 4096);
+	if((read_device_info_rpmb((void*) read_info, PAGE_SIZE)) < 0)
+		dprintf(INFO, "RPMB READ TEST : [ FAIL ]\n");
+
+	write_info->is_unlocked = !read_info->is_unlocked;
+
+	if((write_device_info_rpmb((void*) write_info, PAGE_SIZE)) < 0)
+		dprintf(INFO, "RPMB WRITE TEST : [ FAIL ]\n");
+
+	if((read_device_info_rpmb((void*) read_info, PAGE_SIZE)) < 0)
+		dprintf(INFO, "RPMB READ TEST : [ FAIL ]\n");
+
+	if (read_info->is_unlocked == write_info->is_unlocked)
+		dprintf(INFO, "RPMB READ/WRITE TEST: [ PASS ]\n");
+	else
+		dprintf(INFO, "RPMB READ/WRITE TEST: [ FAIL ]\n");
+
+	free(read_info);
+	free(write_info);
+#endif
+
+#if VERIFIED_BOOT
+	if (!boot_linux_from_mmc())
+		dprintf(INFO, "Verifid Boot authentication test: [ PASS ]\n");
+	else
+		dprintf(INFO, "Verifid Boot authentication test: [ FAIL ]\n");
+
+	kauth_test();
+#endif
+
+	if (!thread_tests())
+		dprintf(INFO, "Thread Test: [ PASS ]\n");
+	else
+		dprintf(INFO, "Thread Test: [ FAIL ]\n");
+
+	printf_tests();
+
+	fastboot_okay("");
+}
diff --git a/app/aboot/fastboot_test.h b/app/aboot/fastboot_test.h
new file mode 100644
index 0000000..005bfa7
--- /dev/null
+++ b/app/aboot/fastboot_test.h
@@ -0,0 +1,36 @@
+/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+    * Neither the name of The Linux Foundation nor the names of its
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#ifndef __FASTBOOT_TEST_H__
+#define __FASTBOOT_TEST_H__
+#include <sys/types.h>
+extern void ramdump_table_map();
+void cmd_oem_runtests();
+extern int boot_linux_from_mmc();
+#endif
diff --git a/app/aboot/rules.mk b/app/aboot/rules.mk
index 24bca43..09a5152 100644
--- a/app/aboot/rules.mk
+++ b/app/aboot/rules.mk
@@ -11,6 +11,11 @@
 	$(LOCAL_DIR)/fastboot.o \
 	$(LOCAL_DIR)/recovery.o
 
+ifeq ($(ENABLE_UNITTEST_FW), 1)
+OBJS += \
+	$(LOCAL_DIR)/fastboot_test.o
+endif
+
 ifeq ($(ENABLE_MDTP_SUPPORT),1)
 OBJS += \
 	$(LOCAL_DIR)/mdtp.o \