msm7627: Add support for eMMC boot for msm7627_surf and msm7627_ffa
diff --git a/target/msm7627_ffa/init.c b/target/msm7627_ffa/init.c
index 663d3af..7157525 100644
--- a/target/msm7627_ffa/init.c
+++ b/target/msm7627_ffa/init.c
@@ -77,6 +77,8 @@
 
 void keypad_init(void);
 
+int target_is_emmc_boot(void);
+
 void target_init(void)
 {
 	unsigned offset;
@@ -88,6 +90,9 @@
 	keys_init();
 	keypad_init();
 
+	if (target_is_emmc_boot())
+		return;
+
 	ptable_init(&flash_ptable);
 	smem_ptable_init();
 
diff --git a/target/msm7627_ffa/tools/makefile b/target/msm7627_ffa/tools/makefile
index 9997849..62da179 100644
--- a/target/msm7627_ffa/tools/makefile
+++ b/target/msm7627_ffa/tools/makefile
@@ -9,14 +9,26 @@
 SRC_DIR  := target/$(TARGET)/tools
 COMPILER := gcc
 
-APPSBOOTHEADER: appsboot.mbn appsboothd.mbn
+ifeq ($(EMMC_BOOT), 1)
+APPSBOOTHDR_FILES := emmc_appsboot.mbn emmc_appsboothd.mbn
+else
+APPSBOOTHDR_FILES := appsboot.mbn appsboothd.mbn
+endif
+
+APPSBOOTHEADER: $(APPSBOOTHDR_FILES)
 
 appsboot.mbn: $(OUTBIN)
-	cp -r $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboot.mbn
+	cp -f $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboot.mbn
 
 appsboothd.mbn: mkheader $(OUTBIN)
 	$(SRC_DIR)/mkheader $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboothd.mbn
 
+emmc_appsboot.mbn: $(OUTBIN)
+	cp -f $(OUTBIN) $(APPSBOOTHEADER_DIR)/emmc_appsboot.mbn
+
+emmc_appsboothd.mbn: mkheader $(OUTBIN)
+	$(SRC_DIR)/mkheader $(OUTBIN) $(APPSBOOTHEADER_DIR)/emmc_appsboothd.mbn unified-boot
+
 mkheader: $(SRC_DIR)/mkheader.c
 	${COMPILER} $(SRC_DIR)/mkheader.c -o $(SRC_DIR)/mkheader
 
diff --git a/target/msm7627_ffa/tools/mkheader.c b/target/msm7627_ffa/tools/mkheader.c
index 302bea2..9b03907 100644
--- a/target/msm7627_ffa/tools/mkheader.c
+++ b/target/msm7627_ffa/tools/mkheader.c
@@ -11,39 +11,71 @@
 {
 	struct stat s;
 	unsigned size, base;
-	unsigned magic[10];
+	int unified_boot = 0;
+	unsigned unified_boot_magic[20];
+	unsigned non_unified_boot_magic[10];
+	unsigned magic_len = 0;
+	unsigned *magic;
 	int fd;
 
-	if(argc != 3) {
+	if(argc < 3) {
 		fprintf(stderr,"usage: mkheader <bin> <hdr>\n");
 		return -1;
 	}
 
+	if (argc == 4) {
+		if(!strcmp("unified-boot",argv[3])) {
+			unified_boot = 1;
+		}
+	}
+
 	if(stat(argv[1], &s)) {
 		perror("cannot stat binary");
 		return -1;
 	}
 
+	if(unified_boot) {
+		magic = unified_boot_magic;
+		magic_len = sizeof(unified_boot_magic);
+	} else {
+		magic = non_unified_boot_magic;
+		magic_len = sizeof(non_unified_boot_magic);
+	}
+
 	size = s.st_size;
 	base = 0;
 
 	magic[0] = 0x00000005; /* appsbl */
 	magic[1] = 0x00000002; /* nand */
-	magic[2] = 0x00000000; 
+	magic[2] = 0x00000000;
 	magic[3] = base;
 	magic[4] = size;
 	magic[5] = size;
 	magic[6] = size + base;
-	magic[7] = 0x00000000; 
+	magic[7] = 0x00000000;
 	magic[8] = size + base;
-        magic[9] = 0x00000000;
+	magic[9] = 0x00000000;
+
+	if (unified_boot == 1)
+	{
+		magic[10] = 0x33836685; /* cookie magic number */
+		magic[11] = 0x00000001; /* cookie version */
+		magic[12] = 0x00000002; /* file formats */
+		magic[13] = 0x00000000;
+		magic[14] = 0x00500000; /* 5M for boot.img */
+		magic[15] = 0x00000000;
+		magic[16] = 0x00000000;
+		magic[17] = 0x00000000;
+		magic[18] = 0x00000000;
+		magic[19] = 0x00000000;
+	}
 
 	fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
 	if(fd < 0) {
 		perror("cannot open header for writing");
 		return -1;
 	}
-	if(write(fd, magic, sizeof(magic)) != sizeof(magic)) {
+	if(write(fd, magic, magic_len) != magic_len) {
 		perror("cannot write header");
 		close(fd);
 		unlink(argv[2]);
diff --git a/target/msm7627_surf/init.c b/target/msm7627_surf/init.c
index d39350b..c298570 100644
--- a/target/msm7627_surf/init.c
+++ b/target/msm7627_surf/init.c
@@ -77,6 +77,8 @@
 
 void keypad_init(void);
 
+int target_is_emmc_boot(void);
+
 void target_init(void)
 {
 	unsigned offset;
@@ -88,6 +90,9 @@
 	keys_init();
 	keypad_init();
 
+	if (target_is_emmc_boot())
+		return;
+
 	ptable_init(&flash_ptable);
 	smem_ptable_init();
 
diff --git a/target/msm7627_surf/tools/makefile b/target/msm7627_surf/tools/makefile
index 9997849..62da179 100644
--- a/target/msm7627_surf/tools/makefile
+++ b/target/msm7627_surf/tools/makefile
@@ -9,14 +9,26 @@
 SRC_DIR  := target/$(TARGET)/tools
 COMPILER := gcc
 
-APPSBOOTHEADER: appsboot.mbn appsboothd.mbn
+ifeq ($(EMMC_BOOT), 1)
+APPSBOOTHDR_FILES := emmc_appsboot.mbn emmc_appsboothd.mbn
+else
+APPSBOOTHDR_FILES := appsboot.mbn appsboothd.mbn
+endif
+
+APPSBOOTHEADER: $(APPSBOOTHDR_FILES)
 
 appsboot.mbn: $(OUTBIN)
-	cp -r $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboot.mbn
+	cp -f $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboot.mbn
 
 appsboothd.mbn: mkheader $(OUTBIN)
 	$(SRC_DIR)/mkheader $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboothd.mbn
 
+emmc_appsboot.mbn: $(OUTBIN)
+	cp -f $(OUTBIN) $(APPSBOOTHEADER_DIR)/emmc_appsboot.mbn
+
+emmc_appsboothd.mbn: mkheader $(OUTBIN)
+	$(SRC_DIR)/mkheader $(OUTBIN) $(APPSBOOTHEADER_DIR)/emmc_appsboothd.mbn unified-boot
+
 mkheader: $(SRC_DIR)/mkheader.c
 	${COMPILER} $(SRC_DIR)/mkheader.c -o $(SRC_DIR)/mkheader
 
diff --git a/target/msm7627_surf/tools/mkheader.c b/target/msm7627_surf/tools/mkheader.c
index 302bea2..9b03907 100644
--- a/target/msm7627_surf/tools/mkheader.c
+++ b/target/msm7627_surf/tools/mkheader.c
@@ -11,39 +11,71 @@
 {
 	struct stat s;
 	unsigned size, base;
-	unsigned magic[10];
+	int unified_boot = 0;
+	unsigned unified_boot_magic[20];
+	unsigned non_unified_boot_magic[10];
+	unsigned magic_len = 0;
+	unsigned *magic;
 	int fd;
 
-	if(argc != 3) {
+	if(argc < 3) {
 		fprintf(stderr,"usage: mkheader <bin> <hdr>\n");
 		return -1;
 	}
 
+	if (argc == 4) {
+		if(!strcmp("unified-boot",argv[3])) {
+			unified_boot = 1;
+		}
+	}
+
 	if(stat(argv[1], &s)) {
 		perror("cannot stat binary");
 		return -1;
 	}
 
+	if(unified_boot) {
+		magic = unified_boot_magic;
+		magic_len = sizeof(unified_boot_magic);
+	} else {
+		magic = non_unified_boot_magic;
+		magic_len = sizeof(non_unified_boot_magic);
+	}
+
 	size = s.st_size;
 	base = 0;
 
 	magic[0] = 0x00000005; /* appsbl */
 	magic[1] = 0x00000002; /* nand */
-	magic[2] = 0x00000000; 
+	magic[2] = 0x00000000;
 	magic[3] = base;
 	magic[4] = size;
 	magic[5] = size;
 	magic[6] = size + base;
-	magic[7] = 0x00000000; 
+	magic[7] = 0x00000000;
 	magic[8] = size + base;
-        magic[9] = 0x00000000;
+	magic[9] = 0x00000000;
+
+	if (unified_boot == 1)
+	{
+		magic[10] = 0x33836685; /* cookie magic number */
+		magic[11] = 0x00000001; /* cookie version */
+		magic[12] = 0x00000002; /* file formats */
+		magic[13] = 0x00000000;
+		magic[14] = 0x00500000; /* 5M for boot.img */
+		magic[15] = 0x00000000;
+		magic[16] = 0x00000000;
+		magic[17] = 0x00000000;
+		magic[18] = 0x00000000;
+		magic[19] = 0x00000000;
+	}
 
 	fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
 	if(fd < 0) {
 		perror("cannot open header for writing");
 		return -1;
 	}
-	if(write(fd, magic, sizeof(magic)) != sizeof(magic)) {
+	if(write(fd, magic, magic_len) != magic_len) {
 		perror("cannot write header");
 		close(fd);
 		unlink(argv[2]);