]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/C/BootSectImage/bootsectimage.c
BaseTools-Source: Update displayed version information
[mirror_edk2.git] / BaseTools / Source / C / BootSectImage / bootsectimage.c
CommitLineData
30fdf114
LG
1/** @file\r
2\r
3Abstract:\r
4 Patch the BPB information in boot sector image file.\r
5 Patch the MBR code in MBR image file.\r
6\r
fbf23381 7Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
40d841f6 8This program and the accompanying materials \r
30fdf114
LG
9are licensed and made available under the terms and conditions of the BSD License \r
10which accompanies this distribution. The full text of the license may be found at \r
11http://opensource.org/licenses/bsd-license.php \r
12 \r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
15\r
16**/\r
17\r
18#include <stdio.h>\r
19#include <string.h>\r
20#include "fat.h"\r
21#include "mbr.h"\r
22#include "EfiUtilityMsgs.h"\r
6780eef1 23#include "ParseInf.h"\r
30fdf114
LG
24\r
25#define DEBUG_WARN 0x1\r
26#define DEBUG_ERROR 0x2\r
27\r
28//\r
29// Utility Name\r
30//\r
31#define UTILITY_NAME "BootSectImage"\r
32\r
33//\r
34// Utility version information\r
35//\r
fbf23381 36#define UTILITY_MAJOR_VERSION 1
37#define UTILITY_MINOR_VERSION 0
30fdf114
LG
38\r
39void\r
40Version (\r
41 void\r
42 )\r
43/*++\r
44\r
45Routine Description:\r
46\r
47 Displays the standard utility information to SDTOUT\r
48\r
49Arguments:\r
50\r
51 None\r
52\r
53Returns:\r
54\r
55 None\r
56\r
57--*/\r
58{\r
fbf23381 59 printf ("%s Version %d.%d Build %s\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);
30fdf114
LG
60}\r
61\r
62void\r
63Usage (\r
64 void\r
65 )\r
66/*++\r
67\r
68Routine Description:\r
69\r
70 GC_TODO: Add function description\r
71\r
72Arguments:\r
73\r
74\r
75Returns:\r
76\r
77 GC_TODO: add return values\r
78\r
79--*/\r
80{\r
81 Version();\r
fbf23381 82 printf ("Copyright (c) 1999-2016 Intel Corporation. All rights reserved.\n");
83 printf ("\n The BootSectImage tool prints information or patch destination file by source\n");
84 printf (" file for BIOS Parameter Block (BPB) or Master Boot Record (MBR).\n");
30fdf114
LG
85 printf ("\nUsage: \n\\r
86 BootSectImage\n\\r
87 [-f, --force force patch even if the FAT type of SrcImage and DstImage mismatch]\n\\r
88 [-m, --mbr process MBR instead of boot sector]\n\\r
89 [-p, --parse parse SrcImageFile]\n\\r
90 [-o, --output DstImage]\n\\r
91 [-g, --patch patch DstImage using data from SrcImageFile]\n\\r
92 [-v, --verbose]\n\\r
93 [--version]\n\\r
94 [-q, --quiet disable all messages except fatal errors]\n\\r
95 [-d, --debug[#]\n\\r
96 [-h, --help]\n\\r
97 [SrcImageFile]\n");\r
98}\r
99\r
100int WriteToFile (\r
101 void *BootSector, \r
102 char *FileName\r
103 )\r
104/*++\r
105Routine Description:\r
106 Write 512 bytes boot sector to file.\r
107\r
108Arguments:\r
109 BootSector - point to a buffer containing 512 bytes boot sector to write\r
110 FileName - file to write to\r
111\r
112Return:\r
113 int - number of bytes wrote,\r
114 512 indicates write successful\r
115 0 indicates write failure\r
116--*/\r
117{\r
118 FILE *FileHandle;\r
119 int result;\r
120\r
1be2ed90 121 FileHandle = fopen (LongFilePath (FileName), "r+b");\r
30fdf114
LG
122 if (FileHandle == NULL) {\r
123 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "Open file: %s", FileName);\r
124 return 0;\r
125 }\r
126 fseek (FileHandle, 0, SEEK_SET);\r
127\r
128 result = fwrite (BootSector, 1, 512, FileHandle);\r
129 if (result != 512) {\r
130 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "Write file: %s", FileName);\r
131 result = 0;\r
132 }\r
133\r
134 fclose (FileHandle);\r
135 return result;\r
136}\r
137\r
138int ReadFromFile (\r
139 void *BootSector, \r
140 char *FileName\r
141 )\r
142/*++\r
143Routine Description:\r
144 Read first 512 bytes from file.\r
145\r
146Arguments:\r
147 BootSector - point to a buffer receiving the first 512 bytes data from file\r
148 FileName - file to read from\r
149\r
150Return:\r
151 int - number of bytes read,\r
152 512 indicates read successful\r
153 0 indicates read failure\r
154--*/\r
155{\r
156 FILE *FileHandle;\r
157 int result;\r
158\r
1be2ed90 159 FileHandle = fopen (LongFilePath (FileName), "rb");\r
30fdf114
LG
160 if (FileHandle == NULL) {\r
161 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E0001: Error opening file: %s", FileName);\r
162 return 0;\r
163 }\r
164\r
165 result = fread (BootSector, 1, 512, FileHandle);\r
166 if (result != 512) {\r
167 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E0004: Error reading file: %s", FileName);\r
168 result = 0;\r
169 }\r
170\r
171 fclose (FileHandle);\r
172 return result;\r
173}\r
174\r
175char *\r
176FatTypeToString (\r
177 IN FAT_TYPE FatType\r
178 )\r
179/*++\r
180Routine Description:\r
181 Convert enum type of FatType to string\r
182--*/\r
183{\r
184 switch (FatType) {\r
185 case FatTypeFat12:\r
186 return "FAT12";\r
187 case FatTypeFat16:\r
188 return "FAT16";\r
189 case FatTypeFat32:\r
190 return "FAT32";\r
191 default:\r
192 break;\r
193 }\r
194 return "FAT Unknown";\r
195}\r
196\r
197FAT_TYPE\r
198GetFatType (\r
199 IN FAT_BPB_STRUCT *FatBpb\r
200 )\r
201/*++\r
202Routine Description:\r
203 Determine the FAT type according to BIOS Paramater Block (BPB) data\r
204\r
205Arguments:\r
206 FatBpb - BIOS Parameter Block (BPB) data, 512 Bytes\r
207\r
208Return:\r
209 FatTypeUnknown - Cannot determine the FAT type\r
210 FatTypeFat12 - FAT12\r
211 FatTypeFat16 - FAT16\r
212 FatTypeFat32 - FAT32\r
213--*/\r
214{\r
215 FAT_TYPE FatType;\r
216 UINTN RootDirSectors;\r
217 UINTN FATSz;\r
218 UINTN TotSec;\r
219 UINTN DataSec;\r
220 UINTN CountOfClusters;\r
221 CHAR8 FilSysType[9];\r
222\r
223 FatType = FatTypeUnknown;\r
224\r
225 //\r
226 // Simple check\r
227 //\r
228 if (FatBpb->Fat12_16.Signature != FAT_BS_SIGNATURE) {\r
229 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - Signature Invalid - %04x, expected: %04x",\r
230 FatBpb->Fat12_16.Signature, FAT_BS_SIGNATURE);\r
231 return FatTypeUnknown;\r
232 }\r
233\r
234 //\r
235 // Check according to FAT spec\r
236 //\r
237 if ((FatBpb->Fat12_16.BS_jmpBoot[0] != FAT_BS_JMP1) &&\r
238 (FatBpb->Fat12_16.BS_jmpBoot[0] != FAT_BS_JMP2)) {\r
239 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BS_jmpBoot - %02x, expected: %02x or %02x",\r
240 FatBpb->Fat12_16.BS_jmpBoot[0], FAT_BS_JMP1, FAT_BS_JMP2);\r
241 return FatTypeUnknown;\r
242 }\r
243\r
244 if ((FatBpb->Fat12_16.BPB_BytsPerSec != 512) &&\r
245 (FatBpb->Fat12_16.BPB_BytsPerSec != 1024) &&\r
246 (FatBpb->Fat12_16.BPB_BytsPerSec != 2048) &&\r
247 (FatBpb->Fat12_16.BPB_BytsPerSec != 4096)) {\r
248 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_BytsPerSec - %04x, expected: %04x, %04x, %04x, or %04x",\r
249 FatBpb->Fat12_16.BPB_BytsPerSec, 512, 1024, 2048, 4096);\r
250 return FatTypeUnknown;\r
251 }\r
252 if (FatBpb->Fat12_16.BPB_BytsPerSec != 512) {\r
253 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT - BPB_BytsPerSec - %04x, expected: %04x",\r
254 FatBpb->Fat12_16.BPB_BytsPerSec, 512);\r
255 }\r
256 if ((FatBpb->Fat12_16.BPB_SecPerClus != 1) &&\r
257 (FatBpb->Fat12_16.BPB_SecPerClus != 2) &&\r
258 (FatBpb->Fat12_16.BPB_SecPerClus != 4) &&\r
259 (FatBpb->Fat12_16.BPB_SecPerClus != 8) &&\r
260 (FatBpb->Fat12_16.BPB_SecPerClus != 16) &&\r
261 (FatBpb->Fat12_16.BPB_SecPerClus != 32) &&\r
262 (FatBpb->Fat12_16.BPB_SecPerClus != 64) &&\r
263 (FatBpb->Fat12_16.BPB_SecPerClus != 128)) {\r
264 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_SecPerClus - %02x, expected: %02x, %02x, %02x, %02x, %02x, %02x, %02x, or %02x",\r
265 FatBpb->Fat12_16.BPB_BytsPerSec, 1, 2, 4, 8, 16, 32, 64, 128);\r
266 return FatTypeUnknown;\r
267 }\r
268 if (FatBpb->Fat12_16.BPB_BytsPerSec * FatBpb->Fat12_16.BPB_SecPerClus > 32 * 1024) {\r
269 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_BytsPerSec * BPB_SecPerClus - %08x, expected: <= %08x",\r
270 FatBpb->Fat12_16.BPB_BytsPerSec * FatBpb->Fat12_16.BPB_SecPerClus, 32 * 1024);\r
271 return FatTypeUnknown;\r
272 }\r
273 if (FatBpb->Fat12_16.BPB_RsvdSecCnt == 0) {\r
274 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_RsvdSecCnt - %04x, expected: Non-Zero Value",\r
275 FatBpb->Fat12_16.BPB_RsvdSecCnt);\r
276 return FatTypeUnknown;\r
277 }\r
278 if (FatBpb->Fat12_16.BPB_NumFATs != 2) {\r
279 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT - BPB_NumFATs - %02x, expected: %02x",\r
280 FatBpb->Fat12_16.BPB_NumFATs, 2);\r
281 }\r
282 if ((FatBpb->Fat12_16.BPB_Media != 0xF0) &&\r
283 (FatBpb->Fat12_16.BPB_Media != 0xF8) &&\r
284 (FatBpb->Fat12_16.BPB_Media != 0xF9) &&\r
285 (FatBpb->Fat12_16.BPB_Media != 0xFA) &&\r
286 (FatBpb->Fat12_16.BPB_Media != 0xFB) &&\r
287 (FatBpb->Fat12_16.BPB_Media != 0xFC) &&\r
288 (FatBpb->Fat12_16.BPB_Media != 0xFD) &&\r
289 (FatBpb->Fat12_16.BPB_Media != 0xFE) &&\r
290 (FatBpb->Fat12_16.BPB_Media != 0xFF)) {\r
291 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_Media - %02x, expected: %02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x, or %02x",\r
292 FatBpb->Fat12_16.BPB_Media, 0xF0, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF);\r
293 return FatTypeUnknown;\r
294 }\r
295\r
296 //\r
297 // Algo in FAT spec\r
298 //\r
299 RootDirSectors = ((FatBpb->Fat12_16.BPB_RootEntCnt * sizeof(FAT_DIRECTORY_ENTRY)) +\r
300 (FatBpb->Fat12_16.BPB_BytsPerSec - 1)) /\r
301 FatBpb->Fat12_16.BPB_BytsPerSec;\r
302\r
303 if (FatBpb->Fat12_16.BPB_FATSz16 != 0) {\r
304 FATSz = FatBpb->Fat12_16.BPB_FATSz16;\r
305 } else {\r
306 FATSz = FatBpb->Fat32.BPB_FATSz32;\r
307 }\r
308 if (FATSz == 0) {\r
309 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_FATSz16, BPB_FATSz32 - 0, expected: Non-Zero Value");\r
310 return FatTypeUnknown;\r
311 }\r
312\r
313 if (FatBpb->Fat12_16.BPB_TotSec16 != 0) {\r
314 TotSec = FatBpb->Fat12_16.BPB_TotSec16;\r
315 } else {\r
316 TotSec = FatBpb->Fat12_16.BPB_TotSec32;\r
317 }\r
318 if (TotSec == 0) {\r
319 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT - BPB_TotSec16, BPB_TotSec32 - 0, expected: Non-Zero Value");\r
320 return FatTypeUnknown;\r
321 }\r
322\r
323 DataSec = TotSec - (\r
324 FatBpb->Fat12_16.BPB_RsvdSecCnt +\r
325 FatBpb->Fat12_16.BPB_NumFATs * FATSz +\r
326 RootDirSectors\r
327 );\r
328\r
329 CountOfClusters = DataSec / FatBpb->Fat12_16.BPB_SecPerClus;\r
330\r
331 if (CountOfClusters < FAT_MAX_FAT12_CLUSTER) {\r
332 FatType = FatTypeFat12;\r
333 } else if (CountOfClusters < FAT_MAX_FAT16_CLUSTER) {\r
334 FatType = FatTypeFat16;\r
335 } else {\r
336 FatType = FatTypeFat32;\r
337 }\r
338 //\r
339 // Check according to FAT spec\r
340 //\r
341 if (((FatType == FatTypeFat12) || (FatType == FatTypeFat16)) &&\r
342 (FatBpb->Fat12_16.BPB_RsvdSecCnt != 1)) {\r
343 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT12_16 - BPB_RsvdSecCnt - %04x, expected: %04x",\r
344 FatBpb->Fat12_16.BPB_RsvdSecCnt, 1);\r
345 }\r
346 if ((FatType == FatTypeFat32) &&\r
347 (FatBpb->Fat12_16.BPB_RsvdSecCnt != 32)) {\r
348 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BPB_RsvdSecCnt - %04x, expected: %04x",\r
349 FatBpb->Fat12_16.BPB_RsvdSecCnt, 32);\r
350 }\r
351 if ((FatType == FatTypeFat16) &&\r
352 (FatBpb->Fat12_16.BPB_RootEntCnt != 512)) {\r
353 printf ("WARNING: FAT16: BPB_RootEntCnt - %04x, expected - %04x\n",\r
354 FatBpb->Fat12_16.BPB_RootEntCnt, 512);\r
355 }\r
356 if ((FatType == FatTypeFat32) &&\r
357 (FatBpb->Fat12_16.BPB_RootEntCnt != 0)) {\r
358 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_RootEntCnt - %04x, expected: %04x",\r
359 FatBpb->Fat12_16.BPB_RootEntCnt, 0);\r
360 return FatTypeUnknown;\r
361 }\r
362 if ((FatType == FatTypeFat32) &&\r
363 (FatBpb->Fat12_16.BPB_TotSec16 != 0)) {\r
364 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_TotSec16 - %04x, expected: %04x",\r
365 FatBpb->Fat12_16.BPB_TotSec16, 0);\r
366 return FatTypeUnknown;\r
367 }\r
368 if ((FatType == FatTypeFat32) &&\r
369 (FatBpb->Fat12_16.BPB_FATSz16 != 0)) {\r
370 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_FATSz16 - %04x, expected: %04x",\r
371 FatBpb->Fat12_16.BPB_FATSz16, 0);\r
372 return FatTypeUnknown;\r
373 }\r
374 if ((FatType == FatTypeFat32) &&\r
375 (FatBpb->Fat12_16.BPB_TotSec32 == 0)) {\r
376 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_TotSec32 - %04x, expected: Non-Zero",\r
fd171542 377 (unsigned) FatBpb->Fat12_16.BPB_TotSec32);\r
30fdf114
LG
378 return FatTypeUnknown;\r
379 }\r
380 if ((FatType == FatTypeFat32) &&\r
381 (FatBpb->Fat32.BPB_FATSz32 == 0)) {\r
382 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_FATSz32 - %08x, expected: Non-Zero",\r
fd171542 383 (unsigned) FatBpb->Fat32.BPB_FATSz32);\r
30fdf114
LG
384 return FatTypeUnknown;\r
385 }\r
386 if ((FatType == FatTypeFat32) &&\r
387 (FatBpb->Fat32.BPB_FSVer != 0)) {\r
388 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BPB_FSVer - %08x, expected: %04x",\r
389 FatBpb->Fat32.BPB_FSVer, 0);\r
390 }\r
391 if ((FatType == FatTypeFat32) &&\r
392 (FatBpb->Fat32.BPB_RootClus != 2)) {\r
393 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BPB_RootClus - %08x, expected: %04x",\r
fd171542 394 (unsigned) FatBpb->Fat32.BPB_RootClus, 2);\r
30fdf114
LG
395 }\r
396 if ((FatType == FatTypeFat32) &&\r
397 (FatBpb->Fat32.BPB_FSInfo != 1)) {\r
398 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BPB_FSInfo - %08x, expected: %04x",\r
399 FatBpb->Fat32.BPB_FSInfo, 1);\r
400 }\r
401 if ((FatType == FatTypeFat32) &&\r
402 (FatBpb->Fat32.BPB_BkBootSec != 6)) {\r
403 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BPB_BkBootSec - %08x, expected: %04x",\r
404 FatBpb->Fat32.BPB_BkBootSec, 6);\r
405 }\r
406 if ((FatType == FatTypeFat32) &&\r
407 ((*(UINT32 *)FatBpb->Fat32.BPB_Reserved != 0) ||\r
408 (*((UINT32 *)FatBpb->Fat32.BPB_Reserved + 1) != 0) ||\r
409 (*((UINT32 *)FatBpb->Fat32.BPB_Reserved + 2) != 0))) {\r
410 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BPB_Reserved - %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x, expected: 0",\r
411 FatBpb->Fat32.BPB_Reserved[0],\r
412 FatBpb->Fat32.BPB_Reserved[1],\r
413 FatBpb->Fat32.BPB_Reserved[2],\r
414 FatBpb->Fat32.BPB_Reserved[3],\r
415 FatBpb->Fat32.BPB_Reserved[4],\r
416 FatBpb->Fat32.BPB_Reserved[5],\r
417 FatBpb->Fat32.BPB_Reserved[6],\r
418 FatBpb->Fat32.BPB_Reserved[7],\r
419 FatBpb->Fat32.BPB_Reserved[8],\r
420 FatBpb->Fat32.BPB_Reserved[9],\r
421 FatBpb->Fat32.BPB_Reserved[10],\r
422 FatBpb->Fat32.BPB_Reserved[11]);\r
423 return FatTypeUnknown;\r
424 }\r
425 if (((FatType == FatTypeFat12) || (FatType == FatTypeFat16)) &&\r
426 (FatBpb->Fat12_16.BS_Reserved1 != 0)) {\r
427 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT12_16 - BS_Reserved1 - %02x, expected: 0\n",\r
428 FatBpb->Fat12_16.BS_Reserved1);\r
429 return FatTypeUnknown;\r
430 }\r
431 if ((FatType == FatTypeFat32) &&\r
432 (FatBpb->Fat32.BS_Reserved1 != 0)) {\r
433 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BS_Reserved1 - %02x, expected: 0\n",\r
434 FatBpb->Fat32.BS_Reserved1);\r
435 return FatTypeUnknown;\r
436 }\r
437 if (((FatType == FatTypeFat12) || (FatType == FatTypeFat16)) &&\r
438 (FatBpb->Fat12_16.BS_BootSig != FAT_BS_BOOTSIG)) {\r
439 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT12_16 - BS_BootSig - %02x, expected: %02x\n",\r
440 FatBpb->Fat12_16.BS_BootSig, FAT_BS_BOOTSIG);\r
441 return FatTypeUnknown;\r
442 }\r
443 if ((FatType == FatTypeFat32) &&\r
444 (FatBpb->Fat32.BS_BootSig != FAT_BS_BOOTSIG)) {\r
445 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3003: FAT32 - BS_BootSig - %02x, expected: %02x\n",\r
446 FatBpb->Fat32.BS_BootSig, FAT_BS_BOOTSIG);\r
447 return FatTypeUnknown;\r
448 }\r
449 \r
450 if ((FatType == FatTypeFat12) || (FatType == FatTypeFat16)) {\r
451 memcpy (FilSysType, FatBpb->Fat12_16.BS_FilSysType, 8);\r
452 FilSysType[8] = 0;\r
453 if ((FatType == FatTypeFat12) && \r
454 (strcmp (FilSysType, FAT12_FILSYSTYPE) != 0) &&\r
455 (strcmp (FilSysType, FAT_FILSYSTYPE) != 0)) {\r
456 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT12 - BS_FilSysType - %s, expected: %s, or %s\n",\r
457 FilSysType, FAT12_FILSYSTYPE, FAT_FILSYSTYPE);\r
458 }\r
459 if ((FatType == FatTypeFat16) && \r
460 (strcmp (FilSysType, FAT16_FILSYSTYPE) != 0) &&\r
461 (strcmp (FilSysType, FAT_FILSYSTYPE) != 0)) {\r
462 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT16 - BS_FilSysType - %s, expected: %s, or %s\n",\r
463 FilSysType, FAT16_FILSYSTYPE, FAT_FILSYSTYPE);\r
464 }\r
465 }\r
466 if (FatType == FatTypeFat32) {\r
467 memcpy (FilSysType, FatBpb->Fat32.BS_FilSysType, 8);\r
468 FilSysType[8] = 0;\r
469 if (strcmp (FilSysType, FAT32_FILSYSTYPE) != 0) {\r
470 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3003: FAT32 - BS_FilSysType - %s, expected: %s\n",\r
471 FilSysType, FAT32_FILSYSTYPE);\r
472 }\r
473 }\r
474\r
475 //\r
476 // pass all check, get FAT type\r
477 //\r
478 return FatType;\r
479}\r
480\r
481\r
482void\r
483ParseBootSector (\r
484 char *FileName\r
485 )\r
486{\r
487 FAT_BPB_STRUCT FatBpb;\r
488 FAT_TYPE FatType;\r
489 \r
490 if (ReadFromFile ((void *)&FatBpb, FileName) == 0) {\r
491 return ;\r
492 }\r
493 \r
494 FatType = GetFatType (&FatBpb);\r
495 if (FatType <= FatTypeUnknown || FatType >= FatTypeMax) {\r
496 printf ("ERROR: E3002: Unknown FAT Type!\n");\r
497 return;\r
498 }\r
499\r
500 printf ("\nBoot Sector %s:\n", FatTypeToString (FatType));\r
501 printf ("\n");\r
502 printf (" Offset Title Data\n");\r
503 printf ("==================================================================\n");\r
504 printf (" 0 JMP instruction %02x %02x %02x\n",\r
505 FatBpb.Fat12_16.BS_jmpBoot[0],\r
506 FatBpb.Fat12_16.BS_jmpBoot[1],\r
507 FatBpb.Fat12_16.BS_jmpBoot[2]);\r
508 printf (" 3 OEM %c%c%c%c%c%c%c%c\n",\r
509 FatBpb.Fat12_16.BS_OEMName[0],\r
510 FatBpb.Fat12_16.BS_OEMName[1],\r
511 FatBpb.Fat12_16.BS_OEMName[2],\r
512 FatBpb.Fat12_16.BS_OEMName[3],\r
513 FatBpb.Fat12_16.BS_OEMName[4],\r
514 FatBpb.Fat12_16.BS_OEMName[5],\r
515 FatBpb.Fat12_16.BS_OEMName[6],\r
516 FatBpb.Fat12_16.BS_OEMName[7]);\r
517 printf ("\n");\r
518 printf ("BIOS Parameter Block\n");\r
519 printf (" B Bytes per sector %04x\n", FatBpb.Fat12_16.BPB_BytsPerSec);\r
520 printf (" D Sectors per cluster %02x\n", FatBpb.Fat12_16.BPB_SecPerClus);\r
521 printf (" E Reserved sectors %04x\n", FatBpb.Fat12_16.BPB_RsvdSecCnt);\r
522 printf (" 10 Number of FATs %02x\n", FatBpb.Fat12_16.BPB_NumFATs);\r
523 printf (" 11 Root entries %04x\n", FatBpb.Fat12_16.BPB_RootEntCnt);\r
524 printf (" 13 Sectors (under 32MB) %04x\n", FatBpb.Fat12_16.BPB_TotSec16);\r
525 printf (" 15 Media descriptor %02x\n", FatBpb.Fat12_16.BPB_Media);\r
526 printf (" 16 Sectors per FAT (small vol.) %04x\n", FatBpb.Fat12_16.BPB_FATSz16);\r
527 printf (" 18 Sectors per track %04x\n", FatBpb.Fat12_16.BPB_SecPerTrk);\r
528 printf (" 1A Heads %04x\n", FatBpb.Fat12_16.BPB_NumHeads);\r
fd171542 529 printf (" 1C Hidden sectors %08x\n", (unsigned) FatBpb.Fat12_16.BPB_HiddSec);\r
530 printf (" 20 Sectors (over 32MB) %08x\n", (unsigned) FatBpb.Fat12_16.BPB_TotSec32);\r
30fdf114
LG
531 printf ("\n");\r
532 if (FatType != FatTypeFat32) {\r
533 printf (" 24 BIOS drive %02x\n", FatBpb.Fat12_16.BS_DrvNum);\r
534 printf (" 25 (Unused) %02x\n", FatBpb.Fat12_16.BS_Reserved1);\r
535 printf (" 26 Ext. boot signature %02x\n", FatBpb.Fat12_16.BS_BootSig);\r
fd171542 536 printf (" 27 Volume serial number %08x\n", (unsigned) FatBpb.Fat12_16.BS_VolID);\r
30fdf114
LG
537 printf (" 2B Volume lable %c%c%c%c%c%c%c%c%c%c%c\n",\r
538 FatBpb.Fat12_16.BS_VolLab[0],\r
539 FatBpb.Fat12_16.BS_VolLab[1],\r
540 FatBpb.Fat12_16.BS_VolLab[2],\r
541 FatBpb.Fat12_16.BS_VolLab[3],\r
542 FatBpb.Fat12_16.BS_VolLab[4],\r
543 FatBpb.Fat12_16.BS_VolLab[5],\r
544 FatBpb.Fat12_16.BS_VolLab[6],\r
545 FatBpb.Fat12_16.BS_VolLab[7],\r
546 FatBpb.Fat12_16.BS_VolLab[8],\r
547 FatBpb.Fat12_16.BS_VolLab[9],\r
548 FatBpb.Fat12_16.BS_VolLab[10]);\r
549 printf (" 36 File system %c%c%c%c%c%c%c%c\n",\r
550 FatBpb.Fat12_16.BS_FilSysType[0],\r
551 FatBpb.Fat12_16.BS_FilSysType[1],\r
552 FatBpb.Fat12_16.BS_FilSysType[2],\r
553 FatBpb.Fat12_16.BS_FilSysType[3],\r
554 FatBpb.Fat12_16.BS_FilSysType[4],\r
555 FatBpb.Fat12_16.BS_FilSysType[5],\r
556 FatBpb.Fat12_16.BS_FilSysType[6],\r
557 FatBpb.Fat12_16.BS_FilSysType[7]);\r
558 printf ("\n");\r
559 } else {\r
560 printf ("FAT32 Section\n");\r
fd171542 561 printf (" 24 Sectors per FAT (large vol.) %08x\n", (unsigned) FatBpb.Fat32.BPB_FATSz32);\r
30fdf114
LG
562 printf (" 28 Flags %04x\n", FatBpb.Fat32.BPB_ExtFlags);\r
563 printf (" 2A Version %04x\n", FatBpb.Fat32.BPB_FSVer);\r
fd171542 564 printf (" 2C Root dir 1st cluster %08x\n", (unsigned) FatBpb.Fat32.BPB_RootClus);\r
30fdf114
LG
565 printf (" 30 FSInfo sector %04x\n", FatBpb.Fat32.BPB_FSInfo);\r
566 printf (" 32 Backup boot sector %04x\n", FatBpb.Fat32.BPB_BkBootSec);\r
567 printf (" 34 (Reserved) %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",\r
568 FatBpb.Fat32.BPB_Reserved[0],\r
569 FatBpb.Fat32.BPB_Reserved[1],\r
570 FatBpb.Fat32.BPB_Reserved[2],\r
571 FatBpb.Fat32.BPB_Reserved[3],\r
572 FatBpb.Fat32.BPB_Reserved[4],\r
573 FatBpb.Fat32.BPB_Reserved[5],\r
574 FatBpb.Fat32.BPB_Reserved[6],\r
575 FatBpb.Fat32.BPB_Reserved[7],\r
576 FatBpb.Fat32.BPB_Reserved[8],\r
577 FatBpb.Fat32.BPB_Reserved[9],\r
578 FatBpb.Fat32.BPB_Reserved[10],\r
579 FatBpb.Fat32.BPB_Reserved[11]);\r
580 printf ("\n");\r
581 printf (" 40 BIOS drive %02x\n", FatBpb.Fat32.BS_DrvNum);\r
582 printf (" 41 (Unused) %02x\n", FatBpb.Fat32.BS_Reserved1);\r
583 printf (" 42 Ext. boot signature %02x\n", FatBpb.Fat32.BS_BootSig);\r
fd171542 584 printf (" 43 Volume serial number %08x\n", (unsigned) FatBpb.Fat32.BS_VolID);\r
30fdf114
LG
585 printf (" 47 Volume lable %c%c%c%c%c%c%c%c%c%c%c\n",\r
586 FatBpb.Fat32.BS_VolLab[0],\r
587 FatBpb.Fat32.BS_VolLab[1],\r
588 FatBpb.Fat32.BS_VolLab[2],\r
589 FatBpb.Fat32.BS_VolLab[3],\r
590 FatBpb.Fat32.BS_VolLab[4],\r
591 FatBpb.Fat32.BS_VolLab[5],\r
592 FatBpb.Fat32.BS_VolLab[6],\r
593 FatBpb.Fat32.BS_VolLab[7],\r
594 FatBpb.Fat32.BS_VolLab[8],\r
595 FatBpb.Fat32.BS_VolLab[9],\r
596 FatBpb.Fat32.BS_VolLab[10]);\r
597 printf (" 52 File system %c%c%c%c%c%c%c%c\n",\r
598 FatBpb.Fat32.BS_FilSysType[0],\r
599 FatBpb.Fat32.BS_FilSysType[1],\r
600 FatBpb.Fat32.BS_FilSysType[2],\r
601 FatBpb.Fat32.BS_FilSysType[3],\r
602 FatBpb.Fat32.BS_FilSysType[4],\r
603 FatBpb.Fat32.BS_FilSysType[5],\r
604 FatBpb.Fat32.BS_FilSysType[6],\r
605 FatBpb.Fat32.BS_FilSysType[7]);\r
606 printf ("\n");\r
607 }\r
608 printf (" 1FE Signature %04x\n", FatBpb.Fat12_16.Signature);\r
609 printf ("\n");\r
610\r
611 \r
612 return ;\r
613}\r
614\r
615void\r
616PatchBootSector (\r
617 char *DestFileName,\r
618 char *SourceFileName,\r
619 BOOLEAN ForcePatch\r
620 )\r
621/*++\r
622Routine Description:\r
623 Patch destination file according to the information from source file.\r
624 Only patch BPB data but leave boot code un-touched.\r
625\r
626Arguments:\r
627 DestFileName - Destination file to patch\r
628 SourceFileName - Source file where patch from\r
629--*/\r
630{\r
631 FAT_BPB_STRUCT DestFatBpb;\r
632 FAT_BPB_STRUCT SourceFatBpb;\r
633 FAT_TYPE DestFatType;\r
634 FAT_TYPE SourceFatType;\r
635 CHAR8 VolLab[11];\r
636 CHAR8 FilSysType[8];\r
637 \r
638 if (ReadFromFile ((void *)&DestFatBpb, DestFileName) == 0) {\r
639 return ;\r
640 }\r
641 if (ReadFromFile ((void *)&SourceFatBpb, SourceFileName) == 0) {\r
642 return ;\r
643 }\r
644 \r
645 DestFatType = GetFatType (&DestFatBpb);\r
646 SourceFatType = GetFatType (&SourceFatBpb);\r
647\r
648 if (DestFatType != SourceFatType) {\r
649 //\r
650 // FAT type mismatch\r
651 //\r
652 if (ForcePatch) {\r
653 DebugMsg (NULL, 0, DEBUG_WARN, NULL, "ERROR: E3004: FAT type mismatch: Source - %s, Dest - %s", \r
654 FatTypeToString(SourceFatType), FatTypeToString(DestFatType));\r
655 } else {\r
656 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3004: FAT type mismatch: Source - %s, Dest - %s", \r
657 FatTypeToString(SourceFatType), FatTypeToString(DestFatType));\r
658 return ;\r
659 }\r
660 }\r
661\r
662 if (SourceFatType <= FatTypeUnknown || SourceFatType >= FatTypeMax) {\r
663 DebugMsg (NULL, 0, DEBUG_ERROR, NULL, "ERROR: E3002: Unknown FAT Type!\n");\r
664 return;\r
665 }\r
666\r
667 //\r
668 // Copy BPB/boot data (excluding BS_jmpBoot, BS_OEMName, BootCode and Signature) from SourceFatBpb to DestFatBpb\r
669 //\r
670 printf ("Patching %s BPB: ", FatTypeToString (SourceFatType));\r
671 if (SourceFatType != FatTypeFat32) {\r
672 memcpy (\r
673 &DestFatBpb.Fat12_16.BPB_BytsPerSec,\r
674 &SourceFatBpb.Fat12_16.BPB_BytsPerSec,\r
675 ((UINTN)&DestFatBpb.Fat12_16.Reserved - (UINTN)&DestFatBpb.Fat12_16.BPB_BytsPerSec)\r
676 );\r
677 } else {\r
678 memcpy (\r
679 &DestFatBpb.Fat32.BPB_BytsPerSec,\r
680 &SourceFatBpb.Fat32.BPB_BytsPerSec,\r
681 ((UINTN)&DestFatBpb.Fat32.Reserved - (UINTN)&DestFatBpb.Fat32.BPB_BytsPerSec)\r
682 );\r
683 }\r
684\r
685 //\r
686 // Set BS_VolLab and BS_FilSysType of DestFatBpb\r
687 //\r
688 // BS_VolLab BS_FilSysType\r
689 // FAT12: EFI FAT12 FAT12\r
690 // FAT16: EFI FAT16 FAT16\r
691 // FAT32: EFI FAT32 FAT32\r
692 //\r
693 if (SourceFatType == FatTypeFat32) {\r
694 memcpy (VolLab, "EFI FAT32 ", sizeof(VolLab));\r
695 memcpy (FilSysType, FAT32_FILSYSTYPE, sizeof(FilSysType));\r
696 } else if (SourceFatType == FatTypeFat16) {\r
697 memcpy (VolLab, "EFI FAT16 ", sizeof(VolLab));\r
698 memcpy (FilSysType, FAT16_FILSYSTYPE, sizeof(FilSysType));\r
699 } else {\r
700 memcpy (VolLab, "EFI FAT12 ", sizeof(VolLab));\r
701 memcpy (FilSysType, FAT12_FILSYSTYPE, sizeof(FilSysType));\r
702 }\r
703 if (SourceFatType != FatTypeFat32) {\r
704 memcpy (DestFatBpb.Fat12_16.BS_VolLab, VolLab, sizeof(VolLab));\r
705 memcpy (DestFatBpb.Fat12_16.BS_FilSysType, FilSysType, sizeof(FilSysType));\r
706 } else {\r
707 memcpy (DestFatBpb.Fat32.BS_VolLab, VolLab, sizeof(VolLab));\r
708 memcpy (DestFatBpb.Fat32.BS_FilSysType, FilSysType, sizeof(FilSysType));\r
709 }\r
710 \r
711 //\r
712 // Set Signature of DestFatBpb to 55AA\r
713 //\r
714 DestFatBpb.Fat12_16.Signature = FAT_BS_SIGNATURE;\r
715\r
716 //\r
717 // Write DestFatBpb\r
718 //\r
719 if (WriteToFile ((void *)&DestFatBpb, DestFileName)) {\r
720 printf ("successful!\n");\r
721 } else {\r
722 printf ("failed!\n");\r
723 }\r
724\r
725 return ;\r
726}\r
727\r
728void\r
729ParseMbr (\r
730 char *FileName\r
731 )\r
732{\r
733 MASTER_BOOT_RECORD Mbr;\r
734 \r
735 if (ReadFromFile ((void *)&Mbr, FileName) == 0) {\r
736 return ;\r
737 }\r
738 \r
739 printf ("\nMaster Boot Record:\n");\r
740 printf ("\n");\r
741 printf (" Offset Title Value\n");\r
742 printf ("==================================================================\n");\r
743 printf (" 0 Master bootstrap loader code (not list)\n");\r
fd171542 744 printf (" 1B8 Windows disk signature %08x\n", (unsigned) Mbr.UniqueMbrSignature);\r
30fdf114
LG
745 printf ("\n");\r
746 printf ("Partition Table Entry #1\n");\r
747 printf (" 1BE 80 = active partition %02x\n", Mbr.PartitionRecord[0].BootIndicator);\r
748 printf (" 1BF Start head %02x\n", Mbr.PartitionRecord[0].StartHead);\r
749 printf (" 1C0 Start sector %02x\n", Mbr.PartitionRecord[0].StartSector);\r
750 printf (" 1C1 Start cylinder %02x\n", Mbr.PartitionRecord[0].StartTrack);\r
751 printf (" 1C2 Partition type indicator %02x\n", Mbr.PartitionRecord[0].OSType);\r
752 printf (" 1C3 End head %02x\n", Mbr.PartitionRecord[0].EndHead);\r
753 printf (" 1C4 End sector %02x\n", Mbr.PartitionRecord[0].EndSector);\r
754 printf (" 1C5 End cylinder %02x\n", Mbr.PartitionRecord[0].EndTrack);\r
fd171542 755 printf (" 1C6 Sectors preceding partition %08x\n", (unsigned) Mbr.PartitionRecord[0].StartingLBA);\r
756 printf (" 1CA Sectors in partition %08x\n", (unsigned) Mbr.PartitionRecord[0].SizeInLBA);\r
30fdf114
LG
757 printf ("\n");\r
758 printf ("Partition Table Entry #2\n");\r
759 printf (" 1CE 80 = active partition %02x\n", Mbr.PartitionRecord[1].BootIndicator);\r
760 printf (" 1CF Start head %02x\n", Mbr.PartitionRecord[1].StartHead);\r
761 printf (" 1D0 Start sector %02x\n", Mbr.PartitionRecord[1].StartSector);\r
762 printf (" 1D1 Start cylinder %02x\n", Mbr.PartitionRecord[1].StartTrack);\r
763 printf (" 1D2 Partition type indicator %02x\n", Mbr.PartitionRecord[1].OSType);\r
764 printf (" 1D3 End head %02x\n", Mbr.PartitionRecord[1].EndHead);\r
765 printf (" 1D4 End sector %02x\n", Mbr.PartitionRecord[1].EndSector);\r
766 printf (" 1D5 End cylinder %02x\n", Mbr.PartitionRecord[1].EndTrack);\r
fd171542 767 printf (" 1D6 Sectors preceding partition %08x\n", (unsigned) Mbr.PartitionRecord[1].StartingLBA);\r
768 printf (" 1DA Sectors in partition %08x\n", (unsigned) Mbr.PartitionRecord[1].SizeInLBA);\r
30fdf114
LG
769 printf ("\n");\r
770 printf ("Partition Table Entry #3\n");\r
771 printf (" 1DE 80 = active partition %02x\n", Mbr.PartitionRecord[2].BootIndicator);\r
772 printf (" 1DF Start head %02x\n", Mbr.PartitionRecord[2].StartHead);\r
773 printf (" 1E0 Start sector %02x\n", Mbr.PartitionRecord[2].StartSector);\r
774 printf (" 1E1 Start cylinder %02x\n", Mbr.PartitionRecord[2].StartTrack);\r
775 printf (" 1E2 Partition type indicator %02x\n", Mbr.PartitionRecord[2].OSType);\r
776 printf (" 1E3 End head %02x\n", Mbr.PartitionRecord[2].EndHead);\r
777 printf (" 1E4 End sector %02x\n", Mbr.PartitionRecord[2].EndSector);\r
778 printf (" 1E5 End cylinder %02x\n", Mbr.PartitionRecord[2].EndTrack);\r
fd171542 779 printf (" 1E6 Sectors preceding partition %08x\n", (unsigned) Mbr.PartitionRecord[2].StartingLBA);\r
780 printf (" 1EA Sectors in partition %08x\n", (unsigned) Mbr.PartitionRecord[2].SizeInLBA);\r
30fdf114
LG
781 printf ("\n");\r
782 printf ("Partition Table Entry #4\n");\r
783 printf (" 1EE 80 = active partition %02x\n", Mbr.PartitionRecord[3].BootIndicator);\r
784 printf (" 1EF Start head %02x\n", Mbr.PartitionRecord[3].StartHead);\r
785 printf (" 1F0 Start sector %02x\n", Mbr.PartitionRecord[3].StartSector);\r
786 printf (" 1F1 Start cylinder %02x\n", Mbr.PartitionRecord[3].StartTrack);\r
787 printf (" 1F2 Partition type indicator %02x\n", Mbr.PartitionRecord[3].OSType);\r
788 printf (" 1F3 End head %02x\n", Mbr.PartitionRecord[3].EndHead);\r
789 printf (" 1F4 End sector %02x\n", Mbr.PartitionRecord[3].EndSector);\r
790 printf (" 1F5 End cylinder %02x\n", Mbr.PartitionRecord[3].EndTrack);\r
fd171542 791 printf (" 1F6 Sectors preceding partition %08x\n", (unsigned) Mbr.PartitionRecord[3].StartingLBA);\r
792 printf (" 1FA Sectors in partition %08x\n", (unsigned) Mbr.PartitionRecord[3].SizeInLBA);\r
30fdf114
LG
793 printf ("\n");\r
794 printf (" 1FE Signature %04x\n", Mbr.Signature);\r
795 printf ("\n");\r
796\r
797 return ;\r
798}\r
799\r
800void\r
801PatchMbr (\r
802 char *DestFileName,\r
803 char *SourceFileName\r
804 )\r
805{\r
806 MASTER_BOOT_RECORD DestMbr;\r
807 MASTER_BOOT_RECORD SourceMbr;\r
808 \r
809 if (ReadFromFile ((void *)&DestMbr, DestFileName) == 0) {\r
810 return ;\r
811 }\r
812 if (ReadFromFile ((void *)&SourceMbr, SourceFileName) == 0) {\r
813 return ;\r
814 }\r
815 \r
816 if (SourceMbr.Signature != MBR_SIGNATURE) {\r
817 printf ("ERROR: E3000: Invalid MBR!\n");\r
818 return;\r
819 }\r
820\r
821 printf ("Patching MBR:\n");\r
822 memcpy (\r
823 &DestMbr.PartitionRecord[0],\r
824 &SourceMbr.PartitionRecord[0],\r
825 sizeof(DestMbr.PartitionRecord)\r
826 );\r
827\r
828 DestMbr.Signature = MBR_SIGNATURE;\r
829\r
830\r
831 if (WriteToFile ((void *)&DestMbr, DestFileName)) {\r
832 printf ("\tsuccessful!\n");\r
833 }\r
834\r
835 return ;\r
836}\r
837\r
838\r
839int\r
840main (\r
841 int argc,\r
842 char *argv[]\r
843 )\r
844{\r
845 char *SrcImage;\r
846 char *DstImage;\r
847 BOOLEAN ForcePatch; // -f\r
848 BOOLEAN ProcessMbr; // -m\r
849 BOOLEAN DoParse; // -p SrcImage or -g SrcImage DstImage\r
850 BOOLEAN Verbose; // -v\r
6780eef1
LG
851 UINT64 LogLevel;\r
852 EFI_STATUS EfiStatus;\r
853\r
30fdf114
LG
854 SrcImage = DstImage = NULL;\r
855 ForcePatch = FALSE;\r
856 ProcessMbr = FALSE;\r
857 DoParse = TRUE;\r
858 Verbose = FALSE;\r
859\r
860 SetUtilityName ("bootsectimage");\r
861\r
862 argc--; argv++;\r
863\r
864 if (argc == 0) {\r
865 Usage ();\r
866 return -1;\r
867 }\r
868\r
869 while (argc != 0) {\r
870 if (strcmp (*argv, "-f") == 0 || strcmp (*argv, "--force") == 0) {\r
871 ForcePatch = TRUE;\r
872 } else if (strcmp (*argv, "-p") == 0 || strcmp (*argv, "--parse") == 0) {\r
873 DoParse = TRUE;\r
874 argc--; argv++;\r
875 if (argc < 1) {\r
876 Usage ();\r
877 return -1;\r
878 }\r
879 SrcImage = *argv;\r
880 } else if (strcmp (*argv, "-g") == 0 || strcmp (*argv, "--patch") == 0) {\r
881 DoParse = FALSE;\r
882 argc--; argv++;\r
883 if (argc < 2) {\r
884 Usage ();\r
885 return -1;\r
886 }\r
887 SrcImage = *argv;\r
888 argc--; argv++;\r
889 DstImage = *argv;\r
890 } else if (strcmp (*argv, "-m") == 0 || strcmp (*argv, "--mbr") == 0) {\r
891 ProcessMbr = TRUE;\r
892 } else if (strcmp (*argv, "-v") == 0 || strcmp (*argv, "--verbose") == 0) {\r
893 Verbose = TRUE;\r
d90aa462 894 } else if (strcmp (*argv, "--version") == 0) {\r
895 Version();\r
896 return 0;\r
6780eef1
LG
897 } else if ((stricmp (*argv, "-d") == 0) || (stricmp (*argv, "--debug") == 0)) {\r
898 argc--; argv++;\r
899 if (argc < 1) {\r
900 Usage ();\r
901 return -1;\r
902 }\r
903 EfiStatus = AsciiStringToUint64 (*argv, FALSE, &LogLevel);\r
904 if (EFI_ERROR (EfiStatus)) {\r
905 Error (NULL, 0, 1003, "Invalid option value", "%s = %s", "--debug", *argv);\r
906 return 1;\r
907 }\r
908 if (LogLevel > 9) {\r
909 Error (NULL, 0, 1003, "Invalid option value", "Debug Level range is 0-9, currnt input level is %d", (int) LogLevel);\r
910 return 1;\r
911 }\r
912 SetPrintLevel (LogLevel);\r
913 DebugMsg (NULL, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", *argv);\r
30fdf114
LG
914 } else {\r
915 Usage ();\r
916 return -1;\r
917 }\r
918\r
919 argc--; argv++;\r
920 }\r
921\r
922 if (ForcePatch && DoParse) {\r
923 printf ("ERROR: E1002: Conflicting options: -f, -p. Cannot apply force(-f) to parse(-p)!\n");\r
924 Usage ();\r
925 return -1;\r
926 }\r
927 if (ForcePatch && !DoParse && ProcessMbr) {\r
928 printf ("ERROR: E1002: Conflicting options: -f, -g -m. Cannot apply force(-f) to processing MBR (-g -m)!\n");\r
929 Usage ();\r
930 return -1;\r
931 }\r
932\r
933 if (Verbose) {\r
934 SetPrintLevel (VERBOSE_LOG_LEVEL);\r
935 } else {\r
936 SetPrintLevel (KEY_LOG_LEVEL);\r
937 }\r
938\r
939 if (DoParse) {\r
940 if (ProcessMbr) {\r
941 ParseMbr (SrcImage);\r
942 } else {\r
943 ParseBootSector (SrcImage);\r
944 }\r
945 } else {\r
946 if (ProcessMbr) {\r
947 PatchMbr (DstImage, SrcImage);\r
948 } else {\r
949 PatchBootSector (DstImage, SrcImage, ForcePatch);\r
950 }\r
951 }\r
952\r
953 return 0;\r
954}\r
955\r