]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/TianoTools/CreateMtFile/CreateMtFile.c
Applied BSD license to the source files
[mirror_edk2.git] / Tools / Source / TianoTools / 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
29#define PROGRAM_NAME "CreateMtFile"\r
30\r
31typedef struct {\r
32 INT8 *OutFileName;\r
33 INT8 ByteValue;\r
34 UINT32 FileSize;\r
35} OPTIONS;\r
36\r
37static\r
38EFI_STATUS\r
39ProcessArgs (\r
40 IN INT32 Argc,\r
41 IN INT8 *Argv[],\r
42 IN OUT OPTIONS *Options\r
43 );\r
44\r
45static\r
46void\r
47Usage (\r
48 VOID\r
49 );\r
50\r
51int\r
52main (\r
53 IN INT32 Argc,\r
54 IN INT8 *Argv[]\r
55 )\r
56/*++\r
57\r
58Routine Description:\r
59 \r
60 Main entry point for this utility.\r
61\r
62Arguments:\r
63\r
64 Standard C entry point args Argc and Argv\r
65\r
66Returns:\r
67\r
68 EFI_SUCCESS if good to go\r
69\r
70--*/\r
71// GC_TODO: ] - add argument and description to function comment\r
72// GC_TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
73// GC_TODO: EFI_DEVICE_ERROR - add return value to function comment\r
74// GC_TODO: EFI_DEVICE_ERROR - add return value to function comment\r
75{\r
76 FILE *OutFptr;\r
77 OPTIONS Options;\r
78\r
79 //\r
80 // Process the command-line arguments.\r
81 //\r
82 if (ProcessArgs (Argc, Argv, &Options) != EFI_SUCCESS) {\r
83 return EFI_INVALID_PARAMETER;\r
84 }\r
85 //\r
86 // Open the output file\r
87 //\r
88 if ((OutFptr = fopen (Options.OutFileName, "wb")) == NULL) {\r
89 fprintf (\r
90 stdout,\r
91 PROGRAM_NAME " ERROR: Could not open output file '%s' for writing\n",\r
92 Options.OutFileName\r
93 );\r
94 return EFI_DEVICE_ERROR;\r
95 }\r
96 //\r
97 // Write the pad bytes. Do it the slow way (one at a time) for now.\r
98 //\r
99 while (Options.FileSize > 0) {\r
100 if (fwrite (&Options.ByteValue, 1, 1, OutFptr) != 1) {\r
101 fclose (OutFptr);\r
102 fprintf (stdout, PROGRAM_NAME " ERROR: Failed to write to output file\n");\r
103 return EFI_DEVICE_ERROR;\r
104 }\r
105\r
106 Options.FileSize--;\r
107 }\r
108 //\r
109 // Close the file\r
110 //\r
111 fclose (OutFptr);\r
112 return EFI_SUCCESS;\r
113}\r
114\r
115static\r
116EFI_STATUS\r
117ProcessArgs (\r
118 IN INT32 Argc,\r
119 IN INT8 *Argv[],\r
120 IN OUT OPTIONS *Options\r
121 )\r
122/*++\r
123\r
124Routine Description:\r
125 \r
126 Process the command line arguments.\r
127\r
128Arguments:\r
129\r
130 Argc - argument count as passed in to the entry point function\r
131 Argv - array of arguments as passed in to the entry point function\r
132 Options - stucture of where to put the values of the parsed arguments\r
133\r
134Returns:\r
135\r
136 EFI_SUCCESS if everything looks good\r
137 EFI_INVALID_PARAMETER otherwise\r
138\r
139--*/\r
140// GC_TODO: ] - add argument and description to function comment\r
141{\r
142 UINT32 Multiplier;\r
143\r
144 //\r
145 // Clear the options\r
146 //\r
147 memset ((char *) Options, 0, sizeof (OPTIONS));\r
148\r
149 //\r
150 // Skip program name\r
151 //\r
152 Argv++;\r
153 Argc--;\r
154 if (Argc < 2) {\r
155 Usage ();\r
156 return EFI_INVALID_PARAMETER;\r
157 }\r
158 //\r
159 // If first arg is dash-option, then print usage.\r
160 //\r
161 if (Argv[0][0] == '-') {\r
162 Usage ();\r
163 return EFI_INVALID_PARAMETER;\r
164 }\r
165 //\r
166 // First arg is file name\r
167 //\r
168 Options->OutFileName = Argv[0];\r
169 Argc--;\r
170 Argv++;\r
171\r
172 //\r
173 // Second arg is file size. Allow 0x1000, 0x100K, 1024, 1K\r
174 //\r
175 Multiplier = 1;\r
176 if ((Argv[0][strlen (Argv[0]) - 1] == 'k') || (Argv[0][strlen (Argv[0]) - 1] == 'K')) {\r
177 Multiplier = 1024;\r
178 }\r
179 //\r
180 // Look for 0x prefix on file size\r
181 //\r
182 if ((Argv[0][0] == '0') && ((Argv[0][1] == 'x') || (Argv[0][1] == 'X'))) {\r
183 if (sscanf (Argv[0], "%x", &Options->FileSize) != 1) {\r
184 fprintf (stdout, PROGRAM_NAME " ERROR: Invalid file size '%s'\n", Argv[0]);\r
185 Usage ();\r
186 return EFI_INVALID_PARAMETER;\r
187 }\r
188 //\r
189 // Otherwise must be a decimal number\r
190 //\r
191 } else {\r
192 if (sscanf (Argv[0], "%d", &Options->FileSize) != 1) {\r
193 fprintf (stdout, PROGRAM_NAME " ERROR: Invalid file size '%s'\n", Argv[0]);\r
194 Usage ();\r
195 return EFI_INVALID_PARAMETER;\r
196 }\r
197 }\r
198\r
199 Options->FileSize *= Multiplier;\r
200 //\r
201 // Assume byte value of 0xff\r
202 //\r
203 Options->ByteValue = (INT8) (UINT8) 0xFF;\r
204 return EFI_SUCCESS;\r
205}\r
206//\r
207// Print utility usage info\r
208//\r
209static\r
210void\r
211Usage (\r
212 VOID\r
213 )\r
214/*++\r
215\r
216Routine Description:\r
217\r
218 GC_TODO: Add function description\r
219\r
220Arguments:\r
221\r
222 None\r
223\r
224Returns:\r
225\r
226 GC_TODO: add return values\r
227\r
228--*/\r
229{\r
230 UINT32 Index;\r
231 static const INT8 *Text[] = {\r
232 " ",\r
233 "Usage: "PROGRAM_NAME " OutFileName FileSize",\r
234 " where:",\r
235 " OutFileName is the name of the output file to generate",\r
236 " FileSize is the size of the file to create",\r
237 " Examples:",\r
238 " "PROGRAM_NAME " OutFile.bin 32K",\r
239 " "PROGRAM_NAME " OutFile.bin 0x1000",\r
240 " ",\r
241 NULL\r
242 };\r
243\r
244 for (Index = 0; Text[Index] != NULL; Index++) {\r
245 fprintf (stdout, "%s\n", Text[Index]);\r
246 }\r
247}\r