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 |
Pirmin Vogel | 9fc1ec9 | 2023-02-16 19:18:42 +0100 | [diff] [blame] | 36 | # simulation platforms (DV and Verilator), and two FPGA platforms (CW305 |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 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"], |
Pirmin Vogel | 9fc1ec9 | 2023-02-16 19:18:42 +0100 | [diff] [blame] | 41 | "fpga_cw305": ["@//sw/device/lib/arch:fpga_cw305"], |
Timothy Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 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 = { |
Alphan Ulusoy | 8f86528 | 2022-11-14 15:40:43 -0500 | [diff] [blame] | 47 | "fake_test_key_0": "@//sw/device/silicon_creator/rom/keys/fake:test_private_key_0", |
| 48 | "fake_dev_key_0": "@//sw/device/silicon_creator/rom/keys/fake:dev_private_key_0", |
| 49 | "fake_prod_key_0": "@//sw/device/silicon_creator/rom/keys/fake:prod_private_key_0", |
Alphan Ulusoy | 3cdeca4 | 2022-09-29 13:04:16 -0400 | [diff] [blame] | 50 | "unauthorized_0": "@//sw/device/silicon_creator/rom/keys:unauthorized_private_key_0", |
Miguel Osorio | 38503db | 2023-02-17 20:19:52 -0800 | [diff] [blame] | 51 | "fake_rom_ext_test_key_0": "@//sw/device/silicon_creator/rom_ext/keys/fake:rom_ext_test_private_key_0", |
| 52 | "fake_rom_ext_dev_key_0": "@//sw/device/silicon_creator/rom_ext/keys/fake:rom_ext_dev_private_key_0", |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 55 | def _obj_transform_impl(ctx): |
Miguel Young de la Sota | 4b01c7a | 2022-04-21 17:25:52 -0400 | [diff] [blame] | 56 | cc_toolchain = find_cc_toolchain(ctx).cc |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 57 | outputs = [] |
| 58 | for src in ctx.files.srcs: |
Timothy Trippel | 36f9654 | 2022-08-02 18:31:20 -0700 | [diff] [blame] | 59 | binary = ctx.actions.declare_file( |
| 60 | "{}.{}".format( |
| 61 | src.basename.replace("." + src.extension, ""), |
| 62 | ctx.attr.suffix, |
| 63 | ), |
| 64 | ) |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 65 | outputs.append(binary) |
| 66 | ctx.actions.run( |
| 67 | outputs = [binary], |
| 68 | inputs = [src] + cc_toolchain.all_files.to_list(), |
| 69 | arguments = [ |
| 70 | "--output-target", |
| 71 | ctx.attr.format, |
| 72 | src.path, |
| 73 | binary.path, |
| 74 | ], |
| 75 | executable = cc_toolchain.objcopy_executable, |
| 76 | ) |
| 77 | return [DefaultInfo(files = depset(outputs), data_runfiles = ctx.runfiles(files = outputs))] |
| 78 | |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 79 | obj_transform = rv_rule( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 80 | implementation = _obj_transform_impl, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 81 | attrs = { |
| 82 | "srcs": attr.label_list(allow_files = True), |
| 83 | "suffix": attr.string(default = "bin"), |
| 84 | "format": attr.string(default = "binary"), |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 85 | "_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] | 86 | }, |
| 87 | toolchains = ["@rules_cc//cc:toolchain_type"], |
| 88 | ) |
| 89 | |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 90 | # A provider for device-specific archive files that hold binaries of SRAM programs. |
| 91 | ArchiveInfo = provider(fields = ["archive_infos"]) |
| 92 | |
| 93 | def _bin_to_archive_impl(ctx): |
| 94 | cc_infos = [] |
| 95 | cc_toolchain = find_cc_toolchain(ctx).cc |
| 96 | cc_info_dict = {} |
| 97 | num_devices = len(ctx.attr.devices) |
| 98 | num_binaries = len(ctx.attr.binaries) |
| 99 | if num_devices != num_binaries: |
| 100 | fail("Number of devices", num_devices, "must be equal to number of binaries", num_binaries) |
| 101 | for (device, binary_target) in zip(ctx.attr.devices, ctx.attr.binaries): |
| 102 | devname = "{}_{}".format(ctx.attr.name, device) |
| 103 | binary_file = binary_target.files.to_list()[0] |
| 104 | object_file = ctx.actions.declare_file("{}.o".format(devname)) |
| 105 | renamed_object_file = ctx.actions.declare_file("{}.renamed.o".format(devname)) |
| 106 | archive_file = ctx.actions.declare_file("{}.a".format(devname)) |
| 107 | |
| 108 | # Create a CcInfo to be able to use this rule as a dependency in other rules. |
| 109 | # See https://bazel.build/docs/integrating-with-rules-cc. |
| 110 | feature_configuration = cc_common.configure_features( |
| 111 | ctx = ctx, |
| 112 | cc_toolchain = cc_toolchain, |
| 113 | requested_features = ctx.features, |
| 114 | unsupported_features = ctx.disabled_features, |
| 115 | ) |
| 116 | action_name = ACTION_NAMES.cpp_link_executable |
| 117 | c_linker_path = cc_common.get_tool_for_action( |
| 118 | feature_configuration = feature_configuration, |
| 119 | action_name = action_name, |
| 120 | ) |
| 121 | c_link_variables = cc_common.create_link_variables( |
| 122 | feature_configuration = feature_configuration, |
| 123 | cc_toolchain = cc_toolchain, |
| 124 | output_file = object_file.path, |
| 125 | ) |
| 126 | command_line = cc_common.get_memory_inefficient_command_line( |
| 127 | feature_configuration = feature_configuration, |
| 128 | action_name = action_name, |
| 129 | variables = c_link_variables, |
| 130 | ) |
| 131 | env = cc_common.get_environment_variables( |
| 132 | feature_configuration = feature_configuration, |
| 133 | action_name = action_name, |
| 134 | variables = c_link_variables, |
| 135 | ) |
| 136 | linker_path = cc_common.get_tool_for_action( |
| 137 | feature_configuration = feature_configuration, |
| 138 | action_name = action_name, |
| 139 | ) |
| 140 | |
| 141 | # Create an object file that contains the binary. |
| 142 | ctx.actions.run( |
| 143 | executable = cc_toolchain.ld_executable, |
| 144 | arguments = [ |
| 145 | "-r", |
| 146 | "-b", |
| 147 | "binary", |
| 148 | "-o", |
| 149 | object_file.path, |
| 150 | binary_file.path, |
| 151 | ], |
| 152 | use_default_shell_env = False, |
| 153 | env = env, |
| 154 | inputs = depset( |
| 155 | direct = [binary_file], |
| 156 | transitive = [cc_toolchain.all_files], |
| 157 | ), |
| 158 | outputs = [object_file], |
| 159 | mnemonic = "CppLink", |
| 160 | ) |
| 161 | |
| 162 | # Rename symbols to make them more manageable. |
| 163 | sym_prefix = "_binary_" + binary_file.path.replace(".", "_").replace("/", "_").replace("-", "_") |
| 164 | suffixes = ["start", "end", "size"] |
| 165 | rename_args = [] |
| 166 | for suffix in suffixes: |
| 167 | old_name = "{}_{}".format(sym_prefix, suffix) |
| 168 | new_name = "_{}_{}".format(ctx.attr.archive_symbol_prefix, suffix) |
| 169 | rename_args.extend(["--redefine-sym", "{}={}".format(old_name, new_name)]) |
| 170 | rename_args.extend(["--rename-section", ".data=.data.sram_program"]) |
| 171 | rename_args.extend([object_file.path, renamed_object_file.path]) |
| 172 | ctx.actions.run( |
| 173 | executable = cc_toolchain.objcopy_executable, |
| 174 | arguments = rename_args, |
| 175 | use_default_shell_env = False, |
| 176 | env = env, |
| 177 | inputs = depset( |
| 178 | direct = [object_file], |
| 179 | transitive = [cc_toolchain.all_files], |
| 180 | ), |
| 181 | outputs = [renamed_object_file], |
| 182 | mnemonic = "RenameSymbols", |
| 183 | ) |
| 184 | |
| 185 | # Create an archive that contains the object file. |
| 186 | ctx.actions.run( |
| 187 | executable = cc_toolchain.ar_executable, |
| 188 | arguments = [ |
| 189 | "r", |
| 190 | archive_file.path, |
| 191 | renamed_object_file.path, |
| 192 | ], |
| 193 | use_default_shell_env = False, |
| 194 | env = env, |
| 195 | inputs = depset( |
| 196 | direct = [renamed_object_file], |
| 197 | transitive = [cc_toolchain.all_files], |
| 198 | ), |
| 199 | outputs = [archive_file], |
| 200 | mnemonic = "Archive", |
| 201 | ) |
| 202 | |
| 203 | cc_info_dict[device] = CcInfo( |
| 204 | compilation_context = cc_common.create_compilation_context( |
| 205 | headers = depset(direct = ctx.attr.hdrs[0].files.to_list()), |
| 206 | ), |
| 207 | linking_context = cc_common.create_linking_context( |
| 208 | linker_inputs = depset([cc_common.create_linker_input( |
| 209 | owner = ctx.label, |
| 210 | libraries = depset([cc_common.create_library_to_link( |
| 211 | actions = ctx.actions, |
| 212 | feature_configuration = feature_configuration, |
| 213 | cc_toolchain = cc_toolchain, |
| 214 | static_library = archive_file, |
| 215 | )]), |
| 216 | )]), |
| 217 | ), |
| 218 | ) |
| 219 | |
| 220 | return ArchiveInfo(archive_infos = cc_info_dict) |
| 221 | |
| 222 | bin_to_archive = rv_rule( |
| 223 | implementation = _bin_to_archive_impl, |
| 224 | attrs = { |
| 225 | "binaries": attr.label_list(allow_files = True), |
| 226 | "devices": attr.string_list(), |
| 227 | "hdrs": attr.label_list(allow_files = True), |
| 228 | "archive_symbol_prefix": attr.string(), |
| 229 | "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), |
| 230 | }, |
| 231 | fragments = ["cpp"], |
| 232 | toolchains = ["@rules_cc//cc:toolchain_type"], |
| 233 | ) |
| 234 | |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 235 | def _sign_bin_impl(ctx): |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 236 | signed_image = ctx.actions.declare_file( |
| 237 | "{0}.{1}.signed.bin".format( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 238 | # Remove ".bin" from file basename. |
| 239 | ctx.file.bin.basename.replace("." + ctx.file.bin.extension, ""), |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 240 | ctx.attr.key_name, |
| 241 | ), |
| 242 | ) |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 243 | outputs = [signed_image] |
| 244 | |
| 245 | inputs = [ |
| 246 | ctx.file.bin, |
| 247 | ctx.file.key, |
Chris Frantz | 40ac0e7 | 2022-09-21 11:40:11 -0700 | [diff] [blame] | 248 | ctx.file._opentitantool, |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 249 | ] |
| 250 | manifest = [] |
| 251 | if ctx.file.manifest: |
| 252 | manifest = ["--manifest={}".format(ctx.file.manifest.path)] |
| 253 | inputs.append(ctx.file.manifest) |
| 254 | |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 255 | ctx.actions.run( |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 256 | outputs = outputs, |
| 257 | inputs = inputs, |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 258 | arguments = [ |
Chris Frantz | 40ac0e7 | 2022-09-21 11:40:11 -0700 | [diff] [blame] | 259 | "--rcfile=", |
Jon Flatley | 26e774c | 2022-06-02 17:29:00 -0400 | [diff] [blame] | 260 | "image", |
Chris Frantz | 47dc84b | 2022-06-16 16:38:08 -0700 | [diff] [blame] | 261 | "manifest", |
| 262 | "update", |
| 263 | "--key-file={}".format(ctx.file.key.path), |
| 264 | "--sign", |
| 265 | "--output={}".format(signed_image.path), |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 266 | ctx.file.bin.path, |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 267 | ] + manifest, |
Chris Frantz | 40ac0e7 | 2022-09-21 11:40:11 -0700 | [diff] [blame] | 268 | executable = ctx.file._opentitantool.path, |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 269 | ) |
| 270 | return [DefaultInfo( |
| 271 | files = depset(outputs), |
| 272 | data_runfiles = ctx.runfiles(files = outputs), |
| 273 | )] |
| 274 | |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 275 | sign_bin = rv_rule( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 276 | implementation = _sign_bin_impl, |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 277 | attrs = { |
| 278 | "bin": attr.label(allow_single_file = True), |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 279 | "key": attr.label( |
Alphan Ulusoy | 776d2ab | 2022-08-13 16:47:14 -0400 | [diff] [blame] | 280 | default = "@//sw/device/silicon_creator/rom/keys:test_private_key_0", |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 281 | allow_single_file = True, |
| 282 | ), |
| 283 | "key_name": attr.string(), |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 284 | "manifest": attr.label(allow_single_file = True), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 285 | # TODO(lowRISC/opentitan:#11199): explore other options to side-step the |
| 286 | # need for this transition, in order to build the ROM_EXT signer tool. |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 287 | "platform": attr.string(default = "@local_config_platform//:host"), |
Chris Frantz | 40ac0e7 | 2022-09-21 11:40:11 -0700 | [diff] [blame] | 288 | "_opentitantool": attr.label( |
Jon Flatley | 26e774c | 2022-06-02 17:29:00 -0400 | [diff] [blame] | 289 | default = "//sw/host/opentitantool:opentitantool", |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 290 | allow_single_file = True, |
| 291 | ), |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 292 | }, |
| 293 | ) |
| 294 | |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 295 | def _elf_to_disassembly_impl(ctx): |
Miguel Young de la Sota | 4b01c7a | 2022-04-21 17:25:52 -0400 | [diff] [blame] | 296 | cc_toolchain = find_cc_toolchain(ctx).cc |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 297 | outputs = [] |
| 298 | for src in ctx.files.srcs: |
Timothy Trippel | 36f9654 | 2022-08-02 18:31:20 -0700 | [diff] [blame] | 299 | disassembly = ctx.actions.declare_file( |
| 300 | "{}.dis".format( |
| 301 | src.basename.replace("." + src.extension, ""), |
| 302 | ), |
| 303 | ) |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 304 | outputs.append(disassembly) |
| 305 | ctx.actions.run_shell( |
Miguel Young de la Sota | 9fd4883 | 2022-03-25 17:35:06 -0400 | [diff] [blame] | 306 | tools = [ctx.file._cleanup_script], |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 307 | outputs = [disassembly], |
| 308 | inputs = [src] + cc_toolchain.all_files.to_list(), |
| 309 | arguments = [ |
| 310 | cc_toolchain.objdump_executable, |
| 311 | src.path, |
Miguel Young de la Sota | 9fd4883 | 2022-03-25 17:35:06 -0400 | [diff] [blame] | 312 | ctx.file._cleanup_script.path, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 313 | disassembly.path, |
| 314 | ], |
Alphan Ulusoy | 883d3a9 | 2022-06-09 16:42:08 -0400 | [diff] [blame] | 315 | execution_requirements = { |
| 316 | "no-sandbox": "1", |
| 317 | }, |
Alphan Ulusoy | 6efbeae | 2022-09-13 15:25:03 -0400 | [diff] [blame] | 318 | command = "$1 -wx --disassemble --headers --line-numbers --disassemble-zeroes --source --visualize-jumps $2 | $3 > $4", |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 319 | ) |
Miguel Young de la Sota | 9fd4883 | 2022-03-25 17:35:06 -0400 | [diff] [blame] | 320 | return [DefaultInfo(files = depset(outputs), data_runfiles = ctx.runfiles(files = outputs))] |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 321 | |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 322 | elf_to_disassembly = rv_rule( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 323 | implementation = _elf_to_disassembly_impl, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 324 | attrs = { |
| 325 | "srcs": attr.label_list(allow_files = True), |
| 326 | "platform": attr.string(default = OPENTITAN_PLATFORM), |
Miguel Young de la Sota | 9fd4883 | 2022-03-25 17:35:06 -0400 | [diff] [blame] | 327 | "_cleanup_script": attr.label( |
| 328 | allow_single_file = True, |
Timothy Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 329 | default = Label("@//rules/scripts:expand_tabs.sh"), |
Miguel Young de la Sota | 9fd4883 | 2022-03-25 17:35:06 -0400 | [diff] [blame] | 330 | ), |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 331 | "_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] | 332 | }, |
| 333 | toolchains = ["@rules_cc//cc:toolchain_type"], |
| 334 | incompatible_use_toolchain_transition = True, |
| 335 | ) |
| 336 | |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 337 | def _elf_to_scrambled_rom_impl(ctx): |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 338 | outputs = [] |
| 339 | for src in ctx.files.srcs: |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 340 | if src.extension != "elf": |
| 341 | 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] | 342 | scrambled = ctx.actions.declare_file( |
Timothy Trippel | d69bcd7 | 2022-08-12 08:20:40 -0700 | [diff] [blame] | 343 | "{}.39.scr.vmem".format( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 344 | # Remove ".elf" from file basename. |
| 345 | src.basename.replace("." + src.extension, ""), |
| 346 | ), |
| 347 | ) |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 348 | outputs.append(scrambled) |
| 349 | ctx.actions.run( |
| 350 | outputs = [scrambled], |
| 351 | inputs = [ |
| 352 | src, |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 353 | ctx.executable._scramble_tool, |
| 354 | ctx.file._config, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 355 | ], |
| 356 | arguments = [ |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 357 | ctx.file._config.path, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 358 | src.path, |
| 359 | scrambled.path, |
| 360 | ], |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 361 | executable = ctx.executable._scramble_tool, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 362 | ) |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 363 | return [DefaultInfo( |
| 364 | files = depset(outputs), |
| 365 | data_runfiles = ctx.runfiles(files = outputs), |
| 366 | )] |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 367 | |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 368 | elf_to_scrambled_rom_vmem = rv_rule( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 369 | implementation = _elf_to_scrambled_rom_impl, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 370 | attrs = { |
| 371 | "srcs": attr.label_list(allow_files = True), |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 372 | "_scramble_tool": attr.label( |
Timothy Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 373 | default = "@//hw/ip/rom_ctrl/util:scramble_image", |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 374 | executable = True, |
| 375 | cfg = "exec", |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 376 | ), |
| 377 | "_config": attr.label( |
Timothy Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 378 | default = "@//hw/top_earlgrey/data:autogen/top_earlgrey.gen.hjson", |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 379 | allow_single_file = True, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 380 | ), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 381 | }, |
| 382 | ) |
| 383 | |
Miles Dai | 1634fac | 2022-05-13 17:55:35 -0400 | [diff] [blame] | 384 | def _bin_to_vmem_impl(ctx): |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 385 | outputs = [] |
| 386 | vmem = ctx.actions.declare_file("{}.{}.vmem".format( |
| 387 | # Remove ".bin" from file basename. |
Timothy Trippel | f17bca8 | 2022-03-08 11:12:41 -0800 | [diff] [blame] | 388 | ctx.file.bin.basename.replace("." + ctx.file.bin.extension, ""), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 389 | ctx.attr.word_size, |
| 390 | )) |
| 391 | outputs.append(vmem) |
| 392 | ctx.actions.run( |
| 393 | outputs = [vmem], |
| 394 | inputs = [ |
| 395 | ctx.file.bin, |
| 396 | ], |
| 397 | arguments = [ |
| 398 | ctx.file.bin.path, |
| 399 | "--binary", |
| 400 | # Reverse the endianness of every word. |
| 401 | "--offset", |
| 402 | "0x0", |
| 403 | "--byte-swap", |
| 404 | str(ctx.attr.word_size // 8), |
| 405 | # Pad to word alignment. |
| 406 | "--fill", |
| 407 | "0xff", |
| 408 | "-within", |
| 409 | ctx.file.bin.path, |
| 410 | "-binary", |
| 411 | "-range-pad", |
| 412 | str(ctx.attr.word_size // 8), |
| 413 | # Output a VMEM file with specified word size |
| 414 | "--output", |
| 415 | vmem.path, |
| 416 | "--vmem", |
| 417 | str(ctx.attr.word_size), |
| 418 | ], |
| 419 | # This this executable is expected to be installed (as required by the |
| 420 | # srecord package in apt-requirements.txt). |
| 421 | executable = "srec_cat", |
Martin Lueker-Boden | c8a7ff7 | 2022-09-08 17:35:29 -0700 | [diff] [blame] | 422 | use_default_shell_env = True, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 423 | ) |
| 424 | return [DefaultInfo( |
| 425 | files = depset(outputs), |
| 426 | data_runfiles = ctx.runfiles(files = outputs), |
| 427 | )] |
| 428 | |
Miles Dai | 1634fac | 2022-05-13 17:55:35 -0400 | [diff] [blame] | 429 | bin_to_vmem = rv_rule( |
| 430 | implementation = _bin_to_vmem_impl, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 431 | attrs = { |
| 432 | "bin": attr.label(allow_single_file = True), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 433 | "word_size": attr.int( |
| 434 | default = 64, |
| 435 | doc = "Word size of VMEM file.", |
| 436 | mandatory = True, |
| 437 | values = [32, 64], |
| 438 | ), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 439 | }, |
| 440 | ) |
| 441 | |
| 442 | def _scramble_flash_vmem_impl(ctx): |
| 443 | outputs = [] |
| 444 | scrambled_vmem = ctx.actions.declare_file("{}.scr.vmem".format( |
| 445 | # Remove ".vmem" from file basename. |
Timothy Trippel | f17bca8 | 2022-03-08 11:12:41 -0800 | [diff] [blame] | 446 | ctx.file.vmem.basename.replace("." + ctx.file.vmem.extension, ""), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 447 | )) |
| 448 | outputs.append(scrambled_vmem) |
| 449 | ctx.actions.run( |
| 450 | outputs = [scrambled_vmem], |
| 451 | inputs = [ |
| 452 | ctx.file.vmem, |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 453 | ctx.executable._tool, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 454 | ], |
| 455 | arguments = [ |
Timothy Trippel | db439f5 | 2023-03-06 10:42:16 -0800 | [diff] [blame] | 456 | "--infile", |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 457 | ctx.file.vmem.path, |
Timothy Trippel | db439f5 | 2023-03-06 10:42:16 -0800 | [diff] [blame] | 458 | "--outfile", |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 459 | scrambled_vmem.path, |
| 460 | ], |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 461 | executable = ctx.executable._tool, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 462 | ) |
| 463 | return [DefaultInfo( |
| 464 | files = depset(outputs), |
| 465 | data_runfiles = ctx.runfiles(files = outputs), |
| 466 | )] |
| 467 | |
Miguel Young de la Sota | 3b5a9f5 | 2022-03-24 16:11:42 -0400 | [diff] [blame] | 468 | scramble_flash_vmem = rv_rule( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 469 | implementation = _scramble_flash_vmem_impl, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 470 | attrs = { |
| 471 | "vmem": attr.label(allow_single_file = True), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 472 | "_tool": attr.label( |
Timothy Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 473 | default = "@//util/design:gen-flash-img", |
Timothy Trippel | 4a90363 | 2022-04-15 15:04:39 -0700 | [diff] [blame] | 474 | executable = True, |
| 475 | cfg = "exec", |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 476 | ), |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 477 | }, |
| 478 | ) |
| 479 | |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 480 | def _gen_sim_dv_logs_db_impl(ctx): |
| 481 | outputs = [] |
| 482 | for src in ctx.files.srcs: |
| 483 | if src.extension != "elf": |
| 484 | fail("can only generate DV logs database files from ELF files.") |
| 485 | logs_db = ctx.actions.declare_file("{}.logs.txt".format( |
| 486 | src.basename.replace("." + src.extension, ""), |
| 487 | )) |
| 488 | rodata = ctx.actions.declare_file("{}.rodata.txt".format( |
| 489 | src.basename.replace("." + src.extension, ""), |
| 490 | )) |
| 491 | outputs.append(logs_db) |
| 492 | outputs.append(rodata) |
Timothy Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 493 | |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 494 | ctx.actions.run( |
| 495 | outputs = outputs, |
Timothy Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 496 | inputs = [src], |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 497 | arguments = [ |
| 498 | "--elf-file", |
| 499 | src.path, |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 500 | "--logs-fields-section", |
| 501 | ".logs.fields", |
| 502 | "--name", |
| 503 | src.basename.replace("." + src.extension, ""), |
| 504 | "--outdir", |
Timothy Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 505 | logs_db.dirname, |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 506 | ], |
Timothy Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 507 | executable = ctx.executable._tool, |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 508 | ) |
| 509 | return [DefaultInfo( |
| 510 | files = depset(outputs), |
| 511 | data_runfiles = ctx.runfiles(files = outputs), |
| 512 | )] |
| 513 | |
| 514 | gen_sim_dv_logs_db = rule( |
| 515 | implementation = _gen_sim_dv_logs_db_impl, |
| 516 | cfg = opentitan_transition, |
| 517 | attrs = { |
| 518 | "srcs": attr.label_list(allow_files = True), |
| 519 | "platform": attr.string(default = OPENTITAN_PLATFORM), |
| 520 | "_tool": attr.label( |
Timothy Trippel | d2277d2 | 2022-06-02 00:36:08 -0700 | [diff] [blame] | 521 | default = "@//util/device_sw_utils:extract_sw_logs_db", |
Timothy Trippel | dea42da | 2022-04-15 14:30:01 -0700 | [diff] [blame] | 522 | cfg = "exec", |
Timothy Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 523 | executable = True, |
Timothy Trippel | 8c421dd | 2022-03-28 15:04:56 -0700 | [diff] [blame] | 524 | ), |
| 525 | "_allowlist_function_transition": attr.label( |
| 526 | default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
| 527 | ), |
| 528 | }, |
| 529 | ) |
| 530 | |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 531 | def _assemble_flash_image_impl(ctx): |
| 532 | output = ctx.actions.declare_file(ctx.attr.output) |
| 533 | outputs = [output] |
| 534 | inputs = [] |
| 535 | arguments = [ |
Chris Frantz | 40ac0e7 | 2022-09-21 11:40:11 -0700 | [diff] [blame] | 536 | "--rcfile=", |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 537 | "image", |
| 538 | "assemble", |
| 539 | "--mirror", |
| 540 | "false", |
| 541 | "--output", |
| 542 | output.path, |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 543 | ] |
Chris Frantz | af9bcb3 | 2022-10-10 13:40:34 -0700 | [diff] [blame] | 544 | if ctx.attr.image_size: |
| 545 | arguments.append("--size={}".format(ctx.attr.image_size)) |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 546 | for binary, offset in ctx.attr.binaries.items(): |
| 547 | inputs.extend(binary.files.to_list()) |
| 548 | arguments.append("{}@{}".format(binary.files.to_list()[0].path, offset)) |
| 549 | ctx.actions.run( |
| 550 | outputs = outputs, |
| 551 | inputs = inputs, |
| 552 | arguments = arguments, |
| 553 | executable = ctx.executable._opentitantool, |
| 554 | ) |
| 555 | return [DefaultInfo( |
| 556 | files = depset(outputs), |
| 557 | data_runfiles = ctx.runfiles(files = outputs), |
| 558 | )] |
| 559 | |
| 560 | assemble_flash_image = rv_rule( |
| 561 | implementation = _assemble_flash_image_impl, |
| 562 | attrs = { |
Chris Frantz | af9bcb3 | 2022-10-10 13:40:34 -0700 | [diff] [blame] | 563 | "image_size": attr.int(default = 0, doc = "Size of the assembled image"), |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 564 | "output": attr.string(), |
| 565 | "binaries": attr.label_keyed_string_dict(allow_empty = False), |
| 566 | "_opentitantool": attr.label( |
| 567 | default = "//sw/host/opentitantool:opentitantool", |
| 568 | allow_single_file = True, |
| 569 | executable = True, |
| 570 | cfg = "exec", |
| 571 | ), |
| 572 | }, |
| 573 | ) |
| 574 | |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 575 | def opentitan_binary( |
| 576 | name, |
| 577 | platform = OPENTITAN_PLATFORM, |
Timothy Trippel | 5ef8a53 | 2022-03-29 09:55:03 -0700 | [diff] [blame] | 578 | extract_sw_logs_db = False, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 579 | testonly = True, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 580 | **kwargs): |
| 581 | """A helper macro for generating OpenTitan binary artifacts. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 582 | |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 583 | This macro is mostly a wrapper around cc_binary, but creates artifacts |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 584 | compatible with OpenTitan binaries. The actual artifacts created are outputs |
| 585 | of the rules listed below. |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 586 | Args: |
| 587 | @param name: The name of this rule. |
| 588 | @param platform: The target platform for the artifacts. |
Timothy Trippel | 5ef8a53 | 2022-03-29 09:55:03 -0700 | [diff] [blame] | 589 | @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] | 590 | @param **kwargs: Arguments to forward to `cc_binary`. |
| 591 | Emits rules: |
Timothy Trippel | bc0af18 | 2022-08-16 18:46:11 -0700 | [diff] [blame] | 592 | cc_binary named: <name>.elf |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 593 | rv_preprocess named: <name>_preproc |
| 594 | rv_asm named: <name>_asm |
| 595 | rv_llvm_ir named: <name>_ll |
| 596 | rv_relink_with_map named: <name>_map |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 597 | obj_transform named: <name>_bin |
| 598 | elf_to_dissassembly named: <name>_dis |
| 599 | Optionally: |
| 600 | gen_sim_dv_logs_db named: <name>_logs_db |
| 601 | Returns: |
| 602 | List of targets generated by all of the above rules. |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 603 | """ |
| 604 | |
| 605 | copts = kwargs.pop("copts", []) + [ |
| 606 | "-nostdlib", |
| 607 | "-ffreestanding", |
| 608 | ] |
| 609 | linkopts = kwargs.pop("linkopts", []) + [ |
| 610 | "-nostartfiles", |
| 611 | "-nostdlib", |
| 612 | ] |
| 613 | deps = kwargs.pop("deps", []) |
| 614 | targets = [] |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 615 | side_targets = [] |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 616 | |
Timothy Trippel | 36f9654 | 2022-08-02 18:31:20 -0700 | [diff] [blame] | 617 | native_binary_name = "{}.elf".format(name) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 618 | native.cc_binary( |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 619 | name = native_binary_name, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 620 | deps = deps, |
| 621 | target_compatible_with = _targets_compatible_with[platform], |
| 622 | copts = copts, |
| 623 | linkopts = linkopts, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 624 | testonly = testonly, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 625 | **kwargs |
| 626 | ) |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 627 | |
Miguel Young de la Sota | 7bb7fe7 | 2022-04-01 16:40:02 -0400 | [diff] [blame] | 628 | preproc_name = "{}_{}".format(name, "preproc") |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 629 | side_targets.append(preproc_name) |
Miguel Young de la Sota | 7bb7fe7 | 2022-04-01 16:40:02 -0400 | [diff] [blame] | 630 | rv_preprocess( |
| 631 | name = preproc_name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 632 | target = native_binary_name, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 633 | testonly = testonly, |
Miguel Young de la Sota | 7bb7fe7 | 2022-04-01 16:40:02 -0400 | [diff] [blame] | 634 | ) |
| 635 | |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 636 | asm_name = "{}_{}".format(name, "asm") |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 637 | side_targets.append(asm_name) |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 638 | rv_asm( |
| 639 | name = asm_name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 640 | target = native_binary_name, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 641 | testonly = testonly, |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 642 | ) |
| 643 | |
| 644 | ll_name = "{}_{}".format(name, "ll") |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 645 | side_targets.append(ll_name) |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 646 | rv_llvm_ir( |
| 647 | name = ll_name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 648 | target = native_binary_name, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 649 | testonly = testonly, |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 650 | ) |
| 651 | |
| 652 | map_name = "{}_{}".format(name, "map") |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 653 | side_targets.append(map_name) |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 654 | rv_relink_with_linkmap( |
| 655 | name = map_name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 656 | target = native_binary_name, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 657 | testonly = testonly, |
Miguel Young de la Sota | 4bfbba3 | 2022-03-24 16:12:14 -0400 | [diff] [blame] | 658 | ) |
| 659 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 660 | bin_name = "{}_{}".format(name, "bin") |
| 661 | targets.append(":" + bin_name) |
| 662 | obj_transform( |
| 663 | name = bin_name, |
| 664 | srcs = [native_binary_name], |
| 665 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 666 | testonly = testonly, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 667 | ) |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 668 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 669 | dis_name = "{}_{}".format(name, "dis") |
| 670 | targets.append(":" + dis_name) |
| 671 | elf_to_disassembly( |
| 672 | name = dis_name, |
| 673 | srcs = [native_binary_name], |
| 674 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 675 | testonly = testonly, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 676 | ) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 677 | |
Timothy Trippel | 1f974b3 | 2022-03-28 16:07:09 -0700 | [diff] [blame] | 678 | # Generate log message database for DV sim testbench |
Timothy Trippel | 5ef8a53 | 2022-03-29 09:55:03 -0700 | [diff] [blame] | 679 | if extract_sw_logs_db: |
Timothy Trippel | 6dd8b22 | 2022-03-29 23:09:02 -0700 | [diff] [blame] | 680 | logs_db_name = "{}_{}".format(name, "logs_db") |
Timothy Trippel | 1f974b3 | 2022-03-28 16:07:09 -0700 | [diff] [blame] | 681 | targets.append(":" + logs_db_name) |
| 682 | gen_sim_dv_logs_db( |
| 683 | name = logs_db_name, |
Timothy Trippel | 36f9654 | 2022-08-02 18:31:20 -0700 | [diff] [blame] | 684 | srcs = [native_binary_name], |
Timothy Trippel | 1f974b3 | 2022-03-28 16:07:09 -0700 | [diff] [blame] | 685 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 686 | testonly = testonly, |
Timothy Trippel | 1f974b3 | 2022-03-28 16:07:09 -0700 | [diff] [blame] | 687 | ) |
| 688 | |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 689 | # Create a filegroup with just the sides targets. |
| 690 | native.filegroup( |
| 691 | name = name + "_side_targets", |
| 692 | srcs = side_targets, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 693 | testonly = testonly, |
Timothy Trippel | ddcc799 | 2022-08-05 13:47:32 -0700 | [diff] [blame] | 694 | ) |
| 695 | |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 696 | return targets |
| 697 | |
| 698 | def opentitan_rom_binary( |
| 699 | name, |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 700 | devices = PER_DEVICE_DEPS.keys(), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 701 | platform = OPENTITAN_PLATFORM, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 702 | testonly = True, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 703 | **kwargs): |
| 704 | """A helper macro for generating OpenTitan binary artifacts for ROM. |
| 705 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 706 | This macro is mostly a wrapper around the `opentitan_binary` macro, but also |
| 707 | creates artifacts for each of the keys in `PER_DEVICE_DEPS`. The actual |
| 708 | artifacts created are outputs of the rules emitted by the `opentitan_binary` |
| 709 | macro and those listed below. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 710 | Args: |
| 711 | @param name: The name of this rule. |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 712 | @param devices: List of devices to build the target for. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 713 | @param platform: The target platform for the artifacts. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 714 | @param **kwargs: Arguments to forward to `opentitan_binary`. |
| 715 | Emits rules: |
| 716 | For each device in per_device_deps entry: |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 717 | rules emitted by `opentitan_binary` named: see `opentitan_binary` macro |
| 718 | bin_to_rom_vmem named: <name>_<device>_vmem |
| 719 | elf_to_scrambled_rom_vmem named: <name>_<device>_scr_vmem |
| 720 | filegroup named: <name>_<device> |
| 721 | Containing all targets for a single device for the above generated rules. |
| 722 | filegroup named: <name> |
| 723 | Containing all targets across all devices for the above generated rules. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 724 | """ |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 725 | deps = kwargs.pop("deps", []) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 726 | all_targets = [] |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 727 | for device in devices: |
| 728 | if device not in PER_DEVICE_DEPS: |
| 729 | fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys())) |
| 730 | dev_deps = PER_DEVICE_DEPS[device] |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 731 | devname = "{}_{}".format(name, device) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 732 | dev_targets = [] |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 733 | |
Timothy Trippel | 1f974b3 | 2022-03-28 16:07:09 -0700 | [diff] [blame] | 734 | # Generate ELF, Binary, Disassembly, and (maybe) sim_dv logs database |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 735 | dev_targets.extend(opentitan_binary( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 736 | name = devname, |
| 737 | deps = deps + dev_deps, |
Alphan Ulusoy | e77043f | 2022-08-29 10:23:46 -0400 | [diff] [blame] | 738 | extract_sw_logs_db = device == "sim_dv", |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 739 | testonly = testonly, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 740 | **kwargs |
| 741 | )) |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 742 | |
| 743 | # We need to generate VMEM files even for FPGA devices, because we use |
| 744 | # them for bitstream splicing. |
Timothy Trippel | 36f9654 | 2022-08-02 18:31:20 -0700 | [diff] [blame] | 745 | elf_name = "{}.{}".format(devname, "elf") |
Miles Dai | 1634fac | 2022-05-13 17:55:35 -0400 | [diff] [blame] | 746 | bin_name = "{}_{}".format(devname, "bin") |
| 747 | |
| 748 | # Generate Un-scrambled ROM VMEM |
| 749 | vmem_name = "{}_vmem".format(devname) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 750 | dev_targets.append(":" + vmem_name) |
Miles Dai | 1634fac | 2022-05-13 17:55:35 -0400 | [diff] [blame] | 751 | bin_to_vmem( |
| 752 | name = vmem_name, |
| 753 | bin = bin_name, |
| 754 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 755 | testonly = testonly, |
Miles Dai | 91d811e | 2022-05-10 15:51:17 -0400 | [diff] [blame] | 756 | word_size = 32, |
Miles Dai | 1634fac | 2022-05-13 17:55:35 -0400 | [diff] [blame] | 757 | ) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 758 | |
| 759 | # Generate Scrambled ROM VMEM |
| 760 | scr_vmem_name = "{}_scr_vmem".format(devname) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 761 | dev_targets.append(":" + scr_vmem_name) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 762 | elf_to_scrambled_rom_vmem( |
| 763 | name = scr_vmem_name, |
| 764 | srcs = [elf_name], |
| 765 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 766 | testonly = testonly, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 767 | ) |
| 768 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 769 | # Create a filegroup with just the current device's targets. |
| 770 | native.filegroup( |
| 771 | name = devname, |
| 772 | srcs = dev_targets, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 773 | testonly = testonly, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 774 | ) |
| 775 | all_targets.extend(dev_targets) |
| 776 | |
| 777 | # Create a filegroup with just all targets from all devices. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 778 | native.filegroup( |
| 779 | name = name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 780 | srcs = all_targets, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 781 | testonly = testonly, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 782 | ) |
| 783 | |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 784 | def _pick_correct_archive_for_device(ctx): |
| 785 | cc_infos = [] |
| 786 | for dep in ctx.attr.deps: |
| 787 | if CcInfo in dep: |
| 788 | cc_info = dep[CcInfo] |
| 789 | elif ArchiveInfo in dep: |
| 790 | cc_info = dep[ArchiveInfo].archive_infos[ctx.attr.device] |
| 791 | else: |
| 792 | fail("Expected either a CcInfo or an ArchiveInfo") |
| 793 | cc_infos.append(cc_info) |
| 794 | return [cc_common.merge_cc_infos(cc_infos = cc_infos)] |
| 795 | |
| 796 | pick_correct_archive_for_device = rv_rule( |
| 797 | implementation = _pick_correct_archive_for_device, |
| 798 | attrs = { |
| 799 | "deps": attr.label_list(allow_files = True), |
| 800 | "device": attr.string(), |
| 801 | }, |
| 802 | fragments = ["cpp"], |
| 803 | toolchains = ["@rules_cc//cc:toolchain_type"], |
| 804 | ) |
| 805 | |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 806 | def opentitan_multislot_flash_binary( |
| 807 | name, |
| 808 | srcs, |
Chris Frantz | af9bcb3 | 2022-10-10 13:40:34 -0700 | [diff] [blame] | 809 | image_size = 0, |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 810 | devices = PER_DEVICE_DEPS.keys(), |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 811 | platform = OPENTITAN_PLATFORM, |
| 812 | testonly = True): |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 813 | """A helper macro for generating multislot OpenTitan binary flash images. |
| 814 | |
| 815 | This macro is mostly a wrapper around the `assemble_flash_image` rule, that |
| 816 | invokes `opentitantool` to stitch together multiple `opentitan_flash_binary` |
Timothy Trippel | fcc8bac | 2022-08-23 17:20:24 -0700 | [diff] [blame] | 817 | images to create a single image for bootstrapping. Since bootstrap erases |
| 818 | the flash for programming this is the only way to load multiple |
| 819 | (A/B/Virtual) slots and (silicon creator, ROM_EXT, and owner, BL0) stages at |
| 820 | the same time. |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 821 | Args: |
| 822 | @param name: The name of this rule. |
| 823 | @param srcs: A dictionary of `opentitan_flash_binary` targets (to stitch |
| 824 | together) as keys, and key/offset configurations as values. |
Chris Frantz | af9bcb3 | 2022-10-10 13:40:34 -0700 | [diff] [blame] | 825 | @param image_size: The final flash image_size to pass to `opentitantool` |
| 826 | (optional). |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 827 | @param devices: List of devices to build the target for. |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 828 | @param platform: The target platform for the artifacts. |
| 829 | Emits rules: |
| 830 | For each device in per_device_deps entry: |
| 831 | rules emitted by `opentitan_binary` named: see `opentitan_binary` macro |
| 832 | assemble_flash_image named: <name>_<device>_bin_signed |
| 833 | bin_to_vmem named: <name>_<device>_vmem64_signed |
| 834 | scrambled_flash_vmem named: <name>_<device>_scr_vmem64_signed |
| 835 | filegroup named: <name>_<device> |
| 836 | Containing all targets for a single device for the above generated rules. |
| 837 | filegroup named: <name> |
| 838 | Containing all targets across all devices for the above generated rules. |
| 839 | """ |
| 840 | all_targets = [] |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 841 | for device in devices: |
| 842 | if device not in PER_DEVICE_DEPS: |
| 843 | fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys())) |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 844 | devname = "{}_{}".format(name, device) |
| 845 | dev_targets = [] |
| 846 | signed_dev_binaries = {} |
| 847 | for src, configs in srcs.items(): |
| 848 | if "key" not in configs: |
| 849 | fail("Missing signing key for binary: {}".format(src)) |
| 850 | if "offset" not in configs: |
| 851 | fail("Missing offset for binary: {}".format(src)) |
| 852 | signed_dev_binary = "{}_{}_bin_signed_{}".format( |
| 853 | src, |
| 854 | device, |
| 855 | configs["key"], |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 856 | testonly = testonly, |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 857 | ) |
| 858 | signed_dev_binaries[signed_dev_binary] = configs["offset"] |
| 859 | |
| 860 | # Assemble the signed binaries into a single binary. |
| 861 | signed_bin_name = "{}_bin_signed".format(devname) |
| 862 | dev_targets.append(":" + signed_bin_name) |
| 863 | assemble_flash_image( |
| 864 | name = signed_bin_name, |
| 865 | output = "{}.signed.bin".format(devname), |
| 866 | image_size = image_size, |
| 867 | binaries = signed_dev_binaries, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 868 | testonly = testonly, |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 869 | ) |
| 870 | |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 871 | # We only need to generate VMEM files for sim devices. |
| 872 | if device in ["sim_dv", "sim_verilator"]: |
| 873 | # Generate a VMEM64 from the binary. |
| 874 | signed_vmem_name = "{}_vmem64_signed".format(devname) |
| 875 | dev_targets.append(":" + signed_vmem_name) |
| 876 | bin_to_vmem( |
| 877 | name = signed_vmem_name, |
| 878 | bin = signed_bin_name, |
| 879 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 880 | testonly = testonly, |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 881 | word_size = 64, # Backdoor-load VMEM image uses 64-bit words |
| 882 | ) |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 883 | |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 884 | # Scramble signed VMEM64. |
| 885 | scr_signed_vmem_name = "{}_scr_vmem64_signed".format(devname) |
| 886 | dev_targets.append(":" + scr_signed_vmem_name) |
| 887 | scramble_flash_vmem( |
| 888 | name = scr_signed_vmem_name, |
| 889 | vmem = signed_vmem_name, |
| 890 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 891 | testonly = testonly, |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 892 | ) |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 893 | |
| 894 | # Create a filegroup with just the current device's targets. |
| 895 | native.filegroup( |
| 896 | name = devname, |
| 897 | srcs = dev_targets, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 898 | testonly = testonly, |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 899 | ) |
| 900 | dev_targets.extend(dev_targets) |
| 901 | |
| 902 | # Create a filegroup with all assembled flash images. |
| 903 | native.filegroup( |
| 904 | name = name, |
| 905 | srcs = all_targets, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 906 | testonly = testonly, |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 907 | ) |
| 908 | |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 909 | def opentitan_flash_binary( |
| 910 | name, |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 911 | devices = PER_DEVICE_DEPS.keys(), |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 912 | platform = OPENTITAN_PLATFORM, |
Timothy Trippel | 1ad4ad6 | 2022-08-23 17:01:28 -0700 | [diff] [blame] | 913 | signing_keys = DEFAULT_SIGNING_KEYS, |
Alphan Ulusoy | c8b2a20 | 2022-09-14 12:39:42 -0400 | [diff] [blame] | 914 | signed = True, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 915 | testonly = True, |
Alphan Ulusoy | c8b2a20 | 2022-09-14 12:39:42 -0400 | [diff] [blame] | 916 | manifest = "//sw/device/silicon_creator/rom_ext:manifest_standard", |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 917 | **kwargs): |
| 918 | """A helper macro for generating OpenTitan binary artifacts for flash. |
| 919 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 920 | This macro is mostly a wrapper around the `opentitan_binary` macro, but also |
| 921 | creates artifacts for each of the keys in `PER_DEVICE_DEPS`, and if signing |
| 922 | is enabled, each of the keys in `signing_keys`. The actual artifacts created |
| 923 | artifacts created are outputs of the rules emitted by the `opentitan_binary` |
| 924 | macro and those listed below. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 925 | Args: |
| 926 | @param name: The name of this rule. |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 927 | @param devices: List of devices to build the target for. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 928 | @param platform: The target platform for the artifacts. |
| 929 | @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] | 930 | @param signed: Whether or not to emit signed binary/VMEM files. |
Timothy Trippel | 49cd1a1 | 2022-08-10 14:11:51 -0700 | [diff] [blame] | 931 | @param manifest: Partially populated manifest to set boot stage/slot configs. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 932 | @param **kwargs: Arguments to forward to `opentitan_binary`. |
| 933 | Emits rules: |
| 934 | For each device in per_device_deps entry: |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 935 | rules emitted by `opentitan_binary` named: see `opentitan_binary` macro |
| 936 | bin_to_vmem named: <name>_<device>_vmem64 |
| 937 | scrambled_flash_vmem named: <name>_<device>_scr_vmem64 |
| 938 | Optionally: |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 939 | sign_bin named: <name>_<device>_bin_signed_<key_name> |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 940 | bin_to_vmem named: <name>_<device>_vmem64_signed_<key_name> |
| 941 | scrambled_flash_vmem named: <name>_<device>_scr_vmem64_signed_<key_name> |
| 942 | filegroup named: <name>_<device> |
| 943 | Containing all targets for a single device for the above generated rules. |
| 944 | filegroup named: <name> |
| 945 | Containing all targets across all devices for the above generated rules. |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 946 | """ |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 947 | deps = kwargs.pop("deps", []) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 948 | all_targets = [] |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 949 | for device in devices: |
| 950 | if device not in PER_DEVICE_DEPS: |
| 951 | fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys())) |
| 952 | dev_deps = PER_DEVICE_DEPS[device] |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 953 | devname = "{}_{}".format(name, device) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 954 | dev_targets = [] |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 955 | |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 956 | depname = "{}_deps".format(devname) |
| 957 | pick_correct_archive_for_device( |
| 958 | name = depname, |
| 959 | deps = deps + dev_deps, |
| 960 | device = device, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 961 | testonly = testonly, |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 962 | ) |
| 963 | |
Timothy Trippel | 1f974b3 | 2022-03-28 16:07:09 -0700 | [diff] [blame] | 964 | # Generate ELF, Binary, Disassembly, and (maybe) sim_dv logs database |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 965 | dev_targets.extend(opentitan_binary( |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 966 | name = devname, |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 967 | deps = [depname], |
Alphan Ulusoy | e77043f | 2022-08-29 10:23:46 -0400 | [diff] [blame] | 968 | extract_sw_logs_db = device == "sim_dv", |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 969 | testonly = testonly, |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 970 | **kwargs |
| 971 | )) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 972 | bin_name = "{}_{}".format(devname, "bin") |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 973 | |
Timothy Trippel | ba061ed | 2022-03-23 00:30:22 -0700 | [diff] [blame] | 974 | # Sign BIN (if required) and generate scrambled VMEM images. |
Timothy Trippel | b867c1c | 2022-08-03 23:42:16 -0700 | [diff] [blame] | 975 | if signed: |
Timothy Trippel | 49cd1a1 | 2022-08-10 14:11:51 -0700 | [diff] [blame] | 976 | if manifest == None: |
| 977 | fail("A 'manifest' must be provided in order to sign flash images.") |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 978 | for (key_name, key) in signing_keys.items(): |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 979 | # Sign the Binary. |
| 980 | signed_bin_name = "{}_bin_signed_{}".format(devname, key_name) |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 981 | dev_targets.append(":" + signed_bin_name) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 982 | sign_bin( |
| 983 | name = signed_bin_name, |
| 984 | bin = bin_name, |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 985 | key = key, |
| 986 | key_name = key_name, |
Chris Frantz | bac0454 | 2022-06-16 09:57:29 -0700 | [diff] [blame] | 987 | manifest = manifest, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 988 | testonly = testonly, |
Timothy Trippel | 92173aa | 2022-02-15 23:41:41 -0800 | [diff] [blame] | 989 | ) |
| 990 | |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 991 | # We only need to generate VMEM files for sim devices. |
| 992 | if device in ["sim_dv", "sim_verilator"]: |
| 993 | # Generate a VMEM64 from the signed binary. |
| 994 | signed_vmem_name = "{}_vmem64_signed_{}".format( |
| 995 | devname, |
| 996 | key_name, |
| 997 | ) |
| 998 | dev_targets.append(":" + signed_vmem_name) |
| 999 | bin_to_vmem( |
| 1000 | name = signed_vmem_name, |
| 1001 | bin = signed_bin_name, |
| 1002 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 1003 | testonly = testonly, |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 1004 | word_size = 64, # Backdoor-load VMEM image uses 64-bit words |
| 1005 | ) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 1006 | |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 1007 | # Scramble signed VMEM64. |
| 1008 | scr_signed_vmem_name = "{}_scr_vmem64_signed_{}".format( |
| 1009 | devname, |
| 1010 | key_name, |
| 1011 | ) |
| 1012 | dev_targets.append(":" + scr_signed_vmem_name) |
| 1013 | scramble_flash_vmem( |
| 1014 | name = scr_signed_vmem_name, |
| 1015 | vmem = signed_vmem_name, |
| 1016 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 1017 | testonly = testonly, |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 1018 | ) |
Timothy Trippel | ba061ed | 2022-03-23 00:30:22 -0700 | [diff] [blame] | 1019 | |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 1020 | # We only need to generate VMEM files for sim devices. |
| 1021 | if device in ["sim_dv", "sim_verilator"]: |
| 1022 | # Generate a VMEM64 from the binary. |
| 1023 | vmem_name = "{}_vmem64".format(devname) |
| 1024 | dev_targets.append(":" + vmem_name) |
| 1025 | bin_to_vmem( |
| 1026 | name = vmem_name, |
| 1027 | bin = bin_name, |
| 1028 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 1029 | testonly = testonly, |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 1030 | word_size = 64, # Backdoor-load VMEM image uses 64-bit words |
| 1031 | ) |
Timothy Trippel | b867c1c | 2022-08-03 23:42:16 -0700 | [diff] [blame] | 1032 | |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 1033 | # Scramble VMEM64. |
| 1034 | scr_vmem_name = "{}_scr_vmem64".format(devname) |
| 1035 | dev_targets.append(":" + scr_vmem_name) |
| 1036 | scramble_flash_vmem( |
| 1037 | name = scr_vmem_name, |
| 1038 | vmem = vmem_name, |
| 1039 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 1040 | testonly = testonly, |
Timothy Trippel | 77c09d5 | 2022-09-02 12:22:02 -0700 | [diff] [blame] | 1041 | ) |
Timothy Trippel | e3a7c57 | 2022-02-17 17:30:47 -0800 | [diff] [blame] | 1042 | |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 1043 | # Create a filegroup with just the current device's targets. |
| 1044 | native.filegroup( |
| 1045 | name = devname, |
| 1046 | srcs = dev_targets, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 1047 | testonly = testonly, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 1048 | ) |
| 1049 | all_targets.extend(dev_targets) |
| 1050 | |
| 1051 | # Create a filegroup with just all targets from all devices. |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 1052 | native.filegroup( |
| 1053 | name = name, |
Timothy Trippel | 3821d5d | 2022-08-02 10:20:49 -0700 | [diff] [blame] | 1054 | srcs = all_targets, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 1055 | testonly = testonly, |
Drew Macrae | 7ac1efb | 2021-09-22 16:08:28 +0000 | [diff] [blame] | 1056 | ) |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1057 | |
| 1058 | def opentitan_ram_binary( |
| 1059 | name, |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1060 | archive_symbol_prefix, |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 1061 | devices = PER_DEVICE_DEPS.keys(), |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1062 | platform = OPENTITAN_PLATFORM, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 1063 | testonly = True, |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1064 | **kwargs): |
| 1065 | """A helper macro for generating OpenTitan binary artifacts for RAM. |
| 1066 | |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1067 | This macro is mostly a wrapper around the `opentitan_binary` macro, but also |
| 1068 | creates artifacts for each of the keys in `PER_DEVICE_DEPS`. The actual |
| 1069 | artifacts created are outputs of the rules emitted by the `opentitan_binary` |
| 1070 | macro and those listed below. |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1071 | Args: |
| 1072 | @param name: The name of this rule. |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1073 | @param archive_symbol_prefix: Prefix used to rename symbols in the binary. |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 1074 | @param devices: List of devices to build the target for. |
| 1075 | @param platform: The target platform for the artifacts. |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1076 | @param **kwargs: Arguments to forward to `opentitan_binary`. |
| 1077 | Emits rules: |
| 1078 | For each device in per_device_deps entry: |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1079 | rules emitted by `opentitan_binary` named: see `opentitan_binary` macro |
| 1080 | bin_to_archive named: <name> |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1081 | """ |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1082 | deps = kwargs.pop("deps", []) |
| 1083 | hdrs = kwargs.pop("hdrs", []) |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1084 | binaries = [] |
Timothy Trippel | 3e3f460 | 2022-09-02 14:30:45 -0700 | [diff] [blame] | 1085 | for device in devices: |
| 1086 | if device not in PER_DEVICE_DEPS: |
| 1087 | fail("invalid device; device must be in {}".format(PER_DEVICE_DEPS.keys())) |
| 1088 | dev_deps = PER_DEVICE_DEPS[device] |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1089 | devname = "{}_{}".format(name, device) |
Timothy Trippel | f742a36 | 2022-11-18 11:09:54 -0800 | [diff] [blame] | 1090 | dev_targets = [] |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1091 | |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1092 | # Generate the binary. |
Timothy Trippel | f742a36 | 2022-11-18 11:09:54 -0800 | [diff] [blame] | 1093 | dev_targets.extend(opentitan_binary( |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1094 | name = devname, |
| 1095 | deps = deps + dev_deps, |
Timothy Trippel | f742a36 | 2022-11-18 11:09:54 -0800 | [diff] [blame] | 1096 | extract_sw_logs_db = device == "sim_dv", |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 1097 | testonly = testonly, |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1098 | **kwargs |
Timothy Trippel | f742a36 | 2022-11-18 11:09:54 -0800 | [diff] [blame] | 1099 | )) |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1100 | bin_name = "{}_{}".format(devname, "bin") |
| 1101 | binaries.append(":" + bin_name) |
| 1102 | |
Timothy Trippel | f742a36 | 2022-11-18 11:09:54 -0800 | [diff] [blame] | 1103 | # Generate Un-scrambled RAM VMEM (for testing SRAM injection in DV) |
| 1104 | vmem_name = "{}_vmem".format(devname) |
| 1105 | dev_targets.append(":" + vmem_name) |
| 1106 | bin_to_vmem( |
| 1107 | name = vmem_name, |
| 1108 | bin = bin_name, |
| 1109 | platform = platform, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 1110 | testonly = testonly, |
Timothy Trippel | f742a36 | 2022-11-18 11:09:54 -0800 | [diff] [blame] | 1111 | word_size = 32, |
| 1112 | ) |
| 1113 | |
| 1114 | # Create a filegroup with just the current device's targets. |
| 1115 | native.filegroup( |
| 1116 | name = devname, |
| 1117 | srcs = dev_targets, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 1118 | testonly = testonly, |
Timothy Trippel | f742a36 | 2022-11-18 11:09:54 -0800 | [diff] [blame] | 1119 | ) |
| 1120 | |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1121 | # Generate the archive file. |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1122 | bin_to_archive( |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1123 | name = name, |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1124 | hdrs = hdrs, |
| 1125 | binaries = binaries, |
Timothy Trippel | 90c0de7 | 2022-08-05 17:18:23 -0700 | [diff] [blame] | 1126 | devices = PER_DEVICE_DEPS.keys(), |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1127 | archive_symbol_prefix = archive_symbol_prefix, |
Alphan Ulusoy | 304038c | 2022-11-15 14:15:59 -0500 | [diff] [blame] | 1128 | testonly = testonly, |
Alphan Ulusoy | 9672599 | 2022-06-27 14:56:59 -0400 | [diff] [blame] | 1129 | ) |