Skip to content

Commit

Permalink
Application: Adds ListPartitions
Browse files Browse the repository at this point in the history
  • Loading branch information
savvamitrofanov committed Mar 13, 2023
1 parent cf392cf commit eabb466
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Application/ListPartitions/ListPartitions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/** @file
List available partitions on the system.
Copyright (c) 2021-2023, Mikhail Krichanov. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/

#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/OcMemoryLib.h>
#include <Library/OcFileLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Protocol/BlockIo.h>

EFI_STATUS
EFIAPI
UefiMain (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
UINTN Index;
UINTN HandleCount;
EFI_HANDLE *Handles;
EFI_BLOCK_IO_PROTOCOL *BlkIo;
CONST EFI_PARTITION_ENTRY *PartitionEntry;

//
// Find HardDrive
//
Status = gBS->LocateHandleBuffer (
ByProtocol,
&gEfiBlockIoProtocolGuid,
NULL,
&HandleCount,
&Handles
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "Failed to find any partitions - %r\n", Status));
return Status;
}

for (Index = 0; Index < HandleCount; ++Index) {
Status = gBS->HandleProtocol (
Handles[Index],
&gEfiBlockIoProtocolGuid,
(VOID **)&BlkIo
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "Partition #%u is unavailable - %r\n", (UINT32)(Index + 1), Status));
continue;
}

if (!BlkIo->Media->LogicalPartition) {
DEBUG ((DEBUG_WARN, "Partition #%u is not a logical partition\n", (UINT32)(Index + 1)));
continue;
}

PartitionEntry = OcGetGptPartitionEntry (Handles[Index]);
if (PartitionEntry == NULL) {
DEBUG ((DEBUG_WARN, "Partition #%u GPT entry is not accessible\n", (UINT32)(Index + 1)));
continue;
}

if (IsZeroGuid (&PartitionEntry->UniquePartitionGUID)) {
DEBUG ((DEBUG_WARN, "Partition #%u GPT entry has all-zero GUID\n", (UINT32)(Index + 1)));
continue;
}

DEBUG ((DEBUG_WARN, "Partition #%u details:\n", (UINT32)(Index + 1)));
DEBUG ((DEBUG_WARN, " PartitionName : %-36s\n", PartitionEntry->PartitionName));
DEBUG ((DEBUG_WARN, " PartitionTypeGUID : %g\n", &PartitionEntry->PartitionTypeGUID));
DEBUG ((DEBUG_WARN, " UniquePartitionGUID: %g\n", &PartitionEntry->UniquePartitionGUID));
}

FreePool (Handles);

return EFI_SUCCESS;
}
53 changes: 53 additions & 0 deletions Application/ListPartitions/ListPartitions.inf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## @file
# List available partitions on the system.
#
# Copyright (c) 2021-2023, Vitaly Cheptsov. All rights reserved.<BR>
#
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#
##

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = ListPartitions
FILE_GUID = 58856DD3-44DF-481A-BBD2-7ED13741EDF8
MODULE_TYPE = UEFI_APPLICATION
VERSION_STRING = 1.0
ENTRY_POINT = UefiMain

#
# This flag specifies whether HII resource section is generated into PE image.
#
UEFI_HII_RESOURCE_SECTION = TRUE

#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64 IPF EBC
#

[Sources]
ListPartitions.c

[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
OpenCorePkg/OpenCorePkg.dec
UefiCpuPkg/UefiCpuPkg.dec

[Protocols]
gEfiBlockIoProtocolGuid ## CONSUMES

[LibraryClasses]
OcConsoleControlEntryModeGenericLib
OcFileLib
DebugLib
UefiApplicationEntryPoint
UefiLib
PcdLib
IoLib
1 change: 1 addition & 0 deletions OpenCorePkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
OpenCorePkg/Application/GopPerf/GopPerf.inf
OpenCorePkg/Application/GopStop/GopStop.inf
OpenCorePkg/Application/KeyTester/KeyTester.inf
OpenCorePkg/Application/ListPartitions/ListPartitions.inf
OpenCorePkg/Application/MmapDump/MmapDump.inf
OpenCorePkg/Application/OpenControl/OpenControl.inf
OpenCorePkg/Application/OpenCore/OpenCore.inf {
Expand Down
1 change: 1 addition & 0 deletions build_oc.tool
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ package() {
"CsrUtil.efi"
"GopStop.efi"
"KeyTester.efi"
"ListPartitions.efi"
"MmapDump.efi"
"ResetSystem.efi"
"RtcRw.efi"
Expand Down

0 comments on commit eabb466

Please sign in to comment.