]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/GnuGenBootSector/GnuGenBootSector.c
Check In tool source code based on Build tool project revision r1655.
[mirror_edk2.git] / BaseTools / Source / C / GnuGenBootSector / GnuGenBootSector.c
1 /** @file
2
3 Copyright 2006 - 2009, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 GnuGenBootSector.c
15
16 Abstract:
17 Reading/writing MBR/DBR.
18 NOTE:
19 If we write MBR to disk, we just update the MBR code and the partition table wouldn't be over written.
20 If we process DBR, we will patch MBR to set first partition active if no active partition exists.
21
22 **/
23
24 #include "CommonLib.h"
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <Common/UefiBaseTypes.h>
29
30 //
31 // Utility Name
32 //
33 #define UTILITY_NAME "GnuGenBootSector"
34
35 //
36 // Utility version information
37 //
38 #define UTILITY_MAJOR_VERSION 0
39 #define UTILITY_MINOR_VERSION 1
40
41 #define MAX_DRIVE 26
42 #define PARTITION_TABLE_OFFSET 0x1BE
43
44 #define SIZE_OF_PARTITION_ENTRY 0x10
45
46 #define PARTITION_ENTRY_STARTLBA_OFFSET 8
47
48 #define PARTITION_ENTRY_NUM 4
49
50 #define DRIVE_UNKNOWN 0
51 #define DRIVE_NO_ROOT_DIR 1
52 #define DRIVE_REMOVABLE 2
53 #define DRIVE_FIXED 3
54 #define DRIVE_REMOTE 4
55 #define DRIVE_CDROM 5
56 #define DRIVE_RAMDISK 6
57
58 typedef struct _DRIVE_TYPE_DESC {
59 UINTN Type;
60 CHAR8 *Description;
61 } DRIVE_TYPE_DESC;
62
63 #define DRIVE_TYPE_ITEM(x) {x, #x}
64
65 DRIVE_TYPE_DESC DriveTypeDesc[] = {
66 DRIVE_TYPE_ITEM (DRIVE_UNKNOWN),
67 DRIVE_TYPE_ITEM (DRIVE_NO_ROOT_DIR),
68 DRIVE_TYPE_ITEM (DRIVE_REMOVABLE),
69 DRIVE_TYPE_ITEM (DRIVE_FIXED),
70 DRIVE_TYPE_ITEM (DRIVE_REMOTE),
71 DRIVE_TYPE_ITEM (DRIVE_CDROM),
72 DRIVE_TYPE_ITEM (DRIVE_RAMDISK),
73 {(UINTN) -1, NULL}
74 };
75
76 typedef struct _DRIVE_INFO {
77 CHAR8 VolumeLetter;
78 DRIVE_TYPE_DESC *DriveType;
79 UINTN DiskNumber;
80 } DRIVE_INFO;
81
82 typedef enum {
83 PathUnknown,
84 PathFile,
85 PathFloppy,
86 PathUsb,
87 PathIde
88 } PATH_TYPE;
89
90 typedef struct _PATH_INFO {
91 CHAR8 *Path;
92 CHAR8 PhysicalPath[260];
93 PATH_TYPE Type;
94 BOOLEAN Input;
95 } PATH_INFO;
96
97 typedef enum {
98 ErrorSuccess,
99 ErrorFileCreate,
100 ErrorFileReadWrite,
101 ErrorNoMbr,
102 ErrorFatType,
103 ErrorPath,
104 } ERROR_STATUS;
105
106 CHAR8 *ErrorStatusDesc[] = {
107 "Success",
108 "Failed to create files",
109 "Failed to read/write files",
110 "No MBR exists",
111 "Failed to detect Fat type",
112 "Inavlid path"
113 };
114
115
116 //UnSupported Windows API functions.
117 UINTN GetLogicalDrives(void) { return 1; }
118
119
120
121 /**
122 Get path information, including physical path for Linux platform.
123
124 @param PathInfo Point to PATH_INFO structure.
125
126 @return whether path is valid.
127 **/
128 ERROR_STATUS
129 GetPathInfo (
130 PATH_INFO *PathInfo
131 )
132 {
133 FILE *f;
134
135 if (strncmp(PathInfo->Path, "/dev/", 5) == 0) {
136 //
137 // Process disk path here.
138 //
139
140 // Process floppy disk
141 if (PathInfo->Path[5] == 'f' && PathInfo->Path[6] == 'd' && PathInfo->Path[8] == '\0') {
142 PathInfo->Type = PathFloppy;
143 strcpy (PathInfo->PhysicalPath, PathInfo->Path);
144
145 return ErrorSuccess;
146 } else {
147 // Other disk types is not supported yet.
148 fprintf (stderr, "ERROR: It's not a floppy disk!\n");
149 return ErrorPath;
150 }
151
152 // Try to open the device.
153 f = fopen(PathInfo->Path,"r");
154 if (f == NULL) {
155 printf ("error :open device failed!\n");
156 return ErrorPath;
157 }
158 fclose (f);
159 return ErrorSuccess;
160 }
161
162 // Process file path here.
163 PathInfo->Type = PathFile;
164 if (PathInfo->Input) {
165 // If path is file path, check whether file is valid.
166 printf("Path = %s\n",PathInfo->Path);
167 f = fopen (PathInfo->Path, "r");
168 if (f == NULL) {
169 fprintf (stderr, "Test error E2003: File was not provided!\n");
170 return ErrorPath;
171 }
172 fclose (f);
173 }
174
175 strcpy(PathInfo->PhysicalPath, PathInfo->Path);
176 return ErrorSuccess;
177
178 }
179
180 VOID
181 ListDrive (
182 VOID
183 )
184 {
185 printf("-l or -list not supported!\n");
186 }
187
188 /**
189 Writing or reading boot sector or MBR according to the argument.
190
191 @param InputInfo PATH_INFO instance for input path
192 @param OutputInfo PATH_INFO instance for output path
193 @param ProcessMbr TRUE is to process MBR, otherwise, processing boot sector
194
195 @return ERROR_STATUS
196 **/
197 ERROR_STATUS
198 ProcessBsOrMbr (
199 PATH_INFO *InputInfo,
200 PATH_INFO *OutputInfo,
201 BOOLEAN ProcessMbr
202 )
203 {
204 CHAR8 FirstSector[0x200] = {0};
205 CHAR8 FirstSectorBackup[0x200] = {0};
206
207 FILE *InputFile;
208 FILE *OutputFile;
209
210
211 InputFile = fopen(InputInfo->PhysicalPath, "r");
212 if (InputFile == NULL) {
213 return ErrorFileReadWrite;
214 }
215
216 if (0x200 != fread(FirstSector, 1, 0x200, InputFile)) {
217 fclose(InputFile);
218 return ErrorFileReadWrite;
219 }
220
221 fclose(InputFile);
222
223 //Not support USB and IDE.
224 if (InputInfo->Type == PathUsb) {
225 printf("USB has not been supported yet!");
226 return ErrorSuccess;
227 }
228
229 if (InputInfo->Type == PathIde) {
230 printf("IDE has not been supported yet!");
231 return ErrorSuccess;
232 }
233
234 //Process Floppy Disk
235 OutputFile = fopen(OutputInfo->PhysicalPath, "w");
236 if (OutputFile == NULL) {
237 return ErrorFileReadWrite;
238 }
239
240 if (OutputInfo->Type != PathFile) {
241 if (ProcessMbr) {
242 //
243 // Use original partition table
244 //
245 if (0x200 != fread (FirstSectorBackup, 1, 0x200, OutputFile)) {
246 fclose(OutputFile);
247 return ErrorFileReadWrite;
248 }
249 memcpy (FirstSector + 0x1BE, FirstSectorBackup + 0x1BE, 0x40);
250 }
251 }
252 if(0x200 != fwrite(FirstSector, 1, 0x200, OutputFile)) {
253 fclose(OutputFile);
254 return ErrorFileReadWrite;
255 }
256
257 fclose(OutputFile);
258 return ErrorSuccess;
259 }
260
261
262 /**
263
264 Displays the standard utility information to SDTOUT
265
266 **/
267 VOID
268 Version (
269 VOID
270 )
271 {
272 printf ("%s v%d.%d -Utility to retrieve and update the boot sector or MBR.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
273 printf ("Copyright (c) 2007-2009 Intel Corporation. All rights reserved.\n");
274 }
275
276
277 VOID
278 PrintUsage (
279 VOID
280 )
281 {
282 Version();
283 printf ("\nUsage: \n\
284 GenBootSector\n\
285 [-l, --list list disks]\n\
286 [-i, --input Filename]\n\
287 [-o, --output Filename]\n\
288 [-m, --mbr process the MBR also]\n\
289 [-v, --verbose]\n\
290 [--version]\n\
291 [-q, --quiet disable all messages except fatal errors]\n\
292 [-d, --debug[#]\n\
293 [-h, --help]\n");
294 }
295
296 INTN
297 main (
298 INTN argc,
299 CHAR8 *argv[]
300 )
301 {
302 CHAR8 *AppName;
303 INTN Index;
304 BOOLEAN ProcessMbr;
305 ERROR_STATUS Status;
306 EFI_STATUS EfiStatus;
307 PATH_INFO InputPathInfo;
308 PATH_INFO OutputPathInfo;
309 UINT64 LogLevel;
310
311 SetUtilityName (UTILITY_NAME);
312
313 ZeroMem(&InputPathInfo, sizeof(PATH_INFO));
314 ZeroMem(&OutputPathInfo, sizeof(PATH_INFO));
315
316 AppName = *argv;
317 argv ++;
318 argc --;
319
320 ProcessMbr = FALSE;
321
322 if (argc == 0) {
323 PrintUsage();
324 return 0;
325 }
326
327 //
328 // Parse command line
329 //
330 for (Index = 0; Index < argc; Index ++) {
331 if ((stricmp (argv[Index], "-l") == 0) || (stricmp (argv[Index], "--list") == 0)) {
332 ListDrive ();
333 return 0;
334 }
335
336 if ((stricmp (argv[Index], "-m") == 0) || (stricmp (argv[Index], "--mbr") == 0)) {
337 ProcessMbr = TRUE;
338 continue;
339 }
340
341 if ((stricmp (argv[Index], "-i") == 0) || (stricmp (argv[Index], "--input") == 0)) {
342 InputPathInfo.Path = argv[Index + 1];
343 InputPathInfo.Input = TRUE;
344 if (InputPathInfo.Path == NULL) {
345 Error (NULL, 0, 1003, "Invalid option value", "Input file name can't be NULL");
346 return 1;
347 }
348 if (InputPathInfo.Path[0] == '-') {
349 Error (NULL, 0, 1003, "Invalid option value", "Input file is missing");
350 return 1;
351 }
352 ++Index;
353 continue;
354 }
355
356 if ((stricmp (argv[Index], "-o") == 0) || (stricmp (argv[Index], "--output") == 0)) {
357 OutputPathInfo.Path = argv[Index + 1];
358 OutputPathInfo.Input = FALSE;
359 if (OutputPathInfo.Path == NULL) {
360 Error (NULL, 0, 1003, "Invalid option value", "Output file name can't be NULL");
361 return 1;
362 }
363 if (OutputPathInfo.Path[0] == '-') {
364 Error (NULL, 0, 1003, "Invalid option value", "Output file is missing");
365 return 1;
366 }
367 ++Index;
368 continue;
369 }
370
371 if ((stricmp (argv[Index], "-h") == 0) || (stricmp (argv[Index], "--help") == 0)) {
372 PrintUsage ();
373 return 0;
374 }
375
376 if (stricmp (argv[Index], "--version") == 0) {
377 Version ();
378 return 0;
379 }
380
381 if ((stricmp (argv[Index], "-v") == 0) || (stricmp (argv[Index], "--verbose") == 0)) {
382 continue;
383 }
384
385 if ((stricmp (argv[Index], "-q") == 0) || (stricmp (argv[Index], "--quiet") == 0)) {
386 continue;
387 }
388
389 if ((stricmp (argv[Index], "-d") == 0) || (stricmp (argv[Index], "--debug") == 0)) {
390 EfiStatus = AsciiStringToUint64 (argv[Index + 1], FALSE, &LogLevel);
391 if (EFI_ERROR (EfiStatus)) {
392 Error (NULL, 0, 1003, "Invalid option value", "%s = %s", argv[Index], argv[Index + 1]);
393 return 1;
394 }
395 if (LogLevel > 9) {
396 Error (NULL, 0, 1003, "Invalid option value", "Debug Level range is 0-9, currnt input level is %d", LogLevel);
397 return 1;
398 }
399 SetPrintLevel (LogLevel);
400 DebugMsg (NULL, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", argv[Index + 1]);
401 ++Index;
402 continue;
403 }
404
405 //
406 // Don't recognize the parameter.
407 //
408 Error (NULL, 0, 1000, "Unknown option", "%s", argv[Index]);
409 return 1;
410 }
411
412 if (InputPathInfo.Path == NULL) {
413 Error (NULL, 0, 1001, "Missing options", "Input file is missing");
414 return 1;
415 }
416
417 if (OutputPathInfo.Path == NULL) {
418 Error (NULL, 0, 1001, "Missing options", "Output file is missing");
419 return 1;
420 }
421
422 if (GetPathInfo(&InputPathInfo) != ErrorSuccess) {
423 Error (NULL, 0, 1003, "Invalid option value", "Input file can't be found.");
424 return 1;
425 }
426
427 if (GetPathInfo(&OutputPathInfo) != ErrorSuccess) {
428 Error (NULL, 0, 1003, "Invalid option value", "Output file can't be found.");
429 return 1;
430 }
431
432 //
433 // Process DBR (Patch or Read)
434 //
435 Status = ProcessBsOrMbr (&InputPathInfo, &OutputPathInfo, ProcessMbr);
436
437 if (Status == ErrorSuccess) {
438 fprintf (
439 stdout,
440 "%s %s: successful!\n",
441 (OutputPathInfo.Type != PathFile) ? "Write" : "Read",
442 ProcessMbr ? "MBR" : "DBR"
443 );
444 return 0;
445 } else {
446 fprintf (
447 stderr,
448 "%s: %s %s: failed - %s (LastError: 0x%x)!\n",
449 (Status == ErrorNoMbr) ? "WARNING" : "ERROR",
450 (OutputPathInfo.Type != PathFile) ? "Write" : "Read",
451 ProcessMbr ? "MBR" : "DBR",
452 ErrorStatusDesc[Status],
453 errno
454 );
455 return 1;
456 }
457 }