[lk/makefile]: Add code to generate appsboot.mbn file for 7627_surf, 7630_surf and 8250_surf targets

Add code to generate appsboot.mbn for 3 targets. Add make file to support
integration of lk to eclair tree.
diff --git a/AndroidBoot.mk b/AndroidBoot.mk
new file mode 100644
index 0000000..43a0f1b
--- /dev/null
+++ b/AndroidBoot.mk
@@ -0,0 +1,11 @@
+#Android makefile to build lk bootloader as a part of Android Build
+
+BOOTLOADER_OUT := $(TOP)/$(TARGET_OUT_INTERMEDIATES)/BOOTLOADER_OBJ
+TARGET_BOOTLOADER := $(BOOTLOADER_OUT)/lk.elf
+
+$(BOOTLOADER_OUT):
+	mkdir -p $(BOOTLOADER_OUT)
+
+$(TARGET_BOOTLOADER): $(BOOTLOADER_OUT)
+	$(MAKE) -C bootable/bootloader/lk TARGET_OUT=../../../$(TARGET_OUT) BOOTLOADER_OUT=../../../$(BOOTLOADER_OUT) $(TARGET_PRODUCT)
+
diff --git a/makefile b/makefile
index d3fe248..47ccb38 100644
--- a/makefile
+++ b/makefile
@@ -24,7 +24,11 @@
 
 DEBUG ?= 2
 
-BUILDDIR := build-$(PROJECT)
+ifndef $(BOOTLOADER_OUT)
+BOOTLOADER_OUT := .
+endif
+
+BUILDDIR := $(BOOTLOADER_OUT)/build-$(PROJECT)
 OUTBIN := $(BUILDDIR)/lk.bin
 OUTELF := $(BUILDDIR)/lk
 CONFIGHEADER := $(BUILDDIR)/config.h
@@ -41,7 +45,7 @@
 LDFLAGS += -gc-sections
 
 # top level rule
-all:: $(OUTBIN) $(OUTELF).lst $(OUTELF).debug.lst $(OUTELF).sym $(OUTELF).size
+all:: $(OUTBIN) $(OUTELF).lst $(OUTELF).debug.lst $(OUTELF).sym $(OUTELF).size APPSBOOTHEADER
 
 # the following three object lists are identical except for the ordering
 # which is bootobjs, kobjs, objs
@@ -76,6 +80,7 @@
 
 include project/$(PROJECT).mk
 include target/$(TARGET)/rules.mk
+include target/$(TARGET)/tools/makefile
 include platform/$(PLATFORM)/rules.mk
 include arch/$(ARCH)/rules.mk
 include platform/rules.mk
diff --git a/target/msm7627_surf/tools/makefile b/target/msm7627_surf/tools/makefile
new file mode 100644
index 0000000..9997849
--- /dev/null
+++ b/target/msm7627_surf/tools/makefile
@@ -0,0 +1,22 @@
+#Makefile to generate appsboot.mbn
+
+ifeq ($(BOOTLOADER_OUT),.)
+APPSBOOTHEADER_DIR  := $(BUILDDIR)
+else
+APPSBOOTHEADER_DIR  := $(BOOTLOADER_OUT)/../../
+endif
+
+SRC_DIR  := target/$(TARGET)/tools
+COMPILER := gcc
+
+APPSBOOTHEADER: appsboot.mbn appsboothd.mbn
+
+appsboot.mbn: $(OUTBIN)
+	cp -r $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboot.mbn
+
+appsboothd.mbn: mkheader $(OUTBIN)
+	$(SRC_DIR)/mkheader $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboothd.mbn
+
+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
new file mode 100644
index 0000000..302bea2
--- /dev/null
+++ b/target/msm7627_surf/tools/mkheader.c
@@ -0,0 +1,55 @@
+/* Copyright 2007, Google Inc. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/stat.h>
+
+int main(int argc, char *argv[])
+{
+	struct stat s;
+	unsigned size, base;
+	unsigned magic[10];
+	int fd;
+
+	if(argc != 3) {
+		fprintf(stderr,"usage: mkheader <bin> <hdr>\n");
+		return -1;
+	}
+
+	if(stat(argv[1], &s)) {
+		perror("cannot stat binary");
+		return -1;
+	}
+
+	size = s.st_size;
+	base = 0;
+
+	magic[0] = 0x00000005; /* appsbl */
+	magic[1] = 0x00000002; /* nand */
+	magic[2] = 0x00000000; 
+	magic[3] = base;
+	magic[4] = size;
+	magic[5] = size;
+	magic[6] = size + base;
+	magic[7] = 0x00000000; 
+	magic[8] = size + base;
+        magic[9] = 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)) {
+		perror("cannot write header");
+		close(fd);
+		unlink(argv[2]);
+		return -1;
+	}
+	close(fd);
+
+	return 0;
+}
diff --git a/target/msm7630_surf/tools/makefile b/target/msm7630_surf/tools/makefile
new file mode 100644
index 0000000..de55adf
--- /dev/null
+++ b/target/msm7630_surf/tools/makefile
@@ -0,0 +1,24 @@
+#Makefile to generate appsboot.mbn
+
+ifeq ($(BOOTLOADER_OUT),.)
+APPSBOOTHEADER_DIR  := $(BUILDDIR)
+else
+APPSBOOTHEADER_DIR  := $(BOOTLOADER_OUT)/../../
+endif
+
+SRC_DIR  := target/$(TARGET)/tools
+COMPILER := gcc
+
+APPSBOOTHEADER: appsboot.mbn
+
+
+appsboot.mbn: appsboothd.mbn $(OUTBIN)
+	cat $(APPSBOOTHEADER_DIR)/appsboothd.mbn $(OUTBIN) > $(APPSBOOTHEADER_DIR)/appsboot.mbn
+	rm -rf $(APPSBOOTHEADER_DIR)/appsboothd.mbn 
+
+appsboothd.mbn: mkheader $(OUTBIN)
+	$(SRC_DIR)/mkheader $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboothd.mbn
+
+mkheader: $(SRC_DIR)/mkheader.c
+	${COMPILER} $(SRC_DIR)/mkheader.c -o $(SRC_DIR)/mkheader
+
diff --git a/target/msm7630_surf/tools/mkheader.c b/target/msm7630_surf/tools/mkheader.c
new file mode 100644
index 0000000..9b03907
--- /dev/null
+++ b/target/msm7630_surf/tools/mkheader.c
@@ -0,0 +1,87 @@
+/* Copyright 2007, Google Inc. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/stat.h>
+
+int main(int argc, char *argv[])
+{
+	struct stat s;
+	unsigned size, base;
+	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) {
+		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[3] = base;
+	magic[4] = size;
+	magic[5] = size;
+	magic[6] = size + base;
+	magic[7] = 0x00000000;
+	magic[8] = size + base;
+	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, magic_len) != magic_len) {
+		perror("cannot write header");
+		close(fd);
+		unlink(argv[2]);
+		return -1;
+	}
+	close(fd);
+
+	return 0;
+}
diff --git a/target/qsd8250_surf/tools/makefile b/target/qsd8250_surf/tools/makefile
new file mode 100644
index 0000000..de55adf
--- /dev/null
+++ b/target/qsd8250_surf/tools/makefile
@@ -0,0 +1,24 @@
+#Makefile to generate appsboot.mbn
+
+ifeq ($(BOOTLOADER_OUT),.)
+APPSBOOTHEADER_DIR  := $(BUILDDIR)
+else
+APPSBOOTHEADER_DIR  := $(BOOTLOADER_OUT)/../../
+endif
+
+SRC_DIR  := target/$(TARGET)/tools
+COMPILER := gcc
+
+APPSBOOTHEADER: appsboot.mbn
+
+
+appsboot.mbn: appsboothd.mbn $(OUTBIN)
+	cat $(APPSBOOTHEADER_DIR)/appsboothd.mbn $(OUTBIN) > $(APPSBOOTHEADER_DIR)/appsboot.mbn
+	rm -rf $(APPSBOOTHEADER_DIR)/appsboothd.mbn 
+
+appsboothd.mbn: mkheader $(OUTBIN)
+	$(SRC_DIR)/mkheader $(OUTBIN) $(APPSBOOTHEADER_DIR)/appsboothd.mbn
+
+mkheader: $(SRC_DIR)/mkheader.c
+	${COMPILER} $(SRC_DIR)/mkheader.c -o $(SRC_DIR)/mkheader
+
diff --git a/target/qsd8250_surf/tools/mkheader.c b/target/qsd8250_surf/tools/mkheader.c
new file mode 100644
index 0000000..302bea2
--- /dev/null
+++ b/target/qsd8250_surf/tools/mkheader.c
@@ -0,0 +1,55 @@
+/* Copyright 2007, Google Inc. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/stat.h>
+
+int main(int argc, char *argv[])
+{
+	struct stat s;
+	unsigned size, base;
+	unsigned magic[10];
+	int fd;
+
+	if(argc != 3) {
+		fprintf(stderr,"usage: mkheader <bin> <hdr>\n");
+		return -1;
+	}
+
+	if(stat(argv[1], &s)) {
+		perror("cannot stat binary");
+		return -1;
+	}
+
+	size = s.st_size;
+	base = 0;
+
+	magic[0] = 0x00000005; /* appsbl */
+	magic[1] = 0x00000002; /* nand */
+	magic[2] = 0x00000000; 
+	magic[3] = base;
+	magic[4] = size;
+	magic[5] = size;
+	magic[6] = size + base;
+	magic[7] = 0x00000000; 
+	magic[8] = size + base;
+        magic[9] = 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)) {
+		perror("cannot write header");
+		close(fd);
+		unlink(argv[2]);
+		return -1;
+	}
+	close(fd);
+
+	return 0;
+}