#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Protocol/BlockIo.h>
EFI_STATUS EFIAPI UefiMain(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE* SystemTable)
{
EFI_STATUS Status;
EFI_HANDLE Handle = ImageHandle;
UINTN BlockSize = 0;
UINT8* BlockBuffer = NULL;
UINT8 NewSerialNumber[] = "MY_NEWSERIALNUMBER";
UINTN NewSerialNumberSize = sizeof(NewSerialNumber);
// Get the handle for the first Block I/O protocol
EFI_HANDLE* HandleBuffer = NULL;
UINTN NumHandles = 0;
Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiBlockIoProtocolGuid, NULL, &NumHandles, &HandleBuffer);
if (EFI_ERROR(Status)) {
Print(L"Failed to locate Block I/O protocol handles\n");
return Status;
}
// Open the first Block I/O protocol
EFI_BLOCK_IO_PROTOCOL* BlockIo = NULL;
Status = gBS->OpenProtocol(HandleBuffer[0], &gEfiBlockIoProtocolGuid, (VOID**)&BlockIo, Handle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (EFI_ERROR(Status)) {
Print(L"Failed to open Block I/O protocol\n");
return Status;
}
// Get the block size and allocate a buffer for the block data
BlockSize = BlockIo->Media->BlockSize;
BlockBuffer = AllocatePool(BlockSize);
if (BlockBuffer == NULL) {
Print(L"Failed to allocate block buffer\n");
return EFI_OUT_OF_RESOURCES;
}
// Read the block data into the buffer
Status = BlockIo->ReadBlocks(BlockIo, BlockIo->Media->MediaId, 0, BlockSize, BlockBuffer);
if (EFI_ERROR(Status)) {
Print(L"Failed to read block data\n");
return Status;
}
// Overwrite the serial number in the block data buffer
CopyMem(BlockBuffer + 20, NewSerialNumber, NewSerialNumberSize);
// Write the modified block data back to the disk
Status = BlockIo->WriteBlocks(BlockIo, BlockIo->Media->MediaId, 0, BlockSize, BlockBuffer);
if (EFI_ERROR(Status)) {
Print(L"Failed to write block data\n");
return Status;
}
// Free resources
FreePool(BlockBuffer);
FreePool(HandleBuffer);
return EFI_SUCCESS;
}