EFR32 Blue Gecko 1 Software Documentation  efr32bg1-doc-5.1.2
rfs_create.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <time.h>
7 #include "rfs.h"
8 
9 #define error_printf printf
10 #define debug_printf if( debugLevel>0 ) printf
11 
12 #define MAX_FILES 256
13 #define MAX_ARGS 256
14 
15 void closeFiles();
16 
17 void dummy_printf(char *ptr,...);
18 
19 FILE *fpout;
20 FILE *fpin;
21 
22 int inputFileCount;
23 char *inputFileName;
24 int fileLength;
25 
26 uint8_t *buf;
27 long buflen;
28 
29 const char *argerror = "error: Two file arguments required";
30 const char commentLine[] = "/************************************************************************/\n";
31 
32 char stringbuf[256];
33 
34 int fileargs = 0;
35 int optArgs = 0;
36 int filearg[ MAX_FILES ];
37 int optArg[ MAX_ARGS ];
38 char *fromfile;
39 char *tofile;
40 struct tm *tptr;
41 time_t lt;
42 
43 char *outputFileName;
44 float compressRatio = 0.0f;
45 
46 int compression = 0;
47 int debugLevel = 0;
48 
49 typedef struct __fileInfo {
50 
51  char *fileName;
52  int fileLength;
53 
54 } FileInfo;
55 
56 FileInfo files[MAX_FILES];
57 
58 int main(int argc, char *argv[]){
59 
60  int i,j;
61  int ret;
62  unsigned char ch='\0';
63  int count;
64  char tempc;
65  int len;
66  char * tmpStr;
67 
68  int temp;
69  int temp2;
70  int temp3;
71 
72  if ( argc <= 2 ){
73  error_printf( "Syntax: rfscreate [option] ... [option] <outputfile> <inputfiles>\n\n\r" );
74  error_printf( " <outputfile> : Name of output file\n\r");
75  error_printf( " <inputfiles> : List of input files to convert\n\r" );
76  error_printf( " -c : Compress files\n\r");
77  error_printf( " -d:<0-9> : Debug level\n\r" );
78  error_printf( "\n" );
79  exit(1);
80  }
81 
82  /*************************************************************************/
83  /* Command line parsing */
84  /*************************************************************************/
85  i=1;
86  optArgs = 0;
87  fileargs = 0;
88 
89  debug_printf( "-------------------- Argument processing -------------------\n" );
90  while( i < argc ){
91 
92  ch = argv[i][0];
93 
94  if( ch == '-' ){
95 
96  debug_printf("option : %s\n", argv[i]);
97 
98  switch(argv[i][1]){
99 
100  case 'n':
101  if (argv[i][2] == 'c'){
102  compression = 0;
103  }
104  break;
105 
106  case 'd':
107  ret = sscanf( argv[i], "-d:%d", &temp );
108  if(ret == 1){
109  debugLevel = temp;
110  }
111  debug_printf( "Debug level : %d\n", temp );
112 
113  break;
114 
115  default:
116  error_printf("Unknown option: %s\n",argv[i]);
117  break;
118  }
119 
120  optArg[optArgs] = i;
121  optArgs++;
122 
123  }
124  else{
125 
126  filearg[fileargs] = i;
127  debug_printf( "filearg[%d] = argv[%d]: %s\n", fileargs, filearg[fileargs], argv[i] );
128 
129  fileargs++;
130 
131  }
132 
133  i++;
134 
135  }
136 
137  if( fileargs<2 ){
138  error_printf(argerror);
139  exit(1);
140  }
141 
142  debug_printf( "------------------- /Argument processing -------------------\n" );
143 
144 
145  debug_printf( "---------------- File information gathering ----------------\n" );
146 
147  /* Get local time */
148  lt = time(NULL);
149  if(lt!=-1){
150  tptr = localtime(&lt);
151  printf( "Local time : %s",asctime(tptr) );
152  }
153  else{
154  error_printf("Error: Failed to get local time.\n");
155  exit(1);
156  }
157 
158  /* Open output file */
159  outputFileName = argv[filearg[0]];
160  if( (fpout=fopen(outputFileName ,"wb")) == NULL ){
161  error_printf("Error: File %s could not be opened for write.\n", outputFileName );
162  closeFiles();
163  exit(1);
164  }
165  else{
166  printf( "Output file : %s\n", outputFileName );
167  }
168 
169  /* Loop through input files */
170  inputFileCount = fileargs-1;
171  for( j=0; j<inputFileCount; j++ ){
172 
173  inputFileName = argv[filearg[j+1]];
174 
175  /* Open the file */
176  if( (fpin=fopen( inputFileName ,"rb")) == NULL ){
177  error_printf("Error: File %s could not be opened for reading.\n", inputFileName );
178  closeFiles();
179  exit(1);
180  }
181 
182  /* Find file length */
183  fseek(fpin,0,SEEK_END);
184  fileLength = ftell(fpin);
185  fseek(fpin,0,SEEK_SET);
186 
187  /* Store the information */
188  files[j].fileName = inputFileName;
189  files[j].fileLength = fileLength;
190 
191  /* Close the file */
192  fclose(fpin);
193 
194  }
195 
196  debug_printf( "--------------- /File information gathering ----------------\n" );
197 
198  /* Write initial header to it */
199  fprintf( fpout, "/************************************************************************/\n");
200  fprintf( fpout, "/* Autogenerated file - DO NOT EDIT */\n");
201  fprintf( fpout, "/* */\n");
202  fprintf( fpout, "/* Date: %02d.%02d.%04d %02d:%02d:%02d */\n",
203  tptr->tm_mday,
204  tptr->tm_mon+1,
205  tptr->tm_year+1900,
206  tptr->tm_hour,
207  tptr->tm_min,
208  tptr->tm_sec
209  );
210  /* fprintf( fpout, "/\* Date: %s *\/\n", asctime(tptr) ); */
211  fprintf( fpout, "/* */\n");
212  fprintf( fpout, "/* Files embedded in this file; */\n");
213  fprintf( fpout, "/* */\n");
214  fprintf( fpout, "/* Length Index Name */\n");
215  fprintf( fpout, "/*----------------------------------------------------------------------*/\n");
216  printf( " Length Index Name\n");
217  printf( "----------------------------------------------------------------------\n");
218  for( j=0; j<inputFileCount; j++ ){
219  printf( " %8d %2d %s\n", files[j].fileLength, j, files[j].fileName );
220  sprintf( stringbuf, "/* %8d %2d %s", files[j].fileLength, j, files[j].fileName );
221  len = strlen( stringbuf );
222  for( i=len; i<(sizeof(commentLine)-4); i++ ) stringbuf[i] = ' ';
223  stringbuf[i++] = '*';
224  stringbuf[i++] = '/';
225  stringbuf[i++] = '\n';
226  stringbuf[i ] = 0x00;
227  /*printf(stringbuf);*/
228  fprintf( fpout, stringbuf );
229  }
230  fprintf( fpout, "/************************************************************************/\n");
231  fprintf( fpout, "#ifndef __RFS_FILES_H_\n\n");
232  fprintf( fpout, "#define __RFS_FILES_H_\n\n");
233 
234  fprintf( fpout, "#include <stdint.h>\n");
235  fprintf( fpout, "\n");
236 
237 
238  fprintf( fpout, "#define RFS_FILE_COUNT %d\n", inputFileCount );
239  fprintf( fpout, "const uint32_t RFS_fileCount = RFS_FILE_COUNT;\n" );
240  fprintf( fpout, "\n");
241 
242  /* Write file name constants */
243  for( j=0; j<inputFileCount; j++ ){
244  fprintf( fpout, "const uint8_t RFS_fileName%d[] = \"%s\";\n", j, files[j].fileName );
245  }
246 
247  /* Write file name pointer array */
248  fprintf( fpout, "\n");
249  fprintf( fpout, "const uint8_t *RFS_fileNames[ RFS_FILE_COUNT ] = {\n" );
250  for( j=0; j<inputFileCount; j++ ){
251  fprintf( fpout, " RFS_fileName%d", j );
252  if( j != (inputFileCount-1) ) fprintf( fpout, ",\n" );
253  }
254  fprintf( fpout, "\n");
255  fprintf( fpout, "};\n");
256 
257  /* Write file length array */
258  fprintf( fpout, "\n");
259  fprintf( fpout, "const uint32_t RFS_fileLength[ RFS_FILE_COUNT ] = {\n" );
260  for( j=0; j<inputFileCount; j++ ){
261  fprintf( fpout, " %d", files[j].fileLength );
262  if( j != (inputFileCount-1) ) fprintf( fpout, ",\n" );
263  }
264  fprintf( fpout, "\n");
265  fprintf( fpout, "};\n");
266 
267  /* Predeclare pointers to file data*/
268  fprintf( fpout, "\n");
269  for( j=0; j<inputFileCount; j++ ){
270  fprintf( fpout, "const uint8_t RFS_fileData%d[];\n",j );
271  }
272 
273  /* Write file data pointer array */
274  fprintf( fpout, "\n");
275  fprintf( fpout, "const uint8_t *RFS_fileData[ RFS_FILE_COUNT ] = {\n" );
276  for( j=0; j<inputFileCount; j++ ){
277  fprintf( fpout, " RFS_fileData%d", j );
278  if( j != (inputFileCount-1) ) fprintf( fpout, ",\n" );
279  }
280  fprintf( fpout, "\n");
281  fprintf( fpout, "};\n");
282 
283  /* Write actual image data */
284  for( j=0; j<inputFileCount; j++ ){
285 
286  /* Open the file */
287  if( (fpin=fopen( files[j].fileName ,"rb")) == NULL ){
288  error_printf("Error: File %s could not be opened for reading.\n", files[j].fileName );
289  closeFiles();
290  exit(1);
291  }
292 
293  /* Write file data into header file */
294  fprintf( fpout, "\n");
295  fprintf( fpout, "const uint8_t RFS_fileData%d[] = {\n",j );
296 
297  count = 0;
298  fprintf( fpout, "\n ");
299  for( i=0; i<files[j].fileLength; i++ ){
300  ch = fgetc( fpin );
301  fprintf( fpout, "0x%02x", (unsigned int)ch );
302  if ( j != (files[j].fileLength-1) ) fprintf( fpout, ", " );
303  if ( (++count % 16) == 0 ) fprintf( fpout, "\n ");
304  }
305 
306  if( debugLevel>0) printf("last byte = %02Xh\n", (unsigned int)ch );
307  temp = fgetc( fpin );
308  ch = (unsigned char)temp;
309  if( temp != EOF ){
310  printf("File %s contains more data than file length indicates! Bytes read = %d [%d]\n",
311  files[j].fileName,
312  count,
313  files[j].fileLength
314  );
315  }
316 
317  fclose( fpin );
318 
319  fprintf( fpout, "\n");
320  fprintf( fpout, "};\n");
321 
322  }
323 
324  fprintf( fpout, "#endif\n");
325 
326  return 0;
327 
328 }
329 
330 void closeFiles(){
331 
332  int ret;
333 
334  debug_printf("Closing all open files\n");
335  fclose(fpin);
336  fclose(fpout);
337 
338 }
339 
340 
341 void dummy_printf(char *ptr,...){
342 }
ROM File System Drive.