📄 mx1-load.c
字号:
while(extprop){ if(!strcmp(extprop->key,key)) return extprop; extprop=extprop->next; } return NULL;}int uniload_extprop_get_ulong(uniload_alg_info_t *algi, char *key, unsigned long *pval){ unsigned long val; char *p; uniload_alg_extprop_t *extprop; extprop=uniload_extprop_find(algi,key); if(!extprop) return 0; p=extprop->val; if(si_long(&p,&val,0)>0){ *pval=val; return 1; }else{ return -1; }}int extprop_parse2list(uniload_alg_extprop_t **extprop_list, char *option){ uniload_alg_extprop_t *extprop; char *p; int key_len; p=strchr(option,'='); if(!p) return -1; key_len=p-option; extprop=malloc(sizeof(uniload_alg_extprop_t)); if(!extprop) return -2; memset(extprop,0,sizeof(uniload_alg_extprop_t)); extprop->key=malloc(key_len+1); if(!extprop->key){ free(extprop); return -1; } strncpy(extprop->key,option,key_len); extprop->key[key_len]=0; extprop->val=p+1; extprop->next=*extprop_list; *extprop_list=extprop; return 1;}/*----------------------------------------------------------------*/#ifndef __u8 #define __u8 uint8_t#endif#ifndef __u16 #define __u16 uint16_t#endif#ifndef __u32 #define __u32 uint32_t#endif#include "linux_setup.h"typedef struct atag_container { unsigned long base; uint8_t *data; unsigned long size; unsigned long capacity;} atag_container_t;typedef struct { char *name; uint32_t tag;} atag_std_names_t;atag_std_names_t atag_std_names[]={ {"none" , ATAG_NONE}, {"core" , ATAG_CORE}, {"mem" , ATAG_MEM}, {"videotext" , ATAG_VIDEOTEXT}, {"ramdisk" , ATAG_RAMDISK}, {"initrd" , ATAG_INITRD}, {"initrd2" , ATAG_INITRD2}, {"serial" , ATAG_SERIAL}, {"revision" , ATAG_REVISION}, {"videolfb" , ATAG_VIDEOLFB}, {"cmdline" , ATAG_CMDLINE}, {"acorn" , ATAG_ACORN}, {"memclk" , ATAG_MEMCLK}, {"memdata" , ATAG_MEMDATA}, {"memcpy" , ATAG_MEMCPY}, {"jump" , ATAG_JUMP}, {NULL , 0}};void *atag_add_space(atag_container_t *container, uint32_t bytes){ uint32_t *p; bytes=(bytes+3)&~3; long size=container->size+bytes; long capacity; if(size>container->capacity){ capacity=size+0x100; if(container->data){ container->data=realloc(container->data,capacity); }else{ container->data=malloc(capacity); } container->capacity=capacity; } if(!container->data) return NULL; p=(uint32_t*)(container->data+container->size); memset(p,0,bytes); container->size=size; return p;}void *atag_add_head(atag_container_t *container, uint32_t size, uint32_t tag){ uint32_t *p; p=(uint32_t*)atag_add_space(container, size*4); if(!p) return NULL; p[0]=size; p[1]=tag; return p+2;}int atag_add_string_tag(atag_container_t *container, uint32_t tag, char *str){ void *p; int l=strlen(str)+1+8; p=atag_add_head(container, (l+3)>>2, tag); if(!p) return 0; strcpy(p,str); return 1;}int atag_parse_tag(char **ps, uint32_t *tag){ char *p, *r; int tlen; atag_std_names_t *a; long l; r=*ps; p=strchr(r,'='); if(!p) return -1; tlen=p-r; if(si_long(&r,&l,0)>0){ if((*r!='=') && *r) return -1; *tag=l; *ps=p+1; return 1; } r=*ps; for(a=atag_std_names;a->name;a++){ if(strlen(a->name)!=tlen) continue; if(memcmp(a->name,r,tlen)) continue; *tag=a->tag; *ps=p+1; return 1; } return -1;}int atag_parse_str(atag_container_t *container, char *option){ char *p=option; uint32_t tag; if(atag_parse_tag(&p, &tag)<0) return -1; return atag_add_string_tag(container, tag, p);}int atag_parse_u32(atag_container_t *container, char *option){ char *p=option; uint32_t tag; uint32_t *a, *b; long l; int i; if(atag_parse_tag(&p, &tag)<0) return -1; a=(uint32_t*)atag_add_head(container, 2, tag); if(!a) return -1; i=0; do{ if(!*p){ a[-2]+=i; return 1; } if(si_long(&p,&l,0)<0) break; b=(uint32_t*)atag_add_space(container, 4); *b=l; i++; if(*p==',') p++; }while(1); return -1;}int atag_container_flg;atag_container_t atag_container;/*----------------------------------------------------------------*/static voidusage(void){ printf("usage: flashmsc <parameters> <send_file>\n"); printf(" -H, --hwlink <name> select target connection HW type [%s]\n",mx1_hwlink); printf(" -d, --sdev <name> name of RS232 device [%s]\n",mx1_sdev); printf(" -B, --baud <num> RS232 baudrate [%d]\n",mx1_baud); printf(" -m, --mem-dump dump memory range defined by -s -l -t\n"); printf(" -s, --start <addr> start address of transfer\n"); printf(" -l, --length <num> length of upload block\n"); printf(" -O, --mem-offs <num> address offset\n"); printf(" -t, --mem-type <num> select memory type\n"); printf(" -g, --go <addr> start program from address\n"); printf(" -G, --go-entry start program from prog. entry\n"); printf(" -V, --version show version\n"); printf(" -h, --help this usage screen\n"); printf(" -E, --mass-erase <mode> full or mode controlled device erase\n"); printf(" -e, --erase erase region defined by -s -l\n"); printf(" -F, --fill <num> fill region defined by -s -l by value\n"); printf(" -f, --format <format> format of data file\n"); printf(" -P, --prop <name>=<val> extended properties\n"); printf(" -A, --atag-str <id>=<s> add ATAG string\n"); printf(" -a, --atag-u32 <id>=<a> add ATAG array of longs\n"); printf(" -S, --atag-base <addr> base address of ATAG list\n");/* printf(" -X, --xtal-hz <num> target crystal frequency\n"); printf(" -c, --command <num> numeric command value\n"); printf(" -b, --blockmode <num> block size\n"); printf(" -w, --wait-reply <num> time to wait for reply in ms\n"); printf(" -r, --reset reset before download\n"); printf(" -b, --break send communication break character\n"); printf(" -u, --upload upload memory block [download]\n"); printf(" -f, --format <format> format of data file [intelhex]\n");*/}int main(int argc, char **argv) { int res; /*char ch;*/ uniload_alg_info_t algi_buff; uniload_alg_info_t *algi=&algi_buff; static struct option long_opts[] = { { "hwlink", 1, 0, 'H' }, { "sdev", 1, 0, 'd' }, { "udev", 1, 0, 'd' }, { "baud", 1, 0, 'B' }, { "command", 1, 0, 'c' }, { "blockmode",1, 0, 'b' }, { "wait-reply",1,0, 'w' }, { "mass-erase",1,0, 'E' }, { "erase", 0, 0, 'e' }, { "fill", 1, 0, 'F' }, { "mem-dump", 0, 0, 'm' }, { "mem-offs", 1, 0, 'O' }, { "mem-type", 1, 0, 't' }, { "start", 1, 0, 's' }, { "length", 1, 0, 'l' }, { "go", 1, 0, 'g' }, { "go-entry", 1, 0, 'G' }, { "reset", 0, 0, 'r' }, { "break", 0, 0, 'k' }, { "upload", 0, 0, 'u' }, { "format", 1, 0, 'f' }, { "prop", 1, 0, 'P' }, { "atag-str", 1, 0, 'A' }, { "atag-u32", 1, 0, 'a' }, { "atag-base",1, 0, 'S' }, { "version", 0, 0, 'V' }, { "help", 0, 0, 'h' }, { "xtal-hz", 1, 0, 'X' }, { 0, 0, 0, 0} }; int opt; memset(algi,0,sizeof(*algi)); #ifndef HAS_GETOPT_LONG while ((opt = getopt(argc, argv, "H:d:B:c:b:w:E:eF:mt:s:l:O:g:Grkuf:P:A:a:S:VhD:X:")) != EOF) #else while ((opt = getopt_long(argc, argv, "H:d:B:c:b:w:E:eF:mt:s:l:O:g:Grkuf:P:A:a:S:VhD:X:", &long_opts[0], NULL)) != EOF) #endif switch (opt) { case 'H': mx1_hwlink=optarg; break; case 'd': mx1_sdev=optarg; break; case 'B': mx1_baud = strtol(optarg,NULL,0); break; case 'X': mx1_xhz = strtol(optarg,NULL,0); break; case 'c': if(optarg[0]=='B'){ command=0; break; } command = strtol(optarg,NULL,0); break; case 'b': blockmode = strtol(optarg,NULL,0); break; case 'w': mx1_waitrep = strtol(optarg,NULL,0)*1000; break; case 'E': masserase_end = strtol(optarg,NULL,0); masserase_flg=1; break; case 'e': regerase_flg=1; break; case 'F': fill_flg=1; if(add_to_arr((void**)&fill_pat_val,&fill_pat_len,0,optarg)<0){ fprintf(stderr,"%s: incorrect patern data \"%s\"\n",argv[0],optarg); exit(2); } if(!fill_pat_len){ fprintf(stderr,"%s: incorrect patern data - empty value\n",argv[0]); exit(2); } break; case 'm': dump_flg=1; break; case 't': mem_type=strtoul(optarg,NULL,0); break; case 's': mem_start=strtoul(optarg,NULL,0); break; case 'l': mem_length=strtoul(optarg,NULL,0); break; case 'O': mem_offs=strtoul(optarg,NULL,0); break; case 'g': go_addr=strtoul(optarg,NULL,0); case 'G': go_flg=1; break; case 'r': reset_flg=1; break; case 'k': break_flg=1; break; case 'u': upload_flg=1; break; case 'f': file_format=optarg; break; case 'P': if(extprop_parse2list(&extprop_list, optarg)<0){ fprintf(stderr,"%s: incorrect extended property %s\n",argv[0],optarg); exit(2); } break; case 'A': if(atag_parse_str(&atag_container, optarg)<0){ fprintf(stderr,"%s: incorrect atag string %s\n",argv[0],optarg); exit(2); } break; case 'a': if(atag_parse_u32(&atag_container, optarg)<0){ fprintf(stderr,"%s: incorrect atag u32 %s\n",argv[0],optarg); exit(2); } break; case 'S': atag_container_flg=1; atag_container.base=strtoul(optarg,NULL,0); break; case 'V': fputs("tohit pre alpha\n", stdout); exit(0); case 'h': default: usage(); exit(opt == 'h' ? 0 : 1); } if(atag_container_flg){ atag_add_space(&atag_container, 8); } if(!strcmp(mx1_hwlink,"tagimage")){ res=mx1_tagimage_open(algi, mx1_sdev, 0); if(res<0){ fprintf(stderr,"TAGIMAGE open failed\n"); return -1; } }else if(!strcmp(mx1_hwlink,"usb")){ res=mx1_usb_open(algi, mx1_sdev); if(res<0){ fprintf(stderr,"USB open failed\n"); return -1; } }else{ res=mx1_rs232_open(algi, mx1_sdev, mx1_baud, 0, 0); if(res<0){ fprintf(stderr,"RS-232 open failed\n"); return -1; } } algi->extprop=extprop_list; algi->def_mem_type=mem_type; algi->mem_offs=mem_offs; if(!algi->max_blksize){ algi->max_blksize=512; } /*mx1_xhz*/ if(uniload_setup(algi)<0){ fprintf(stderr,"Target setup failed\n"); close(algi->dev_fd); return -1; } sleep(1); tcflush(algi->dev_fd, TCIOFLUSH); if(reset_flg){ } if(masserase_flg){ if(uniload_masserase(algi,masserase_end)<0){ fprintf(stderr,"Mass Erase failed\n"); close(mx1_fd); return -1; } } if(regerase_flg){ res=uniload_regerase(algi, mem_start, mem_length); if(res<0){ fprintf(stderr,"mx1_regerase: returned %d\n",res); exit(1); } } if(fill_flg) { uniload_mem_fill(algi, mem_type, mem_start, mem_length?mem_length:fill_pat_len, fill_pat_val, fill_pat_len); } if(!upload_flg){ while(optind<argc) { uniload_load_use_lma=1; if(uniload_download_file(algi,argv[optind],file_format,NULL,&entry_addr)<0){ fprintf(stderr,"Error to download file %s\n", argv[optind]); exit(1); } optind++; } }else if(upload_flg&&(optind<argc)){ if(optind+1!=argc) { printf("upload_file : needs exactly one filename\n"); exit(1); }; if(uniload_upload_file(algi, argv[optind], file_format, mem_type, mem_start, mem_length)<0){ exit(2); } } if(dump_flg){ res=uniload_mem_dump(algi, mem_type, mem_start, mem_length); if(res<0){ fprintf(stderr,"uniload_mem_dump: returned %d\n",res); exit(1); } } if(atag_container_flg){ res=uniload_mem_write(algi, mem_type, atag_container.base, atag_container.size, atag_container.data); if(res<0){ fprintf(stderr,"uniload_mem_write: returned %d for ATAG list\n",res); exit(1); } } if(go_flg){ if(go_addr) uniload_go_addr(algi, go_addr); else uniload_go_addr(algi, entry_addr); } uniload_release(algi); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -