-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
1666 lines (1525 loc) · 45.5 KB
/
main.c
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libusb.h>
#include "main.h"
#include "checksum.h"
#include "protocol.h"
#include "ff.h"
#include "diskio.h"
#include "ext4.h"
#include "blockdev.h"
#include "test_lwext4.h"
#define SYBER_USB_VERSION "VERSION - 0.3"
/* usb bulk transfer buffer */
uint8_t data_buffer[DATA_BUFFER_SIZE];
/* libusb variable */
libusb_device **devs;
libusb_device *sprd_dev = NULL;
libusb_device_handle *sprd_handle;
/* check sum type */
uint8_t checksum_type = TYPE_CRC;
/* FatFs work area needed for each volume */
FATFS FatFs;
/* File object needed for each open file */
FIL Fil;
/**@brief Block device handle.*/
static struct ext4_blockdev *bd;
/**@brief Block cache handle.*/
static struct ext4_bcache *bc;
char part_table[][15]={
"prodnv", //05000000 = 80m
"miscdata", //01000000 = 16m
"l_fixnv1", //01000000 = 16m
"l_fixnv2", //01000000 = 16m
"l_runtimenv1", //01000000 = 16m
"l_runtimenv2", //01000000 = 16m
"l_modem", //0c000000 = 192m
"l_ldsp", //03000000 = 48m
"l_gdsp", //03000000 = 48m
"l_warm", //03000000 = 48m
"pm_sys", //01000000 = 16m
"sml", //01000000 = 16m
"logo", //01000000 = 16m
"fbootlogo", //01000000 = 16m
"wcnfdl", //01000000 = 16m
"wcnmodem", //0a000000 = 160m
"boot", //01000000 = 16m
"system", //b0040000 = 2.9g(about)
"cache", //96000000 = 2.4g
"recovery", //14000000 = 320m
"misc", //01000000 = 16m
"userdata", //00340000 = 3328k
"ubootlogo", //01000000 = 16m
"security", //32000000 = 800m
"dt", //04000000 = 64m
"cboot", //20000000 = 512m
"syberfs", //e8030000 = 3.8g(about)
"data", //00100000 = 1m(actual=4g)
"internalsd", //ffffffff = 4g
"", //end
};
char *usage="\
USAGE:\n\
========\n\
[sudo] ./syber_usb [ready|reset|shutdown|camera|read|write|ext4fs] [args]\n\
[sudo] ./syber_usb read {partition name} {size} {file}\n\
[sudo] ./syber_usb write {partition name} {file}\n\
[sudo] ./syber_usb ext4fs {ls|get} {dir|file}\n\
ready|reset|shutdown|camera|read|write|ext4fs\n\
- Connect device(ready)\n\
Reset device(reset)\n\
shutdown device(shutdown)\n\
Read image file to directory 'syberos_camera'(camera)\n\
Read partition(read)\n\
Write partition(write)\n\
Browse ext4fs directory or get ext4fs files(ext4fs)\n\
partition name - The name of the partition to read&write\n\
size - The size of the partition to read\n\
Support 'm/M' 'k/K' - 1k/K=1024Bytes\n\
file - The name of the file to read&write\n\
ls|get - Browse directory or get file\n\
dir - Directory to browse\n\
";
int is_sprd_dev(libusb_device *dev)
{
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev,&desc);
if( r == 0){
if(desc.idProduct == SPRD_PID && desc.idVendor == SPRD_VID){
printf("Found sprd usb\n");
printf("PID = 0x%X , VID = 0x%X\n",desc.idProduct,desc.idVendor);
return 0;
}
else return 1;
}
else{
return r;
}
}
void print_hex(uint8_t* str,int length)
{
int i;
for(i = 0; i < length; i++){
if(!(i%8)) putchar('\n');
printf("0x%02x ",str[i]);
}
if(i && (i%9))
putchar('\n');
}
void debug_print_hex(uint8_t* str,int length)
{
int i;
#ifdef SPRD_DEBUG
printf("debug:");
for(i = 0; i < length; i++){
if(!(i%8)) putchar('\n');
printf("0x%02x ",str[i]);
}
if(i && (i%9))
putchar('\n');
#endif
}
unsigned long get_file_size(const char *path)
{
unsigned long filesize = -1;
struct stat statbuff;
if(stat(path, &statbuff) < 0){
return filesize;
}else{
filesize = statbuff.st_size;
}
return filesize;
}
void print_device_descriptor(struct libusb_device_descriptor *desc_p)
{
int i;
for(i = 0; i < desc_p->bLength; i++){
if(!(i%8)) putchar('\n');
printf("0x%02x ",((uint8_t *)desc_p)[i]);
}
}
void print_config_descriptor(struct libusb_config_descriptor *desc_p)
{
int i;
for(i = 0; i < desc_p->bLength; i++){
if(!(i%8)) putchar('\n');
printf("0x%02x ",((uint8_t *)desc_p)[i]);
}
}
void print_interface_descriptor(struct libusb_interface_descriptor *desc_p)
{
int i;
for(i = 0; i < desc_p->bLength; i++){
if(!(i%8)) putchar('\n');
printf("0x%02x ",((uint8_t *)desc_p)[i]);
}
}
void printf_endpoint_descriptor(struct libusb_endpoint_descriptor *desc_p)
{
int i;
for(i = 0; i < desc_p->bLength; i++){
if(!(i%8)) putchar('\n');
printf("0x%02x ",((uint8_t *)desc_p)[i]);
}
}
/* printf the device descriptor & active config descriptor */
int print_descriptor(libusb_device *sprd_dev)
{
struct libusb_device_descriptor device_desc;
struct libusb_config_descriptor *config_desc;
struct libusb_interface_descriptor *interface_desc;
struct libusb_endpoint_descriptor *endpoint_desc;
int i,j;
int r;
r = libusb_get_device_descriptor(sprd_dev,&device_desc);
if(r != 0)
return r;
r = libusb_get_active_config_descriptor(sprd_dev,&config_desc);
//r = libusb_get_config_descriptor(sprd_dev,0,&config_desc);
if(r != 0)
return r;
/* device descritpors */
printf("Device descriptor:");
print_device_descriptor(&device_desc);
putchar('\n');
/* config descriptors */
printf("Active config descriptor:");
print_config_descriptor(config_desc);
putchar('\n');
/* interface & endpoint descriptors */
printf("Interface number:0x%02x\n",config_desc->bNumInterfaces);
for(i = 0; i < config_desc->bNumInterfaces; i++){
printf("Interface descriptor:");
interface_desc = \
(struct libusb_interface_descriptor*)config_desc->interface[i].altsetting;
print_interface_descriptor(interface_desc);
putchar('\n');
printf("Endpoint descriptor(num=%02x):",interface_desc->bNumEndpoints);
endpoint_desc = \
(struct libusb_endpoint_descriptor*)interface_desc->endpoint;
for(j = 0; j < interface_desc->bNumEndpoints ;j++){
printf_endpoint_descriptor(&endpoint_desc[j]);
}
putchar('\n');
}
/* free the config struct!!! */
libusb_free_config_descriptor(config_desc);
return 0;
}
/* return 0 - normal no 0 - error */
int sprd_usb_transfer(uint8_t* data,int size)
{
int r;int cnt;
r = libusb_bulk_transfer(sprd_handle,SPRD_ENDP_OUT,data,size,&cnt,200);
return r;
}
int sprd_usb_receive(uint8_t* data,int *size)
{
int r;
r = libusb_bulk_transfer(sprd_handle,SPRD_ENDP_IN,data,DATA_BUFFER_SIZE,size,200);
return r;
}
int sprd_verify_frame(uint8_t* frame,int frame_size)
{
if(frame[0] != SPRD_START_BYTE || frame[frame_size-1] != SPRD_END_BYTE)
return -1;
if(checksum(checksum_type,frame+1,frame_size-4) != ((frame[frame_size-3]<<8)/*'( )'!!*/ | frame[frame_size-2]))
return -1;
return 0;
}
/* send com frame without data field */
int sprd_com_nodata(uint8_t bsl_com_byte)
{
int r;
uint8_t data[8];
uint16_t crc;
data_buffer[0] = SPRD_START_BYTE; //header
data_buffer[1] = 0x00;
data_buffer[2] = bsl_com_byte; //type
data_buffer[3] = 0x00;
data_buffer[4] = 0x00; //data size
crc = checksum(checksum_type,&data_buffer[1],4);
data_buffer[5] = crc>>8;
data_buffer[6] = (crc&0xff); //crc
data_buffer[7] = SPRD_END_BYTE; //ender
r = sprd_usb_transfer(data_buffer,8);
debug_print_hex(data_buffer,8);
if(r) return r;
return 0;
}
/* phone ack only first commication (check_baudrate)*/
int sprd_version(void)
{
int r;int cnt;
data_buffer[0] = SPRD_START_BYTE;
r = sprd_usb_transfer(data_buffer,1);
if(r != 0) return r;
r = sprd_usb_receive(data_buffer,&cnt);
if(r != 0) return r;
r = sprd_verify_frame(data_buffer,cnt);
if(r != 0) return r;
debug_print_hex(data_buffer,cnt);
return 0;
}
int sprd_connect(void)
{
int cnt;int r;
r = sprd_com_nodata(BSL_CMD_CONNECT);
if(r) return r;
r = sprd_usb_receive(data_buffer,&cnt);
if(r) return r;
r = sprd_verify_frame(data_buffer,cnt);
if(r) return r;
debug_print_hex(data_buffer,cnt);
if(data_buffer[SPRD_FRAME_TYPE_OFF] != BSL_REP_ACK){
return -1;
}
return 0;
}
int sprd_exec_data(void)
{
int cnt;int r;
r = sprd_com_nodata(BSL_CMD_EXEC_DATA);
if(r) return r;
r = sprd_usb_receive(data_buffer,&cnt);
if(r) return r;
r = sprd_verify_frame(data_buffer,cnt);
if(r) return r;
debug_print_hex(data_buffer,cnt);
if(data_buffer[SPRD_FRAME_TYPE_OFF] == BSL_REP_ACK )
return 0;
//else if(data_buffer[SPRD_FRAME_TYPE_OFF] == BSL_INCOMPATIBLE_PARTITION)
// return 1;
else return -1;
return 0;
}
/* reset to normal state */
int sprd_normal_reset(void)
{
int r;int cnt;
r = sprd_com_nodata(BSL_CMD_NORMAL_RESET);
if(r) return r;
r = sprd_usb_receive(data_buffer,&cnt);
if(r) return r;
r = sprd_verify_frame(data_buffer,cnt);
if(r) return r;
debug_print_hex(data_buffer,cnt);
if(data_buffer[SPRD_FRAME_TYPE_OFF] == BSL_REP_ACK)
return 0;
else return -1;
return 0;
}
/* power down device */
int sprd_power_down(void)
{
int r;int cnt;
r = sprd_com_nodata(BSL_CMD_POWER_DOWN_TYPE);
if(r) return r;
r = sprd_usb_receive(data_buffer,&cnt);
if(r) return r;
r = sprd_verify_frame(data_buffer,cnt);
if(r) return r;
debug_print_hex(data_buffer,cnt);
if(data_buffer[SPRD_FRAME_TYPE_OFF] == BSL_REP_ACK)
return 0;
else return -1;
return 0;
}
/*
*dir:0 0x7e->0x7d 0x5e
* 0x7d->0x7d 0x5d
*dir:1 0x7d 0x5e->0x7e
* 0x7d 0x5d->0x7d
*return:exchange size
*/
int sprd_frame_exchange(char *dst, const char *src, int src_size, int dir)
{
int i,cnt;
dir &= 1;
if(dir == 0){
for(i = 0,cnt = 0;i < src_size; i++){
switch(src[i]){
case 0x7e:
if(i && (i != src_size-1)){
dst[cnt++] = 0x7d;dst[cnt++] = 0x5e;
}else dst[cnt++] = src[i];
break;
case 0x7d:
dst[cnt++] = 0x7d;dst[cnt++] = 0x5d;
break;
default:
dst[cnt++] = src[i];
break;
}
}
}
else if(dir == 1){
for(i = 0,cnt = 0;i < src_size;i++){
if(src[i] == 0x7d){
switch(src[i+1]){
case 0x5e:dst[cnt++] = 0x7e;
break;
case 0x5d:dst[cnt++] = 0x7d;
break;
default:break;
}
i++;
}
else{
dst[cnt++] = src[i];
}
}
}
return cnt;
}
/* send file to destnation addr */
int sprd_download(const char *file_name,uint32_t download_size,uint32_t dst_addr,uint32_t win_size)
{
int r;int i=0;int cnt;
uint16_t crc;
uint8_t com_buff[16];
/* start */
#ifdef SPRD_DEBUG
printf("sprd download step:start\n");
#endif
com_buff[i++] = SPRD_START_BYTE;
com_buff[i++] = 0x00;
com_buff[i++] = BSL_CMD_START_DATA; //com
com_buff[i++] = 0x00;
com_buff[i++] = 0x08; //data size
com_buff[i++] = dst_addr>>24;
com_buff[i++] = dst_addr>>16;
com_buff[i++] = dst_addr>>8;
com_buff[i++] = dst_addr; //distnation
com_buff[i++] = download_size>>24;
com_buff[i++] = download_size>>16;
com_buff[i++] = download_size>>8;
com_buff[i++] = download_size; // size
crc = checksum(checksum_type,com_buff+1,12);
com_buff[i++] = crc>>8;
com_buff[i++] = crc; //crc16
com_buff[i++] = SPRD_END_BYTE;
debug_print_hex(com_buff,16);
r = sprd_usb_transfer(com_buff,16);
if(r != 0) return r;
r = sprd_usb_receive(data_buffer,&cnt);
if(r != 0) return r;
r = sprd_verify_frame(data_buffer,cnt);
if(r != 0) return r;
if(data_buffer[SPRD_FRAME_TYPE_OFF] != BSL_REP_ACK){
return -1;
}
debug_print_hex(data_buffer,cnt);
/* middle */
#ifdef SPRD_DEBUG
printf("sprd download step:middle\n");
#endif
int fd = open(file_name,O_RDONLY);
if(fd == -1){
printf("middle:open %s error\n",file_name);
return -1;
}
ssize_t r_size;
char *s_buffer = malloc(win_size*2);
while(download_size){
#ifdef SPRD_DEBUG
printf("download_size is %d\n",download_size);
#endif
r_size = (download_size > win_size ) ? win_size:download_size;
r_size = read(fd,(void*)(data_buffer+SPRD_FRAME_DATA_OFF),r_size);
if(r_size == 0){
printf("middle:read file error\n");
return -1;
}
download_size -= r_size;
data_buffer[SPRD_FRAME_START_OFF] = SPRD_START_BYTE;
data_buffer[1] = 0x00;
data_buffer[SPRD_FRAME_TYPE_OFF] = BSL_CMD_MIDST_DATA;
data_buffer[SPRD_FRAME_DATA_SIZE_OFF] = r_size>>8;
data_buffer[SPRD_FRAME_DATA_SIZE_OFF+1] = r_size;
crc = checksum(checksum_type,data_buffer+1,r_size+4);
data_buffer[SPRD_FRAME_DATA_OFF+r_size] = crc>>8;
data_buffer[SPRD_FRAME_DATA_OFF+r_size+1] = crc;
data_buffer[r_size+8-1] = SPRD_END_BYTE;
//send frame steaming
//0x7e = 0x7d 0x7e^0x20 0x7d = 0x7d 0x7d^0x20 , except header & ender
for(i = 0,cnt = 0; i < r_size+8; i++){
switch(data_buffer[i]){
case 0x7e:
if(i && (i != r_size+7)){
s_buffer[cnt++] = 0x7d;s_buffer[cnt++] = 0x5e;
}else s_buffer[cnt++] = data_buffer[i];
break;
case 0x7d:
s_buffer[cnt++] = 0x7d;s_buffer[cnt++] = 0x5d;
break;
default:
s_buffer[cnt++] = data_buffer[i];
break;
}
}
debug_print_hex(s_buffer,cnt);
r = sprd_usb_transfer(s_buffer,cnt);
if(r) {
printf("sprd_usb_transfer error\n");
free(s_buffer);
return r;
}
r = sprd_usb_receive(data_buffer,&cnt);
if(r){
printf("sprd_usb_receive error\n");
free(s_buffer);
return r;
}
r = sprd_verify_frame(data_buffer,cnt);
if(r){
printf("sprd verify error\n");
free(s_buffer);
return r;
}
debug_print_hex(data_buffer,cnt);
if(data_buffer[SPRD_FRAME_TYPE_OFF] != BSL_REP_ACK){
printf("sprd ack error\n");
free(s_buffer);
return -1;
}
}
free(s_buffer);
if(close(fd) == -1){
printf("close file error\n");
return -1;
}
/* end */
#ifdef SPRD_DEBUG
printf("sprd download step:end\n");
#endif
r = sprd_com_nodata(BSL_CMD_END_DATA);
if(r) return r;
r = sprd_usb_receive(data_buffer,&cnt);
if(r) return r;
r = sprd_verify_frame(data_buffer,cnt);
if(r) {
return r;
}
debug_print_hex(data_buffer,cnt);
if(data_buffer[SPRD_FRAME_TYPE_OFF] != BSL_REP_ACK){
return -1;
}
return 0;
}
/* get sum of file */
uint32_t get_sum_file(const char* pathname)
{
int fd;int i;
ssize_t r_size =0;
uint32_t buff_size = 0x2800;
uint32_t check_sum = 0;
uint32_t total_size = 0;
void *buff = malloc(buff_size);//10k
fd = open(pathname,O_RDONLY);
r_size = read(fd,buff,buff_size);
while(r_size){
for(i = 0;i < r_size;i++){
check_sum += ((uint8_t*)buff)[i];
}
total_size += r_size;
r_size = read(fd,buff,buff_size);
}
free(buff);
close(fd);
return check_sum;
}
/* write file to partition
*part_name - partition name
*down_size - size of write
*file_name - file to write
*win_size - size of one receive
* Larger and faster, according to the mobile phone transmission capacity adjustment
* win_size < mobile maximum transmission size
*/
int sprd_download_partition(char* part_name,const char* file_name,uint32_t down_size,uint32_t win_size)
{
int i;int r;int cnt;
uint16_t crc;
uint8_t com_buffer[100];
char *s_buffer = malloc(win_size*2);
/* check param */
if(!down_size){
printf("download size = 0,nothing to do\n");
return -1;
}
if(file_name == NULL || part_name == NULL){
printf("file_name/part_name is NULL\n");
return -1;
}
if(access(file_name,F_OK) != 0){
printf("file '%s' not exist\n",file_name);
return -1;
}
struct stat sb;
stat(file_name, &sb);
if ((sb.st_mode & S_IFMT) != S_IFREG) {
printf("file '%s' is not regular file\n",file_name);
return -1;
}
printf("Writing file:'%s'(size=0x%x) to partition '%s'\n",file_name,down_size,part_name);
/* start */
#ifdef SPRD_DEBUG
printf("sprd download partition step:start\n");
#endif
if(strcmp(part_name,"l_fixnv1") == 0){//writing l_fixnv1 is diff
memset(com_buffer,0x00,88);
com_buffer[SPRD_FRAME_START_OFF] = SPRD_START_BYTE;
com_buffer[1] = 0x00;
com_buffer[SPRD_FRAME_TYPE_OFF] = BSL_CMD_START_DATA;
com_buffer[SPRD_FRAME_DATA_SIZE_OFF] = 0x50>>8;
com_buffer[SPRD_FRAME_DATA_SIZE_OFF+1] = 0x50;
com_buffer[88-1] = SPRD_END_BYTE;
for(i = 0;part_name[i] != '\0';i++){
com_buffer[SPRD_FRAME_DATA_OFF+i*2] = part_name[i];
com_buffer[SPRD_FRAME_DATA_OFF+i*2+1] = 0x00;
}
*((uint32_t*)(com_buffer+77)) = down_size; //download size
*((uint32_t*)(com_buffer+81)) = get_sum_file(file_name); //total data sum
crc = checksum(checksum_type,com_buffer+1,88-4);
com_buffer[88-3] = crc>>8;
com_buffer[88-2] = crc;
cnt = sprd_frame_exchange(s_buffer,com_buffer,88,0);
debug_print_hex(s_buffer,cnt);
}
else{
memset(com_buffer,0x00,84);
com_buffer[SPRD_FRAME_START_OFF] = SPRD_START_BYTE;
com_buffer[1] = 0x00;
com_buffer[SPRD_FRAME_TYPE_OFF] = BSL_CMD_START_DATA;
com_buffer[SPRD_FRAME_DATA_SIZE_OFF] = 0x4c>>8;
com_buffer[SPRD_FRAME_DATA_SIZE_OFF+1] = 0x4c;
com_buffer[84-1] = SPRD_END_BYTE;
for(i = 0;part_name[i] != '\0';i++){
com_buffer[SPRD_FRAME_DATA_OFF+i*2] = part_name[i];
com_buffer[SPRD_FRAME_DATA_OFF+i*2+1] = 0x00;
}
*((uint32_t*)(com_buffer+77)) = down_size; //download size
crc = checksum(checksum_type,com_buffer+1,84-4);
com_buffer[84-3] = crc>>8;
com_buffer[84-2] = crc;
cnt = sprd_frame_exchange(s_buffer,com_buffer,84,0);
debug_print_hex(s_buffer,cnt);
}
r = sprd_usb_transfer(s_buffer,cnt);
if(r != 0){
printf("start:sprd usb transfer error:%d\n",r);
return r;
}
for(i = 5;i ;i--){
r = sprd_usb_receive(data_buffer,&cnt);
if(r) continue;else break;
}
if(!i){
printf("start:sprd usb receive error:%d\n",r);
return r;
}
debug_print_hex(data_buffer,cnt);
if(sprd_verify_frame(data_buffer,cnt) != 0){
printf("start:sprd verify frame error\n");
return -1;
}
if(data_buffer[SPRD_FRAME_TYPE_OFF] != BSL_REP_ACK){
if(data_buffer[SPRD_FRAME_TYPE_OFF] == BSL_REP_DOWN_SIZE_ERROR){
printf("start:download size error(file '%s' is larger than partition '%s' size?)\n",file_name,part_name);
return -1;
}
else {
printf("start:sprd ack error\n");
return -1;
}
}
/* middle */
#ifdef SPRD_DEBUG
printf("sprd download partition step:middle\n");
#endif
umask(0);
int fd = open(file_name,O_RDONLY);
if(fd == -1){
printf("middle:open %s error\n",file_name);
return -1;
}
uint32_t down_size_count = down_size;
uint32_t down_size_percent = 255;/* if percent = 0,0% may not display Immediately */
uint32_t offset = 0;
uint32_t r_size = 0;
while(down_size){
#ifdef SPRD_DEBUG
printf("down_size is %d\n",down_size);
#endif
r_size = (down_size > win_size ) ? win_size:down_size;
r_size = read(fd,(void*)(data_buffer+SPRD_FRAME_DATA_OFF),r_size);
if(r_size == 0){
printf("middle:read file error\n");
return -1;
}
data_buffer[SPRD_FRAME_START_OFF] = SPRD_START_BYTE;
data_buffer[1] = 0x00;
data_buffer[SPRD_FRAME_TYPE_OFF] = BSL_CMD_MIDST_DATA;
data_buffer[SPRD_FRAME_DATA_SIZE_OFF] = r_size>>8;
data_buffer[SPRD_FRAME_DATA_SIZE_OFF+1] = r_size;
crc = checksum(checksum_type,data_buffer+1,r_size+4);
data_buffer[SPRD_FRAME_DATA_OFF+r_size] = crc>>8;
data_buffer[SPRD_FRAME_DATA_OFF+r_size+1] = crc;
data_buffer[r_size+8-1] = SPRD_END_BYTE;
//send frame steaming
//0x7e = 0x7d 0x7e^0x20 0x7d = 0x7d 0x7d^0x20 , except header & ender
cnt = r_size + 8;
cnt = sprd_frame_exchange(s_buffer,data_buffer,cnt,0);
debug_print_hex(s_buffer,cnt);
r = sprd_usb_transfer(s_buffer,cnt);
if(r) {
printf("middle:sprd_usb_transfer error:%d\n",r);
free(s_buffer);
return r;
}
r = sprd_usb_receive(data_buffer,&cnt);
if(r){
printf("middle:sprd_usb_receive error:%d\n",r);
free(s_buffer);
return r;
}
r = sprd_verify_frame(data_buffer,cnt);
if(r){
printf("middle:sprd verify error:%d\n",r);
free(s_buffer);
return r;
}
debug_print_hex(data_buffer,cnt);
if(data_buffer[SPRD_FRAME_TYPE_OFF] != BSL_REP_ACK){
printf("sprd ack error\n");
free(s_buffer);
return -1;
}
offset += r_size;
down_size -= r_size;
if(down_size_percent != ((unsigned long)offset*100/down_size_count)){
down_size_percent = (unsigned long)offset*100/down_size_count;
printf("\rdowload percent:%%%d",down_size_percent);
fflush(stdout);
if(down_size_percent == 100) putchar('\n');
}
}
free(s_buffer);
if(close(fd) != 0){
printf("close file error\n");
return -1;
}
/* end */
#ifdef SPRD_DEBUG
printf("sprd download partition step:end\n");
#endif
r = sprd_com_nodata(BSL_CMD_END_DATA);
if(r) {
printf("end step:sprd com nodata error:%d\n",r);
return r;
}
r = sprd_usb_receive(data_buffer,&cnt);
if(r) {
printf("end step:sprd usb receive error:%d\n",r);
return r;
}
r = sprd_verify_frame(data_buffer,cnt);
if(r) {
printf("end step:frame verify error:%d\n",r);
return r;
}
debug_print_hex(data_buffer,cnt);
if(data_buffer[SPRD_FRAME_TYPE_OFF] != BSL_REP_ACK){
printf("end step:ack error\n");
return -1;
}
return 0;
}
/* read partition to file
*part_name - partition name
*up_size - size of read
*win_size - size of one receive
* Larger and faster, according to the mobile phone transmission capacity adjustment
win_size < mobile maximum transmission size
*file_name - file to store
*/
int sprd_upload(char* part_name,uint32_t up_size,uint32_t win_size,char *file_name)
{
int i;int r;int cnt;
uint16_t crc;
uint8_t com_buffer[84];
char *s_buffer = malloc(win_size*2);
printf("Saving partition:'%s'(size=0x%x) to '%s'\n",part_name,up_size,file_name);
/* start */
#ifdef SPRD_DEBUG
printf("sprd upload step:start\n");
#endif
memset(com_buffer,0x00,84);
com_buffer[SPRD_FRAME_START_OFF] = SPRD_START_BYTE;
com_buffer[1] = 0x00;
com_buffer[SPRD_FRAME_TYPE_OFF] = BSL_CMD_READ_FLASH_START;
com_buffer[SPRD_FRAME_DATA_SIZE_OFF] = 0x4c>>8;
com_buffer[SPRD_FRAME_DATA_SIZE_OFF+1] = 0x4c;
com_buffer[84-1] = SPRD_END_BYTE;
for(i = 0;part_name[i] != '\0';i++){
com_buffer[SPRD_FRAME_DATA_OFF+i*2] = part_name[i];
com_buffer[SPRD_FRAME_DATA_OFF+i*2+1] = 0x00;
}
*((uint32_t*)(com_buffer+77)) = /*0x01000000*/0xffffffff; //max size???
crc = checksum(checksum_type,com_buffer+1,84-4);
com_buffer[84-3] = crc>>8;
com_buffer[84-2] = crc;
cnt = sprd_frame_exchange(s_buffer,com_buffer,84,0);
debug_print_hex(s_buffer,cnt);
r = sprd_usb_transfer(s_buffer,cnt);
if(r != 0){
printf("start:sprd usb transfer error:%d\n",r);
return r;
}
for(i = 5;i ;i--){
r = sprd_usb_receive(data_buffer,&cnt);
if(r) continue;else break;
}
if(!i){
printf("start:sprd usb receive error:%d\n",r);
return r;
}
debug_print_hex(data_buffer,cnt);
if(sprd_verify_frame(data_buffer,cnt) != 0){
printf("start:sprd verify frame error\n");
return -1;
}
if(data_buffer[SPRD_FRAME_TYPE_OFF] == BSL_REP_DOWN_SIZE_ERROR){
#ifdef SPRD_DEBUG
printf("partition size error(not care!)\n");
#endif
}
/* middle */
#ifdef SPRD_DEBUG
printf("sprd upload step:middle\n");
#endif
umask(0);
int fd = open(file_name,O_CREAT|O_WRONLY|O_TRUNC,00666);
if(fd == -1){
printf("middle:open or create %s error\n",file_name);
return -1;
}
uint32_t up_size_count = up_size;
uint32_t up_size_percent = 255;/* if up_size_percent = 0,0% may not display Immediately */
uint32_t offset = 0;
uint32_t s_size = 0;
while(up_size){
s_size = (up_size > win_size) ? win_size:up_size;
com_buffer[SPRD_FRAME_START_OFF] = SPRD_START_BYTE;
com_buffer[1] = 0x00;
com_buffer[SPRD_FRAME_TYPE_OFF] = BSL_CMD_READ_FLASH_MIDST;
com_buffer[SPRD_FRAME_DATA_SIZE_OFF] = 0x08>>8;
com_buffer[SPRD_FRAME_DATA_SIZE_OFF+1] = 0x08;
((uint32_t*)(com_buffer+SPRD_FRAME_DATA_OFF))[0] = s_size;
((uint32_t*)(com_buffer+SPRD_FRAME_DATA_OFF))[1] = offset;
crc = checksum(checksum_type,com_buffer+1,16-4);
com_buffer[16-3] = crc>>8;
com_buffer[16-2] = crc;
com_buffer[16-1] = SPRD_END_BYTE;
cnt = sprd_frame_exchange(s_buffer,com_buffer,16,0);
r = sprd_usb_transfer(s_buffer,cnt);
debug_print_hex(s_buffer,cnt);
if(r != 0){
printf("middle:sprd usb transfer error:%d\n",r);
return r;
}
int i_temp = 0;
for(cnt = 0;cnt != s_size + 8;){
i = 0;
do{
r = sprd_usb_receive(s_buffer+i,&i_temp);
if(r != 0){
printf("middle:sprd usb receive error:%d\n",r);
return r;
}
i += i_temp;
}while(s_buffer[i-1] == 0x7d);//last byte maybe 0x7d
debug_print_hex(s_buffer,i);
i = sprd_frame_exchange(data_buffer+cnt,s_buffer,i,1);
cnt += i;
}
if(sprd_verify_frame(data_buffer,cnt) != 0 || data_buffer[SPRD_FRAME_TYPE_OFF] != BSL_REP_READ_FLASH){
printf("middle:sprd verify frame error\n");
return -1;
}
//write to file cnt = frame_size data_buffer = frame_pointer
r = write(fd,data_buffer+5,cnt-8);
if(r == -1){
printf("middle:write to %s error\n",file_name);
return r;
}
if(r != cnt-8){
printf("middle:write %x bytes,not complete\n",r);
return r;
}
offset += s_size;
up_size -= s_size;
if(up_size_percent != ((unsigned long)offset*100/up_size_count)){
up_size_percent = (unsigned long)offset*100/up_size_count;
printf("\rupload percent:%%%d",up_size_percent);
fflush(stdout);
if(up_size_percent == 100) putchar('\n');
}
}
free(s_buffer);
if(close(fd) != 0){
printf("close file error\n");
return -1;
}
/* end */
#ifdef SPRD_DEBUG
printf("sprd upload step:end\n");
#endif
r = sprd_com_nodata(BSL_CMD_READ_FLASH_END);
if(r != 0){
printf("end:sprd com nodata error:%d\n",r);
return r;
}
r = sprd_usb_receive(data_buffer,&cnt);
if(r != 0){
printf("end:sprd usb receive error:%d\n",r);
return r;
}
debug_print_hex(data_buffer,cnt);
if(sprd_verify_frame(data_buffer,cnt) != 0 || data_buffer[SPRD_FRAME_TYPE_OFF] != BSL_REP_ACK){
printf("end:sprd ack error\n");
return -1;
}
return 0;
}
/* commucate with bootrom */
int sprd_task_bootrom(void)
{
int r;
char fdl_path[1024];
ssize_t s;
/* sprd version */
r = sprd_version();
if(r != 0) {
printf("sprd version error\n");
}else{
printf("sprd_version:%s\n",data_buffer+SPRD_FRAME_DATA_OFF);
}
/* sprd connect */
r = sprd_connect();
if(r != 0){
printf("sprd_connect failed\n");
return r;
}else{
printf("sprd_connect success\n");
}