Merge "platform: msm_shared: Correct invalidate cache actions"
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index e4154ac..62cc67a 100644
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -205,6 +205,9 @@
 static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
 #else
 static const char *sys_path = " root=/dev/mmcblk0p";
+
+#define MAX_DTBO_IDX_STR 64
+static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
 #endif
 
 #if VERIFIED_BOOT
@@ -398,7 +401,9 @@
 				+ strlen(verity_system_part) + (sizeof(char) * 2) + 2
 				+ strlen(verity_params) + sizeof(int) + 2;
 #else
-        int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
+	int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
+	char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
+	int dtbo_idx = INVALID_PTN;
 #endif
 	char syspath_buf[syspath_buflen];
 #if HIBERNATION_SUPPORT
@@ -641,6 +646,15 @@
 	cmdline_len += target_cmd_line_len;
 #endif
 
+#if !VERITY_LE
+	dtbo_idx = get_dtbo_idx ();
+	if (dtbo_idx != INVALID_PTN) {
+		snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
+			android_boot_dtbo_idx, dtbo_idx);
+		cmdline_len += strlen (dtbo_idx_str);
+	}
+#endif
+
 	if (cmdline_len > 0) {
 		const char *src;
 		unsigned char *dst;
@@ -898,6 +912,14 @@
 			free(target_cmdline_buf);
 		}
 #endif
+
+#if !VERITY_LE
+		if (dtbo_idx != INVALID_PTN) {
+			src = dtbo_idx_str;
+			--dst;
+			while ((*dst++ = *src++));
+		}
+#endif
 	}
 
 
diff --git a/arch/arm/include/arch/arm/mmu.h b/arch/arm/include/arch/arm/mmu.h
index 43a474e..c870329 100644
--- a/arch/arm/include/arch/arm/mmu.h
+++ b/arch/arm/include/arch/arm/mmu.h
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2008 Travis Geiselbrecht
  *
- * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2015,2018 The Linux Foundation. All rights reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files
@@ -49,6 +49,7 @@
 #define MMU_MEMORY_AP_NO_ACCESS     (0x0 << 10)
 #define MMU_MEMORY_AP_READ_ONLY     (0x7 << 10)
 #define MMU_MEMORY_AP_READ_WRITE    (0x3 << 10)
+#define MMU_MEMORY_APX_READ_ONLY    (0x1 << 15)
 
 #define MMU_MEMORY_XN               (0x1 << 4)
 #else /* LPAE */
diff --git a/arch/arm/rules.mk b/arch/arm/rules.mk
index 7e3ace0..bfef682 100644
--- a/arch/arm/rules.mk
+++ b/arch/arm/rules.mk
@@ -150,7 +150,7 @@
 $(BUILDDIR)/system-onesegment.ld: $(LOCAL_DIR)/system-onesegment.ld $(LK_TOP_DIR)/target/$(TARGET)/rules.mk .FORCE
 	@echo generating $@
 	@$(MKDIR)
-	$(NOECHO)sed "s/%MEMBASE%/$(MEMBASE)/;s/%MEMSIZE%/$(MEMSIZE)/" < $< > $@
+	$(NOECHO)sed "s/%MEMBASE%/$(MEMBASE)/;s/%MEMSIZE%/$(MEMSIZE)/;s/%MEMRWOFF%/$(MEMRWOFF)/" < $< > $@
 
 $(BUILDDIR)/system-twosegment.ld: $(LOCAL_DIR)/system-twosegment.ld $(LK_TOP_DIR)/target/$(TARGET)/rules.mk .FORCE
 	@echo generating $@
diff --git a/arch/arm/system-onesegment.ld b/arch/arm/system-onesegment.ld
index f2c62ac..7768853 100644
--- a/arch/arm/system-onesegment.ld
+++ b/arch/arm/system-onesegment.ld
@@ -37,6 +37,9 @@
 	.init : { *(.init) } =0x9090
 	.plt : { *(.plt) }
 
+
+	/* Use read/write memory offset for everything except code section */
+	. = %MEMRWOFF% ;
 	.rodata : { 
 		*(.rodata .rodata.* .gnu.linkonce.r.*)
 		. = ALIGN(4);
diff --git a/makefile b/makefile
index 417b414..b2d40a6 100644
--- a/makefile
+++ b/makefile
@@ -211,6 +211,13 @@
 	ARCH_$(ARCH)=1 \
 	$(addsuffix =1,$(addprefix WITH_,$(ALLMODULES)))
 
+# Add MEMRWOFF as . for targets this is not declared.
+# . will be replaced as string in linker file.
+ifeq ($(MEMRWOFF),)
+MEMRWOFF:= .
+DEFINES += MEMRWOFF=$(MEMRWOFF)
+endif
+
 # debug build?
 ifneq ($(DEBUG),)
 DEFINES += \
diff --git a/platform/msm8953/platform.c b/platform/msm8953/platform.c
index 5f75b70..87d45f3 100644
--- a/platform/msm8953/platform.c
+++ b/platform/msm8953/platform.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2015-2018, 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
@@ -45,9 +45,17 @@
 #define APPS_SS_SIZE   ((APPS_SS_END - APPS_SS_BASE)/MB)
 
 /* LK memory - cacheable, write through */
+#ifdef SECURE_CODE_MEM
+#define LK_MEMORY         (MMU_MEMORY_TYPE_NORMAL_WRITE_BACK_ALLOCATE | \
+				MMU_MEMORY_AP_READ_ONLY |\
+				MMU_MEMORY_APX_READ_ONLY)
+
+#define LK_MEMORY_RW      (MMU_MEMORY_TYPE_NORMAL_WRITE_BACK_ALLOCATE | \
+				MMU_MEMORY_AP_READ_WRITE | MMU_MEMORY_XN)
+#else
 #define LK_MEMORY         (MMU_MEMORY_TYPE_NORMAL_WRITE_BACK_ALLOCATE | \
 				MMU_MEMORY_AP_READ_WRITE)
-
+#endif
 /* Peripherals - non-shared device */
 #define IOMAP_MEMORY      (MMU_MEMORY_TYPE_DEVICE_SHARED | \
 				MMU_MEMORY_AP_READ_WRITE | MMU_MEMORY_XN)
@@ -61,7 +69,12 @@
 
 static mmu_section_t mmu_section_table[] = {
 /*           Physical addr,         Virtual addr,            Size (in MB),     Flags */
+#ifdef SECURE_CODE_MEM
+	{    MEMBASE,               MEMBASE,                 1,                  LK_MEMORY},
+	{    MEMRWOFF,              MEMRWOFF,                (MEMSIZE / MB) - 1, LK_MEMORY_RW},
+#else
 	{    MEMBASE,               MEMBASE,                 (MEMSIZE / MB),   LK_MEMORY},
+#endif
 	{    MSM_IOMAP_BASE,        MSM_IOMAP_BASE,          MSM_IOMAP_SIZE,   IOMAP_MEMORY},
 	{    APPS_SS_BASE,          APPS_SS_BASE,            APPS_SS_SIZE,      IOMAP_MEMORY},
 	{    MSM_SHARED_IMEM_BASE,  MSM_SHARED_IMEM_BASE,    1,                COMMON_MEMORY},
diff --git a/platform/msm_shared/dev_tree.c b/platform/msm_shared/dev_tree.c
index d6b3970..2b146cc 100644
--- a/platform/msm_shared/dev_tree.c
+++ b/platform/msm_shared/dev_tree.c
@@ -109,6 +109,12 @@
  */
 extern int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size);
 
+static int dtbo_idx = INVALID_PTN;
+int get_dtbo_idx (void)
+{
+   return dtbo_idx;
+}
+
 int fdt_check_header_ext(const void *fdt)
 {
 	uintptr_t fdt_start, fdt_end;
@@ -332,7 +338,7 @@
   |     |               | PmicVariantRev  | N     | Y    | N       |
 */
 
-static void dtb_read_find_match(dt_info *current_dtb_info, dt_info *best_dtb_info, uint32_t exact_match)
+static boolean dtb_read_find_match(dt_info *current_dtb_info, dt_info *best_dtb_info, uint32_t exact_match)
 {
 	int board_id_len;
 	int platform_id_len = 0;
@@ -343,12 +349,13 @@
 	const char *platform_prop = NULL;
 	const char *board_prop = NULL;
 	const char *pmic_prop = NULL;
+	boolean find_best_match = false;
 
 	current_dtb_info->dt_match_val = 0;
 	root_offset = fdt_path_offset(dtb, "/");
 	if (root_offset < 0) {
 		dprintf(CRITICAL, "ERROR: Unable to locate root node\n");
-		return;
+		return false;
 	}
 
 	/* Get the msm-id prop from DTB and find best match */
@@ -547,9 +554,11 @@
 
 cleanup:
 	if (current_dtb_info->dt_match_val & BIT(exact_match)) {
-		if (best_dtb_info->dt_match_val < current_dtb_info->dt_match_val)
+		if (best_dtb_info->dt_match_val < current_dtb_info->dt_match_val) {
 			memscpy(best_dtb_info, sizeof(dt_info), current_dtb_info, sizeof(dt_info));
-		else if (best_dtb_info->dt_match_val == current_dtb_info->dt_match_val) {
+			find_best_match = true;
+		} else if (best_dtb_info->dt_match_val == current_dtb_info->dt_match_val) {
+			find_best_match = true;
 			if (best_dtb_info->dt_soc_rev < current_dtb_info->dt_soc_rev)
 				memscpy(best_dtb_info, sizeof(dt_info), current_dtb_info, sizeof(dt_info));
 			else if (best_dtb_info->dt_variant_major < current_dtb_info->dt_variant_major)
@@ -564,8 +573,12 @@
 				memscpy(best_dtb_info, sizeof(dt_info), current_dtb_info, sizeof(dt_info));
 			else if (best_dtb_info->dt_pmic_rev[3] < current_dtb_info->dt_pmic_rev[3])
 				memscpy(best_dtb_info, sizeof(dt_info), current_dtb_info, sizeof(dt_info));
+			else
+				find_best_match = false;
 		}
 	}
+
+	return find_best_match;
 }
 
 void *get_soc_dtb(void *kernel, uint32_t kernel_size, uint32_t dtb_offset)
@@ -628,6 +641,7 @@
 	uint32_t dtb_size = 0;
 	dt_info cur_dtb_info = {0};
 	dt_info best_dtb_info = {0};
+	boolean find_best_dtb = false;
 
 	if (!dtbo_image_buf) {
 		dprintf(CRITICAL, "dtbo image buffer is NULL\n");
@@ -656,9 +670,13 @@
 		}
 		dprintf(SPEW, "Valid board dtb is found\n");
 		cur_dtb_info.dtb = board_dtb;
-		dtb_read_find_match(&cur_dtb_info, &best_dtb_info, VARIANT_MATCH);
+		find_best_dtb = dtb_read_find_match(&cur_dtb_info, &best_dtb_info, VARIANT_MATCH);
 		dprintf(SPEW, "dtbo count = %u local_board_dt_match =%x\n",dtbo_count, cur_dtb_info.dt_match_val);
 		dtb_table_entry++;
+
+		if (find_best_dtb) {
+			dtbo_idx = dtbo_count;
+		}
 	}
 	if (!best_dtb_info.dtb) {
 		dprintf(CRITICAL, "Unable to find the board dtb\n");
diff --git a/platform/msm_shared/include/dev_tree.h b/platform/msm_shared/include/dev_tree.h
old mode 100755
new mode 100644
index 6457525..6c06b44
--- a/platform/msm_shared/include/dev_tree.h
+++ b/platform/msm_shared/include/dev_tree.h
@@ -244,4 +244,5 @@
 int update_device_tree(void *fdt, const char *, void *, unsigned);
 int dev_tree_add_mem_info(void *fdt, uint32_t offset, uint64_t size, uint64_t addr);
 void *dev_tree_appended(void *kernel, uint32_t kernel_size, uint32_t dtb_offset, void *tags);
+int get_dtbo_idx (void);
 #endif
diff --git a/project/msm8953.mk b/project/msm8953.mk
index 0c21ce0..4a502c6 100644
--- a/project/msm8953.mk
+++ b/project/msm8953.mk
@@ -43,6 +43,9 @@
 ENABLE_SMD_SUPPORT := 1
 ENABLE_PWM_SUPPORT := true
 
+#Comment this to disable this feature.
+DEFINES += SECURE_CODE_MEM=1
+
 #DEFINES += WITH_DEBUG_DCC=1
 DEFINES += WITH_DEBUG_LOG_BUF=1
 DEFINES += WITH_DEBUG_UART=1
diff --git a/target/msm8953/rules.mk b/target/msm8953/rules.mk
index 55aeb86..032988e 100644
--- a/target/msm8953/rules.mk
+++ b/target/msm8953/rules.mk
@@ -11,6 +11,11 @@
 MEMBASE := 0x8F600000 # SDRAM
 MEMSIZE := 0x00A00000 # 10MB
 
+ifneq (,$(findstring SECURE_CODE_MEM,$(DEFINES)))
+MEMRWOFF:= 0x8F700000 # MEMBASE + 1MB(section page size)
+DEFINES += MEMRWOFF=$(MEMRWOFF)
+endif
+
 BASE_ADDR        := 0x80000000
 SCRATCH_ADDR     := 0xA0100000
 SCRATCH_SIZE     := 510 # 510MB