OpenCores
URL https://opencores.org/ocsvn/test_project/test_project/trunk

Subversion Repositories test_project

[/] [test_project/] [trunk/] [sw/] [utils/] [bin2hex.c] - Rev 31

Go to most recent revision | Compare with Previous | Blame | View Log

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Number of bytes before line is broken
   For example if target flash is 8 bits wide,
   define BREAK as 1. If it is 16 bits wide,
   define it as 2 etc.
*/
#define BREAK 1
 
int main(int argc, char **argv)
{
 
	FILE  *fd;
	int c;
	int i = 0;
	int write_size_word=1; // ON BY DEFAULT NOW!
	int filename_index=1;
	unsigned int image_size;
 
	if(argc < 2) {
		fprintf(stderr,"\n\tInsufficient options.\n");
		fprintf(stderr,"\tPlease specify binary file to convert.\n");
		fprintf(stderr,"\tOptionally specify the option -size_word to output,\n");
		fprintf(stderr,"\tthe size of the image in the first 4 bytes. This is\n");
		fprintf(stderr,"\tused by some of the new OR1k bootloaders.\n\n");
		exit(1);
	}
 
	if(argc == 3)
	  {
	    if (strcmp("-size_word", argv[1]) == 0)
	      {
		filename_index=2;
		write_size_word=1;
	      }
	    else if (strcmp("-size_word", argv[2]) == 0)
	      {
		filename_index=1;
		write_size_word=1;
	      }
	  }
 
	fd = fopen( argv[filename_index], "r" );
 
	if (fd == NULL) {
		fprintf(stderr,"failed to open input file: %s\n",argv[1]);
		exit(1);
	}
 
	if (write_size_word)
	  {
	    // or1200 startup method of determining size of boot image we're copying by reading out
	    // the very first word in flash is used. Determine the length of this file
	    fseek(fd, 0, SEEK_END);
	    image_size = ftell(fd);
	    fseek(fd,0,SEEK_SET);
 
	    // Now we should have the size of the file in bytes. Let's ensure it's a word multiple
	    image_size+=3;
	    image_size &= 0xfffffffc;
 
	    // Sanity check on image size
	    if (image_size < 8){ 
	      fprintf(stderr, "Bad binary image. Size too small\n");
	      return 1;
	    }
 
	    // Now write out the image size
	    i=0;
	    printf("%.2x",(image_size >> 24) & 0xff);
	    if(++i==BREAK){ printf("\n"); i=0; }
	    printf("%.2x",(image_size >> 16) & 0xff);
	    if(++i==BREAK){ printf("\n"); i=0; }
	    printf("%.2x",(image_size >> 8) & 0xff);
	    if(++i==BREAK){ printf("\n"); i=0; }
	    printf("%.2x",(image_size) & 0xff);
	    if(++i==BREAK){ printf("\n"); i=0; }
	  }
 
	// Fix for the current bootloader software! Skip the first 4 bytes of application data. Hopefully it's not important. 030509 -- jb
	for(i=0;i<4;i++)
	  c=fgetc(fd);
 
	i=0;
 
	// Now write out the binary data to hex format
	while ((c = fgetc(fd)) != EOF) {
		printf("%.2x", (unsigned int) c);
		if (++i == BREAK) {
			printf("\n");
			i = 0;
		}
        }
 
	return 0;
}	
 

Go to most recent revision | Compare with Previous | Blame | View Log

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.