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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [orpsocv2/] [sw/] [utils/] [bin2hex.c] - Blame information for rev 438

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

Line No. Rev Author Line
1 6 julius
/*$$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 415 julius
        int padding = 0;
68 6 julius
        unsigned int image_size;
69
 
70
        if(argc < 3) {
71
          fprintf(stderr,"\n\tInsufficient options.\n");
72
          fprintf(stderr,"\tPlease specify, in this order: a binary file to\n");
73
          fprintf(stderr,"\tconvert and the number of bytes of data to putput\n");
74
          fprintf(stderr,"\tper line.\n");
75
          fprintf(stderr,"\tOptionally specify the option -size_word to output,\n");
76
          fprintf(stderr,"\tthe size of the image in the first 4 bytes. This is\n");
77
          fprintf(stderr,"\tused by some of the new OR1k bootloaders.\n\n");
78 415 julius
          fprintf(stderr,"\tOptionally specify padding to be applied to the file,\n");
79
          fprintf(stderr,"\twith the -pad switch, followed by either decimal or\n");
80
          fprintf(stderr,"\thexademical format offset. Value is in bytes, pad\n");
81
          fprintf(stderr,"\tfill will be zeros.\n");
82
          fprintf(stderr,"\n");
83 6 julius
          exit(1);
84
        }
85
 
86 415 julius
 
87
        if (argc >= 4)
88 6 julius
          {
89 415 julius
            for (i = 1; i< argc; i++)
90
              {
91
                if ((strcmp("-pad", argv[i]) == 0) ||
92
                    (strcmp("--pad", argv[i]) == 0))
93
                  {
94
                    if (i+1 < argc)
95
                      {
96
                          if (1 == (sscanf(argv[i+1], "0x%x", &padding)))
97
                            i++;
98
                          else if (1 == (sscanf(argv[i+1], "%d", &padding)))
99
                            i++;
100
 
101
                          //fprintf(stderr,"Padding offset: 0x%x\n",padding);
102
                      }
103
 
104
                  }
105
              }
106
 
107
            // This will always be in argv[3]
108 6 julius
            if (strcmp("-size_word", argv[3]) == 0)
109
              // We will calculate the number of bytes first
110
              write_size_word=1;
111 415 julius
 
112 6 julius
          }
113 415 julius
 
114 6 julius
        fd = fopen( argv[filename_index], "r" );
115
 
116
        bytes_per_line = atoi(argv[bytes_per_line_index]);
117
 
118
        if ((bytes_per_line == 0) || (bytes_per_line > 8))
119
          {
120 415 julius
            fprintf(stderr,"bytes per line incorrect or missing: %s\n",
121
                    argv[bytes_per_line_index]);
122 6 julius
            exit(1);
123
          }
124
 
125
        // Now subtract 1 from bytes_per_line
126
        //if (bytes_per_line == 2)
127
        //  bytes_per_line--;
128
 
129
        if (fd == NULL) {
130
                fprintf(stderr,"failed to open input file: %s\n",argv[1]);
131
                exit(1);
132
        }
133
 
134 415 julius
 
135
        i=0;
136
 
137
            // Write out padding bytes amount of zeros
138
        while (padding) {
139
          printf("00");
140
          if (++i == bytes_per_line) {
141
            printf("\n");
142
            i = 0;
143
          }
144
          padding--;
145
        }
146
 
147 6 julius
        if (write_size_word)
148
          {
149 415 julius
            // or1200 startup method of determining size of boot image we're 
150
            // copying by reading out the very first word in flash is used. 
151
            // Determine the length of this file
152 6 julius
            fseek(fd, 0, SEEK_END);
153
            image_size = ftell(fd);
154
            fseek(fd,0,SEEK_SET);
155
 
156 415 julius
            // Now we should have the size of the file in bytes. Let's ensure 
157
            // it's a word multiple
158 6 julius
            image_size+=3;
159
            image_size &= 0xfffffffc;
160
 
161
            // Sanity check on image size
162
            if (image_size < 8){
163
              fprintf(stderr, "Bad binary image. Size too small\n");
164
              return 1;
165
            }
166
 
167
            // Now write out the image size
168
            i=0;
169
            printf("%.2x",(image_size >> 24) & 0xff);
170
            if(++i==bytes_per_line){ printf("\n"); i=0; }
171
            printf("%.2x",(image_size >> 16) & 0xff);
172
            if(++i==bytes_per_line){ printf("\n"); i=0; }
173
            printf("%.2x",(image_size >> 8) & 0xff);
174
            if(++i==bytes_per_line){ printf("\n"); i=0; }
175
            printf("%.2x",(image_size) & 0xff);
176
            if(++i==bytes_per_line){ printf("\n"); i=0; }
177
          }
178
 
179 415 julius
        // Fix for the current bootloader software! Skip the first 4 bytes of 
180
        // application data. Hopefully it's not important. 030509 -- jb
181 6 julius
        for(i=0;i<4;i++)
182
          c=fgetc(fd);
183
 
184
        i=0;
185
 
186
        // Now write out the binary data to hex format
187
        while ((c = fgetc(fd)) != EOF) {
188
                printf("%.2x", (unsigned int) c);
189
                if (++i == bytes_per_line) {
190
                        printf("\n");
191
                        i = 0;
192
                }
193
        }
194
 
195
        return 0;
196
}

powered by: WebSVN 2.1.0

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