GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / cfe / cfe / hosttools / mkflashimage.c
blob11fc6802383352c84deef301ac59bdc4d9b5dda7
1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
4 * Flash Image generator File: mkflashimage.c
5 *
6 * Author: Mitch Lichtenberg (mpl@broadcom.com)
7 *
8 * This program sticks a header on the front of a binary
9 * file making it suitable for use with the 'flash' command
10 * in CFE. The header contains the CFE version # and board
11 * type, a CRC, and other info to help prevent us from
12 * flashing bad stuff onto a board.
14 *********************************************************************
16 * Copyright 2000,2001,2002,2003
17 * Broadcom Corporation. All rights reserved.
19 * This software is furnished under license and may be used and
20 * copied only in accordance with the following terms and
21 * conditions. Subject to these conditions, you may download,
22 * copy, install, use, modify and distribute modified or unmodified
23 * copies of this software in source and/or binary form. No title
24 * or ownership is transferred hereby.
26 * 1) Any source code used, modified or distributed must reproduce
27 * and retain this copyright notice and list of conditions
28 * as they appear in the source file.
30 * 2) No right is granted to use any trade name, trademark, or
31 * logo of Broadcom Corporation. The "Broadcom Corporation"
32 * name may not be used to endorse or promote products derived
33 * from this software without the prior written permission of
34 * Broadcom Corporation.
36 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
37 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
38 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
39 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
40 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
41 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
42 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
44 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
45 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
46 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
47 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
48 * THE POSSIBILITY OF SUCH DAMAGE.
49 ********************************************************************* */
51 #include <sys/types.h>
52 #include <sys/stat.h>
54 #ifndef _SYS_INT_TYPES_H
55 typedef unsigned char uint8_t;
56 typedef unsigned long long uint64_t;
57 typedef unsigned long uint32_t;
58 #endif
60 #include "cfe_flashimage.h"
61 #include <fcntl.h>
62 #include <stdlib.h>
63 #include <stdio.h>
64 #include <string.h>
67 static int verbose = 0;
68 static int big_endian = 1;
69 static int both_endian = 0;
70 int mlong64 = 0;
71 char boardname[32];
74 * This is the offset in the flash where the little-endian image goes
75 * if we're making a bi-endian flash.
78 #define CFE_BIENDIAN_LE_OFFSET (1024*1024)
80 static void usage(void)
82 fprintf(stderr,"usage: mkflashimage [-v] [-EB] [-EL] [-64] [-B boardname] [-V v.v.v] binfile outfile\n");
83 fprintf(stderr,"\n");
84 fprintf(stderr," mkflashimage [-v] -EX [-64] [-B boardname] [-V v.v.v] BE-binfile LE-binfile outfile\n");
85 fprintf(stderr," (this variant used for making bi-endian flash files)\n");
86 exit(1);
90 static int host_is_little(void)
92 unsigned long var = 1;
93 unsigned char *pvar = (unsigned char *) &var;
95 return (*pvar == 1);
98 #define CRC32_POLY 0xEDB88320UL /* CRC-32 Poly */
100 static unsigned int
101 crc32(const unsigned char *databuf, unsigned int datalen)
103 unsigned int idx, bit, data, crc = 0xFFFFFFFFUL;
105 for (idx = 0; idx < datalen; idx++) {
106 for (data = *databuf++, bit = 0; bit < 8; bit++, data >>= 1) {
107 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CRC32_POLY : 0);
111 return crc;
115 void stuff_be32(uint8_t *dest,unsigned int src)
117 *dest++ = (src >> 24) & 0xFF;
118 *dest++ = (src >> 16) & 0xFF;
119 *dest++ = (src >> 8) & 0xFF;
120 *dest++ = (src >> 0) & 0xFF;
123 int main(int argc, char *argv[])
125 int fh;
126 int flashsize;
127 unsigned char *flashcode;
128 cfe_flashimage_t header;
129 int host_le;
130 int majver,minver,ecover;
131 uint32_t crc;
132 uint32_t flags;
133 char *outfile;
135 majver = minver = ecover = 0;
136 boardname[0] = 0;
138 while ((argc > 1) && (argv[1][0] == '-')) {
139 if (strcmp(argv[1],"-v") == 0) {
140 verbose = 1;
142 else if (strcmp(argv[1],"-EX") == 0) {
143 if (verbose) fprintf(stderr,"[Image file will be marked Bi-Endian]\n");
144 both_endian = 1;
146 else if (strcmp(argv[1],"-EB") == 0) {
147 if (verbose) fprintf(stderr,"[Image file will be marked Big-Endian]\n");
148 big_endian = 1;
150 else if (strcmp(argv[1],"-EL") == 0) {
151 if (verbose) fprintf(stderr,"[Image file will be marked Little-Endian]\n");
152 big_endian = 0;
154 else if (strcmp(argv[1],"-64") == 0) {
155 if (verbose) fprintf(stderr,"[Image file will be marked 64-bit]\n");
156 mlong64 = 1;
158 else if (strcmp(argv[1],"-B") == 0) {
159 argc--;
160 argv++;
161 strcpy(boardname,argv[1]);
162 if (verbose) fprintf(stderr,"[Board name: %s]\n",boardname);
164 else if (strcmp(argv[1],"-V") == 0) {
165 argc--;
166 argv++;
167 sscanf(argv[1],"%d.%d.%d",&majver,&minver,&ecover);
168 if (verbose) fprintf(stderr,"[Image version: %d.%d.%d]\n",majver,minver,ecover);
170 else {
171 fprintf(stderr,"Invalid switch: %s\n",argv[1]);
172 exit(1);
174 argv++;
175 argc--;
179 * We need to swap things around if the host and
180 * target are different endianness
183 host_le = host_is_little();
185 if (verbose) {
186 fprintf(stderr,"Host is %s-endian.\n",host_le ? "little" : "big");
187 if (both_endian) {
188 fprintf(stderr,"Target is bi-endian.\n");
190 else {
191 fprintf(stderr,"Target is %s-endian.\n",big_endian ? "big" : "little");
195 if ((both_endian && (argc != 4)) || (!both_endian && (argc != 3))) {
196 usage();
200 * Read in the boot file(s)
203 flags = 0;
205 if (both_endian) {
206 int be_size;
208 flags |= (CFE_IMAGE_EB | CFE_IMAGE_EL);
210 if (verbose) fprintf(stderr,"Reading: %s\n",argv[2]);
212 fh = open(argv[2],O_RDONLY);
213 if (fh < 0) {
214 perror(argv[2]);
217 flashsize = lseek(fh,0L,SEEK_END);
218 lseek(fh,0L,SEEK_SET);
220 flashcode = malloc(flashsize+CFE_BIENDIAN_LE_OFFSET);
221 if (flashcode == NULL) {
222 perror("malloc");
223 exit(1);
225 memset(flashcode,0xFF,flashsize+CFE_BIENDIAN_LE_OFFSET);
227 if (read(fh,flashcode+CFE_BIENDIAN_LE_OFFSET,flashsize) != flashsize) {
228 perror("read");
229 exit(1);
232 close(fh);
234 if (memcmp(flashcode+CFE_BIENDIAN_LE_OFFSET,CFE_IMAGE_SEAL,4) == 0) {
235 fprintf(stderr,"File '%s' already has an image header.\n",argv[2]);
236 exit(1);
239 flashsize += CFE_BIENDIAN_LE_OFFSET; /* actual file size */
241 if (verbose) fprintf(stderr,"Reading: %s\n",argv[1]);
243 fh = open(argv[1],O_RDONLY);
244 if (fh < 0) {
245 perror(argv[1]);
246 exit(1);
249 be_size = lseek(fh,0L,SEEK_END);
250 lseek(fh,0L,SEEK_SET);
251 if (be_size > CFE_BIENDIAN_LE_OFFSET) {
252 fprintf(stderr,"File '%s' will not fit within first %d bytes of flash image\n",
253 argv[1],CFE_BIENDIAN_LE_OFFSET);
254 close(fh);
255 exit(1);
258 if (read(fh,flashcode,be_size) != be_size) {
259 perror("read");
260 exit(1);
263 close(fh);
265 outfile = argv[3];
268 else {
269 if (big_endian) flags |= CFE_IMAGE_EB;
270 else flags |= CFE_IMAGE_EL;
272 fh = open(argv[1],O_RDONLY);
273 if (fh < 0) {
274 perror(argv[1]);
275 exit(1);
278 flashsize = lseek(fh,0L,SEEK_END);
279 lseek(fh,0L,SEEK_SET);
281 flashcode = malloc(flashsize);
282 if (flashcode == NULL) {
283 perror("malloc");
284 exit(1);
286 memset(flashcode,0,flashsize);
287 if (read(fh,flashcode,flashsize) != flashsize) {
288 perror("read");
289 exit(1);
292 close(fh);
294 if (memcmp(flashcode,CFE_IMAGE_SEAL,4) == 0) {
295 fprintf(stderr,"File '%s' already has an image header.\n",argv[1]);
296 exit(1);
299 outfile = argv[2];
304 * Construct the flash header
307 if (mlong64) flags |= CFE_IMAGE_MLONG64;
308 crc = crc32(flashcode,flashsize);
310 memset(&header,0,sizeof(header));
311 memcpy(header.seal,CFE_IMAGE_SEAL,sizeof(header.seal));
312 stuff_be32(header.flags,flags);
313 stuff_be32(header.size,flashsize);
314 stuff_be32(header.crc,crc);
315 header.majver = majver;
316 header.minver = minver;
317 header.ecover = ecover;
318 header.miscver = 0;
319 strcpy(header.boardname,boardname);
322 * Now write the output file
325 fh = open(outfile,O_RDWR|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE|S_IRGRP|S_IWGRP|S_IROTH);
326 if (fh < 0) {
327 perror(outfile);
328 exit(1);
330 if (write(fh,&header,sizeof(header)) != sizeof(header)) {
331 perror(outfile);
332 exit(1);
334 if (write(fh,flashcode,flashsize) != flashsize) {
335 perror(outfile);
336 exit(1);
339 fprintf(stderr,"Wrote %d bytes to %s\n",sizeof(header)+flashsize,outfile);
341 close(fh);
343 exit(0);