blob: f4f89280aa3dfd7ed24359465e59349dda9f3d4e [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",
416 )
417 return [DefaultInfo(
418 files = depset(outputs),
419 data_runfiles = ctx.runfiles(files = outputs),
420 )]
421
Miles Dai1634fac2022-05-13 17:55:35 -0400422bin_to_vmem = rv_rule(
423 implementation = _bin_to_vmem_impl,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800424 attrs = {
425 "bin": attr.label(allow_single_file = True),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800426 "word_size": attr.int(
427 default = 64,
428 doc = "Word size of VMEM file.",
429 mandatory = True,
430 values = [32, 64],
431 ),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800432 },
433)
434
435def _scramble_flash_vmem_impl(ctx):
436 outputs = []
437 scrambled_vmem = ctx.actions.declare_file("{}.scr.vmem".format(
438 # Remove ".vmem" from file basename.
Timothy Trippelf17bca82022-03-08 11:12:41 -0800439 ctx.file.vmem.basename.replace("." + ctx.file.vmem.extension, ""),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800440 ))
441 outputs.append(scrambled_vmem)
442 ctx.actions.run(
443 outputs = [scrambled_vmem],
444 inputs = [
445 ctx.file.vmem,
Timothy Trippel4a903632022-04-15 15:04:39 -0700446 ctx.executable._tool,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800447 ],
448 arguments = [
449 ctx.file.vmem.path,
450 scrambled_vmem.path,
451 ],
Timothy Trippel4a903632022-04-15 15:04:39 -0700452 executable = ctx.executable._tool,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800453 )
454 return [DefaultInfo(
455 files = depset(outputs),
456 data_runfiles = ctx.runfiles(files = outputs),
457 )]
458
Miguel Young de la Sota3b5a9f52022-03-24 16:11:42 -0400459scramble_flash_vmem = rv_rule(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800460 implementation = _scramble_flash_vmem_impl,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800461 attrs = {
462 "vmem": attr.label(allow_single_file = True),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800463 "_tool": attr.label(
Timothy Trippeld2277d22022-06-02 00:36:08 -0700464 default = "@//util/design:gen-flash-img",
Timothy Trippel4a903632022-04-15 15:04:39 -0700465 executable = True,
466 cfg = "exec",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800467 ),
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000468 },
469)
470
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700471def _gen_sim_dv_logs_db_impl(ctx):
472 outputs = []
473 for src in ctx.files.srcs:
474 if src.extension != "elf":
475 fail("can only generate DV logs database files from ELF files.")
476 logs_db = ctx.actions.declare_file("{}.logs.txt".format(
477 src.basename.replace("." + src.extension, ""),
478 ))
479 rodata = ctx.actions.declare_file("{}.rodata.txt".format(
480 src.basename.replace("." + src.extension, ""),
481 ))
482 outputs.append(logs_db)
483 outputs.append(rodata)
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700484
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700485 ctx.actions.run(
486 outputs = outputs,
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700487 inputs = [src],
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700488 arguments = [
489 "--elf-file",
490 src.path,
491 "--rodata-sections",
492 ".rodata",
493 "--logs-fields-section",
494 ".logs.fields",
495 "--name",
496 src.basename.replace("." + src.extension, ""),
497 "--outdir",
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700498 logs_db.dirname,
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700499 ],
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700500 executable = ctx.executable._tool,
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700501 )
502 return [DefaultInfo(
503 files = depset(outputs),
504 data_runfiles = ctx.runfiles(files = outputs),
505 )]
506
507gen_sim_dv_logs_db = rule(
508 implementation = _gen_sim_dv_logs_db_impl,
509 cfg = opentitan_transition,
510 attrs = {
511 "srcs": attr.label_list(allow_files = True),
512 "platform": attr.string(default = OPENTITAN_PLATFORM),
513 "_tool": attr.label(
Timothy Trippeld2277d22022-06-02 00:36:08 -0700514 default = "@//util/device_sw_utils:extract_sw_logs_db",
Timothy Trippeldea42da2022-04-15 14:30:01 -0700515 cfg = "exec",
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700516 executable = True,
Timothy Trippel8c421dd2022-03-28 15:04:56 -0700517 ),
518 "_allowlist_function_transition": attr.label(
519 default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
520 ),
521 },
522)
523
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700524def _assemble_flash_image_impl(ctx):
525 output = ctx.actions.declare_file(ctx.attr.output)
526 outputs = [output]
527 inputs = []
528 arguments = [
529 "image",
530 "assemble",
531 "--mirror",
532 "false",
533 "--output",
534 output.path,
535 "--size",
536 ctx.attr.image_size,
537 ]
538 for binary, offset in ctx.attr.binaries.items():
539 inputs.extend(binary.files.to_list())
540 arguments.append("{}@{}".format(binary.files.to_list()[0].path, offset))
541 ctx.actions.run(
542 outputs = outputs,
543 inputs = inputs,
544 arguments = arguments,
545 executable = ctx.executable._opentitantool,
546 )
547 return [DefaultInfo(
548 files = depset(outputs),
549 data_runfiles = ctx.runfiles(files = outputs),
550 )]
551
552assemble_flash_image = rv_rule(
553 implementation = _assemble_flash_image_impl,
554 attrs = {
555 "image_size": attr.string(),
556 "output": attr.string(),
557 "binaries": attr.label_keyed_string_dict(allow_empty = False),
558 "_opentitantool": attr.label(
559 default = "//sw/host/opentitantool:opentitantool",
560 allow_single_file = True,
561 executable = True,
562 cfg = "exec",
563 ),
564 },
565)
566
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000567def opentitan_binary(
568 name,
569 platform = OPENTITAN_PLATFORM,
Timothy Trippel5ef8a532022-03-29 09:55:03 -0700570 extract_sw_logs_db = False,
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000571 **kwargs):
572 """A helper macro for generating OpenTitan binary artifacts.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800573
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000574 This macro is mostly a wrapper around cc_binary, but creates artifacts
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700575 compatible with OpenTitan binaries. The actual artifacts created are outputs
576 of the rules listed below.
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000577 Args:
578 @param name: The name of this rule.
579 @param platform: The target platform for the artifacts.
Timothy Trippel5ef8a532022-03-29 09:55:03 -0700580 @param extract_sw_logs_db: Whether to emit a log database for DV testbench.
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000581 @param **kwargs: Arguments to forward to `cc_binary`.
582 Emits rules:
Timothy Trippelbc0af182022-08-16 18:46:11 -0700583 cc_binary named: <name>.elf
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700584 rv_preprocess named: <name>_preproc
585 rv_asm named: <name>_asm
586 rv_llvm_ir named: <name>_ll
587 rv_relink_with_map named: <name>_map
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700588 obj_transform named: <name>_bin
589 elf_to_dissassembly named: <name>_dis
590 Optionally:
591 gen_sim_dv_logs_db named: <name>_logs_db
592 Returns:
593 List of targets generated by all of the above rules.
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000594 """
595
596 copts = kwargs.pop("copts", []) + [
597 "-nostdlib",
598 "-ffreestanding",
599 ]
600 linkopts = kwargs.pop("linkopts", []) + [
601 "-nostartfiles",
602 "-nostdlib",
603 ]
604 deps = kwargs.pop("deps", [])
605 targets = []
Timothy Trippelddcc7992022-08-05 13:47:32 -0700606 side_targets = []
Timothy Trippele3a7c572022-02-17 17:30:47 -0800607
Timothy Trippel36f96542022-08-02 18:31:20 -0700608 native_binary_name = "{}.elf".format(name)
Timothy Trippele3a7c572022-02-17 17:30:47 -0800609 native.cc_binary(
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700610 name = native_binary_name,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800611 deps = deps,
612 target_compatible_with = _targets_compatible_with[platform],
613 copts = copts,
614 linkopts = linkopts,
615 **kwargs
616 )
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400617
Miguel Young de la Sota7bb7fe72022-04-01 16:40:02 -0400618 preproc_name = "{}_{}".format(name, "preproc")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700619 side_targets.append(preproc_name)
Miguel Young de la Sota7bb7fe72022-04-01 16:40:02 -0400620 rv_preprocess(
621 name = preproc_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700622 target = native_binary_name,
Miguel Young de la Sota7bb7fe72022-04-01 16:40:02 -0400623 )
624
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400625 asm_name = "{}_{}".format(name, "asm")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700626 side_targets.append(asm_name)
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400627 rv_asm(
628 name = asm_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700629 target = native_binary_name,
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400630 )
631
632 ll_name = "{}_{}".format(name, "ll")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700633 side_targets.append(ll_name)
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400634 rv_llvm_ir(
635 name = ll_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700636 target = native_binary_name,
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400637 )
638
639 map_name = "{}_{}".format(name, "map")
Timothy Trippelddcc7992022-08-05 13:47:32 -0700640 side_targets.append(map_name)
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400641 rv_relink_with_linkmap(
642 name = map_name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700643 target = native_binary_name,
Miguel Young de la Sota4bfbba32022-03-24 16:12:14 -0400644 )
645
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700646 bin_name = "{}_{}".format(name, "bin")
647 targets.append(":" + bin_name)
648 obj_transform(
649 name = bin_name,
650 srcs = [native_binary_name],
651 platform = platform,
652 )
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000653
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700654 dis_name = "{}_{}".format(name, "dis")
655 targets.append(":" + dis_name)
656 elf_to_disassembly(
657 name = dis_name,
658 srcs = [native_binary_name],
659 platform = platform,
660 )
Timothy Trippele3a7c572022-02-17 17:30:47 -0800661
Timothy Trippel1f974b32022-03-28 16:07:09 -0700662 # Generate log message database for DV sim testbench
Timothy Trippel5ef8a532022-03-29 09:55:03 -0700663 if extract_sw_logs_db:
Timothy Trippel6dd8b222022-03-29 23:09:02 -0700664 logs_db_name = "{}_{}".format(name, "logs_db")
Timothy Trippel1f974b32022-03-28 16:07:09 -0700665 targets.append(":" + logs_db_name)
666 gen_sim_dv_logs_db(
667 name = logs_db_name,
Timothy Trippel36f96542022-08-02 18:31:20 -0700668 srcs = [native_binary_name],
Timothy Trippel1f974b32022-03-28 16:07:09 -0700669 platform = platform,
670 )
671
Timothy Trippelddcc7992022-08-05 13:47:32 -0700672 # Create a filegroup with just the sides targets.
673 native.filegroup(
674 name = name + "_side_targets",
675 srcs = side_targets,
676 )
677
Timothy Trippele3a7c572022-02-17 17:30:47 -0800678 return targets
679
680def opentitan_rom_binary(
681 name,
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700682 devices = PER_DEVICE_DEPS.keys(),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800683 platform = OPENTITAN_PLATFORM,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800684 **kwargs):
685 """A helper macro for generating OpenTitan binary artifacts for ROM.
686
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700687 This macro is mostly a wrapper around the `opentitan_binary` macro, but also
688 creates artifacts for each of the keys in `PER_DEVICE_DEPS`. The actual
689 artifacts created are outputs of the rules emitted by the `opentitan_binary`
690 macro and those listed below.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800691 Args:
692 @param name: The name of this rule.
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700693 @param devices: List of devices to build the target for.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800694 @param platform: The target platform for the artifacts.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800695 @param **kwargs: Arguments to forward to `opentitan_binary`.
696 Emits rules:
697 For each device in per_device_deps entry:
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700698 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
699 bin_to_rom_vmem named: <name>_<device>_vmem
700 elf_to_scrambled_rom_vmem named: <name>_<device>_scr_vmem
701 filegroup named: <name>_<device>
702 Containing all targets for a single device for the above generated rules.
703 filegroup named: <name>
704 Containing all targets across all devices for the above generated rules.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800705 """
Timothy Trippele3a7c572022-02-17 17:30:47 -0800706 deps = kwargs.pop("deps", [])
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700707 all_targets = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700708 for device in devices:
709 if device not in PER_DEVICE_DEPS:
710 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
711 dev_deps = PER_DEVICE_DEPS[device]
Timothy Trippele3a7c572022-02-17 17:30:47 -0800712 devname = "{}_{}".format(name, device)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700713 dev_targets = []
Timothy Trippele3a7c572022-02-17 17:30:47 -0800714
Timothy Trippel1f974b32022-03-28 16:07:09 -0700715 # Generate ELF, Binary, Disassembly, and (maybe) sim_dv logs database
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700716 dev_targets.extend(opentitan_binary(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800717 name = devname,
718 deps = deps + dev_deps,
Alphan Ulusoye77043f2022-08-29 10:23:46 -0400719 extract_sw_logs_db = device == "sim_dv",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800720 **kwargs
721 ))
Timothy Trippel77c09d52022-09-02 12:22:02 -0700722
723 # We need to generate VMEM files even for FPGA devices, because we use
724 # them for bitstream splicing.
Timothy Trippel36f96542022-08-02 18:31:20 -0700725 elf_name = "{}.{}".format(devname, "elf")
Miles Dai1634fac2022-05-13 17:55:35 -0400726 bin_name = "{}_{}".format(devname, "bin")
727
728 # Generate Un-scrambled ROM VMEM
729 vmem_name = "{}_vmem".format(devname)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700730 dev_targets.append(":" + vmem_name)
Miles Dai1634fac2022-05-13 17:55:35 -0400731 bin_to_vmem(
732 name = vmem_name,
733 bin = bin_name,
734 platform = platform,
Miles Dai91d811e2022-05-10 15:51:17 -0400735 word_size = 32,
Miles Dai1634fac2022-05-13 17:55:35 -0400736 )
Timothy Trippele3a7c572022-02-17 17:30:47 -0800737
738 # Generate Scrambled ROM VMEM
739 scr_vmem_name = "{}_scr_vmem".format(devname)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700740 dev_targets.append(":" + scr_vmem_name)
Timothy Trippele3a7c572022-02-17 17:30:47 -0800741 elf_to_scrambled_rom_vmem(
742 name = scr_vmem_name,
743 srcs = [elf_name],
744 platform = platform,
745 )
746
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700747 # Create a filegroup with just the current device's targets.
748 native.filegroup(
749 name = devname,
750 srcs = dev_targets,
751 )
752 all_targets.extend(dev_targets)
753
754 # Create a filegroup with just all targets from all devices.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800755 native.filegroup(
756 name = name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700757 srcs = all_targets,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800758 )
759
Alphan Ulusoy96725992022-06-27 14:56:59 -0400760def _pick_correct_archive_for_device(ctx):
761 cc_infos = []
762 for dep in ctx.attr.deps:
763 if CcInfo in dep:
764 cc_info = dep[CcInfo]
765 elif ArchiveInfo in dep:
766 cc_info = dep[ArchiveInfo].archive_infos[ctx.attr.device]
767 else:
768 fail("Expected either a CcInfo or an ArchiveInfo")
769 cc_infos.append(cc_info)
770 return [cc_common.merge_cc_infos(cc_infos = cc_infos)]
771
772pick_correct_archive_for_device = rv_rule(
773 implementation = _pick_correct_archive_for_device,
774 attrs = {
775 "deps": attr.label_list(allow_files = True),
776 "device": attr.string(),
777 },
778 fragments = ["cpp"],
779 toolchains = ["@rules_cc//cc:toolchain_type"],
780)
781
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700782def opentitan_multislot_flash_binary(
783 name,
784 srcs,
785 image_size,
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700786 devices = PER_DEVICE_DEPS.keys(),
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700787 platform = OPENTITAN_PLATFORM):
788 """A helper macro for generating multislot OpenTitan binary flash images.
789
790 This macro is mostly a wrapper around the `assemble_flash_image` rule, that
791 invokes `opentitantool` to stitch together multiple `opentitan_flash_binary`
Timothy Trippelfcc8bac2022-08-23 17:20:24 -0700792 images to create a single image for bootstrapping. Since bootstrap erases
793 the flash for programming this is the only way to load multiple
794 (A/B/Virtual) slots and (silicon creator, ROM_EXT, and owner, BL0) stages at
795 the same time.
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700796 Args:
797 @param name: The name of this rule.
798 @param srcs: A dictionary of `opentitan_flash_binary` targets (to stitch
799 together) as keys, and key/offset configurations as values.
800 @param image_size: The final flash image_size to pass to `opentitantool`.
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700801 @param devices: List of devices to build the target for.
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700802 @param platform: The target platform for the artifacts.
803 Emits rules:
804 For each device in per_device_deps entry:
805 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
806 assemble_flash_image named: <name>_<device>_bin_signed
807 bin_to_vmem named: <name>_<device>_vmem64_signed
808 scrambled_flash_vmem named: <name>_<device>_scr_vmem64_signed
809 filegroup named: <name>_<device>
810 Containing all targets for a single device for the above generated rules.
811 filegroup named: <name>
812 Containing all targets across all devices for the above generated rules.
813 """
814 all_targets = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700815 for device in devices:
816 if device not in PER_DEVICE_DEPS:
817 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700818 devname = "{}_{}".format(name, device)
819 dev_targets = []
820 signed_dev_binaries = {}
821 for src, configs in srcs.items():
822 if "key" not in configs:
823 fail("Missing signing key for binary: {}".format(src))
824 if "offset" not in configs:
825 fail("Missing offset for binary: {}".format(src))
826 signed_dev_binary = "{}_{}_bin_signed_{}".format(
827 src,
828 device,
829 configs["key"],
830 )
831 signed_dev_binaries[signed_dev_binary] = configs["offset"]
832
833 # Assemble the signed binaries into a single binary.
834 signed_bin_name = "{}_bin_signed".format(devname)
835 dev_targets.append(":" + signed_bin_name)
836 assemble_flash_image(
837 name = signed_bin_name,
838 output = "{}.signed.bin".format(devname),
839 image_size = image_size,
840 binaries = signed_dev_binaries,
841 )
842
Timothy Trippel77c09d52022-09-02 12:22:02 -0700843 # We only need to generate VMEM files for sim devices.
844 if device in ["sim_dv", "sim_verilator"]:
845 # Generate a VMEM64 from the binary.
846 signed_vmem_name = "{}_vmem64_signed".format(devname)
847 dev_targets.append(":" + signed_vmem_name)
848 bin_to_vmem(
849 name = signed_vmem_name,
850 bin = signed_bin_name,
851 platform = platform,
852 word_size = 64, # Backdoor-load VMEM image uses 64-bit words
853 )
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700854
Timothy Trippel77c09d52022-09-02 12:22:02 -0700855 # Scramble signed VMEM64.
856 scr_signed_vmem_name = "{}_scr_vmem64_signed".format(devname)
857 dev_targets.append(":" + scr_signed_vmem_name)
858 scramble_flash_vmem(
859 name = scr_signed_vmem_name,
860 vmem = signed_vmem_name,
861 platform = platform,
862 )
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700863
864 # Create a filegroup with just the current device's targets.
865 native.filegroup(
866 name = devname,
867 srcs = dev_targets,
868 )
869 dev_targets.extend(dev_targets)
870
871 # Create a filegroup with all assembled flash images.
872 native.filegroup(
873 name = name,
874 srcs = all_targets,
875 )
876
Timothy Trippele3a7c572022-02-17 17:30:47 -0800877def opentitan_flash_binary(
878 name,
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700879 devices = PER_DEVICE_DEPS.keys(),
Timothy Trippele3a7c572022-02-17 17:30:47 -0800880 platform = OPENTITAN_PLATFORM,
Timothy Trippel1ad4ad62022-08-23 17:01:28 -0700881 signing_keys = DEFAULT_SIGNING_KEYS,
Timothy Trippelb867c1c2022-08-03 23:42:16 -0700882 signed = False,
Chris Frantzbac04542022-06-16 09:57:29 -0700883 manifest = None,
Timothy Trippele3a7c572022-02-17 17:30:47 -0800884 **kwargs):
885 """A helper macro for generating OpenTitan binary artifacts for flash.
886
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700887 This macro is mostly a wrapper around the `opentitan_binary` macro, but also
888 creates artifacts for each of the keys in `PER_DEVICE_DEPS`, and if signing
889 is enabled, each of the keys in `signing_keys`. The actual artifacts created
890 artifacts created are outputs of the rules emitted by the `opentitan_binary`
891 macro and those listed below.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800892 Args:
893 @param name: The name of this rule.
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700894 @param devices: List of devices to build the target for.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800895 @param platform: The target platform for the artifacts.
896 @param signing_keys: The signing keys for to sign each BIN file with.
Timothy Trippelb867c1c2022-08-03 23:42:16 -0700897 @param signed: Whether or not to emit signed binary/VMEM files.
Timothy Trippel49cd1a12022-08-10 14:11:51 -0700898 @param manifest: Partially populated manifest to set boot stage/slot configs.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800899 @param **kwargs: Arguments to forward to `opentitan_binary`.
900 Emits rules:
901 For each device in per_device_deps entry:
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700902 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
903 bin_to_vmem named: <name>_<device>_vmem64
904 scrambled_flash_vmem named: <name>_<device>_scr_vmem64
905 Optionally:
Timothy Trippele3a7c572022-02-17 17:30:47 -0800906 sign_bin named: <name>_<device>_bin_signed_<key_name>
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700907 bin_to_vmem named: <name>_<device>_vmem64_signed_<key_name>
908 scrambled_flash_vmem named: <name>_<device>_scr_vmem64_signed_<key_name>
909 filegroup named: <name>_<device>
910 Containing all targets for a single device for the above generated rules.
911 filegroup named: <name>
912 Containing all targets across all devices for the above generated rules.
Timothy Trippele3a7c572022-02-17 17:30:47 -0800913 """
Timothy Trippele3a7c572022-02-17 17:30:47 -0800914 deps = kwargs.pop("deps", [])
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700915 all_targets = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -0700916 for device in devices:
917 if device not in PER_DEVICE_DEPS:
918 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
919 dev_deps = PER_DEVICE_DEPS[device]
Timothy Trippele3a7c572022-02-17 17:30:47 -0800920 devname = "{}_{}".format(name, device)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700921 dev_targets = []
Timothy Trippele3a7c572022-02-17 17:30:47 -0800922
Alphan Ulusoy96725992022-06-27 14:56:59 -0400923 depname = "{}_deps".format(devname)
924 pick_correct_archive_for_device(
925 name = depname,
926 deps = deps + dev_deps,
927 device = device,
928 )
929
Timothy Trippel1f974b32022-03-28 16:07:09 -0700930 # Generate ELF, Binary, Disassembly, and (maybe) sim_dv logs database
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700931 dev_targets.extend(opentitan_binary(
Timothy Trippele3a7c572022-02-17 17:30:47 -0800932 name = devname,
Alphan Ulusoy96725992022-06-27 14:56:59 -0400933 deps = [depname],
Alphan Ulusoye77043f2022-08-29 10:23:46 -0400934 extract_sw_logs_db = device == "sim_dv",
Timothy Trippele3a7c572022-02-17 17:30:47 -0800935 **kwargs
936 ))
Timothy Trippele3a7c572022-02-17 17:30:47 -0800937 bin_name = "{}_{}".format(devname, "bin")
Drew Macrae7ac1efb2021-09-22 16:08:28 +0000938
Timothy Trippelba061ed2022-03-23 00:30:22 -0700939 # Sign BIN (if required) and generate scrambled VMEM images.
Timothy Trippelb867c1c2022-08-03 23:42:16 -0700940 if signed:
Timothy Trippel49cd1a12022-08-10 14:11:51 -0700941 if manifest == None:
942 fail("A 'manifest' must be provided in order to sign flash images.")
Timothy Trippel92173aa2022-02-15 23:41:41 -0800943 for (key_name, key) in signing_keys.items():
Timothy Trippele3a7c572022-02-17 17:30:47 -0800944 # Sign the Binary.
945 signed_bin_name = "{}_bin_signed_{}".format(devname, key_name)
Timothy Trippel3821d5d2022-08-02 10:20:49 -0700946 dev_targets.append(":" + signed_bin_name)
Timothy Trippele3a7c572022-02-17 17:30:47 -0800947 sign_bin(
948 name = signed_bin_name,
949 bin = bin_name,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800950 key = key,
951 key_name = key_name,
Chris Frantzbac04542022-06-16 09:57:29 -0700952 manifest = manifest,
Timothy Trippel92173aa2022-02-15 23:41:41 -0800953 )
954
Timothy Trippel77c09d52022-09-02 12:22:02 -0700955 # We only need to generate VMEM files for sim devices.
956 if device in ["sim_dv", "sim_verilator"]:
957 # Generate a VMEM64 from the signed binary.
958 signed_vmem_name = "{}_vmem64_signed_{}".format(
959 devname,
960 key_name,
961 )
962 dev_targets.append(":" + signed_vmem_name)
963 bin_to_vmem(
964 name = signed_vmem_name,
965 bin = signed_bin_name,
966 platform = platform,
967 word_size = 64, # Backdoor-load VMEM image uses 64-bit words
968 )
Timothy Trippele3a7c572022-02-17 17:30:47 -0800969
Timothy Trippel77c09d52022-09-02 12:22:02 -0700970 # Scramble signed VMEM64.
971 scr_signed_vmem_name = "{}_scr_vmem64_signed_{}".format(
972 devname,
973 key_name,
974 )
975 dev_targets.append(":" + scr_signed_vmem_name)
976 scramble_flash_vmem(
977 name = scr_signed_vmem_name,
978 vmem = signed_vmem_name,
979 platform = platform,
980 )
Timothy Trippelba061ed2022-03-23 00:30:22 -0700981
Timothy Trippel77c09d52022-09-02 12:22:02 -0700982 # We only need to generate VMEM files for sim devices.
983 if device in ["sim_dv", "sim_verilator"]:
984 # Generate a VMEM64 from the binary.
985 vmem_name = "{}_vmem64".format(devname)
986 dev_targets.append(":" + vmem_name)
987 bin_to_vmem(
988 name = vmem_name,
989 bin = bin_name,
990 platform = platform,
991 word_size = 64, # Backdoor-load VMEM image uses 64-bit words
992 )
Timothy Trippelb867c1c2022-08-03 23:42:16 -0700993
Timothy Trippel77c09d52022-09-02 12:22:02 -0700994 # Scramble VMEM64.
995 scr_vmem_name = "{}_scr_vmem64".format(devname)
996 dev_targets.append(":" + scr_vmem_name)
997 scramble_flash_vmem(
998 name = scr_vmem_name,
999 vmem = vmem_name,
1000 platform = platform,
1001 )
Timothy Trippele3a7c572022-02-17 17:30:47 -08001002
Timothy Trippel3821d5d2022-08-02 10:20:49 -07001003 # Create a filegroup with just the current device's targets.
1004 native.filegroup(
1005 name = devname,
1006 srcs = dev_targets,
1007 )
1008 all_targets.extend(dev_targets)
1009
1010 # Create a filegroup with just all targets from all devices.
Drew Macrae7ac1efb2021-09-22 16:08:28 +00001011 native.filegroup(
1012 name = name,
Timothy Trippel3821d5d2022-08-02 10:20:49 -07001013 srcs = all_targets,
Drew Macrae7ac1efb2021-09-22 16:08:28 +00001014 )
Alphan Ulusoy96725992022-06-27 14:56:59 -04001015
1016def opentitan_ram_binary(
1017 name,
Timothy Trippel90c0de72022-08-05 17:18:23 -07001018 archive_symbol_prefix,
Timothy Trippel3e3f4602022-09-02 14:30:45 -07001019 devices = PER_DEVICE_DEPS.keys(),
Alphan Ulusoy96725992022-06-27 14:56:59 -04001020 platform = OPENTITAN_PLATFORM,
Alphan Ulusoy96725992022-06-27 14:56:59 -04001021 **kwargs):
1022 """A helper macro for generating OpenTitan binary artifacts for RAM.
1023
Timothy Trippel90c0de72022-08-05 17:18:23 -07001024 This macro is mostly a wrapper around the `opentitan_binary` macro, but also
1025 creates artifacts for each of the keys in `PER_DEVICE_DEPS`. The actual
1026 artifacts created are outputs of the rules emitted by the `opentitan_binary`
1027 macro and those listed below.
Alphan Ulusoy96725992022-06-27 14:56:59 -04001028 Args:
1029 @param name: The name of this rule.
Timothy Trippel90c0de72022-08-05 17:18:23 -07001030 @param archive_symbol_prefix: Prefix used to rename symbols in the binary.
Timothy Trippel3e3f4602022-09-02 14:30:45 -07001031 @param devices: List of devices to build the target for.
1032 @param platform: The target platform for the artifacts.
Alphan Ulusoy96725992022-06-27 14:56:59 -04001033 @param **kwargs: Arguments to forward to `opentitan_binary`.
1034 Emits rules:
1035 For each device in per_device_deps entry:
Timothy Trippel90c0de72022-08-05 17:18:23 -07001036 rules emitted by `opentitan_binary` named: see `opentitan_binary` macro
1037 bin_to_archive named: <name>
Alphan Ulusoy96725992022-06-27 14:56:59 -04001038 """
Alphan Ulusoy96725992022-06-27 14:56:59 -04001039 deps = kwargs.pop("deps", [])
1040 hdrs = kwargs.pop("hdrs", [])
Alphan Ulusoy96725992022-06-27 14:56:59 -04001041 binaries = []
Timothy Trippel3e3f4602022-09-02 14:30:45 -07001042 for device in devices:
1043 if device not in PER_DEVICE_DEPS:
1044 fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys()))
1045 dev_deps = PER_DEVICE_DEPS[device]
Alphan Ulusoy96725992022-06-27 14:56:59 -04001046 devname = "{}_{}".format(name, device)
1047
Timothy Trippel90c0de72022-08-05 17:18:23 -07001048 # Generate the binary.
1049 opentitan_binary(
Alphan Ulusoy96725992022-06-27 14:56:59 -04001050 name = devname,
1051 deps = deps + dev_deps,
1052 extract_sw_logs_db = False,
1053 **kwargs
Timothy Trippel90c0de72022-08-05 17:18:23 -07001054 )
Alphan Ulusoy96725992022-06-27 14:56:59 -04001055 bin_name = "{}_{}".format(devname, "bin")
1056 binaries.append(":" + bin_name)
1057
Alphan Ulusoy96725992022-06-27 14:56:59 -04001058 # Generate the archive file.
Alphan Ulusoy96725992022-06-27 14:56:59 -04001059 bin_to_archive(
Timothy Trippel90c0de72022-08-05 17:18:23 -07001060 name = name,
Alphan Ulusoy96725992022-06-27 14:56:59 -04001061 hdrs = hdrs,
1062 binaries = binaries,
Timothy Trippel90c0de72022-08-05 17:18:23 -07001063 devices = PER_DEVICE_DEPS.keys(),
Alphan Ulusoy96725992022-06-27 14:56:59 -04001064 archive_symbol_prefix = archive_symbol_prefix,
1065 )