putelf.c
Go to the documentation of this file.00001 #include <stdio.h>
00002 #include "elf.h"
00003
00004 static struct ehdr exec;
00005 static struct phdr prgm;
00006 static struct shdr sect;
00007 static struct sym symb;
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 int cmd_putelf(int argc,char **argv)
00032 {
00033 FILE *in, *out;
00034 int i;
00035 long len;
00036 char tmpfile[100];
00037
00038 if(argv[0][0] == 'p') argv[0][3] = 0;
00039 else argv[0][4] = 0;
00040
00041 if( (in = fopen(argv[1], "rb")) == NULL ) {
00042 perror(argv[1]);
00043 return -1;
00044 }
00045
00046 strcpy(tmpfile,"/tmp/ejtagbin_XXXXXX");
00047 if(mktemp(tmpfile) == NULL)
00048 {
00049 perror(tmpfile);
00050 return -1;
00051 }
00052
00053
00054 if( fread(&exec, sizeof(exec), 1, in) == 0 ) {
00055 fprintf(stderr, "fread fails\n");
00056 return -3;
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066 for ( i =0 ; i < exec.phcount; i++ ) {
00067 char *buf;
00068
00069 fseek(in,exec.phoff + i* exec.phsize,SEEK_SET);
00070
00071 if( fread(&prgm, sizeof(prgm), 1, in) == 0 ) {
00072 fprintf(stderr, "fread fails\n");
00073 return -3;
00074 }
00075
00076 if(prgm.type != PT_LOAD)
00077 continue;
00078
00079 if( (out = fopen(&tmpfile, "wb")) == NULL ) {
00080 perror(&tmpfile);
00081 fclose(in);
00082 return -2;
00083 }
00084
00085 len = prgm.filesz;
00086 fseek(in, prgm.offset , SEEK_SET);
00087 buf = malloc(len);
00088 fread(buf,len,1,in);
00089 fwrite(buf,len,1,out);
00090 fclose(out);
00091 sprintf(buf,"%s %s 0x%x",argv[0],tmpfile,prgm.vaddr|0xa0000000);
00092 printf("%s\n",buf);
00093 do_cmd(buf);
00094 unlink(&tmpfile);
00095 free(buf);
00096 }
00097
00098 fclose(in);
00099
00100 ui_datamem[0] = exec.entry;
00101
00102 tgt_exec(\
00103 "1:mtc0 $2, $31;\n" \
00104 "lui $2,0xFF20;\n" \
00105 "lw $2, ($2);\n" \
00106 "mtc0 $2, $24;\n" \
00107 "mfc0 $2, $31;\n" \
00108 "b 1b;\n" \
00109 "nop;\n" \
00110 ,0x8000);
00111
00112 return 0;
00113 }
00114
00115 mycmd_init(putelf,cmd_putelf,"putelf filename","put kernel");
00116 mycmd_init(fputelf,cmd_putelf,"fputelf filename","put kernel");
00117 mycmd_init(sputelf,cmd_putelf,"sputelf filename","put kernel");
00118