mch_flasher.c 15.9 KB
Newer Older
1 2 3 4
/* V3 Flasher - temporary version */

#include <stdio.h>
#include <stdlib.h>
5
#include <unistd.h>
6 7
#include <stdarg.h>
#include <string.h>
8
#include <errno.h>
9
#include <getopt.h>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

#include <libgen.h>

#include <inttypes.h>

#include "serial.h"
#include "applet.h"

#define BOARD_REV_V2 2
#define BOARD_REV_V3 3


#define ID_SAM9263 0x019607A2
#define ID_SAM9G45 0x819b05a2
#define SERIAL_TIMEOUT 10000000

#define MBOX_COMMAND 			0x4
#define MBOX_STATUS 			0x8
#define MBOX_COMTYPE			0xc
#define MBOX_TRACELEVEL			0x10
#define MBOX_EXTRAM_VDDMEM		0x14
#define MBOX_EXTRAM_RAMTYPE		0x18
#define MBOX_EXTRAM_BUSWIDTH	0x1c
#define MBOX_EXTRAM_DDRMODEL	0x20
#define MBOX_DATAFLASH_INDEX	0x14
#define MBOX_DATAFLASH_BUFFER_ADDR	0x10
#define MBOX_PROG_BUF_SIZE	0x

38 39 40 41 42
#define MBOX_MEM_BUF_ADDR 0xc
#define MBOX_MEM_BUF_SIZE 0x10
#define MBOX_MEM_BUF_OFFSET 0x14

#define MBOX_MEM_ERASE_TYPE 0xc
43 44
#define MBOX_MEM_ERASE_START 0x10
#define MBOX_MEM_ERASE_END   0x14
45

46 47 48 49

#define INTERNAL_SRAM_BUF 		0x300000
#define SDRAM_START 			0x70000000

50 51 52
#define DF_HWINFO_START			0x000000094800
#define DF_HWINFO_END			0x000000095040

53 54 55

#define PORT_SPEED 			115200

56 57 58 59
#define MEMTYPE_DDR 	0
#define MEMTYPE_DF 		1
#define MEMTYPE_NAND 	2

60 61 62 63
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
#endif

64 65 66 67 68 69 70 71
const char* memName[3] = { "DDR", "DataFlash", "NandFlash" };


typedef struct {
	char *fname;
	uint32_t offset;
} sndfile;

72
sndfile filearray[16];
73 74


75 76 77 78 79
//External variable from version.c
extern const char build_time[];
extern const char git_user[];
extern const char git_revision[];

80
char *program_path;
81
char *program_name;
82 83 84 85 86
int applet_silent_mode = 1;

unsigned int buffer_size = 0x3000;
unsigned int buffer_addr ;

87 88 89 90




91
int mem_write(int type, uint32_t offset, uint32_t buf_addr, uint32_t size);
92

93 94
int crc16(int value, int crcin)
{
95 96 97 98 99 100 101 102 103 104 105 106 107
	int k = (((crcin >> 8) ^ value) & 255) << 8;
	int crc = 0;
	int bits = 8;
	do
	{
		if (( crc ^ k ) & 0x8000)
			crc = (crc << 1) ^ 0x1021;
		else
			crc <<= 1;
		k <<= 1;
	}
	while (--bits);
	return ((crcin << 8) ^ crc);
108 109 110
}


111
int write_xmodem(int index, char *p)
112
{
113
	unsigned char data[133];
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	unsigned short crc=0;
	int i;
	data[0]=1;
	data[1]=index;
	data[2]=0xff-index;

	memcpy(data+3,p,128);
	for(i=3;i<131;i++){
		crc=crc16(data[i],crc);
	}
	data[131] = (crc>>8)&0xff;
	data[132] = (crc)&0xff;
	printf("XMDump: ");
	for(i=0;i<133;i++) printf("%02x ", data[i]);
	printf("\n\n");
	serial_write(data,133);
	return serial_read_byte();
131 132 133 134 135 136
}


void die(const char *fmt, ...)
{
	va_list ap;
137

138 139 140 141 142 143 144 145 146 147 148 149
	va_start(ap, fmt);
	fprintf(stderr, "Error: ");
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, "\n\n");
	va_end(ap);
	exit(-1);
}



void samba_write (uint32_t addr, uint32_t data, int size, int timeout)
{
150
	char tmpbuf[1024];
151 152 153 154 155 156
	uint32_t tstart;

	serial_purge();
	snprintf(tmpbuf, 1024, "%c%08x,%08x#", size==1?'O':(size==2?'H':'W'), addr, data);
	serial_write(tmpbuf, strlen(tmpbuf));

157
	tstart = sys_get_clock_usec();
158 159 160

	while(1)
	{
161 162 163 164 165
		if(serial_data_avail())
		{
			if(serial_read_byte() =='>') break;
		}
		if(sys_get_clock_usec() - tstart > timeout) die("tx timeout");
166 167
	}

168 169


170 171 172 173 174 175 176 177 178 179

}


uint32_t samba_read (uint32_t addr, int size, int timeout)
{
	char tmpbuf[1024],c=0;
	int i,xpos=-1;
	uint32_t rval;
	uint32_t tstart;
180

181 182
	serial_purge();
	snprintf(tmpbuf, 1024, "%c%08x,#", size==1?'o':(size==2?'h':'w'), addr);
183 184

	//	printf("--> cmd: '%s'\n", tmpbuf);
185 186 187

	serial_write(tmpbuf, strlen(tmpbuf));

188
	tstart = sys_get_clock_usec();
189

190 191
	for(i=0;i<1024 && c!='>';)
	{
192 193 194 195 196 197 198 199 200 201
		if(serial_data_avail())
		{
			tmpbuf[i]=c=serial_read_byte();
			//		fprintf(stderr,"%c", c);
			if(c=='x') xpos=i;
			i++;
		}

		if(sys_get_clock_usec()-tstart>timeout) die("rx timeout");

202
	}
203

204
	if(xpos<0) die("invalid response from samba");
205

206
	sscanf(tmpbuf, "%x", &rval);
207
	//	fprintf(stderr,"samba_read: %x\n", rval);
208
	return rval;
209 210
}

211 212 213 214 215 216
/**
 * filename: name of the file in the host
 * address: place to send to file on the device
 * offset: internal offset, can be use with size
 * size: can be 0
 */
217 218
static int samba_send_file(const char *filename, uint32_t address, uint32_t offset, uint32_t size, int quiet)
{
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	FILE *f;
	unsigned char *buf;
	int boffset = 0;
	char tmp[4097];

	//    printf("SendFile: %s\n", filename);

	f = fopen(filename, "rb");
	if(!f) die("file open error: '%s'",filename);


	if(!size)
	{
		fseek(f, 0, SEEK_END);
		size = ftell(f);
		offset = 0;
		rewind(f);
	} else
		fseek(f, offset, SEEK_SET);

	if(!quiet) printf("size 0x%x (%dKb)\n",size,size/1024);

	buf = malloc(size+1);
	if(!buf) die("malloc failed");

	fread(buf, 1, size, f);
	fclose(f);

	//    printf("Send: %s\n", filename);

	while(size > 0)
	{
		int tosend = size > 1024 ? 1024 : size;
		//      printf("Sending %d bytes\n", tosend);

		snprintf(tmp, 128, "S%08x,%x#", address, tosend);
		serial_write(tmp, strlen(tmp));
		memcpy(tmp, buf + boffset, tosend);
		tmp[tosend]='\n';
		serial_write(tmp, tosend+1);

		size -= tosend;
		boffset +=tosend;
		address += tosend;

		while(1)
		{
			if(serial_data_avail())
				if(serial_read_byte() == '>')
					break;
		}
	}

	free(buf);

	return boffset;
275 276 277 278 279 280 281 282

}

static void samba_load_applet(char *applet_name, uint32_t address)
{
	char namebuf[1024];

	printf("loading applet %s at 0x%08x\n", applet_name, address);
283

284 285 286 287 288 289 290
	snprintf(namebuf, 1024, "%s/samba_applets/%s.bin", program_path, applet_name);
	samba_send_file(namebuf, address, 0, 0, 1);
}


static void mbox_write(uint32_t base, uint32_t offset, uint32_t value)
{
291
	samba_write(base+offset, value, 4, SERIAL_TIMEOUT);
292
}
293 294 295 296 297 298 299 300 301 302

void samba_run(uint32_t addr, int timeout)
{
	char tmpbuf[1024],c;
	uint32_t tstart,t1;

	serial_purge();
	snprintf(tmpbuf, 1024, "G%08x#",  addr);

	serial_write(tmpbuf, strlen(tmpbuf));
303

304
	t1 = tstart = sys_get_clock_usec();
305

306 307
	while((timeout && (sys_get_clock_usec()- tstart < timeout)) || !timeout)
	{
308 309 310 311 312 313 314
		if(serial_data_avail()) {
			c = serial_read_byte();
			if(c == '>') break;
			if(!applet_silent_mode )fprintf(stderr,"%c", c);
			t1 = sys_get_clock_usec();
		}
		else if(sys_get_clock_usec() - t1 > 10000)
315
		{
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
			//	fprintf(stderr,"Phase1\n");
			//		serial_write_byte('\n');
			serial_write_byte(0x0a);
			serial_write_byte(0x0d);
			serial_write_byte('T');
			serial_write_byte('#');
			serial_write_byte(0x0a);
			serial_write_byte(0x0d);
			usleep(10000);
			while(serial_data_avail())
			{
				c=serial_read_byte();
				if( c == '>')
					return;
				else if(!applet_silent_mode)
					fprintf(stderr,"%c", c);
			}
333
		}
334 335
		usleep(1000);
		//TODO: Improve data available for DDR load
336 337 338 339 340 341 342
	}
}



int samba_connect(int board_rev)
{
343
	char handshake[] = {0x80, 0x80, 0x23};
344 345 346 347 348

	serial_write(handshake,3);
	sys_delay(100);

	uint32_t id = samba_read(0xffffee40, 4, SERIAL_TIMEOUT);
349

350 351 352 353
	printf("CPU ID: 0x%08x\n",id);

	if(board_rev == BOARD_REV_V2 && id != ID_SAM9263) die ("Not a 9263 CPU");
	if(board_rev == BOARD_REV_V3 && id != ID_SAM9G45) die ("Not a 9G45 CPU");
354
	return 0;
355
}
356

357
int ddr_init(int board_rev)
358
{
359 360
	if(board_rev == BOARD_REV_V2)
		samba_load_applet("isp-extram-at91sam9263", INTERNAL_SRAM_BUF);
361 362
	else if(board_rev == BOARD_REV_V3)
		samba_load_applet("isp-extram-at91sam9g45", INTERNAL_SRAM_BUF);
363

364
	mbox_write(INTERNAL_SRAM_BUF, MBOX_COMMAND, APPLET_CMD_INIT);
365

366 367 368 369 370
	mbox_write(INTERNAL_SRAM_BUF, MBOX_TRACELEVEL, 0);
	mbox_write(INTERNAL_SRAM_BUF, MBOX_EXTRAM_RAMTYPE, 1);
	mbox_write(INTERNAL_SRAM_BUF, MBOX_EXTRAM_VDDMEM, 0);
	mbox_write(INTERNAL_SRAM_BUF, MBOX_EXTRAM_BUSWIDTH, 16);
	mbox_write(INTERNAL_SRAM_BUF, MBOX_EXTRAM_DDRMODEL, 0);
371

372
	samba_run(INTERNAL_SRAM_BUF, 100000000);
373

374 375
	if((samba_read(INTERNAL_SRAM_BUF + MBOX_COMMAND, 4, 10000000)) != ~APPLET_CMD_INIT) die("invalid response from applet init");
	if((samba_read(INTERNAL_SRAM_BUF + MBOX_STATUS, 4, 10000000)) != APPLET_SUCCESS) die("invalid response from applet status");
376 377 378
	return 0;
}

379 380 381


int ddr_load(int nFile, const sndfile* pFileArray)
382
{
383 384 385 386 387 388 389 390 391
	int i;
	uint32_t len=0;
	fprintf(stderr,"Loading DDR...\n");
	for(i=0;i<nFile; i++)
	{
		printf("\t @ 0x%08x : %s ; ",SDRAM_START+ pFileArray[i].offset, pFileArray[i].fname);
		len += samba_send_file(pFileArray[i].fname, SDRAM_START+pFileArray[i].offset,0, 0, 0);
	}
	mem_write(MEMTYPE_DDR,0,SDRAM_START,len);
392
	fprintf(stderr,"\n");
393
	return 0;
394 395
}

396 397 398


int memflash_init(int type, int board_rev)
399
{
400
	fprintf(stderr,"Initializing %s...\n",memName[type]);
401

402
	//Load the correct applet
403
	if(board_rev == BOARD_REV_V2)
404 405 406 407
	{
		if(type==MEMTYPE_NAND) samba_load_applet("isp-nandflash-at91sam9263", INTERNAL_SRAM_BUF);
		else if(type==MEMTYPE_DF) samba_load_applet("isp-dataflash-at91sam9263", INTERNAL_SRAM_BUF);
	}
408
	else if(board_rev == BOARD_REV_V3)
409 410 411 412 413
	{
		if(type==MEMTYPE_NAND) samba_load_applet("isp-nandflash-at91sam9g45", INTERNAL_SRAM_BUF);
		else if(type==MEMTYPE_DF) samba_load_applet("isp-dataflash-at91sam9g45", INTERNAL_SRAM_BUF);
	}

414 415 416 417 418 419 420 421 422

	mbox_write(INTERNAL_SRAM_BUF, MBOX_COMMAND, APPLET_CMD_INIT);
	mbox_write(INTERNAL_SRAM_BUF, MBOX_COMTYPE, 1);
	mbox_write(INTERNAL_SRAM_BUF, MBOX_TRACELEVEL, 4);
	mbox_write(INTERNAL_SRAM_BUF, MBOX_DATAFLASH_INDEX, 0);

	samba_run(INTERNAL_SRAM_BUF, 0);

	if(samba_read(INTERNAL_SRAM_BUF + MBOX_COMMAND, 4, 10000000) != ~APPLET_CMD_INIT) die("invalid response from applet");
423
	if(samba_read(INTERNAL_SRAM_BUF + MBOX_STATUS, 4, 10000000) != APPLET_SUCCESS) die("Initialization failure (no chip deteted?)");
424 425

	buffer_addr = samba_read(INTERNAL_SRAM_BUF + MBOX_DATAFLASH_BUFFER_ADDR, 4, 10000000);
426 427
	fprintf(stderr,"Initializing %s > Done! \n\n",memName[type]);

428 429 430 431 432
	return 0;
}



433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
int mem_program(int type, int nFile, const sndfile* pFileArray)
{
	int i;
	uint32_t len=0;
	if(type==MEMTYPE_DDR) return ddr_load(nFile,pFileArray);
	else
	{

		fprintf(stderr,"Programming %s...\n",memName[type]);
		for(i=0;i<nFile; i++)
		{
			printf("\t @ 0x%08x : %s ; ",SDRAM_START+ pFileArray[i].offset, pFileArray[i].fname);
			len = samba_send_file(pFileArray[i].fname, SDRAM_START,0,0,0);
			mem_write(type,pFileArray[i].offset, SDRAM_START, len);
		}
		fprintf(stderr,"Programming %s Done!!!\n",memName[type]);
		return 0;
	}
}

void mem_check(int type)
{
	fprintf(stderr,"Checking %s...\n\n",memName[type]);

	mbox_write(INTERNAL_SRAM_BUF, MBOX_COMMAND, APPLET_CMD_LIST_BAD_BLOCKS);
	samba_run(INTERNAL_SRAM_BUF, 0);
	if((samba_read(INTERNAL_SRAM_BUF + MBOX_COMMAND, 4, 10000000)) != ~APPLET_CMD_LIST_BAD_BLOCKS) die("invalid response from applet write");
460
	if((samba_read(INTERNAL_SRAM_BUF + MBOX_STATUS, 4, 10000000)) != APPLET_SUCCESS) die("invalid response from applet status");
461 462
}

463
void mem_erase(int type, int scrub)
464
{
465 466
	const char *action=(scrub)?"Scrubing":"Erasing";
	fprintf(stderr,"%s %s...",action,memName[type]);
467

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	if(type==MEMTYPE_DF)
	{
		mbox_write(INTERNAL_SRAM_BUF, MBOX_COMMAND, APPLET_CMD_FULL_ERASE);
		mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_ERASE_TYPE,0);
		mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_ERASE_START,0);
		if(scrub)
			mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_ERASE_END,0);
		else
			mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_ERASE_END,DF_HWINFO_START);
		samba_run(INTERNAL_SRAM_BUF, 0);
//		fprint("\rDF: 2nd part...");
//		mbox_write(INTERNAL_SRAM_BUF, MBOX_COMMAND, APPLET_CMD_FULL_ERASE);
//		mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_ERASE_TYPE,0);
//		mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_ERASE_START,DF_HWINFO_END);
//		mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_ERASE_END,0);
//		samba_run(INTERNAL_SRAM_BUF, 0);
	}
	else
	{
		//Full NF Erase
		mbox_write(INTERNAL_SRAM_BUF, MBOX_COMMAND, APPLET_CMD_FULL_ERASE);
		if(scrub) mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_ERASE_TYPE,0x0000EA11);
		samba_run(INTERNAL_SRAM_BUF, 0);
	}
	fprintf(stderr,"\r%s %s > DONE\n\n",action,memName[type]);
493 494
}

495

496
int mem_write(int type, uint32_t offset, uint32_t buf_addr, uint32_t size)
497
{
498 499 500 501 502 503 504 505 506 507 508
	uint32_t timeout;
	applet_silent_mode=1;
	fprintf(stderr, "%s: Writing %d bytes at offset 0x%x buffer %x....", memName[type], size, offset, buf_addr);
	mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_BUF_ADDR, buf_addr);
	fprintf(stderr,"A");
	mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_BUF_ADDR, buf_addr);
	fprintf(stderr,"B");
	mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_BUF_SIZE, size);
	fprintf(stderr,"C");
	mbox_write(INTERNAL_SRAM_BUF, MBOX_MEM_BUF_OFFSET, offset);
	fprintf(stderr,"D");
509

510 511
	mbox_write(INTERNAL_SRAM_BUF, MBOX_COMMAND, APPLET_CMD_WRITE);
	fprintf(stderr,"E");
512

513 514
	if(type==MEMTYPE_DDR) timeout=10;
	else timeout=0;
515

516 517
	samba_run(INTERNAL_SRAM_BUF, timeout);
	fprintf(stderr,"F");
518

519 520
	if(type == MEMTYPE_DDR) // booting a barebox/kernel will fuck up USB, so we'll never get any response
		return 0;
521

522 523
	if(samba_read(INTERNAL_SRAM_BUF + MBOX_COMMAND, 4, 10000000) != ~APPLET_CMD_WRITE) die(" invalid response from applet");
	if(samba_read(INTERNAL_SRAM_BUF + MBOX_STATUS, 4, 10000000) != APPLET_SUCCESS) die(" write failure");
524

525
	printf(" OK\n");
526
	return 0;
527 528
}

529 530

uint32_t getOffset(const char *str)
531
{
532 533 534 535 536 537 538 539 540
	uint32_t ret=0;
	if (str == NULL || *str == '\0') return ret;
	if(str[0]=='0' && (str[1]=='X' || str[1]=='x'))
	{
		str+=2;
		sscanf(str, "%x", &ret);
	}
	else sscanf(str, "%d", &ret);
	return ret;
541 542
}

543 544 545 546 547

void show_help(const char* serial_port)
{
	printf("WhiteRabbit MCH DataFlash programmer (c) T.W. 2010\n");
	printf("Compiled by %s (%s)\ngit rev:%s\n\n",git_user,build_time,git_revision);
548 549 550
	printf("Usage: %s [options] [<FILE1> <OFFSET1> <FILE2> <OFFSET2> <FILE3> <OFFSET3>]\n", program_name);
	printf("if FILEx set it will write the FILEx at the given OFFSETx (by default 0x0), "
			"otherwise you can use this binary to erase, check or scrub a memory\n");
551
	printf("Options:\n");
552 553 554
	printf("\t-m \t Select the memory type: ddr, nf, df\n");
	printf("\t-e \t\t erase the memory\n");
	printf("\t-c \t\t check the memory (not available for df)\n");
555
	printf("\t-s \t\t scrub the memory (WARNING: full erase)\n");
556
	printf("\t-r \t\t addr run the image at address addr\n");
557
	printf("\t-p SERIAL_PORT\t By default it is: -p %s\n",serial_port);
558 559 560 561
	printf("\t-h \t\t Show this little help\n");
	printf("\n");
}

562
int main(int argc, char *argv[])
563 564 565
{
	int board_rev  = BOARD_REV_V3;
	char *serial_port = "/dev/ttyACM0";
566 567 568 569
	char * mode_str ="df";
	char opt;
	int erase=0, check=0, scrub=0;
	int type=-1;
570
	unsigned int run_addr=0;
571
	int nFile=0;
572
	int run =0 ;
573 574


575 576
	program_name = basename(strdup(argv[0]));
	program_path = dirname(strdup(argv[0]));
577 578 579 580 581 582

	if(argc==1)
	{
		show_help(serial_port);
		return 1;
	}
583

584
	//Parse options
585
	while ((opt = getopt (argc, argv, "m:p:r:ecsh")) != -1)
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
		switch (opt) {
		case 'm':
			mode_str = optarg;
			break;
		case 'p':
			serial_port = optarg;
			break;
		case 'e':
			erase = 1;
			break;
		case 'c':
			check = 1;
			break;
		case 's':
			scrub = 1;
			break;
		case 'r':
			run = 1;
			sscanf(optarg,"%i", &run_addr);
			break;

607 608 609 610 611 612 613
		case 'h':
		case '?':
			show_help(serial_port);
			return 0;
		default:
			printf("find: illegal option %c\n", opt);
			break;
614
		}
615 616 617 618 619



	//Obtain the various filename & offset (3 MAX)
	for(nFile=0; optind < argc; nFile++)
620
	{
621 622 623 624 625
		if (nFile >= ARRAY_SIZE(filearray)) {
			fprintf(stderr, "%s: too may arguments\n",
				argv[0]);
			exit(1);
		}
626 627
		filearray[nFile].fname=argv[optind++];
		filearray[nFile].offset=getOffset(argv[optind++]);
628
	}
629 630 631

	printf("n=%d, mode=%s, nFile=%d, serial=%s, (e=%d,c=%d,s=%d)\n", argc, mode_str, nFile, serial_port, erase,check,scrub);

632 633 634 635 636
	if (serial_open(serial_port, PORT_SPEED) < 0) {
		fprintf(stderr, "%s: %s: %s\n", argv[0], serial_port,
			strerror(errno));
		exit(1);
	}
637 638
	fprintf(stderr,"Initializing SAM-BA: ");
	samba_connect(board_rev);
639

640 641
	//Init the DDR for every action (used as buffer)
	fprintf(stderr,"\nInitializing DDR...\n");
642
	ddr_init(board_rev);
643
	fprintf(stderr,"Initializing DDR > Done\n\n");
644

645
	//Obtain the memory type mode
646 647 648 649 650 651 652
	if(strcmp(mode_str,"nand")==0 || strcmp(mode_str,"nf")==0) {
		type=MEMTYPE_NAND;
	} else if(strcmp(mode_str,"dataflash")==0 || strcmp(mode_str,"df")==0) {
		type=MEMTYPE_DF;
	} else if(strcmp(mode_str,"ddr")==0 || strcmp(mode_str,"ram")==0) {
		type=MEMTYPE_DDR;
	} else {
653 654 655
		fprintf(stderr,"Bad memory type %s...\n\n",mode_str);
		show_help(serial_port);
		return 0;
656
	}
657 658 659


	//Init the memory (except DDR which is already init)
660 661
	if(type!=MEMTYPE_DDR)
		memflash_init(type, board_rev);
662 663

	//Run the action
664 665
	if((erase || scrub) && type!=MEMTYPE_DDR)
		mem_erase(type,scrub);
666 667 668 669
	if(check)
		mem_check(type);
	if(nFile>0)
		mem_program(type,nFile,(const sndfile*)&filearray);
670

671 672 673 674 675
	if(run)
	{
		fprintf(stderr,"Executing payload @ %x\n", run_addr);
		samba_run(run_addr, 0);
	}
676

677
	serial_close();
678 679 680
	printf("Closing...\n");

	return 0;
681
}