]>
Commit | Line | Data |
---|---|---|
d25c4bf0 | 1 | /*++\r |
2 | \r | |
3 | Copyright (c) 1999 - 2002 Intel Corporation. All rights reserved\r | |
4 | This software and associated documentation (if any) is furnished\r | |
5 | under a license and may only be used or copied in accordance\r | |
6 | with the terms of the license. Except as permitted by such\r | |
7 | license, no part of this software or documentation may be\r | |
8 | reproduced, stored in a retrieval system, or transmitted in any\r | |
9 | form or by any means without the express written consent of\r | |
10 | Intel Corporation.\r | |
11 | \r | |
12 | \r | |
13 | Module Name:\r | |
14 | \r | |
15 | CreateMtFile.c\r | |
16 | \r | |
17 | Abstract:\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 | |
51d48c26 | 26 | #include "UefiBaseTypes.h"\r |
27 | #include "Base.h"\r | |
d25c4bf0 | 28 | \r |
29 | #define PROGRAM_NAME "CreateMtFile"\r | |
30 | \r | |
31 | typedef struct {\r | |
32 | INT8 *OutFileName;\r | |
33 | INT8 ByteValue;\r | |
34 | UINT32 FileSize;\r | |
35 | } OPTIONS;\r | |
36 | \r | |
37 | static\r | |
38 | EFI_STATUS\r | |
39 | ProcessArgs (\r | |
40 | IN INT32 Argc,\r | |
41 | IN INT8 *Argv[],\r | |
42 | IN OUT OPTIONS *Options\r | |
43 | );\r | |
44 | \r | |
45 | static\r | |
46 | void\r | |
47 | Usage (\r | |
48 | VOID\r | |
49 | );\r | |
50 | \r | |
51 | int\r | |
52 | main (\r | |
53 | IN INT32 Argc,\r | |
54 | IN INT8 *Argv[]\r | |
55 | )\r | |
56 | /*++\r | |
57 | \r | |
58 | Routine Description:\r | |
59 | \r | |
60 | Main entry point for this utility.\r | |
61 | \r | |
62 | Arguments:\r | |
63 | \r | |
64 | Standard C entry point args Argc and Argv\r | |
65 | \r | |
66 | Returns:\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 | |
115 | static\r | |
116 | EFI_STATUS\r | |
117 | ProcessArgs (\r | |
118 | IN INT32 Argc,\r | |
119 | IN INT8 *Argv[],\r | |
120 | IN OUT OPTIONS *Options\r | |
121 | )\r | |
122 | /*++\r | |
123 | \r | |
124 | Routine Description:\r | |
125 | \r | |
126 | Process the command line arguments.\r | |
127 | \r | |
128 | Arguments:\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 | |
134 | Returns:\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 | |
209 | static\r | |
210 | void\r | |
211 | Usage (\r | |
212 | VOID\r | |
213 | )\r | |
214 | /*++\r | |
215 | \r | |
216 | Routine Description:\r | |
217 | \r | |
218 | GC_TODO: Add function description\r | |
219 | \r | |
220 | Arguments:\r | |
221 | \r | |
222 | None\r | |
223 | \r | |
224 | Returns:\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 |