ifwitool.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright 2022 Google LLC
  3. #
  4. """Bintool implementation for ifwitool
  5. ifwitool provides a way to package firmware in an Intel Firmware Image (IFWI)
  6. file on some Intel SoCs, e.g. Apolo Lake.
  7. Documentation is not really available so far as I can tell
  8. Source code is at tools/ifwitool.c which is a cleaned-up version of
  9. https://github.com/coreboot/coreboot/blob/master/util/cbfstool/ifwitool.c
  10. Here is the help:
  11. ifwitool: Utility for IFWI manipulation
  12. USAGE:
  13. /tmp/b/sandbox/tools/ifwitool [-h]
  14. /tmp/b/sandbox/tools/ifwitool FILE COMMAND [PARAMETERS]
  15. COMMANDs:
  16. add -f FILE -n NAME [-d -e ENTRY]
  17. create -f FILE
  18. delete -n NAME
  19. extract -f FILE -n NAME [-d -e ENTRY]
  20. print [-d]
  21. replace -f FILE -n NAME [-d -e ENTRY]
  22. OPTIONs:
  23. -f FILE : File to read/write/create/extract
  24. -d : Perform directory operation
  25. -e ENTRY: Name of directory entry to operate on
  26. -v : Verbose level
  27. -h : Help message
  28. -n NAME : Name of sub-partition to operate on
  29. NAME should be one of:
  30. SMIP(SMIP)
  31. RBEP(CSE_RBE)
  32. FTPR(CSE_BUP)
  33. UCOD(Microcode)
  34. IBBP(Bootblock)
  35. S_BPDT(S-BPDT)
  36. OBBP(OEM boot block)
  37. NFTP(CSE_MAIN)
  38. ISHP(ISH)
  39. DLMP(CSE_IDLM)
  40. IFP_OVERRIDE(IFP_OVERRIDE)
  41. DEBUG_TOKENS(Debug Tokens)
  42. UFS_PHY(UFS Phy)
  43. UFS_GPP(UFS GPP)
  44. PMCP(PMC firmware)
  45. IUNP(IUNIT)
  46. NVM_CONFIG(NVM Config)
  47. UEP(UEP)
  48. UFS_RATE_B(UFS Rate B Config)
  49. """
  50. from binman import bintool
  51. class Bintoolifwitool(bintool.Bintool):
  52. """Handles the 'ifwitool' tool
  53. This bintool supports running `ifwitool` with some basic parameters as
  54. neeed by binman. It includes creating a file from a FIT as well as adding,
  55. replacing, deleting and extracting subparts.
  56. The tool is built as part of U-Boot, but a binary version can be fetched if
  57. required.
  58. ifwitool provides a way to package firmware in an Intel Firmware Image
  59. (IFWI) file on some Intel SoCs, e.g. Apolo Lake.
  60. """
  61. def __init__(self, name):
  62. super().__init__(name, 'Manipulate Intel IFWI files')
  63. def create_ifwi(self, intel_fit, ifwi_file):
  64. """Create a new IFWI file, using an existing Intel FIT binary
  65. Args:
  66. intel_fit (str): Filename of exist Intel FIT file
  67. ifwi_file (str): Output filename to write the new IFWI too
  68. Returns:
  69. str: Tool output
  70. """
  71. args = [intel_fit, 'create', '-f', ifwi_file]
  72. return self.run_cmd(*args)
  73. def delete_subpart(self, ifwi_file, subpart):
  74. """Delete a subpart within the IFWI file
  75. Args:
  76. ifwi_file (str): IFWI filename to update
  77. subpart (str): Name of subpart to delete, e.g. 'OBBP'
  78. Returns:
  79. str: Tool output
  80. """
  81. args = [ifwi_file, 'delete', '-n', subpart]
  82. return self.run_cmd(*args)
  83. # pylint: disable=R0913
  84. def add_subpart(self, ifwi_file, subpart, entry_name, infile,
  85. replace=False):
  86. """Add or replace a subpart within the IFWI file
  87. Args:
  88. ifwi_file (str): IFWI filename to update
  89. subpart (str): Name of subpart to add/replace
  90. entry_nme (str): Name of entry to add/replace
  91. replace (bool): True to replace the existing entry, False to add a
  92. new one
  93. Returns:
  94. str: Tool output
  95. """
  96. args = [
  97. ifwi_file,
  98. 'replace' if replace else 'add',
  99. '-n', subpart,
  100. '-d', '-e', entry_name,
  101. '-f', infile,
  102. ]
  103. return self.run_cmd(*args)
  104. def extract(self, ifwi_file, subpart, entry_name, outfile):
  105. """Extract a subpart from the IFWI file
  106. Args:
  107. ifwi_file (str): IFWI filename to extract from
  108. subpart (str): Name of subpart to extract
  109. entry_nme (str): Name of entry to extract
  110. Returns:
  111. str: Tool output
  112. """
  113. args = [
  114. ifwi_file,
  115. 'extract',
  116. '-n', subpart,
  117. '-d', '-e', entry_name,
  118. '-f', outfile,
  119. ]
  120. return self.run_cmd(*args)
  121. def fetch(self, method):
  122. """Fetch handler for ifwitool
  123. This installs ifwitool using a binary download.
  124. Args:
  125. method (FETCH_...): Method to use
  126. Returns:
  127. True if the file was fetched, None if a method other than FETCH_BIN
  128. was requested
  129. Raises:
  130. Valuerror: Fetching could not be completed
  131. """
  132. if method != bintool.FETCH_BIN:
  133. return None
  134. fname, tmpdir = self.fetch_from_drive(
  135. '18JDghOxlt2Hcc5jv51O1t6uNVHQ0XKJS')
  136. return fname, tmpdir