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  * bootelf -- strip and reorganize the elf executable into format
00012  *            suitable for blasting into PROM.
00013  *
00014  * args:        infile  - the elf executable file
00015  *              outfile - name of file to write striped data to
00016  *
00017  * There is also a problem in that the constant initialized data follows the code
00018  * but the 'etext' symbol only reflects the end of the code.  Hence it is impossible
00019  * for the ROM code to know where the end of the constant initialized data ends so
00020  * it can copy the normal (non-constant) initialized data out to RAM.
00021  *
00022  *      [CODE SEGMENT][CONST INIT DATA]  [INIT DATA][UNINIT DATA]
00023  *      \_____________________________/  \______________________/
00024  *              READ ONLY                     WRITEABLE DATA
00025  *         Program Header #1                 Program Header #0
00026  *
00027  * Since the bootelf converter knows the size of Program Headers, we insert this
00028  * value into a known variable (reotext) where the rom code can read it and make
00029  * use of the value. (Yes this is an aweful hack!)
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          * For the BOOTROM, the code is compiled to 0xbfc00000 while the data is usually
00061          * compiled to address 0xa0000200 which is uncached DRAM or 0x00000200 which is
00062          * the cached DRAM.  This causes a problem as the elf headers appear to be ordering
00063          * in accending order (hence the data will appear before the code).  Thus we process
00064          * the headers in reverse order.
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 

Generated on Fri Nov 5 07:24:46 2010 for ejtag debug tools for loongson 232 cpu by  doxygen 1.5.6