MdeModulePkg/UefiBootManagerLib: Generate boot description for NVME
authorRuiyu Ni <ruiyu.ni@intel.com>
Fri, 10 Mar 2017 07:41:21 +0000 (15:41 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Mon, 13 Mar 2017 02:42:21 +0000 (10:42 +0800)
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Feng Tian <feng.tian@intel.com>
Reviewed-by: Sunny Wang <sunnywang@hpe.com>
MdeModulePkg/Library/UefiBootManagerLib/BmBootDescription.c
MdeModulePkg/Library/UefiBootManagerLib/InternalBm.h
MdeModulePkg/Library/UefiBootManagerLib/UefiBootManagerLib.inf

index 050647d..501a0cc 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Library functions which relate with boot option description.\r
 \r
-Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>\r
 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
@@ -500,6 +500,107 @@ BmGetLoadFileDescription (
   return NULL;\r
 }\r
 \r
+/**\r
+  Return the boot description for NVME boot device.\r
+\r
+  @param Handle                Controller handle.\r
+\r
+  @return  The description string.\r
+**/\r
+CHAR16 *\r
+BmGetNvmeDescription (\r
+  IN EFI_HANDLE                      Handle\r
+  )\r
+{\r
+  EFI_STATUS                               Status;\r
+  EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL       *NvmePassthru;\r
+  EFI_DEV_PATH_PTR                         DevicePath;\r
+  EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET CommandPacket;\r
+  EFI_NVM_EXPRESS_COMMAND                  Command;\r
+  EFI_NVM_EXPRESS_COMPLETION               Completion;\r
+  NVME_ADMIN_CONTROLLER_DATA               ControllerData;\r
+  CHAR16                                   *Description;\r
+  CHAR16                                   *Char;\r
+  UINTN                                    Index;\r
+\r
+  Status = gBS->HandleProtocol (Handle, &gEfiDevicePathProtocolGuid, (VOID **) &DevicePath.DevPath);\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+\r
+  Status = gBS->LocateDevicePath (&gEfiNvmExpressPassThruProtocolGuid, &DevicePath.DevPath, &Handle);\r
+  if (EFI_ERROR (Status) ||\r
+      (DevicePathType (DevicePath.DevPath) != MESSAGING_DEVICE_PATH) ||\r
+      (DevicePathSubType (DevicePath.DevPath) != MSG_NVME_NAMESPACE_DP)) {\r
+    //\r
+    // Do not return description when the Handle is not a child of NVME controller.\r
+    //\r
+    return NULL;\r
+  }\r
+\r
+  //\r
+  // Send ADMIN_IDENTIFY command to NVME controller to get the model and serial number.\r
+  //\r
+  Status = gBS->HandleProtocol (Handle, &gEfiNvmExpressPassThruProtocolGuid, (VOID **) &NvmePassthru);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  ZeroMem (&CommandPacket, sizeof(EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));\r
+  ZeroMem (&Command, sizeof(EFI_NVM_EXPRESS_COMMAND));\r
+  ZeroMem (&Completion, sizeof(EFI_NVM_EXPRESS_COMPLETION));\r
+\r
+  Command.Cdw0.Opcode = NVME_ADMIN_IDENTIFY_CMD;\r
+  //\r
+  // According to Nvm Express 1.1 spec Figure 38, When not used, the field shall be cleared to 0h.\r
+  // For the Identify command, the Namespace Identifier is only used for the Namespace data structure.\r
+  //\r
+  Command.Nsid        = 0;\r
+  CommandPacket.NvmeCmd        = &Command;\r
+  CommandPacket.NvmeCompletion = &Completion;\r
+  CommandPacket.TransferBuffer = &ControllerData;\r
+  CommandPacket.TransferLength = sizeof (ControllerData);\r
+  CommandPacket.CommandTimeout = EFI_TIMER_PERIOD_SECONDS (5);\r
+  CommandPacket.QueueType      = NVME_ADMIN_QUEUE;\r
+  //\r
+  // Set bit 0 (Cns bit) to 1 to identify a controller\r
+  //\r
+  Command.Cdw10                = 1;\r
+  Command.Flags                = CDW10_VALID;\r
+\r
+  Status = NvmePassthru->PassThru (\r
+                               NvmePassthru,\r
+                               0,\r
+                               &CommandPacket,\r
+                               NULL\r
+                               );\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+\r
+  Description = AllocateZeroPool (\r
+                  (ARRAY_SIZE (ControllerData.Mn) + 1\r
+                   + ARRAY_SIZE (ControllerData.Sn) + 1\r
+                   + MAXIMUM_VALUE_CHARACTERS + 1\r
+                   ) * sizeof (CHAR16));\r
+  if (Description != NULL) {\r
+    Char = Description;\r
+    for (Index = 0; Index < ARRAY_SIZE (ControllerData.Mn); Index++) {\r
+      *(Char++) = (CHAR16) ControllerData.Mn[Index];\r
+    }\r
+    *(Char++) = L' ';\r
+    for (Index = 0; Index < ARRAY_SIZE (ControllerData.Sn); Index++) {\r
+      *(Char++) = (CHAR16) ControllerData.Sn[Index];\r
+    }\r
+    *(Char++) = L' ';\r
+    UnicodeValueToStringS (\r
+      Char, sizeof (CHAR16) * (MAXIMUM_VALUE_CHARACTERS + 1),\r
+      0, DevicePath.NvmeNamespace->NamespaceId, 0\r
+      );\r
+    BmEliminateExtraSpaces (Description);\r
+  }\r
+\r
+  return Description;\r
+}\r
+\r
 /**\r
   Return the boot description for the controller based on the type.\r
 \r
@@ -606,6 +707,7 @@ BM_GET_BOOT_DESCRIPTION mBmBootDescriptionHandlers[] = {
   BmGetDescriptionFromDiskInfo,\r
   BmGetNetworkDescription,\r
   BmGetLoadFileDescription,\r
+  BmGetNvmeDescription,\r
   BmGetMiscDescription\r
 };\r
 \r
index 444d4a5..8d7215a 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   BDS library definition, include the file and data structure\r
 \r
-Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2017, Intel Corporation. All rights reserved.<BR>\r
 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
@@ -22,6 +22,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <IndustryStandard/PeImage.h>\r
 #include <IndustryStandard/Atapi.h>\r
 #include <IndustryStandard/Scsi.h>\r
+#include <IndustryStandard/Nvme.h>\r
 \r
 #include <Protocol/PciRootBridgeIo.h>\r
 #include <Protocol/BlockIo.h>\r
@@ -38,6 +39,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Protocol/GraphicsOutput.h>\r
 #include <Protocol/UsbIo.h>\r
 #include <Protocol/DiskInfo.h>\r
+#include <Protocol/NvmExpressPassthru.h>\r
 #include <Protocol/IdeControllerInit.h>\r
 #include <Protocol/BootLogo.h>\r
 #include <Protocol/DriverHealth.h>\r
index 6442f22..264d726 100644 (file)
   gEdkiiVariableLockProtocolGuid                ## SOMETIMES_CONSUMES\r
   gEfiGraphicsOutputProtocolGuid                ## SOMETIMES_CONSUMES\r
   gEfiUsbIoProtocolGuid                         ## SOMETIMES_CONSUMES\r
+  gEfiNvmExpressPassThruProtocolGuid            ## SOMETIMES_CONSUMES\r
   gEfiDiskInfoProtocolGuid                      ## SOMETIMES_CONSUMES\r
   gEfiDriverHealthProtocolGuid                  ## SOMETIMES_CONSUMES\r
   gEfiFormBrowser2ProtocolGuid                  ## SOMETIMES_CONSUMES\r