Advertisement
MrRockchip

dump.c

Feb 15th, 2013 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.71 KB | None | 0 0
  1. /* */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <signal.h>
  8. #include <ctype.h>
  9. #include <time.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <termios.h>
  13. #include <sys/types.h>
  14. #include <sys/mman.h>
  15. #include <math.h>
  16. #include <limits.h>
  17.  
  18. char quads[16][5];
  19.  
  20. void fill_quads() {
  21.     sprintf(quads[0],  "0000");
  22.     sprintf(quads[1],  "0001");
  23.     sprintf(quads[2],  "0010");
  24.     sprintf(quads[3],  "0011");
  25.     sprintf(quads[4],  "0100");
  26.     sprintf(quads[5],  "0101");
  27.     sprintf(quads[6],  "0110");
  28.     sprintf(quads[7],  "0111");
  29.     sprintf(quads[8],  "1000");
  30.     sprintf(quads[9],  "1001");
  31.     sprintf(quads[10], "1010");
  32.     sprintf(quads[11], "1011");
  33.     sprintf(quads[12], "1100");
  34.     sprintf(quads[13], "1101");
  35.     sprintf(quads[14], "1110");
  36.     sprintf(quads[15], "1111");
  37. }
  38.  
  39. #define BASE_ADDR   0x40000000
  40.  
  41. int main ( ) {
  42.  
  43.     FILE *binfile, *dumphex, *dumpbin;
  44.     volatile unsigned int *pointer;
  45.     unsigned long int address, devmem, memsize, offset, counter, counter2, m, n;
  46.     unsigned char *b;
  47.     unsigned char tmp[3];
  48.     unsigned char buf[8];
  49.  
  50.     fill_quads();
  51.  
  52.     address = BASE_ADDR;
  53.     memsize = 1024;
  54.  
  55.     devmem = open("/dev/mem", O_RDWR | O_SYNC);
  56.  
  57.     if (devmem < 0) {
  58.     printf("= ERROR: Opening of /dev/mem failed with (%d) %s\n", errno,
  59.     strerror(errno));
  60.     return -1;
  61.     }
  62.  
  63.     pointer = (unsigned int *)mmap( 0, memsize, PROT_READ|PROT_WRITE, MAP_SHARED, devmem, address);
  64.  
  65.     if (pointer == (unsigned int *)MAP_FAILED) {
  66.     printf("= ERROR: Could not map registers - (%d) %s", errno, strerror(errno));
  67.     close(devmem);
  68.     return 1;
  69.     }
  70.  
  71.     printf("SUCCESS : Registers mapping   - address = 0x%08x\n                                memsize = %d\n", address, memsize);
  72.  
  73.     if((binfile = fopen("file.bin", "wb")) == NULL) {
  74.     printf ( "= ERROR : Cannot create the binary output file\n");
  75.     return 1;
  76.     }
  77.  
  78.     if( fwrite(pointer, memsize, 1, binfile) != 1) {
  79.     printf ( "= ERROR : Cannot write to binary output file\n");
  80.     return 1;
  81.     } else printf("SUCCESS : Binary output file  - file.bin\n");
  82.  
  83.     fclose(binfile);
  84.  
  85.     binfile = fopen ("file.bin", "rb" );
  86.  
  87.     if ( binfile == NULL ) {
  88.     printf ( "= ERROR : Cannot open the binary input file\n");
  89.     return 1;
  90.     }
  91.  
  92.     dumphex = fopen ("dumphex.txt", "w" );
  93.  
  94.     if ( dumphex == NULL ) {
  95.     printf ( "= ERROR : Cannot create the hexdump output file\n");
  96.     return 1;
  97.     }
  98.  
  99.     dumpbin = fopen ("dumpbin.txt", "w" );
  100.  
  101.     if ( dumpbin == NULL ) {
  102.     printf ( "= ERROR : Cannot create the bindump output file\n");
  103.     return 1;
  104.     }
  105.  
  106.     fprintf ( dumphex, "       Address    Hexadecimal values     Printable\n" );
  107.     fprintf ( dumphex, "--------------  -----------------------  ---------\n" );
  108.  
  109.     fprintf ( dumpbin, "       Address                               Binary values                               Printable\n" );
  110.     fprintf ( dumpbin, "--------------  -----------------------------------------------------------------------  ---------\n" );
  111.  
  112.     offset = 0;
  113.  
  114.     while ( ( counter = ( long )
  115.       fread ( buf, 1, 8, binfile ) ) > 0 ) {
  116.  
  117.     b = buf;
  118.  
  119.     fprintf ( dumphex, "%5d %08lx  ", (int) offset, offset );
  120.     fprintf ( dumpbin, "%5d %08lx  ", (int) offset, offset );
  121.     offset = offset + 8;
  122.  
  123.     counter2 = 0;
  124.     for ( m = 0; m < 8; m++ ) {  
  125.         counter2 = counter2 + 1;
  126.  
  127.         if ( counter2 <= counter ) {
  128.         sprintf (tmp, "%02x", *b++);
  129.         if (tmp[0] >= '0' && tmp[0] <= '9') fprintf(dumpbin, "%s", quads[     tmp[0] - '0']);
  130.         if (tmp[0] >= 'a' && tmp[0] <= 'f') fprintf(dumpbin, "%s", quads[10 + tmp[0] - 'a']);
  131.         if (tmp[1] >= '0' && tmp[1] <= '9') fprintf(dumpbin, "%s", quads[     tmp[1] - '0']);
  132.         if (tmp[1] >= 'a' && tmp[1] <= 'f') fprintf(dumpbin, "%s", quads[10 + tmp[1] - 'a']);
  133.         fprintf (dumphex, "%s", tmp);
  134.         }
  135.         else {
  136.         fprintf ( dumphex, "  " );
  137.         fprintf ( dumpbin, "  " );
  138.         }
  139.         fprintf ( dumphex, " " );
  140.         fprintf ( dumpbin, " " );
  141.     }
  142.     fprintf ( dumphex, " " );
  143.     fprintf ( dumpbin, " " );
  144.     counter2 = 0;
  145.     for ( n = 0; n < 8; n++ ) {
  146.         counter2 = counter2 + 1;
  147.  
  148.         if ( counter2 <= counter ) {
  149.         if ( ( buf[n] < 32 ) || ( buf[n] > 126 ) ) {
  150.             fprintf ( dumphex, "%c", '.' );
  151.             fprintf ( dumpbin, "%c", '.' );
  152.         }
  153.         else {
  154.             fprintf ( dumphex, "%c", buf[n] );
  155.             fprintf ( dumpbin, "%c", buf[n] );
  156.         }
  157.         }
  158.     }
  159.     fprintf( dumphex, "\n" );
  160.     fprintf( dumpbin, "\n" );
  161.     }
  162.  
  163.     printf("SUCCESS : Hexdump output file - dumphex.txt\n");
  164.     printf("SUCCESS : Bindump output file - dumpbin.txt\n");
  165.  
  166.     fclose ( binfile );
  167.     fclose ( dumphex );
  168.     fclose ( dumpbin );
  169.  
  170.     munmap((void *)address, memsize);
  171.     close(devmem);
  172.  
  173.     return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement