1 |
349 |
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 :
|
9 |
|
|
// File Name : bin2binsizeword.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 a binary file, but with the first word replaced as the size of the
|
44 |
|
|
// image in big endian format
|
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, *dest_fd;
|
61 |
|
|
int i = 0;
|
62 |
|
|
int n;
|
63 |
|
|
int filename_index=1;
|
64 |
|
|
int output_filename_index=2;
|
65 |
|
|
unsigned int image_size;
|
66 |
|
|
|
67 |
|
|
if(argc < 2)
|
68 |
|
|
{
|
69 |
|
|
fprintf(stderr,"Usage: bin2binsizeword INFILE OUTFILE\n");
|
70 |
|
|
fprintf(stderr,"Copy contents of INFILE to OUTFILE, replacing the first word of INFILE with the\n");
|
71 |
|
|
fprintf(stderr,"size of the file in bytes.\n\n");
|
72 |
|
|
fprintf(stderr,"\tInsufficient options.\n");
|
73 |
|
|
fprintf(stderr,"\tPlease specify an input and then an output name.\n");
|
74 |
|
|
fprintf(stderr,"\n");
|
75 |
|
|
return 1;
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
// Open input file
|
79 |
|
|
fd = fopen( argv[filename_index], "r" );
|
80 |
|
|
if (fd == NULL)
|
81 |
|
|
{
|
82 |
|
|
fprintf(stderr,"failed to open input file: %s\n",argv[filename_index]);
|
83 |
|
|
return 1;
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
// Open the output file
|
87 |
|
|
dest_fd = fopen( argv[output_filename_index], "w");
|
88 |
|
|
if ( dest_fd == NULL )
|
89 |
|
|
{
|
90 |
|
|
fprintf(stderr,"failed to open output file: %s\n",argv[output_filename_index]);
|
91 |
|
|
return 1;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
// the very first word in flash is used. Determine the length of this file
|
95 |
|
|
fseek(fd, 0, SEEK_END);
|
96 |
|
|
image_size = ftell(fd);
|
97 |
|
|
fseek(fd,0,SEEK_SET);
|
98 |
|
|
|
99 |
|
|
// This bit ensures the size word is a word multiple, but that shouldn't be
|
100 |
|
|
// required.
|
101 |
|
|
//image_size+=3;
|
102 |
|
|
//image_size &= 0xfffffffc;
|
103 |
|
|
|
104 |
|
|
// Sanity check on image size
|
105 |
|
|
if (image_size < 8){
|
106 |
|
|
fprintf(stderr, "Bad binary image. Size too small\n");
|
107 |
|
|
return 1;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
// Now write out the image size, BIG ENDIAN
|
111 |
|
|
i=0;
|
112 |
|
|
unsigned char data_byte;
|
113 |
|
|
data_byte = (image_size >> 24) & 0xff;
|
114 |
|
|
fwrite(&data_byte, 1, 1, dest_fd);
|
115 |
|
|
data_byte = (image_size >> 16) & 0xff;
|
116 |
|
|
fwrite(&data_byte, 1, 1, dest_fd);
|
117 |
|
|
data_byte = (image_size >> 8) & 0xff;
|
118 |
|
|
fwrite(&data_byte, 1, 1, dest_fd);
|
119 |
|
|
data_byte = (image_size >> 0) & 0xff;
|
120 |
|
|
fwrite(&data_byte, 1, 1, dest_fd);
|
121 |
|
|
|
122 |
|
|
// Now copy the binary file into RAM
|
123 |
|
|
|
124 |
|
|
// Fix for the current bootloader software! Skip the first 4 bytes of
|
125 |
|
|
// application data. Hopefully it's not important. 030509 -- jb
|
126 |
|
|
for(i=0;i<4;i++)
|
127 |
|
|
n = fread(&data_byte,1,1,fd);
|
128 |
|
|
|
129 |
|
|
while(i<image_size)
|
130 |
|
|
{
|
131 |
|
|
n = fread(&data_byte,1,1,fd);
|
132 |
|
|
if (!n)
|
133 |
|
|
{
|
134 |
|
|
fprintf(stderr,"error reading input file\n");
|
135 |
|
|
fclose(fd);
|
136 |
|
|
fclose(dest_fd);
|
137 |
|
|
return 1;
|
138 |
|
|
}
|
139 |
|
|
fwrite(&data_byte, 1, 1, dest_fd);
|
140 |
|
|
i++;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
fclose(fd);
|
144 |
|
|
fclose(dest_fd);
|
145 |
|
|
|
146 |
|
|
return 0;
|
147 |
|
|
}
|