root/tools/dumputil.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ptr_to_offset
  2. offset_to_ptr
  3. find_cstring
  4. find_word_aligned
  5. get_word
  6. deref_word_ptr
  7. deref_byte_ptr
  8. deref_string_ptr
  9. load_dump

   1 /*some utility functions for working with firmware dumps*/
   2 #include <stdio.h>
   3 #include <stdint.h>
   4 #include <string.h>
   5 #include <stdlib.h>
   6 #include <sys/stat.h>
   7 #include "dumputil.h"
   8 
   9 // translate a pointer found in the dump to an index in the dump
  10 unsigned ptr_to_offset(dump_t *dump,unsigned ptr) 
  11 {
  12         if(ptr < dump->base) // warn
  13                 return 0;
  14         return ptr - dump->base;
  15 }
  16 
  17 unsigned offset_to_ptr(dump_t *dump,unsigned off) 
  18 {
  19         if(off >= dump->size) // warn
  20                 return 0;
  21         return off + dump->base;
  22 }
  23 
  24 int find_cstring(dump_t *dump, unsigned *pos, const char *string)
  25 {
  26         unsigned i;
  27         for(i=*pos; i<dump->size - strlen(string); i++) {
  28                 if(strcmp((const char *)(dump->pb+i),string)==0) {
  29                         *pos = i;
  30                         return 1;
  31                 }
  32         }
  33         return 0;
  34 }
  35 
  36 int find_word_aligned(dump_t *dump, unsigned *index, uint32_t word)
  37 {
  38         unsigned i;
  39         for(i=*index; i<dump->words - 1; i++) {
  40                 if(dump->pw[i]==word) {
  41                         *index = i;
  42                         return 1;
  43                 }
  44         }
  45         return 0;
  46 }
  47 
  48 // get an arbitrary unaligned word
  49 uint32_t get_word(dump_t *dump,unsigned off)
  50 {
  51         if(off > dump->size) // warn
  52                 return 0;
  53         return *(unsigned *)(dump->pb+off);
  54 }
  55 
  56 // treat the value at off as a pointer in the dump, and return the word pointed to
  57 uint32_t deref_word_ptr(dump_t *dump,unsigned off)
  58 {
  59         uint32_t ptr=get_word(dump,off);
  60         return get_word(dump,ptr_to_offset(dump,ptr));
  61 }
  62 
  63 uint8_t deref_byte_ptr(dump_t *dump,unsigned off)
  64 {
  65         uint32_t ptr=get_word(dump,off);
  66         return dump->pb[ptr_to_offset(dump,ptr)];
  67 }
  68 
  69 const char* deref_string_ptr(dump_t *dump,unsigned off)
  70 {
  71         uint32_t ptr=get_word(dump,off);
  72         return (const char *)(dump->pb + ptr_to_offset(dump,ptr));
  73 }
  74 
  75 int load_dump(const char *dumpname,const char *base, dump_t *dump)
  76 {
  77         FILE *dumpfile;
  78         uint8_t *p;
  79         struct stat st;
  80         size_t rcnt;
  81 
  82         dump->base=strtoul(base,NULL,0);
  83         if(dump->base != 0xFFC00000 
  84         && dump->base != 0xFF810000 
  85         && dump->base != 0xFF000000 
  86         && dump->base != 0xF8000000
  87         && dump->base != 0xFC000000
  88         && dump->base != 0xE0000000) {
  89                 fprintf(stderr,"error base '%s' %x\n",base,dump->base);
  90                 return 0;
  91         }
  92 
  93     if (stat(dumpname,&st) != 0) {
  94         fprintf(stderr,"error: unable to stat %s\n",dumpname);
  95                 return 0;
  96         }
  97 
  98         if (st.st_size < MIN_DUMP_SIZE) {
  99         fprintf(stderr,"error: invalid dump %s\n",dumpname);
 100         return 0;
 101     }
 102 
 103     if ((p = malloc(st.st_size)) == NULL ) {
 104         fprintf(stderr,"error: unable to allocate %lu bytes\n",(unsigned long)st.st_size);
 105         return 0;
 106     }
 107 
 108     dumpfile = fopen(dumpname, "rb");
 109 
 110     if (dumpfile == NULL) {
 111         fprintf(stderr,"error: unable to open %s\n",dumpname);
 112                 free(p);
 113         return 0;
 114     }
 115 
 116     rcnt=fread(p, 1, st.st_size, dumpfile);
 117     fclose(dumpfile);
 118 
 119     if (rcnt != (size_t)st.st_size) {
 120         fprintf(stderr,"error: unable to read %s\n",dumpname);
 121                 free(p);
 122         return 0;
 123     }
 124         dump->pb=p;
 125         dump->size=st.st_size;
 126         dump->words = dump->size >> 2;
 127         // TODO is size is such that base + size > 0xFFFFFFFF should warn and trim size values
 128         // can also detect start sig ala dumpchk
 129         return 1;
 130 }
 131 

/* [<][>][^][v][top][bottom][index][help] */