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