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

Subversion Repositories minsoc

[/] [minsoc/] [branches/] [rc-1.0/] [sw/] [utils/] [bin2hex.c] - Blame information for rev 160

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 133 rfajardo
// Modified by R. Diez in 2011 so that, when using option -size_word,
47
// padding zeroes are eventually appended, so that the length of
48
// the resulting file matches the length written in the header.
49 2 rfajardo
 
50
#include <stdio.h>
51
#include <stdlib.h>
52
#include <string.h>
53
/* Number of bytes before line is broken
54
   For example if target flash is 8 bits wide,
55
   define BREAK as 1. If it is 16 bits wide,
56
   define it as 2 etc.
57
*/
58
#define BREAK 1
59
 
60
int main(int argc, char **argv)
61
{
62
 
63
        FILE  *fd;
64
        int c;
65
        int i = 0;
66
        int write_size_word=0; // Disabled by default
67
        int filename_index=1;
68
        int bytes_per_line=1;
69
        int bytes_per_line_index=2;
70 133 rfajardo
    unsigned int padding_size = 0;
71 2 rfajardo
 
72
        if(argc < 3) {
73
          fprintf(stderr,"\n\tInsufficient options.\n");
74
          fprintf(stderr,"\tPlease specify, in this order: a binary file to\n");
75 133 rfajardo
          fprintf(stderr,"\tconvert and the number of bytes of data to output\n");
76 2 rfajardo
          fprintf(stderr,"\tper line.\n");
77 133 rfajardo
          fprintf(stderr,"\tOptionally specify the option -size_word to output\n");
78 2 rfajardo
          fprintf(stderr,"\tthe size of the image in the first 4 bytes. This is\n");
79 133 rfajardo
          fprintf(stderr,"\tused by some of the new OR1k bootloaders. Note that\n");
80
          fprintf(stderr,"\tpadding zeroes will be appended so that the image size\n");
81
      fprintf(stderr,"\tis a multiple of 4.\n\n");
82 2 rfajardo
          exit(1);
83
        }
84
 
85
        if(argc == 4)
86
          {
87
            if (strcmp("-size_word", argv[3]) == 0)
88
              // We will calculate the number of bytes first
89
              write_size_word=1;
90
          }
91
 
92
        fd = fopen( argv[filename_index], "r" );
93
 
94
        bytes_per_line = atoi(argv[bytes_per_line_index]);
95
 
96
        if ((bytes_per_line == 0) || (bytes_per_line > 8))
97
          {
98
            fprintf(stderr,"bytes per line incorrect or missing: %s\n",argv[bytes_per_line_index]);
99
            exit(1);
100
          }
101
 
102
        // Now subtract 1 from bytes_per_line
103
        //if (bytes_per_line == 2)
104
        //  bytes_per_line--;
105
 
106
        if (fd == NULL) {
107
                fprintf(stderr,"failed to open input file: %s\n",argv[1]);
108
                exit(1);
109
        }
110
 
111
        if (write_size_word)
112
          {
113 133 rfajardo
        unsigned int image_size;
114
 
115 2 rfajardo
            // or1200 startup method of determining size of boot image we're copying by reading out
116
            // the very first word in flash is used. Determine the length of this file
117
            fseek(fd, 0, SEEK_END);
118
            image_size = ftell(fd);
119
            fseek(fd,0,SEEK_SET);
120
 
121
            // Now we should have the size of the file in bytes. Let's ensure it's a word multiple
122 133 rfajardo
        padding_size = ( 4 - (image_size % 4) ) % 4;
123
            image_size += padding_size;
124 2 rfajardo
 
125
            // Sanity check on image size
126
            if (image_size < 8){
127
              fprintf(stderr, "Bad binary image. Size too small\n");
128
              return 1;
129
            }
130
 
131
            // Now write out the image size
132
            i=0;
133
            printf("%.2x",(image_size >> 24) & 0xff);
134
            if(++i==bytes_per_line){ printf("\n"); i=0; }
135
            printf("%.2x",(image_size >> 16) & 0xff);
136
            if(++i==bytes_per_line){ printf("\n"); i=0; }
137
            printf("%.2x",(image_size >> 8) & 0xff);
138
            if(++i==bytes_per_line){ printf("\n"); i=0; }
139
            printf("%.2x",(image_size) & 0xff);
140
            if(++i==bytes_per_line){ printf("\n"); i=0; }
141
          }
142
 
143
        // Fix for the current bootloader software! Skip the first 4 bytes of application data. Hopefully it's not important. 030509 -- jb
144
        for(i=0;i<4;i++)
145
          c=fgetc(fd);
146
 
147
        i=0;
148
 
149
        // Now write out the binary data to hex format
150
        while ((c = fgetc(fd)) != EOF) {
151
                printf("%.2x", (unsigned int) c);
152
                if (++i == bytes_per_line) {
153
                        printf("\n");
154
                        i = 0;
155
                }
156
        }
157
 
158 133 rfajardo
    unsigned j;
159
    for ( j = 0; j < padding_size; ++j ) {
160
        // printf("Adding one padding byte.\n");
161
                printf("%.2x", 0);
162
                if (++i == bytes_per_line) {
163
                        printf("\n");
164
                        i = 0;
165
                }
166
    }
167
 
168 2 rfajardo
        return 0;
169
}

powered by: WebSVN 2.1.0

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