1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 
6 /* Define the file handles.  */
7 
8 FILE	*source_file;
9 FILE    *array_file;
10 
11 
main(int argc,char * argv[])12 int main(int argc, char* argv[])
13 {
14 
15 int             alpha;
16 int             alpha1;
17 int             alpha2;
18 int             alpha3;
19 unsigned long   address;
20 unsigned long   column;
21 
22 
23     /* Determine if the proper number of files are provided.  */
24     if (argc != 3)
25     {
26 
27         /* Print an error message out and wait for user key hit.  */
28 		printf("module_binary_to_c_array.exe - Copyright (c) Microsoft Corporation v5.8\n");
29         printf("**** Error: invalid input parameter for module_binary_to_c_array.exe **** \n");
30         printf("     Command Line Should be:\n\n");
31         printf("     > module_binary_to_c_array source_binary_file c_array_file <cr> \n\n");
32         return(1);
33     }
34 
35     /* Attempt to open the source file for reading.  */
36     source_file =  fopen(argv[1], "rb");
37 
38     /* Determine if the source file was opened properly.  */
39     if (source_file == NULL)
40     {
41 
42         /* Print an error message out and wait for user key hit.  */
43         printf("**** Error: open failed on binary source file **** \n");
44         printf("            File: %s   ", argv[1]);
45         return(2);
46     }
47 
48 	/* Determine if the binary file is a valid ThreadX module.  */
49 	alpha =   fgetc(source_file);
50 	alpha1 =  fgetc(source_file);
51 	alpha2 =  fgetc(source_file);
52 	alpha3 =  fgetc(source_file);
53 
54 	if ((alpha != 0x4D && alpha != 0x55) || (alpha1 != 0x4F && alpha1 != 0x44) || (alpha2 != 0x44 && alpha2 != 0x4F) || (alpha3 != 0x55 && alpha3 != 0x4D))
55 	{
56 
57         /* Print an error message out and wait for user key hit.  */
58         printf("**** Error: invalid format of binary input file **** \n");
59         printf("            File: %s   ", argv[1]);
60         return(3);
61 	}
62 
63 	/* Attempt to open the dump file for writing.  */
64     array_file =  fopen(argv[2], "w");
65 
66     /* Determine if the dump file was opened properly.  */
67     if (array_file == NULL)
68     {
69 
70         /* Print an error message out and wait for user key hit.  */
71         printf("**** Error: open failed on C array file **** \n");
72         printf("            File: %s   ", argv[2]);
73         return(4);
74     }
75 
76     fprintf(array_file, "/**************************** Module-Binary-to-C-array Utility **********************************/\n");
77 	fprintf(array_file, "/*                                                                                              */\n");
78 	fprintf(array_file, "/* Copyright (c) Microsoft Corporation                      Version 5.4, build date: 03-01-2018 */\n");
79 	fprintf(array_file, "/*                                                                                              */\n");
80 	fprintf(array_file, "/************************************************************************************************/\n\n");
81     fprintf(array_file, "/* \n");
82 	fprintf(array_file, "   Input Binary file:   %30s\n", argv[1]);
83 	fprintf(array_file, "   Output C Array file: %30s\n", argv[2]);
84 	fprintf(array_file, "*/\n\n");
85 
86 	/* Now print out the sections in a C array.  */
87 	fprintf(array_file, "unsigned char  module_code[] = {\n\n");
88 	fprintf(array_file, "/* Address                                            Contents                                        */\n\n");
89 
90 	/* Seek to the beginning of the source file. */
91     fseek(source_file, 0, SEEK_SET);
92 
93 	/* Initialize the variables.  */
94 	address =  0;
95 	column =   0;
96 
97 	do
98 	{
99 
100 		/* Get character from the input file.  */
101 		alpha =   fgetc(source_file);
102 
103 		/* Have we reached EOF?  */
104 		if (alpha == EOF)
105 			break;
106 
107 		/* Print out character with a leading comma, except on the first character.  */
108 		if (column == 0)
109 		{
110 			if (address != 0)
111 				fprintf(array_file, ",\n");
112 			fprintf(array_file, "/* 0x%08X */   0x%02X", address, (unsigned int) alpha);
113 		}
114 		else
115 			fprintf(array_file, ", 0x%02X", (unsigned int) alpha);
116 
117 		/* Move column forward.  */
118 		column++;
119 
120 		/* Are we at the end of the column?  */
121 		if (column >= 16)
122 		{
123 
124 			column =  0;
125     	}
126 
127 		/* Move address forward.  */
128 		address++;
129 	} while (alpha != EOF);
130 
131 	/* Finally, finish the C array containing the module code.  */
132 	fprintf(array_file, "};\n\n");
133 
134 	/* Close files.  */
135 	fclose(source_file);
136     fclose(array_file);
137 
138 	return 0;
139 }
140