PlatformBuildLib.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # @file
  2. # Script to Build OVMF UEFI firmware
  3. #
  4. # Copyright (c) Microsoft Corporation.
  5. # SPDX-License-Identifier: BSD-2-Clause-Patent
  6. ##
  7. import os
  8. import logging
  9. import io
  10. from edk2toolext.environment import shell_environment
  11. from edk2toolext.environment.uefi_build import UefiBuilder
  12. from edk2toolext.invocables.edk2_platform_build import BuildSettingsManager
  13. from edk2toolext.invocables.edk2_setup import SetupSettingsManager, RequiredSubmodule
  14. from edk2toolext.invocables.edk2_update import UpdateSettingsManager
  15. from edk2toolext.invocables.edk2_pr_eval import PrEvalSettingsManager
  16. from edk2toollib.utility_functions import RunCmd
  17. # ####################################################################################### #
  18. # Configuration for Update & Setup #
  19. # ####################################################################################### #
  20. class SettingsManager(UpdateSettingsManager, SetupSettingsManager, PrEvalSettingsManager):
  21. def GetPackagesSupported(self):
  22. ''' return iterable of edk2 packages supported by this build.
  23. These should be edk2 workspace relative paths '''
  24. return CommonPlatform.PackagesSupported
  25. def GetArchitecturesSupported(self):
  26. ''' return iterable of edk2 architectures supported by this build '''
  27. return CommonPlatform.ArchSupported
  28. def GetTargetsSupported(self):
  29. ''' return iterable of edk2 target tags supported by this build '''
  30. return CommonPlatform.TargetsSupported
  31. def GetRequiredSubmodules(self):
  32. ''' return iterable containing RequiredSubmodule objects.
  33. If no RequiredSubmodules return an empty iterable
  34. '''
  35. rs = []
  36. # intentionally declare this one with recursive false to avoid overhead
  37. rs.append(RequiredSubmodule(
  38. "CryptoPkg/Library/OpensslLib/openssl", False))
  39. # To avoid maintenance of this file for every new submodule
  40. # lets just parse the .gitmodules and add each if not already in list.
  41. # The GetRequiredSubmodules is designed to allow a build to optimize
  42. # the desired submodules but it isn't necessary for this repository.
  43. result = io.StringIO()
  44. ret = RunCmd("git", "config --file .gitmodules --get-regexp path", workingdir=self.GetWorkspaceRoot(), outstream=result)
  45. # Cmd output is expected to look like:
  46. # submodule.CryptoPkg/Library/OpensslLib/openssl.path CryptoPkg/Library/OpensslLib/openssl
  47. # submodule.SoftFloat.path ArmPkg/Library/ArmSoftFloatLib/berkeley-softfloat-3
  48. if ret == 0:
  49. for line in result.getvalue().splitlines():
  50. _, _, path = line.partition(" ")
  51. if path is not None:
  52. if path not in [x.path for x in rs]:
  53. rs.append(RequiredSubmodule(path, True)) # add it with recursive since we don't know
  54. return rs
  55. def SetArchitectures(self, list_of_requested_architectures):
  56. ''' Confirm the requests architecture list is valid and configure SettingsManager
  57. to run only the requested architectures.
  58. Raise Exception if a list_of_requested_architectures is not supported
  59. '''
  60. unsupported = set(list_of_requested_architectures) - set(self.GetArchitecturesSupported())
  61. if(len(unsupported) > 0):
  62. errorString = ( "Unsupported Architecture Requested: " + " ".join(unsupported))
  63. logging.critical( errorString )
  64. raise Exception( errorString )
  65. self.ActualArchitectures = list_of_requested_architectures
  66. def GetWorkspaceRoot(self):
  67. ''' get WorkspacePath '''
  68. return CommonPlatform.WorkspaceRoot
  69. def GetActiveScopes(self):
  70. ''' return tuple containing scopes that should be active for this process '''
  71. return CommonPlatform.Scopes
  72. def FilterPackagesToTest(self, changedFilesList: list, potentialPackagesList: list) -> list:
  73. ''' Filter other cases that this package should be built
  74. based on changed files. This should cover things that can't
  75. be detected as dependencies. '''
  76. build_these_packages = []
  77. possible_packages = potentialPackagesList.copy()
  78. for f in changedFilesList:
  79. # BaseTools files that might change the build
  80. if "BaseTools" in f:
  81. if os.path.splitext(f) not in [".txt", ".md"]:
  82. build_these_packages = possible_packages
  83. break
  84. # if the azure pipeline platform template file changed
  85. if "platform-build-run-steps.yml" in f:
  86. build_these_packages = possible_packages
  87. break
  88. return build_these_packages
  89. def GetPlatformDscAndConfig(self) -> tuple:
  90. ''' If a platform desires to provide its DSC then Policy 4 will evaluate if
  91. any of the changes will be built in the dsc.
  92. The tuple should be (<workspace relative path to dsc file>, <input dictionary of dsc key value pairs>)
  93. '''
  94. dsc = CommonPlatform.GetDscName(",".join(self.ActualArchitectures))
  95. return (f"OvmfPkg/{dsc}", {})
  96. # ####################################################################################### #
  97. # Actual Configuration for Platform Build #
  98. # ####################################################################################### #
  99. class PlatformBuilder( UefiBuilder, BuildSettingsManager):
  100. def __init__(self):
  101. UefiBuilder.__init__(self)
  102. def AddCommandLineOptions(self, parserObj):
  103. ''' Add command line options to the argparser '''
  104. parserObj.add_argument('-a', "--arch", dest="build_arch", type=str, default="IA32,X64",
  105. help="Optional - CSV of architecture to build. IA32 will use IA32 for Pei & Dxe. "
  106. "X64 will use X64 for both PEI and DXE. IA32,X64 will use IA32 for PEI and "
  107. "X64 for DXE. default is IA32,X64")
  108. def RetrieveCommandLineOptions(self, args):
  109. ''' Retrieve command line options from the argparser '''
  110. shell_environment.GetBuildVars().SetValue("TARGET_ARCH"," ".join(args.build_arch.upper().split(",")), "From CmdLine")
  111. dsc = CommonPlatform.GetDscName(args.build_arch)
  112. shell_environment.GetBuildVars().SetValue("ACTIVE_PLATFORM", f"OvmfPkg/{dsc}", "From CmdLine")
  113. def GetWorkspaceRoot(self):
  114. ''' get WorkspacePath '''
  115. return CommonPlatform.WorkspaceRoot
  116. def GetPackagesPath(self):
  117. ''' Return a list of workspace relative paths that should be mapped as edk2 PackagesPath '''
  118. return ()
  119. def GetActiveScopes(self):
  120. ''' return tuple containing scopes that should be active for this process '''
  121. return CommonPlatform.Scopes
  122. def GetName(self):
  123. ''' Get the name of the repo, platform, or product being build '''
  124. ''' Used for naming the log file, among others '''
  125. # check the startup nsh flag and if set then rename the log file.
  126. # this helps in CI so we don't overwrite the build log since running
  127. # uses the stuart_build command.
  128. if(shell_environment.GetBuildVars().GetValue("MAKE_STARTUP_NSH", "FALSE") == "TRUE"):
  129. return "OvmfPkg_With_Run"
  130. return "OvmfPkg"
  131. def GetLoggingLevel(self, loggerType):
  132. ''' Get the logging level for a given type
  133. base == lowest logging level supported
  134. con == Screen logging
  135. txt == plain text file logging
  136. md == markdown file logging
  137. '''
  138. return logging.DEBUG
  139. def SetPlatformEnv(self):
  140. logging.debug("PlatformBuilder SetPlatformEnv")
  141. self.env.SetValue("PRODUCT_NAME", "OVMF", "Platform Hardcoded")
  142. self.env.SetValue("MAKE_STARTUP_NSH", "FALSE", "Default to false")
  143. self.env.SetValue("QEMU_HEADLESS", "FALSE", "Default to false")
  144. return 0
  145. def PlatformPreBuild(self):
  146. return 0
  147. def PlatformPostBuild(self):
  148. return 0
  149. def FlashRomImage(self):
  150. VirtualDrive = os.path.join(self.env.GetValue("BUILD_OUTPUT_BASE"), "VirtualDrive")
  151. os.makedirs(VirtualDrive, exist_ok=True)
  152. OutputPath_FV = os.path.join(self.env.GetValue("BUILD_OUTPUT_BASE"), "FV")
  153. if (self.env.GetValue("QEMU_SKIP") and
  154. self.env.GetValue("QEMU_SKIP").upper() == "TRUE"):
  155. logging.info("skipping qemu boot test")
  156. return 0
  157. #
  158. # QEMU must be on the path
  159. #
  160. cmd = "qemu-system-x86_64"
  161. args = "-debugcon stdio" # write messages to stdio
  162. args += " -global isa-debugcon.iobase=0x402" # debug messages out thru virtual io port
  163. args += " -net none" # turn off network
  164. args += f" -drive file=fat:rw:{VirtualDrive},format=raw,media=disk" # Mount disk with startup.nsh
  165. if (self.env.GetValue("QEMU_HEADLESS").upper() == "TRUE"):
  166. args += " -display none" # no graphics
  167. if (self.env.GetBuildValue("SMM_REQUIRE") == "1"):
  168. args += " -machine q35,smm=on" #,accel=(tcg|kvm)"
  169. #args += " -m ..."
  170. #args += " -smp ..."
  171. args += " -global driver=cfi.pflash01,property=secure,value=on"
  172. args += " -drive if=pflash,format=raw,unit=0,file=" + os.path.join(OutputPath_FV, "OVMF_CODE.fd") + ",readonly=on"
  173. args += " -drive if=pflash,format=raw,unit=1,file=" + os.path.join(OutputPath_FV, "OVMF_VARS.fd")
  174. else:
  175. args += " -pflash " + os.path.join(OutputPath_FV, "OVMF.fd") # path to firmware
  176. if (self.env.GetValue("MAKE_STARTUP_NSH").upper() == "TRUE"):
  177. f = open(os.path.join(VirtualDrive, "startup.nsh"), "w")
  178. f.write("BOOT SUCCESS !!! \n")
  179. ## add commands here
  180. f.write("reset -s\n")
  181. f.close()
  182. ret = RunCmd(cmd, args)
  183. if ret == 0xc0000005:
  184. #for some reason getting a c0000005 on successful return
  185. return 0
  186. return ret