summaryrefslogtreecommitdiff
path: root/Tools/CCode/Source/CreateMtFile/CreateMtFile.c
blob: 8bb07293e3707d4bc707743948914d82163df881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*++

Copyright (c)  1999-2006 Intel Corporation. All rights reserved
This program and the accompanying materials are licensed and made available 
under the terms and conditions of the BSD License which accompanies this 
distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.


Module Name:

  CreateMtFile.c

Abstract:

  Simple utility to create a pad file containing fixed data.
  
--*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <Common/UefiBaseTypes.h>

#define UTILITY_NAME "CreateMtFile"
#define UTILITY_MAJOR_VERSION 1
#define UTILITY_MINOR_VERSION 1

typedef struct {
  INT8    *OutFileName;
  INT8    ByteValue;
  UINT32  FileSize;
} OPTIONS;

static
EFI_STATUS
ProcessArgs (
  IN INT32          Argc,
  IN INT8           *Argv[],
  IN OUT OPTIONS    *Options
  );

static
void
Usage (
  VOID
  );

static
void
Version (
  VOID
  );

int
main (
  IN INT32  Argc,
  IN INT8   *Argv[]
  )
/*++

Routine Description:
  
  Main entry point for this utility.

Arguments:

  Standard C entry point args Argc and Argv

Returns:

  EFI_SUCCESS if good to go

--*/
// GC_TODO:    ] - add argument and description to function comment
// GC_TODO:    EFI_INVALID_PARAMETER - add return value to function comment
// GC_TODO:    EFI_DEVICE_ERROR - add return value to function comment
// GC_TODO:    EFI_DEVICE_ERROR - add return value to function comment
{
  FILE    *OutFptr;
  OPTIONS Options;

  //
  // Process the command-line arguments.
  //
  if (ProcessArgs (Argc, Argv, &Options) != EFI_SUCCESS) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Open the output file
  //
  if ((OutFptr = fopen (Options.OutFileName, "wb")) == NULL) {
      printf (" ERROR: Could not open output file '%s' for writing\n", Options.OutFileName);
    return EFI_DEVICE_ERROR;
  }
  //
  // Write the pad bytes. Do it the slow way (one at a time) for now.
  //
  while (Options.FileSize > 0) {
    if (fwrite (&Options.ByteValue, 1, 1, OutFptr) != 1) {
      fclose (OutFptr);
      printf (" ERROR: Failed to write to output file\n");
      return EFI_DEVICE_ERROR;
    }

    Options.FileSize--;
  }
  //
  // Close the file
  //
  fclose (OutFptr);
  return EFI_SUCCESS;
}

static
EFI_STATUS
ProcessArgs (
  IN INT32          Argc,
  IN INT8           *Argv[],
  IN OUT OPTIONS    *Options
  )
/*++

Routine Description:
  
  Process the command line arguments.

Arguments:

  Argc    - argument count as passed in to the entry point function
  Argv    - array of arguments as passed in to the entry point function
  Options - stucture of where to put the values of the parsed arguments

Returns:

  EFI_SUCCESS if everything looks good
  EFI_INVALID_PARAMETER otherwise

--*/
// GC_TODO:    ] - add argument and description to function comment
{
  UINT32  Multiplier;

  //
  // Clear the options
  //
  memset ((char *) Options, 0, sizeof (OPTIONS));

  //
  // Skip program name
  //
  Argv++;
  Argc--;
  
  if (Argc < 1) {
    Usage();
    return EFI_INVALID_PARAMETER;
  }
  
  if ((strcmp(Argv[0], "-h") == 0) || (strcmp(Argv[0], "--help") == 0) ||
      (strcmp(Argv[0], "-?") == 0) || (strcmp(Argv[0], "/?") == 0)) {
    Usage();
    return EFI_INVALID_PARAMETER;
  }
  
  if ((strcmp(Argv[0], "-V") == 0) || (strcmp(Argv[0], "--version") == 0)) {
    Version();
    return EFI_INVALID_PARAMETER;
  }
 
  if (Argc < 2) {
    Usage ();
    return EFI_INVALID_PARAMETER;
  }
  //
  // If first arg is dash-option, then print usage.
  //
  if (Argv[0][0] == '-') {
    Usage ();
    return EFI_INVALID_PARAMETER;
  }
  //
  // First arg is file name
  //
  Options->OutFileName = Argv[0];
  Argc--;
  Argv++;

  //
  // Second arg is file size. Allow 0x1000, 0x100K, 1024, 1K
  //
  Multiplier = 1;
  if ((Argv[0][strlen (Argv[0]) - 1] == 'k') || (Argv[0][strlen (Argv[0]) - 1] == 'K')) {
    Multiplier = 1024;
  }
  
  //
  // Check for negtive size
  //
  if (Argv[0][0] == '-') {
    printf("ERROR: File size should be non-negtive.\n");
    return EFI_INVALID_PARAMETER;
  }
  
  //
  // Look for 0x prefix on file size
  //
  if ((Argv[0][0] == '0') && ((Argv[0][1] == 'x') || (Argv[0][1] == 'X'))) {
    if (sscanf (Argv[0], "%x", &Options->FileSize) != 1) {
      printf ("ERROR: Invalid file size '%s'\n", Argv[0]);
      Usage ();
      return EFI_INVALID_PARAMETER;
    }
    //
    // Otherwise must be a decimal number
    //
  } else {
    if (sscanf (Argv[0], "%d", &Options->FileSize) != 1) {
      printf ("ERROR: Invalid file size '%s'\n", Argv[0]);
      Usage ();
      return EFI_INVALID_PARAMETER;
    }
  }
  
  Options->FileSize *= Multiplier;
  //
  // Assume byte value of 0xff
  //
  Options->ByteValue = (INT8) (UINT8) 0xFF;
  return EFI_SUCCESS;
}


static
void 
Version(
  void
  )
/*++

Routine Description:

  Print out version information for this utility.

Arguments:

  None
  
Returns:

  None
  
--*/ 
{
  printf ("%s v%d.%d -EDK utility to create a pad file containing fixed data\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
  printf ("Copyright (c) 1999-2006 Intel Corporation. All rights reserved.\n");
}

//
// Print utility usage info
//
static
void
Usage (
  VOID
  )
/*++

Routine Description:

  GC_TODO: Add function description

Arguments:

  None

Returns:

  GC_TODO: add return values

--*/
{ 
  Version();
  
  printf ("\nUsage: %s OutFileName FileSize \n\
   where: \n\
      OutFileName is the name of the output file to generate \n\
      FileSize is the size of the file to create \n\
   Examples: \n\
      %s OutFile.bin 32K \n\
      %s OutFile.bin 0x1000 \n",UTILITY_NAME, UTILITY_NAME, UTILITY_NAME);
}