Index: tt-loader/main.c
===================================================================
--- tt-loader/main.c	(revision 11)
+++ tt-loader/main.c	(revision 12)
@@ -33,4 +33,6 @@
 #include <errno.h>
 
+extern unsigned long crc32( unsigned long crc, char* buf, unsigned int len);
+
 /*
  * Here Vendor/Product detected for the target device
@@ -43,23 +45,29 @@
 
 /*
- * Length of memory dump done by 2nd.bin
+ * Length of memory dump lines
  */
-#define MEM_READ_SIZE		32
+#define DUMP_LINE_SIZE		32
 
 struct mem_file {
 	void *content ;
 	int size ;
+	char *filename ;
 };
 
+/*
+ * MAX_SIZE is the maximum size of the 2nd.bin program
+ */
 #define	MAX_SIZE	65536
+#define CHUNK_SIZE	8192
 static char	buffer[MAX_SIZE + 128];
 static int	buffsize = 0;
-static struct mem_file cmdfile = { NULL, 0 };
+static struct mem_file cmdfile = { NULL, 0, NULL };
 static double btime ;
 static double ticks ;
 
 #define log1(X)		fprintf(stderr, "%9.3f: "X,((double)times(NULL)-btime)/ticks)
-#define log2(X,Y) 	fprintf(stderr, "%9.3f: "X,((double)times(NULL)-btime)/ticks,Y)
-#define log3(X,Y,Z)	fprintf(stderr, "%9.3f: "X,((double)times(NULL)-btime)/ticks,Y,Z)
+#define log2(X,Y) 	fprintf(stderr, "%9.3f: "X,((double)times(NULL)-btime)/ticks,(Y))
+#define log3(X,Y,Z)	fprintf(stderr, "%9.3f: "X,((double)times(NULL)-btime)/ticks,(Y),(Z))
+#define log4(W,X,Y,Z)	fprintf(stderr, "%9.3f: "W,((double)times(NULL)-btime)/ticks,(X),(Y),(Z))
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
@@ -71,5 +79,5 @@
 #endif
 
-static inline unsigned do_div (unsigned v, unsigned d)
+static inline unsigned roundup (unsigned v, unsigned d)
 {
 	v+= --d;
@@ -92,5 +100,4 @@
 {
 	int	res, len = 5 ;
-	char buffer[64];
 	
 	buffer[0]= 'T';
@@ -101,4 +108,6 @@
 	if (src!=NULL)
 		len += stringcopy (&buffer[4], src, 60);
+	else
+		len = 4 ;
 	buffer[63]= '\0' ; // truncate anyway
 	
@@ -106,5 +115,5 @@
 	if (res < len)
 	{
-		fprintf (stderr, "Error in usb_bulk_write during send_char: %d/4\n", res);
+		log2("Error in usb_bulk_write during send_char: %d/4\n", res);
 		return 0;
 	}
@@ -113,18 +122,17 @@
 }
 
-static int send_binsize (usb_dev_handle *handle, int sz)
+static int send_binsize (usb_dev_handle *handle, int size)
 {
 	int	res;
-	char	buffer[8];
 	buffer[0]= 'T';
 	buffer[1]= 'I';
 	buffer[2]= 'S';
 	buffer[3]= 's';
-	*(u_int32_t *) &buffer[4]= cpu_to_le32 ((sz>4096)?4096:sz);
+	*(u_int32_t *) &buffer[4]= cpu_to_le32 ( size < CHUNK_SIZE ? size : CHUNK_SIZE );
 	
 	res= usb_bulk_write (handle, OUT_EP, buffer, 8, 1000);
 	if (res < 8)
 	{
-		fprintf (stderr, "Error in usb_bulk_write during send_binsize: %d/8\n", res);
+		log2("Error in usb_bulk_write during send_binsize: %d/8\n", res);
 		return 0;
 	}
@@ -133,18 +141,17 @@
 }
 
-static int send_address (usb_dev_handle *handle, int addr)
+static int send_word (usb_dev_handle *handle, const char req, int word)
 {
 	int	res;
-	char	buffer[8];
 	buffer[0]= 'T';
 	buffer[1]= 'I';
 	buffer[2]= 'S';
-	buffer[3]= 'a';
-	*(u_int32_t *) &buffer[4]= cpu_to_le32 (addr);
+	buffer[3]= req;
+	*(u_int32_t *) &buffer[4]= cpu_to_le32 (word);
 	
 	res= usb_bulk_write (handle, OUT_EP, buffer, 8, 1000);
 	if (res < 8)
 	{
-		fprintf (stderr, "Error in usb_bulk_write: %d/8\n", res);
+		log2("Error in usb_bulk_write: %d/8\n", res);
 		return 0;
 	}
@@ -153,12 +160,31 @@
 }
 
-static struct mem_file readfile( const char *filename )
+static int send_poke (usb_dev_handle *handle, int poke)
 {
-	struct mem_file toread = { NULL, 0 };
+	int	res;
+	buffer[0]= 'T';
+	buffer[1]= 'I';
+	buffer[2]= 'S';
+	buffer[3]= 'p';
+	*(u_int32_t *) &buffer[4]= cpu_to_le32 (poke);
+	
+	res= usb_bulk_write (handle, OUT_EP, buffer, 8, 1000);
+	if (res < 8)
+	{
+		log2("Error in usb_bulk_write: %d/8\n", res);
+		return 0;
+	}
+	
+	return 1;
+}
+
+static struct mem_file readfile( char *filename )
+{
+	struct mem_file toread = { NULL, 0, NULL };
 	int fd= open (filename, O_RDONLY);
 	
 	if (fd < 0)
 	{
-		fprintf (stderr, "Error opening %s file\n", filename);
+		log3("Error opening %s file (err=%d)\n", filename, errno);
 		return toread;
 	}
@@ -167,13 +193,13 @@
 	if (toread.size < 0  ||  lseek (fd, 0, SEEK_SET) < 0)
 	{
-		fprintf (stderr, "Error with lseek other %s file\n", filename);
+		log3("Error with lseek other %s file (err=%d)\n", filename, errno);
 		close (fd);
 		return toread;
 	}
 	
-	toread.content= malloc (do_div (toread.size, 4));
+	toread.content= malloc (roundup (toread.size, 4));
 	if (toread.content == NULL)
 	{
-		fprintf (stderr, "Out of memory requesting %d bytes\n", toread.size);
+		log2("Out of memory requesting %d bytes\n", toread.size);
 		close (fd);
 		return toread;
@@ -182,5 +208,5 @@
 	if ((toread.size= read (fd, toread.content, toread.size)) < 0)
 	{
-		fprintf (stderr, "Error reading %s file\n", filename);
+		log3("Error reading %s file (err=%d)\n", filename, errno);
 		close (fd);
 		return toread;
@@ -190,4 +216,26 @@
 	
 	return toread ;
+}
+
+static int savefile( char *filename, struct mem_file buffer )
+{
+	int fd= creat (filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH );
+	
+	if (fd < 0)
+	{
+		log3("Error opening %s file for writing (err=%d)\n", filename, errno);
+		return 1;
+	}
+	
+	if (buffer.size != write (fd, buffer.content, buffer.size))
+	{
+		log3("Error writing %s file (err=%d)\n", filename, errno);
+		close (fd);
+		return 1;
+	}
+	
+	close (fd);
+	
+	return 0 ;
 }
 
@@ -218,7 +266,6 @@
 	else
 	{
-		char	inbuff[256];
-		int	insize= usb_bulk_read (handle, IN_EP,
-			inbuff, sizeof (inbuff), 5000);
+		char inbuff[MAX_SIZE + 128];
+		int	insize= usb_bulk_read (handle, IN_EP, inbuff, sizeof (inbuff), 1000);
 		
 		if (insize < 48)
@@ -235,13 +282,14 @@
 			while (size > 0)
 			{
-				int	outsize= usb_bulk_write (handle, OUT_EP, p, 64, 5000);
-				if (outsize < 64)
+				int chunksize = size < CHUNK_SIZE ? size : CHUNK_SIZE ;
+				int	outsize = usb_bulk_write (handle, OUT_EP, p, chunksize, 1000);
+				if (outsize < chunksize)
 				{
-					log2("Error in usb_bulk_write: %d/64\n", outsize);
+					log3("Error in usb_bulk_write: %d/%d\n", outsize,chunksize);
 					break;
 				}
 
-				p+= 64;
-				size-= 64;
+				p+= chunksize;
+				size-= chunksize;
 			}
 			
@@ -249,13 +297,12 @@
 			{
 				log2("Sent %d bytes to OMAP\n",buffsize);
-				usb_bulk_write (handle, OUT_EP, buffer, 0, 1000);
 				
 				p= NULL;
 				size= 0;
 				
-				int	res=1, sz= 0, loop= 1, cmd=0 ;
+				int	res=1, sz= 0, loop= 1, cmd=0, ftag=0, finfo=0, fline=0 ;
 				int vendor, device, info ;
 				char *cmdp = cmdfile.content ;
-				struct mem_file current = { NULL, 0 };
+				struct mem_file current = { NULL, 0, NULL };
 				
 				err = 1 ; // Set error by default
@@ -265,5 +312,5 @@
 					if (loop<0) loop++ ; // Avoid infinite loop
 					
-					if (res>0)
+					if (res>0 || ftag)
 						res= usb_bulk_read (handle, IN_EP,
 							inbuff, sizeof (inbuff), 5000);
@@ -275,4 +322,61 @@
 					}
 					
+					if (ftag)
+					{
+						switch (ftag)
+						{
+							case 0xaaaa0001:
+								// A string should be in the buffer with length still set in finfo
+								if ( res != finfo)
+								{
+									log3("Error waiting string from flasher program: length = %d vs %d expected\n", res, finfo);
+									break ;
+								}
+								// Fix the string just in case
+								inbuff[res] = '\0' ;
+								if (fline)
+									fprintf(stderr, "%s",inbuff);
+								else
+									log2("Flasher: %s", inbuff);
+								if ( inbuff[res-1] == '\n' )
+									fline = 0 ;
+								else
+									fline ++ ;
+								break ;
+							case 0xaaaa0002:
+								finfo = le32_to_cpu (*(u_int32_t *) inbuff);
+								break ;
+							case 0xaaaa0003:
+								finfo = le32_to_cpu (*(u_int32_t *) inbuff);
+								log2("Flasher: NAND read = 0x%08X\n", finfo);
+								break ;
+							case 0xaaaa0004:
+								finfo = le32_to_cpu (*(u_int32_t *) inbuff);
+								log2("Flasher: NAND size = 0x%08X\n", finfo);
+								break ;
+							case 0xaaaa0005:
+								finfo = le32_to_cpu (*(u_int32_t *) inbuff);
+								log2("Flasher: Error code = %d\n", finfo);
+								err = - finfo ; // Will quit on error
+								break ;
+							case 0xaaaa0006:
+								finfo = le32_to_cpu (*(u_int32_t *) inbuff);
+								log2("Flasher: NAND remain = 0x%08X\n", finfo);
+								break ;
+						}
+						ftag = 0 ;
+						continue ;
+					}
+					
+					if (err<0)
+					{
+						err = -err ; // Keep err as positive number
+						if (send_cmd (handle,'e',NULL))
+							log1("Asking stop\n");
+						else
+							log1("Can't ask to stop\n");
+						break ; // Leave loop
+					}
+
 					if (res >= 4)
 					{
@@ -305,5 +409,5 @@
 								sz= le32_to_cpu (*(u_int32_t *) &inbuff[4]);
 								//log2("OMAP read %d\n", sz); 					// DEBUG
-								res= usb_bulk_write (handle, OUT_EP, p, sz, 5000);
+								res= usb_bulk_write (handle, OUT_EP, p, sz, 1000);
 								if (res < sz)
 								{
@@ -318,7 +422,19 @@
 								break;
 								
+							case 'x':
+								info = le32_to_cpu (*(u_int32_t *) &inbuff[4]);
+								u_int32_t len = le32_to_cpu (*(u_int32_t *) &inbuff[8]);
+								u_int32_t crc = le32_to_cpu (*(u_int32_t *) &inbuff[12]);
+								log4("OMAP crc32 computing: pos 0x%08x/0x%08x, current crc32 0x%08X\n",info,len,crc);
+								break;
+								
 							case 'A':
 								info = le32_to_cpu (*(u_int32_t *) &inbuff[4]);
 								log2("OMAP address info: 0x%08X\n", info);
+								break;
+								
+							case 'C':
+								info = le32_to_cpu (*(u_int32_t *) &inbuff[4]);
+								log2("OMAP crc32 found: 0x%08X\n", info);
 								break;
 								
@@ -343,19 +459,62 @@
 								break;
 								
+							case 'D':
+								if (!current.content)
+								{
+									log1("Can't dump memory without buffer\n");
+									loop = 0 ;
+									break ;
+								}
+								res -= 4 ;
+								if (res < 1)
+								{
+									log2("Received empty memory dump (res=%d)\n",res);
+									// reset buffer size anyway
+									current.size = 0 ;
+									res = 0 ; // Target is waiting a new command
+									
+								} else {
+									if (res > size)
+									{
+										log2("Can't dump %d bytes as buffer is full\n",res);
+										loop = 0 ;
+										break ;
+									}
+									memcpy(p,&inbuff[4],res);
+									p += res ;
+									size -= res ;
+								}
+								if ( size == 0 )
+								{
+									log2("%d bytes dumped to buffer\n",current.size);
+									res = 0 ; // Target is waiting a new command
+								}
+								break;
+								
 							case 'M':
-								loop=3 ;
-								log2("0x%08X | ",info);
-								while (++loop<36)
-									fprintf (stderr,"%02hhX", inbuff[loop]);
-								loop=3 ;
-								fprintf (stderr, " |");
-								while (++loop<36)
-									if ((int)inbuff[loop]>=0x20 && (int)inbuff[loop]<0x7f)
-										fprintf (stderr,"%c", inbuff[loop]);
-									else
-										fprintf (stderr,".");
-								fprintf (stderr,"|\n");
+								sz = 0 ;
+								res -= 4 ;
+								while (sz<res)
+								{
+									log2("0x%08X | ",info);
+									
+									loop=0 ;
+									while (++loop<=DUMP_LINE_SIZE)
+										fprintf (stderr,"%02hhX", inbuff[3+sz+loop]);
+									fprintf (stderr, " |");
+									
+									loop=0 ;
+									while (++loop<=DUMP_LINE_SIZE)
+										if ((int)inbuff[3+sz+loop]>=0x20 && (int)inbuff[3+sz+loop]<0x7f)
+											fprintf (stderr,"%c", inbuff[3+sz+loop]);
+										else
+											fprintf (stderr,".");
+									
+									fprintf (stderr,"|\n");
+									
+									info += DUMP_LINE_SIZE;
+									sz += DUMP_LINE_SIZE;
+								}
 								res = 0 ; // Target is waiting a new command
-								info += 32 ;
 								break;
 								
@@ -369,5 +528,5 @@
 								info = le32_to_cpu (*(u_int32_t *) &inbuff[4]);
 								log2("OMAP is booting at address: 0x%08X\n", info);
-								res = 0 ; // Target is waiting a new command
+								loop = err = res = 0 ; // Quit now
 								break;
 
@@ -376,8 +535,31 @@
 								break;
 							}
-						} else {
-							// Fix the string just in case
-							inbuff[res] = '\0' ;
-							log2("Got: %s\n", inbuff);
+						} else
+						{
+							info = le32_to_cpu (*(u_int32_t *) inbuff);
+							switch (info)
+							{
+								case 0xaaaa0001:
+								case 0xaaaa0002:
+								case 0xaaaa0003:
+								case 0xaaaa0004:
+								case 0xaaaa0005:
+								case 0xaaaa0006:
+									ftag = info ;
+									break ;
+								case 0xff555580:
+									log1("Flasher: Command finished\n");
+									break ;
+								default:
+									// Fix the string just in case
+									inbuff[res] = '\0' ;
+									// Strip final EOL in the string
+									if ( inbuff[res-1] == '\n' )
+										inbuff[res-1] = '\0' ;
+									if (res==4)
+										log3("Got: %s (0x%08X)\n", inbuff, info);
+									else
+										log2("Got: %s\n", inbuff);
+							}
 						}
 					}
@@ -397,4 +579,66 @@
 							switch (cmdp[cmd])
 							{
+							case 'D': // Save downloaded memory to a file still specified
+								if (current.filename == NULL)
+								{
+									log1("Can't save without a file specified with 'F' command\n");
+									loop = 0 ; // Just quit on error
+									break ;
+								}
+								if (current.content == NULL)
+								{
+									log2("Can't save without download buffer to file '%s'\n",current.filename);
+									loop = 0 ; // Just quit on error
+									break ;
+								}
+								if (current.size==0)
+									log2("Nothing to save, file %s is still up to date\n",current.filename);
+								else if (savefile(current.filename, current)==0)
+								{
+									info = crc32(0,current.content,current.size);
+									log3("Downloaded memory saved to file %s with CRC32=0x%08X\n",current.filename,info);
+								} else
+								{
+									log2("Can't save downloaded memory saved to file %s\n",current.filename);
+									// Just quit on error
+									loop = 0 ;
+								}
+								// Release memory
+								free((void *)current.content);
+								free((void *)current.filename);
+								current.content = NULL ;
+								current.filename = NULL ;
+								res -- ;
+								break;
+							case 'd': // Save downloaded memory to a file
+								cmd += 2 ;
+								if (cmd >= cmdfile.size || cmdp[cmd] == '\0')
+								{
+									log1("Bad 'd' format in command download\n");
+									break ;
+								}
+								if (current.content == NULL)
+								{
+									log2("Can't save without download buffer to file '%s'\n",&cmdp[cmd]);
+									loop = 0 ; // Just quit on error
+									break ;
+								}
+								if (current.size==0)
+									log2("Nothing to save, file %s is still up to date\n",&cmdp[cmd]);
+								else if (savefile(&cmdp[cmd], current)==0)
+								{
+									info = crc32(0,current.content,current.size);
+									log3("Downloaded memory saved to file %s with CRC32=0x%08X\n",&cmdp[cmd],info);
+								} else
+								{
+									log2("Can't save downloaded memory saved to file %s\n",&cmdp[cmd]);
+									// Just quit on error
+									loop = 0 ;
+								}
+								// Release memory
+								free((void *)current.content);
+								current.content = NULL ;
+								res -- ;
+								break;
 							case 'f': // Put a file to be uploaded in a memory buffer
 								cmd += 2 ;
@@ -407,9 +651,10 @@
 								if (current.content != NULL)
 									free((void *)current.content);
-								current = readfile((const char *)&cmdp[cmd]);
+								current = readfile(&cmdp[cmd]);
 								if (current.content == NULL)
 								{
 									log2("Can't read file '%s'\n",&cmdp[cmd]);
 									loop = 0 ; // Just quit on error
+									break ;
 								}
 								p = current.content ;
@@ -425,4 +670,67 @@
 								}
 								break;
+							case 'F': // Set a filename that would be loaded to check crc and saved
+								cmd += 2 ;
+								if (cmd >= cmdfile.size || cmdp[cmd] == '\0')
+								{
+									log1("Bad 'F' format in command file\n");
+									break ;
+								}
+								// Free previously used memory buffer
+								if (current.filename != NULL)
+									free((void *)current.filename);
+								info = strlen(&cmdp[cmd]);
+								if (info == 0)
+								{
+									log2("Can't read filename '%s' length\n",&cmdp[cmd]);
+									loop = 0 ; // Just quit on error
+									break ;
+								}
+								current.filename = malloc (++info);
+								if (current.filename == NULL)
+								{
+									log2("Out of memory requesting %d bytes\n", info);
+									loop = 0 ; // Just quit on error
+									break ;
+								}
+								if (info!=stringcopy(current.filename,&cmdp[cmd],info))
+								{
+									log2("Can't read filename '%s'\n",&cmdp[cmd]);
+									loop = 0 ; // Just quit on error
+									break ;
+								}
+								// still read next command
+								res -- ;
+								break;
+							case 'C': // Get a CRC32 and send it to target
+								cmd += 2 ;
+								if (sscanf((const char *)&cmdp[cmd], "%i", &info) != 1)
+								{
+									log2("Can't read crc32 '%s'\n",&cmdp[cmd]);
+									loop = 0 ; // Just quit on error
+									break ;
+								}
+								// compute the current CRC32 if a file is referenced and exists
+								if (!info && current.filename != NULL)
+								{
+									struct mem_file loader = readfile(current.filename);
+									if (loader.content == NULL)
+									{
+										log2("Can't read file '%s', will be initialized with target\n",current.filename);
+									} else
+									{
+										log3("Checking CRC32 of %d bytes from %s file\n",loader.size,current.filename);
+										info = crc32(0,loader.content,loader.size);
+										free(loader.content);
+									}
+								}
+								if (send_word (handle, 'C', info))
+									log2("Sending crc32 0x%02X\n", info);
+								else
+								{
+									log2("Can't send crc32 0x%02X\n", info);
+									loop = 0 ; // Just quit on error
+								}
+								break;
 							case 'a': // Get an address and send it to target
 								cmd += 2 ;
@@ -431,6 +739,7 @@
 									log2("Can't read address '%s'\n",&cmdp[cmd]);
 									loop = 0 ; // Just quit on error
-								}
-								if (send_address (handle, info))
+									break ;
+								}
+								if (send_word (handle, 'a', info))
 									log2("Sending address 0x%02X\n", info);
 								else
@@ -440,8 +749,53 @@
 								}
 								break;
+							case 'm': // Get a size in kB and ask a memory download
+								cmd += 2 ;
+								if (sscanf((const char *)&cmdp[cmd], "%i", &info) != 1)
+								{
+									log2("Can't read size '%s'\n",&cmdp[cmd]);
+									loop = 0 ; // Just quit on error
+									break ;
+								}
+								// size unit is kbytes
+								size = info * 1024 ;
+								// Free previously used memory buffer
+								if (current.content != NULL)
+									free((void *)current.content);
+								current.size = size ;
+								p = malloc (roundup (size, 4));
+								if ( p == NULL)
+								{
+									log2("Out of memory requesting %d bytes\n", roundup (size, 4));
+									loop=0 ;
+									break ;
+								}
+								current.content = p ;
+								if (!(send_word (handle,'m',size)))
+								{
+									log2("Can't ask to download %d bytes from memory\n",size);
+									loop = 0 ; // Just quit on error
+								} else
+									log2("Allocated %d kBytes to download memory\n",info);
+								break;
 							case 'M': // Ask memory dump
 								if (!(send_cmd (handle,'M',NULL)))
 								{
 									log1("Can't ask to read memory\n");
+									loop = 0 ; // Just quit on error
+								}
+								break;
+							case 'P': // Ask a peek onto the address
+								if (!(send_cmd (handle,'P',NULL)))
+								{
+									log1("Can't ask to peek memory\n");
+									loop = 0 ; // Just quit on error
+								}
+								break;
+							case 'S': // Ask to change stack to the last given address
+								if (send_cmd (handle,'S',NULL))
+									log1("Asking stack relocate\n");
+								else
+								{
+									log1("Can't ask to relocate stack\n");
 									loop = 0 ; // Just quit on error
 								}
@@ -457,7 +811,11 @@
 								break;
 							case 'b': // Boot the target by just branching to the last given address
-								if (send_cmd (handle,'b',NULL))
+								if (send_cmd (handle,'b',NULL)) {
 									log1("Asking boot\n");
-								else
+									// Quit now witout error set
+									loop = err = -10 ;
+									cmd = cmdfile.size ;
+									res = 1 ;
+								} else
 								{
 									log1("Can't ask to boot\n");
@@ -467,4 +825,8 @@
 							case 'e': // End of commands
 								log1("Commands read and sent\n");
+								if (send_cmd (handle,'e',NULL))
+									log1("Asking stop\n");
+								else
+									log1("Can't ask to stop\n");
 								// Quit now witout error set
 								loop = err = 0 ;
@@ -472,5 +834,43 @@
 								res = 1 ;
 								break ;
+							case 'k': // Boot a linux kernel to the last given address
+								cmd += 2 ;
+								if (sscanf((const char *)&cmdp[cmd], "%i", &info) != 1)
+								{
+									log2("Can't read mach id '%s'\n",&cmdp[cmd]);
+									loop = 0 ; // Just quit on error
+									break ;
+								}
+								if (send_word (handle,'k', info)) {
+									log1("Asking kernel to boot\n");
+									// Quit now witout error set
+									loop = err = -10 ;
+									cmd = cmdfile.size ;
+									res = 1 ;
+								} else
+								{
+									log1("Can't ask to boot a kernel\n");
+									loop = 0 ; // Just quit on error
+								}
+								break;
+							case 'p': // poke
+								cmd += 2 ;
+								if (sscanf((const char *)&cmdp[cmd], "%i", &info) != 1)
+								{
+									log2("Can't read value to poke '%s'\n",&cmdp[cmd]);
+									loop = 0 ; // Just quit on error
+									break ;
+								}
+								if (send_poke (handle, info))
+									log2("Sending poke 0x%08X\n", info);
+								else
+								{
+									log2("Can't send poke 0x%08X\n", info);
+									loop = 0 ; // Just quit on error
+								}
+								break;
 							case '#':
+								cmd += 2 ;
+								log2("%s\n", &cmdp[cmd]);
 							case '/':
 								// skip comments
@@ -490,4 +890,9 @@
 					}
 				}
+				// Free used memory buffer
+				if (current.content != NULL)
+					free((void *)current.content);
+				if (current.filename != NULL)
+					free((void *)current.filename);
 			}
 		}
@@ -495,7 +900,9 @@
 	
 	if (err>=0) {
+		log1("Releasing usb interface\n");
 		err = usb_release_interface (handle, 0);
 		if (err < 0)
 			log2("Error in usb_release_interface (%d)\n",err);
+		log1("Released usb interface\n");
 	}
 	
@@ -551,5 +958,5 @@
 	}
 	
-	size= do_div (size, 4);
+	size= roundup (size, 4);
 	
 	p[0x21]= cpu_to_le32 (size - 0x40);
@@ -562,5 +969,5 @@
 	p[0x12]= cpu_to_le32 (size);
 	
-	buffsize= 128 + do_div (size, 4);
+	buffsize= 128 + roundup (size, 4);
 	
 	cmdfile = readfile(argv[ARG_CMDFILE]);
@@ -575,4 +982,13 @@
 			if (cmdbuffer[i] == '\n' || cmdbuffer[i] == '\r' || cmdbuffer[i] == ';')
 				cmdbuffer[i] = '\0' ;
+			// Skip comment line
+			if (cmdbuffer[i] == '/') {
+				while ( i++ < cmdfile.size ) {
+					if (cmdbuffer[i] == '\n' || cmdbuffer[i] == '\r') {
+						i-- ;
+						break ;
+					}
+				}
+			}
 		} while ( i++ < cmdfile.size );
 	}
