[msm] Add nandwrite to support flashing apps images over JTAG

Port over nandwrite from legacy to allow flashing of apps images
using JTAG.  nandwrite is an app in LK with its own project name that
runs in its own thread, just like aboot.  Just like aboot can accept
commands to flash images over usb, nandwrite accepts commands over
JTAG interface to allow flashing of apps images.
diff --git a/AndroidBoot.mk b/AndroidBoot.mk
index a976140..e5e2c28 100644
--- a/AndroidBoot.mk
+++ b/AndroidBoot.mk
@@ -1,7 +1,7 @@
 #Android makefile to build lk bootloader as a part of Android Build
 
-BOOTLOADER_OUT := $(TOP)/$(TARGET_OUT_INTERMEDIATES)/BOOTLOADER_OBJ
 TARGET_BOOTLOADER := out/target/product/$(TARGET_PRODUCT)/appsboot.mbn
+BOOTLOADER_OUT := $(TOP)/$(TARGET_OUT_INTERMEDIATES)/BOOTLOADER_OBJ
 
 $(BOOTLOADER_OUT):
 	mkdir -p $(BOOTLOADER_OUT)
@@ -9,3 +9,15 @@
 $(TARGET_BOOTLOADER): $(BOOTLOADER_OUT)
 	$(MAKE) -C bootable/bootloader/lk BOOTLOADER_OUT=../../../$(BOOTLOADER_OUT) $(TARGET_PRODUCT)
 
+
+#build nandwrite as a part of Android Build
+TARGET_NANDWRITE := $(PRODUCT_OUT)/obj/nandwrite/build-$(TARGET_PRODUCT)_nandwrite/lk
+NANDWRITE_OUT := $(TOP)/$(TARGET_OUT_INTERMEDIATES)/nandwrite
+
+$(NANDWRITE_OUT):
+	mkdir -p $(BOOTLOADER_OUT)
+
+$(TARGET_NANDWRITE): $(NANDWRITE_OUT)
+	@echo $(TARGET_PRODUCT)_nandwrite
+	$(MAKE) -C bootable/bootloader/lk BOOTLOADER_OUT=../../../$(NANDWRITE_OUT) $(TARGET_PRODUCT)_nandwrite
+
diff --git a/app/nandwrite/bootimg.h b/app/nandwrite/bootimg.h
new file mode 100644
index 0000000..87a0b3a
--- /dev/null
+++ b/app/nandwrite/bootimg.h
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
+ * Copyright (c) 2009, 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE 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.
+ */
+
+#ifndef _BOOT_IMAGE_H_
+#define _BOOT_IMAGE_H_
+
+typedef struct boot_img_hdr boot_img_hdr;
+
+#define BOOT_MAGIC "ANDROID!"
+#define BOOT_MAGIC_SIZE 8
+#define BOOT_NAME_SIZE 16
+#define BOOT_ARGS_SIZE 512
+
+struct boot_img_hdr
+{
+    unsigned char magic[BOOT_MAGIC_SIZE];
+
+    unsigned kernel_size;  /* size in bytes */
+    unsigned kernel_addr;  /* physical load addr */
+
+    unsigned ramdisk_size; /* size in bytes */
+    unsigned ramdisk_addr; /* physical load addr */
+
+    unsigned second_size;  /* size in bytes */
+    unsigned second_addr;  /* physical load addr */
+
+    unsigned tags_addr;    /* physical addr for kernel tags */
+    unsigned page_size;    /* flash page size we assume */
+    unsigned unused[2];    /* future expansion: should be 0 */
+
+    unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
+
+    unsigned char cmdline[BOOT_ARGS_SIZE];
+
+    unsigned id[8]; /* timestamp / checksum / sha1 / etc */
+};
+
+/*
+** +-----------------+
+** | boot header     | 1 page
+** +-----------------+
+** | kernel          | n pages
+** +-----------------+
+** | ramdisk         | m pages
+** +-----------------+
+** | second stage    | o 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
+**
+** 0. all entities are page_size aligned in flash
+** 1. kernel and ramdisk are required (size != 0)
+** 2. second is optional (second_size == 0 -> no second)
+** 3. load each element (kernel, ramdisk, second) at
+**    the specified physical address (kernel_addr, etc)
+** 4. prepare tags at tag_addr.  kernel_args[] is
+**    appended to the kernel commandline in the tags.
+** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
+** 6. 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,
+                        unsigned page_size,
+                        unsigned *bootimg_size);
+
+void bootimg_set_cmdline(boot_img_hdr *hdr, const char *cmdline);
+#endif
+
diff --git a/app/nandwrite/nandwrite.c b/app/nandwrite/nandwrite.c
new file mode 100644
index 0000000..bc3f0cc
--- /dev/null
+++ b/app/nandwrite/nandwrite.c
@@ -0,0 +1,194 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
+ * Copyright (c) 2009, 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE 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 <app.h>
+#include <debug.h>
+#include <lib/ptable.h>
+#include <malloc.h>
+#include <dev/flash.h>
+#include <string.h>
+#include <jtag.h>
+#include "bootimg.h"
+
+#define FLASH_PAGE_SIZE 2048
+#define FLASH_PAGE_BITS 11
+
+unsigned page_size = 0;
+unsigned page_mask = 0;
+
+#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
+
+void acpu_clock_init(void);
+
+int startswith(const char *str, const char *prefix)
+{
+    while(*prefix){
+        if(*prefix++ != *str++) return 0;
+    }
+    return 1;
+}
+
+/* XXX */
+void verify_flash(struct ptentry *p, void *addr, unsigned len, int extra)
+{
+    uint32_t offset = 0;
+    void *buf = malloc(FLASH_PAGE_SIZE + extra);
+    int verify_extra = extra;
+    if(verify_extra > 4)
+        verify_extra = 16;
+    while(len > 0) {
+        flash_read_ext(p, extra, offset, buf, FLASH_PAGE_SIZE);
+        if(memcmp(addr, buf, FLASH_PAGE_SIZE + verify_extra)) {
+            dprintf(CRITICAL, "verify failed at 0x%08x\n", offset);
+            jtag_fail("verify failed");
+            return;
+        }
+        offset += FLASH_PAGE_SIZE;
+        addr += FLASH_PAGE_SIZE;
+        len -= FLASH_PAGE_SIZE;
+        if(extra) {
+            addr += extra;
+            len -= extra;
+        }
+    }
+    dprintf(INFO, "verify done %d extra bytes\n", verify_extra);
+    jtag_okay("verify done");
+}
+
+void handle_flash(const char *name, unsigned addr, unsigned sz)
+{
+	struct ptentry *ptn;
+	struct ptable *ptable;
+        void *data = (void *) addr;
+	unsigned extra = 0;
+
+	ptable = flash_get_ptable();
+	if (ptable == NULL) {
+		jtag_fail("partition table doesn't exist");
+		return;
+	}
+
+	ptn = ptable_find(ptable, name);
+	if (ptn == NULL) {
+		jtag_fail("unknown partition name");
+		return;
+	}
+
+	if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
+		if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
+			jtag_fail("image is not a boot image");
+			return;
+		}
+	}
+
+	if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata"))
+		extra = ((page_size >> 9) * 16);
+	else
+		sz = ROUND_TO_PAGE(sz, page_mask);
+
+	dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
+	if (flash_write(ptn, extra, data, sz)) {
+		jtag_fail("flash write failure");
+		return;
+	}
+	dprintf(INFO, "partition '%s' updated\n", ptn->name);
+        jtag_okay("Done");
+}
+
+static unsigned char *tmpbuf = 0;
+
+
+/*XXX*/
+void handle_dump(const char *name, unsigned offset)
+{
+    struct ptentry *p;
+    struct ptable *ptable;
+
+    if(tmpbuf == 0) {
+        tmpbuf = malloc(4096);
+    }
+
+    dprintf(INFO, "dump '%s' partition\n", name);
+
+    ptable = flash_get_ptable();
+
+    if (ptable == NULL) {
+        jtag_fail("partition table doesn't exist");
+        return;
+    }
+
+    p = ptable_find(ptable, name);
+
+    if(p == 0) {
+        jtag_fail("partition not found");
+        return;
+    } else {
+
+#if 0
+        /* XXX reimpl */
+        if(flash_read_page(p->start * 64, tmpbuf, tmpbuf + 2048)){
+            jtag_fail("flash_read() failed");
+            return;
+        }
+#endif
+        dprintf(INFO, "page %d data:\n", p->start * 64);
+        hexdump(tmpbuf, 256);
+        dprintf(INFO, "page %d extra:\n", p->start * 64);
+        hexdump(tmpbuf, 16);
+        jtag_okay("done");
+    }
+}
+
+void handle_command(const char *cmd, unsigned a0, unsigned a1, unsigned a2)
+{
+    if(startswith(cmd,"flash:")){
+        handle_flash(cmd + 6, a0, a1);
+        return;
+    }
+
+    if(startswith(cmd,"dump:")){
+        handle_dump(cmd + 5, a0);
+        return;
+    }
+
+    jtag_fail("unknown command");
+}
+
+void nandwrite_init(const struct app_descriptor *app)
+{
+    page_size = flash_page_size();
+    page_mask = page_size - 1;
+    jtag_cmd_loop(handle_command);
+}
+
+APP_START(nandwrite)
+	.init = nandwrite_init,
+APP_END
+
+
diff --git a/app/nandwrite/rules.mk b/app/nandwrite/rules.mk
new file mode 100644
index 0000000..40d84ab
--- /dev/null
+++ b/app/nandwrite/rules.mk
@@ -0,0 +1,6 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+OBJS += \
+	$(LOCAL_DIR)/nandwrite.o \
+
+
diff --git a/platform/msm_shared/debug.c b/platform/msm_shared/debug.c
index ebe7361..935d886 100644
--- a/platform/msm_shared/debug.c
+++ b/platform/msm_shared/debug.c
@@ -1,6 +1,7 @@
 /*
  * Copyright (c) 2009, Google Inc.
  * All rights reserved.
+ * Copyright (c) 2009, 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
@@ -49,6 +50,9 @@
 #if WITH_DEBUG_FBCON && WITH_DEV_FBCON
 	fbcon_putc(c);
 #endif
+#if WITH_DEBUG_JTAG
+	jtag_dputc(c);
+#endif
 }
 
 int dgetc(char *c)
diff --git a/platform/msm_shared/include/jtag.h b/platform/msm_shared/include/jtag.h
new file mode 100644
index 0000000..e6476d7
--- /dev/null
+++ b/platform/msm_shared/include/jtag.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE 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.
+ */
+
+
+#ifndef _JTAG_H_
+#define _JTAG_H_
+
+void jtag_okay(const char *msg);
+void jtag_fail(const char *msg);
+void jtag_dputc(unsigned ch);
+void jtag_cmd_loop(void (*do_cmd)(const char *, unsigned, unsigned, unsigned));
+
+
+#endif /*__JTAG_H_ */
+
diff --git a/platform/msm_shared/nand.h b/platform/msm_shared/include/nand.h
similarity index 100%
rename from platform/msm_shared/nand.h
rename to platform/msm_shared/include/nand.h
diff --git a/platform/msm_shared/jtag.c b/platform/msm_shared/jtag.c
new file mode 100644
index 0000000..ca45404
--- /dev/null
+++ b/platform/msm_shared/jtag.c
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE 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 <jtag.h>
+
+#define STATUS_NOMSG 0
+#define STATUS_OKAY  1
+#define STATUS_FAIL  2
+#define STATUS_PRINT 3
+
+volatile unsigned _jtag_cmd = 0;
+volatile unsigned _jtag_msg = 0;
+unsigned char _jtag_cmd_buffer[128];
+unsigned char _jtag_msg_buffer[128];
+
+volatile unsigned _jtag_arg0 = 0;
+volatile unsigned _jtag_arg1 = 0;
+volatile unsigned _jtag_arg2 = 0;
+
+
+void jtag_hook(void);
+
+static void jtag_msg(unsigned status, const char *msg)
+{
+    unsigned char *out = _jtag_msg_buffer;
+    while((*out++ = *msg++) != 0) ;
+    _jtag_msg = status;
+    do {
+        jtag_hook();
+    } while(_jtag_msg != 0);
+}
+
+void jtag_okay(const char *msg)
+{
+    if(msg == 0) msg = "OKAY";
+    jtag_msg(STATUS_OKAY, msg);
+}
+
+void jtag_fail(const char *msg)
+{
+    if(msg == 0) msg = "FAIL";
+    jtag_msg(STATUS_FAIL, msg);
+}
+
+int jtag_cmd_pending()
+{
+    jtag_hook();
+    return (int) _jtag_cmd;
+}
+
+void jtag_cmd_loop(void (*do_cmd)(const char *, unsigned, unsigned, unsigned))
+{
+    unsigned n;
+    for(;;) {
+        if(jtag_cmd_pending()){
+            do_cmd((const char*) _jtag_cmd_buffer, _jtag_arg0, _jtag_arg1, _jtag_arg2);
+            for(n = 0; n < 256; n++) _jtag_cmd_buffer[n] = 0;
+            _jtag_arg0 = 0;
+            _jtag_arg1 = 0;
+            _jtag_arg2 = 0;
+            _jtag_cmd = 0;
+        }
+    }
+}
+
+static char jtag_putc_buffer[128];
+static unsigned jtag_putc_count = 0;
+
+static void jtag_push_buffer(void)
+{
+    jtag_putc_buffer[jtag_putc_count] = 0;
+    jtag_putc_count = 0;
+    jtag_msg(STATUS_PRINT, jtag_putc_buffer);
+}
+
+void jtag_dputc(unsigned c)
+{
+    if((c < 32) || (c > 127)) {
+        if(c == '\n') {
+            jtag_push_buffer();
+        }
+        return;
+    }
+
+    jtag_putc_buffer[jtag_putc_count++] = c;
+    if(jtag_putc_count == 127) {
+        jtag_push_buffer();
+    }
+}
+
diff --git a/platform/msm_shared/jtag_hook.S b/platform/msm_shared/jtag_hook.S
new file mode 100644
index 0000000..df41bbb
--- /dev/null
+++ b/platform/msm_shared/jtag_hook.S
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * 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.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE 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.
+ */
+
+.global jtag_hook, func
+
+jtag_hook:
+	bx lr
+
diff --git a/platform/msm_shared/nand.c b/platform/msm_shared/nand.c
index 691c6b4..49bec32 100755
--- a/platform/msm_shared/nand.c
+++ b/platform/msm_shared/nand.c
@@ -33,9 +33,9 @@
 #include <string.h>
 #include <dev/flash.h>
 #include <lib/ptable.h>
+#include <nand.h>
 
 #include "dmov.h"
-#include "nand.h"
 
 #define VERBOSE 0
 #define VERIFY_WRITE 0
diff --git a/platform/msm_shared/rules.mk b/platform/msm_shared/rules.mk
index c0de0b4..d856dfa 100644
--- a/platform/msm_shared/rules.mk
+++ b/platform/msm_shared/rules.mk
@@ -1,5 +1,8 @@
 LOCAL_DIR := $(GET_LOCAL_DIR)
 
+INCLUDES += \
+			-I$(LOCAL_DIR)/include
+
 OBJS += \
 	$(LOCAL_DIR)/uart.o \
 	$(LOCAL_DIR)/timer.o \
@@ -8,5 +11,7 @@
 	$(LOCAL_DIR)/smem.o \
 	$(LOCAL_DIR)/smem_ptable.o \
 	$(LOCAL_DIR)/hsusb.o \
+	$(LOCAL_DIR)/jtag_hook.o \
+	$(LOCAL_DIR)/jtag.o \
 	$(LOCAL_DIR)/nand.o
 
diff --git a/project/msm7627_ffa.mk b/project/msm7627_ffa.mk
old mode 100755
new mode 100644
diff --git a/project/msm7627_ffa_nandwrite.mk b/project/msm7627_ffa_nandwrite.mk
new file mode 100644
index 0000000..64cfa3a
--- /dev/null
+++ b/project/msm7627_ffa_nandwrite.mk
@@ -0,0 +1,13 @@
+# top level project rules for the msm7627_ffa_nandwrite project
+#
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+TARGET := msm7627_ffa
+
+MODULES += app/nandwrite
+
+DEFINES += WITH_DEBUG_JTAG=1
+#DEFINES += WITH_DEBUG_DCC=1
+#DEFINES += WITH_DEBUG_UART=1
+#DEFINES += WITH_DEBUG_FBCON=1
+
diff --git a/project/msm7627_surf.mk b/project/msm7627_surf.mk
old mode 100755
new mode 100644
diff --git a/project/msm7627_surf_nandwrite.mk b/project/msm7627_surf_nandwrite.mk
new file mode 100644
index 0000000..98d365a
--- /dev/null
+++ b/project/msm7627_surf_nandwrite.mk
@@ -0,0 +1,13 @@
+# top level project rules for the msm7627_surf_nandwrite project
+#
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+TARGET := msm7627_surf
+
+MODULES += app/nandwrite
+
+DEFINES += WITH_DEBUG_JTAG=1
+#DEFINES += WITH_DEBUG_DCC=1
+#DEFINES += WITH_DEBUG_UART=1
+#DEFINES += WITH_DEBUG_FBCON=1
+
diff --git a/project/msm7630_surf.mk b/project/msm7630_surf.mk
old mode 100755
new mode 100644
diff --git a/project/msm7630_surf_nandwrite.mk b/project/msm7630_surf_nandwrite.mk
new file mode 100644
index 0000000..bf9e5e1
--- /dev/null
+++ b/project/msm7630_surf_nandwrite.mk
@@ -0,0 +1,13 @@
+# top level project rules for the msm7630_surf_nandwrite project
+#
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+TARGET := msm7630_surf
+
+MODULES += app/nandwrite
+
+DEFINES += WITH_DEBUG_JTAG=1
+#DEFINES += WITH_DEBUG_DCC=1
+#DEFINES += WITH_DEBUG_UART=1
+#DEFINES += WITH_DEBUG_FBCON=1
+
diff --git a/project/qsd8250_ffa.mk b/project/qsd8250_ffa.mk
old mode 100755
new mode 100644
diff --git a/project/qsd8250_ffa_nandwrite.mk b/project/qsd8250_ffa_nandwrite.mk
new file mode 100644
index 0000000..ef1bf0e
--- /dev/null
+++ b/project/qsd8250_ffa_nandwrite.mk
@@ -0,0 +1,13 @@
+# top level project rules for the qsd8250_ffa_nandwrite project
+#
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+TARGET := qsd8250_ffa
+
+MODULES += app/nandwrite
+
+DEFINES += WITH_DEBUG_JTAG=1
+#DEFINES += WITH_DEBUG_DCC=1
+#DEFINES += WITH_DEBUG_UART=1
+#DEFINES += WITH_DEBUG_FBCON=1
+
diff --git a/project/qsd8250_surf.mk b/project/qsd8250_surf.mk
old mode 100755
new mode 100644
diff --git a/project/qsd8250_surf_nandwrite.mk b/project/qsd8250_surf_nandwrite.mk
new file mode 100644
index 0000000..6b362bb
--- /dev/null
+++ b/project/qsd8250_surf_nandwrite.mk
@@ -0,0 +1,13 @@
+# top level project rules for the qsd8250_surf_nandwrite project
+#
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+TARGET := qsd8250_surf
+
+MODULES += app/nandwrite
+
+DEFINES += WITH_DEBUG_JTAG=1
+#DEFINES += WITH_DEBUG_DCC=1
+#DEFINES += WITH_DEBUG_UART=1
+#DEFINES += WITH_DEBUG_FBCON=1
+