NorFlashQemuLib.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /** @file
  2. Copyright (c) 2023 Loongson Technology Corporation Limited. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include <Library/BaseLib.h>
  6. #include <Library/DebugLib.h>
  7. #include <Library/UefiBootServicesTableLib.h>
  8. #include <Library/VirtNorFlashPlatformLib.h>
  9. #include <Protocol/FdtClient.h>
  10. #define QEMU_NOR_BLOCK_SIZE SIZE_128KB
  11. EFI_STATUS
  12. VirtNorFlashPlatformInitialization (
  13. VOID
  14. )
  15. {
  16. return EFI_SUCCESS;
  17. }
  18. STATIC VIRT_NOR_FLASH_DESCRIPTION mNorFlashDevices;
  19. EFI_STATUS
  20. VirtNorFlashPlatformGetDevices (
  21. OUT VIRT_NOR_FLASH_DESCRIPTION **NorFlashDescriptions,
  22. OUT UINT32 *Count
  23. )
  24. {
  25. FDT_CLIENT_PROTOCOL *FdtClient;
  26. INT32 Node;
  27. EFI_STATUS Status;
  28. EFI_STATUS FindNodeStatus;
  29. CONST UINT32 *Reg;
  30. UINT32 PropSize;
  31. UINT64 Base;
  32. UINT64 Size;
  33. Status = gBS->LocateProtocol (
  34. &gFdtClientProtocolGuid,
  35. NULL,
  36. (VOID **)&FdtClient
  37. );
  38. ASSERT_EFI_ERROR (Status);
  39. FindNodeStatus = FdtClient->FindCompatibleNode (
  40. FdtClient,
  41. "cfi-flash",
  42. &Node
  43. );
  44. ASSERT_EFI_ERROR (FindNodeStatus);
  45. Status = FdtClient->GetNodeProperty (
  46. FdtClient,
  47. Node,
  48. "reg",
  49. (CONST VOID **)&Reg,
  50. &PropSize
  51. );
  52. if (EFI_ERROR (Status)) {
  53. DEBUG ((
  54. DEBUG_ERROR,
  55. "%a: GetNodeProperty () failed (Status == %r)\n",
  56. __FUNCTION__,
  57. Status
  58. ));
  59. return Status;
  60. }
  61. ASSERT ((PropSize % (4 * sizeof (UINT32))) == 0);
  62. if (PropSize < (4 * sizeof (UINT32))) {
  63. DEBUG ((
  64. DEBUG_ERROR,
  65. "%a: reg node size(%d) is too small \n",
  66. __FUNCTION__,
  67. PropSize
  68. ));
  69. return EFI_NOT_FOUND;
  70. }
  71. Base = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));
  72. Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));
  73. mNorFlashDevices.DeviceBaseAddress = (UINTN)Base;
  74. mNorFlashDevices.RegionBaseAddress = (UINTN)Base;
  75. mNorFlashDevices.Size = (UINTN)Size;
  76. mNorFlashDevices.BlockSize = QEMU_NOR_BLOCK_SIZE;
  77. Status = PcdSet32S (PcdFlashNvStorageVariableBase, Base);
  78. ASSERT_EFI_ERROR (Status);
  79. /*
  80. * Base is the value of PcdFlashNvStorageVariableBase,
  81. * PcdFlashNvStorageFtwWorkingBase can be got by
  82. * PcdFlashNvStorageVariableBase + PcdFlashNvStorageVariableSize
  83. */
  84. Base += PcdGet32 (PcdFlashNvStorageVariableSize);
  85. Status = PcdSet32S (PcdFlashNvStorageFtwWorkingBase, Base);
  86. ASSERT_EFI_ERROR (Status);
  87. /*
  88. * Now,Base is the value of PcdFlashNvStorageFtwWorkingBase,
  89. * PcdFlashNvStorageFtwSpareBase can be got by
  90. * PcdFlashNvStorageFtwWorkingBase + PcdFlashNvStorageFtwWorkingSize.
  91. */
  92. Base += PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
  93. Status = PcdSet32S (PcdFlashNvStorageFtwSpareBase, Base);
  94. ASSERT_EFI_ERROR (Status);
  95. //
  96. // UEFI takes ownership of the NOR flash, and exposes its functionality
  97. // through the UEFI Runtime Services GetVariable, SetVariable, etc. This
  98. // means we need to disable it in the device tree to prevent the OS from
  99. // attaching its device driver as well.
  100. // Note that this also hides other flash banks, but the only other flash
  101. // bank we expect to encounter is the one that carries the UEFI executable
  102. // code, which is not intended to be guest updatable, and is usually backed
  103. // in a readonly manner by QEMU anyway.
  104. //
  105. Status = FdtClient->SetNodeProperty (
  106. FdtClient,
  107. Node,
  108. "status",
  109. "disabled",
  110. sizeof ("disabled")
  111. );
  112. if (EFI_ERROR (Status)) {
  113. DEBUG ((DEBUG_WARN, "Failed to set NOR flash status to 'disabled'\n"));
  114. }
  115. *NorFlashDescriptions = &mNorFlashDevices;
  116. *Count = 1;
  117. return EFI_SUCCESS;
  118. }