]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Nt32Pkg/WinNtSimpleFileSystemDxe/WinNtSimpleFileSystem.c
Fix ICC compatibility issues
[mirror_edk2.git] / Nt32Pkg / WinNtSimpleFileSystemDxe / WinNtSimpleFileSystem.c
index 62cb47960dd80ad31c717a32daba422963271cf0..1abb84185e54e252c63b2bfa00d79eeb8ab3bc77 100644 (file)
@@ -1,6 +1,6 @@
-/*++\r
+/**@file\r
 \r
-Copyright (c) 2006 - 2007, Intel Corporation\r
+Copyright (c) 2006 - 2008, Intel Corporation\r
 All rights reserved. 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
@@ -22,7 +22,7 @@ Abstract:
 \r
   * Other names and brands may be claimed as the property of others.\r
 \r
---*/\r
+**/\r
 \r
 //\r
 // The package level header files this module uses\r
@@ -83,14 +83,13 @@ InitializeWinNtSimpleFileSystem(
   //\r
   // Install driver model protocol(s).\r
   //\r
-  Status = EfiLibInstallAllDriverProtocols (\r
+  Status = EfiLibInstallDriverBindingComponentName2 (\r
              ImageHandle,\r
              SystemTable,\r
              &gWinNtSimpleFileSystemDriverBinding,\r
              ImageHandle,\r
              &gWinNtSimpleFileSystemComponentName,\r
-             NULL,\r
-             NULL\r
+             &gWinNtSimpleFileSystemComponentName2\r
              );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
@@ -256,7 +255,7 @@ Returns:
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
                   &gEfiWinNtIoProtocolGuid,\r
-                  &WinNtIo,\r
+                  (VOID **) &WinNtIo,\r
                   This->DriverBindingHandle,\r
                   ControllerHandle,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
@@ -329,7 +328,7 @@ Returns:
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
                   &gEfiWinNtIoProtocolGuid,\r
-                  &WinNtIo,\r
+                  (VOID **) &WinNtIo,\r
                   This->DriverBindingHandle,\r
                   ControllerHandle,\r
                   EFI_OPEN_PROTOCOL_BY_DRIVER\r
@@ -373,12 +372,21 @@ Returns:
 \r
   Private->ControllerNameTable = NULL;\r
 \r
-  AddUnicodeString (\r
+  AddUnicodeString2 (\r
     "eng",\r
     gWinNtSimpleFileSystemComponentName.SupportedLanguages,\r
     &Private->ControllerNameTable,\r
-    WinNtIo->EnvString\r
+    WinNtIo->EnvString,\r
+    TRUE\r
     );\r
+  AddUnicodeString2 (\r
+    "en",\r
+    gWinNtSimpleFileSystemComponentName2.SupportedLanguages,\r
+    &Private->ControllerNameTable,\r
+    WinNtIo->EnvString,\r
+    FALSE\r
+    );\r
+\r
 \r
   Status = gBS->InstallMultipleProtocolInterfaces (\r
                   &ControllerHandle,\r
@@ -451,7 +459,7 @@ Returns:
   Status = gBS->OpenProtocol (\r
                   ControllerHandle,\r
                   &gEfiSimpleFileSystemProtocolGuid,\r
-                  &SimpleFileSystem,\r
+                  (VOID **) &SimpleFileSystem,\r
                   This->DriverBindingHandle,\r
                   ControllerHandle,\r
                   EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
@@ -609,6 +617,156 @@ Done:
   return Status;\r
 }\r
 \r
+/**\r
+  Count the number of Leading Dot in FileNameToken.\r
+\r
+  @param FileNameToken  A string representing a token in the path name.\r
+\r
+  @return  UINTN             The number of leading dot in the name.\r
+\r
+**/\r
+UINTN\r
+CountLeadingDots (\r
+  IN CONST CHAR16 * FileNameToken\r
+  )\r
+{\r
+  UINTN          Num;\r
+\r
+  Num = 0;\r
+  while (*FileNameToken == L'.') {\r
+    Num++;\r
+    FileNameToken++;\r
+  }\r
+  \r
+  return Num;\r
+}\r
+\r
+BOOLEAN \r
+IsFileNameTokenValid (\r
+  IN CONST CHAR16 * FileNameToken\r
+  )\r
+{\r
+  UINTN Num;\r
+  if (StrStr (FileNameToken, L"/") != NULL) {\r
+    //\r
+    // No L'/' in file name.\r
+    //\r
+    return FALSE;\r
+  } else {\r
+    //\r
+    // If Token has all dot, the number should not exceed 2\r
+    //\r
+    Num = CountLeadingDots (FileNameToken);\r
+\r
+    if (Num == StrLen (FileNameToken)) {\r
+      //\r
+      // If the FileNameToken only contains a number of L'.'.\r
+      //\r
+      if (Num > 2) {\r
+        return FALSE;\r
+      }\r
+    }\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r
+/**\r
+  Return the first string token found in the indirect pointer a String named by FileName.\r
+\r
+  On input, FileName is a indirect pointer pointing to a String.\r
+  On output, FileName is a updated to point to the next character after the first\r
+  found L"\" or NULL if there is no L"\" found.\r
+\r
+  @param FileName  A indirect pointer pointing to a FileName.\r
+\r
+  @return  Token      The first string token found before a L"\".\r
+\r
+**/\r
+CHAR16 *\r
+GetNextFileNameToken (\r
+  IN OUT CONST CHAR16 ** FileName \r
+  )\r
+{\r
+  CHAR16 *SlashPos;\r
+  CHAR16 *Token;\r
+  UINTN  Offset;\r
+  ASSERT (**FileName != L'\\');\r
+  ASSERT (**FileName != L'\0');\r
+\r
+  SlashPos = StrStr (*FileName, L"\\");\r
+  if (SlashPos == NULL) {\r
+    Token = AllocateCopyPool (StrSize(*FileName), *FileName);\r
+    *FileName = NULL;\r
+  } else {\r
+    Offset = SlashPos - *FileName;\r
+    Token = AllocateZeroPool ((Offset + 1) * sizeof (CHAR16));\r
+    StrnCpy (Token, *FileName, Offset);\r
+    //\r
+    // Point *FileName to the next character after L'\'.\r
+    //\r
+    *FileName = *FileName + Offset + 1;\r
+  }\r
+\r
+  return Token;\r
+}\r
+\r
+/**\r
+  Check if a FileName contains only Valid Characters.\r
+\r
+  If FileName contains only a single L'\', return TRUE.\r
+  If FileName contains two adjacent L'\', return FALSE.\r
+  If FileName conatins L'/' , return FALSE.\r
+  If FielName contains more than two dots seperated with other FileName characters\r
+  by L'\', return FALSE. For example, L'.\...\filename.txt' is invalid path name. But L'..TwoDots\filename.txt' is valid path name.\r
+\r
+  @param FileName  The File Name String to check.\r
+\r
+  @return  TRUE        FileName only contains valid characters.\r
+  @return  FALSE       FileName contains at least one invalid character.\r
+\r
+**/\r
+\r
+BOOLEAN\r
+IsFileNameValid (\r
+  IN CONST CHAR16 *FileName \r
+  )\r
+{\r
+  CHAR16       *Token;\r
+  BOOLEAN      Valid;\r
+\r
+  //\r
+  // If FileName is just L'\', then it is a valid pathname. \r
+  //\r
+  if (StrCmp (FileName, L"\\") == 0) {\r
+    return TRUE;\r
+  }\r
+  //\r
+  // We don't support two or more adjacent L'\'.\r
+  //\r
+  if (StrStr (FileName, L"\\\\") != NULL) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Is FileName has a leading L"\", skip to next character.\r
+  //\r
+  if (FileName [0] == L'\\') {\r
+    FileName++;\r
+  }\r
+\r
+  do {\r
+    Token = GetNextFileNameToken (&FileName);\r
+    Valid = IsFileNameTokenValid (Token);\r
+    FreePool (Token);\r
+    \r
+    if (!Valid)\r
+      return FALSE;\r
+  } while (FileName != NULL);\r
+\r
+  return TRUE;\r
+}\r
+\r
 EFI_STATUS\r
 EFIAPI\r
 WinNtSimpleFileSystemOpen (\r
@@ -626,7 +784,7 @@ Routine Description:
 \r
 Arguments:\r
 \r
-  This        - A pointer to the source file location.\r
+  This        - A pointer to the seource file location.\r
 \r
   NewHandle   - Pointer to storage for the new file handle.\r
 \r
@@ -679,6 +837,7 @@ Returns:
   BOOLEAN                           LoopFinish;\r
   UINTN                             InfoSize;\r
   EFI_FILE_INFO                     *Info;\r
+  UINTN                             Size;\r
 \r
   //\r
   // Check for obvious invalid parameters.\r
@@ -744,10 +903,11 @@ OpenRoot:
   }\r
 \r
   //\r
-  // If file name does not equal to "." or "..",\r
+  // If file name does not equal to "." or ".." and not trailed with "\..",\r
   // then we trim the leading/trailing blanks and trailing dots\r
   //\r
-  if (StrCmp (FileName, L".") != 0 && StrCmp (FileName, L"..") != 0) {\r
+  if (StrCmp (FileName, L".") != 0 && StrCmp (FileName, L"..") != 0 && \r
+    ((StrLen (FileName) >= 3) ? (StrCmp (&FileName[StrLen (FileName) - 3], L"\\..") != 0) : TRUE)) {\r
     //\r
     // Trim leading blanks\r
     //\r
@@ -759,10 +919,10 @@ OpenRoot:
     }\r
     CutPrefix (FileName, Count);\r
     //\r
-    // Trim trailing dots and blanks\r
+    // Trim trailing blanks\r
     //\r
     for (TempFileName = FileName + StrLen (FileName) - 1;\r
-      TempFileName >= FileName && (*TempFileName == L' ' || *TempFileName == L'.');\r
+      TempFileName >= FileName && (*TempFileName == L' ');\r
       TempFileName--) {\r
       ;\r
     }\r
@@ -792,7 +952,10 @@ OpenRoot:
     StrCpy (NewPrivateFile->FilePath, PrivateFile->FilePath);\r
   }\r
 \r
-  NewPrivateFile->FileName = AllocatePool (StrSize (NewPrivateFile->FilePath) + StrSize (L"\\") + StrSize (FileName));\r
+  Size = StrSize (NewPrivateFile->FilePath);\r
+  Size += StrSize (L"\\");\r
+  Size += StrSize (FileName);\r
+  NewPrivateFile->FileName = AllocatePool (Size);\r
   if (NewPrivateFile->FileName == NULL) {\r
     Status = EFI_OUT_OF_RESOURCES;\r
     goto Done;\r
@@ -813,6 +976,11 @@ OpenRoot:
     }\r
   }\r
 \r
+  if (!IsFileNameValid (NewPrivateFile->FileName)) {\r
+    Status = EFI_NOT_FOUND;\r
+    goto Done;\r
+  }\r
+\r
   //\r
   // Get rid of . and .., except leading . or ..\r
   //\r
@@ -943,7 +1111,9 @@ OpenRoot:
   //\r
   if (NewPrivateFile->IsDirectoryPath) {\r
 \r
-    TempFileName = AllocatePool (StrSize (NewPrivateFile->FileName) + StrSize (L"\\*"));\r
+    Size  = StrSize (NewPrivateFile->FileName);\r
+    Size += StrSize (L"\\*");\r
+    TempFileName = AllocatePool (Size);\r
     if (TempFileName == NULL) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
       goto Done;\r
@@ -1092,7 +1262,7 @@ OpenRoot:
     WinNtSimpleFileSystemSetInfo (&NewPrivateFile->EfiFile, &gEfiFileInfoGuid, InfoSize, Info);\r
   }\r
 \r
-Done: ;\r
+Done:\r
   FreePool (FileName);\r
 \r
   if (EFI_ERROR (Status)) {\r
@@ -1243,7 +1413,6 @@ Returns:
   return Status;\r
 }\r
 \r
-STATIC\r
 VOID\r
 WinNtSystemTimeToEfiTime (\r
   IN SYSTEMTIME             *SystemTime,\r
@@ -1397,12 +1566,12 @@ Returns:
     }\r
 \r
     Status = PrivateFile->WinNtThunk->ReadFile (\r
-                                      PrivateFile->LHandle,\r
-                                      Buffer,\r
-                                      *BufferSize,\r
-                                      BufferSize,\r
-                                      NULL\r
-                                      ) ? EFI_SUCCESS : EFI_DEVICE_ERROR;\r
+                                        PrivateFile->LHandle,\r
+                                        Buffer,\r
+                                        *BufferSize,\r
+                                        (LPDWORD)BufferSize,\r
+                                        NULL\r
+                                        ) ? EFI_SUCCESS : EFI_DEVICE_ERROR;\r
     goto Done;\r
   }\r
 \r
@@ -1568,12 +1737,12 @@ Returns:
   }\r
 \r
   Status = PrivateFile->WinNtThunk->WriteFile (\r
-                                    PrivateFile->LHandle,\r
-                                    Buffer,\r
-                                    *BufferSize,\r
-                                    BufferSize,\r
-                                    NULL\r
-                                    ) ? EFI_SUCCESS : EFI_DEVICE_ERROR;\r
+                                      PrivateFile->LHandle,\r
+                                      Buffer,\r
+                                      *BufferSize,\r
+                                      (LPDWORD)BufferSize,\r
+                                      NULL\r
+                                      ) ? EFI_SUCCESS : EFI_DEVICE_ERROR;\r
 \r
 Done:\r
   gBS->RestoreTPL (OldTpl);\r
@@ -1617,6 +1786,7 @@ Returns:
   UINT32                  PosHigh;\r
   CHAR16                  *FileName;\r
   EFI_TPL                 OldTpl;\r
+  UINTN                   Size;\r
 \r
   if (This == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -1632,7 +1802,9 @@ Returns:
       goto Done;\r
     }\r
 \r
-    FileName = AllocatePool (StrSize (PrivateFile->FileName) + StrSize (L"\\*"));\r
+    Size  = StrSize (PrivateFile->FileName);\r
+    Size += StrSize (L"\\*");\r
+    FileName = AllocatePool (Size);\r
     if (FileName == NULL) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
       goto Done;\r
@@ -1662,7 +1834,7 @@ Returns:
     } else {\r
       PosHigh = (UINT32) RShiftU64 (Position, 32);\r
 \r
-      PosLow  = PrivateFile->WinNtThunk->SetFilePointer (PrivateFile->LHandle, (ULONG) Position, &PosHigh, FILE_BEGIN);\r
+      PosLow  = PrivateFile->WinNtThunk->SetFilePointer (PrivateFile->LHandle, (ULONG) Position, (PLONG)&PosHigh, FILE_BEGIN);\r
     }\r
 \r
     Status = (PosLow == 0xFFFFFFFF) ? EFI_DEVICE_ERROR : EFI_SUCCESS;\r
@@ -1725,11 +1897,11 @@ Returns:
 \r
     PositionHigh = 0;\r
     *Position = PrivateFile->WinNtThunk->SetFilePointer (\r
-                                          PrivateFile->LHandle,\r
-                                          0,\r
-                                          &PositionHigh,\r
-                                          FILE_CURRENT\r
-                                          );\r
+                                           PrivateFile->LHandle,\r
+                                           0,\r
+                                           (PLONG)&PositionHigh,\r
+                                           FILE_CURRENT\r
+                                           );\r
 \r
     Status = *Position == 0xffffffff ? EFI_DEVICE_ERROR : EFI_SUCCESS;\r
     if (EFI_ERROR (Status)) {\r
@@ -1745,7 +1917,6 @@ Done:
   return Status;\r
 }\r
 \r
-STATIC\r
 EFI_STATUS\r
 WinNtSimpleFileSystemFileInfo (\r
   IN     WIN_NT_EFI_FILE_PRIVATE  *PrivateFile,\r
@@ -2037,10 +2208,10 @@ Returns:
     //\r
     NtStatus = PrivateFile->WinNtThunk->GetDiskFreeSpace (\r
                                           DriveNameFound ? DriveName : NULL,\r
-                                          &SectorsPerCluster,\r
-                                          &BytesPerSector,\r
-                                          &FreeClusters,\r
-                                          &TotalClusters\r
+                                          (LPDWORD)&SectorsPerCluster,\r
+                                          (LPDWORD)&BytesPerSector,\r
+                                          (LPDWORD)&FreeClusters,\r
+                                          (LPDWORD)&TotalClusters\r
                                           );\r
     if (DriveName) {\r
       FreePool (DriveName);\r
@@ -2169,6 +2340,7 @@ Returns:
   WIN32_FIND_DATA                   FindBuf;\r
   EFI_FILE_SYSTEM_INFO              *NewFileSystemInfo;\r
   EFI_TPL                           OldTpl;\r
+  UINTN                             Size;\r
 \r
   //\r
   // Check for invalid parameters.\r
@@ -2297,25 +2469,20 @@ Returns:
   //\r
   // Make full pathname from new filename and rootpath.\r
   //\r
-  if (NewFileInfo->FileName[0] == '\\') {\r
-    NewFileName = AllocatePool (StrSize (PrivateRoot->FilePath) + StrSize (L"\\") + StrSize (NewFileInfo->FileName));\r
-    if (NewFileName == NULL) {\r
-      Status = EFI_OUT_OF_RESOURCES;\r
-      goto Done;\r
-    }\r
+  Size  = StrSize (PrivateRoot->FilePath);\r
+  Size += StrSize (L"\\");\r
+  Size += StrSize (NewFileInfo->FileName);\r
+  NewFileName = AllocatePool (Size);\r
+  if (NewFileName == NULL) {\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Done;\r
+  }\r
 \r
-    StrCpy (NewFileName, PrivateRoot->FilePath);\r
-    StrCat (NewFileName, L"\\");\r
+  StrCpy (NewFileName, PrivateRoot->FilePath);\r
+  StrCat (NewFileName, L"\\");\r
+  if (NewFileInfo->FileName[0] == '\\') {\r
     StrCat (NewFileName, NewFileInfo->FileName + 1);\r
   } else {\r
-    NewFileName = AllocatePool (StrSize (PrivateFile->FilePath) + StrSize (L"\\") + StrSize (NewFileInfo->FileName));\r
-    if (NewFileName == NULL) {\r
-      Status = EFI_OUT_OF_RESOURCES;\r
-      goto Done;\r
-    }\r
-\r
-    StrCpy (NewFileName, PrivateFile->FilePath);\r
-    StrCat (NewFileName, L"\\");\r
     StrCat (NewFileName, NewFileInfo->FileName);\r
   }\r
 \r
@@ -2426,7 +2593,9 @@ Returns:
 \r
       StrCpy (PrivateFile->FileName, NewFileName);\r
 \r
-      TempFileName = AllocatePool (StrSize (NewFileName) + StrSize (L"\\*"));\r
+      Size  =  StrSize (NewFileName);\r
+      Size += StrSize (L"\\*");\r
+      TempFileName = AllocatePool (Size);\r
 \r
       StrCpy (TempFileName, NewFileName);\r
 \r
@@ -2476,7 +2645,9 @@ Reopen: ;
         goto Done;\r
       }\r
 \r
-      TempFileName = AllocatePool (StrSize (OldFileName) + StrSize (L"\\*"));\r
+      Size =  StrSize (OldFileName);\r
+      Size += StrSize (L"\\*");\r
+      TempFileName = AllocatePool (Size);\r
 \r
       StrCpy (TempFileName, OldFileName);\r
 \r