sp_mk_generator.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/python3
  2. # Copyright (c) 2020-2022, Arm Limited. All rights reserved.
  3. #
  4. # SPDX-License-Identifier: BSD-3-Clause
  5. """
  6. This script is invoked by Make system and generates secure partition makefile.
  7. It expects platform provided secure partition layout file which contains list
  8. of Secure Partition Images and Partition manifests(PM).
  9. Layout file can exist outside of TF-A tree and the paths of Image and PM files
  10. must be relative to it.
  11. This script parses the layout file and generates a make file which updates
  12. FDT_SOURCES, FIP_ARGS, CRT_ARGS and SPTOOL_ARGS which are used in later build
  13. steps.
  14. If the SP entry in the layout file has a "uuid" field the scripts gets the UUID
  15. from there, otherwise it parses the associated partition manifest and extracts
  16. the UUID from there.
  17. param1: Generated mk file "sp_gen.mk"
  18. param2: "SP_LAYOUT_FILE", json file containing platform provided information
  19. param3: plat out directory
  20. param4: CoT parameter
  21. Generated "sp_gen.mk" file contains triplet of following information for each
  22. Secure Partition entry
  23. FDT_SOURCES += sp1.dts
  24. SPTOOL_ARGS += -i sp1.bin:sp1.dtb -o sp1.pkg
  25. FIP_ARGS += --blob uuid=XXXXX-XXX...,file=sp1.pkg
  26. CRT_ARGS += --sp-pkg1 sp1.pkg
  27. A typical SP_LAYOUT_FILE file will look like
  28. {
  29. "SP1" : {
  30. "image": "sp1.bin",
  31. "pm": "test/sp1.dts"
  32. },
  33. "SP2" : {
  34. "image": "sp2.bin",
  35. "pm": "test/sp2.dts",
  36. "uuid": "1b1820fe-48f7-4175-8999-d51da00b7c9f"
  37. }
  38. ...
  39. }
  40. """
  41. import json
  42. import os
  43. import re
  44. import sys
  45. import uuid
  46. from spactions import SpSetupActions
  47. MAX_SP = 8
  48. UUID_LEN = 4
  49. # Some helper functions to access args propagated to the action functions in
  50. # SpSetupActions framework.
  51. def check_sp_mk_gen(args :dict):
  52. if "sp_gen_mk" not in args.keys():
  53. raise Exception(f"Path to file sp_gen.mk needs to be in 'args'.")
  54. def check_out_dir(args :dict):
  55. if "out_dir" not in args.keys() or not os.path.isdir(args["out_dir"]):
  56. raise Exception("Define output folder with \'out_dir\' key.")
  57. def check_sp_layout_dir(args :dict):
  58. if "sp_layout_dir" not in args.keys() or not os.path.isdir(args["sp_layout_dir"]):
  59. raise Exception("Define output folder with \'sp_layout_dir\' key.")
  60. def write_to_sp_mk_gen(content, args :dict):
  61. check_sp_mk_gen(args)
  62. with open(args["sp_gen_mk"], "a") as f:
  63. f.write(f"{content}\n")
  64. def get_sp_manifest_full_path(sp_node, args :dict):
  65. check_sp_layout_dir(args)
  66. return os.path.join(args["sp_layout_dir"], get_file_from_layout(sp_node["pm"]))
  67. def get_sp_img_full_path(sp_node, args :dict):
  68. check_sp_layout_dir(args)
  69. return os.path.join(args["sp_layout_dir"], get_file_from_layout(sp_node["image"]))
  70. def get_sp_pkg(sp, args :dict):
  71. check_out_dir(args)
  72. return os.path.join(args["out_dir"], f"{sp}.pkg")
  73. def is_line_in_sp_gen(line, args :dict):
  74. with open(args["sp_gen_mk"], "r") as f:
  75. sppkg_rule = [l for l in f if line in l]
  76. return len(sppkg_rule) != 0
  77. def get_file_from_layout(node):
  78. ''' Helper to fetch a file path from sp_layout.json. '''
  79. if type(node) is dict and "file" in node.keys():
  80. return node["file"]
  81. return node
  82. def get_offset_from_layout(node):
  83. ''' Helper to fetch an offset from sp_layout.json. '''
  84. if type(node) is dict and "offset" in node.keys():
  85. return int(node["offset"], 0)
  86. return None
  87. def get_image_offset(node):
  88. ''' Helper to fetch image offset from sp_layout.json '''
  89. return get_offset_from_layout(node["image"])
  90. def get_pm_offset(node):
  91. ''' Helper to fetch pm offset from sp_layout.json '''
  92. return get_offset_from_layout(node["pm"])
  93. @SpSetupActions.sp_action(global_action=True)
  94. def check_max_sps(sp_layout, _, args :dict):
  95. ''' Check validate the maximum number of SPs is respected. '''
  96. if len(sp_layout.keys()) > MAX_SP:
  97. raise Exception(f"Too many SPs in SP layout file. Max: {MAX_SP}")
  98. return args
  99. @SpSetupActions.sp_action
  100. def gen_fdt_sources(sp_layout, sp, args :dict):
  101. ''' Generate FDT_SOURCES values for a given SP. '''
  102. manifest_path = get_sp_manifest_full_path(sp_layout[sp], args)
  103. write_to_sp_mk_gen(f"FDT_SOURCES += {manifest_path}", args)
  104. return args
  105. @SpSetupActions.sp_action
  106. def gen_sptool_args(sp_layout, sp, args :dict):
  107. ''' Generate Sp Pkgs rules. '''
  108. sp_pkg = get_sp_pkg(sp, args)
  109. sp_dtb_name = os.path.basename(get_file_from_layout(sp_layout[sp]["pm"]))[:-1] + "b"
  110. sp_dtb = os.path.join(args["out_dir"], f"fdts/{sp_dtb_name}")
  111. sp_img = get_sp_img_full_path(sp_layout[sp], args)
  112. # Do not generate rule if already there.
  113. if is_line_in_sp_gen(f'{sp_pkg}:', args):
  114. return args
  115. write_to_sp_mk_gen(f"SP_PKGS += {sp_pkg}\n", args)
  116. sptool_args = f" -i {sp_img}:{sp_dtb}"
  117. pm_offset = get_pm_offset(sp_layout[sp])
  118. sptool_args += f" --pm-offset {pm_offset}" if pm_offset is not None else ""
  119. image_offset = get_image_offset(sp_layout[sp])
  120. sptool_args += f" --img-offset {image_offset}" if image_offset is not None else ""
  121. sptool_args += f" -o {sp_pkg}"
  122. sppkg_rule = f'''
  123. {sp_pkg}: {sp_dtb} {sp_img}
  124. \t$(Q)echo Generating {sp_pkg}
  125. \t$(Q)$(PYTHON) $(SPTOOL) {sptool_args}
  126. '''
  127. write_to_sp_mk_gen(sppkg_rule, args)
  128. return args
  129. @SpSetupActions.sp_action(global_action=True, exec_order=1)
  130. def check_dualroot(sp_layout, _, args :dict):
  131. ''' Validate the amount of SPs from SiP and Platform owners. '''
  132. if not args.get("dualroot"):
  133. return args
  134. args["split"] = int(MAX_SP / 2)
  135. owners = [sp_layout[sp].get("owner") for sp in sp_layout]
  136. args["plat_max_count"] = owners.count("Plat")
  137. # If it is owned by the platform owner, it is assigned to the SiP.
  138. args["sip_max_count"] = len(sp_layout.keys()) - args["plat_max_count"]
  139. if args["sip_max_count"] > args["split"] or args["sip_max_count"] > args["split"]:
  140. print(f"WARN: SiP Secure Partitions should not be more than {args['split']}")
  141. # Counters for gen_crt_args.
  142. args["sip_count"] = 1
  143. args["plat_count"] = 1
  144. return args
  145. @SpSetupActions.sp_action
  146. def gen_crt_args(sp_layout, sp, args :dict):
  147. ''' Append CRT_ARGS. '''
  148. # If "dualroot" is configured, 'sp_pkg_idx' depends on whether the SP is owned
  149. # by the "SiP" or the "Plat".
  150. if args.get("dualroot"):
  151. # If the owner is not specified as "Plat", default to "SiP".
  152. if sp_layout[sp].get("owner") == "Plat":
  153. if args["plat_count"] > args["plat_max_count"]:
  154. raise ValueError("plat_count can't surpass plat_max_count in args.")
  155. sp_pkg_idx = args["plat_count"] + args["split"]
  156. args["plat_count"] += 1
  157. else:
  158. if args["sip_count"] > args["sip_max_count"]:
  159. raise ValueError("sip_count can't surpass sip_max_count in args.")
  160. sp_pkg_idx = args["sip_count"]
  161. args["sip_count"] += 1
  162. else:
  163. sp_pkg_idx = [k for k in sp_layout.keys()].index(sp) + 1
  164. write_to_sp_mk_gen(f"CRT_ARGS += --sp-pkg{sp_pkg_idx} {get_sp_pkg(sp, args)}\n", args)
  165. return args
  166. @SpSetupActions.sp_action
  167. def gen_fiptool_args(sp_layout, sp, args :dict):
  168. ''' Generate arguments for the FIP Tool. '''
  169. if "uuid" in sp_layout[sp]:
  170. # Extract the UUID from the JSON file if the SP entry has a 'uuid' field
  171. uuid_std = uuid.UUID(sp_layout[sp]['uuid'])
  172. else:
  173. with open(get_sp_manifest_full_path(sp_layout[sp], args), "r") as pm_f:
  174. uuid_lines = [l for l in pm_f if 'uuid' in l]
  175. assert(len(uuid_lines) == 1)
  176. # The uuid field in SP manifest is the little endian representation
  177. # mapped to arguments as described in SMCCC section 5.3.
  178. # Convert each unsigned integer value to a big endian representation
  179. # required by fiptool.
  180. uuid_parsed = re.findall("0x([0-9a-f]+)", uuid_lines[0])
  181. y = list(map(bytearray.fromhex, uuid_parsed))
  182. z = [int.from_bytes(i, byteorder='little', signed=False) for i in y]
  183. uuid_std = uuid.UUID(f'{z[0]:08x}{z[1]:08x}{z[2]:08x}{z[3]:08x}')
  184. write_to_sp_mk_gen(f"FIP_ARGS += --blob uuid={str(uuid_std)},file={get_sp_pkg(sp, args)}\n", args)
  185. return args
  186. def init_sp_actions(sys):
  187. sp_layout_file = os.path.abspath(sys.argv[2])
  188. with open(sp_layout_file) as json_file:
  189. sp_layout = json.load(json_file)
  190. # Initialize arguments for the SP actions framework
  191. args = {}
  192. args["sp_gen_mk"] = os.path.abspath(sys.argv[1])
  193. args["sp_layout_dir"] = os.path.dirname(sp_layout_file)
  194. args["out_dir"] = os.path.abspath(sys.argv[3])
  195. args["dualroot"] = sys.argv[4] == "dualroot"
  196. #Clear content of file "sp_gen.mk".
  197. with open(args["sp_gen_mk"], "w"):
  198. None
  199. return args, sp_layout
  200. if __name__ == "__main__":
  201. args, sp_layout = init_sp_actions(sys)
  202. SpSetupActions.run_actions(sp_layout, args)