summaryrefslogtreecommitdiff
path: root/common/cmd_mmc.c
blob: 02b387899e162e5c19fa56f57491bff807d929c5 (plain)
1
2
3
4
5
6
7
8
9
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
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
460
461
462
463
464
465
466
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
/*
 * (C) Copyright 2003
 * Kyle Harris, kharris@nexus-tech.net
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <command.h>


#if (CONFIG_COMMANDS & CFG_CMD_MMC)

#include <mmc.h>
#include <environment.h>
#include <fastboot.h>
#include <part.h>
#include <fastboot.h>

//#define	RSA_DEBUG 1
#ifdef	RSA_DEBUG
#define	RSA_PRINTF(fmt,args...)	printf (fmt ,##args)
#else
#define  RSA_PRINTF(fmt,args...)
#endif
static   block_dev_desc_t *mmc_cur_dev = NULL;
static  unsigned long mmc_part_offset =0;
static  int mmc_cur_part =1;
struct sig_header {
    unsigned int img_size;      //kernel signature size in bytes
    unsigned int reserved[3];   //reserved.
};
extern  int load_ptbl(void) ; //Charles

int mmc_register_device(block_dev_desc_t *dev_desc, int part_no);
extern int image_rsa_check(
unsigned int image_mem_addr,
unsigned int image_size,
unsigned int sig_addr,
unsigned int sig_size);

static int sd_check_ctrlc(void)
{
    extern int ctrlc (void);
    if( ctrlc()){
        printf("Abort\n");
        return 1;
    }
    return 0;
}

int do_mmc_wait_insert(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
    SD_Controller_Powerup();
	int insert = simple_strtoul(argv[1], NULL, 10);
	if(insert){
		printf("waiting insert SD card\n");
		while(SD_card_inserted()!=1){
			if( sd_check_ctrlc())
                    return -1;
		}
		return 0;

	}else{
		printf("wainting remove SD card\n");
		while(SD_card_inserted()!=0){
			if( sd_check_ctrlc())
                    return -1;
            udelay(500000);//delay 500ms
		}
		
		return 0;
	}
}

int do_mmc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int device_num = 1;
	ulong dev_id = 0;
	
	if (argc <= 1) {
		printf("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}
	
	if (argc == 2)
		dev_id = simple_strtoul(argv[1], NULL, 16);
	if (dev_id == 0 || dev_id == 1 || dev_id == 2)
		device_num = dev_id;

	if (mmc_init (1, (int)device_num) != 0) {
		//printf ("No MMC card found\n");
		return 1;
	}
	return 0;
}

int do_mmc_read (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int device_num = 1;
	ulong dev_id = 0;
	ulong addr,block_num,bytes,blk_cnt;
	ulong ret;
		
	if (argc < 4) {
		printf("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}
	
	/*get device id*/
	dev_id = simple_strtoul(argv[1], NULL, 16);
	if (dev_id == 0 || dev_id == 1 || dev_id == 2)
		device_num = dev_id;
	else {
		printf("dev_id Invalid\n");
		return 1;
	}

	/*get memory address*/
	addr = simple_strtoul (argv[2], NULL, 16);
	if (addr < 0) {
		printf("addr Invalid\n");
		return 1;
	}

	/*get card block address*/
	block_num = simple_strtoul (argv[3], NULL, 16);
	if (block_num < 0) {
		printf("block_num Invalid\n");
		return 1;
	}

	/*get transfer size is bytes*/
	bytes = simple_strtoul (argv[4], NULL, 16);
	if (bytes < 0) {
		printf("bytes Invalid\n");
		return 1;
	}

	if (bytes == 0)
		return 0;

	/*calculate transfer block count*/
	blk_cnt = (bytes / 512);
	if (bytes % 512)
		blk_cnt++;
	
	//printf("device_num = %x block_num = %x addr =  %x bytes = %x blk_cnt =%x\n",device_num,block_num,addr,bytes,blk_cnt);

	ret = mmc_bread(device_num,block_num,blk_cnt,(ulong *)addr);

	if (ret != blk_cnt) {
		printf("Read Data Fail\n");
		return 1;
	} else {
		printf("Read Data Success\n");
	}
		

	return 0;
}

int do_mmc_write (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int device_num = 1;
	ulong dev_id = 0;
	ulong addr,block_num,bytes,blk_cnt;
	ulong ret;
		
	if (argc < 4) {
		printf("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}
	
	/*get device id*/
	dev_id = simple_strtoul(argv[1], NULL, 16);
	if (dev_id == 0 || dev_id == 1 || dev_id == 2)
		device_num = dev_id;
	else {
		printf("dev_id Invalid\n");
		return 1;
	}

	/*get memory address*/
	addr = simple_strtoul (argv[2], NULL, 16);
	if (addr < 0) {
		printf("addr Invalid\n");
		return 1;
	}

	/*get card block address*/
	block_num = simple_strtoul (argv[3], NULL, 16);
	if (block_num < 0) {
		printf("block_num Invalid\n");
		return 1;
	}

	/*get transfer size is bytes*/
	bytes = simple_strtoul (argv[4], NULL, 16);
	if (bytes < 0) {
		printf("bytes Invalid\n");
		return 1;
	}
	
	if (bytes == 0)
		return 0;

	/*calculate transfer block count*/
	blk_cnt = (bytes / 512);
	if (bytes % 512)
		blk_cnt++;
	
	//printf("device_num = %x block_num = %x addr =  %x bytes = %x blk_cnt =%x\n",device_num,block_num,addr,bytes,blk_cnt);
	ret = mmc_bwrite(device_num,block_num,blk_cnt,(ulong *)addr);

	if (ret != blk_cnt) {
		printf("Write Data Fail\n");
		return 1;
	} else {
		printf("Write Data Success\n");
	}
		

	return 0;
}

 void memdump (void *pv, int num)
{
	unsigned int  tmp,ba;
	ba =(unsigned int) pv;
	for (tmp = 0;tmp < num/16; tmp++)
	printf("[%8.8x]%8.8x %8.8x %8.8x %8.8x\n",(0x10*tmp+ba),*(volatile unsigned int *)(ba+(tmp*0x10)),*(volatile unsigned int *)(ba+(tmp*0x10)+0x4),*(volatile unsigned int *)(ba+(tmp*0x10)+0x8),*(volatile unsigned int *)(ba+(tmp*0x10)+0xc));
}
extern int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
int do_saveenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
extern block_dev_desc_t *get_dev (char*, int);
extern fastboot_ptentry *fastboot_flash_find_ptn(const char *name);
extern int mmc_controller_no ; //Charles
int fastboot_part_type = 0;
int do_mmc_read_img (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int device_num = 1;
	int dev_id = 0;
	ulong kernel_addr,ramdisk_addr;
	ulong blk_cnt_kenel, blk_cnt_ramdisk, page_cnt_kenel, page_cnt_ramdisk;
	ulong ret;
	static unsigned char data[512];
	block_dev_desc_t *dev_desc=NULL;
	ulong part=1;
	char *ep;
	unsigned int  page_size, kernel_size, ramdisk_size, total_size,total_page;
	
	char var[64], val[32];
	char *setenv[4]  = { "setenv", NULL, NULL, NULL, };
	setenv[1] = var;
	setenv[2] = val;

	char *key = NULL;
	unsigned int  pub_key_size;
	unsigned int image_mem_addr = 0x01E00000;  //addr 30M 
	unsigned int image_size;
	unsigned int sig_addr = 0x02E00000;
	unsigned int sig_size;
	int rcode = 0, pub_key_flag = 0;
	unsigned char idx_max = 8, i = 0;
	char *p = NULL;
	char ps[idx_max];
	char * endp;

	key = getenv("wmt.rsa.pem");
	pub_key_size = strlen(key);
	if (!key) {
		printf("No RSA public key !! \n");
		pub_key_flag = 0;
	} else {
		pub_key_flag = 1;
	}
	p = getenv("wmt.fb.param");
	if (p) {
		while (i < idx_max) {
			ps[i++] = simple_strtoul(p, &endp, 16);
			if (*endp == '\0')
				break;
			p = endp + 1;
			if (*p == '\0')
				break;        
		}
		fastboot_part_type= ps[3];//support MBR or GPT
	}
	/*RSA_PRINTF("pub_key_size=0x%x, key=%s\n", pub_key_size, key);*/	

	if (argc < 4) {
		printf("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}
	/*get device id*/
	dev_id = (int)simple_strtoul (argv[2], &ep, 16);
	if (dev_id == 0 || dev_id == 1 || dev_id == 2 || dev_id == 3)  {
		device_num = dev_id;
		mmc_controller_no = dev_id;
	}	
	else {
		printf("dev_id Invalid\n");
		return 1;
	}
	/*get mmc dev descriptor*/
	dev_desc= get_dev(argv[1], dev_id);
	if (dev_desc == NULL) {
		puts ("\n** Invalid boot device **\n");
		return 1;
	}
	if (*ep) {
		if (*ep != ':') {
			puts ("\n** Invalid boot device, use `dev[:part]' **\n");
			return 1;
		}
		part = (int)simple_strtoul(++ep, NULL, 16);
	}

	/*printf("dev_id:%x,part:%x\n",dev_id,part);*/
	/* init mmc controller */	
	if (mmc_init(1, device_num)) {
		printf("mmc init failed?\n");
		return 1;
	}
	if (mmc_register_device(dev_desc, part) != 0) {
		printf ("\n** Unable to use %s %d:%d for fatload **\n", argv[1], dev_id, part);
		return 1;
	}
 		

	/*get kernel memory address*/
	kernel_addr = simple_strtoul (argv[3], NULL, 16);
	if (kernel_addr < 0) {
		printf("kernel addr Invalid\n");
		return 1;
	}

	/*get ramdisk memory address*/
	ramdisk_addr = simple_strtoul (argv[4], NULL, 16);
	if (ramdisk_addr < 0) {
		printf("ramdisk addr  Invalid\n");
		return 1;
	}
	/*mmc block read partition offset sectors*/
	ret = mmc_bread(device_num, mmc_part_offset, 1, (void *)data);
	if (ret == -1  ) {
		printf("Read Bootimg Header Fail\n");
		return 1;
	} else {
		RSA_PRINTF("Read Bootimg Header Success\n");
	}

	/*memdump ((void *)data, 512);*/
	struct fastboot_boot_img_hdr *fb_hdr = (struct fastboot_boot_img_hdr *)data;
	
	if (memcmp(data, "ANDROID!", 8)) {
		printf(" boot.img  partition table not found\n");
		return -1;
	}
	/*kernel size change to sectors*/
	kernel_size = fb_hdr->kernel_size ;
	ramdisk_size = fb_hdr->ramdisk_size;
	page_size = fb_hdr->page_size ; 
	total_size = 1*page_size + kernel_size + ramdisk_size;

	/* calculate transfer block count */
	blk_cnt_kenel = (fb_hdr->kernel_size / 512);
	if (fb_hdr->kernel_size % 512)
		blk_cnt_kenel++;
	
	page_cnt_kenel = (fb_hdr->kernel_size / page_size);
	if (fb_hdr->kernel_size % page_size)
		page_cnt_kenel++;
	
	blk_cnt_ramdisk = (fb_hdr->ramdisk_size / 512);
	if (fb_hdr->ramdisk_size % 512)
		blk_cnt_ramdisk++;
	
	page_cnt_ramdisk = (fb_hdr->ramdisk_size / page_size);
	if (fb_hdr->ramdisk_size % page_size)
		page_cnt_ramdisk++;
	
	total_page = 1 + page_cnt_kenel + page_cnt_ramdisk;
	image_size = total_page * page_size;
	
	RSA_PRINTF("rsa page_cnt_kenel :0x%x, page_cnt_ramdisk :0x%x \n", page_cnt_kenel, page_cnt_ramdisk);
	RSA_PRINTF("rsa total_page :0x%x, image_size :0x%x \n", total_page, image_size);
	/*if pub_key_flag are set ,then verify signed bootimg signature*/
	if (pub_key_flag == 1) { 
		/*read total signed boot image*/	
		ret = mmc_bread(device_num, mmc_part_offset , (image_size/512) + 1, (void *)image_mem_addr);
		if (ret == -1  ) {
			printf("Read Bootimg Fail\n");
			return 1;
		} else {
			printf("Read Bootimg Success\n");
		}
		/* image sign signature addr */
		sig_addr = image_mem_addr + image_size;
		/*memdump ((void *)sig_addr, 512);*/
		
	      	struct sig_header *sig_hdr = (struct sig_header *)sig_addr;
		sig_size = sig_hdr->img_size ;
		/*memdump ((void *)sig_addr + 0x10, 256);*/
	     
		rcode = image_rsa_check(image_mem_addr, image_size, sig_addr + 0x10, sig_size);
	       if (rcode != 0 ) {
	   		printf("\nImage RSA Check Fail, rcode :%x\n", rcode);	
		   	return 1;  /* rsa verify fail */
	        } else {
      	   			printf("\nImage RSA Check Success\n");	
					/*No need copy to kernel addr  */
				memcpy ((void *)kernel_addr, (void *)image_mem_addr + 1*page_size, page_cnt_kenel*page_size);
					/*copy to ramdisk addr  */
				memcpy ((void *)ramdisk_addr, (void *)image_mem_addr + 1*page_size + page_cnt_kenel*page_size, page_cnt_ramdisk*page_size);
				
	        }
	}else {
		/* image_mem_addr  0x01E00000 */
		/*ret = mmc_bread(device_num, mmc_part_offset , image_size/512 , image_mem_addr);*/

		/* load kernel image from mmc boot image to kernel_addr */
		RSA_PRINTF(" mmc_part_offset + page_size:0x%x, fb_hdr->kernel_size:0x%x, mem kernel_addr:0x%x\n", 
			mmc_part_offset + page_size / 512, fb_hdr->kernel_size, kernel_addr);
	    	ret = mmc_bread(device_num, mmc_part_offset + page_size / 512, blk_cnt_kenel, (void *)kernel_addr);
		if (ret == -1  ) {
			printf("Read Kernel Data Fail\n");
			return 1;
		} else {
			printf("Read Kernel Data Success\n");
		}

		/* load ramdisk image from mmc boot image to ramdisk_addr */
		RSA_PRINTF("mmc_part_offset + page_size + blk_cnt_kenel:0x%x, fb_hdr->ramdisk_size:0x%x, mem ramdisk_addr:0x%x\n",
				mmc_part_offset + page_size /512 + page_cnt_kenel *(page_size/512), fb_hdr->ramdisk_size, ramdisk_addr);
	    	ret = mmc_bread(device_num, mmc_part_offset + page_size /512 + page_cnt_kenel *(page_size /512), blk_cnt_ramdisk, (void *)ramdisk_addr);
		if (ret == -1  ) {
			printf("Read Ramdisk Data Fail\n");
			return 1;
		} else {
			printf("Read Ramdisk Data Success\n");
		}
	}
	/*save ramdisk size to env argv[4]*/
	sprintf (var, "%s", argv[5]);
	sprintf (val, "%x", fb_hdr->ramdisk_size);
	do_setenv (NULL, 0, 3, setenv);

	printf("%s %s %s\n", setenv[0], setenv[1], setenv[2]);
	/*do_saveenv (NULL, 0, 1, saveenv);*/

	return 0;
}
int do_mem_read_img (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int device_num = 1;
	int dev_id = 0;
	ulong kernel_addr,ramdisk_addr;
	ulong blk_cnt_kenel, blk_cnt_ramdisk, page_cnt_kenel, page_cnt_ramdisk;
	ulong ret;
	static unsigned char data[512];
	block_dev_desc_t *dev_desc=NULL;
	ulong part=1;
	char *ep;
	unsigned int  page_size, kernel_size, ramdisk_size, total_size,total_page;
	
	char var[64], val[32];
	char *setenv[4]  = { "setenv", NULL, NULL, NULL, };
	setenv[1] = var;
	setenv[2] = val;

	char *key = NULL;
	unsigned int  pub_key_size;
	unsigned int image_mem_addr = 0x01E00000;  //addr 30M 
	unsigned int image_size;
	unsigned int sig_addr = 0x02E00000;
	unsigned int sig_size;
	int rcode = 0, pub_key_flag = 0;
	unsigned char idx_max = 8, i = 0;
	char *p = NULL;
	char ps[idx_max];
	char * endp;

	key = getenv("wmt.rsa.pem");
	pub_key_size = strlen(key);
	if (!key) {
		printf("No RSA public key !! \n");
		pub_key_flag = 0;
	} else {
		pub_key_flag = 1;
	}

	if (argc < 4) {
		printf("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}
	/*get memory bootimg address */
	image_mem_addr= (int)simple_strtoul (argv[1], &ep, 16);
	if (image_mem_addr < 0) {
		printf("image_mem_addr Invalid\n");
		return 1;
	} 		

	/*get kernel memory address*/
	kernel_addr = simple_strtoul (argv[2], NULL, 16);
	if (kernel_addr < 0) {
		printf("kernel addr Invalid\n");
		return 1;
	}

	/*get ramdisk memory address*/
	ramdisk_addr = simple_strtoul (argv[3], NULL, 16);
	if (ramdisk_addr < 0) {
		printf("ramdisk addr Invalid\n");
		return 1;
	}

	/*memdump ((void *)data, 512);*/
	struct fastboot_boot_img_hdr *fb_hdr = (struct fastboot_boot_img_hdr *)image_mem_addr;
	
	if (memcmp(image_mem_addr, "ANDROID!", 8)) {
		printf(" boot.img  partition table not found\n");
		return -1;
	}
	/*kernel size change to sectors*/
	kernel_size = fb_hdr->kernel_size ;
	ramdisk_size = fb_hdr->ramdisk_size;
	page_size = fb_hdr->page_size ; 
	total_size = 1*page_size + kernel_size + ramdisk_size;

	/* calculate transfer block count */
	blk_cnt_kenel = (fb_hdr->kernel_size / 512);
	if (fb_hdr->kernel_size % 512)
		blk_cnt_kenel++;
	
	page_cnt_kenel = (fb_hdr->kernel_size / page_size);
	if (fb_hdr->kernel_size % page_size)
		page_cnt_kenel++;
	
	blk_cnt_ramdisk = (fb_hdr->ramdisk_size / 512);
	if (fb_hdr->ramdisk_size % 512)
		blk_cnt_ramdisk++;
	
	page_cnt_ramdisk = (fb_hdr->ramdisk_size / page_size);
	if (fb_hdr->ramdisk_size % page_size)
		page_cnt_ramdisk++;
	
	total_page = 1 + page_cnt_kenel + page_cnt_ramdisk;
	image_size = total_page * page_size;
	
	RSA_PRINTF("page_cnt_kenel :0x%x, page_cnt_ramdisk :0x%x \n", page_cnt_kenel, page_cnt_ramdisk);
	RSA_PRINTF("total_page :0x%x, image_size :0x%x \n", total_page, image_size);
	/*if pub_key_flag are set ,then verify signed bootimg signature*/
	if (pub_key_flag == 1) { 
		
		/* image sign signature addr */
		sig_addr = image_mem_addr + image_size;
		memdump ((void *)sig_addr, 512);
		
	    struct sig_header *sig_hdr = (struct sig_header *)sig_addr;
		sig_size = sig_hdr->img_size ;
		memdump ((void *)sig_addr + 0x10, 256);
	     
		rcode = image_rsa_check(image_mem_addr, image_size, sig_addr + 0x10, sig_size);
       if (rcode != 0 ) {
	   		printf("\nImage RSA Check Fail, rcode :%x\n", rcode);	
		   	return 1;  /* rsa verify fail */
        } else {
			printf("\nImage RSA Check Success\n");	
			/*No need copy to kernel addr  */
			memcpy ((void *)kernel_addr, (void *)image_mem_addr + 1*page_size, page_cnt_kenel*page_size);
			/*copy to ramdisk addr  */
			memcpy ((void *)ramdisk_addr, (void *)image_mem_addr + 1*page_size + page_cnt_kenel*page_size,
			page_cnt_ramdisk*page_size);			
        }
	}else {
		/* load kernel image from mem boot image to kernel_addr */
		RSA_PRINTF("fb_hdr->kernel_size:0x%x, mem kernel_addr:0x%x\n", 
		fb_hdr->kernel_size, kernel_addr);
		/*need copy to kernel addr  */
		memcpy ((void *)kernel_addr, (void *)image_mem_addr + 1*page_size, page_cnt_kenel*page_size);
		/*copy to ramdisk addr  */
		memcpy ((void *)ramdisk_addr, (void *)image_mem_addr + 1*page_size + page_cnt_kenel*page_size,
		page_cnt_ramdisk*page_size);
		/* load ramdisk image from mmc boot image to ramdisk_addr */
		RSA_PRINTF("fb_hdr->ramdisk_size:0x%x, mem ramdisk_addr:0x%x\n",
		fb_hdr->ramdisk_size, ramdisk_addr);
	}
	/*save ramdisk size to env argv[4]*/
	sprintf (var, "%s", argv[4]);
	sprintf (val, "%x", fb_hdr->ramdisk_size);
	do_setenv (NULL, 0, 3, setenv);

	printf("%s %s %s\n", setenv[0], setenv[1], setenv[2]);
	/*do_saveenv (NULL, 0, 1, saveenv);*/

	return 0;
}

struct partition {
	const char *name;
	unsigned size_kb;
};
#define GPT 1
int mmc_register_device(block_dev_desc_t *dev_desc, int part_no)
{
	unsigned char buffer[0x200];
	disk_partition_t info;
	struct fastboot_ptentry *ptn;
	char *name;
	if (!dev_desc->block_read)
		return -1;
	mmc_cur_dev=dev_desc;
	/* check if we have a MBR (on floppies we have only a PBR) */
	if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
		printf ("** Can't read from device %d **\n", dev_desc->dev);
		return -1;
	}
	if (part_no == 2)	{
		name ="boot";
	} else if (part_no == 7) {
		name ="u-boot-logo";
	} else if (part_no == 8) {
		name ="kernel-logo";
	} else {
		name ="undefine";
	}

	/*ext4 GUID partition */
	if (fastboot_part_type == GPT) {
		load_ptbl();
		ptn = fastboot_flash_find_ptn(name);
		if (ptn == 0) {
			printf("%s partition does not exist", name);
			return -1;
		}
		mmc_part_offset = ptn->start;
		mmc_cur_part = part_no;
	} else {
	/*FAT partition*/
     
	if(!get_partition_info(dev_desc, part_no, &info)) {
			mmc_part_offset = info.start;
			mmc_cur_part = part_no;
			printf("part_offset : %x, cur_part : %x\n", mmc_part_offset, mmc_cur_part);
	} else {
#if 1
			mmc_part_offset = info.start;
			mmc_cur_part = part_no;

			//printf ("**Partition %d not valid on device %d **\n",part_no,dev_desc->dev);
			//return -1;
#else

			part_offset=32;
			cur_part = 1;
#endif
		}
	}	
	return 0;
}

int do_mmc_load_guid_img (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int device_num = 1;
	int dev_id = 0;
	ulong logo_addr,logo_size;
	ulong blk_cnt_logo;
	ulong ret;
	block_dev_desc_t *dev_desc=NULL;
	ulong part=1;
	char *ep;
	
	char var[64], val[32];
	char *setenv[4]  = { "setenv", NULL, NULL, NULL, };
	setenv[1] = var;
	setenv[2] = val;

	unsigned char idx_max = 8, i = 0;
	char *p = NULL;
	char ps[idx_max];
	char * endp;
	
	p = getenv("wmt.fb.param");
	if (p) {
		while (i < idx_max) {
			ps[i++] = simple_strtoul(p, &endp, 16);
			if (*endp == '\0')
				break;
			p = endp + 1;
			if (*p == '\0')
				break;        
		}
		fastboot_part_type= ps[3];//support MBR or GPT
	}

	if (argc < 4) {
		printf("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}
	/*get device id*/
	dev_id = (int)simple_strtoul (argv[2], &ep, 16);
	if (dev_id == 0 || dev_id == 1 || dev_id == 2 || dev_id == 3)  {
		device_num = dev_id;
		mmc_controller_no = dev_id;
	}	
	else {
		printf("dev_id Invalid\n");
		return 1;
	}
	/*get mmc dev descriptor*/
	dev_desc= get_dev(argv[1], dev_id);
	if (dev_desc == NULL) {
		puts ("\n** Invalid boot device **\n");
		return 1;
	}
	if (*ep) {
		if (*ep != ':') {
			puts ("\n** Invalid boot device, use `dev[:part]' **\n");
			return 1;
		}
		part = (int)simple_strtoul(++ep, NULL, 16);
	}
	/*printf("dev_id=%d,part=%d\n", dev_id, part);*/
	/* init mmc controller */	
	if (mmc_init(1, device_num)) {
		printf("mmc init failed?\n");
		return 1;
	}
	if (mmc_register_device(dev_desc, part) != 0) {
		printf ("\n** Unable to use %s %d:%d for fatload or guidload **\n", argv[1], dev_id, part);
		return 1;
	}
 		
	/*get logo memory address*/
	logo_addr = simple_strtoul (argv[3], NULL, 16);
	if (logo_addr < 0) {
		printf("logo addr Invalid\n");
		return 1;
	}

	/*get logo size*/
	logo_size = simple_strtoul (argv[4], NULL, 16);
	if (logo_size < 0) {
		printf("logo_size  Invalid\n");
		return 1;
	}
	blk_cnt_logo = (logo_size / 512);
	if (logo_size % 512)
		blk_cnt_logo++;

	/*mmc block read partition offset sectors*/
	ret = mmc_bread(device_num, mmc_part_offset, blk_cnt_logo, (void *)logo_addr);
	if (ret == -1  ) {
		printf("Read Logo Fail\n");
		return 1;
	} else {
		RSA_PRINTF("Read Logo Success\n");
	}

	/*memdump ((void *)logo_addr, 512);*/

	return 0;
}

U_BOOT_CMD(
	mmcinit,	2,	1,	do_mmc,
	"mmcinit - init mmc card\n"
	"  mmcinit 0 -- init mmc device 0 \n"
	"  mmcinit 1 -- init mmc device 1 \n"
	"  mmcinit 2 -- init mmc device 2 \n",
	"mmcinit - init mmc card\n"
	"  mmcinit 0 -- init mmc device 0 \n"
	"  mmcinit 1 -- init mmc device 1 \n"
	"  mmcinit 2 -- init mmc device 2 \n"
);

U_BOOT_CMD(
	mmcread,	5,	1,	do_mmc_read,
	"mmcread - read data from SD/MMC card\n"
	"  <dev_id> <addr> <block_num> <bytes>\n"
	"   -read data from SD/MMC card block address 'block_num' on 'dev_id'\n"
	"    to memory address 'addr' size is 'bytes'\n",
	"mmcread - read data from SD/MMC card\n"
	"  <dev_id> <addr> <block_num> <bytes>\n"
	"   -read data from SD/MMC card block address 'block_num' on 'dev_id'\n"
	"    to memory address 'addr' size is 'bytes'\n"
);

U_BOOT_CMD(
	mmcwrite,	5,	1,	do_mmc_write,
	"mmcwrite - write data to SD/MMC card\n"
	"  <dev_id> <addr> <block_num> <bytes>\n"
	"   -write data to SD/MMC card block address 'block_num' on 'dev_id'\n"
	"    from memory address 'addr' size is 'bytes'\n",
	"mmcwrite - write data to SD/MMC card\n"
	"  <dev_id> <addr> <block_num> <bytes>\n"
	"   -write data to SD/MMC card block address 'block_num' on 'dev_id'\n"
	"    from memory address 'addr' size is 'bytes'\n"
);


U_BOOT_CMD(
	sdwaitins,	2,	1,	do_mmc_wait_insert,
	"sdwaitins - wait sd card inserted or removed\n"
	"sdwaitins 0 -- waiting removed\n"
	"sdwaitins 1 -- waiting inserted\n",
	"sdwaitins - wait sd card inserted or removed\n"
	"sdwaitins 0 -- waiting removed\n"
	"sdwaitins 1 -- waiting inserted\n"
);
//Charles
U_BOOT_CMD(
	mmcreadimg,	6,	1,	do_mmc_read_img,
	"mmcreadimg - read boot.img or recovery.img from SD/MMC card\n"
	"  <dev_id[:partition_no]> <kernel_addr> <ramdisk_addr> <ramdisk_sizes>\n"
	"   -read boot.img from SD/MMC card partition on 'dev_id'\n"
	"    to memory address 'addr' size is 'bytes'\n",
	"mmcreadimg - read boot.img or recovery.img from SD/MMC card\n"
	"  <dev_id[:partition_no]> <kernel_addr> <ramdisk_addr> <ramdisk_sizes>\n"
	"   -read boot.img from SD/MMC card partition on 'dev_id'\n"
	"    to memory address 'addr' size is 'bytes'\n"
);
U_BOOT_CMD(
	guidload,	5,	1,	do_mmc_load_guid_img,
	"guidload - read u-boot-logo or kernel-logo partition from SD/MMC card\n"
	"  <dev_id[:partition_no]> <load_addr> <filesize> \n"
	"   -read u-boot-logo or kernel-logo partition from SD/MMC card partition on 'dev_id'\n"
	"    to memory address 'load_addr' in bytes'\n",
	"guidload - read u-boot-logo or kernel-logo partition from SD/MMC card\n"
	"  <dev_id[:partition_no]> <load_addr> <filesize> \n"
	"   -read u-boot-logo or kernel-logo partition from SD/MMC card partition on 'dev_id'\n"
	"    to memory address 'load_addr' in 'bytes'\n"
);
U_BOOT_CMD(
	memreadimg,	5,	1,	do_mem_read_img,
	"memreadimg - read boot.img or recovery.img from memory\n"
	"  <bootimg_mem_addr> <kernel_addr> <ramdisk_addr> <ramdisk_sizes>\n"
	"   -read boot.img from memory'\n"
	"    to memory address 'addr' size is 'bytes'\n",
	"memreadimg - read boot.img or recovery.img from memory\n"
	"  <bootimg_mem_addr> <kernel_addr> <ramdisk_addr> <ramdisk_sizes>\n"
	"   -read boot.img from bootimg_mem_addr\n"
	"    to memory address 'addr' size is 'bytes'\n"
);
#endif	/* CFG_CMD_MMC */