]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenBootSector/GenBootSector.c
BaseTools/GenBootSector: Add/refine boundary checks for strcpy/strcat
[mirror_edk2.git] / BaseTools / Source / C / GenBootSector / GenBootSector.c
index 05839bb736e356095d49f108232a15136fcd3494..c02de49ba2ad6e3e67e59f68853e8655cf572e68 100644 (file)
@@ -1,7 +1,11 @@
 /** @file\r
-\r
-Copyright 2006 - 2008, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
+Reading/writing MBR/DBR.\r
+  NOTE:\r
+    If we write MBR to disk, we just update the MBR code and the partition table wouldn't be over written.\r
+    If we process DBR, we will patch MBR to set first partition active if no active partition exists.\r
+    \r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials                          \r
 are licensed and made available under the terms and conditions of the BSD License         \r
 which accompanies this distribution.  The full text of the license may be found at        \r
 http://opensource.org/licenses/bsd-license.php                                            \r
@@ -9,16 +13,6 @@ http://opensource.org/licenses/bsd-license.php
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
 \r
-Module Name:\r
-\r
-  genbootsector.c\r
-  \r
-Abstract:\r
-  Reading/writing MBR/DBR.\r
-  NOTE:\r
-    If we write MBR to disk, we just update the MBR code and the partition table wouldn't be over written.\r
-    If we process DBR, we will patch MBR to set first partition active if no active partition exists.\r
-\r
 **/\r
 \r
 #include <windows.h>\r
@@ -28,6 +22,7 @@ Abstract:
 \r
 #include "ParseInf.h"\r
 #include "EfiUtilityMsgs.h"\r
+#include "CommonLib.h"\r
 \r
 //\r
 // Utility Name\r
@@ -38,7 +33,7 @@ Abstract:
 // Utility version information\r
 //\r
 #define UTILITY_MAJOR_VERSION 0\r
-#define UTILITY_MINOR_VERSION 1\r
+#define UTILITY_MINOR_VERSION 2\r
 \r
 #define MAX_DRIVE                             26\r
 #define PARTITION_TABLE_OFFSET                0x1BE\r
@@ -172,7 +167,7 @@ Return:
   if (VolumeHandle == INVALID_HANDLE_VALUE) {\r
     fprintf (\r
       stderr, \r
-      "error E0005: CreateFile failed: Volume = %s, LastError = 0x%x\n", \r
+      "error E0005: CreateFile failed: Volume = %s, LastError = 0x%lx\n", \r
       VolumeAccessPath, \r
       GetLastError ()\r
       );\r
@@ -206,6 +201,7 @@ Return:
     //\r
     // Only care about the disk.\r
     //\r
+    CloseHandle(VolumeHandle);\r
     return FALSE;\r
   } else{\r
     DriveInfo->DiskNumber = StorageDeviceNumber.DeviceNumber;\r
@@ -283,7 +279,7 @@ GetBootSectorOffset (
 Description:\r
   Get the offset of boot sector.\r
   For non-MBR disk, offset is just 0\r
-  for disk with MBR, offset needs to be caculated by parsing MBR\r
+  for disk with MBR, offset needs to be calculated by parsing MBR\r
 \r
   NOTE: if no one is active, we will patch MBR to select first partition as active.\r
 \r
@@ -442,8 +438,8 @@ ProcessBsOrMbr (
   BYTE              DiskPartitionBackup[0x200] = {0};\r
   DWORD             BytesReturn;\r
   INT               DrvNumOffset;\r
-  HANDLE            InputHandle;\r
-  HANDLE            OutputHandle;\r
+  HANDLE            InputHandle = INVALID_HANDLE_VALUE;\r
+  HANDLE            OutputHandle = INVALID_HANDLE_VALUE;\r
   ERROR_STATUS      Status;\r
   DWORD             InputDbrOffset;\r
   DWORD             OutputDbrOffset;\r
@@ -453,7 +449,7 @@ ProcessBsOrMbr (
   //\r
   Status =  GetFileHandle(InputInfo, ProcessMbr, &InputHandle, &InputDbrOffset);\r
   if (Status != ErrorSuccess) {\r
-    return Status;\r
+    goto Done;\r
   }\r
 \r
   //\r
@@ -461,14 +457,15 @@ ProcessBsOrMbr (
   //\r
   Status = GetFileHandle(OutputInfo, ProcessMbr, &OutputHandle, &OutputDbrOffset);\r
   if (Status != ErrorSuccess) {\r
-    return Status;\r
+    goto Done;\r
   }\r
 \r
   //\r
   // Read boot sector from source disk/file\r
   // \r
   if (!ReadFile (InputHandle, DiskPartition, 0x200, &BytesReturn, NULL)) {\r
-    return ErrorFileReadWrite;\r
+    Status = ErrorFileReadWrite;\r
+    goto Done;\r
   }\r
 \r
   if (InputInfo->Type == PathUsb) {\r
@@ -478,7 +475,8 @@ ProcessBsOrMbr (
       //\r
       DrvNumOffset = GetDrvNumOffset (DiskPartition);\r
       if (DrvNumOffset == -1) {\r
-        return ErrorFatType;\r
+        Status = ErrorFatType;\r
+        goto Done;\r
       }\r
       //\r
       // Some legacy BIOS require 0x80 discarding MBR.\r
@@ -500,7 +498,8 @@ ProcessBsOrMbr (
       // Use original partition table\r
       //\r
       if (!ReadFile (OutputHandle, DiskPartitionBackup, 0x200, &BytesReturn, NULL)) {\r
-        return ErrorFileReadWrite;\r
+        Status = ErrorFileReadWrite;\r
+        goto Done;\r
       }\r
       memcpy (DiskPartition + 0x1BE, DiskPartitionBackup + 0x1BE, 0x40);\r
       SetFilePointer (OutputHandle, 0, NULL, FILE_BEGIN);\r
@@ -512,13 +511,19 @@ ProcessBsOrMbr (
   // Write boot sector to taget disk/file\r
   // \r
   if (!WriteFile (OutputHandle, DiskPartition, 0x200, &BytesReturn, NULL)) {\r
-    return ErrorFileReadWrite;\r
+    Status = ErrorFileReadWrite;\r
+    goto Done;\r
   }\r
 \r
-  CloseHandle (InputHandle);\r
-  CloseHandle (OutputHandle);\r
+Done:\r
+  if (InputHandle != INVALID_HANDLE_VALUE) {\r
+    CloseHandle (InputHandle);\r
+  }\r
+  if (OutputHandle != INVALID_HANDLE_VALUE) {\r
+    CloseHandle (OutputHandle);\r
+  }\r
 \r
-  return ErrorSuccess;\r
+  return Status;\r
 }\r
 \r
 void\r
@@ -541,8 +546,7 @@ Returns:
 \r
 --*/\r
 {\r
-  printf ("%s v%d.%d -Utility to retrieve and update the boot sector or MBR.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);\r
-  printf ("Copyright (c) 2009 Intel Corporation. All rights reserved.\n");\r
+  printf ("%s Version %d.%d %s\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);\r
 }\r
 \r
 VOID\r
@@ -550,18 +554,27 @@ PrintUsage (
   void\r
   )\r
 {\r
-  Version();\r
-  printf ("\nUsage: \n\\r
-   GenBootSector\n\\r
-     [-l, --list list disks]\n\\r
-     [-i, --input Filename]\n\\r
-     [-o, --output Filename]\n\\r
-     [-m, --mbr process the MBR also]\n\\r
-     [-v, --verbose]\n\\r
-     [--version]\n\\r
-     [-q, --quiet disable all messages except fatal errors]\n\\r
-     [-d, --debug[#]\n\\r
-     [-h, --help]\n");\r
+  printf ("Usage: GenBootSector [options] --cfg-file CFG_FILE\n\n\\r
+Copyright (c) 2009 - 2014, Intel Corporation.  All rights reserved.\n\n\\r
+  Utility to retrieve and update the boot sector or MBR.\n\n\\r
+optional arguments:\n\\r
+  -h, --help            Show this help message and exit\n\\r
+  --version             Show program's version number and exit\n\\r
+  -d [DEBUG], --debug [DEBUG]\n\\r
+                        Output DEBUG statements, where DEBUG_LEVEL is 0 (min)\n\\r
+                        - 9 (max)\n\\r
+  -v, --verbose         Print informational statements\n\\r
+  -q, --quiet           Returns the exit code, error messages will be\n\\r
+                        displayed\n\\r
+  -s, --silent          Returns only the exit code; informational and error\n\\r
+                        messages are not displayed\n\\r
+  -l, --list            List disk drives\n\\r
+  -i INPUT_FILENAME, --input INPUT_FILENAME\n\\r
+                        Input file name\n\\r
+  -o OUTPUT_FILENAME, --output OUTPUT_FILENAME\n\\r
+                        Output file name\n\\r
+  -m, --mbr             Also process the MBR\n\\r
+  --sfo                 Reserved for future use\n");\r
 \r
 }\r
 \r
@@ -596,7 +609,7 @@ GetPathInfo (
     }\r
 \r
     if (!GetDriveInfo(VolumeLetter, &DriveInfo)) {\r
-      fprintf (stderr, "ERROR: GetDriveInfo - 0x%x\n", GetLastError ());\r
+      fprintf (stderr, "ERROR: GetDriveInfo - 0x%lx\n", GetLastError ());\r
       return ErrorPath;\r
     }\r
 \r
@@ -618,19 +631,33 @@ GetPathInfo (
        return ErrorSuccess;\r
   } \r
 \r
+  //\r
+  // Check the path length\r
+  //\r
+  if (strlen (PathInfo->Path) >= (sizeof (PathInfo->PhysicalPath) / sizeof (PathInfo->PhysicalPath[0]))) {\r
+    fprintf (stderr, "ERROR, Path is too long for - %s", PathInfo->Path);\r
+    return ErrorPath;\r
+  }\r
+\r
   PathInfo->Type = PathFile;\r
   if (PathInfo->Input) {\r
     //\r
     // If path is file path, check whether file is valid.\r
     //\r
-    f = fopen (PathInfo->Path, "r");\r
+    f = fopen (LongFilePath (PathInfo->Path), "r");\r
     if (f == NULL) {\r
       fprintf (stderr, "error E2003: File was not provided!\n");\r
       return ErrorPath;\r
-    }  \r
+    }\r
+    fclose (f);\r
   }\r
   PathInfo->Type = PathFile;\r
-  strcpy(PathInfo->PhysicalPath, PathInfo->Path);\r
+  strncpy(\r
+    PathInfo->PhysicalPath,\r
+    PathInfo->Path,\r
+    sizeof (PathInfo->PhysicalPath) / sizeof (PathInfo->PhysicalPath[0]) - 1\r
+    );\r
+  PathInfo->PhysicalPath[sizeof (PathInfo->PhysicalPath) / sizeof (PathInfo->PhysicalPath[0]) - 1] = 0;\r
 \r
   return ErrorSuccess;\r
 }    \r
@@ -784,7 +811,7 @@ main (
   } else {\r
     fprintf (\r
       stderr, \r
-      "%s: %s %s: failed - %s (LastError: 0x%x)!\n",\r
+      "%s: %s %s: failed - %s (LastError: 0x%lx)!\n",\r
       (Status == ErrorNoMbr) ? "WARNING" : "ERROR",\r
       (OutputPathInfo.Type != PathFile) ? "Write" : "Read", \r
       ProcessMbr ? "MBR" : "DBR", \r