From nobody Wed Dec 25 02:07:26 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1511177853032244.8697959977768; Mon, 20 Nov 2017 03:37:33 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 6BB2320347134; Mon, 20 Nov 2017 03:33:15 -0800 (PST) Received: from mail-wm0-x229.google.com (mail-wm0-x229.google.com [IPv6:2a00:1450:400c:c09::229]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 9D36020347134 for ; Mon, 20 Nov 2017 03:33:13 -0800 (PST) Received: by mail-wm0-x229.google.com with SMTP id v186so17956780wma.2 for ; Mon, 20 Nov 2017 03:37:27 -0800 (PST) Received: from localhost.localdomain ([154.145.25.106]) by smtp.gmail.com with ESMTPSA id d4sm14760181wmh.35.2017.11.20.03.37.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 20 Nov 2017 03:37:23 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:400c:c09::229; helo=mail-wm0-x229.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=n4Fk1PGKDteCjO04GLJ8ByaaSegow5Zh66ChqlmjwX4=; b=Fux628us6iNG28vyLsJRC7hQzc6p2Vo7s0bVlTCd5TIpls3nSRDz5HXe2LDhvYakDo eFSbLJR575kGH8YR3EH4ceIPUZUWxV4hLuWPLkQbtrdEE4QcC/iRWavTUXhURehTwATi v8Z2Nvm52D8SZuZ7nQxaMDyetsTVyDk+kkmz0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=n4Fk1PGKDteCjO04GLJ8ByaaSegow5Zh66ChqlmjwX4=; b=B2T3YSF2HkXra+O9gQEk3jWbL7JDQL0bqabzHa3HhyuyfzenkegRn/i0CjVgxVgUCT fr+wnezF2xEnPzzjKclbYJ1MTpReLEuqk2cW5LUgWH8LRCz4PNTTvpqvMgaci60Jy4r4 NFHxKp+p63okj9rxgUumWueviGDkyNLJrtYmjxTbjqfIY7CPk695U+TboEpS9bCCZM+n cLeH25xYR/13o5VEdNrJM4BdAPzfBfGTupVoguXpO8Xb+1PDRT4+caTM4uKn21cVHXXZ sk7FhmHzRo9QbAgBEMG5rzesyoZi8w8q5RpUL3uCSAMGKcl6ottEv6XqNeR8Z1M2Y/7x aW/A== X-Gm-Message-State: AJaThX5GOCclq6ADCg4VjrAzxtgj3NUr/hTG1yiO1KF0t8YEe+yC4BnB ZYxvi+1T5HxLHU4SvBqDz+HPrtGSIfQ= X-Google-Smtp-Source: AGs4zMYwBEh7+Euedea9hCG9ktPiB6rDhkBar53NA/xy6GQjtoJNJiMHgid5DV1GW+wFW7rFoV85jQ== X-Received: by 10.28.96.195 with SMTP id u186mr6885986wmb.121.1511177844357; Mon, 20 Nov 2017 03:37:24 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org Date: Mon, 20 Nov 2017 11:37:12 +0000 Message-Id: <20171120113714.21856-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171120113714.21856-1-ard.biesheuvel@linaro.org> References: <20171120113714.21856-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH edk2-platforms 2/4] Platform/ARM: import FdtPlatformDxe driver from EDK2 X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: leif.lindholm@linaro.org, Ard Biesheuvel MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Import FdtPlatformDxe from EmbeddedPkg into Platform/ARM, given that it is not used anywhere else, nor should it be. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ard Biesheuvel --- Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatform.c | 461 +++++++++++++= ++++++ Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatform.h | 174 ++++++++ Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.dec | 31 ++ Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.inf | 65 +++ Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.uni | 109 +++++ Platform/ARM/Drivers/FdtPlatformDxe/README.txt | 72 +++ Platform/ARM/Drivers/FdtPlatformDxe/ShellDumpFdt.c | 279 ++++++++++++ Platform/ARM/Drivers/FdtPlatformDxe/ShellSetFdt.c | 468 +++++++++++++= +++++++ 8 files changed, 1659 insertions(+) diff --git a/Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatform.c b/Platform/A= RM/Drivers/FdtPlatformDxe/FdtPlatform.c new file mode 100644 index 000000000000..b4be2a078991 --- /dev/null +++ b/Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatform.c @@ -0,0 +1,461 @@ +/** @file + + Copyright (c) 2015, ARM Ltd. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "FdtPlatform.h" + +#include +#include +#include + +#include + +// +// Internal variables +// + +STATIC CONST EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL mShellDynCmdProtocolSetFdt= =3D { + L"setfdt", // Name of the command + ShellDynCmdSetFdtHandler, // Handler + ShellDynCmdSetFdtGetHelp // GetHelp +}; + +STATIC CONST EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL mShellDynCmdProtocolDumpFd= t =3D { + L"dumpfdt", // Name of the command + ShellDynCmdDumpFdtHandler, // Handler + ShellDynCmdDumpFdtGetHelp // GetHelp +}; + +STATIC CONST EFI_GUID mFdtPlatformDxeHiiGuid =3D { + 0x8afa7610, 0x62b1, 0x46aa, + {0xb5, 0x34, 0xc3, 0xde, 0xff, 0x39, 0x77, 0x8c} + }; + +EFI_HANDLE mFdtPlatformDxeHiiHandle; + +/** + Install the FDT specified by its device path in text form. + + @param[in] TextDevicePath Device path of the FDT to install in text fo= rm + + @retval EFI_SUCCESS The FDT was installed. + @retval EFI_NOT_FOUND Failed to locate a protocol or a file. + @retval EFI_INVALID_PARAMETER Invalid device path. + @retval EFI_UNSUPPORTED Device path not supported. + @retval EFI_OUT_OF_RESOURCES An allocation failed. +**/ +STATIC +EFI_STATUS +InstallFdt ( + IN CONST CHAR16* TextDevicePath + ) +{ + EFI_STATUS Status; + EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *EfiDevicePathFromTextProtocol; + EFI_DEVICE_PATH *DevicePath; + EFI_PHYSICAL_ADDRESS FdtBlobBase; + UINTN FdtBlobSize; + UINTN NumPages; + EFI_PHYSICAL_ADDRESS FdtConfigurationTableBase; + + Status =3D gBS->LocateProtocol ( + &gEfiDevicePathFromTextProtocolGuid, + NULL, + (VOID **)&EfiDevicePathFromTextProtocol + ); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "InstallFdt() - Failed to locate EFI_DEVICE_PATH_= FROM_TEXT_PROTOCOL protocol\n")); + return Status; + } + + DevicePath =3D (EFI_DEVICE_PATH*)EfiDevicePathFromTextProtocol->ConvertT= extToDevicePath (TextDevicePath); + if (DevicePath =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // Load the FDT given its device path. + // This operation may fail if the device path is not supported. + // + FdtBlobBase =3D 0; + NumPages =3D 0; + Status =3D BdsLoadImage (DevicePath, AllocateAnyPages, &FdtBlobBase, &Fd= tBlobSize); + if (EFI_ERROR (Status)) { + goto Error; + } + + // + // Ensure that the FDT header is valid and that the Size of the Device T= ree + // is smaller than the size of the read file + // + if (fdt_check_header ((VOID*)(UINTN)FdtBlobBase) !=3D 0 || + (UINTN)fdt_totalsize ((VOID*)(UINTN)FdtBlobBase) > FdtBlobSize) { + DEBUG ((EFI_D_ERROR, "InstallFdt() - loaded FDT binary image seems cor= rupt\n")); + Status =3D EFI_LOAD_ERROR; + goto Error; + } + + // + // Store the FDT as Runtime Service Data to prevent the Kernel from + // overwritting its data. + // + NumPages =3D EFI_SIZE_TO_PAGES (FdtBlobSize); + Status =3D gBS->AllocatePages ( + AllocateAnyPages, EfiRuntimeServicesData, + NumPages, &FdtConfigurationTableBase + ); + if (EFI_ERROR (Status)) { + goto Error; + } + CopyMem ( + (VOID*)(UINTN)FdtConfigurationTableBase, + (VOID*)(UINTN)FdtBlobBase, + FdtBlobSize + ); + + // + // Install the FDT into the Configuration Table + // + Status =3D gBS->InstallConfigurationTable ( + &gFdtTableGuid, + (VOID*)(UINTN)FdtConfigurationTableBase + ); + if (EFI_ERROR (Status)) { + gBS->FreePages (FdtConfigurationTableBase, NumPages); + } + +Error: + if (FdtBlobBase !=3D 0) { + gBS->FreePages (FdtBlobBase, NumPages); + } + FreePool (DevicePath); + + return Status; +} + +/** + Main entry point of the FDT platform driver. + + @param[in] ImageHandle The firmware allocated handle for the present = driver + UEFI image. + @param[in] *SystemTable A pointer to the EFI System table. + + @retval EFI_SUCCESS The driver was initialized. + @retval EFI_OUT_OF_RESOURCES The "End of DXE" event could not be alloc= ated or + there was not enough memory in pool to in= stall + the Shell Dynamic Command protocol. + @retval EFI_LOAD_ERROR Unable to add the HII package. + +**/ +EFI_STATUS +FdtPlatformEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + EFI_HANDLE Handle; + + // + // Install the Device Tree from its expected location + // + Status =3D RunFdtInstallation (NULL); + + if (FeaturePcdGet (PcdOverridePlatformFdt) || FeaturePcdGet (PcdDumpFdtS= hellCommand)) { + // + // Register the strings for the user interface in the HII Database. + // This shows the way to the multi-language support, even if + // only the English language is actually supported. The strings to reg= ister + // are stored in the "ShellSetFdtStrings[]" array. This array is + // built by the building process from the "*.uni" file associated to + // the present driver (cf. FdtPlatfromDxe.inf). Examine your Build + // folder under your package's DEBUG folder and you will find the array + // defined in a xxxStrDefs.h file. + // + mFdtPlatformDxeHiiHandle =3D HiiAddPackages ( + &mFdtPlatformDxeHiiGuid, + ImageHandle, + FdtPlatformDxeStrings, + NULL + ); + } + + // + // If the development features are enabled, install the dynamic shell + // command "setfdt" to be able to define a device path for the FDT + // that has precedence over the device paths defined by + // "PcdFdtDevicePaths". + // + + if (FeaturePcdGet (PcdOverridePlatformFdt)) { + if (mFdtPlatformDxeHiiHandle !=3D NULL) { + // We install dynamic EFI command on separate handles as we cannot r= egister + // more than one protocol of the same protocol interface on the same= handle. + Handle =3D NULL; + Status =3D gBS->InstallMultipleProtocolInterfaces ( + &Handle, + &gEfiShellDynamicCommandProtocolGuid, + &mShellDynCmdProtocolSetFdt, + NULL + ); + if (EFI_ERROR (Status)) { + HiiRemovePackages (mFdtPlatformDxeHiiHandle); + } + } else { + Status =3D EFI_LOAD_ERROR; + } + if (EFI_ERROR (Status)) { + DEBUG (( + EFI_D_WARN, + "Unable to install \"setfdt\" EFI Shell command - %r \n", + Status + )); + } + } + + if (FeaturePcdGet (PcdDumpFdtShellCommand)) { + if (mFdtPlatformDxeHiiHandle !=3D NULL) { + // We install dynamic EFI command on separate handles as we cannot r= egister + // more than one protocol of the same protocol interface on the same= handle. + Handle =3D NULL; + Status =3D gBS->InstallMultipleProtocolInterfaces ( + &Handle, + &gEfiShellDynamicCommandProtocolGuid, + &mShellDynCmdProtocolDumpFdt, + NULL + ); + if (EFI_ERROR (Status)) { + HiiRemovePackages (mFdtPlatformDxeHiiHandle); + } + } else { + Status =3D EFI_LOAD_ERROR; + } + if (EFI_ERROR (Status)) { + DEBUG (( + EFI_D_WARN, + "Unable to install \"dumpfdt\" EFI Shell command - %r \n", + Status + )); + } + } + + return Status; +} + +/** + Run the FDT installation process. + + Loop in priority order over the device paths from which the FDT has + been asked to be retrieved for. For each device path, try to install + the FDT. Stop as soon as an installation succeeds. + + @param[in] SuccessfullDevicePath If not NULL, address where to store t= he + pointer to the text device path from + which the FDT was successfully retrie= ved. + Not used if the FDT installation fail= ed. + The returned address is the address of + an allocated buffer that has to be + freed by the caller. + + @retval EFI_SUCCESS The FDT was installed. + @retval EFI_NOT_FOUND Failed to locate a protocol or a file. + @retval EFI_INVALID_PARAMETER Invalid device path. + @retval EFI_UNSUPPORTED Device path not supported. + @retval EFI_OUT_OF_RESOURCES An allocation failed. + +**/ +EFI_STATUS +RunFdtInstallation ( + OUT CHAR16 **SuccessfullDevicePath + ) +{ + EFI_STATUS Status; + UINTN DataSize; + CHAR16 *TextDevicePath; + CHAR16 *TextDevicePathStart; + CHAR16 *TextDevicePathSeparator; + UINTN TextDevicePathLen; + + TextDevicePath =3D NULL; + // + // For development purpose, if enabled through the "PcdOverridePlatformF= dt" + // feature PCD, try first to install the FDT specified by the device pat= h in + // text form stored in the "Fdt" UEFI variable. + // + if (FeaturePcdGet (PcdOverridePlatformFdt)) { + DataSize =3D 0; + Status =3D gRT->GetVariable ( + L"Fdt", + &gFdtVariableGuid, + NULL, + &DataSize, + NULL + ); + + // + // Keep going only if the "Fdt" variable is defined. + // + + if (Status =3D=3D EFI_BUFFER_TOO_SMALL) { + TextDevicePath =3D AllocatePool (DataSize); + if (TextDevicePath =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Error; + } + + Status =3D gRT->GetVariable ( + L"Fdt", + &gFdtVariableGuid, + NULL, + &DataSize, + TextDevicePath + ); + if (EFI_ERROR (Status)) { + FreePool (TextDevicePath); + goto Error; + } + + Status =3D InstallFdt (TextDevicePath); + if (!EFI_ERROR (Status)) { + DEBUG (( + EFI_D_WARN, + "Installation of the FDT using the device path <%s> completed.\n= ", + TextDevicePath + )); + goto Done; + } + DEBUG (( + EFI_D_ERROR, + "Installation of the FDT specified by the \"Fdt\" UEFI variable fa= iled - %r\n", + Status + )); + FreePool (TextDevicePath); + } + } + + // + // Loop over the device path list provided by "PcdFdtDevicePaths". The d= evice + // paths are in text form and separated by a semi-colon. + // + + Status =3D EFI_NOT_FOUND; + for (TextDevicePathStart =3D (CHAR16*)PcdGetPtr (PcdFdtDevicePaths); + *TextDevicePathStart !=3D L'\0' ; ) { + TextDevicePathSeparator =3D StrStr (TextDevicePathStart, L";"); + + // + // Last device path of the list + // + if (TextDevicePathSeparator =3D=3D NULL) { + TextDevicePathLen =3D StrLen (TextDevicePathStart); + } else { + TextDevicePathLen =3D (UINTN)(TextDevicePathSeparator - TextDevicePa= thStart); + } + + TextDevicePath =3D AllocateCopyPool ( + (TextDevicePathLen + 1) * sizeof (CHAR16), + TextDevicePathStart + ); + if (TextDevicePath =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Error; + } + TextDevicePath[TextDevicePathLen] =3D L'\0'; + + Status =3D InstallFdt (TextDevicePath); + if (!EFI_ERROR (Status)) { + DEBUG ((EFI_D_WARN, "Installation of the FDT using the device path <= %s> completed.\n", + TextDevicePath + )); + goto Done; + } + + DEBUG ((EFI_D_WARN, "Installation of the FDT using the device path <%s= > failed - %r.\n", + TextDevicePath, Status + )); + FreePool (TextDevicePath); + + if (TextDevicePathSeparator =3D=3D NULL) { + goto Error; + } + TextDevicePathStart =3D TextDevicePathSeparator + 1; + } + +Error: +Done: + + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "Failed to install the FDT - %r.\n", Status)); + return Status; + } + + if (SuccessfullDevicePath !=3D NULL) { + *SuccessfullDevicePath =3D TextDevicePath; + } else { + FreePool (TextDevicePath); + } + + return EFI_SUCCESS; +} + +/** + Transcode one of the EFI return code used by the model into an EFI Shell= return code. + + @param[in] Status EFI return code. + + @return Transcoded EFI Shell return code. + +**/ +SHELL_STATUS +EfiCodeToShellCode ( + IN EFI_STATUS Status + ) +{ + SHELL_STATUS ShellStatus; + + switch (Status) { + case EFI_SUCCESS : + ShellStatus =3D SHELL_SUCCESS; + break; + + case EFI_INVALID_PARAMETER : + ShellStatus =3D SHELL_INVALID_PARAMETER; + break; + + case EFI_UNSUPPORTED : + ShellStatus =3D SHELL_UNSUPPORTED; + break; + + case EFI_DEVICE_ERROR : + ShellStatus =3D SHELL_DEVICE_ERROR; + break; + + case EFI_WRITE_PROTECTED : + case EFI_SECURITY_VIOLATION : + ShellStatus =3D SHELL_ACCESS_DENIED; + break; + + case EFI_OUT_OF_RESOURCES : + ShellStatus =3D SHELL_OUT_OF_RESOURCES; + break; + + case EFI_NOT_FOUND : + ShellStatus =3D SHELL_NOT_FOUND; + break; + + default : + ShellStatus =3D SHELL_ABORTED; + } + + return ShellStatus; +} diff --git a/Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatform.h b/Platform/A= RM/Drivers/FdtPlatformDxe/FdtPlatform.h new file mode 100644 index 000000000000..a631f2847bf5 --- /dev/null +++ b/Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatform.h @@ -0,0 +1,174 @@ +/** @file + + Copyright (c) 2015, ARM Ltd. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#ifndef __FDT_PLATFORM_DXE_H__ +#define __FDT_PLATFORM_DXE_H__ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +extern EFI_HANDLE mFdtPlatformDxeHiiHandle; + +/** + Transcode one of the EFI return code used by the model into an EFI Shell= return code. + + @param[in] Status EFI return code. + + @return Transcoded EFI Shell return code. + +**/ +SHELL_STATUS +EfiCodeToShellCode ( + IN EFI_STATUS Status + ); + +/** + Run the FDT installation process. + + Loop in priority order over the device paths from which the FDT has + been asked to be retrieved for. For each device path, try to install + the FDT. Stop as soon as an installation succeeds. + + @param[in] SuccessfullDevicePath If not NULL, address where to store t= he + pointer to the text device path from + which the FDT was successfully retrie= ved. + Not used if the FDT installation fail= ed. + The returned address is the address of + an allocated buffer that has to be + freed by the caller. + + @retval EFI_SUCCESS The FDT was installed. + @retval EFI_NOT_FOUND Failed to locate a protocol or a file. + @retval EFI_INVALID_PARAMETER Invalid device path. + @retval EFI_UNSUPPORTED Device path not supported. + @retval EFI_OUT_OF_RESOURCES An allocation failed. + +**/ +EFI_STATUS +RunFdtInstallation ( + OUT CHAR16 **SuccessfullDevicePath + ); + +/** + This is the shell command "setfdt" handler function. This function handl= es + the command when it is invoked in the shell. + + @param[in] This The instance of the + EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL. + @param[in] SystemTable The pointer to the UEFI system table. + @param[in] ShellParameters The parameters associated with the command. + @param[in] Shell The instance of the shell protocol used in = the + context of processing this command. + + @return SHELL_SUCCESS The operation was successful. + @return SHELL_ABORTED Operation aborted due to internal erro= r. + @return SHELL_INVALID_PARAMETER The parameters of the command are not = valid. + @return SHELL_INVALID_PARAMETER The EFI Shell file path is not valid. + @return SHELL_NOT_FOUND Failed to locate a protocol or a file. + @return SHELL_UNSUPPORTED Device path not supported. + @return SHELL_OUT_OF_RESOURCES A memory allocation failed. + @return SHELL_DEVICE_ERROR The "Fdt" variable could not be saved = due to a hardware failure. + @return SHELL_ACCESS_DENIED The "Fdt" variable is read-only. + @return SHELL_ACCESS_DENIED The "Fdt" variable cannot be deleted. + @return SHELL_ACCESS_DENIED The "Fdt" variable could not be writte= n due to security violation. + +**/ +SHELL_STATUS +EFIAPI +ShellDynCmdSetFdtHandler ( + IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This, + IN EFI_SYSTEM_TABLE *SystemTable, + IN EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters, + IN EFI_SHELL_PROTOCOL *Shell + ); + +/** + This is the shell command "setfdt" help handler function. This + function returns the formatted help for the "setfdt" command. + The format matchs that in Appendix B of the revision 2.1 of the + UEFI Shell Specification. + + @param[in] This The instance of the EFI_SHELL_DYNAMIC_COMMAND_PROT= OCOL. + @param[in] Language The pointer to the language string to use. + + @return CHAR16* Pool allocated help string, must be freed by caller. +**/ +CHAR16* +EFIAPI +ShellDynCmdSetFdtGetHelp ( + IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This, + IN CONST CHAR8 *Language + ); + +/** + This is the shell command "dumpfdt" handler function. This function hand= les + the command when it is invoked in the shell. + + @param[in] This The instance of the + EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL. + @param[in] SystemTable The pointer to the UEFI system table. + @param[in] ShellParameters The parameters associated with the command. + @param[in] Shell The instance of the shell protocol used in = the + context of processing this command. + + @return SHELL_SUCCESS The operation was successful. + @return SHELL_ABORTED Operation aborted due to internal erro= r. + @return SHELL_NOT_FOUND Failed to locate the Device Tree into = the EFI Configuration Table + @return SHELL_OUT_OF_RESOURCES A memory allocation failed. + +**/ +SHELL_STATUS +EFIAPI +ShellDynCmdDumpFdtHandler ( + IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This, + IN EFI_SYSTEM_TABLE *SystemTable, + IN EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters, + IN EFI_SHELL_PROTOCOL *Shell + ); + +/** + This is the shell command "dumpfdt" help handler function. This + function returns the formatted help for the "dumpfdt" command. + The format matchs that in Appendix B of the revision 2.1 of the + UEFI Shell Specification. + + @param[in] This The instance of the EFI_SHELL_DYNAMIC_COMMAND_PROT= OCOL. + @param[in] Language The pointer to the language string to use. + + @return CHAR16* Pool allocated help string, must be freed by caller. +**/ +CHAR16* +EFIAPI +ShellDynCmdDumpFdtGetHelp ( + IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This, + IN CONST CHAR8 *Language + ); + +#endif /* __FDT_PLATFORM_DXE_H__ */ diff --git a/Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.dec b/Platf= orm/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.dec new file mode 100644 index 000000000000..3faced589504 --- /dev/null +++ b/Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.dec @@ -0,0 +1,31 @@ +#/** @file +# +# Copyright (c) 2011-2017, ARM Limited. All rights reserved. +# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD 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 IM= PLIED. +# +#**/ + +[Defines] + DEC_SPECIFICATION =3D 0x0001001A + PACKAGE_NAME =3D FdtPlatformDxe + PACKAGE_GUID =3D ed22c1e5-71cb-48d6-a9d8-c20f8d6b909f + PACKAGE_VERSION =3D 0.1 + +[Guids] + gFdtPlatformDxeTokenSpaceGuid =3D { 0xbfcaa0af, 0xedd4, 0x4ce7, { 0xbd, = 0xb3, 0x39, 0x15, 0x07, 0x28, 0x65, 0x77 } } + +[PcdsFeatureFlag.common] + # Enable the development specific features + gFdtPlatformDxeTokenSpaceGuid.PcdOverridePlatformFdt|TRUE|BOOLEAN|0x0000= 0001 + # Add 'dumpfdt' EFI Shell command + gFdtPlatformDxeTokenSpaceGuid.PcdDumpFdtShellCommand|TRUE|BOOLEAN|0x0000= 0002 + +[PcdsFixedAtBuild.common, PcdsDynamic.common] + gFdtPlatformDxeTokenSpaceGuid.PcdFdtDevicePaths|L""|VOID*|0x00000055 diff --git a/Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.inf b/Platf= orm/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.inf new file mode 100644 index 000000000000..f9a5aee3596e --- /dev/null +++ b/Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.inf @@ -0,0 +1,65 @@ +#/** @file +# +# Copyright (c) 2015, ARM Ltd. All rights reserved.
+# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the B= SD 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 IM= PLIED. +# +#**/ + +[Defines] + INF_VERSION =3D 0x00010006 + BASE_NAME =3D FdtPlatformDxe + MODULE_UNI_FILE =3D FdtPlatformDxe.uni + FILE_GUID =3D 4bd726b2-d1c8-4e98-ba08-2bc2ab251daf + MODULE_TYPE =3D UEFI_DRIVER + VERSION_STRING =3D 0.1 + ENTRY_POINT =3D FdtPlatformEntryPoint + +[Sources.common] + FdtPlatform.c + FdtPlatformDxe.uni + ShellDumpFdt.c + ShellSetFdt.c + +[Packages] + ArmPkg/ArmPkg.dec + EmbeddedPkg/EmbeddedPkg.dec + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.dec + ShellPkg/ShellPkg.dec + +[LibraryClasses] + BaseMemoryLib + BdsLib + DebugLib + DxeServicesTableLib + FdtLib + HiiLib + ShellLib + UefiBootServicesTableLib + UefiDriverEntryPoint + UefiRuntimeServicesTableLib + +[Protocols] + gEfiDevicePathFromTextProtocolGuid + gEfiDevicePathToTextProtocolGuid + gEfiShellDynamicCommandProtocolGuid + +[Guids] + gEfiEndOfDxeEventGroupGuid + gFdtTableGuid + gFdtVariableGuid + +[FeaturePcd] + gFdtPlatformDxeTokenSpaceGuid.PcdDumpFdtShellCommand + gFdtPlatformDxeTokenSpaceGuid.PcdOverridePlatformFdt + +[Pcd] + gFdtPlatformDxeTokenSpaceGuid.PcdFdtDevicePaths diff --git a/Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.uni b/Platf= orm/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.uni new file mode 100644 index 000000000000..f8bde834841d --- /dev/null +++ b/Platform/ARM/Drivers/FdtPlatformDxe/FdtPlatformDxe.uni @@ -0,0 +1,109 @@ +// *++ +// +// Copyright (c) 2014, ARM Ltd. All rights reserved.
+// +// This program and the accompanying materials are licensed and made avail= able +// 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 IM= PLIED. +// +// +// Module Name: +// +// FdtPlatformDxe +// +// Abstract: +// +// String definitions for the EFI Shell 'setfdt' command +// +// Revision History: +// +// --*/ + +/=3D# + +#langdef en-US "English" + +#string STR_SETFDT_INSTALLING #language en-US "Installing the FD= T ...\r\n" +#string STR_SETFDT_INSTALL_SUCCEEDED #language en-US "Installation of\r= \n'%s'\r\ncompleted.\r\n" + +#string STR_SETFDT_UPDATING #language en-US "Updating the FDT = device path ...\r\n" +#string STR_SETFDT_UPDATE_SUCCEEDED #language en-US "Update of the FDT= device path '%s' completed.\r\n" +#string STR_SETFDT_UPDATE_DELETED #language en-US "The UEFI variable= "Fdt" was deleted.\r\n" + +#string STR_SETFDT_INVALID_DEVICE_PATH #language en-US "Invalid device pa= th.\r\n" +#string STR_SETFDT_INVALID_PATH #language en-US "The EFI Shell or = device file path '%s' is invalid.\r\n" +#string STR_SETFDT_ERROR #language en-US "Error - %r.\r\n" +#string STR_SETFDT_DEVICE_PATH_LIST #language en-US "FDT device paths = :\r\n" +#string STR_SETFDT_DEVICE_PATH #language en-US "'%s'\r\n" + +#string STR_GET_HELP_SETFDT #language en-US "" +".TH setfdt 0 "Define and/or install a new Flat Device Tree (FDT) for the = platform."\r\n" +".SH NAME\r\n" +"Define and/or re-install a Flat Device Tree (FDT)\r\n" +".SH SYNOPSIS\r\n" +"setfdt [-i] [fdt_path]\r\n" +".SH OPTIONS\r\n" +"-i run the FDT installation process\r\n" +"file_path EFI Shell file path or device path to a FDT\r\n" +"\r\n" +".SH DESCRIPTION\r\n" +"NOTES:\r\n" +"1. If a valid EFI Shell file path is passed to the command, then the\r\n" +" command translates the EFI Shell file path into a device path in the\r= \n" +" text form and saves it in the non volatile UEFI variable "Fdt". If\r\n" +" the path to the FDT is a device path in the text form, it is saved as\= r\n" +" it is in the non volatile UEFI variable "Fdt". The next time the FDT\r= \n" +" installation process is run, it will first try to install the FDT from= \r\n" +" the device path specified by the UEFI variable "Fdt".\r\n" +" \r\n +"2. If the option -i is passed to the command, then the FDT installation\r= \n" +" process is run. If a path to the FDT is passed to the command as well,= \r\n" +" the update of the "Fdt" UEFI variable is done first before to launch\r= \n" +" the FDT installation process.\r\n" +" \r\n +".SH RETURNVALUES\r\n" +"SHELL_SUCCESS Operation(s) completed.\r\n" +"SHELL_ABORTED Operation aborted.\r\n" +"SHELL_INVALID_PARAMETER Invalid argument(s).\r\n" +"SHELL_NOT_FOUND Failed to locate a protocol or a file.\r\n" +"SHELL_UNSUPPORTED Device path not supported.\r\n" +"SHELL_OUT_OF_RESOURCES A memory allocation failed.\r\n" +"SHELL_DEVICE ERROR Hardware failure.\r\n" +"SHELL_ACCESS_DENIED Access to the Fdt UEFI variable for modification= denied.\r\n" +".SH EXAMPLES\r\n" +"EXAMPLES:\r\n" +"1. Relaunch the FDT installation process :\r\n" +" Shell> setfdt -i\r\n" +" \r\n" +"2. Set the EFI Shell file path 'fs0:\>fdt.dtb' to be the default path\r\n" +" to the FDT :\r\n" +" Shell> setfdt fs0:fdt.dtb\r\n" +" \r\n" +"3. Set a TFTP device path to be the default path to the FDT :\r\n" +" Shell> setfdt MAC(0002f700570b,0x1)/IPv4(192.168.1.1)/fdt.dtb\r\n" +" where . 00:02:f7:00:57:0b is the MAC address of the network\r\n" +" interface card to be used. The 'ifconfig -l' EFI Shell\r\n" +" command allows to get the MAC address of the network\r\n" +" interface cards.\r\n" +" . 192.168.1.1 is the address of the TFTP server.\r\n" +" . fdt.dtb is the file path to the FDT file on the server.\r\n" +"4. Display the FDT device paths from the highest to the lowest\r\n" +" priority :\r\n" +" Shell> setfdt\r\n" +"5. Delete the "Fdt" UEFI variable :\r\n" +" Shell> setfdt ""\r\n" +"\r\n" + +#string STR_GET_HELP_DUMPFDT #language en-US "" +".TH dumpfdt 0 "Dump installed Flat Device Tree (FDT) of the platform."\r\= n" +".SH NAME\r\n" +"Dump current Flat Device Tree (FDT)\r\n" +".SH SYNOPSIS\r\n" +"dumpfdt\r\n" +"\r\n" +".SH DESCRIPTION\r\n" +"\r\n" diff --git a/Platform/ARM/Drivers/FdtPlatformDxe/README.txt b/Platform/ARM/= Drivers/FdtPlatformDxe/README.txt new file mode 100644 index 000000000000..5f052d9a63aa --- /dev/null +++ b/Platform/ARM/Drivers/FdtPlatformDxe/README.txt @@ -0,0 +1,72 @@ +/** @file + + Copyright (c) 2015, ARM Ltd. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +The purpose of the FdtPlatformDxe UEFI driver is to install the Flat Device +Tree (FDT) of the platform the UEFI frimware is running on into the UEFI +Configuration Table. The FDT is identified within the UEFI Configuration +Table by the "gFdtTableGuid" GUID defined in "EmbeddedPkg.dec". + +Once installed, an UEFI application or OS boot loader can get from the UEFI +Configuration Table the FDT of the platform from the "gFdtTableGuid" GUID. + +The installation is done after each boot at the end of the DXE phase, +just before the BDS phase. It is done at the end of the DXE phase to be su= re +that all drivers have been dispatched. That way, all UEFI protocols that m= ay +be needed to retrieve the FDT can be made available. It is done before the= BDS +phase to be able to provide the FDT during that phase. + +The present driver tries to retrieve the FDT from the device paths defined= in the +"gFdtPlatformDxeTokenSpaceGuid.PcdFdtDevicePaths" PCD. The "PcdFdtDevicePa= ths" +PCD contains a list a device paths. The device paths are in the text form = and +separated by semi-colons. The present driver tries the device paths in the= order +it finds them in the "PcdFdtDevicePaths" PCD as long as he did not install +succesfully a FDT. + +The "PcdFdtDevicePaths" PCD is a dynamic PCD that can be modified during t= he +DXE phase. This allows for exemple to select the right FDT when a binary is +intended to run on several platforms and/or variants of a platform. + +If the driver manages to download a FDT from one of the device paths menti= oned +above then it installs it in the UEFI Configuration table and the run over= the +device paths is stopped. + +For development purposes only, if the feature PCD "gFdtPlatformDxeTokenSpa= ceGuid. +PcdOverridePlatformFdt" is equal to TRUE, then before to try to install the +FDT from the device paths listed in the "PcdFdtDevicePaths" PCD, the prese= nt +driver tries to install it using the device path defined by the UEFI varia= ble +"Fdt". If the variable does not exist or the installation using the device= path +defined by the UEFI variable fails then the installation proceeds as descr= ibed +above. + +Furthermore and again for development purposes only, if the feature PCD +"PcdOverridePlatformFdt" is equal to TRUE, the current driver provides the= EFI +Shell command "setfdt" to define the location of the FDT by the mean of an= EFI +Shell file path (like "fs2:\boot\fdt.dtb") or a device path. + +If the path passed in to the command is a valid EFI Shell file path, the +command translates it into the corresponding device path and stores that +device path in the "Fdt" UEFI variable asking for the variable to be non +volatile. + +If the path passed in to the command is not recognised as a valid EFI +Shell device path, the command handles it as device path and stored +in the "Fdt" UEFI variable as it is. + +Finally, the "-i" option of the "setfdt" command allows to trigger the FDT +installation process. The installation process is completed when the comma= nd +returns. The command can be invoked with the "-i" option only and in that +case the "Fdt" UEFI variable is not updated and the command just runs the +FDT installation process. If the command is invoked with the "-i" option a= nd +an EFI Shell file path then first the "Fdt" UEFI variable is updated accor= dingly +and then the FDT installation process is run. diff --git a/Platform/ARM/Drivers/FdtPlatformDxe/ShellDumpFdt.c b/Platform/= ARM/Drivers/FdtPlatformDxe/ShellDumpFdt.c new file mode 100644 index 000000000000..c7dc8985685b --- /dev/null +++ b/Platform/ARM/Drivers/FdtPlatformDxe/ShellDumpFdt.c @@ -0,0 +1,279 @@ +/** @file + + Copyright (c) 2015, ARM Ltd. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "FdtPlatform.h" + +#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1)) +#define PALIGN(p, a) ((void *)(ALIGN ((unsigned long)(p), (a)))) +#define GET_CELL(p) (p +=3D 4, *((const uint32_t *)(p-4))) + +STATIC +UINTN +IsPrintableString ( + IN CONST VOID* data, + IN UINTN len + ) +{ + CONST CHAR8 *s =3D data; + CONST CHAR8 *ss; + + // Zero length is not + if (len =3D=3D 0) { + return 0; + } + + // Must terminate with zero + if (s[len - 1] !=3D '\0') { + return 0; + } + + ss =3D s; + while (*s/* && isprint (*s)*/) { + s++; + } + + // Not zero, or not done yet + if (*s !=3D '\0' || (s + 1 - ss) < len) { + return 0; + } + + return 1; +} + +STATIC +VOID +PrintData ( + IN CONST CHAR8* data, + IN UINTN len + ) +{ + UINTN i; + CONST CHAR8 *p =3D data; + + // No data, don't print + if (len =3D=3D 0) + return; + + if (IsPrintableString (data, len)) { + Print (L" =3D \"%a\"", (const char *)data); + } else if ((len % 4) =3D=3D 0) { + Print (L" =3D <"); + for (i =3D 0; i < len; i +=3D 4) { + Print (L"0x%08x%a", fdt32_to_cpu (GET_CELL (p)), i < (len - 4) ? " "= : ""); + } + Print (L">"); + } else { + Print (L" =3D ["); + for (i =3D 0; i < len; i++) + Print (L"%02x%a", *p++, i < len - 1 ? " " : ""); + Print (L"]"); + } +} + +STATIC +VOID +DumpFdt ( + IN VOID* FdtBlob + ) +{ + struct fdt_header *bph; + UINT32 off_dt; + UINT32 off_str; + CONST CHAR8* p_struct; + CONST CHAR8* p_strings; + CONST CHAR8* p; + CONST CHAR8* s; + CONST CHAR8* t; + UINT32 tag; + UINTN sz; + UINTN depth; + UINTN shift; + UINT32 version; + + { + // Can 'memreserve' be printed by below code? + INTN num =3D fdt_num_mem_rsv (FdtBlob); + INTN i, err; + UINT64 addr =3D 0, size =3D 0; + + for (i =3D 0; i < num; i++) { + err =3D fdt_get_mem_rsv (FdtBlob, i, &addr, &size); + if (err) { + DEBUG ((EFI_D_ERROR, "Error (%d) : Cannot get memreserve section (= %d)\n", err, i)); + } + else { + Print (L"/memreserve/ \t0x%lx \t0x%lx;\n", addr, size); + } + } + } + + depth =3D 0; + shift =3D 4; + + bph =3D FdtBlob; + off_dt =3D fdt32_to_cpu (bph->off_dt_struct); + off_str =3D fdt32_to_cpu (bph->off_dt_strings); + p_struct =3D (CONST CHAR8*)FdtBlob + off_dt; + p_strings =3D (CONST CHAR8*)FdtBlob + off_str; + version =3D fdt32_to_cpu (bph->version); + + p =3D p_struct; + while ((tag =3D fdt32_to_cpu (GET_CELL (p))) !=3D FDT_END) { + if (tag =3D=3D FDT_BEGIN_NODE) { + s =3D p; + p =3D PALIGN (p + AsciiStrLen (s) + 1, 4); + + if (*s =3D=3D '\0') + s =3D "/"; + + Print (L"%*s%a {\n", depth * shift, L" ", s); + + depth++; + continue; + } + + if (tag =3D=3D FDT_END_NODE) { + depth--; + + Print (L"%*s};\n", depth * shift, L" "); + continue; + } + + if (tag =3D=3D FDT_NOP) { + Print (L"%*s// [NOP]\n", depth * shift, L" "); + continue; + } + + if (tag !=3D FDT_PROP) { + Print (L"%*s ** Unknown tag 0x%08x\n", depth * shift, L" ", tag); + break; + } + sz =3D fdt32_to_cpu (GET_CELL (p)); + s =3D p_strings + fdt32_to_cpu (GET_CELL (p)); + if (version < 16 && sz >=3D 8) + p =3D PALIGN (p, 8); + t =3D p; + + p =3D PALIGN (p + sz, 4); + + Print (L"%*s%a", depth * shift, L" ", s); + PrintData (t, sz); + Print (L";\n"); + } +} + +/** + This is the shell command "dumpfdt" handler function. This function hand= les + the command when it is invoked in the shell. + + @param[in] This The instance of the + EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL. + @param[in] SystemTable The pointer to the UEFI system table. + @param[in] ShellParameters The parameters associated with the command. + @param[in] Shell The instance of the shell protocol used in = the + context of processing this command. + + @return SHELL_SUCCESS The operation was successful. + @return SHELL_ABORTED Operation aborted due to internal erro= r. + @return SHELL_NOT_FOUND Failed to locate the Device Tree into = the EFI Configuration Table + @return SHELL_OUT_OF_RESOURCES A memory allocation failed. + +**/ +SHELL_STATUS +EFIAPI +ShellDynCmdDumpFdtHandler ( + IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This, + IN EFI_SYSTEM_TABLE *SystemTable, + IN EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters, + IN EFI_SHELL_PROTOCOL *Shell + ) +{ + SHELL_STATUS ShellStatus; + EFI_STATUS Status; + VOID *FdtBlob; + + ShellStatus =3D SHELL_SUCCESS; + + // + // Install the Shell and Shell Parameters Protocols on the driver + // image. This is necessary for the initialisation of the Shell + // Library to succeed in the next step. + // + Status =3D gBS->InstallMultipleProtocolInterfaces ( + &gImageHandle, + &gEfiShellProtocolGuid, Shell, + &gEfiShellParametersProtocolGuid, ShellParameters, + NULL + ); + if (EFI_ERROR (Status)) { + return SHELL_ABORTED; + } + + // + // Initialise the Shell Library as we are going to use it. + // Assert that the return code is EFI_SUCCESS as it should. + // To anticipate any change is the codes returned by + // ShellInitialize(), leave in case of error. + // + Status =3D ShellInitialize (); + if (EFI_ERROR (Status)) { + ASSERT_EFI_ERROR (Status); + return SHELL_ABORTED; + } + + Status =3D EfiGetSystemConfigurationTable (&gFdtTableGuid, &FdtBlob); + if (EFI_ERROR (Status)) { + Print (L"ERROR: Did not find the Fdt Blob.\n"); + return EfiCodeToShellCode (Status); + } + + DumpFdt (FdtBlob); + + gBS->UninstallMultipleProtocolInterfaces ( + gImageHandle, + &gEfiShellProtocolGuid, Shell, + &gEfiShellParametersProtocolGuid, ShellParameters, + NULL + ); + + return ShellStatus; +} + +/** + This is the shell command "dumpfdt" help handler function. This + function returns the formatted help for the "dumpfdt" command. + The format matchs that in Appendix B of the revision 2.1 of the + UEFI Shell Specification. + + @param[in] This The instance of the EFI_SHELL_DYNAMIC_COMMAND_PROT= OCOL. + @param[in] Language The pointer to the language string to use. + + @return CHAR16* Pool allocated help string, must be freed by caller. +**/ +CHAR16* +EFIAPI +ShellDynCmdDumpFdtGetHelp ( + IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This, + IN CONST CHAR8 *Language + ) +{ + // + // This allocates memory. The caller has to free the allocated memory. + // + return HiiGetString ( + mFdtPlatformDxeHiiHandle, + STRING_TOKEN (STR_GET_HELP_DUMPFDT), + Language + ); +} diff --git a/Platform/ARM/Drivers/FdtPlatformDxe/ShellSetFdt.c b/Platform/A= RM/Drivers/FdtPlatformDxe/ShellSetFdt.c new file mode 100644 index 000000000000..9be23c845593 --- /dev/null +++ b/Platform/ARM/Drivers/FdtPlatformDxe/ShellSetFdt.c @@ -0,0 +1,468 @@ +/** @file + + Copyright (c) 2015, ARM Ltd. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BS= D License + which accompanies this distribution. The full text of the license may b= e 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 IMP= LIED. + +**/ + +#include "FdtPlatform.h" + +STATIC CONST SHELL_PARAM_ITEM ParamList[] =3D { + {L"-i", TypeFlag }, + {NULL , TypeMax } +}; + +/** + Display FDT device paths. + + Display in text form the device paths used to install the FDT from the + highest to the lowest priority. + +**/ +STATIC +VOID +DisplayFdtDevicePaths ( + VOID + ) +{ + EFI_STATUS Status; + UINTN DataSize; + CHAR16 *TextDevicePath; + CHAR16 *TextDevicePaths; + CHAR16 *TextDevicePathSeparator; + + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_DEVICE_PATH_LIST), + mFdtPlatformDxeHiiHandle + ); + + if (FeaturePcdGet (PcdOverridePlatformFdt)) { + DataSize =3D 0; + Status =3D gRT->GetVariable ( + L"Fdt", + &gFdtVariableGuid, + NULL, + &DataSize, + NULL + ); + + // + // Keep going only if the "Fdt" variable is defined. + // + + if (Status =3D=3D EFI_BUFFER_TOO_SMALL) { + TextDevicePath =3D AllocatePool (DataSize); + if (TextDevicePath =3D=3D NULL) { + return; + } + + Status =3D gRT->GetVariable ( + L"Fdt", + &gFdtVariableGuid, + NULL, + &DataSize, + TextDevicePath + ); + if (!EFI_ERROR (Status)) { + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_DEVICE_PATH), + mFdtPlatformDxeHiiHandle, + TextDevicePath + ); + } + + FreePool (TextDevicePath); + } + } + + // + // Loop over the device path list provided by "PcdFdtDevicePaths". The d= evice + // paths are in text form and separated by a semi-colon. + // + + TextDevicePaths =3D AllocateCopyPool ( + StrSize ((CHAR16*)PcdGetPtr (PcdFdtDevicePaths)), + (CHAR16*)PcdGetPtr (PcdFdtDevicePaths) + ); + if (TextDevicePaths =3D=3D NULL) { + return; + } + + for (TextDevicePath =3D TextDevicePaths; + *TextDevicePath !=3D L'\0' ; ) { + TextDevicePathSeparator =3D StrStr (TextDevicePath, L";"); + + if (TextDevicePathSeparator !=3D NULL) { + *TextDevicePathSeparator =3D L'\0'; + } + + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_DEVICE_PATH), + mFdtPlatformDxeHiiHandle, + TextDevicePath + ); + + if (TextDevicePathSeparator =3D=3D NULL) { + break; + } + TextDevicePath =3D TextDevicePathSeparator + 1; + } + + FreePool (TextDevicePaths); +} + +/** + Update the text device path stored in the "Fdt" UEFI variable given + an EFI Shell file path or a text device path. + + This function is a subroutine of the ShellDynCmdSetFdtHandler() function + to make its code easier to read. + + @param[in] Shell The instance of the shell protocol used in the + context of processing the "setfdt" command. + @param[in] FilePath EFI Shell path or the device path to the FDT = file. + + @return SHELL_SUCCESS The text device path was succesfully u= pdated. + @return SHELL_INVALID_PARAMETER The Shell file path is not valid. + @return SHELL_OUT_OF_RESOURCES A memory allocation failed. + @return SHELL_DEVICE_ERROR The "Fdt" variable could not be saved = due to a hardware failure. + @return SHELL_ACCESS_DENIED The "Fdt" variable is read-only. + @return SHELL_ACCESS_DENIED The "Fdt" variable cannot be deleted. + @return SHELL_ACCESS_DENIED The "Fdt" variable could not be writte= n due to security violation. + @return SHELL_NOT_FOUND Device path to text protocol not found. + @return SHELL_ABORTED Operation aborted. + +**/ +STATIC +SHELL_STATUS +UpdateFdtTextDevicePath ( + IN EFI_SHELL_PROTOCOL *Shell, + IN CONST CHAR16 *FilePath + ) +{ + EFI_STATUS Status; + EFI_DEVICE_PATH *DevicePath; + EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *EfiDevicePathToTextProtocol; + CHAR16 *TextDevicePath; + CHAR16 *FdtVariableValue; + EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *EfiDevicePathFromTextProtocol; + SHELL_STATUS ShellStatus; + + ASSERT (FilePath !=3D NULL); + DevicePath =3D NULL; + TextDevicePath =3D NULL; + FdtVariableValue =3D NULL; + + if (*FilePath !=3D L'\0') { + DevicePath =3D Shell->GetDevicePathFromFilePath (FilePath); + if (DevicePath !=3D NULL) { + Status =3D gBS->LocateProtocol ( + &gEfiDevicePathToTextProtocolGuid, + NULL, + (VOID **)&EfiDevicePathToTextProtocol + ); + if (EFI_ERROR (Status)) { + goto Error; + } + + TextDevicePath =3D EfiDevicePathToTextProtocol->ConvertDevicePathToT= ext ( + DevicePath, + FALSE, + FALSE + ); + if (TextDevicePath =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Error; + } + FdtVariableValue =3D TextDevicePath; + } else { + // + // Try to convert back the EFI Device Path String into a EFI device = Path + // to ensure the format is valid + // + Status =3D gBS->LocateProtocol ( + &gEfiDevicePathFromTextProtocolGuid, + NULL, + (VOID **)&EfiDevicePathFromTextProtocol + ); + if (EFI_ERROR (Status)) { + goto Error; + } + + DevicePath =3D EfiDevicePathFromTextProtocol->ConvertTextToDevicePat= h ( + FilePath + ); + if (DevicePath =3D=3D NULL) { + Status =3D EFI_INVALID_PARAMETER; + goto Error; + } + FdtVariableValue =3D (CHAR16*)FilePath; + } + } + + Status =3D gRT->SetVariable ( + (CHAR16*)L"Fdt", + &gFdtVariableGuid, + EFI_VARIABLE_RUNTIME_ACCESS | + EFI_VARIABLE_NON_VOLATILE | + EFI_VARIABLE_BOOTSERVICE_ACCESS , + (FdtVariableValue !=3D NULL) ? + StrSize (FdtVariableValue) : 0, + FdtVariableValue + ); + +Error: + ShellStatus =3D EfiCodeToShellCode (Status); + if (!EFI_ERROR (Status)) { + if (FdtVariableValue !=3D NULL) { + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_UPDATE_SUCCEEDED), + mFdtPlatformDxeHiiHandle, + FdtVariableValue + ); + } else { + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_UPDATE_DELETED), + mFdtPlatformDxeHiiHandle + ); + } + } else { + if (Status =3D=3D EFI_INVALID_PARAMETER) { + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_INVALID_PATH), + mFdtPlatformDxeHiiHandle, + FilePath + ); + } else { + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_ERROR), + mFdtPlatformDxeHiiHandle, + Status + ); + } + } + + if (DevicePath !=3D NULL) { + FreePool (DevicePath); + } + if (TextDevicePath !=3D NULL) { + FreePool (TextDevicePath); + } + + return ShellStatus; +} + +/** + This is the shell command "setfdt" handler function. This function handl= es + the command when it is invoked in the shell. + + @param[in] This The instance of the + EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL. + @param[in] SystemTable The pointer to the UEFI system table. + @param[in] ShellParameters The parameters associated with the command. + @param[in] Shell The instance of the shell protocol used in = the + context of processing this command. + + @return SHELL_SUCCESS The operation was successful. + @return SHELL_ABORTED Operation aborted due to internal erro= r. + @return SHELL_INVALID_PARAMETER The parameters of the command are not = valid. + @return SHELL_INVALID_PARAMETER The EFI Shell file path is not valid. + @return SHELL_NOT_FOUND Failed to locate a protocol or a file. + @return SHELL_UNSUPPORTED Device path not supported. + @return SHELL_OUT_OF_RESOURCES A memory allocation failed. + @return SHELL_DEVICE_ERROR The "Fdt" variable could not be saved = due to a hardware failure. + @return SHELL_ACCESS_DENIED The "Fdt" variable is read-only. + @return SHELL_ACCESS_DENIED The "Fdt" variable cannot be deleted. + @return SHELL_ACCESS_DENIED The "Fdt" variable could not be writte= n due to security violation. + +**/ +SHELL_STATUS +EFIAPI +ShellDynCmdSetFdtHandler ( + IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This, + IN EFI_SYSTEM_TABLE *SystemTable, + IN EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters, + IN EFI_SHELL_PROTOCOL *Shell + ) +{ + SHELL_STATUS ShellStatus; + EFI_STATUS Status; + LIST_ENTRY *ParamPackage; + BOOLEAN FilePath; + CONST CHAR16 *ValueStr; + CHAR16 *TextDevicePath; + + ShellStatus =3D SHELL_SUCCESS; + ParamPackage =3D NULL; + FilePath =3D FALSE; + + // + // Install the Shell and Shell Parameters Protocols on the driver + // image. This is necessary for the initialisation of the Shell + // Library to succeed in the next step. + // + Status =3D gBS->InstallMultipleProtocolInterfaces ( + &gImageHandle, + &gEfiShellProtocolGuid, Shell, + &gEfiShellParametersProtocolGuid, ShellParameters, + NULL + ); + if (EFI_ERROR (Status)) { + return SHELL_ABORTED; + } + + // + // Initialise the Shell Library as we are going to use it. + // Assert that the return code is EFI_SUCCESS as it should. + // To anticipate any change is the codes returned by + // ShellInitialize(), leave in case of error. + // + Status =3D ShellInitialize (); + if (EFI_ERROR (Status)) { + ASSERT_EFI_ERROR (Status); + return SHELL_ABORTED; + } + + Status =3D ShellCommandLineParse (ParamList, &ParamPackage, NULL, TRUE); + if (!EFI_ERROR (Status)) { + switch (ShellCommandLineGetCount (ParamPackage)) { + case 1: + // + // Case "setfdt" or "setfdt -i" + // + if (!ShellCommandLineGetFlag (ParamPackage, L"-i")) { + DisplayFdtDevicePaths (); + } + break; + + case 2: + // + // Case "setfdt file_path" or + // "setfdt -i file_path" or + // "setfdt file_path -i" + // + FilePath =3D TRUE; + break; + + default: + Status =3D EFI_INVALID_PARAMETER; + } + } + if (EFI_ERROR (Status)) { + ShellStatus =3D EfiCodeToShellCode (Status); + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_ERROR), + mFdtPlatformDxeHiiHandle, + Status + ); + goto Error; + } + + // + // Update the preferred device path for the FDT if asked for. + // + if (FilePath) { + ValueStr =3D ShellCommandLineGetRawValue (ParamPackage, 1); + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_UPDATING), + mFdtPlatformDxeHiiHandle + ); + ShellStatus =3D UpdateFdtTextDevicePath (Shell, ValueStr); + if (ShellStatus !=3D SHELL_SUCCESS) { + goto Error; + } + } + + // + // Run the FDT installation process if asked for. + // + if (ShellCommandLineGetFlag (ParamPackage, L"-i")) { + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_INSTALLING), + mFdtPlatformDxeHiiHandle + ); + Status =3D RunFdtInstallation (&TextDevicePath); + ShellStatus =3D EfiCodeToShellCode (Status); + if (!EFI_ERROR (Status)) { + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_INSTALL_SUCCEEDED), + mFdtPlatformDxeHiiHandle, + TextDevicePath + ); + FreePool (TextDevicePath); + } else { + if (Status =3D=3D EFI_INVALID_PARAMETER) { + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_INVALID_DEVICE_PATH), + mFdtPlatformDxeHiiHandle + ); + } else { + ShellPrintHiiEx ( + -1, -1, NULL, + STRING_TOKEN (STR_SETFDT_ERROR), + mFdtPlatformDxeHiiHandle, + Status + ); + } + DisplayFdtDevicePaths (); + } + } + +Error: + gBS->UninstallMultipleProtocolInterfaces ( + gImageHandle, + &gEfiShellProtocolGuid, Shell, + &gEfiShellParametersProtocolGuid, ShellParameters, + NULL + ); + ShellCommandLineFreeVarList (ParamPackage); + + return ShellStatus; +} + +/** + This is the shell command "setfdt" help handler function. This + function returns the formatted help for the "setfdt" command. + The format matchs that in Appendix B of the revision 2.1 of the + UEFI Shell Specification. + + @param[in] This The instance of the EFI_SHELL_DYNAMIC_COMMAND_PROT= OCOL. + @param[in] Language The pointer to the language string to use. + + @return CHAR16* Pool allocated help string, must be freed by caller. +**/ +CHAR16* +EFIAPI +ShellDynCmdSetFdtGetHelp ( + IN EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *This, + IN CONST CHAR8 *Language + ) +{ + // + // This allocates memory. The caller has to free the allocated memory. + // + return HiiGetString ( + mFdtPlatformDxeHiiHandle, + STRING_TOKEN (STR_GET_HELP_SETFDT), + Language + ); +} --=20 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel