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

Subversion Repositories minsoc

[/] [minsoc/] [trunk/] [sw/] [utils/] [bin2hex.c] - Diff between revs 2 and 141

Only display areas with differences | Details | Blame | View Log

Rev 2 Rev 141
/*$$HEADER*/
/*$$HEADER*/
/******************************************************************************/
/******************************************************************************/
/*                                                                            */
/*                                                                            */
/*                    H E A D E R   I N F O R M A T I O N                     */
/*                    H E A D E R   I N F O R M A T I O N                     */
/*                                                                            */
/*                                                                            */
/******************************************************************************/
/******************************************************************************/
 
 
// Project Name                   : ORPSoC v2
// Project Name                   : ORPSoC v2
// File Name                      : bin2hex.c
// File Name                      : bin2hex.c
// Prepared By                    : 
// Prepared By                    : 
// Project Start                  : 
// Project Start                  : 
 
 
/*$$COPYRIGHT NOTICE*/
/*$$COPYRIGHT NOTICE*/
/******************************************************************************/
/******************************************************************************/
/*                                                                            */
/*                                                                            */
/*                      C O P Y R I G H T   N O T I C E                       */
/*                      C O P Y R I G H T   N O T I C E                       */
/*                                                                            */
/*                                                                            */
/******************************************************************************/
/******************************************************************************/
/*
/*
  This library is free software; you can redistribute it and/or
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation;
  License as published by the Free Software Foundation;
  version 2.1 of the License, a copy of which is available from
  version 2.1 of the License, a copy of which is available from
  http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
  http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
 
 
  This library is distributed in the hope that it will be useful,
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  Lesser General Public License for more details.
 
 
  You should have received a copy of the GNU Lesser General Public
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
*/
 
 
/*$$DESCRIPTION*/
/*$$DESCRIPTION*/
/******************************************************************************/
/******************************************************************************/
/*                                                                            */
/*                                                                            */
/*                           D E S C R I P T I O N                            */
/*                           D E S C R I P T I O N                            */
/*                                                                            */
/*                                                                            */
/******************************************************************************/
/******************************************************************************/
//
//
// Generates basic ASCII hex output to stdout from binary file input
// Generates basic ASCII hex output to stdout from binary file input
// Compile and run the program with no options for usage.
// Compile and run the program with no options for usage.
//
//
 
// Modified by R. Diez in 2011 so that, when using option -size_word,
 
// padding zeroes are eventually appended, so that the length of
 
// the resulting file matches the length written in the header.
 
 
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
/* Number of bytes before line is broken
/* Number of bytes before line is broken
   For example if target flash is 8 bits wide,
   For example if target flash is 8 bits wide,
   define BREAK as 1. If it is 16 bits wide,
   define BREAK as 1. If it is 16 bits wide,
   define it as 2 etc.
   define it as 2 etc.
*/
*/
#define BREAK 1
#define BREAK 1
 
 
int main(int argc, char **argv)
int main(int argc, char **argv)
{
{
 
 
        FILE  *fd;
        FILE  *fd;
        int c;
        int c;
        int i = 0;
        int i = 0;
        int write_size_word=0; // Disabled by default
        int write_size_word=0; // Disabled by default
        int filename_index=1;
        int filename_index=1;
        int bytes_per_line=1;
        int bytes_per_line=1;
        int bytes_per_line_index=2;
        int bytes_per_line_index=2;
        unsigned int image_size;
    unsigned int padding_size = 0;
 
 
        if(argc < 3) {
        if(argc < 3) {
          fprintf(stderr,"\n\tInsufficient options.\n");
          fprintf(stderr,"\n\tInsufficient options.\n");
          fprintf(stderr,"\tPlease specify, in this order: a binary file to\n");
          fprintf(stderr,"\tPlease specify, in this order: a binary file to\n");
          fprintf(stderr,"\tconvert and the number of bytes of data to putput\n");
          fprintf(stderr,"\tconvert and the number of bytes of data to output\n");
          fprintf(stderr,"\tper line.\n");
          fprintf(stderr,"\tper line.\n");
          fprintf(stderr,"\tOptionally specify the option -size_word to output,\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,"\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");
          fprintf(stderr,"\tused by some of the new OR1k bootloaders. Note that\n");
 
          fprintf(stderr,"\tpadding zeroes will be appended so that the image size\n");
 
      fprintf(stderr,"\tis a multiple of 4.\n\n");
          exit(1);
          exit(1);
        }
        }
 
 
        if(argc == 4)
        if(argc == 4)
          {
          {
            if (strcmp("-size_word", argv[3]) == 0)
            if (strcmp("-size_word", argv[3]) == 0)
              // We will calculate the number of bytes first
              // We will calculate the number of bytes first
              write_size_word=1;
              write_size_word=1;
          }
          }
 
 
        fd = fopen( argv[filename_index], "r" );
        fd = fopen( argv[filename_index], "r" );
 
 
        bytes_per_line = atoi(argv[bytes_per_line_index]);
        bytes_per_line = atoi(argv[bytes_per_line_index]);
 
 
        if ((bytes_per_line == 0) || (bytes_per_line > 8))
        if ((bytes_per_line == 0) || (bytes_per_line > 8))
          {
          {
            fprintf(stderr,"bytes per line incorrect or missing: %s\n",argv[bytes_per_line_index]);
            fprintf(stderr,"bytes per line incorrect or missing: %s\n",argv[bytes_per_line_index]);
            exit(1);
            exit(1);
          }
          }
 
 
        // Now subtract 1 from bytes_per_line
        // Now subtract 1 from bytes_per_line
        //if (bytes_per_line == 2)
        //if (bytes_per_line == 2)
        //  bytes_per_line--;
        //  bytes_per_line--;
 
 
        if (fd == NULL) {
        if (fd == NULL) {
                fprintf(stderr,"failed to open input file: %s\n",argv[1]);
                fprintf(stderr,"failed to open input file: %s\n",argv[1]);
                exit(1);
                exit(1);
        }
        }
 
 
        if (write_size_word)
        if (write_size_word)
          {
          {
 
        unsigned int image_size;
 
 
            // or1200 startup method of determining size of boot image we're copying by reading out
            // 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
            // the very first word in flash is used. Determine the length of this file
            fseek(fd, 0, SEEK_END);
            fseek(fd, 0, SEEK_END);
            image_size = ftell(fd);
            image_size = ftell(fd);
            fseek(fd,0,SEEK_SET);
            fseek(fd,0,SEEK_SET);
 
 
            // Now we should have the size of the file in bytes. Let's ensure it's a word multiple
            // Now we should have the size of the file in bytes. Let's ensure it's a word multiple
            image_size+=3;
        padding_size = ( 4 - (image_size % 4) ) % 4;
            image_size &= 0xfffffffc;
            image_size += padding_size;
 
 
            // Sanity check on image size
            // Sanity check on image size
            if (image_size < 8){
            if (image_size < 8){
              fprintf(stderr, "Bad binary image. Size too small\n");
              fprintf(stderr, "Bad binary image. Size too small\n");
              return 1;
              return 1;
            }
            }
 
 
            // Now write out the image size
            // Now write out the image size
            i=0;
            i=0;
            printf("%.2x",(image_size >> 24) & 0xff);
            printf("%.2x",(image_size >> 24) & 0xff);
            if(++i==bytes_per_line){ printf("\n"); i=0; }
            if(++i==bytes_per_line){ printf("\n"); i=0; }
            printf("%.2x",(image_size >> 16) & 0xff);
            printf("%.2x",(image_size >> 16) & 0xff);
            if(++i==bytes_per_line){ printf("\n"); i=0; }
            if(++i==bytes_per_line){ printf("\n"); i=0; }
            printf("%.2x",(image_size >> 8) & 0xff);
            printf("%.2x",(image_size >> 8) & 0xff);
            if(++i==bytes_per_line){ printf("\n"); i=0; }
            if(++i==bytes_per_line){ printf("\n"); i=0; }
            printf("%.2x",(image_size) & 0xff);
            printf("%.2x",(image_size) & 0xff);
            if(++i==bytes_per_line){ printf("\n"); i=0; }
            if(++i==bytes_per_line){ 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
        // 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++)
        for(i=0;i<4;i++)
          c=fgetc(fd);
          c=fgetc(fd);
 
 
        i=0;
        i=0;
 
 
        // Now write out the binary data to hex format
        // Now write out the binary data to hex format
        while ((c = fgetc(fd)) != EOF) {
        while ((c = fgetc(fd)) != EOF) {
                printf("%.2x", (unsigned int) c);
                printf("%.2x", (unsigned int) c);
                if (++i == bytes_per_line) {
                if (++i == bytes_per_line) {
                        printf("\n");
                        printf("\n");
                        i = 0;
                        i = 0;
                }
                }
        }
        }
 
 
 
    unsigned j;
 
    for ( j = 0; j < padding_size; ++j ) {
 
        // printf("Adding one padding byte.\n");
 
                printf("%.2x", 0);
 
                if (++i == bytes_per_line) {
 
                        printf("\n");
 
                        i = 0;
 
                }
 
    }
 
 
        return 0;
        return 0;
}
}
 
 

powered by: WebSVN 2.1.0

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