blob: 63981957e32aedbde9704d91fca528b484a752cc [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
36# simulation platforms (DV and Verilator), and two FPGA platforms (NexysVideo
37# 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"],
41 "fpga_nexysvideo": ["@//sw/device/lib/arch:fpga_nexysvideo"],
42 "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 = {
47 "test_key_0": "@//sw/device/silicon_creator/rom/keys:test_private_key_0",
48}
49
Timothy Trippele3a7c572022-02-17 17:30:47 -080050def _obj_transform_impl(ctx):
Miguel Young de la Sota4b01c7a2022-04-21 17:25:52 -040051 cc_toolchain = find_cc_toolchain(ctx).cc
Drew Macrae7ac1efb2021-09-22 16:08:28 +000052 outputs = []
53 for src in ctx.files.srcs:
Timothy Trippel36f96542022-08-02 18:31:20 -070054 binary = ctx.actions.declare_file(
55 "{}.{}".format(
56 src.basename.replace("." + src.extension, ""),
57 ctx.attr.suffix,
58 ),
59 )
Drew Macrae7ac1efb2021-09-22 16:08:28 +000060 outputs.append(binary)
61 ctx.actions.run(
62 outputs = [binary],
63 inputs = [src] + cc_toolchain.all_files.to_list(),
64 arguments = [
65 "--output-target",
66 ctx.attr.format,
67 src.path,
68 binary.path,
69 ],
70 executable = cc_toolchain.objcopy_executable,
71 )
72 return [DefaultInfo(files = depset(outputs), data_runfiles = ctx.runfiles(files = outputs))]
73
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -040074obj_transform = rv_rule(
Timothy Trippele3a7c572022-02-17 17:30:47 -080075 implementation = _obj_transform_impl,
Drew Macrae7ac1efb2021-09-22 16:08:28 +000076 attrs = {
77 "srcs": attr.label_list(allow_files = True),
78 "suffix": attr.string(default = "bin"),
79 "format": attr.string(default = "binary"),
Drew Macrae7ac1efb2021-09-22 16:08:28 +000080 "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
Drew Macrae7ac1efb2021-09-22 16:08:28 +000081 },
82 toolchains = ["@rules_cc//cc:toolchain_type"],
83)
84
Alphan Ulusoy96725992022-06-27 14:56:59 -040085# A provider for device-specific archive files that hold binaries of SRAM programs.
86ArchiveInfo = provider(fields = ["archive_infos"])
87
88def _bin_to_archive_impl(ctx):
89 cc_infos = []
90 cc_toolchain = find_cc_toolchain(ctx).cc
91 cc_info_dict = {}
92 num_devices = len(ctx.attr.devices)
93 num_binaries = len(ctx.attr.binaries)
94 if num_devices != num_binaries:
95 fail("Number of devices", num_devices, "must be equal to number of binaries", num_binaries)
96 for (device, binary_target) in zip(ctx.attr.devices, ctx.attr.binaries):
97 devname = "{}_{}".format(ctx.attr.name, device)
98 binary_file = binary_target.files.to_list()[0]
99 object_file = ctx.actions.declare_file("{}.o".format(devname))
100 renamed_object_file = ctx.actions.declare_file("{}.renamed.o".format(devname))
101 archive_file = ctx.actions.declare_file("{}.a".format(devname))
102
103 # Create a CcInfo to be able to use this rule as a dependency in other rules.
104 # See https://bazel.build/docs/integrating-with-rules-cc.
105 feature_configuration = cc_common.configure_features(
106 ctx = ctx,
107 cc_toolchain = cc_toolchain,
108 requested_features = ctx.features,
109 unsupported_features = ctx.disabled_features,
110 )
111 action_name = ACTION_NAMES.cpp_link_executable
112 c_linker_path = cc_common.get_tool_for_action(
113 feature_configuration = feature_configuration,
114 action_name = action_name,
115 )
116 c_link_variables = cc_common.create_link_variables(
117 feature_configuration = feature_configuration,
118 cc_toolchain = cc_toolchain,
119 output_file = object_file.path,
120 )
121 command_line = cc_common.get_memory_inefficient_command_line(
122 feature_configuration = feature_configuration,
123 action_name = action_name,
124 variables = c_link_variables,
125 )
126 env = cc_common.get_environment_variables(
127 feature_configuration = feature_configuration,
128 action_name = action_name,
129 variables = c_link_variables,
130 )
131 linker_path = cc_common.get_tool_for_action(
132 feature_configuration = feature_configuration,
133 action_name = action_name,
134 )
135
136 # Create an object file that contains the binary.
137 ctx.actions.run(
138 executable = cc_toolchain.ld_executable,
139 arguments = [
140 "-r",
141 "-b",
142 "binary",
143 "-o",
144 object_file.path,
145 binary_file.path,
146 ],
147 use_default_shell_env = False,
148 env = env,
149 inputs = depset(
150 direct = [binary_file],
151 transitive = [cc_toolchain.all_files],
152 ),
153 outputs = [object_file],
154 mnemonic = "CppLink",
155 )
156
157 # Rename symbols to make them more manageable.
158 sym_prefix = "_binary_" + binary_file.path.replace(".", "_").replace("/", "_").replace("-", "_")
159 suffixes = ["start", "end", "size"]
160 rename_args = []
161 for suffix in suffixes:
162 old_name = "{}_{}".format(sym_prefix, suffix)
163 new_name = "_{}_{}".format(ctx.attr.archive_symbol_prefix, suffix)
164 rename_args.extend(["--redefine-sym", "{}={}".format(old_name, new_name)])
165 rename_args.extend(["--rename-section", ".data=.data.sram_program"])
166 rename_args.extend([object_file.path, renamed_object_file.path])
167 ctx.actions.run(
168 executable = cc_toolchain.objcopy_executable,
169 arguments = rename_args,
170 use_default_shell_env = False,
171 env = env,
172 inputs = depset(
173 direct = [object_file],
174 transitive = [cc_toolchain.all_files],
175 ),
176 outputs = [renamed_object_file],
177 mnemonic = "RenameSymbols",
178 )
179
180 # Create an archive that contains the object file.
181 ctx.actions.run(
182 executable = cc_toolchain.ar_executable,
183 arguments = [
184 "r",
185 archive_file.path,
186 renamed_object_file.path,
187 ],
188 use_default_shell_env = False,
189 env = env,
190 inputs = depset(
191 direct = [renamed_object_file],
192 transitive = [cc_toolchain.all_files],
193 ),
194 outputs = [archive_file],
195 mnemonic = "Archive",
196 )
197
198 cc_info_dict[device] = CcInfo(
199 compilation_context = cc_common.create_compilation_context(
200 headers = depset(direct = ctx.attr.hdrs[0].files.to_list()),
201 ),
202 linking_context = cc_common.create_linking_context(
203 linker_inputs = depset([cc_common.create_linker_input(
204 owner = ctx.label,
205 libraries = depset([cc_common.create_library_to_link(
206 actions = ctx.actions,
207 feature_configuration = feature_configuration,
208 cc_toolchain = cc_toolchain,
209 static_library = archive_file,
210 )]),
211 )]),
212 ),
213 )
214
215 return ArchiveInfo(archive_infos = cc_info_dict)
216
217bin_to_archive = rv_rule(
218 implementation = _bin_to_archive_impl,
219 attrs = {
220 "binaries": attr.label_list(allow_files = True),
221 "devices": attr.string_list(),
222 "hdrs": attr.label_list(allow_files = True),
223 "archive_symbol_prefix": attr.string(),
224 "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
225 },
226 fragments = ["cpp"],
227 toolchains = ["@rules_cc//cc:toolchain_type"],
228)
229
Timothy Trippele3a7c572022-02-17 17:30:47 -0800230def _sign_bin_impl(ctx):
Timothy Trippel92173aa2022-02-15 23:41:41 -0800231 signed_image = ctx.actions.declare_file(
232 "{0}.{1}.signed.bin".format(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800233 # Remove ".bin" from file basename.
234 ctx.file.bin.basename.replace("." + ctx.file.bin.extension, ""),
Timothy Trippel92173aa2022-02-15 23:41:41 -0800235 ctx.attr.key_name,
236 ),
237 )
Chris Frantzbac04542022-06-16 09:57:29 -0700238 outputs = [signed_image]
239
240 inputs = [
241 ctx.file.bin,
242 ctx.file.key,
243 ctx.file._tool,
244 ]
245 manifest = []
246 if ctx.file.manifest:
247 manifest = ["--manifest={}".format(ctx.file.manifest.path)]
248 inputs.append(ctx.file.manifest)
249
Timothy Trippel92173aa2022-02-15 23:41:41 -0800250 ctx.actions.run(
Chris Frantzbac04542022-06-16 09:57:29 -0700251 outputs = outputs,
252 inputs = inputs,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800253 arguments = [
Jon Flatley26e774c2022-06-02 17:29:00 -0400254 "image",
Chris Frantz47dc84b2022-06-16 16:38:08 -0700255 "manifest",
256 "update",
257 "--key-file={}".format(ctx.file.key.path),
258 "--sign",
259 "--output={}".format(signed_image.path),
Timothy Trippel92173aa2022-02-15 23:41:41 -0800260 ctx.file.bin.path,
Chris Frantzbac04542022-06-16 09:57:29 -0700261 ] + manifest,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800262 executable = ctx.file._tool.path,
263 )
264 return [DefaultInfo(
265 files = depset(outputs),
266 data_runfiles = ctx.runfiles(files = outputs),
267 )]
268
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -0400269sign_bin = rv_rule(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800270 implementation = _sign_bin_impl,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800271 attrs = {
272 "bin": attr.label(allow_single_file = True),
Timothy Trippel92173aa2022-02-15 23:41:41 -0800273 "key": attr.label(
Alphan Ulusoy776d2ab2022-08-13 16:47:14 -0400274 default = "@//sw/device/silicon_creator/rom/keys:test_private_key_0",
Timothy Trippel92173aa2022-02-15 23:41:41 -0800275 allow_single_file = True,
276 ),
277 "key_name": attr.string(),
Chris Frantzbac04542022-06-16 09:57:29 -0700278 "manifest": attr.label(allow_single_file = True),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800279 # TODO(lowRISC/opentitan:#11199): explore other options to side-step the
280 # need for this transition, in order to build the ROM_EXT signer tool.
Timothy Trippel92173aa2022-02-15 23:41:41 -0800281 "platform": attr.string(default = "@local_config_platform//:host"),
282 "_tool": attr.label(
Jon Flatley26e774c2022-06-02 17:29:00 -0400283 default = "//sw/host/opentitantool:opentitantool",
Timothy Trippel92173aa2022-02-15 23:41:41 -0800284 allow_single_file = True,
285 ),
Timothy Trippel92173aa2022-02-15 23:41:41 -0800286 },
287)
288
Timothy Trippele3a7c572022-02-17 17:30:47 -0800289def _elf_to_disassembly_impl(ctx):
Miguel Young de la Sota4b01c7a2022-04-21 17:25:52 -0400290 cc_toolchain = find_cc_toolchain(ctx).cc
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000291 outputs = []
292 for src in ctx.files.srcs:
Timothy Trippel36f96542022-08-02 18:31:20 -0700293 disassembly = ctx.actions.declare_file(
294 "{}.dis".format(
295 src.basename.replace("." + src.extension, ""),
296 ),
297 )
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000298 outputs.append(disassembly)
299 ctx.actions.run_shell(
Miguel Young de la Sota9fd48832022-03-25 17:35:06 -0400300 tools = [ctx.file._cleanup_script],
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000301 outputs = [disassembly],
302 inputs = [src] + cc_toolchain.all_files.to_list(),
303 arguments = [
304 cc_toolchain.objdump_executable,
305 src.path,
Miguel Young de la Sota9fd48832022-03-25 17:35:06 -0400306 ctx.file._cleanup_script.path,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000307 disassembly.path,
308 ],
Alphan Ulusoy883d3a92022-06-09 16:42:08 -0400309 execution_requirements = {
310 "no-sandbox": "1",
311 },
Alphan Ulusoyd953c362022-06-16 16:10:47 -0400312 command = "$1 --disassemble --headers --line-numbers --disassemble-zeroes --source --visualize-jumps $2 | $3 > $4",
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000313 )
Miguel Young de la Sota9fd48832022-03-25 17:35:06 -0400314 return [DefaultInfo(files = depset(outputs), data_runfiles = ctx.runfiles(files = outputs))]
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000315
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -0400316elf_to_disassembly = rv_rule(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800317 implementation = _elf_to_disassembly_impl,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000318 attrs = {
319 "srcs": attr.label_list(allow_files = True),
320 "platform": attr.string(default = OPENTITAN_PLATFORM),
Miguel Young de la Sota9fd48832022-03-25 17:35:06 -0400321 "_cleanup_script": attr.label(
322 allow_single_file = True,
Timothy Trippeld2277d22022-06-02 00:36:08 -0700323 default = Label("@//rules/scripts:expand_tabs.sh"),
Miguel Young de la Sota9fd48832022-03-25 17:35:06 -0400324 ),
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -0400325 "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000326 },
327 toolchains = ["@rules_cc//cc:toolchain_type"],
328 incompatible_use_toolchain_transition = True,
329)
330
Timothy Trippele3a7c572022-02-17 17:30:47 -0800331def _elf_to_scrambled_rom_impl(ctx):
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000332 outputs = []
333 for src in ctx.files.srcs:
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700334 if src.extension != "elf":
335 fail("only ROM images in the ELF format may be converted to the VMEM format and scrambled.")
Timothy Trippele3a7c572022-02-17 17:30:47 -0800336 scrambled = ctx.actions.declare_file(
Timothy Trippeld69bcd72022-08-12 08:20:40 -0700337 "{}.39.scr.vmem".format(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800338 # Remove ".elf" from file basename.
339 src.basename.replace("." + src.extension, ""),
340 ),
341 )
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000342 outputs.append(scrambled)
343 ctx.actions.run(
344 outputs = [scrambled],
345 inputs = [
346 src,
Timothy Trippeldea42da2022-04-15 14:30:01 -0700347 ctx.executable._scramble_tool,
348 ctx.file._config,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000349 ],
350 arguments = [
Timothy Trippeldea42da2022-04-15 14:30:01 -0700351 ctx.file._config.path,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000352 src.path,
353 scrambled.path,
354 ],
Timothy Trippeldea42da2022-04-15 14:30:01 -0700355 executable = ctx.executable._scramble_tool,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000356 )
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700357 return [DefaultInfo(
358 files = depset(outputs),
359 data_runfiles = ctx.runfiles(files = outputs),
360 )]
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000361
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -0400362elf_to_scrambled_rom_vmem = rv_rule(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800363 implementation = _elf_to_scrambled_rom_impl,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000364 attrs = {
365 "srcs": attr.label_list(allow_files = True),
Timothy Trippeldea42da2022-04-15 14:30:01 -0700366 "_scramble_tool": attr.label(
Timothy Trippeld2277d22022-06-02 00:36:08 -0700367 default = "@//hw/ip/rom_ctrl/util:scramble_image",
Timothy Trippeldea42da2022-04-15 14:30:01 -0700368 executable = True,
369 cfg = "exec",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800370 ),
371 "_config": attr.label(
Timothy Trippeld2277d22022-06-02 00:36:08 -0700372 default = "@//hw/top_earlgrey/data:autogen/top_earlgrey.gen.hjson",
Timothy Trippeldea42da2022-04-15 14:30:01 -0700373 allow_single_file = True,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800374 ),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800375 },
376)
377
Miles Dai1634fac2022-05-13 17:55:35 -0400378def _bin_to_vmem_impl(ctx):
Timothy Trippele3a7c572022-02-17 17:30:47 -0800379 outputs = []
380 vmem = ctx.actions.declare_file("{}.{}.vmem".format(
381 # Remove ".bin" from file basename.
Timothy Trippelf17bca82022-03-08 11:12:41 -0800382 ctx.file.bin.basename.replace("." + ctx.file.bin.extension, ""),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800383 ctx.attr.word_size,
384 ))
385 outputs.append(vmem)
386 ctx.actions.run(
387 outputs = [vmem],
388 inputs = [
389 ctx.file.bin,
390 ],
391 arguments = [
392 ctx.file.bin.path,
393 "--binary",
394 # Reverse the endianness of every word.
395 "--offset",
396 "0x0",
397 "--byte-swap",
398 str(ctx.attr.word_size // 8),
399 # Pad to word alignment.
400 "--fill",
401 "0xff",
402 "-within",
403 ctx.file.bin.path,
404 "-binary",
405 "-range-pad",
406 str(ctx.attr.word_size // 8),
407 # Output a VMEM file with specified word size
408 "--output",
409 vmem.path,
410 "--vmem",
411 str(ctx.attr.word_size),
412 ],
413 # This this executable is expected to be installed (as required by the
414 # srecord package in apt-requirements.txt).
415 executable = "srec_cat",
Martin Lueker-Bodenc8a7ff72022-09-08 17:35:29 -0700416 use_default_shell_env = True,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800417 )
418 return [DefaultInfo(
419 files = depset(outputs),
420 data_runfiles = ctx.runfiles(files = outputs),
421 )]
422
Miles Dai1634fac2022-05-13 17:55:35 -0400423bin_to_vmem = rv_rule(
424 implementation = _bin_to_vmem_impl,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800425 attrs = {
426 "bin": attr.label(allow_single_file = True),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800427 "word_size": attr.int(
428 default = 64,
429 doc = "Word size of VMEM file.",
430 mandatory = True,
431 values = [32, 64],
432 ),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800433 },
434)
435
436def _scramble_flash_vmem_impl(ctx):
437 outputs = []
438 scrambled_vmem = ctx.actions.declare_file("{}.scr.vmem".format(
439 # Remove ".vmem" from file basename.
Timothy Trippelf17bca82022-03-08 11:12:41 -0800440 ctx.file.vmem.basename.replace("." + ctx.file.vmem.extension, ""),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800441 ))
442 outputs.append(scrambled_vmem)
443 ctx.actions.run(
444 outputs = [scrambled_vmem],
445 inputs = [
446 ctx.file.vmem,
Timothy Trippel4a903632022-04-15 15:04:39 -0700447 ctx.executable._tool,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800448 ],
449 arguments = [
450 ctx.file.vmem.path,
451 scrambled_vmem.path,
452 ],
Timothy Trippel4a903632022-04-15 15:04:39 -0700453 executable = ctx.executable._tool,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800454 )
455 return [DefaultInfo(
456 files = depset(outputs),
457 data_runfiles = ctx.runfiles(files = outputs),
458 )]
459
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -0400460scramble_flash_vmem = rv_rule(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800461 implementation = _scramble_flash_vmem_impl,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800462 attrs = {
463 "vmem": attr.label(allow_single_file = True),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800464 "_tool": attr.label(
Timothy Trippeld2277d22022-06-02 00:36:08 -0700465 default = "@//util/design:gen-flash-img",
Timothy Trippel4a903632022-04-15 15:04:39 -0700466 executable = True,
467 cfg = "exec",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800468 ),
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000469 },
470)
471
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700472def _gen_sim_dv_logs_db_impl(ctx):
473 outputs = []
474 for src in ctx.files.srcs:
475 if src.extension != "elf":
476 fail("can only generate DV logs database files from ELF files.")
477 logs_db = ctx.actions.declare_file("{}.logs.txt".format(
478 src.basename.replace("." + src.extension, ""),
479 ))
480 rodata = ctx.actions.declare_file("{}.rodata.txt".format(
481 src.basename.replace("." + src.extension, ""),
482 ))
483 outputs.append(logs_db)
484 outputs.append(rodata)
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700485
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700486 ctx.actions.run(
487 outputs = outputs,
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700488 inputs = [src],
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700489 arguments = [
490 "--elf-file",
491 src.path,
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700492 "--logs-fields-section",
493 ".logs.fields",
494 "--name",
495 src.basename.replace("." + src.extension, ""),
496 "--outdir",
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700497 logs_db.dirname,
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700498 ],
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700499 executable = ctx.executable._tool,
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700500 )
501 return [DefaultInfo(
502 files = depset(outputs),
503 data_runfiles = ctx.runfiles(files = outputs),
504 )]
505
506gen_sim_dv_logs_db = rule(
507 implementation = _gen_sim_dv_logs_db_impl,
508 cfg = opentitan_transition,
509 attrs = {
510 "srcs": attr.label_list(allow_files = True),
511 "platform": attr.string(default = OPENTITAN_PLATFORM),
512 "_tool": attr.label(
Timothy Trippeld2277d22022-06-02 00:36:08 -0700513 default = "@//util/device_sw_utils:extract_sw_logs_db",
Timothy Trippeldea42da2022-04-15 14:30:01 -0700514 cfg = "exec",
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700515 executable = True,
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700516 ),
517 "_allowlist_function_transition": attr.label(
518 default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
519 ),
520 },
521)
522
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700523def _assemble_flash_image_impl(ctx):
524 output = ctx.actions.declare_file(ctx.attr.output)
525 outputs = [output]
526 inputs = []
527 arguments = [
528 "image",
529 "assemble",
530 "--mirror",
531 "false",
532 "--output",
533 output.path,
534 "--size",
535 ctx.attr.image_size,
536 ]
537 for binary, offset in ctx.attr.binaries.items():
538 inputs.extend(binary.files.to_list())
539 arguments.append("{}@{}".format(binary.files.to_list()[0].path, offset))
540 ctx.actions.run(
541 outputs = outputs,
542 inputs = inputs,
543 arguments = arguments,
544 executable = ctx.executable._opentitantool,
545 )
546 return [DefaultInfo(
547 files = depset(outputs),
548 data_runfiles = ctx.runfiles(files = outputs),
549 )]
550
551assemble_flash_image = rv_rule(
552 implementation = _assemble_flash_image_impl,
553 attrs = {
554 "image_size": attr.string(),
555 "output": attr.string(),
556 "binaries": attr.label_keyed_string_dict(allow_empty = False),
557 "_opentitantool": attr.label(
558 default = "//sw/host/opentitantool:opentitantool",
559 allow_single_file = True,
560 executable = True,
561 cfg = "exec",
562 ),
563 },
564)
565
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000566def opentitan_binary(
567 name,
568 platform = OPENTITAN_PLATFORM,
Timothy Trippel5ef8a532022-03-29 09:55:03 -0700569 extract_sw_logs_db = False,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000570 **kwargs):
571 """A helper macro for generating OpenTitan binary artifacts.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800572
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000573 This macro is mostly a wrapper around cc_binary, but creates artifacts
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700574 compatible with OpenTitan binaries. The actual artifacts created are outputs
575 of the rules listed below.
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000576 Args:
577 @param name: The name of this rule.
578 @param platform: The target platform for the artifacts.
Timothy Trippel5ef8a532022-03-29 09:55:03 -0700579 @param extract_sw_logs_db: Whether to emit a log database for DV testbench.
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000580 @param **kwargs: Arguments to forward to `cc_binary`.
581 Emits rules:
Timothy Trippelbc0af182022-08-16 18:46:11 -0700582 cc_binary named: <name>.elf
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700583 rv_preprocess named: <name>_preproc
584 rv_asm named: <name>_asm
585 rv_llvm_ir named: <name>_ll
586 rv_relink_with_map named: <name>_map
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700587 obj_transform named: <name>_bin
588 elf_to_dissassembly named: <name>_dis
589 Optionally:
590 gen_sim_dv_logs_db named: <name>_logs_db
591 Returns:
592 List of targets generated by all of the above rules.
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000593 """
594
595 copts = kwargs.pop("copts", []) + [
596 "-nostdlib",
597 "-ffreestanding",
598 ]
599 linkopts = kwargs.pop("linkopts", []) + [
600 "-nostartfiles",
601 "-nostdlib",
602 ]
603 deps = kwargs.pop("deps", [])
604 targets = []
Timothy Trippelddcc7992022-08-05 13:47:32 -0700605 side_targets = []
Timothy Trippele3a7c572022-02-17 17:30:47 -0800606
Timothy Trippel36f96542022-08-02 18:31:20 -0700607 native_binary_name = "{}.elf".format(name)
Timothy Trippele3a7c572022-02-17 17:30:47 -0800608 native.cc_binary(
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700609 name = native_binary_name,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800610 deps = deps,
611 target_compatible_with = _targets_compatible_with[platform],
612 copts = copts,
613 linkopts = linkopts,
614 **kwargs
615 )
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400616
Miguel Young de la Sota7bb7fe72022-04-01 16:40:02 -0400617 preproc_name = "{}_{}".format(name, "preproc")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700618 side_targets.append(preproc_name)
Miguel Young de la Sota7bb7fe72022-04-01 16:40:02 -0400619 rv_preprocess(
620 name = preproc_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700621 target = native_binary_name,
Miguel Young de la Sota7bb7fe72022-04-01 16:40:02 -0400622 )
623
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400624 asm_name = "{}_{}".format(name, "asm")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700625 side_targets.append(asm_name)
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400626 rv_asm(
627 name = asm_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700628 target = native_binary_name,
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400629 )
630
631 ll_name = "{}_{}".format(name, "ll")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700632 side_targets.append(ll_name)
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400633 rv_llvm_ir(
634 name = ll_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700635 target = native_binary_name,
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400636 )
637
638 map_name = "{}_{}".format(name, "map")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700639 side_targets.append(map_name)
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400640 rv_relink_with_linkmap(
641 name = map_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700642 target = native_binary_name,
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400643 )
644
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700645 bin_name = "{}_{}".format(name, "bin")
646 targets.append(":" + bin_name)
647 obj_transform(
648 name = bin_name,
649 srcs = [native_binary_name],
650 platform = platform,
651 )
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000652
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700653 dis_name = "{}_{}".format(name, "dis")
654 targets.append(":" + dis_name)
655 elf_to_disassembly(
656 name = dis_name,
657 srcs = [native_binary_name],
658 platform = platform,
659 )
Timothy Trippele3a7c572022-02-17 17:30:47 -0800660
Timothy Trippel1f974b32022-03-28 16:07:09 -0700661 # Generate log message database for DV sim testbench
Timothy Trippel5ef8a532022-03-29 09:55:03 -0700662 if extract_sw_logs_db:
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700663 logs_db_name = "{}_{}".format(name, "logs_db")
Timothy Trippel1f974b32022-03-28 16:07:09 -0700664 targets.append(":" + logs_db_name)
665 gen_sim_dv_logs_db(
666 name = logs_db_name,
Timothy Trippel36f96542022-08-02 18:31:20 -0700667 srcs = [native_binary_name],
Timothy Trippel1f974b32022-03-28 16:07:09 -0700668 platform = platform,
669 )
670
Timothy Trippelddcc7992022-08-05 13:47:32 -0700671 # Create a filegroup with just the sides targets.
672 native.filegroup(
673 name = name + "_side_targets",
674 srcs = side_targets,
675 )
676
Timothy Trippele3a7c572022-02-17 17:30:47 -0800677 return targets
678
679def opentitan_rom_binary(
680 name,
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700681 devices = PER_DEVICE_DEPS.keys(),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800682 platform = OPENTITAN_PLATFORM,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800683 **kwargs):
684 """A helper macro for generating OpenTitan binary artifacts for ROM.
685
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700686 This macro is mostly a wrapper around the `opentitan_binary` macro, but also
687 creates artifacts for each of the keys in `PER_DEVICE_DEPS`. The actual
688 artifacts created are outputs of the rules emitted by the `opentitan_binary`
689 macro and those listed below.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800690 Args:
691 @param name: The name of this rule.
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700692 @param devices: List of devices to build the target for.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800693 @param platform: The target platform for the artifacts.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800694 @param **kwargs: Arguments to forward to `opentitan_binary`.
695 Emits rules:
696 For each device in per_device_deps entry:
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700697 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
698 bin_to_rom_vmem named: <name>_<device>_vmem
699 elf_to_scrambled_rom_vmem named: <name>_<device>_scr_vmem
700 filegroup named: <name>_<device>
701 Containing all targets for a single device for the above generated rules.
702 filegroup named: <name>
703 Containing all targets across all devices for the above generated rules.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800704 """
Timothy Trippele3a7c572022-02-17 17:30:47 -0800705 deps = kwargs.pop("deps", [])
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700706 all_targets = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700707 for device in devices:
708 if device not in PER_DEVICE_DEPS:
709 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
710 dev_deps = PER_DEVICE_DEPS[device]
Timothy Trippele3a7c572022-02-17 17:30:47 -0800711 devname = "{}_{}".format(name, device)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700712 dev_targets = []
Timothy Trippele3a7c572022-02-17 17:30:47 -0800713
Timothy Trippel1f974b32022-03-28 16:07:09 -0700714 # Generate ELF, Binary, Disassembly, and (maybe) sim_dv logs database
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700715 dev_targets.extend(opentitan_binary(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800716 name = devname,
717 deps = deps + dev_deps,
Alphan Ulusoye77043f2022-08-29 10:23:46 -0400718 extract_sw_logs_db = device == "sim_dv",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800719 **kwargs
720 ))
Timothy Trippel77c09d52022-09-02 12:22:02 -0700721
722 # We need to generate VMEM files even for FPGA devices, because we use
723 # them for bitstream splicing.
Timothy Trippel36f96542022-08-02 18:31:20 -0700724 elf_name = "{}.{}".format(devname, "elf")
Miles Dai1634fac2022-05-13 17:55:35 -0400725 bin_name = "{}_{}".format(devname, "bin")
726
727 # Generate Un-scrambled ROM VMEM
728 vmem_name = "{}_vmem".format(devname)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700729 dev_targets.append(":" + vmem_name)
Miles Dai1634fac2022-05-13 17:55:35 -0400730 bin_to_vmem(
731 name = vmem_name,
732 bin = bin_name,
733 platform = platform,
Miles Dai91d811e2022-05-10 15:51:17 -0400734 word_size = 32,
Miles Dai1634fac2022-05-13 17:55:35 -0400735 )
Timothy Trippele3a7c572022-02-17 17:30:47 -0800736
737 # Generate Scrambled ROM VMEM
738 scr_vmem_name = "{}_scr_vmem".format(devname)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700739 dev_targets.append(":" + scr_vmem_name)
Timothy Trippele3a7c572022-02-17 17:30:47 -0800740 elf_to_scrambled_rom_vmem(
741 name = scr_vmem_name,
742 srcs = [elf_name],
743 platform = platform,
744 )
745
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700746 # Create a filegroup with just the current device's targets.
747 native.filegroup(
748 name = devname,
749 srcs = dev_targets,
750 )
751 all_targets.extend(dev_targets)
752
753 # Create a filegroup with just all targets from all devices.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800754 native.filegroup(
755 name = name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700756 srcs = all_targets,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800757 )
758
Alphan Ulusoy96725992022-06-27 14:56:59 -0400759def _pick_correct_archive_for_device(ctx):
760 cc_infos = []
761 for dep in ctx.attr.deps:
762 if CcInfo in dep:
763 cc_info = dep[CcInfo]
764 elif ArchiveInfo in dep:
765 cc_info = dep[ArchiveInfo].archive_infos[ctx.attr.device]
766 else:
767 fail("Expected either a CcInfo or an ArchiveInfo")
768 cc_infos.append(cc_info)
769 return [cc_common.merge_cc_infos(cc_infos = cc_infos)]
770
771pick_correct_archive_for_device = rv_rule(
772 implementation = _pick_correct_archive_for_device,
773 attrs = {
774 "deps": attr.label_list(allow_files = True),
775 "device": attr.string(),
776 },
777 fragments = ["cpp"],
778 toolchains = ["@rules_cc//cc:toolchain_type"],
779)
780
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700781def opentitan_multislot_flash_binary(
782 name,
783 srcs,
784 image_size,
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700785 devices = PER_DEVICE_DEPS.keys(),
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700786 platform = OPENTITAN_PLATFORM):
787 """A helper macro for generating multislot OpenTitan binary flash images.
788
789 This macro is mostly a wrapper around the `assemble_flash_image` rule, that
790 invokes `opentitantool` to stitch together multiple `opentitan_flash_binary`
Timothy Trippelfcc8bac2022-08-23 17:20:24 -0700791 images to create a single image for bootstrapping. Since bootstrap erases
792 the flash for programming this is the only way to load multiple
793 (A/B/Virtual) slots and (silicon creator, ROM_EXT, and owner, BL0) stages at
794 the same time.
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700795 Args:
796 @param name: The name of this rule.
797 @param srcs: A dictionary of `opentitan_flash_binary` targets (to stitch
798 together) as keys, and key/offset configurations as values.
799 @param image_size: The final flash image_size to pass to `opentitantool`.
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700800 @param devices: List of devices to build the target for.
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700801 @param platform: The target platform for the artifacts.
802 Emits rules:
803 For each device in per_device_deps entry:
804 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
805 assemble_flash_image named: <name>_<device>_bin_signed
806 bin_to_vmem named: <name>_<device>_vmem64_signed
807 scrambled_flash_vmem named: <name>_<device>_scr_vmem64_signed
808 filegroup named: <name>_<device>
809 Containing all targets for a single device for the above generated rules.
810 filegroup named: <name>
811 Containing all targets across all devices for the above generated rules.
812 """
813 all_targets = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700814 for device in devices:
815 if device not in PER_DEVICE_DEPS:
816 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700817 devname = "{}_{}".format(name, device)
818 dev_targets = []
819 signed_dev_binaries = {}
820 for src, configs in srcs.items():
821 if "key" not in configs:
822 fail("Missing signing key for binary: {}".format(src))
823 if "offset" not in configs:
824 fail("Missing offset for binary: {}".format(src))
825 signed_dev_binary = "{}_{}_bin_signed_{}".format(
826 src,
827 device,
828 configs["key"],
829 )
830 signed_dev_binaries[signed_dev_binary] = configs["offset"]
831
832 # Assemble the signed binaries into a single binary.
833 signed_bin_name = "{}_bin_signed".format(devname)
834 dev_targets.append(":" + signed_bin_name)
835 assemble_flash_image(
836 name = signed_bin_name,
837 output = "{}.signed.bin".format(devname),
838 image_size = image_size,
839 binaries = signed_dev_binaries,
840 )
841
Timothy Trippel77c09d52022-09-02 12:22:02 -0700842 # We only need to generate VMEM files for sim devices.
843 if device in ["sim_dv", "sim_verilator"]:
844 # Generate a VMEM64 from the binary.
845 signed_vmem_name = "{}_vmem64_signed".format(devname)
846 dev_targets.append(":" + signed_vmem_name)
847 bin_to_vmem(
848 name = signed_vmem_name,
849 bin = signed_bin_name,
850 platform = platform,
851 word_size = 64, # Backdoor-load VMEM image uses 64-bit words
852 )
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700853
Timothy Trippel77c09d52022-09-02 12:22:02 -0700854 # Scramble signed VMEM64.
855 scr_signed_vmem_name = "{}_scr_vmem64_signed".format(devname)
856 dev_targets.append(":" + scr_signed_vmem_name)
857 scramble_flash_vmem(
858 name = scr_signed_vmem_name,
859 vmem = signed_vmem_name,
860 platform = platform,
861 )
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700862
863 # Create a filegroup with just the current device's targets.
864 native.filegroup(
865 name = devname,
866 srcs = dev_targets,
867 )
868 dev_targets.extend(dev_targets)
869
870 # Create a filegroup with all assembled flash images.
871 native.filegroup(
872 name = name,
873 srcs = all_targets,
874 )
875
Timothy Trippele3a7c572022-02-17 17:30:47 -0800876def opentitan_flash_binary(
877 name,
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700878 devices = PER_DEVICE_DEPS.keys(),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800879 platform = OPENTITAN_PLATFORM,
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700880 signing_keys = DEFAULT_SIGNING_KEYS,
Timothy Trippelb867c1c2022-08-03 23:42:16 -0700881 signed = False,
Chris Frantzbac04542022-06-16 09:57:29 -0700882 manifest = None,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800883 **kwargs):
884 """A helper macro for generating OpenTitan binary artifacts for flash.
885
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700886 This macro is mostly a wrapper around the `opentitan_binary` macro, but also
887 creates artifacts for each of the keys in `PER_DEVICE_DEPS`, and if signing
888 is enabled, each of the keys in `signing_keys`. The actual artifacts created
889 artifacts created are outputs of the rules emitted by the `opentitan_binary`
890 macro and those listed below.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800891 Args:
892 @param name: The name of this rule.
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700893 @param devices: List of devices to build the target for.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800894 @param platform: The target platform for the artifacts.
895 @param signing_keys: The signing keys for to sign each BIN file with.
Timothy Trippelb867c1c2022-08-03 23:42:16 -0700896 @param signed: Whether or not to emit signed binary/VMEM files.
Timothy Trippel49cd1a12022-08-10 14:11:51 -0700897 @param manifest: Partially populated manifest to set boot stage/slot configs.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800898 @param **kwargs: Arguments to forward to `opentitan_binary`.
899 Emits rules:
900 For each device in per_device_deps entry:
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700901 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
902 bin_to_vmem named: <name>_<device>_vmem64
903 scrambled_flash_vmem named: <name>_<device>_scr_vmem64
904 Optionally:
Timothy Trippele3a7c572022-02-17 17:30:47 -0800905 sign_bin named: <name>_<device>_bin_signed_<key_name>
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700906 bin_to_vmem named: <name>_<device>_vmem64_signed_<key_name>
907 scrambled_flash_vmem named: <name>_<device>_scr_vmem64_signed_<key_name>
908 filegroup named: <name>_<device>
909 Containing all targets for a single device for the above generated rules.
910 filegroup named: <name>
911 Containing all targets across all devices for the above generated rules.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800912 """
Timothy Trippele3a7c572022-02-17 17:30:47 -0800913 deps = kwargs.pop("deps", [])
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700914 all_targets = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700915 for device in devices:
916 if device not in PER_DEVICE_DEPS:
917 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
918 dev_deps = PER_DEVICE_DEPS[device]
Timothy Trippele3a7c572022-02-17 17:30:47 -0800919 devname = "{}_{}".format(name, device)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700920 dev_targets = []
Timothy Trippele3a7c572022-02-17 17:30:47 -0800921
Alphan Ulusoy96725992022-06-27 14:56:59 -0400922 depname = "{}_deps".format(devname)
923 pick_correct_archive_for_device(
924 name = depname,
925 deps = deps + dev_deps,
926 device = device,
927 )
928
Timothy Trippel1f974b32022-03-28 16:07:09 -0700929 # Generate ELF, Binary, Disassembly, and (maybe) sim_dv logs database
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700930 dev_targets.extend(opentitan_binary(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800931 name = devname,
Alphan Ulusoy96725992022-06-27 14:56:59 -0400932 deps = [depname],
Alphan Ulusoye77043f2022-08-29 10:23:46 -0400933 extract_sw_logs_db = device == "sim_dv",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800934 **kwargs
935 ))
Timothy Trippele3a7c572022-02-17 17:30:47 -0800936 bin_name = "{}_{}".format(devname, "bin")
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000937
Timothy Trippelba061ed2022-03-23 00:30:22 -0700938 # Sign BIN (if required) and generate scrambled VMEM images.
Timothy Trippelb867c1c2022-08-03 23:42:16 -0700939 if signed:
Timothy Trippel49cd1a12022-08-10 14:11:51 -0700940 if manifest == None:
941 fail("A 'manifest' must be provided in order to sign flash images.")
Timothy Trippel92173aa2022-02-15 23:41:41 -0800942 for (key_name, key) in signing_keys.items():
Timothy Trippele3a7c572022-02-17 17:30:47 -0800943 # Sign the Binary.
944 signed_bin_name = "{}_bin_signed_{}".format(devname, key_name)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700945 dev_targets.append(":" + signed_bin_name)
Timothy Trippele3a7c572022-02-17 17:30:47 -0800946 sign_bin(
947 name = signed_bin_name,
948 bin = bin_name,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800949 key = key,
950 key_name = key_name,
Chris Frantzbac04542022-06-16 09:57:29 -0700951 manifest = manifest,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800952 )
953
Timothy Trippel77c09d52022-09-02 12:22:02 -0700954 # We only need to generate VMEM files for sim devices.
955 if device in ["sim_dv", "sim_verilator"]:
956 # Generate a VMEM64 from the signed binary.
957 signed_vmem_name = "{}_vmem64_signed_{}".format(
958 devname,
959 key_name,
960 )
961 dev_targets.append(":" + signed_vmem_name)
962 bin_to_vmem(
963 name = signed_vmem_name,
964 bin = signed_bin_name,
965 platform = platform,
966 word_size = 64, # Backdoor-load VMEM image uses 64-bit words
967 )
Timothy Trippele3a7c572022-02-17 17:30:47 -0800968
Timothy Trippel77c09d52022-09-02 12:22:02 -0700969 # Scramble signed VMEM64.
970 scr_signed_vmem_name = "{}_scr_vmem64_signed_{}".format(
971 devname,
972 key_name,
973 )
974 dev_targets.append(":" + scr_signed_vmem_name)
975 scramble_flash_vmem(
976 name = scr_signed_vmem_name,
977 vmem = signed_vmem_name,
978 platform = platform,
979 )
Timothy Trippelba061ed2022-03-23 00:30:22 -0700980
Timothy Trippel77c09d52022-09-02 12:22:02 -0700981 # We only need to generate VMEM files for sim devices.
982 if device in ["sim_dv", "sim_verilator"]:
983 # Generate a VMEM64 from the binary.
984 vmem_name = "{}_vmem64".format(devname)
985 dev_targets.append(":" + vmem_name)
986 bin_to_vmem(
987 name = vmem_name,
988 bin = bin_name,
989 platform = platform,
990 word_size = 64, # Backdoor-load VMEM image uses 64-bit words
991 )
Timothy Trippelb867c1c2022-08-03 23:42:16 -0700992
Timothy Trippel77c09d52022-09-02 12:22:02 -0700993 # Scramble VMEM64.
994 scr_vmem_name = "{}_scr_vmem64".format(devname)
995 dev_targets.append(":" + scr_vmem_name)
996 scramble_flash_vmem(
997 name = scr_vmem_name,
998 vmem = vmem_name,
999 platform = platform,
1000 )
Timothy Trippele3a7c572022-02-17 17:30:47 -08001001
Timothy Trippel3821d5d2022-08-02 10:20:49 -07001002 # Create a filegroup with just the current device's targets.
1003 native.filegroup(
1004 name = devname,
1005 srcs = dev_targets,
1006 )
1007 all_targets.extend(dev_targets)
1008
1009 # Create a filegroup with just all targets from all devices.
Drew Macrae7ac1efb2021-09-22 16:08:28 +00001010 native.filegroup(
1011 name = name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -07001012 srcs = all_targets,
Drew Macrae7ac1efb2021-09-22 16:08:28 +00001013 )
Alphan Ulusoy96725992022-06-27 14:56:59 -04001014
1015def opentitan_ram_binary(
1016 name,
Timothy Trippel90c0de72022-08-05 17:18:23 -07001017 archive_symbol_prefix,
Timothy Trippel3e3f4602022-09-02 14:30:45 -07001018 devices = PER_DEVICE_DEPS.keys(),
Alphan Ulusoy96725992022-06-27 14:56:59 -04001019 platform = OPENTITAN_PLATFORM,
Alphan Ulusoy96725992022-06-27 14:56:59 -04001020 **kwargs):
1021 """A helper macro for generating OpenTitan binary artifacts for RAM.
1022
Timothy Trippel90c0de72022-08-05 17:18:23 -07001023 This macro is mostly a wrapper around the `opentitan_binary` macro, but also
1024 creates artifacts for each of the keys in `PER_DEVICE_DEPS`. The actual
1025 artifacts created are outputs of the rules emitted by the `opentitan_binary`
1026 macro and those listed below.
Alphan Ulusoy96725992022-06-27 14:56:59 -04001027 Args:
1028 @param name: The name of this rule.
Timothy Trippel90c0de72022-08-05 17:18:23 -07001029 @param archive_symbol_prefix: Prefix used to rename symbols in the binary.
Timothy Trippel3e3f4602022-09-02 14:30:45 -07001030 @param devices: List of devices to build the target for.
1031 @param platform: The target platform for the artifacts.
Alphan Ulusoy96725992022-06-27 14:56:59 -04001032 @param **kwargs: Arguments to forward to `opentitan_binary`.
1033 Emits rules:
1034 For each device in per_device_deps entry:
Timothy Trippel90c0de72022-08-05 17:18:23 -07001035 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
1036 bin_to_archive named: <name>
Alphan Ulusoy96725992022-06-27 14:56:59 -04001037 """
Alphan Ulusoy96725992022-06-27 14:56:59 -04001038 deps = kwargs.pop("deps", [])
1039 hdrs = kwargs.pop("hdrs", [])
Alphan Ulusoy96725992022-06-27 14:56:59 -04001040 binaries = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -07001041 for device in devices:
1042 if device not in PER_DEVICE_DEPS:
1043 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
1044 dev_deps = PER_DEVICE_DEPS[device]
Alphan Ulusoy96725992022-06-27 14:56:59 -04001045 devname = "{}_{}".format(name, device)
1046
Timothy Trippel90c0de72022-08-05 17:18:23 -07001047 # Generate the binary.
1048 opentitan_binary(
Alphan Ulusoy96725992022-06-27 14:56:59 -04001049 name = devname,
1050 deps = deps + dev_deps,
1051 extract_sw_logs_db = False,
1052 **kwargs
Timothy Trippel90c0de72022-08-05 17:18:23 -07001053 )
Alphan Ulusoy96725992022-06-27 14:56:59 -04001054 bin_name = "{}_{}".format(devname, "bin")
1055 binaries.append(":" + bin_name)
1056
Alphan Ulusoy96725992022-06-27 14:56:59 -04001057 # Generate the archive file.
Alphan Ulusoy96725992022-06-27 14:56:59 -04001058 bin_to_archive(
Timothy Trippel90c0de72022-08-05 17:18:23 -07001059 name = name,
Alphan Ulusoy96725992022-06-27 14:56:59 -04001060 hdrs = hdrs,
1061 binaries = binaries,
Timothy Trippel90c0de72022-08-05 17:18:23 -07001062 devices = PER_DEVICE_DEPS.keys(),
Alphan Ulusoy96725992022-06-27 14:56:59 -04001063 archive_symbol_prefix = archive_symbol_prefix,
1064 )