blob: 2714c04d968d0dad376c7c374dc6b33099c42683 [file] [log] [blame]
Drew Macrae7ac1efb2021-09-22 16:08:28 +00001# Copyright lowRISC contributors.
2# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3# SPDX-License-Identifier: Apache-2.0
4
Alphan Ulusoy96725992022-06-27 14:56:59 -04005load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES")
Miguel Young de la Sota4b01c7a2022-04-21 17:25:52 -04006load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain")
Miguel Young de la Sota7bb7fe72022-04-01 16:40:02 -04007load(
Yen-Kai Wangf3252d32022-06-08 12:33:58 -05008 "@lowrisc_opentitan//rules:cc_side_outputs.bzl",
Miguel Young de la Sota7bb7fe72022-04-01 16:40:02 -04009 "rv_asm",
10 "rv_llvm_ir",
11 "rv_preprocess",
12 "rv_relink_with_linkmap",
13)
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -040014load(
Yen-Kai Wangf3252d32022-06-08 12:33:58 -050015 "@lowrisc_opentitan//rules:rv.bzl",
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -040016 "rv_rule",
17 _OPENTITAN_CPU = "OPENTITAN_CPU",
18 _OPENTITAN_PLATFORM = "OPENTITAN_PLATFORM",
19 _opentitan_transition = "opentitan_transition",
20)
Drew Macrae7ac1efb2021-09-22 16:08:28 +000021
Timothy Trippel3821d5d2022-08-02 10:20:49 -070022"""Rules to build OpenTitan for the RISC-V target"""
Drew Macrae7ac1efb2021-09-22 16:08:28 +000023
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -040024# Re-exports of names from transition.bzl; many files in the repo use opentitan.bzl
25# to get to them.
26OPENTITAN_CPU = _OPENTITAN_CPU
27OPENTITAN_PLATFORM = _OPENTITAN_PLATFORM
28opentitan_transition = _opentitan_transition
Drew Macrae7ac1efb2021-09-22 16:08:28 +000029
30_targets_compatible_with = {
31 OPENTITAN_PLATFORM: [OPENTITAN_CPU],
32}
33
Timothy Trippele3a7c572022-02-17 17:30:47 -080034# This constant holds a dictionary of per-device dependencies which are used to
35# generate slightly different binaries for each hardware target, including two
Pirmin Vogel9fc1ec92023-02-16 19:18:42 +010036# simulation platforms (DV and Verilator), and two FPGA platforms (CW305
Timothy Trippele3a7c572022-02-17 17:30:47 -080037# and CW310).
38PER_DEVICE_DEPS = {
Timothy Trippeld2277d22022-06-02 00:36:08 -070039 "sim_verilator": ["@//sw/device/lib/arch:sim_verilator"],
40 "sim_dv": ["@//sw/device/lib/arch:sim_dv"],
Pirmin Vogel9fc1ec92023-02-16 19:18:42 +010041 "fpga_cw305": ["@//sw/device/lib/arch:fpga_cw305"],
Timothy Trippeld2277d22022-06-02 00:36:08 -070042 "fpga_cw310": ["@//sw/device/lib/arch:fpga_cw310"],
Timothy Trippele3a7c572022-02-17 17:30:47 -080043}
44
Timothy Trippelfcc8bac2022-08-23 17:20:24 -070045# Default keys used to sign ROM_EXT and BL0 images for testing.
Timothy Trippel1ad4ad62022-08-23 17:01:28 -070046DEFAULT_SIGNING_KEYS = {
Alphan Ulusoy8f865282022-11-14 15:40:43 -050047 "fake_test_key_0": "@//sw/device/silicon_creator/rom/keys/fake:test_private_key_0",
48 "fake_dev_key_0": "@//sw/device/silicon_creator/rom/keys/fake:dev_private_key_0",
49 "fake_prod_key_0": "@//sw/device/silicon_creator/rom/keys/fake:prod_private_key_0",
Alphan Ulusoy3cdeca42022-09-29 13:04:16 -040050 "unauthorized_0": "@//sw/device/silicon_creator/rom/keys:unauthorized_private_key_0",
Miguel Osorio38503db2023-02-17 20:19:52 -080051 "fake_rom_ext_test_key_0": "@//sw/device/silicon_creator/rom_ext/keys/fake:rom_ext_test_private_key_0",
52 "fake_rom_ext_dev_key_0": "@//sw/device/silicon_creator/rom_ext/keys/fake:rom_ext_dev_private_key_0",
Timothy Trippel1ad4ad62022-08-23 17:01:28 -070053}
54
Timothy Trippele3a7c572022-02-17 17:30:47 -080055def _obj_transform_impl(ctx):
Miguel Young de la Sota4b01c7a2022-04-21 17:25:52 -040056 cc_toolchain = find_cc_toolchain(ctx).cc
Drew Macrae7ac1efb2021-09-22 16:08:28 +000057 outputs = []
58 for src in ctx.files.srcs:
Timothy Trippel36f96542022-08-02 18:31:20 -070059 binary = ctx.actions.declare_file(
60 "{}.{}".format(
61 src.basename.replace("." + src.extension, ""),
62 ctx.attr.suffix,
63 ),
64 )
Drew Macrae7ac1efb2021-09-22 16:08:28 +000065 outputs.append(binary)
66 ctx.actions.run(
67 outputs = [binary],
68 inputs = [src] + cc_toolchain.all_files.to_list(),
69 arguments = [
70 "--output-target",
71 ctx.attr.format,
72 src.path,
73 binary.path,
74 ],
75 executable = cc_toolchain.objcopy_executable,
76 )
77 return [DefaultInfo(files = depset(outputs), data_runfiles = ctx.runfiles(files = outputs))]
78
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -040079obj_transform = rv_rule(
Timothy Trippele3a7c572022-02-17 17:30:47 -080080 implementation = _obj_transform_impl,
Drew Macrae7ac1efb2021-09-22 16:08:28 +000081 attrs = {
82 "srcs": attr.label_list(allow_files = True),
83 "suffix": attr.string(default = "bin"),
84 "format": attr.string(default = "binary"),
Drew Macrae7ac1efb2021-09-22 16:08:28 +000085 "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
Drew Macrae7ac1efb2021-09-22 16:08:28 +000086 },
87 toolchains = ["@rules_cc//cc:toolchain_type"],
88)
89
Alphan Ulusoy96725992022-06-27 14:56:59 -040090# A provider for device-specific archive files that hold binaries of SRAM programs.
91ArchiveInfo = provider(fields = ["archive_infos"])
92
93def _bin_to_archive_impl(ctx):
94 cc_infos = []
95 cc_toolchain = find_cc_toolchain(ctx).cc
96 cc_info_dict = {}
97 num_devices = len(ctx.attr.devices)
98 num_binaries = len(ctx.attr.binaries)
99 if num_devices != num_binaries:
100 fail("Number of devices", num_devices, "must be equal to number of binaries", num_binaries)
101 for (device, binary_target) in zip(ctx.attr.devices, ctx.attr.binaries):
102 devname = "{}_{}".format(ctx.attr.name, device)
103 binary_file = binary_target.files.to_list()[0]
104 object_file = ctx.actions.declare_file("{}.o".format(devname))
105 renamed_object_file = ctx.actions.declare_file("{}.renamed.o".format(devname))
106 archive_file = ctx.actions.declare_file("{}.a".format(devname))
107
108 # Create a CcInfo to be able to use this rule as a dependency in other rules.
109 # See https://bazel.build/docs/integrating-with-rules-cc.
110 feature_configuration = cc_common.configure_features(
111 ctx = ctx,
112 cc_toolchain = cc_toolchain,
113 requested_features = ctx.features,
114 unsupported_features = ctx.disabled_features,
115 )
116 action_name = ACTION_NAMES.cpp_link_executable
117 c_linker_path = cc_common.get_tool_for_action(
118 feature_configuration = feature_configuration,
119 action_name = action_name,
120 )
121 c_link_variables = cc_common.create_link_variables(
122 feature_configuration = feature_configuration,
123 cc_toolchain = cc_toolchain,
124 output_file = object_file.path,
125 )
126 command_line = cc_common.get_memory_inefficient_command_line(
127 feature_configuration = feature_configuration,
128 action_name = action_name,
129 variables = c_link_variables,
130 )
131 env = cc_common.get_environment_variables(
132 feature_configuration = feature_configuration,
133 action_name = action_name,
134 variables = c_link_variables,
135 )
136 linker_path = cc_common.get_tool_for_action(
137 feature_configuration = feature_configuration,
138 action_name = action_name,
139 )
140
141 # Create an object file that contains the binary.
142 ctx.actions.run(
143 executable = cc_toolchain.ld_executable,
144 arguments = [
145 "-r",
146 "-b",
147 "binary",
148 "-o",
149 object_file.path,
150 binary_file.path,
151 ],
152 use_default_shell_env = False,
153 env = env,
154 inputs = depset(
155 direct = [binary_file],
156 transitive = [cc_toolchain.all_files],
157 ),
158 outputs = [object_file],
159 mnemonic = "CppLink",
160 )
161
162 # Rename symbols to make them more manageable.
163 sym_prefix = "_binary_" + binary_file.path.replace(".", "_").replace("/", "_").replace("-", "_")
164 suffixes = ["start", "end", "size"]
165 rename_args = []
166 for suffix in suffixes:
167 old_name = "{}_{}".format(sym_prefix, suffix)
168 new_name = "_{}_{}".format(ctx.attr.archive_symbol_prefix, suffix)
169 rename_args.extend(["--redefine-sym", "{}={}".format(old_name, new_name)])
170 rename_args.extend(["--rename-section", ".data=.data.sram_program"])
171 rename_args.extend([object_file.path, renamed_object_file.path])
172 ctx.actions.run(
173 executable = cc_toolchain.objcopy_executable,
174 arguments = rename_args,
175 use_default_shell_env = False,
176 env = env,
177 inputs = depset(
178 direct = [object_file],
179 transitive = [cc_toolchain.all_files],
180 ),
181 outputs = [renamed_object_file],
182 mnemonic = "RenameSymbols",
183 )
184
185 # Create an archive that contains the object file.
186 ctx.actions.run(
187 executable = cc_toolchain.ar_executable,
188 arguments = [
189 "r",
190 archive_file.path,
191 renamed_object_file.path,
192 ],
193 use_default_shell_env = False,
194 env = env,
195 inputs = depset(
196 direct = [renamed_object_file],
197 transitive = [cc_toolchain.all_files],
198 ),
199 outputs = [archive_file],
200 mnemonic = "Archive",
201 )
202
203 cc_info_dict[device] = CcInfo(
204 compilation_context = cc_common.create_compilation_context(
205 headers = depset(direct = ctx.attr.hdrs[0].files.to_list()),
206 ),
207 linking_context = cc_common.create_linking_context(
208 linker_inputs = depset([cc_common.create_linker_input(
209 owner = ctx.label,
210 libraries = depset([cc_common.create_library_to_link(
211 actions = ctx.actions,
212 feature_configuration = feature_configuration,
213 cc_toolchain = cc_toolchain,
214 static_library = archive_file,
215 )]),
216 )]),
217 ),
218 )
219
220 return ArchiveInfo(archive_infos = cc_info_dict)
221
222bin_to_archive = rv_rule(
223 implementation = _bin_to_archive_impl,
224 attrs = {
225 "binaries": attr.label_list(allow_files = True),
226 "devices": attr.string_list(),
227 "hdrs": attr.label_list(allow_files = True),
228 "archive_symbol_prefix": attr.string(),
229 "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
230 },
231 fragments = ["cpp"],
232 toolchains = ["@rules_cc//cc:toolchain_type"],
233)
234
Timothy Trippele3a7c572022-02-17 17:30:47 -0800235def _sign_bin_impl(ctx):
Timothy Trippel92173aa2022-02-15 23:41:41 -0800236 signed_image = ctx.actions.declare_file(
237 "{0}.{1}.signed.bin".format(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800238 # Remove ".bin" from file basename.
239 ctx.file.bin.basename.replace("." + ctx.file.bin.extension, ""),
Timothy Trippel92173aa2022-02-15 23:41:41 -0800240 ctx.attr.key_name,
241 ),
242 )
Chris Frantzbac04542022-06-16 09:57:29 -0700243 outputs = [signed_image]
244
245 inputs = [
246 ctx.file.bin,
247 ctx.file.key,
Chris Frantz40ac0e72022-09-21 11:40:11 -0700248 ctx.file._opentitantool,
Chris Frantzbac04542022-06-16 09:57:29 -0700249 ]
250 manifest = []
251 if ctx.file.manifest:
252 manifest = ["--manifest={}".format(ctx.file.manifest.path)]
253 inputs.append(ctx.file.manifest)
254
Timothy Trippel92173aa2022-02-15 23:41:41 -0800255 ctx.actions.run(
Chris Frantzbac04542022-06-16 09:57:29 -0700256 outputs = outputs,
257 inputs = inputs,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800258 arguments = [
Chris Frantz40ac0e72022-09-21 11:40:11 -0700259 "--rcfile=",
Jon Flatley26e774c2022-06-02 17:29:00 -0400260 "image",
Chris Frantz47dc84b2022-06-16 16:38:08 -0700261 "manifest",
262 "update",
263 "--key-file={}".format(ctx.file.key.path),
264 "--sign",
265 "--output={}".format(signed_image.path),
Timothy Trippel92173aa2022-02-15 23:41:41 -0800266 ctx.file.bin.path,
Chris Frantzbac04542022-06-16 09:57:29 -0700267 ] + manifest,
Chris Frantz40ac0e72022-09-21 11:40:11 -0700268 executable = ctx.file._opentitantool.path,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800269 )
270 return [DefaultInfo(
271 files = depset(outputs),
272 data_runfiles = ctx.runfiles(files = outputs),
273 )]
274
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -0400275sign_bin = rv_rule(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800276 implementation = _sign_bin_impl,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800277 attrs = {
278 "bin": attr.label(allow_single_file = True),
Timothy Trippel92173aa2022-02-15 23:41:41 -0800279 "key": attr.label(
Alphan Ulusoy776d2ab2022-08-13 16:47:14 -0400280 default = "@//sw/device/silicon_creator/rom/keys:test_private_key_0",
Timothy Trippel92173aa2022-02-15 23:41:41 -0800281 allow_single_file = True,
282 ),
283 "key_name": attr.string(),
Chris Frantzbac04542022-06-16 09:57:29 -0700284 "manifest": attr.label(allow_single_file = True),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800285 # TODO(lowRISC/opentitan:#11199): explore other options to side-step the
286 # need for this transition, in order to build the ROM_EXT signer tool.
Timothy Trippel92173aa2022-02-15 23:41:41 -0800287 "platform": attr.string(default = "@local_config_platform//:host"),
Chris Frantz40ac0e72022-09-21 11:40:11 -0700288 "_opentitantool": attr.label(
Jon Flatley26e774c2022-06-02 17:29:00 -0400289 default = "//sw/host/opentitantool:opentitantool",
Timothy Trippel92173aa2022-02-15 23:41:41 -0800290 allow_single_file = True,
291 ),
Timothy Trippel92173aa2022-02-15 23:41:41 -0800292 },
293)
294
Timothy Trippele3a7c572022-02-17 17:30:47 -0800295def _elf_to_disassembly_impl(ctx):
Miguel Young de la Sota4b01c7a2022-04-21 17:25:52 -0400296 cc_toolchain = find_cc_toolchain(ctx).cc
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000297 outputs = []
298 for src in ctx.files.srcs:
Timothy Trippel36f96542022-08-02 18:31:20 -0700299 disassembly = ctx.actions.declare_file(
300 "{}.dis".format(
301 src.basename.replace("." + src.extension, ""),
302 ),
303 )
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000304 outputs.append(disassembly)
305 ctx.actions.run_shell(
Miguel Young de la Sota9fd48832022-03-25 17:35:06 -0400306 tools = [ctx.file._cleanup_script],
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000307 outputs = [disassembly],
308 inputs = [src] + cc_toolchain.all_files.to_list(),
309 arguments = [
310 cc_toolchain.objdump_executable,
311 src.path,
Miguel Young de la Sota9fd48832022-03-25 17:35:06 -0400312 ctx.file._cleanup_script.path,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000313 disassembly.path,
314 ],
Alphan Ulusoy883d3a92022-06-09 16:42:08 -0400315 execution_requirements = {
316 "no-sandbox": "1",
317 },
Alphan Ulusoy6efbeae2022-09-13 15:25:03 -0400318 command = "$1 -wx --disassemble --headers --line-numbers --disassemble-zeroes --source --visualize-jumps $2 | $3 > $4",
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000319 )
Miguel Young de la Sota9fd48832022-03-25 17:35:06 -0400320 return [DefaultInfo(files = depset(outputs), data_runfiles = ctx.runfiles(files = outputs))]
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000321
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -0400322elf_to_disassembly = rv_rule(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800323 implementation = _elf_to_disassembly_impl,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000324 attrs = {
325 "srcs": attr.label_list(allow_files = True),
326 "platform": attr.string(default = OPENTITAN_PLATFORM),
Miguel Young de la Sota9fd48832022-03-25 17:35:06 -0400327 "_cleanup_script": attr.label(
328 allow_single_file = True,
Timothy Trippeld2277d22022-06-02 00:36:08 -0700329 default = Label("@//rules/scripts:expand_tabs.sh"),
Miguel Young de la Sota9fd48832022-03-25 17:35:06 -0400330 ),
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -0400331 "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000332 },
333 toolchains = ["@rules_cc//cc:toolchain_type"],
334 incompatible_use_toolchain_transition = True,
335)
336
Timothy Trippele3a7c572022-02-17 17:30:47 -0800337def _elf_to_scrambled_rom_impl(ctx):
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000338 outputs = []
339 for src in ctx.files.srcs:
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700340 if src.extension != "elf":
341 fail("only ROM images in the ELF format may be converted to the VMEM format and scrambled.")
Timothy Trippele3a7c572022-02-17 17:30:47 -0800342 scrambled = ctx.actions.declare_file(
Timothy Trippeld69bcd72022-08-12 08:20:40 -0700343 "{}.39.scr.vmem".format(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800344 # Remove ".elf" from file basename.
345 src.basename.replace("." + src.extension, ""),
346 ),
347 )
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000348 outputs.append(scrambled)
349 ctx.actions.run(
350 outputs = [scrambled],
351 inputs = [
352 src,
Timothy Trippeldea42da2022-04-15 14:30:01 -0700353 ctx.executable._scramble_tool,
354 ctx.file._config,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000355 ],
356 arguments = [
Timothy Trippeldea42da2022-04-15 14:30:01 -0700357 ctx.file._config.path,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000358 src.path,
359 scrambled.path,
360 ],
Timothy Trippeldea42da2022-04-15 14:30:01 -0700361 executable = ctx.executable._scramble_tool,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000362 )
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700363 return [DefaultInfo(
364 files = depset(outputs),
365 data_runfiles = ctx.runfiles(files = outputs),
366 )]
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000367
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -0400368elf_to_scrambled_rom_vmem = rv_rule(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800369 implementation = _elf_to_scrambled_rom_impl,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000370 attrs = {
371 "srcs": attr.label_list(allow_files = True),
Timothy Trippeldea42da2022-04-15 14:30:01 -0700372 "_scramble_tool": attr.label(
Timothy Trippeld2277d22022-06-02 00:36:08 -0700373 default = "@//hw/ip/rom_ctrl/util:scramble_image",
Timothy Trippeldea42da2022-04-15 14:30:01 -0700374 executable = True,
375 cfg = "exec",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800376 ),
377 "_config": attr.label(
Timothy Trippeld2277d22022-06-02 00:36:08 -0700378 default = "@//hw/top_earlgrey/data:autogen/top_earlgrey.gen.hjson",
Timothy Trippeldea42da2022-04-15 14:30:01 -0700379 allow_single_file = True,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800380 ),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800381 },
382)
383
Miles Dai1634fac2022-05-13 17:55:35 -0400384def _bin_to_vmem_impl(ctx):
Timothy Trippele3a7c572022-02-17 17:30:47 -0800385 outputs = []
386 vmem = ctx.actions.declare_file("{}.{}.vmem".format(
387 # Remove ".bin" from file basename.
Timothy Trippelf17bca82022-03-08 11:12:41 -0800388 ctx.file.bin.basename.replace("." + ctx.file.bin.extension, ""),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800389 ctx.attr.word_size,
390 ))
391 outputs.append(vmem)
392 ctx.actions.run(
393 outputs = [vmem],
394 inputs = [
395 ctx.file.bin,
396 ],
397 arguments = [
398 ctx.file.bin.path,
399 "--binary",
400 # Reverse the endianness of every word.
401 "--offset",
402 "0x0",
403 "--byte-swap",
404 str(ctx.attr.word_size // 8),
405 # Pad to word alignment.
406 "--fill",
407 "0xff",
408 "-within",
409 ctx.file.bin.path,
410 "-binary",
411 "-range-pad",
412 str(ctx.attr.word_size // 8),
413 # Output a VMEM file with specified word size
414 "--output",
415 vmem.path,
416 "--vmem",
417 str(ctx.attr.word_size),
418 ],
419 # This this executable is expected to be installed (as required by the
420 # srecord package in apt-requirements.txt).
421 executable = "srec_cat",
Martin Lueker-Bodenc8a7ff72022-09-08 17:35:29 -0700422 use_default_shell_env = True,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800423 )
424 return [DefaultInfo(
425 files = depset(outputs),
426 data_runfiles = ctx.runfiles(files = outputs),
427 )]
428
Miles Dai1634fac2022-05-13 17:55:35 -0400429bin_to_vmem = rv_rule(
430 implementation = _bin_to_vmem_impl,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800431 attrs = {
432 "bin": attr.label(allow_single_file = True),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800433 "word_size": attr.int(
434 default = 64,
435 doc = "Word size of VMEM file.",
436 mandatory = True,
437 values = [32, 64],
438 ),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800439 },
440)
441
442def _scramble_flash_vmem_impl(ctx):
443 outputs = []
444 scrambled_vmem = ctx.actions.declare_file("{}.scr.vmem".format(
445 # Remove ".vmem" from file basename.
Timothy Trippelf17bca82022-03-08 11:12:41 -0800446 ctx.file.vmem.basename.replace("." + ctx.file.vmem.extension, ""),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800447 ))
448 outputs.append(scrambled_vmem)
449 ctx.actions.run(
450 outputs = [scrambled_vmem],
451 inputs = [
452 ctx.file.vmem,
Timothy Trippel4a903632022-04-15 15:04:39 -0700453 ctx.executable._tool,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800454 ],
455 arguments = [
Timothy Trippeldb439f52023-03-06 10:42:16 -0800456 "--infile",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800457 ctx.file.vmem.path,
Timothy Trippeldb439f52023-03-06 10:42:16 -0800458 "--outfile",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800459 scrambled_vmem.path,
460 ],
Timothy Trippel4a903632022-04-15 15:04:39 -0700461 executable = ctx.executable._tool,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800462 )
463 return [DefaultInfo(
464 files = depset(outputs),
465 data_runfiles = ctx.runfiles(files = outputs),
466 )]
467
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -0400468scramble_flash_vmem = rv_rule(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800469 implementation = _scramble_flash_vmem_impl,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800470 attrs = {
471 "vmem": attr.label(allow_single_file = True),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800472 "_tool": attr.label(
Timothy Trippeld2277d22022-06-02 00:36:08 -0700473 default = "@//util/design:gen-flash-img",
Timothy Trippel4a903632022-04-15 15:04:39 -0700474 executable = True,
475 cfg = "exec",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800476 ),
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000477 },
478)
479
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700480def _gen_sim_dv_logs_db_impl(ctx):
481 outputs = []
482 for src in ctx.files.srcs:
483 if src.extension != "elf":
484 fail("can only generate DV logs database files from ELF files.")
485 logs_db = ctx.actions.declare_file("{}.logs.txt".format(
486 src.basename.replace("." + src.extension, ""),
487 ))
488 rodata = ctx.actions.declare_file("{}.rodata.txt".format(
489 src.basename.replace("." + src.extension, ""),
490 ))
491 outputs.append(logs_db)
492 outputs.append(rodata)
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700493
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700494 ctx.actions.run(
495 outputs = outputs,
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700496 inputs = [src],
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700497 arguments = [
498 "--elf-file",
499 src.path,
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700500 "--logs-fields-section",
501 ".logs.fields",
502 "--name",
503 src.basename.replace("." + src.extension, ""),
504 "--outdir",
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700505 logs_db.dirname,
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700506 ],
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700507 executable = ctx.executable._tool,
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700508 )
509 return [DefaultInfo(
510 files = depset(outputs),
511 data_runfiles = ctx.runfiles(files = outputs),
512 )]
513
514gen_sim_dv_logs_db = rule(
515 implementation = _gen_sim_dv_logs_db_impl,
516 cfg = opentitan_transition,
517 attrs = {
518 "srcs": attr.label_list(allow_files = True),
519 "platform": attr.string(default = OPENTITAN_PLATFORM),
520 "_tool": attr.label(
Timothy Trippeld2277d22022-06-02 00:36:08 -0700521 default = "@//util/device_sw_utils:extract_sw_logs_db",
Timothy Trippeldea42da2022-04-15 14:30:01 -0700522 cfg = "exec",
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700523 executable = True,
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700524 ),
525 "_allowlist_function_transition": attr.label(
526 default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
527 ),
528 },
529)
530
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700531def _assemble_flash_image_impl(ctx):
532 output = ctx.actions.declare_file(ctx.attr.output)
533 outputs = [output]
534 inputs = []
535 arguments = [
Chris Frantz40ac0e72022-09-21 11:40:11 -0700536 "--rcfile=",
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700537 "image",
538 "assemble",
539 "--mirror",
540 "false",
541 "--output",
542 output.path,
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700543 ]
Chris Frantzaf9bcb32022-10-10 13:40:34 -0700544 if ctx.attr.image_size:
545 arguments.append("--size={}".format(ctx.attr.image_size))
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700546 for binary, offset in ctx.attr.binaries.items():
547 inputs.extend(binary.files.to_list())
548 arguments.append("{}@{}".format(binary.files.to_list()[0].path, offset))
549 ctx.actions.run(
550 outputs = outputs,
551 inputs = inputs,
552 arguments = arguments,
553 executable = ctx.executable._opentitantool,
554 )
555 return [DefaultInfo(
556 files = depset(outputs),
557 data_runfiles = ctx.runfiles(files = outputs),
558 )]
559
560assemble_flash_image = rv_rule(
561 implementation = _assemble_flash_image_impl,
562 attrs = {
Chris Frantzaf9bcb32022-10-10 13:40:34 -0700563 "image_size": attr.int(default = 0, doc = "Size of the assembled image"),
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700564 "output": attr.string(),
565 "binaries": attr.label_keyed_string_dict(allow_empty = False),
566 "_opentitantool": attr.label(
567 default = "//sw/host/opentitantool:opentitantool",
568 allow_single_file = True,
569 executable = True,
570 cfg = "exec",
571 ),
572 },
573)
574
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000575def opentitan_binary(
576 name,
577 platform = OPENTITAN_PLATFORM,
Timothy Trippel5ef8a532022-03-29 09:55:03 -0700578 extract_sw_logs_db = False,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500579 testonly = True,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000580 **kwargs):
581 """A helper macro for generating OpenTitan binary artifacts.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800582
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000583 This macro is mostly a wrapper around cc_binary, but creates artifacts
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700584 compatible with OpenTitan binaries. The actual artifacts created are outputs
585 of the rules listed below.
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000586 Args:
587 @param name: The name of this rule.
588 @param platform: The target platform for the artifacts.
Timothy Trippel5ef8a532022-03-29 09:55:03 -0700589 @param extract_sw_logs_db: Whether to emit a log database for DV testbench.
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000590 @param **kwargs: Arguments to forward to `cc_binary`.
591 Emits rules:
Timothy Trippelbc0af182022-08-16 18:46:11 -0700592 cc_binary named: <name>.elf
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700593 rv_preprocess named: <name>_preproc
594 rv_asm named: <name>_asm
595 rv_llvm_ir named: <name>_ll
596 rv_relink_with_map named: <name>_map
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700597 obj_transform named: <name>_bin
598 elf_to_dissassembly named: <name>_dis
599 Optionally:
600 gen_sim_dv_logs_db named: <name>_logs_db
601 Returns:
602 List of targets generated by all of the above rules.
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000603 """
604
605 copts = kwargs.pop("copts", []) + [
606 "-nostdlib",
607 "-ffreestanding",
608 ]
609 linkopts = kwargs.pop("linkopts", []) + [
610 "-nostartfiles",
611 "-nostdlib",
612 ]
613 deps = kwargs.pop("deps", [])
614 targets = []
Timothy Trippelddcc7992022-08-05 13:47:32 -0700615 side_targets = []
Timothy Trippele3a7c572022-02-17 17:30:47 -0800616
Timothy Trippel36f96542022-08-02 18:31:20 -0700617 native_binary_name = "{}.elf".format(name)
Timothy Trippele3a7c572022-02-17 17:30:47 -0800618 native.cc_binary(
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700619 name = native_binary_name,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800620 deps = deps,
621 target_compatible_with = _targets_compatible_with[platform],
622 copts = copts,
623 linkopts = linkopts,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500624 testonly = testonly,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800625 **kwargs
626 )
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400627
Miguel Young de la Sota7bb7fe72022-04-01 16:40:02 -0400628 preproc_name = "{}_{}".format(name, "preproc")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700629 side_targets.append(preproc_name)
Miguel Young de la Sota7bb7fe72022-04-01 16:40:02 -0400630 rv_preprocess(
631 name = preproc_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700632 target = native_binary_name,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500633 testonly = testonly,
Miguel Young de la Sota7bb7fe72022-04-01 16:40:02 -0400634 )
635
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400636 asm_name = "{}_{}".format(name, "asm")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700637 side_targets.append(asm_name)
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400638 rv_asm(
639 name = asm_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700640 target = native_binary_name,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500641 testonly = testonly,
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400642 )
643
644 ll_name = "{}_{}".format(name, "ll")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700645 side_targets.append(ll_name)
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400646 rv_llvm_ir(
647 name = ll_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700648 target = native_binary_name,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500649 testonly = testonly,
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400650 )
651
652 map_name = "{}_{}".format(name, "map")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700653 side_targets.append(map_name)
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400654 rv_relink_with_linkmap(
655 name = map_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700656 target = native_binary_name,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500657 testonly = testonly,
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400658 )
659
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700660 bin_name = "{}_{}".format(name, "bin")
661 targets.append(":" + bin_name)
662 obj_transform(
663 name = bin_name,
664 srcs = [native_binary_name],
665 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500666 testonly = testonly,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700667 )
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000668
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700669 dis_name = "{}_{}".format(name, "dis")
670 targets.append(":" + dis_name)
671 elf_to_disassembly(
672 name = dis_name,
673 srcs = [native_binary_name],
674 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500675 testonly = testonly,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700676 )
Timothy Trippele3a7c572022-02-17 17:30:47 -0800677
Timothy Trippel1f974b32022-03-28 16:07:09 -0700678 # Generate log message database for DV sim testbench
Timothy Trippel5ef8a532022-03-29 09:55:03 -0700679 if extract_sw_logs_db:
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700680 logs_db_name = "{}_{}".format(name, "logs_db")
Timothy Trippel1f974b32022-03-28 16:07:09 -0700681 targets.append(":" + logs_db_name)
682 gen_sim_dv_logs_db(
683 name = logs_db_name,
Timothy Trippel36f96542022-08-02 18:31:20 -0700684 srcs = [native_binary_name],
Timothy Trippel1f974b32022-03-28 16:07:09 -0700685 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500686 testonly = testonly,
Timothy Trippel1f974b32022-03-28 16:07:09 -0700687 )
688
Timothy Trippelddcc7992022-08-05 13:47:32 -0700689 # Create a filegroup with just the sides targets.
690 native.filegroup(
691 name = name + "_side_targets",
692 srcs = side_targets,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500693 testonly = testonly,
Timothy Trippelddcc7992022-08-05 13:47:32 -0700694 )
695
Timothy Trippele3a7c572022-02-17 17:30:47 -0800696 return targets
697
698def opentitan_rom_binary(
699 name,
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700700 devices = PER_DEVICE_DEPS.keys(),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800701 platform = OPENTITAN_PLATFORM,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500702 testonly = True,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800703 **kwargs):
704 """A helper macro for generating OpenTitan binary artifacts for ROM.
705
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700706 This macro is mostly a wrapper around the `opentitan_binary` macro, but also
707 creates artifacts for each of the keys in `PER_DEVICE_DEPS`. The actual
708 artifacts created are outputs of the rules emitted by the `opentitan_binary`
709 macro and those listed below.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800710 Args:
711 @param name: The name of this rule.
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700712 @param devices: List of devices to build the target for.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800713 @param platform: The target platform for the artifacts.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800714 @param **kwargs: Arguments to forward to `opentitan_binary`.
715 Emits rules:
716 For each device in per_device_deps entry:
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700717 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
718 bin_to_rom_vmem named: <name>_<device>_vmem
719 elf_to_scrambled_rom_vmem named: <name>_<device>_scr_vmem
720 filegroup named: <name>_<device>
721 Containing all targets for a single device for the above generated rules.
722 filegroup named: <name>
723 Containing all targets across all devices for the above generated rules.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800724 """
Timothy Trippele3a7c572022-02-17 17:30:47 -0800725 deps = kwargs.pop("deps", [])
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700726 all_targets = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700727 for device in devices:
728 if device not in PER_DEVICE_DEPS:
729 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
730 dev_deps = PER_DEVICE_DEPS[device]
Timothy Trippele3a7c572022-02-17 17:30:47 -0800731 devname = "{}_{}".format(name, device)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700732 dev_targets = []
Timothy Trippele3a7c572022-02-17 17:30:47 -0800733
Timothy Trippel1f974b32022-03-28 16:07:09 -0700734 # Generate ELF, Binary, Disassembly, and (maybe) sim_dv logs database
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700735 dev_targets.extend(opentitan_binary(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800736 name = devname,
737 deps = deps + dev_deps,
Alphan Ulusoye77043f2022-08-29 10:23:46 -0400738 extract_sw_logs_db = device == "sim_dv",
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500739 testonly = testonly,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800740 **kwargs
741 ))
Timothy Trippel77c09d52022-09-02 12:22:02 -0700742
743 # We need to generate VMEM files even for FPGA devices, because we use
744 # them for bitstream splicing.
Timothy Trippel36f96542022-08-02 18:31:20 -0700745 elf_name = "{}.{}".format(devname, "elf")
Miles Dai1634fac2022-05-13 17:55:35 -0400746 bin_name = "{}_{}".format(devname, "bin")
747
748 # Generate Un-scrambled ROM VMEM
749 vmem_name = "{}_vmem".format(devname)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700750 dev_targets.append(":" + vmem_name)
Miles Dai1634fac2022-05-13 17:55:35 -0400751 bin_to_vmem(
752 name = vmem_name,
753 bin = bin_name,
754 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500755 testonly = testonly,
Miles Dai91d811e2022-05-10 15:51:17 -0400756 word_size = 32,
Miles Dai1634fac2022-05-13 17:55:35 -0400757 )
Timothy Trippele3a7c572022-02-17 17:30:47 -0800758
759 # Generate Scrambled ROM VMEM
760 scr_vmem_name = "{}_scr_vmem".format(devname)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700761 dev_targets.append(":" + scr_vmem_name)
Timothy Trippele3a7c572022-02-17 17:30:47 -0800762 elf_to_scrambled_rom_vmem(
763 name = scr_vmem_name,
764 srcs = [elf_name],
765 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500766 testonly = testonly,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800767 )
768
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700769 # Create a filegroup with just the current device's targets.
770 native.filegroup(
771 name = devname,
772 srcs = dev_targets,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500773 testonly = testonly,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700774 )
775 all_targets.extend(dev_targets)
776
777 # Create a filegroup with just all targets from all devices.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800778 native.filegroup(
779 name = name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700780 srcs = all_targets,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500781 testonly = testonly,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800782 )
783
Alphan Ulusoy96725992022-06-27 14:56:59 -0400784def _pick_correct_archive_for_device(ctx):
785 cc_infos = []
786 for dep in ctx.attr.deps:
787 if CcInfo in dep:
788 cc_info = dep[CcInfo]
789 elif ArchiveInfo in dep:
790 cc_info = dep[ArchiveInfo].archive_infos[ctx.attr.device]
791 else:
792 fail("Expected either a CcInfo or an ArchiveInfo")
793 cc_infos.append(cc_info)
794 return [cc_common.merge_cc_infos(cc_infos = cc_infos)]
795
796pick_correct_archive_for_device = rv_rule(
797 implementation = _pick_correct_archive_for_device,
798 attrs = {
799 "deps": attr.label_list(allow_files = True),
800 "device": attr.string(),
801 },
802 fragments = ["cpp"],
803 toolchains = ["@rules_cc//cc:toolchain_type"],
804)
805
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700806def opentitan_multislot_flash_binary(
807 name,
808 srcs,
Chris Frantzaf9bcb32022-10-10 13:40:34 -0700809 image_size = 0,
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700810 devices = PER_DEVICE_DEPS.keys(),
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500811 platform = OPENTITAN_PLATFORM,
812 testonly = True):
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700813 """A helper macro for generating multislot OpenTitan binary flash images.
814
815 This macro is mostly a wrapper around the `assemble_flash_image` rule, that
816 invokes `opentitantool` to stitch together multiple `opentitan_flash_binary`
Timothy Trippelfcc8bac2022-08-23 17:20:24 -0700817 images to create a single image for bootstrapping. Since bootstrap erases
818 the flash for programming this is the only way to load multiple
819 (A/B/Virtual) slots and (silicon creator, ROM_EXT, and owner, BL0) stages at
820 the same time.
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700821 Args:
822 @param name: The name of this rule.
823 @param srcs: A dictionary of `opentitan_flash_binary` targets (to stitch
824 together) as keys, and key/offset configurations as values.
Chris Frantzaf9bcb32022-10-10 13:40:34 -0700825 @param image_size: The final flash image_size to pass to `opentitantool`
826 (optional).
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700827 @param devices: List of devices to build the target for.
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700828 @param platform: The target platform for the artifacts.
829 Emits rules:
830 For each device in per_device_deps entry:
831 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
832 assemble_flash_image named: <name>_<device>_bin_signed
833 bin_to_vmem named: <name>_<device>_vmem64_signed
834 scrambled_flash_vmem named: <name>_<device>_scr_vmem64_signed
835 filegroup named: <name>_<device>
836 Containing all targets for a single device for the above generated rules.
837 filegroup named: <name>
838 Containing all targets across all devices for the above generated rules.
839 """
840 all_targets = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700841 for device in devices:
842 if device not in PER_DEVICE_DEPS:
843 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700844 devname = "{}_{}".format(name, device)
845 dev_targets = []
846 signed_dev_binaries = {}
847 for src, configs in srcs.items():
848 if "key" not in configs:
849 fail("Missing signing key for binary: {}".format(src))
850 if "offset" not in configs:
851 fail("Missing offset for binary: {}".format(src))
852 signed_dev_binary = "{}_{}_bin_signed_{}".format(
853 src,
854 device,
855 configs["key"],
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500856 testonly = testonly,
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700857 )
858 signed_dev_binaries[signed_dev_binary] = configs["offset"]
859
860 # Assemble the signed binaries into a single binary.
861 signed_bin_name = "{}_bin_signed".format(devname)
862 dev_targets.append(":" + signed_bin_name)
863 assemble_flash_image(
864 name = signed_bin_name,
865 output = "{}.signed.bin".format(devname),
866 image_size = image_size,
867 binaries = signed_dev_binaries,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500868 testonly = testonly,
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700869 )
870
Timothy Trippel77c09d52022-09-02 12:22:02 -0700871 # We only need to generate VMEM files for sim devices.
872 if device in ["sim_dv", "sim_verilator"]:
873 # Generate a VMEM64 from the binary.
874 signed_vmem_name = "{}_vmem64_signed".format(devname)
875 dev_targets.append(":" + signed_vmem_name)
876 bin_to_vmem(
877 name = signed_vmem_name,
878 bin = signed_bin_name,
879 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500880 testonly = testonly,
Timothy Trippel77c09d52022-09-02 12:22:02 -0700881 word_size = 64, # Backdoor-load VMEM image uses 64-bit words
882 )
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700883
Timothy Trippel77c09d52022-09-02 12:22:02 -0700884 # Scramble signed VMEM64.
885 scr_signed_vmem_name = "{}_scr_vmem64_signed".format(devname)
886 dev_targets.append(":" + scr_signed_vmem_name)
887 scramble_flash_vmem(
888 name = scr_signed_vmem_name,
889 vmem = signed_vmem_name,
890 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500891 testonly = testonly,
Timothy Trippel77c09d52022-09-02 12:22:02 -0700892 )
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700893
894 # Create a filegroup with just the current device's targets.
895 native.filegroup(
896 name = devname,
897 srcs = dev_targets,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500898 testonly = testonly,
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700899 )
900 dev_targets.extend(dev_targets)
901
902 # Create a filegroup with all assembled flash images.
903 native.filegroup(
904 name = name,
905 srcs = all_targets,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500906 testonly = testonly,
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700907 )
908
Timothy Trippele3a7c572022-02-17 17:30:47 -0800909def opentitan_flash_binary(
910 name,
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700911 devices = PER_DEVICE_DEPS.keys(),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800912 platform = OPENTITAN_PLATFORM,
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700913 signing_keys = DEFAULT_SIGNING_KEYS,
Alphan Ulusoyc8b2a202022-09-14 12:39:42 -0400914 signed = True,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500915 testonly = True,
Alphan Ulusoyc8b2a202022-09-14 12:39:42 -0400916 manifest = "//sw/device/silicon_creator/rom_ext:manifest_standard",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800917 **kwargs):
918 """A helper macro for generating OpenTitan binary artifacts for flash.
919
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700920 This macro is mostly a wrapper around the `opentitan_binary` macro, but also
921 creates artifacts for each of the keys in `PER_DEVICE_DEPS`, and if signing
922 is enabled, each of the keys in `signing_keys`. The actual artifacts created
923 artifacts created are outputs of the rules emitted by the `opentitan_binary`
924 macro and those listed below.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800925 Args:
926 @param name: The name of this rule.
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700927 @param devices: List of devices to build the target for.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800928 @param platform: The target platform for the artifacts.
929 @param signing_keys: The signing keys for to sign each BIN file with.
Timothy Trippelb867c1c2022-08-03 23:42:16 -0700930 @param signed: Whether or not to emit signed binary/VMEM files.
Timothy Trippel49cd1a12022-08-10 14:11:51 -0700931 @param manifest: Partially populated manifest to set boot stage/slot configs.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800932 @param **kwargs: Arguments to forward to `opentitan_binary`.
933 Emits rules:
934 For each device in per_device_deps entry:
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700935 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
936 bin_to_vmem named: <name>_<device>_vmem64
937 scrambled_flash_vmem named: <name>_<device>_scr_vmem64
938 Optionally:
Timothy Trippele3a7c572022-02-17 17:30:47 -0800939 sign_bin named: <name>_<device>_bin_signed_<key_name>
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700940 bin_to_vmem named: <name>_<device>_vmem64_signed_<key_name>
941 scrambled_flash_vmem named: <name>_<device>_scr_vmem64_signed_<key_name>
942 filegroup named: <name>_<device>
943 Containing all targets for a single device for the above generated rules.
944 filegroup named: <name>
945 Containing all targets across all devices for the above generated rules.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800946 """
Timothy Trippele3a7c572022-02-17 17:30:47 -0800947 deps = kwargs.pop("deps", [])
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700948 all_targets = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700949 for device in devices:
950 if device not in PER_DEVICE_DEPS:
951 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
952 dev_deps = PER_DEVICE_DEPS[device]
Timothy Trippele3a7c572022-02-17 17:30:47 -0800953 devname = "{}_{}".format(name, device)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700954 dev_targets = []
Timothy Trippele3a7c572022-02-17 17:30:47 -0800955
Alphan Ulusoy96725992022-06-27 14:56:59 -0400956 depname = "{}_deps".format(devname)
957 pick_correct_archive_for_device(
958 name = depname,
959 deps = deps + dev_deps,
960 device = device,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500961 testonly = testonly,
Alphan Ulusoy96725992022-06-27 14:56:59 -0400962 )
963
Timothy Trippel1f974b32022-03-28 16:07:09 -0700964 # Generate ELF, Binary, Disassembly, and (maybe) sim_dv logs database
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700965 dev_targets.extend(opentitan_binary(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800966 name = devname,
Alphan Ulusoy96725992022-06-27 14:56:59 -0400967 deps = [depname],
Alphan Ulusoye77043f2022-08-29 10:23:46 -0400968 extract_sw_logs_db = device == "sim_dv",
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500969 testonly = testonly,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800970 **kwargs
971 ))
Timothy Trippele3a7c572022-02-17 17:30:47 -0800972 bin_name = "{}_{}".format(devname, "bin")
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000973
Timothy Trippelba061ed2022-03-23 00:30:22 -0700974 # Sign BIN (if required) and generate scrambled VMEM images.
Timothy Trippelb867c1c2022-08-03 23:42:16 -0700975 if signed:
Timothy Trippel49cd1a12022-08-10 14:11:51 -0700976 if manifest == None:
977 fail("A 'manifest' must be provided in order to sign flash images.")
Timothy Trippel92173aa2022-02-15 23:41:41 -0800978 for (key_name, key) in signing_keys.items():
Timothy Trippele3a7c572022-02-17 17:30:47 -0800979 # Sign the Binary.
980 signed_bin_name = "{}_bin_signed_{}".format(devname, key_name)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700981 dev_targets.append(":" + signed_bin_name)
Timothy Trippele3a7c572022-02-17 17:30:47 -0800982 sign_bin(
983 name = signed_bin_name,
984 bin = bin_name,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800985 key = key,
986 key_name = key_name,
Chris Frantzbac04542022-06-16 09:57:29 -0700987 manifest = manifest,
Alphan Ulusoy304038c2022-11-15 14:15:59 -0500988 testonly = testonly,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800989 )
990
Timothy Trippel77c09d52022-09-02 12:22:02 -0700991 # We only need to generate VMEM files for sim devices.
992 if device in ["sim_dv", "sim_verilator"]:
993 # Generate a VMEM64 from the signed binary.
994 signed_vmem_name = "{}_vmem64_signed_{}".format(
995 devname,
996 key_name,
997 )
998 dev_targets.append(":" + signed_vmem_name)
999 bin_to_vmem(
1000 name = signed_vmem_name,
1001 bin = signed_bin_name,
1002 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -05001003 testonly = testonly,
Timothy Trippel77c09d52022-09-02 12:22:02 -07001004 word_size = 64, # Backdoor-load VMEM image uses 64-bit words
1005 )
Timothy Trippele3a7c572022-02-17 17:30:47 -08001006
Timothy Trippel77c09d52022-09-02 12:22:02 -07001007 # Scramble signed VMEM64.
1008 scr_signed_vmem_name = "{}_scr_vmem64_signed_{}".format(
1009 devname,
1010 key_name,
1011 )
1012 dev_targets.append(":" + scr_signed_vmem_name)
1013 scramble_flash_vmem(
1014 name = scr_signed_vmem_name,
1015 vmem = signed_vmem_name,
1016 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -05001017 testonly = testonly,
Timothy Trippel77c09d52022-09-02 12:22:02 -07001018 )
Timothy Trippelba061ed2022-03-23 00:30:22 -07001019
Timothy Trippel77c09d52022-09-02 12:22:02 -07001020 # We only need to generate VMEM files for sim devices.
1021 if device in ["sim_dv", "sim_verilator"]:
1022 # Generate a VMEM64 from the binary.
1023 vmem_name = "{}_vmem64".format(devname)
1024 dev_targets.append(":" + vmem_name)
1025 bin_to_vmem(
1026 name = vmem_name,
1027 bin = bin_name,
1028 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -05001029 testonly = testonly,
Timothy Trippel77c09d52022-09-02 12:22:02 -07001030 word_size = 64, # Backdoor-load VMEM image uses 64-bit words
1031 )
Timothy Trippelb867c1c2022-08-03 23:42:16 -07001032
Timothy Trippel77c09d52022-09-02 12:22:02 -07001033 # Scramble VMEM64.
1034 scr_vmem_name = "{}_scr_vmem64".format(devname)
1035 dev_targets.append(":" + scr_vmem_name)
1036 scramble_flash_vmem(
1037 name = scr_vmem_name,
1038 vmem = vmem_name,
1039 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -05001040 testonly = testonly,
Timothy Trippel77c09d52022-09-02 12:22:02 -07001041 )
Timothy Trippele3a7c572022-02-17 17:30:47 -08001042
Timothy Trippel3821d5d2022-08-02 10:20:49 -07001043 # Create a filegroup with just the current device's targets.
1044 native.filegroup(
1045 name = devname,
1046 srcs = dev_targets,
Alphan Ulusoy304038c2022-11-15 14:15:59 -05001047 testonly = testonly,
Timothy Trippel3821d5d2022-08-02 10:20:49 -07001048 )
1049 all_targets.extend(dev_targets)
1050
1051 # Create a filegroup with just all targets from all devices.
Drew Macrae7ac1efb2021-09-22 16:08:28 +00001052 native.filegroup(
1053 name = name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -07001054 srcs = all_targets,
Alphan Ulusoy304038c2022-11-15 14:15:59 -05001055 testonly = testonly,
Drew Macrae7ac1efb2021-09-22 16:08:28 +00001056 )
Alphan Ulusoy96725992022-06-27 14:56:59 -04001057
1058def opentitan_ram_binary(
1059 name,
Timothy Trippel90c0de72022-08-05 17:18:23 -07001060 archive_symbol_prefix,
Timothy Trippel3e3f4602022-09-02 14:30:45 -07001061 devices = PER_DEVICE_DEPS.keys(),
Alphan Ulusoy96725992022-06-27 14:56:59 -04001062 platform = OPENTITAN_PLATFORM,
Alphan Ulusoy304038c2022-11-15 14:15:59 -05001063 testonly = True,
Alphan Ulusoy96725992022-06-27 14:56:59 -04001064 **kwargs):
1065 """A helper macro for generating OpenTitan binary artifacts for RAM.
1066
Timothy Trippel90c0de72022-08-05 17:18:23 -07001067 This macro is mostly a wrapper around the `opentitan_binary` macro, but also
1068 creates artifacts for each of the keys in `PER_DEVICE_DEPS`. The actual
1069 artifacts created are outputs of the rules emitted by the `opentitan_binary`
1070 macro and those listed below.
Alphan Ulusoy96725992022-06-27 14:56:59 -04001071 Args:
1072 @param name: The name of this rule.
Timothy Trippel90c0de72022-08-05 17:18:23 -07001073 @param archive_symbol_prefix: Prefix used to rename symbols in the binary.
Timothy Trippel3e3f4602022-09-02 14:30:45 -07001074 @param devices: List of devices to build the target for.
1075 @param platform: The target platform for the artifacts.
Alphan Ulusoy96725992022-06-27 14:56:59 -04001076 @param **kwargs: Arguments to forward to `opentitan_binary`.
1077 Emits rules:
1078 For each device in per_device_deps entry:
Timothy Trippel90c0de72022-08-05 17:18:23 -07001079 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
1080 bin_to_archive named: <name>
Alphan Ulusoy96725992022-06-27 14:56:59 -04001081 """
Alphan Ulusoy96725992022-06-27 14:56:59 -04001082 deps = kwargs.pop("deps", [])
1083 hdrs = kwargs.pop("hdrs", [])
Alphan Ulusoy96725992022-06-27 14:56:59 -04001084 binaries = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -07001085 for device in devices:
1086 if device not in PER_DEVICE_DEPS:
1087 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
1088 dev_deps = PER_DEVICE_DEPS[device]
Alphan Ulusoy96725992022-06-27 14:56:59 -04001089 devname = "{}_{}".format(name, device)
Timothy Trippelf742a362022-11-18 11:09:54 -08001090 dev_targets = []
Alphan Ulusoy96725992022-06-27 14:56:59 -04001091
Timothy Trippel90c0de72022-08-05 17:18:23 -07001092 # Generate the binary.
Timothy Trippelf742a362022-11-18 11:09:54 -08001093 dev_targets.extend(opentitan_binary(
Alphan Ulusoy96725992022-06-27 14:56:59 -04001094 name = devname,
1095 deps = deps + dev_deps,
Timothy Trippelf742a362022-11-18 11:09:54 -08001096 extract_sw_logs_db = device == "sim_dv",
Alphan Ulusoy304038c2022-11-15 14:15:59 -05001097 testonly = testonly,
Alphan Ulusoy96725992022-06-27 14:56:59 -04001098 **kwargs
Timothy Trippelf742a362022-11-18 11:09:54 -08001099 ))
Alphan Ulusoy96725992022-06-27 14:56:59 -04001100 bin_name = "{}_{}".format(devname, "bin")
1101 binaries.append(":" + bin_name)
1102
Timothy Trippelf742a362022-11-18 11:09:54 -08001103 # Generate Un-scrambled RAM VMEM (for testing SRAM injection in DV)
1104 vmem_name = "{}_vmem".format(devname)
1105 dev_targets.append(":" + vmem_name)
1106 bin_to_vmem(
1107 name = vmem_name,
1108 bin = bin_name,
1109 platform = platform,
Alphan Ulusoy304038c2022-11-15 14:15:59 -05001110 testonly = testonly,
Timothy Trippelf742a362022-11-18 11:09:54 -08001111 word_size = 32,
1112 )
1113
1114 # Create a filegroup with just the current device's targets.
1115 native.filegroup(
1116 name = devname,
1117 srcs = dev_targets,
Alphan Ulusoy304038c2022-11-15 14:15:59 -05001118 testonly = testonly,
Timothy Trippelf742a362022-11-18 11:09:54 -08001119 )
1120
Alphan Ulusoy96725992022-06-27 14:56:59 -04001121 # Generate the archive file.
Alphan Ulusoy96725992022-06-27 14:56:59 -04001122 bin_to_archive(
Timothy Trippel90c0de72022-08-05 17:18:23 -07001123 name = name,
Alphan Ulusoy96725992022-06-27 14:56:59 -04001124 hdrs = hdrs,
1125 binaries = binaries,
Timothy Trippel90c0de72022-08-05 17:18:23 -07001126 devices = PER_DEVICE_DEPS.keys(),
Alphan Ulusoy96725992022-06-27 14:56:59 -04001127 archive_symbol_prefix = archive_symbol_prefix,
Alphan Ulusoy304038c2022-11-15 14:15:59 -05001128 testonly = testonly,
Alphan Ulusoy96725992022-06-27 14:56:59 -04001129 )