appsboot/msm_shared: Supporting SSD Fastboot

When SSD is enabled, fastboot will decrypt enc files,
which are encrypted. Using TZ, appsbl will decrypt
image and flash normally if encrypted image detected,
or flash images normally enabled for 8660 emmc.

Change-Id: I85f54a9ad9a17edc42449b225f1f16975fce0a2f
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index 392bdae..2f526f4 100644
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -49,6 +49,8 @@
 #include "fastboot.h"
 #include "sparse_format.h"
 
+#include "scm_decrypt.h"
+
 #define EXPAND(NAME) #NAME
 #define TARGET(NAME) EXPAND(NAME)
 #define DEFAULT_CMDLINE "mem=100M console=null";
@@ -118,7 +120,7 @@
 	*ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
 }
 
-void boot_linux(void *kernel, unsigned *tags, 
+void boot_linux(void *kernel, unsigned *tags,
 		const char *cmdline, unsigned machtype,
 		void *ramdisk, unsigned ramdisk_size)
 {
@@ -695,13 +697,27 @@
 void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
 {
 	sparse_header_t *sparse_header;
-	sparse_header = (sparse_header_t *) data;
+	/* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
+	unsigned int *magic_number = (unsigned int *) data;
+	int ret=0;
 
+	if (magic_number[0] == SSD_HEADER_MAGIC_0 &&
+		magic_number[1] == SSD_HEADER_MAGIC_1)
+	{
+#ifdef SSD_ENABLE
+		ret = decrypt_img_scm(&data, &sz);
+#endif
+		if(ret != 0)
+		{
+			dprintf(CRITICAL, "ERROR: Invalid secure image\n");
+			return;
+		}
+	}
+	sparse_header = (sparse_header_t *) data;
 	if (sparse_header->magic != SPARSE_HEADER_MAGIC)
 		cmd_flash_mmc_img(arg, data, sz);
 	else
 		cmd_flash_mmc_sparse_img(arg, data, sz);
-
 	return;
 }
 
diff --git a/platform/msm8x60/rules.mk b/platform/msm8x60/rules.mk
index e4adc5b..f258110 100644
--- a/platform/msm8x60/rules.mk
+++ b/platform/msm8x60/rules.mk
@@ -8,7 +8,9 @@
 MMC_SLOT         := 1
 
 DEFINES += WITH_CPU_EARLY_INIT=0 WITH_CPU_WARM_BOOT=0 \
-	   MMC_SLOT=$(MMC_SLOT) MDP4=1
+	   MMC_SLOT=$(MMC_SLOT) MDP4=1 \
+	   SSD_ENABLE
+
 
 DEFINES += QT_8660_KEYPAD_HW_BUG=1
 
diff --git a/platform/msm_shared/include/scm_decrypt.h b/platform/msm_shared/include/scm_decrypt.h
new file mode 100644
index 0000000..4740759
--- /dev/null
+++ b/platform/msm_shared/include/scm_decrypt.h
@@ -0,0 +1,71 @@
+/* Copyright (c) 2011, Code Aurora Forum. 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 Code Aurora Forum, Inc. 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>
+
+/* 8 Byte SSD magic number (LE) */
+#define SSD_HEADER_MAGIC_0     0x73737A74
+#define SSD_HEADER_MAGIC_1     0x676D6964
+#define SSD_HEADER_MAGIC_SIZE  8
+#define SSD_HEADER_XML_SIZE    2048
+
+typedef unsigned int uint32;
+
+typedef struct
+{
+    uint32  len;
+    uint32  buf_offset;
+    uint32  resp_hdr_offset;
+    uint32  id;
+} scm_command;
+
+typedef struct
+{
+    uint32  len;
+    uint32  buf_offset;
+    uint32  is_complete;
+} scm_response;
+
+typedef struct
+{
+    scm_command     common_req;
+    uint32*         img_ptr;
+    uint32*         img_len_ptr;
+} decrypt_img_req;
+
+#define SYSCALL_CREATE_CMD_ID(s, f) \
+  ((uint32)(((s & 0x3ff) << 10) | (f & 0x3ff)))
+
+#define SCM_SVC_SSD                7
+#define SSD_DECRYPT_IMG_ID SYSCALL_CREATE_CMD_ID(SCM_SVC_SSD, 0x01)
+
+void setup_decrypt_cmd ( decrypt_img_req* dec_cmd,
+                         uint32** img_ptr,
+                         uint32* img_len_ptr);
+static uint32 smc(uint32 cmd_addr);
+int decrypt_img_scm(uint32** img_ptr, uint32* img_len_ptr);
diff --git a/platform/msm_shared/rules.mk b/platform/msm_shared/rules.mk
index d01ca40..25cba45 100644
--- a/platform/msm_shared/rules.mk
+++ b/platform/msm_shared/rules.mk
@@ -45,5 +45,6 @@
 
 ifeq ($(PLATFORM),msm8x60)
 	OBJS += $(LOCAL_DIR)/crypto_eng.o \
-	        $(LOCAL_DIR)/crypto_hash.o
+	        $(LOCAL_DIR)/crypto_hash.o \
+		$(LOCAL_DIR)/scm_decrypt.o
 endif
diff --git a/platform/msm_shared/scm_decrypt.c b/platform/msm_shared/scm_decrypt.c
new file mode 100644
index 0000000..df1b399
--- /dev/null
+++ b/platform/msm_shared/scm_decrypt.c
@@ -0,0 +1,82 @@
+/* Copyright (c) 2011, Code Aurora Forum. 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 Code Aurora Forum, Inc. 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 "scm_decrypt.h"
+
+#pragma GCC optimize ("O0")
+
+/* From Linux Kernel asm/system.h */
+#define __asmeq(x, y)  ".ifnc " x "," y " ; .err ; .endif\n\t"
+
+void setup_decrypt_cmd ( decrypt_img_req* dec_cmd,
+                         uint32_t** img_ptr,
+                         uint32_t* img_len_ptr)
+{
+    dec_cmd->common_req.len = sizeof(decrypt_img_req);
+    dec_cmd->common_req.buf_offset = sizeof(scm_command);
+    dec_cmd->common_req.resp_hdr_offset = 0;
+    dec_cmd->common_req.id = SSD_DECRYPT_IMG_ID;
+
+    dec_cmd->img_ptr = img_ptr;
+    dec_cmd->img_len_ptr = img_len_ptr;
+}
+
+static uint32_t smc(uint32_t cmd_addr)
+{
+	uint32_t context_id;
+	register uint32_t r0 __asm__("r0") = 1;
+	register uint32_t r1 __asm__("r1") = (uint32_t)&context_id;
+	register uint32_t r2 __asm__("r2") = cmd_addr;
+	__asm__(
+		"1:smc	#0	@ switch to secure world\n"
+		"cmp	r0, #1				\n"
+		"beq	1b				\n"
+		: "=r" (r0)
+		: "r" (r0), "r" (r1), "r" (r2)
+		: "r3", "cc");
+	return r0;
+}
+
+
+int decrypt_img_scm(uint32_t** img_ptr, uint32_t* img_len_ptr)
+{
+   int ret = 0;
+   decrypt_img_req *decrypt_cmd;
+
+   /* allocate memory for the command structure */
+   /* NEEDS TO BE CONTIGUOUS MEMORY */
+   decrypt_cmd = malloc(sizeof(decrypt_img_req));
+
+   /* setup the command for decryption */
+   setup_decrypt_cmd(decrypt_cmd, img_ptr, img_len_ptr);
+
+   ret = smc(decrypt_cmd);
+   free(decrypt_cmd);
+   return ret;
+}
+