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