Untitled

 avatar
unknown
plain_text
a year ago
3.8 kB
12
Indexable
#include <efi.h>
#include <efilib.h>

#define SECTOR_SIZE 512
#define NUM_PARTITION_ENTRIES 128

// GPT header structure
typedef struct {
    uint64_t signature;
    uint32_t revision;
    uint32_t headerSize;
    uint32_t headerCRC32;
    uint32_t reserved;
    uint64_t currentLBA;
    uint64_t backupLBA;
    uint64_t firstUsableLBA;
    uint64_t lastUsableLBA;
    EFI_GUID diskGUID;
    uint64_t startingLBA;
    uint32_t partitionEntryCount;
    uint32_t partitionEntrySize;
    uint32_t partitionEntryArrayCRC32;
} GPTHeader;

// GPT partition entry structure
typedef struct {
    EFI_GUID partitionTypeGUID;
    EFI_GUID uniquePartitionGUID;
    uint64_t startingLBA;
    uint64_t endingLBA;
    uint64_t attributes;
    CHAR16 partitionName[36]; // Unicode string
} GPTPartitionEntry;

// GPT partition table structure
typedef struct {
    GPTHeader primaryHeader;
    GPTPartitionEntry partitionEntries[NUM_PARTITION_ENTRIES];
    GPTHeader backupHeader;
} GPTPartitionTable;

EFI_GUID gEfiPartTypeSystemPartGuid = { 0xEBD0A0A2, 0xB9E5, 0x4433, { 0x87, 0xC0, 0x68, 0xB6, 0xB7, 0x26, 0x99, 0xC7 } };

EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE* SystemTable) {
    InitializeLib(ImageHandle, SystemTable);

    Print(L"START\n");

    EFI_STATUS Status;
    UINTN HandleCount;
    EFI_HANDLE* HandleBuffer;
    Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &HandleCount, &HandleBuffer);
    if (EFI_ERROR(Status)) {
        Print(L"Failed to locate block protocol handles\n");
        return Status;
    }

    // Loop through each handle
    for (UINTN i = 0; i < HandleCount; i++) {
        EFI_BLOCK_IO_PROTOCOL* BlockIo;
        Status = gBS->HandleProtocol(HandleBuffer[i], &gEfiBlockIoProtocolGuid, (VOID**)&BlockIo);
        if (EFI_ERROR(Status)) {
            Print(L"Failed to get block IO protocol\n");
            continue;
        }

        // Read the GPT partition table
        GPTPartitionTable PartitionTable;
        UINTN NumSectors = sizeof(GPTPartitionTable) / SECTOR_SIZE;
        Status = BlockIo->ReadBlocks(BlockIo, 1, 0, NumSectors * SECTOR_SIZE, &PartitionTable);
        if (EFI_ERROR(Status)) {
            Print(L"Failed to read GPT partition table\n");
            continue;
        }

        // Loop through partition entries
        for (UINTN j = 0; j < NUM_PARTITION_ENTRIES; j++) {
            GPTPartitionEntry* PartitionEntry = &PartitionTable.partitionEntries[j];
            if (PartitionEntry->startingLBA == 0 && PartitionEntry->endingLBA == 0) {
                Print(L"EMPTY\n");
                continue;
            }

            // Check if it's a FAT32 partition
            if (CompareGuid(&PartitionEntry->partitionTypeGUID, &gEfiPartTypeSystemPartGuid)) {
                // Print first 200 bytes of the partition
                UINT64 PartitionSize = (PartitionEntry->endingLBA - PartitionEntry->startingLBA + 1) * SECTOR_SIZE;
                if (PartitionSize > 200)
                    PartitionSize = 200; // Limit to 200 bytes
                CHAR8 Buffer[200];
                Status = BlockIo->ReadBlocks(BlockIo, 1, PartitionEntry->startingLBA, PartitionSize, Buffer);
                if (!EFI_ERROR(Status)) {
                    Print(L"First 200 bytes of FAT32 partition:\n");
                    for (UINTN k = 0; k < PartitionSize; k++) {
                        if (k % 16 == 0)
                            Print(L"\n");
                        Print(L"%02x ", Buffer[k]);
                    }
                    Print(L"\n");
                }
                else {
                    Print(L"Failed to read FAT32 partition\n");
                }
            }
        }
    }

    // Free handle buffer
    FreePool(HandleBuffer);

    return EFI_SUCCESS;
}
Editor is loading...
Leave a Comment