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

Subversion Repositories minsoc

[/] [minsoc/] [trunk/] [sw/] [utils/] [bin2hex.c] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 rfajardo
/*$$HEADER*/
2
/******************************************************************************/
3
/*                                                                            */
4
/*                    H E A D E R   I N F O R M A T I O N                     */
5
/*                                                                            */
6
/******************************************************************************/
7
 
8
// Project Name                   : ORPSoC v2
9
// File Name                      : bin2hex.c
10
// Prepared By                    : 
11
// Project Start                  : 
12
 
13
/*$$COPYRIGHT NOTICE*/
14
/******************************************************************************/
15
/*                                                                            */
16
/*                      C O P Y R I G H T   N O T I C E                       */
17
/*                                                                            */
18
/******************************************************************************/
19
/*
20
  This library is free software; you can redistribute it and/or
21
  modify it under the terms of the GNU Lesser General Public
22
  License as published by the Free Software Foundation;
23
  version 2.1 of the License, a copy of which is available from
24
  http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
25
 
26
  This library is distributed in the hope that it will be useful,
27
  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29
  Lesser General Public License for more details.
30
 
31
  You should have received a copy of the GNU Lesser General Public
32
  License along with this library; if not, write to the Free Software
33
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34
*/
35
 
36
/*$$DESCRIPTION*/
37
/******************************************************************************/
38
/*                                                                            */
39
/*                           D E S C R I P T I O N                            */
40
/*                                                                            */
41
/******************************************************************************/
42
//
43
// Generates basic ASCII hex output to stdout from binary file input
44
// Compile and run the program with no options for usage.
45
//
46
 
47
#include <stdio.h>
48
#include <stdlib.h>
49
#include <string.h>
50
/* Number of bytes before line is broken
51
   For example if target flash is 8 bits wide,
52
   define BREAK as 1. If it is 16 bits wide,
53
   define it as 2 etc.
54
*/
55
#define BREAK 1
56
 
57
int main(int argc, char **argv)
58
{
59
 
60
        FILE  *fd;
61
        int c;
62
        int i = 0;
63
        int write_size_word=0; // Disabled by default
64
        int filename_index=1;
65
        int bytes_per_line=1;
66
        int bytes_per_line_index=2;
67
        unsigned int image_size;
68
 
69
        if(argc < 3) {
70
          fprintf(stderr,"\n\tInsufficient options.\n");
71
          fprintf(stderr,"\tPlease specify, in this order: a binary file to\n");
72
          fprintf(stderr,"\tconvert and the number of bytes of data to putput\n");
73
          fprintf(stderr,"\tper line.\n");
74
          fprintf(stderr,"\tOptionally specify the option -size_word to output,\n");
75
          fprintf(stderr,"\tthe size of the image in the first 4 bytes. This is\n");
76
          fprintf(stderr,"\tused by some of the new OR1k bootloaders.\n\n");
77
          exit(1);
78
        }
79
 
80
        if(argc == 4)
81
          {
82
            if (strcmp("-size_word", argv[3]) == 0)
83
              // We will calculate the number of bytes first
84
              write_size_word=1;
85
          }
86
 
87
        fd = fopen( argv[filename_index], "r" );
88
 
89
        bytes_per_line = atoi(argv[bytes_per_line_index]);
90
 
91
        if ((bytes_per_line == 0) || (bytes_per_line > 8))
92
          {
93
            fprintf(stderr,"bytes per line incorrect or missing: %s\n",argv[bytes_per_line_index]);
94
            exit(1);
95
          }
96
 
97
        // Now subtract 1 from bytes_per_line
98
        //if (bytes_per_line == 2)
99
        //  bytes_per_line--;
100
 
101
        if (fd == NULL) {
102
                fprintf(stderr,"failed to open input file: %s\n",argv[1]);
103
                exit(1);
104
        }
105
 
106
        if (write_size_word)
107
          {
108
            // or1200 startup method of determining size of boot image we're copying by reading out
109
            // the very first word in flash is used. Determine the length of this file
110
            fseek(fd, 0, SEEK_END);
111
            image_size = ftell(fd);
112
            fseek(fd,0,SEEK_SET);
113
 
114
            // Now we should have the size of the file in bytes. Let's ensure it's a word multiple
115
            image_size+=3;
116
            image_size &= 0xfffffffc;
117
 
118
            // Sanity check on image size
119
            if (image_size < 8){
120
              fprintf(stderr, "Bad binary image. Size too small\n");
121
              return 1;
122
            }
123
 
124
            // Now write out the image size
125
            i=0;
126
            printf("%.2x",(image_size >> 24) & 0xff);
127
            if(++i==bytes_per_line){ printf("\n"); i=0; }
128
            printf("%.2x",(image_size >> 16) & 0xff);
129
            if(++i==bytes_per_line){ printf("\n"); i=0; }
130
            printf("%.2x",(image_size >> 8) & 0xff);
131
            if(++i==bytes_per_line){ printf("\n"); i=0; }
132
            printf("%.2x",(image_size) & 0xff);
133
            if(++i==bytes_per_line){ printf("\n"); i=0; }
134
          }
135
 
136
        // Fix for the current bootloader software! Skip the first 4 bytes of application data. Hopefully it's not important. 030509 -- jb
137
        for(i=0;i<4;i++)
138
          c=fgetc(fd);
139
 
140
        i=0;
141
 
142
        // Now write out the binary data to hex format
143
        while ((c = fgetc(fd)) != EOF) {
144
                printf("%.2x", (unsigned int) c);
145
                if (++i == bytes_per_line) {
146
                        printf("\n");
147
                        i = 0;
148
                }
149
        }
150
 
151
        return 0;
152
}

powered by: WebSVN 2.1.0

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