Skip to content

Commit

Permalink
Add reset to MaskROM boot option
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Bălănică <[email protected]>
  • Loading branch information
mariobalanica committed Dec 7, 2024
1 parent d6e003b commit 61fdb61
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/** @file
*
* Copyright (c) 2024, Mario Bălănică <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause-Patent
*
**/

#include <Uefi.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Library/UefiLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>

STATIC
VOID
EFIAPI
RuntimeResetPlatformSpecific (
IN CONST GUID *ResetSubtype,
IN CONST CHAR16 *ResetString
)
{
UINTN StringSize;
UINTN ResetDataSize = 0;
VOID *ResetData = NULL;

if (ResetSubtype == NULL) {
ASSERT (FALSE);
goto Exit;
}

if (ResetString == NULL) {
ResetString = L"";
}

StringSize = StrSize (ResetString);
ResetDataSize = StringSize + sizeof (GUID);

ResetData = AllocateZeroPool (ResetDataSize);
if (ResetData == NULL) {
ResetDataSize = 0;
goto Exit;
}

CopyMem (ResetData, ResetString, StringSize);
CopyGuid ((GUID *)((UINT8 *)ResetData + StringSize), ResetSubtype);

Exit:
gRT->ResetSystem (EfiResetPlatformSpecific, EFI_SUCCESS, ResetDataSize, ResetData);

if (ResetData != NULL) {
FreePool (ResetData);
}
}

EFI_STATUS
EFIAPI
UefiMain (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
RuntimeResetPlatformSpecific (&gRockchipResetTypeMaskromGuid, L"MASKROM");

return EFI_SUCCESS;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#/** @file
#
# Copyright (c) 2024, Mario Bălănică <[email protected]>
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
#**/

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = MaskromReset
FILE_GUID = 1f64e768-9f2c-4b39-a54a-f84a31ed6d6b
MODULE_TYPE = UEFI_APPLICATION
VERSION_STRING = 1.0
ENTRY_POINT = UefiMain

[Sources]
MaskromReset.c

[Packages]
MdePkg/MdePkg.dec
Silicon/Rockchip/RockchipPkg.dec

[LibraryClasses]
BaseMemoryLib
DebugLib
MemoryAllocationLib
UefiApplicationEntryPoint
UefiLib
UefiRuntimeServicesTableLib

[Guids]
gRockchipResetTypeMaskromGuid
3 changes: 3 additions & 0 deletions edk2-rockchip/Silicon/Rockchip/FvMainModules.fdf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,6 @@

# SPI Flash Cmd application
# INF Silicon/Rockchip/Applications/SpiTool/SpiFlashCmd.inf

# Maskrom Reset application
INF Silicon/Rockchip/Applications/MaskromReset/MaskromReset.inf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @file
Implementation for PlatformBootManagerLib library class interfaces.
Copyright (c) 2023-2024, Mario Bălănică <[email protected]>
Copyright (C) 2015-2016, Red Hat, Inc.
Copyright (c) 2014 - 2021, ARM Ltd. All rights reserved.<BR>
Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
Expand Down Expand Up @@ -39,6 +40,8 @@

#include "PlatformBm.h"

#define BOOT_PROMPT L"Setup (ESC/F2) Shell (F1) Reset to MaskROM (F4) Continue (Enter)"

#define DP_NODE_LEN(Type) { (UINT8)sizeof (Type), (UINT8)(sizeof (Type) >> 8) }

#pragma pack (1)
Expand Down Expand Up @@ -789,6 +792,8 @@ PlatformRegisterOptionsAndKeys (
EFI_INPUT_KEY Enter;
EFI_INPUT_KEY F2;
EFI_INPUT_KEY Esc;
EFI_INPUT_KEY F1;
EFI_INPUT_KEY F4;
EFI_BOOT_MANAGER_LOAD_OPTION BootOption;

GetPlatformOptions ();
Expand Down Expand Up @@ -826,6 +831,22 @@ PlatformRegisterOptionsAndKeys (
NULL
);
ASSERT (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED);

//
// Register UEFI Shell
//
F1.ScanCode = SCAN_F1;
F1.UnicodeChar = CHAR_NULL;
PlatformRegisterFvBootOption (&gUefiShellFileGuid, L"UEFI Shell", 0, &F1);

//
// Register Maskrom Reset
//
F4.ScanCode = SCAN_F4;
F4.UnicodeChar = CHAR_NULL;
PlatformRegisterFvBootOption (&gRockchipMaskromResetFileGuid, L"Reset to MaskROM", 0, &F4);

RemoveStaleFvFileOptions ();
}

//
Expand Down Expand Up @@ -1158,7 +1179,6 @@ PlatformBootManagerAfterConsole (
UINTN FirmwareVerLength;
UINTN PosX;
UINTN PosY;
EFI_INPUT_KEY Key;

EfiEventGroupSignal (&gRockchipEventPlatformBmAfterConsoleGuid);

Expand All @@ -1175,7 +1195,7 @@ PlatformBootManagerAfterConsole (
PcdGetPtr (PcdFirmwareVersionString)
);
}
Print (L"Press ESCAPE for boot options");
Print (BOOT_PROMPT);
} else if (FirmwareVerLength > 0) {
Status = gBS->HandleProtocol (
gST->ConsoleOutHandle,
Expand Down Expand Up @@ -1213,15 +1233,6 @@ PlatformBootManagerAfterConsole (
// feedback about what is going on.
//
HandleCapsules ();

//
// Register UEFI Shell
//
Key.ScanCode = SCAN_NULL;
Key.UnicodeChar = L's';
PlatformRegisterFvBootOption (&gUefiShellFileGuid, L"UEFI Shell", 0, &Key);

RemoveStaleFvFileOptions ();
}

/**
Expand Down Expand Up @@ -1249,7 +1260,7 @@ PlatformBootManagerWaitCallback (
Status = BootLogoUpdateProgress (
White.Pixel,
Black.Pixel,
L"Press ESCAPE for boot options",
BOOT_PROMPT,
White.Pixel,
(Timeout - TimeoutRemain) * 100 / Timeout,
0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
gEfiTtyTermGuid
gUefiShellFileGuid
gRockchipEventPlatformBmAfterConsoleGuid
gRockchipMaskromResetFileGuid

[Protocols]
gEdkiiNonDiscoverableDeviceProtocolGuid
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/** @file
*
* Copyright (c) 2024, Mario Bălănică <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause-Patent
*
**/

#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/IoLib.h>
#include <Library/ResetUtilityLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Protocol/PlatformSpecificResetHandler.h>

#define ROCKCHIP_BOOT_MODE_REG 0xFD588080 // PMU0_GRF_OS_REG8

#define BOOT_BROM_DOWNLOAD 0xEF08A53C

#define PMU1GRF_SOC_CON1 0xfd58a004

STATIC
VOID
EFIAPI
ResetNotifyHandler (
IN EFI_RESET_TYPE ResetType,
IN EFI_STATUS ResetStatus,
IN UINTN DataSize,
IN VOID *ResetData OPTIONAL
)
{
GUID *ResetSubtype;
BOOLEAN SoftReset;

if ((ResetType != EfiResetPlatformSpecific) ||
(DataSize == 0) ||
(ResetData == NULL))
{
return;
}

ResetSubtype = GetResetPlatformSpecificGuid (DataSize, ResetData);

SoftReset = TRUE;

if (CompareGuid (ResetSubtype, &gRockchipResetTypeMaskromGuid)) {
MmioWrite32 (ROCKCHIP_BOOT_MODE_REG, BOOT_BROM_DOWNLOAD);
} else {
SoftReset = FALSE;
}

//
// It has been observed that the boot mode register does not persist
// across reset after BL31 sets reset_width to 0xFFFF.
// Setting it back to 0 causes a softer reset, which solves the issue.
//
if (SoftReset) {
MmioWrite32 (PMU1GRF_SOC_CON1, 0xFFFF0000);
}
}

EFI_STATUS
EFIAPI
ResetPlatformDxeInitialize (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;

EDKII_PLATFORM_SPECIFIC_RESET_HANDLER_PROTOCOL *ResetNotify;

Status = gBS->LocateProtocol (
&gEdkiiPlatformSpecificResetHandlerProtocolGuid,
NULL,
(VOID **)&ResetNotify
);
ASSERT_EFI_ERROR (Status);
if (!EFI_ERROR (Status)) {
Status = ResetNotify->RegisterResetNotify (
ResetNotify,
ResetNotifyHandler
);
ASSERT_EFI_ERROR (Status);
}

return Status;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#/** @file
#
# Copyright (c) 2024, Mario Bălănică <[email protected]>
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
#**/

[Defines]
INF_VERSION = 0x0001001A
BASE_NAME = ResetPlatformDxe
FILE_GUID = 8c01f5b0-1da5-471b-97fd-f60f77e09de6
MODULE_TYPE = DXE_DRIVER
VERSION_STRING = 1.0
ENTRY_POINT = ResetPlatformDxeInitialize

[Sources]
ResetPlatformDxe.c

[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
Silicon/Rockchip/RockchipPkg.dec

[LibraryClasses]
BaseMemoryLib
DebugLib
IoLib
ResetUtilityLib
UefiBootServicesTableLib
UefiDriverEntryPoint

[Guids]
gRockchipResetTypeMaskromGuid

[Protocols]
gEdkiiPlatformSpecificResetHandlerProtocolGuid

[Pcd]

[Depex]
gEdkiiPlatformSpecificResetHandlerProtocolGuid
7 changes: 6 additions & 1 deletion edk2-rockchip/Silicon/Rockchip/RK3588/RK3588.fdf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Copyright (c) 2014-2018, Linaro Limited. All rights reserved.
# Copyright (c) 2021-2022, Rockchip Limited. All rights reserved.
# Copyright (c) 2023, Mario Bălănică <[email protected]>
# Copyright (c) 2023-2024, Mario Bălănică <[email protected]>
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
Expand Down Expand Up @@ -110,6 +110,11 @@ READ_LOCK_STATUS = TRUE
#
INF Silicon/Rockchip/RK3588/Drivers/RK3588Dxe/RK3588Dxe.inf

#
# Custom reset handler
#
INF Silicon/Rockchip/RK3588/Drivers/ResetPlatformDxe/ResetPlatformDxe.inf

#
# PCI Support
#
Expand Down
5 changes: 5 additions & 0 deletions edk2-rockchip/Silicon/Rockchip/RK3588/RK3588Base.dsc.inc
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@
# General platform manager
Silicon/Rockchip/RK3588/Drivers/RK3588Dxe/RK3588Dxe.inf

#
# Custom reset handler
#
Silicon/Rockchip/RK3588/Drivers/ResetPlatformDxe/ResetPlatformDxe.inf

#
# PCI Support
#
Expand Down
5 changes: 5 additions & 0 deletions edk2-rockchip/Silicon/Rockchip/Rockchip.dsc.inc
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@

ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.inf

ResetUtilityLib|MdeModulePkg/Library/ResetUtilityLib/ResetUtilityLib.inf

# RkSdmmcDxe dependencies
RkSdmmcPlatformLib|Silicon/Rockchip/Library/RkSdmmcPlatformLibNull/RkSdmmcPlatformLibNull.inf

Expand Down Expand Up @@ -793,3 +795,6 @@ FspiLib|Silicon/Rockchip/Library/FspiLib/FspiLib.inf

# SPI Flash Cmd application
# Silicon/Rockchip/Applications/SpiTool/SpiFlashCmd.inf

# Maskrom Reset application
Silicon/Rockchip/Applications/MaskromReset/MaskromReset.inf
2 changes: 2 additions & 0 deletions edk2-rockchip/Silicon/Rockchip/RockchipPkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
gShellSfHiiGuid = { 0x03a67756, 0x8cde, 0x4638, { 0x82, 0x34, 0x4a, 0x0f, 0x6d, 0x58, 0x81, 0x39 } }
gShellI2cDemoHiiGuid = { 0xb2f4c714, 0x147f, 0x4ff7, { 0x82, 0x1b, 0xce, 0x7b, 0x91, 0x7f, 0x5f, 0x2f } }
gRockchipEventPlatformBmAfterConsoleGuid = { 0xf1272c11, 0xb418, 0x40ca, { 0x88, 0x26, 0x11, 0x94, 0xd7, 0xb7, 0x30, 0x4a } }
gRockchipResetTypeMaskromGuid = { 0x44a5917b, 0x1f57, 0x467d, { 0x96, 0xe5, 0xb2, 0xc2, 0x22, 0x1f, 0xa7, 0x21 } }
gRockchipMaskromResetFileGuid = { 0x1f64e768, 0x9f2c, 0x4b39, { 0xa5, 0x4a, 0xf8, 0x4a, 0x31, 0xed, 0x6d, 0x6b } }

[PcdsFixedAtBuild]
gRockchipTokenSpaceGuid.PcdProcessorName|"Unknown"|VOID*|0x00000001
Expand Down

0 comments on commit 61fdb61

Please sign in to comment.