MrRockchip

muh_dumper.c

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