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