]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/CCode/Source/CreateMtFile/CreateMtFile.c
File modified to add usage information and implement minor corrections.
[mirror_edk2.git] / Tools / CCode / Source / CreateMtFile / CreateMtFile.c
1 /*++
2
3 Copyright (c) 1999-2006 Intel Corporation. All rights reserved
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13 Module Name:
14
15 CreateMtFile.c
16
17 Abstract:
18
19 Simple utility to create a pad file containing fixed data.
20
21 --*/
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <Common/UefiBaseTypes.h>
28
29 #define UTILITY_NAME "CreateMtFile"
30 #define UTILITY_MAJOR_VERSION 1
31 #define UTILITY_MINOR_VERSION 1
32
33 typedef struct {
34 INT8 *OutFileName;
35 INT8 ByteValue;
36 UINT32 FileSize;
37 } OPTIONS;
38
39 static
40 EFI_STATUS
41 ProcessArgs (
42 IN INT32 Argc,
43 IN INT8 *Argv[],
44 IN OUT OPTIONS *Options
45 );
46
47 static
48 void
49 CMFUsage (
50 VOID
51 );
52
53 static
54 void
55 CMFVersion (
56 VOID
57 );
58
59 int
60 main (
61 IN INT32 Argc,
62 IN INT8 *Argv[]
63 )
64 /*++
65
66 Routine Description:
67
68 Main entry point for this utility.
69
70 Arguments:
71
72 Standard C entry point args Argc and Argv
73
74 Returns:
75
76 EFI_SUCCESS if good to go
77
78 --*/
79 // GC_TODO: ] - add argument and description to function comment
80 // GC_TODO: EFI_INVALID_PARAMETER - add return value to function comment
81 // GC_TODO: EFI_DEVICE_ERROR - add return value to function comment
82 // GC_TODO: EFI_DEVICE_ERROR - add return value to function comment
83 {
84 FILE *OutFptr;
85 OPTIONS Options;
86
87 //
88 // Process the command-line arguments.
89 //
90 if (ProcessArgs (Argc, Argv, &Options) != EFI_SUCCESS) {
91 return EFI_INVALID_PARAMETER;
92 }
93 //
94 // Open the output file
95 //
96 if ((OutFptr = fopen (Options.OutFileName, "wb")) == NULL) {
97 printf (" ERROR: Could not open output file '%s' for writing\n", Options.OutFileName);
98 return EFI_DEVICE_ERROR;
99 }
100 //
101 // Write the pad bytes. Do it the slow way (one at a time) for now.
102 //
103 while (Options.FileSize > 0) {
104 if (fwrite (&Options.ByteValue, 1, 1, OutFptr) != 1) {
105 fclose (OutFptr);
106 printf (" ERROR: Failed to write to output file\n");
107 return EFI_DEVICE_ERROR;
108 }
109
110 Options.FileSize--;
111 }
112 //
113 // Close the file
114 //
115 fclose (OutFptr);
116 return EFI_SUCCESS;
117 }
118
119 static
120 EFI_STATUS
121 ProcessArgs (
122 IN INT32 Argc,
123 IN INT8 *Argv[],
124 IN OUT OPTIONS *Options
125 )
126 /*++
127
128 Routine Description:
129
130 Process the command line arguments.
131
132 Arguments:
133
134 Argc - argument count as passed in to the entry point function
135 Argv - array of arguments as passed in to the entry point function
136 Options - stucture of where to put the values of the parsed arguments
137
138 Returns:
139
140 EFI_SUCCESS if everything looks good
141 EFI_INVALID_PARAMETER otherwise
142
143 --*/
144 // GC_TODO: ] - add argument and description to function comment
145 {
146 UINT32 Multiplier;
147
148 //
149 // Clear the options
150 //
151 memset ((char *) Options, 0, sizeof (OPTIONS));
152
153 //
154 // Skip program name
155 //
156 Argv++;
157 Argc--;
158
159 if (Argc < 1) {
160 CMFUsage();
161 return EFI_INVALID_PARAMETER;
162 }
163
164 if ((strcmp(Argv[0], "-h") == 0) || (strcmp(Argv[0], "--help") == 0) ||
165 (strcmp(Argv[0], "-?") == 0) || (strcmp(Argv[0], "/?") == 0)) {
166 CMFUsage();
167 return EFI_INVALID_PARAMETER;
168 }
169
170 if ((strcmp(Argv[0], "-V") == 0) || (strcmp(Argv[0], "--version") == 0)) {
171 CMFVersion();
172 return EFI_INVALID_PARAMETER;
173 }
174
175 if (Argc < 2) {
176 CMFUsage ();
177 return EFI_INVALID_PARAMETER;
178 }
179 //
180 // If first arg is dash-option, then print usage.
181 //
182 if (Argv[0][0] == '-') {
183 CMFUsage ();
184 return EFI_INVALID_PARAMETER;
185 }
186 //
187 // First arg is file name
188 //
189 Options->OutFileName = Argv[0];
190 Argc--;
191 Argv++;
192
193 //
194 // Second arg is file size. Allow 0x1000, 0x100K, 1024, 1K
195 //
196 Multiplier = 1;
197 if ((Argv[0][strlen (Argv[0]) - 1] == 'k') || (Argv[0][strlen (Argv[0]) - 1] == 'K')) {
198 Multiplier = 1024;
199 }
200
201 //
202 // Check for negtive size
203 //
204 if (Argv[0][0] == '-') {
205 printf("ERROR: File size should be non-negtive.\n");
206 return EFI_INVALID_PARAMETER;
207 }
208
209 //
210 // Look for 0x prefix on file size
211 //
212 if ((Argv[0][0] == '0') && ((Argv[0][1] == 'x') || (Argv[0][1] == 'X'))) {
213 if (sscanf (Argv[0], "%x", &Options->FileSize) != 1) {
214 printf ("ERROR: Invalid file size '%s'\n", Argv[0]);
215 CMFUsage ();
216 return EFI_INVALID_PARAMETER;
217 }
218 //
219 // Otherwise must be a decimal number
220 //
221 } else {
222 if (sscanf (Argv[0], "%d", &Options->FileSize) != 1) {
223 printf ("ERROR: Invalid file size '%s'\n", Argv[0]);
224 CMFUsage ();
225 return EFI_INVALID_PARAMETER;
226 }
227 }
228
229 Options->FileSize *= Multiplier;
230 //
231 // Assume byte value of 0xff
232 //
233 Options->ByteValue = (INT8) (UINT8) 0xFF;
234 return EFI_SUCCESS;
235 }
236
237
238 static
239 void
240 CMFVersion(
241 void
242 )
243 /*++
244
245 Routine Description:
246
247 Print out version information for Strip.
248
249 Arguments:
250
251 None
252
253 Returns:
254
255 None
256
257 --*/
258 {
259 printf ("%s v%d.%d -EDK utility to create a pad file containing fixed data\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
260 printf ("Copyright (c) 1999-2006 Intel Corporation. All rights reserved.\n");
261 }
262
263 //
264 // Print utility usage info
265 //
266 static
267 void
268 CMFUsage (
269 VOID
270 )
271 /*++
272
273 Routine Description:
274
275 GC_TODO: Add function description
276
277 Arguments:
278
279 None
280
281 Returns:
282
283 GC_TODO: add return values
284
285 --*/
286 {
287 CMFVersion();
288
289 printf ("\n Usage: %s OutFileName FileSize \n\
290 where: \n\
291 OutFileName is the name of the output file to generate \n\
292 FileSize is the size of the file to create \n\
293 Examples: \n\
294 %s OutFile.bin 32K \n\
295 %s OutFile.bin 0x1000 \n",UTILITY_NAME, UTILITY_NAME, UTILITY_NAME);
296 }
297