Merge "target: msm8952: Enable RM69090 truly cmd mode panel support"
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
old mode 100644
new mode 100755
index 3d82528..55a145a
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -210,6 +210,9 @@
 
 #define MAX_DTBO_IDX_STR 64
 static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
+
+#define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR
+static const char *android_boot_dtb_idx = " androidboot.dtb_idx=";
 #endif
 
 #if VERIFIED_BOOT
@@ -453,6 +456,8 @@
 	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;
+	char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
+	int dtb_idx = INVALID_PTN;
 #endif
 	char syspath_buf[syspath_buflen];
 #if HIBERNATION_SUPPORT
@@ -712,6 +717,13 @@
 			android_boot_dtbo_idx, dtbo_idx);
 		cmdline_len += strlen (dtbo_idx_str);
 	}
+
+	dtb_idx = get_dtb_idx ();
+	if (dtb_idx != INVALID_PTN) {
+		snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
+				 android_boot_dtb_idx, dtb_idx);
+		cmdline_len += strlen (dtb_idx_str);
+	}
 #endif
 
 	if (cmdline_len > 0) {
@@ -992,6 +1004,12 @@
 			--dst;
 			while ((*dst++ = *src++));
 		}
+
+		if (dtb_idx != INVALID_PTN) {
+			src = dtb_idx_str;
+			--dst;
+			while ((*dst++ = *src++));
+		}
 #endif
 	}
 
@@ -1476,8 +1494,11 @@
 	unsigned ramdisk_actual;
 	unsigned imagesize_actual;
 	unsigned second_actual = 0;
+	void * image_buf = NULL;
 
 	unsigned int dtb_size = 0;
+	unsigned dtb_image_size = 0;
+	uint32_t dtb_image_offset = 0;
 	unsigned int out_len = 0;
 	unsigned int out_avai_len = 0;
 	unsigned char *out_addr = NULL;
@@ -1601,6 +1622,7 @@
 	}
 	imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
 #endif
+	dtb_image_size = hdr->kernel_size;
 
 #ifdef OSVERSION_IN_BOOTIMAGE
 	/* If header version is ONE and booting into recovery,
@@ -1660,8 +1682,31 @@
 		dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
 
 	}
+
+	if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
+		struct boot_img_hdr_v1 *hdr1 =
+			(struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
+		struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
+			(image_addr + sizeof(boot_img_hdr) +
+			BOOT_IMAGE_HEADER_V2_OFFSET);
+		unsigned int recovery_dtbo_actual = 0;
+
+		imagesize_actual += ROUND_TO_PAGE(hdr1->recovery_dtbo_size,
+					page_mask);
+
+		imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
+
+
+		dtb_image_offset = page_size + patched_kernel_hdr_size +
+				   kernel_actual + ramdisk_actual + second_actual +
+				   recovery_dtbo_actual;
+
+		dprintf(SPEW, "Header version: %d\n", hdr->header_version);
+		dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
+	}
 #endif
 
+
 	/* Validate the boot/recovery image size is within the bounds of partition size */
 	if (imagesize_actual > image_size) {
 		dprintf(CRITICAL, "Image size is greater than partition size.\n");
@@ -2047,10 +2092,16 @@
 		 * In case of dtbo, this API is going to read dtbo on scratch.
 		 */
 		void *dtb;
-		dtb = dev_tree_appended(
-				(void*)(image_addr + page_size +
-					patched_kernel_hdr_size),
-				hdr->kernel_size, dtb_offset,
+		image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
+
+		if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
+
+			image_buf = (void*)(image_addr);
+			dtb_offset = dtb_image_offset;
+			dtb_image_size = imagesize_actual;
+		}
+
+		dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
 				(void *)hdr->tags_addr);
 		if (!dtb) {
 			dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
diff --git a/app/aboot/bootimg.h b/app/aboot/bootimg.h
index a07db93..1ad5a64 100644
--- a/app/aboot/bootimg.h
+++ b/app/aboot/bootimg.h
@@ -2,7 +2,7 @@
  * Copyright (C) 2008 The Android Open Source Project
  * All rights reserved.
  *
- * Copyright (c) 2014,2016 The Linux Foundation. All rights reserved.
+ * Copyright (c) 2014,2016,2019 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
@@ -154,6 +154,50 @@
  *    else: jump to kernel_addr
  */
 
+#define BOOT_IMAGE_HEADER_V2_OFFSET sizeof (struct boot_img_hdr_v1)
+#define BOOT_HEADER_VERSION_TWO 2
+
+struct boot_img_hdr_v2 {
+    uint32_t dtb_size; /* size in bytes for DTB image */
+    uint64_t dtb_addr; /* physical load address for DTB image */
+} __attribute__((packed));
+
+/* When the boot image header has a version of BOOT_HEADER_VERSION_TWO,
+ * the structure of the boot image is as follows:
+ *
+ * +-----------------+
+ * | boot header     | 1 page
+ * +-----------------+
+ * | kernel          | n pages
+ * +-----------------+
+ * | ramdisk         | m pages
+ * +-----------------+
+ * | second stage    | o pages
+ * +-----------------+
+ * | recovery dtbo   | p pages
+ * +-----------------+
+ * | dtb.img         | q pages
+ * +-----------------+
+ * n = (kernel_size + page_size - 1) / page_size
+ * m = (ramdisk_size + page_size - 1) / page_size
+ * o = (second_size + page_size - 1) / page_size
+ * p = (recovery_dtbo_size + page_size - 1) / page_size
+ * q = (dtb_size + page_size - 1) / page_size
+ *
+ * 0. all entities are page_size aligned in flash
+ * 1. kernel and ramdisk are required (size != 0)
+ * 2. recovery_dtbo is required for recovery.img
+ *    in non-A/B devices(recovery_dtbo_size != 0)
+ * 3. second is optional (second_size == 0 -> no second)
+ * 4. load each element (kernel, ramdisk, second, recovery_dtbo) at
+ *    the specified physical address (kernel_addr, etc)
+ * 5. prepare tags at tag_addr.  kernel_args[] is
+ *    appended to the kernel commandline in the tags.
+ * 6. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
+ * 7. if second_size != 0: jump to second_addr
+ *    else: jump to kernel_addr
+ */
+
 boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
                         void *ramdisk, unsigned ramdisk_size,
                         void *second, unsigned second_size,
diff --git a/platform/msm_shared/dev_tree.c b/platform/msm_shared/dev_tree.c
old mode 100644
new mode 100755
index fa7b28a..024b5c3
--- a/platform/msm_shared/dev_tree.c
+++ b/platform/msm_shared/dev_tree.c
@@ -113,6 +113,12 @@
    return dtbo_idx;
 }
 
+static int dtb_idx = INVALID_PTN;
+int get_dtb_idx (void)
+{
+   return dtb_idx;
+}
+
 int fdt_check_header_ext(const void *fdt)
 {
 	uintptr_t fdt_start, fdt_end;
@@ -621,6 +627,8 @@
 	uint32_t dtb_size = 0;
 	dt_info cur_dtb_info = {0};
 	dt_info best_dtb_info = {0};
+	uint32_t dtb_cnt = 0;
+	boolean find_best_dtb = false;
 
 	if (!dtb_offset){
 		dprintf(CRITICAL, "DTB offset is NULL\n");
@@ -642,7 +650,7 @@
 		break;
 
 		cur_dtb_info.dtb = dtb;
-		dtb_read_find_match(&cur_dtb_info, &best_dtb_info, SOC_MATCH);
+		find_best_dtb = dtb_read_find_match(&cur_dtb_info, &best_dtb_info, SOC_MATCH);
 		if (cur_dtb_info.dt_match_val) {
 			if (cur_dtb_info.dt_match_val & BIT(SOC_MATCH)) {
 				if (check_all_bits_set(cur_dtb_info.dt_match_val)) {
@@ -651,8 +659,15 @@
 				}
 			}
 		}
-		dprintf(SPEW, "Best Match DTB VAL = %x\n", best_dtb_info.dt_match_val);
+
+		dprintf(SPEW, "dtb count = %u local_soc_dt_match val = %x\n", dtb_cnt, best_dtb_info.dt_match_val);
 		dtb += dtb_size;
+
+		if (find_best_dtb) {
+			dtb_idx = dtb_cnt;
+		}
+		dtb_cnt++;
+
 	}
 	if (!best_dtb_info.dtb) {
 		dprintf(CRITICAL, "No match found for soc dtb type\n");
@@ -755,6 +770,7 @@
 	uint32_t msm_data_count;
 	uint32_t board_data_count;
 	uint32_t pmic_data_count;
+	uint32_t dtb_count = 0;;
 
 	root_offset = fdt_path_offset(dtb, "/");
 	if (root_offset < 0)
@@ -962,9 +978,11 @@
 		i = 0;
 		k = 0;
 		n = 0;
+		dtb_count++;
 		for (i = 0; i < msm_data_count; i++) {
 			for (j = 0; j < board_data_count; j++) {
-				if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop) {
+				dt_entry_array[k].idx = dtb_count;
+				if (dtb_ver == DEV_TREE_VERSION_V3 && pmic_prop){
 					for (n = 0; n < pmic_data_count; n++) {
 						dt_entry_array[k].platform_id = platform_data[i].platform_id;
 						dt_entry_array[k].soc_rev = platform_data[i].soc_rev;
@@ -1145,9 +1163,9 @@
  * Will relocate the DTB to the tags addr if the device tree is found and return
  * its address
  *
- * Arguments:    kernel - Start address of the kernel loaded in RAM
+ * Arguments:    kernel - Start address of the kernel/bootimage loaded in RAM
  *               tags - Start address of the tags loaded in RAM
- *               kernel_size - Size of the kernel in bytes
+ *               kernel_size - Size of the kernel/bootimage size in bytes
  *
  * Return Value: DTB address : If appended device tree is found
  *               'NULL'         : Otherwise
@@ -1216,12 +1234,14 @@
 	if (best_match_dt_entry){
 		bestmatch_tag = (void *)best_match_dt_entry->offset;
 		bestmatch_tag_size = best_match_dt_entry->size;
-		dprintf(INFO, "Best match DTB tags %u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
-			best_match_dt_entry->platform_id, best_match_dt_entry->variant_id,
-			best_match_dt_entry->board_hw_subtype, best_match_dt_entry->soc_rev,
-			best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1],
-			best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3],
-			best_match_dt_entry->offset, best_match_dt_entry->size);
+		dtb_idx = best_match_dt_entry->idx;
+		dprintf(INFO, "Best match DTB tags %u/%u/%08x/0x%08x/%x/%x/%x/%x/%x/%x/%x\n",
+			best_match_dt_entry->idx, best_match_dt_entry->platform_id,
+			best_match_dt_entry->variant_id, best_match_dt_entry->board_hw_subtype,
+			best_match_dt_entry->soc_rev, best_match_dt_entry->pmic_rev[0],
+			best_match_dt_entry->pmic_rev[1], best_match_dt_entry->pmic_rev[2],
+			best_match_dt_entry->pmic_rev[3], best_match_dt_entry->offset,
+			best_match_dt_entry->size);
 		dprintf(INFO, "Using pmic info 0x%0x/0x%x/0x%x/0x%0x for device 0x%0x/0x%x/0x%x/0x%0x\n",
 			best_match_dt_entry->pmic_rev[0], best_match_dt_entry->pmic_rev[1],
 			best_match_dt_entry->pmic_rev[2], best_match_dt_entry->pmic_rev[3],
diff --git a/platform/msm_shared/include/dev_tree.h b/platform/msm_shared/include/dev_tree.h
index 1d8dd07..4449286 100644
--- a/platform/msm_shared/include/dev_tree.h
+++ b/platform/msm_shared/include/dev_tree.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2014,2017-2018 The Linux Foundation. All rights reserved.
+/* Copyright (c) 2012-2014,2017-2019 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
@@ -163,6 +163,7 @@
 	uint32_t pmic_rev[4];
 	uint32_t offset;
 	uint32_t size;
+	uint32_t idx;
 };
 
 struct dt_table
@@ -256,4 +257,5 @@
 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);
+int get_dtb_idx (void);
 #endif