Index: tt-loader/2nd/main.c
===================================================================
--- tt-loader/2nd/main.c	(revision 11)
+++ tt-loader/2nd/main.c	(revision 12)
@@ -4,4 +4,8 @@
  * Copyright (C) 2008 Guillaume Bougard <gbougard@pkg.fr>
  * Copyright (C) 2005 Luis Recuerda <lrec@helios.homeip.net>
+ *
+ * ARM kernel loader. Adapt from qemu-neo1973
+ * Copyright (c) 2006-2007 CodeSourcery.
+ * Written by Paul Brook
  *
  * This program is free software; you can redistribute it and/or
@@ -24,14 +28,16 @@
 #include "usb.h"
 
-#define _LSR_   ((volatile u8 *) 0xfffb0014)
-#define _THR_   ((volatile u8 *) 0xfffb0000)
-#define _RHR_   ((volatile u8 *) 0xfffb0000)
-
-#define MEM_READ_SIZE	32
+extern u32 crc32( u32 crc, char* buf, u32 len, crc_cb_fnc_t* cb);
+
+/*
+ * Length of memory dump done by 2nd.bin, must be a multiple of DUMP_LINE_SIZE from host main.c
+ * Must also be sufficient to output lines of text, let's say 128 bytes
+ */
+#define MEM_READ_SIZE	128*32
+#define CHUNK_SIZE		8192
 
 static char *usb_hdr = "TIS" ;
-static char usb_outbuffer[64];
-
-// limited strcpy
+static char usb_outbuffer[MEM_READ_SIZE+4];
+
 static u32 strcpy ( char *dest, char *src )
 {
@@ -39,5 +45,5 @@
 	char *tmp = dest, *s = src;
 	
-	while ( ++count < 256 && *s != '\0' )
+	while ( ++count < MEM_READ_SIZE && *s != '\0' )
 		*tmp++ = *s++ ;
 	
@@ -45,4 +51,16 @@
 	
 	return count ;
+}
+
+static u32 strlen ( char *src )
+{
+	u32 len = 0 ;
+	
+	while ( *src != '\0' ) {
+		src++ ;
+		len ++ ;
+	}
+	
+	return len ;
 }
 
@@ -56,70 +74,4 @@
 	
 	return count ;
-}
-
-static void bsend (char ch)
-{
-	while (!((*_LSR_) & 0x20));
-	*_THR_= ch;
-}
-
-static inline int check_serial (void) { return (*_LSR_) & 0x01; }
-
-static char brecv (void)
-{
-	while (!((*_LSR_) & 0x01));
-	return *_RHR_;
-}
-
-char crecv (void)
-{
-	return brecv ();
-}
-
-void csend (char ch)
-{
-	if (ch == '\n')
-		bsend ('\r');
-	bsend (ch);
-}
-
-void tsend (const char *text)
-{
-	while (*text)
-		csend (*text++);
-}
-
-void xsend (unsigned value)
-{
-	if (value < 10)
-		bsend (value + '0');
-	else
-		bsend (value + ('A'-10));
-}
-
-void x8send (u8 value)
-{
-	xsend (value >> 4);
-	xsend (value & 0xf);
-}
-
-void x16send (u16 value)
-{
-	int	i;
-	for (i= 0; i < 4; ++i)
-	{
-		xsend (value >> 12);
-		value<<= 4;
-	}
-}
-
-void x32send (u32 value)
-{
-	int	i;
-	for (i= 0; i < 8; ++i)
-	{
-		xsend (value >> 28);
-		value<<= 4;
-	}
 }
 
@@ -181,7 +133,90 @@
 }
 
+// stl_raw is qemu related
+#define stl_raw(x,v) *(u32 *)(x) = v
+static void set_kernel_args(u32 ram_size, int initrd_size, const char *kernel_cmdline, u32 ram_start)
+{
+    u32 *p;
+	
+	// Set pointer to kernel params
+    p = (u32 *)(CFG_PARAMADDR);
+	
+    /* ATAG_CORE */
+    stl_raw(p++, 5);
+    stl_raw(p++, 0x54410001);
+    stl_raw(p++, 0); // 0 = ro ; 1= rw
+    stl_raw(p++, 0x1000); // Page size
+    stl_raw(p++, 0); // root device overrided by cmdline
+    /* ATAG_MEM */
+    stl_raw(p++, 4);
+    stl_raw(p++, 0x54410002);
+    stl_raw(p++, ram_size);
+    stl_raw(p++, ram_start);
+    if (initrd_size) {
+        /* ATAG_INITRD2 */
+        stl_raw(p++, 4);
+        stl_raw(p++, 0x54420005);
+        stl_raw(p++, ram_start + INITRD_LOAD_ADDR);
+        stl_raw(p++, initrd_size);
+    }
+    if (kernel_cmdline && *kernel_cmdline) {
+        /* ATAG_CMDLINE */
+        int cmdline_size;
+
+        cmdline_size = strlen((char *)kernel_cmdline);
+        memcpy ((char *)p + 2, (char *)kernel_cmdline, cmdline_size + 1);
+        cmdline_size = (cmdline_size >> 2) + 1;
+        stl_raw(p++, cmdline_size + 2);
+        stl_raw(p++, 0x54410009);
+        p += cmdline_size;
+    }
+    /* ATAG_END */
+    stl_raw(p++, 0);
+    stl_raw(p++, 0);
+}
+
+#define C1_DC		(1<<2)		/* dcache off/on */
+#define C1_IC		(1<<12)		/* icache off/on */
+
+void cleanup_before_linux (void)
+{
+	/*
+	 * this function is called just before we call linux
+	 * it prepares the processor for linux
+	 *
+	 * we turn off caches etc ...
+	 */
+
+	unsigned long i;
+
+	/* turn off I/D-cache */
+	asm ("mrc p15, 0, %0, c1, c0, 0":"=r" (i));
+	i &= ~(C1_DC | C1_IC);
+	asm ("mcr p15, 0, %0, c1, c0, 0": :"r" (i));
+
+	/* flush I/D-cache */
+	i = 0;
+	asm ("mcr p15, 0, %0, c7, c7, 0": :"r" (i));
+}
+
+void usb_crc32_pos( u32 pos, u32 total, u32 crc )
+{
+	u32 len = strcpy(usb_outbuffer,usb_hdr);
+	usb_outbuffer[len-1] = 'x' ;
+	(*(u32 *)(usb_outbuffer+len)) = pos ;
+	len += sizeof(u32);
+	(*(u32 *)(usb_outbuffer+len)) = total ;
+	len += sizeof(u32);
+	(*(u32 *)(usb_outbuffer+len)) = crc ;
+	len += sizeof(u32);
+	usb_send ( usb_outbuffer, len );
+	while (!usb_sent ());
+}
+
 u32 _main (void)
 {	
+	u32 params = CFG_PARAMADDR ;
 	u32 address = CFG_LOADADDR ;
+	u32 crc = 0 ;
 	usb_msg ('m', "In omap plateform");
 	
@@ -205,5 +240,5 @@
 		u32 total, cmd, index = 0 ;
 		
-		u8 usb_inbuffer[64];
+		u8 usb_inbuffer[CHUNK_SIZE];
 		usb_recv (usb_inbuffer, sizeof (usb_inbuffer));
 		while (!usb_rcvd ());
@@ -239,4 +274,52 @@
 			
 		} else
+		if ((char)cmd == 'C') // set crc32 value for next download
+		{
+			crc = *(u32 *) &usb_inbuffer[index];
+			usb_code ('i', crc);
+			
+		} else
+		if ((char)cmd == 'm') // Download size from memory
+		{
+			total= *(u32 *) &usb_inbuffer[index];
+			u32 size = 0;
+			
+			usb_code ('I', total);
+			
+			// Check crc32 if it has been set
+			if (total && crc)
+			{
+				crc_cb_fnc_t* cb = NULL ;
+				if (total>1024*1024)
+					cb = &usb_crc32_pos ;
+				u32 foundcrc = crc32(0, (char *)address,total,cb) ;
+				usb_code ('C', foundcrc);
+				// Skip download on crc match
+				if (foundcrc == crc)
+					total = 0 ;
+			}
+			
+			if (total == 0)
+				usb_blk ('D', (char *)address, 0);
+			
+			while (size < total)
+			{
+				u32 bs = MEM_READ_SIZE ;
+				if ( total - size < MEM_READ_SIZE)
+					bs = total - size ;
+				usb_blk ('D', (char *)address, bs);
+				address += bs ;
+				size += bs ;
+			}
+			
+			// reset crc for next download anyway
+			crc = 0 ;
+			
+		} else
+		if ((char)cmd == 'e') // End, just loop
+		{
+			usb_reset();
+			
+		} else
 		if ((char)cmd == 'f') // load file ACK
 		{
@@ -256,7 +339,28 @@
 			
 		} else
+		if ((char)cmd == 'p') // do a poke on address
+		{
+			u32 size = memcpy( (char *) address, (char *) &usb_inbuffer[index], 4);
+			address += size ;
+			usb_code ('i', address );
+			
+		} else
+		if ((char)cmd == 'P') // do a peek on address
+		{
+			usb_code ('i', *(u32 *) address );
+			address += 4 ;
+			
+		} else
 		if ((char)cmd == 'c') // call command
 		{
-			index = ((u32_fnc_t *)address)();
+			__asm__ __volatile__ (
+						"mov r0, #0	;"
+						"mov r1, #0	;"
+						"mov r2, #0	;"
+						"ldr r3, %1	;"
+						"blx r3		;"
+						"mov %0, r0	;"
+						: "=r"(index) : "m"(address) : "r0", "r1", "r2", "r3"
+						);
 			usb_code ('i', index);
 			
@@ -265,7 +369,42 @@
 		{
 			usb_code ('b', address );
-			// Just return the address where is store the branch to do
-			u32 bootaddress = (u32) &address ; // Convert/cast address just to avoid gcc warning
-			return bootaddress ;
+			usb_reset();
+			__asm__ __volatile__ (
+						"ldr r0, %0	;"
+						"mov pc, r0	;"
+						: : "m"(address)
+						);
+			
+		} else
+		if ((char)cmd == 'k') // boot kernel command
+		{
+			int machid =  * (int *) &usb_inbuffer[index];
+			usb_code ('I', machid);
+			set_kernel_args( RAM_SIZE, INITRD_SIZE, KERNEL_CMDLINE, CFG_BASESDRAM );
+			usb_code ('b', address );
+			usb_reset();
+			cleanup_before_linux();
+			__asm__ __volatile__ (
+						"mov r0, #0	;"
+						"ldr r1, %0	;"
+						"ldr r2, %1	;"
+						"mov r3, #0	;"
+						"mov r4, #0	;"
+						"ldr r5, %2	;"
+						"mov pc, r5	;"
+						: : "m" (machid), "m"(params), "m"(address)
+						);
+		} else
+		if ((char)cmd == 'S') // Stack command
+		{
+			__asm__ __volatile__ (
+						"ldr fp, %0	;"
+						"sub fp, #4 ;"
+						"mov sp, fp	;"
+						"sub sp, #256 ;"
+						: : "m"(address)
+						);
+			usb_code ('i', address);
+			
 		}
 	}
