Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 1 | # 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 Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 5 | load("@rules_cc//cc:action_names.bzl", "ACTION_NAMES") |
Miguel Young de la Sota | 4b01c7a | 2022-04-21 17:25:52 -0400 | [diff] [blame] | 6 | load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain") |
Miguel Young de la Sota | 7bb7fe7 | 2022-04-01 16:40:02 -0400 | [diff] [blame] | 7 | load( |
Yen-Kai Wang | f3252d3 | 2022-06-08 12:33:58 -0500 | [diff] [blame] | 8 | "@lowrisc_opentitan//rules:cc_side_outputs.bzl", |
Miguel Young de la Sota | 7bb7fe7 | 2022-04-01 16:40:02 -0400 | [diff] [blame] | 9 | "rv_asm", |
| 10 | "rv_llvm_ir", |
| 11 | "rv_preprocess", |
| 12 | "rv_relink_with_linkmap", |
| 13 | ) |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 14 | load( |
Yen-Kai Wang | f3252d3 | 2022-06-08 12:33:58 -0500 | [diff] [blame] | 15 | "@lowrisc_opentitan//rules:rv.bzl", |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 16 | "rv_rule", |
| 17 | _OPENTITAN_CPU = "OPENTITAN_CPU", |
| 18 | _OPENTITAN_PLATFORM = "OPENTITAN_PLATFORM", |
| 19 | _opentitan_transition = "opentitan_transition", |
| 20 | ) |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 21 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 22 | """Rules to build OpenTitan for the RISC-V target""" |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 23 | |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 24 | # Re-exports of names from transition.bzl; many files in the repo use opentitan.bzl |
| 25 | # to get to them. |
| 26 | OPENTITAN_CPU = _OPENTITAN_CPU |
| 27 | OPENTITAN_PLATFORM = _OPENTITAN_PLATFORM |
| 28 | opentitan_transition = _opentitan_transition |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 29 | |
| 30 | _targets_compatible_with = { |
| 31 | OPENTITAN_PLATFORM: [OPENTITAN_CPU], |
| 32 | } |
| 33 | |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 34 | # 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). |
| 38 | PER_DEVICE_DEPS = { |
Timothy Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 39 | "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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Timothy Trippel | fcc8bac | 2022-08-23 17:20:24 -0700 | [diff] [blame] | 45 | # Default keys used to sign ROM_EXT and BL0 images for testing. |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 46 | DEFAULT_SIGNING_KEYS = { |
| 47 | "test_key_0": "@//sw/device/silicon_creator/rom/keys:test_private_key_0", |
| 48 | } |
| 49 | |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 50 | def _obj_transform_impl(ctx): |
Miguel Young de la Sota | 4b01c7a | 2022-04-21 17:25:52 -0400 | [diff] [blame] | 51 | cc_toolchain = find_cc_toolchain(ctx).cc |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 52 | outputs = [] |
| 53 | for src in ctx.files.srcs: |
Timothy Trippel | 36f9654 | 2022-08-02 18:31:20 -0700 | [diff] [blame] | 54 | binary = ctx.actions.declare_file( |
| 55 | "{}.{}".format( |
| 56 | src.basename.replace("." + src.extension, ""), |
| 57 | ctx.attr.suffix, |
| 58 | ), |
| 59 | ) |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 60 | 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 Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 74 | obj_transform = rv_rule( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 75 | implementation = _obj_transform_impl, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 76 | attrs = { |
| 77 | "srcs": attr.label_list(allow_files = True), |
| 78 | "suffix": attr.string(default = "bin"), |
| 79 | "format": attr.string(default = "binary"), |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 80 | "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 81 | }, |
| 82 | toolchains = ["@rules_cc//cc:toolchain_type"], |
| 83 | ) |
| 84 | |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 85 | # A provider for device-specific archive files that hold binaries of SRAM programs. |
| 86 | ArchiveInfo = provider(fields = ["archive_infos"]) |
| 87 | |
| 88 | def _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 | |
| 217 | bin_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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 230 | def _sign_bin_impl(ctx): |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 231 | signed_image = ctx.actions.declare_file( |
| 232 | "{0}.{1}.signed.bin".format( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 233 | # Remove ".bin" from file basename. |
| 234 | ctx.file.bin.basename.replace("." + ctx.file.bin.extension, ""), |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 235 | ctx.attr.key_name, |
| 236 | ), |
| 237 | ) |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 238 | 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 Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 250 | ctx.actions.run( |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 251 | outputs = outputs, |
| 252 | inputs = inputs, |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 253 | arguments = [ |
Jon Flatley | 26e774c | 2022-06-02 17:29:00 -0400 | [diff] [blame] | 254 | "image", |
Chris Frantz | 47dc84b | 2022-06-16 16:38:08 -0700 | [diff] [blame] | 255 | "manifest", |
| 256 | "update", |
| 257 | "--key-file={}".format(ctx.file.key.path), |
| 258 | "--sign", |
| 259 | "--output={}".format(signed_image.path), |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 260 | ctx.file.bin.path, |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 261 | ] + manifest, |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 262 | 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 Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 269 | sign_bin = rv_rule( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 270 | implementation = _sign_bin_impl, |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 271 | attrs = { |
| 272 | "bin": attr.label(allow_single_file = True), |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 273 | "key": attr.label( |
Alphan Ulusoy | 776d2ab | 2022-08-13 16:47:14 -0400 | [diff] [blame] | 274 | default = "@//sw/device/silicon_creator/rom/keys:test_private_key_0", |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 275 | allow_single_file = True, |
| 276 | ), |
| 277 | "key_name": attr.string(), |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 278 | "manifest": attr.label(allow_single_file = True), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 279 | # 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 Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 281 | "platform": attr.string(default = "@local_config_platform//:host"), |
| 282 | "_tool": attr.label( |
Jon Flatley | 26e774c | 2022-06-02 17:29:00 -0400 | [diff] [blame] | 283 | default = "//sw/host/opentitantool:opentitantool", |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 284 | allow_single_file = True, |
| 285 | ), |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 286 | }, |
| 287 | ) |
| 288 | |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 289 | def _elf_to_disassembly_impl(ctx): |
Miguel Young de la Sota | 4b01c7a | 2022-04-21 17:25:52 -0400 | [diff] [blame] | 290 | cc_toolchain = find_cc_toolchain(ctx).cc |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 291 | outputs = [] |
| 292 | for src in ctx.files.srcs: |
Timothy Trippel | 36f9654 | 2022-08-02 18:31:20 -0700 | [diff] [blame] | 293 | disassembly = ctx.actions.declare_file( |
| 294 | "{}.dis".format( |
| 295 | src.basename.replace("." + src.extension, ""), |
| 296 | ), |
| 297 | ) |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 298 | outputs.append(disassembly) |
| 299 | ctx.actions.run_shell( |
Miguel Young de la Sota | 9fd4883 | 2022-03-25 17:35:06 -0400 | [diff] [blame] | 300 | tools = [ctx.file._cleanup_script], |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 301 | 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 Sota | 9fd4883 | 2022-03-25 17:35:06 -0400 | [diff] [blame] | 306 | ctx.file._cleanup_script.path, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 307 | disassembly.path, |
| 308 | ], |
Alphan Ulusoy | 883d3a9 | 2022-06-09 16:42:08 -0400 | [diff] [blame] | 309 | execution_requirements = { |
| 310 | "no-sandbox": "1", |
| 311 | }, |
Alphan Ulusoy | d953c36 | 2022-06-16 16:10:47 -0400 | [diff] [blame] | 312 | command = "$1 --disassemble --headers --line-numbers --disassemble-zeroes --source --visualize-jumps $2 | $3 > $4", |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 313 | ) |
Miguel Young de la Sota | 9fd4883 | 2022-03-25 17:35:06 -0400 | [diff] [blame] | 314 | return [DefaultInfo(files = depset(outputs), data_runfiles = ctx.runfiles(files = outputs))] |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 315 | |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 316 | elf_to_disassembly = rv_rule( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 317 | implementation = _elf_to_disassembly_impl, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 318 | attrs = { |
| 319 | "srcs": attr.label_list(allow_files = True), |
| 320 | "platform": attr.string(default = OPENTITAN_PLATFORM), |
Miguel Young de la Sota | 9fd4883 | 2022-03-25 17:35:06 -0400 | [diff] [blame] | 321 | "_cleanup_script": attr.label( |
| 322 | allow_single_file = True, |
Timothy Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 323 | default = Label("@//rules/scripts:expand_tabs.sh"), |
Miguel Young de la Sota | 9fd4883 | 2022-03-25 17:35:06 -0400 | [diff] [blame] | 324 | ), |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 325 | "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 326 | }, |
| 327 | toolchains = ["@rules_cc//cc:toolchain_type"], |
| 328 | incompatible_use_toolchain_transition = True, |
| 329 | ) |
| 330 | |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 331 | def _elf_to_scrambled_rom_impl(ctx): |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 332 | outputs = [] |
| 333 | for src in ctx.files.srcs: |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 334 | if src.extension != "elf": |
| 335 | fail("only ROM images in the ELF format may be converted to the VMEM format and scrambled.") |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 336 | scrambled = ctx.actions.declare_file( |
Timothy Trippel | d69bcd7 | 2022-08-12 08:20:40 -0700 | [diff] [blame] | 337 | "{}.39.scr.vmem".format( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 338 | # Remove ".elf" from file basename. |
| 339 | src.basename.replace("." + src.extension, ""), |
| 340 | ), |
| 341 | ) |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 342 | outputs.append(scrambled) |
| 343 | ctx.actions.run( |
| 344 | outputs = [scrambled], |
| 345 | inputs = [ |
| 346 | src, |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 347 | ctx.executable._scramble_tool, |
| 348 | ctx.file._config, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 349 | ], |
| 350 | arguments = [ |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 351 | ctx.file._config.path, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 352 | src.path, |
| 353 | scrambled.path, |
| 354 | ], |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 355 | executable = ctx.executable._scramble_tool, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 356 | ) |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 357 | return [DefaultInfo( |
| 358 | files = depset(outputs), |
| 359 | data_runfiles = ctx.runfiles(files = outputs), |
| 360 | )] |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 361 | |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 362 | elf_to_scrambled_rom_vmem = rv_rule( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 363 | implementation = _elf_to_scrambled_rom_impl, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 364 | attrs = { |
| 365 | "srcs": attr.label_list(allow_files = True), |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 366 | "_scramble_tool": attr.label( |
Timothy Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 367 | default = "@//hw/ip/rom_ctrl/util:scramble_image", |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 368 | executable = True, |
| 369 | cfg = "exec", |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 370 | ), |
| 371 | "_config": attr.label( |
Timothy Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 372 | default = "@//hw/top_earlgrey/data:autogen/top_earlgrey.gen.hjson", |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 373 | allow_single_file = True, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 374 | ), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 375 | }, |
| 376 | ) |
| 377 | |
Miles Dai | 1634fac | 2022-05-13 17:55:35 -0400 | [diff] [blame] | 378 | def _bin_to_vmem_impl(ctx): |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 379 | outputs = [] |
| 380 | vmem = ctx.actions.declare_file("{}.{}.vmem".format( |
| 381 | # Remove ".bin" from file basename. |
Timothy Trippel | f17bca8 | 2022-03-08 11:12:41 -0800 | [diff] [blame] | 382 | ctx.file.bin.basename.replace("." + ctx.file.bin.extension, ""), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 383 | 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 Dai | 1634fac | 2022-05-13 17:55:35 -0400 | [diff] [blame] | 422 | bin_to_vmem = rv_rule( |
| 423 | implementation = _bin_to_vmem_impl, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 424 | attrs = { |
| 425 | "bin": attr.label(allow_single_file = True), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 426 | "word_size": attr.int( |
| 427 | default = 64, |
| 428 | doc = "Word size of VMEM file.", |
| 429 | mandatory = True, |
| 430 | values = [32, 64], |
| 431 | ), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 432 | }, |
| 433 | ) |
| 434 | |
| 435 | def _scramble_flash_vmem_impl(ctx): |
| 436 | outputs = [] |
| 437 | scrambled_vmem = ctx.actions.declare_file("{}.scr.vmem".format( |
| 438 | # Remove ".vmem" from file basename. |
Timothy Trippel | f17bca8 | 2022-03-08 11:12:41 -0800 | [diff] [blame] | 439 | ctx.file.vmem.basename.replace("." + ctx.file.vmem.extension, ""), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 440 | )) |
| 441 | outputs.append(scrambled_vmem) |
| 442 | ctx.actions.run( |
| 443 | outputs = [scrambled_vmem], |
| 444 | inputs = [ |
| 445 | ctx.file.vmem, |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 446 | ctx.executable._tool, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 447 | ], |
| 448 | arguments = [ |
| 449 | ctx.file.vmem.path, |
| 450 | scrambled_vmem.path, |
| 451 | ], |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 452 | executable = ctx.executable._tool, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 453 | ) |
| 454 | return [DefaultInfo( |
| 455 | files = depset(outputs), |
| 456 | data_runfiles = ctx.runfiles(files = outputs), |
| 457 | )] |
| 458 | |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 459 | scramble_flash_vmem = rv_rule( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 460 | implementation = _scramble_flash_vmem_impl, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 461 | attrs = { |
| 462 | "vmem": attr.label(allow_single_file = True), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 463 | "_tool": attr.label( |
Timothy Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 464 | default = "@//util/design:gen-flash-img", |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 465 | executable = True, |
| 466 | cfg = "exec", |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 467 | ), |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 468 | }, |
| 469 | ) |
| 470 | |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 471 | def _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 Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 484 | |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 485 | ctx.actions.run( |
| 486 | outputs = outputs, |
Timothy Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 487 | inputs = [src], |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 488 | 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 Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 498 | logs_db.dirname, |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 499 | ], |
Timothy Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 500 | executable = ctx.executable._tool, |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 501 | ) |
| 502 | return [DefaultInfo( |
| 503 | files = depset(outputs), |
| 504 | data_runfiles = ctx.runfiles(files = outputs), |
| 505 | )] |
| 506 | |
| 507 | gen_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 Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 514 | default = "@//util/device_sw_utils:extract_sw_logs_db", |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 515 | cfg = "exec", |
Timothy Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 516 | executable = True, |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 517 | ), |
| 518 | "_allowlist_function_transition": attr.label( |
| 519 | default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
| 520 | ), |
| 521 | }, |
| 522 | ) |
| 523 | |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 524 | def _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 | |
| 552 | assemble_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 Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 567 | def opentitan_binary( |
| 568 | name, |
| 569 | platform = OPENTITAN_PLATFORM, |
Timothy Trippel | 5ef8a53 | 2022-03-29 09:55:03 -0700 | [diff] [blame] | 570 | extract_sw_logs_db = False, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 571 | **kwargs): |
| 572 | """A helper macro for generating OpenTitan binary artifacts. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 573 | |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 574 | This macro is mostly a wrapper around cc_binary, but creates artifacts |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 575 | compatible with OpenTitan binaries. The actual artifacts created are outputs |
| 576 | of the rules listed below. |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 577 | Args: |
| 578 | @param name: The name of this rule. |
| 579 | @param platform: The target platform for the artifacts. |
Timothy Trippel | 5ef8a53 | 2022-03-29 09:55:03 -0700 | [diff] [blame] | 580 | @param extract_sw_logs_db: Whether to emit a log database for DV testbench. |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 581 | @param **kwargs: Arguments to forward to `cc_binary`. |
| 582 | Emits rules: |
Timothy Trippel | bc0af18 | 2022-08-16 18:46:11 -0700 | [diff] [blame] | 583 | cc_binary named: <name>.elf |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 584 | 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 Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 588 | 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 Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 594 | """ |
| 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 Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 606 | side_targets = [] |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 607 | |
Timothy Trippel | 36f9654 | 2022-08-02 18:31:20 -0700 | [diff] [blame] | 608 | native_binary_name = "{}.elf".format(name) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 609 | native.cc_binary( |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 610 | name = native_binary_name, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 611 | deps = deps, |
| 612 | target_compatible_with = _targets_compatible_with[platform], |
| 613 | copts = copts, |
| 614 | linkopts = linkopts, |
| 615 | **kwargs |
| 616 | ) |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 617 | |
Miguel Young de la Sota | 7bb7fe7 | 2022-04-01 16:40:02 -0400 | [diff] [blame] | 618 | preproc_name = "{}_{}".format(name, "preproc") |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 619 | side_targets.append(preproc_name) |
Miguel Young de la Sota | 7bb7fe7 | 2022-04-01 16:40:02 -0400 | [diff] [blame] | 620 | rv_preprocess( |
| 621 | name = preproc_name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 622 | target = native_binary_name, |
Miguel Young de la Sota | 7bb7fe7 | 2022-04-01 16:40:02 -0400 | [diff] [blame] | 623 | ) |
| 624 | |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 625 | asm_name = "{}_{}".format(name, "asm") |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 626 | side_targets.append(asm_name) |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 627 | rv_asm( |
| 628 | name = asm_name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 629 | target = native_binary_name, |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 630 | ) |
| 631 | |
| 632 | ll_name = "{}_{}".format(name, "ll") |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 633 | side_targets.append(ll_name) |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 634 | rv_llvm_ir( |
| 635 | name = ll_name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 636 | target = native_binary_name, |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 637 | ) |
| 638 | |
| 639 | map_name = "{}_{}".format(name, "map") |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 640 | side_targets.append(map_name) |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 641 | rv_relink_with_linkmap( |
| 642 | name = map_name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 643 | target = native_binary_name, |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 644 | ) |
| 645 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 646 | 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 Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 653 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 654 | 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 661 | |
Timothy Trippel | 1f974b3 | 2022-03-28 16:07:09 -0700 | [diff] [blame] | 662 | # Generate log message database for DV sim testbench |
Timothy Trippel | 5ef8a53 | 2022-03-29 09:55:03 -0700 | [diff] [blame] | 663 | if extract_sw_logs_db: |
Timothy Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 664 | logs_db_name = "{}_{}".format(name, "logs_db") |
Timothy Trippel | 1f974b3 | 2022-03-28 16:07:09 -0700 | [diff] [blame] | 665 | targets.append(":" + logs_db_name) |
| 666 | gen_sim_dv_logs_db( |
| 667 | name = logs_db_name, |
Timothy Trippel | 36f9654 | 2022-08-02 18:31:20 -0700 | [diff] [blame] | 668 | srcs = [native_binary_name], |
Timothy Trippel | 1f974b3 | 2022-03-28 16:07:09 -0700 | [diff] [blame] | 669 | platform = platform, |
| 670 | ) |
| 671 | |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 672 | # Create a filegroup with just the sides targets. |
| 673 | native.filegroup( |
| 674 | name = name + "_side_targets", |
| 675 | srcs = side_targets, |
| 676 | ) |
| 677 | |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 678 | return targets |
| 679 | |
| 680 | def opentitan_rom_binary( |
| 681 | name, |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 682 | devices = PER_DEVICE_DEPS.keys(), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 683 | platform = OPENTITAN_PLATFORM, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 684 | **kwargs): |
| 685 | """A helper macro for generating OpenTitan binary artifacts for ROM. |
| 686 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 687 | 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 691 | Args: |
| 692 | @param name: The name of this rule. |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 693 | @param devices: List of devices to build the target for. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 694 | @param platform: The target platform for the artifacts. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 695 | @param **kwargs: Arguments to forward to `opentitan_binary`. |
| 696 | Emits rules: |
| 697 | For each device in per_device_deps entry: |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 698 | 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 705 | """ |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 706 | deps = kwargs.pop("deps", []) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 707 | all_targets = [] |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 708 | 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 712 | devname = "{}_{}".format(name, device) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 713 | dev_targets = [] |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 714 | |
Timothy Trippel | 1f974b3 | 2022-03-28 16:07:09 -0700 | [diff] [blame] | 715 | # Generate ELF, Binary, Disassembly, and (maybe) sim_dv logs database |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 716 | dev_targets.extend(opentitan_binary( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 717 | name = devname, |
| 718 | deps = deps + dev_deps, |
Alphan Ulusoy | e77043f | 2022-08-29 10:23:46 -0400 | [diff] [blame] | 719 | extract_sw_logs_db = device == "sim_dv", |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 720 | **kwargs |
| 721 | )) |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 722 | |
| 723 | # We need to generate VMEM files even for FPGA devices, because we use |
| 724 | # them for bitstream splicing. |
Timothy Trippel | 36f9654 | 2022-08-02 18:31:20 -0700 | [diff] [blame] | 725 | elf_name = "{}.{}".format(devname, "elf") |
Miles Dai | 1634fac | 2022-05-13 17:55:35 -0400 | [diff] [blame] | 726 | bin_name = "{}_{}".format(devname, "bin") |
| 727 | |
| 728 | # Generate Un-scrambled ROM VMEM |
| 729 | vmem_name = "{}_vmem".format(devname) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 730 | dev_targets.append(":" + vmem_name) |
Miles Dai | 1634fac | 2022-05-13 17:55:35 -0400 | [diff] [blame] | 731 | bin_to_vmem( |
| 732 | name = vmem_name, |
| 733 | bin = bin_name, |
| 734 | platform = platform, |
Miles Dai | 91d811e | 2022-05-10 15:51:17 -0400 | [diff] [blame] | 735 | word_size = 32, |
Miles Dai | 1634fac | 2022-05-13 17:55:35 -0400 | [diff] [blame] | 736 | ) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 737 | |
| 738 | # Generate Scrambled ROM VMEM |
| 739 | scr_vmem_name = "{}_scr_vmem".format(devname) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 740 | dev_targets.append(":" + scr_vmem_name) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 741 | elf_to_scrambled_rom_vmem( |
| 742 | name = scr_vmem_name, |
| 743 | srcs = [elf_name], |
| 744 | platform = platform, |
| 745 | ) |
| 746 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 747 | # 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 755 | native.filegroup( |
| 756 | name = name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 757 | srcs = all_targets, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 758 | ) |
| 759 | |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 760 | def _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 | |
| 772 | pick_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 Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 782 | def opentitan_multislot_flash_binary( |
| 783 | name, |
| 784 | srcs, |
| 785 | image_size, |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 786 | devices = PER_DEVICE_DEPS.keys(), |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 787 | 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 Trippel | fcc8bac | 2022-08-23 17:20:24 -0700 | [diff] [blame] | 792 | 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 Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 796 | 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 Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 801 | @param devices: List of devices to build the target for. |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 802 | @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 Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 815 | 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 Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 818 | 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 Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 843 | # 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 Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 854 | |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 855 | # 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 Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 863 | |
| 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 877 | def opentitan_flash_binary( |
| 878 | name, |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 879 | devices = PER_DEVICE_DEPS.keys(), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 880 | platform = OPENTITAN_PLATFORM, |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 881 | signing_keys = DEFAULT_SIGNING_KEYS, |
Timothy Trippel | b867c1c | 2022-08-03 23:42:16 -0700 | [diff] [blame] | 882 | signed = False, |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 883 | manifest = None, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 884 | **kwargs): |
| 885 | """A helper macro for generating OpenTitan binary artifacts for flash. |
| 886 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 887 | 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 892 | Args: |
| 893 | @param name: The name of this rule. |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 894 | @param devices: List of devices to build the target for. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 895 | @param platform: The target platform for the artifacts. |
| 896 | @param signing_keys: The signing keys for to sign each BIN file with. |
Timothy Trippel | b867c1c | 2022-08-03 23:42:16 -0700 | [diff] [blame] | 897 | @param signed: Whether or not to emit signed binary/VMEM files. |
Timothy Trippel | 49cd1a1 | 2022-08-10 14:11:51 -0700 | [diff] [blame] | 898 | @param manifest: Partially populated manifest to set boot stage/slot configs. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 899 | @param **kwargs: Arguments to forward to `opentitan_binary`. |
| 900 | Emits rules: |
| 901 | For each device in per_device_deps entry: |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 902 | 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 906 | sign_bin named: <name>_<device>_bin_signed_<key_name> |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 907 | 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 913 | """ |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 914 | deps = kwargs.pop("deps", []) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 915 | all_targets = [] |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 916 | 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 920 | devname = "{}_{}".format(name, device) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 921 | dev_targets = [] |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 922 | |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 923 | depname = "{}_deps".format(devname) |
| 924 | pick_correct_archive_for_device( |
| 925 | name = depname, |
| 926 | deps = deps + dev_deps, |
| 927 | device = device, |
| 928 | ) |
| 929 | |
Timothy Trippel | 1f974b3 | 2022-03-28 16:07:09 -0700 | [diff] [blame] | 930 | # Generate ELF, Binary, Disassembly, and (maybe) sim_dv logs database |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 931 | dev_targets.extend(opentitan_binary( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 932 | name = devname, |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 933 | deps = [depname], |
Alphan Ulusoy | e77043f | 2022-08-29 10:23:46 -0400 | [diff] [blame] | 934 | extract_sw_logs_db = device == "sim_dv", |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 935 | **kwargs |
| 936 | )) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 937 | bin_name = "{}_{}".format(devname, "bin") |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 938 | |
Timothy Trippel | ba061ed | 2022-03-23 00:30:22 -0700 | [diff] [blame] | 939 | # Sign BIN (if required) and generate scrambled VMEM images. |
Timothy Trippel | b867c1c | 2022-08-03 23:42:16 -0700 | [diff] [blame] | 940 | if signed: |
Timothy Trippel | 49cd1a1 | 2022-08-10 14:11:51 -0700 | [diff] [blame] | 941 | if manifest == None: |
| 942 | fail("A 'manifest' must be provided in order to sign flash images.") |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 943 | for (key_name, key) in signing_keys.items(): |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 944 | # Sign the Binary. |
| 945 | signed_bin_name = "{}_bin_signed_{}".format(devname, key_name) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 946 | dev_targets.append(":" + signed_bin_name) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 947 | sign_bin( |
| 948 | name = signed_bin_name, |
| 949 | bin = bin_name, |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 950 | key = key, |
| 951 | key_name = key_name, |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 952 | manifest = manifest, |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 953 | ) |
| 954 | |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 955 | # 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 969 | |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 970 | # 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 Trippel | ba061ed | 2022-03-23 00:30:22 -0700 | [diff] [blame] | 981 | |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 982 | # 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 Trippel | b867c1c | 2022-08-03 23:42:16 -0700 | [diff] [blame] | 993 | |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 994 | # 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 Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 1002 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 1003 | # 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 Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 1011 | native.filegroup( |
| 1012 | name = name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 1013 | srcs = all_targets, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 1014 | ) |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1015 | |
| 1016 | def opentitan_ram_binary( |
| 1017 | name, |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1018 | archive_symbol_prefix, |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 1019 | devices = PER_DEVICE_DEPS.keys(), |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1020 | platform = OPENTITAN_PLATFORM, |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1021 | **kwargs): |
| 1022 | """A helper macro for generating OpenTitan binary artifacts for RAM. |
| 1023 | |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1024 | 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 Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1028 | Args: |
| 1029 | @param name: The name of this rule. |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1030 | @param archive_symbol_prefix: Prefix used to rename symbols in the binary. |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 1031 | @param devices: List of devices to build the target for. |
| 1032 | @param platform: The target platform for the artifacts. |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1033 | @param **kwargs: Arguments to forward to `opentitan_binary`. |
| 1034 | Emits rules: |
| 1035 | For each device in per_device_deps entry: |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1036 | rules emitted by `opentitan_binary` named: see `opentitan_binary` macro |
| 1037 | bin_to_archive named: <name> |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1038 | """ |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1039 | deps = kwargs.pop("deps", []) |
| 1040 | hdrs = kwargs.pop("hdrs", []) |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1041 | binaries = [] |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 1042 | 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 Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1046 | devname = "{}_{}".format(name, device) |
| 1047 | |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1048 | # Generate the binary. |
| 1049 | opentitan_binary( |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1050 | name = devname, |
| 1051 | deps = deps + dev_deps, |
| 1052 | extract_sw_logs_db = False, |
| 1053 | **kwargs |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1054 | ) |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1055 | bin_name = "{}_{}".format(devname, "bin") |
| 1056 | binaries.append(":" + bin_name) |
| 1057 | |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1058 | # Generate the archive file. |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1059 | bin_to_archive( |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1060 | name = name, |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1061 | hdrs = hdrs, |
| 1062 | binaries = binaries, |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1063 | devices = PER_DEVICE_DEPS.keys(), |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1064 | archive_symbol_prefix = archive_symbol_prefix, |
| 1065 | ) |