Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <string.h>
- #include <errno.h>
- #include <signal.h>
- #include <ctype.h>
- #include <time.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <sys/types.h>
- #include <sys/mman.h>
- #include <math.h>
- #include <limits.h>
- char quads[16][5];
- void fill_quads() {
- sprintf(quads[0], "0000");
- sprintf(quads[1], "0001");
- sprintf(quads[2], "0010");
- sprintf(quads[3], "0011");
- sprintf(quads[4], "0100");
- sprintf(quads[5], "0101");
- sprintf(quads[6], "0110");
- sprintf(quads[7], "0111");
- sprintf(quads[8], "1000");
- sprintf(quads[9], "1001");
- sprintf(quads[10], "1010");
- sprintf(quads[11], "1011");
- sprintf(quads[12], "1100");
- sprintf(quads[13], "1101");
- sprintf(quads[14], "1110");
- sprintf(quads[15], "1111");
- }
- #define BASE_ADDR 0x40000000
- int main ( ) {
- FILE *binfile, *dumphex, *dumpbin;
- volatile unsigned int *pointer;
- unsigned long int address, devmem, memsize, offset, counter, counter2, m, n;
- unsigned char *b;
- unsigned char tmp[3];
- unsigned char buf[8];
- fill_quads();
- address = BASE_ADDR;
- memsize = 1024;
- devmem = open("/dev/mem", O_RDWR | O_SYNC);
- if (devmem < 0) {
- printf("= ERROR: Opening of /dev/mem failed with (%d) %s\n", errno,
- strerror(errno));
- return -1;
- }
- pointer = (unsigned int *)mmap( 0, memsize, PROT_READ|PROT_WRITE, MAP_SHARED, devmem, address);
- if (pointer == (unsigned int *)MAP_FAILED) {
- printf("= ERROR: Could not map registers - (%d) %s", errno, strerror(errno));
- close(devmem);
- return 1;
- }
- printf("SUCCESS : Registers mapping - address = 0x%08x\n memsize = %d\n", address, memsize);
- if((binfile = fopen("file.bin", "wb")) == NULL) {
- printf ( "= ERROR : Cannot create the binary output file\n");
- return 1;
- }
- if( fwrite(pointer, memsize, 1, binfile) != 1) {
- printf ( "= ERROR : Cannot write to binary output file\n");
- return 1;
- } else printf("SUCCESS : Binary output file - file.bin\n");
- fclose(binfile);
- binfile = fopen ("file.bin", "rb" );
- if ( binfile == NULL ) {
- printf ( "= ERROR : Cannot open the binary input file\n");
- return 1;
- }
- dumphex = fopen ("dumphex.txt", "w" );
- if ( dumphex == NULL ) {
- printf ( "= ERROR : Cannot create the hexdump output file\n");
- return 1;
- }
- dumpbin = fopen ("dumpbin.txt", "w" );
- if ( dumpbin == NULL ) {
- printf ( "= ERROR : Cannot create the bindump output file\n");
- return 1;
- }
- fprintf ( dumphex, " Address Hexadecimal values Printable\n" );
- fprintf ( dumphex, "-------------- ----------------------- ---------\n" );
- fprintf ( dumpbin, " Address Binary values Printable\n" );
- fprintf ( dumpbin, "-------------- ----------------------------------------------------------------------- ---------\n" );
- offset = 0;
- while ( ( counter = ( long )
- fread ( buf, 1, 8, binfile ) ) > 0 ) {
- b = buf;
- fprintf ( dumphex, "%5d %08lx ", (int) offset, offset );
- fprintf ( dumpbin, "%5d %08lx ", (int) offset, offset );
- offset = offset + 8;
- counter2 = 0;
- for ( m = 0; m < 8; m++ ) {
- counter2 = counter2 + 1;
- if ( counter2 <= counter ) {
- sprintf (tmp, "%02x", *b++);
- if (tmp[0] >= '0' && tmp[0] <= '9') fprintf(dumpbin, "%s", quads[ tmp[0] - '0']);
- if (tmp[0] >= 'a' && tmp[0] <= 'f') fprintf(dumpbin, "%s", quads[10 + tmp[0] - 'a']);
- if (tmp[1] >= '0' && tmp[1] <= '9') fprintf(dumpbin, "%s", quads[ tmp[1] - '0']);
- if (tmp[1] >= 'a' && tmp[1] <= 'f') fprintf(dumpbin, "%s", quads[10 + tmp[1] - 'a']);
- fprintf (dumphex, "%s", tmp);
- }
- else {
- fprintf ( dumphex, " " );
- fprintf ( dumpbin, " " );
- }
- fprintf ( dumphex, " " );
- fprintf ( dumpbin, " " );
- }
- fprintf ( dumphex, " " );
- fprintf ( dumpbin, " " );
- counter2 = 0;
- for ( n = 0; n < 8; n++ ) {
- counter2 = counter2 + 1;
- if ( counter2 <= counter ) {
- if ( ( buf[n] < 32 ) || ( buf[n] > 126 ) ) {
- fprintf ( dumphex, "%c", '.' );
- fprintf ( dumpbin, "%c", '.' );
- }
- else {
- fprintf ( dumphex, "%c", buf[n] );
- fprintf ( dumpbin, "%c", buf[n] );
- }
- }
- }
- fprintf( dumphex, "\n" );
- fprintf( dumpbin, "\n" );
- }
- printf("SUCCESS : Hexdump output file - dumphex.txt\n");
- printf("SUCCESS : Bindump output file - dumpbin.txt\n");
- fclose ( binfile );
- fclose ( dumphex );
- fclose ( dumpbin );
- munmap((void *)address, memsize);
- close(devmem);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement