diff --git a/hw/ip/otp_ctrl/data/otp_ctrl_img.h.tpl b/hw/ip/otp_ctrl/data/otp_ctrl_img.c.tpl similarity index 91% rename from hw/ip/otp_ctrl/data/otp_ctrl_img.h.tpl rename to hw/ip/otp_ctrl/data/otp_ctrl_img.c.tpl index aee2cf1381413..855e4b703078b 100644 --- a/hw/ip/otp_ctrl/data/otp_ctrl_img.h.tpl +++ b/hw/ip/otp_ctrl/data/otp_ctrl_img.c.tpl @@ -6,18 +6,12 @@ ${fileheader} // AUTOGENERATED. Do not edit this file by hand. // See the hw/ip/otp_ctrl/data README for details. -#ifndef ${include_guard} -#define ${include_guard} +#include -// See the following include file for details on the types used in this header -// file. +// See the following include file for details on the types used in this file. #include "sw/device/silicon_creator/manuf/lib/otp_img_types.h" -#include "otp_ctrl_regs.h" - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus +#include "otp_ctrl_regs.h" // Generated. <% def ToPascalCase(in_str): @@ -123,7 +117,8 @@ ${ConstTypeDefinition(item, data[partition_name]["alignment"])} alignment = data[partition_name]["alignment"] %> // Partition ${partition_name} -static otp_kv_t ${"kOtpKv" + ToPascalCase(partition_name)}[] = { +const size_t ${"kOtpKv" + ToPascalCase(partition_name) + "Size"} = ${len(data[partition_name]["items"])}; +const otp_kv_t ${"kOtpKv" + ToPascalCase(partition_name)}[] = { % for item in data[partition_name]["items"]: { .type = ${ToOtpValType(alignment)}, @@ -134,11 +129,3 @@ static otp_kv_t ${"kOtpKv" + ToPascalCase(partition_name)}[] = { % endfor }; % endfor - - -#ifdef __cplusplus -} // extern "C" -#endif // __cplusplus - -#endif // ${include_guard} - diff --git a/hw/ip/otp_ctrl/data/sku_earlgrey_a0/BUILD b/hw/ip/otp_ctrl/data/sku_earlgrey_a0/BUILD index 81ff2b324d213..68e090463751b 100644 --- a/hw/ip/otp_ctrl/data/sku_earlgrey_a0/BUILD +++ b/hw/ip/otp_ctrl/data/sku_earlgrey_a0/BUILD @@ -2,7 +2,7 @@ # Licensed under the Apache License, Version 2.0, see LICENSE for details. # SPDX-License-Identifier: Apache-2.0 -load("//rules:otp.bzl", "otp_image_header", "otp_json") +load("//rules:otp.bzl", "otp_image_consts", "otp_json") package(default_visibility = ["//visibility:public"]) @@ -12,8 +12,8 @@ otp_json( seed = "94259314771464387", ) -otp_image_header( - name = "otp_img_sku_earlgrey_a0_stage_individualize", +otp_image_consts( + name = "otp_img_consts_c_file", src = ":otp_json_baseline", overlays = [ "//hw/ip/otp_ctrl/data:otp_json_creator_sw_cfg", @@ -21,3 +21,12 @@ otp_image_header( "//hw/ip/otp_ctrl/data:otp_json_alert_digest_cfg", ], ) + +cc_library( + name = "otp_img_consts", + srcs = [":otp_img_consts_c_file"], + deps = [ + "//hw/ip/otp_ctrl/data:otp_ctrl_regs", + "//sw/device/silicon_creator/manuf/lib:otp_img_types", + ], +) diff --git a/rules/otp.bzl b/rules/otp.bzl index c8e9897872776..d78c9477907ce 100644 --- a/rules/otp.bzl +++ b/rules/otp.bzl @@ -39,7 +39,7 @@ format expected by the image generation tool. load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("@bazel_skylib//lib:new_sets.bzl", "sets") load("//rules:host.bzl", "host_tools_transition") -load("//rules:const.bzl", "CONST", "hex", "hex_digits") +load("//rules:const.bzl", "CONST", "hex") def get_otp_images(): """Returns a list of (otp_name, img_target) tuples. @@ -246,8 +246,8 @@ otp_image = rule( }, ) -def _otp_image_header(ctx): - output = ctx.actions.declare_file(ctx.attr.name + ".h") +def _otp_image_consts_impl(ctx): + output = ctx.actions.declare_file(ctx.attr.name + ".c") args = ctx.actions.args() if not ctx.attr.verbose: args.add("--quiet") @@ -257,14 +257,14 @@ def _otp_image_header(ctx): args.add("--lc-seed", ctx.attr.lc_seed[BuildSettingInfo].value) args.add("--otp-seed", ctx.attr.otp_seed[BuildSettingInfo].value) args.add("--img-cfg", ctx.file.src) - args.add("--header-template", ctx.file.header_template) - args.add("--header-out", "{}/{}.h".format(output.dirname, ctx.attr.name)) + args.add("--c-template", ctx.file.c_template) + args.add("--c-out", "{}/{}.c".format(output.dirname, ctx.attr.name)) args.add_all(ctx.files.overlays, before_each = "--add-cfg") ctx.actions.run( outputs = [output], inputs = [ ctx.file.src, - ctx.file.header_template, + ctx.file.c_template, ctx.file.lc_state_def, ctx.file.mmap_def, ] + ctx.files.overlays, @@ -272,19 +272,11 @@ def _otp_image_header(ctx): executable = ctx.executable._tool, ) return [ - CcInfo(compilation_context = cc_common.create_compilation_context( - includes = depset([output.dirname]), - headers = depset([output]), - defines = depset(["RULE_NAME=\"{}\"".format(ctx.label.name)]), - )), DefaultInfo(files = depset([output]), runfiles = ctx.runfiles(files = [output])), - OutputGroupInfo( - header = depset([output]), - ), ] -otp_image_header = rule( - implementation = _otp_image_header, +otp_image_consts = rule( + implementation = _otp_image_consts_impl, attrs = { "src": attr.label( allow_single_file = [".json", ".hjson"], @@ -316,9 +308,9 @@ otp_image_header = rule( default = "//hw/ip/otp_ctrl/data:otp_seed", doc = "Configuration override seed used to randomize OTP netlist constants.", ), - "header_template": attr.label( + "c_template": attr.label( allow_single_file = True, - default = "//hw/ip/otp_ctrl/data:otp_ctrl_img.h.tpl", + default = "//hw/ip/otp_ctrl/data:otp_ctrl_img.c.tpl", doc = "OTP image header template.", ), "verbose": attr.bool( diff --git a/sw/device/silicon_creator/manuf/lib/BUILD b/sw/device/silicon_creator/manuf/lib/BUILD index 809c67608a9af..3be69c99e13e7 100644 --- a/sw/device/silicon_creator/manuf/lib/BUILD +++ b/sw/device/silicon_creator/manuf/lib/BUILD @@ -106,14 +106,14 @@ opentitan_functest( ) cc_library( - name = "individualize_preop", - srcs = ["individualize_preop.c"], - hdrs = [ + name = "individualize_preop_generic", + srcs = [ + "individualize_preop.c", "individualize_preop.h", ], deps = [ ":otp_img", - "//hw/ip/otp_ctrl/data/sku_earlgrey_a0:otp_img_sku_earlgrey_a0_stage_individualize", + ":otp_img_types", "//sw/device/lib/base:status", "//sw/device/lib/dif:otp_ctrl", "//sw/device/lib/testing:lc_ctrl_testutils", @@ -121,6 +121,16 @@ cc_library( ], ) +# As more SKUs are created with different OTP software configuration partitions, +# libraries can be added accordingly. +cc_library( + name = "individualize_preop_sku_earlgrey_a0", + deps = [ + ":individualize_preop_generic", + "//hw/ip/otp_ctrl/data/sku_earlgrey_a0:otp_img_consts", + ], +) + opentitan_functest( name = "individualize_preop_functest", srcs = ["individualize_preop_functest.c"], @@ -132,7 +142,7 @@ opentitan_functest( "cw310_rom_with_fake_keys", ], deps = [ - ":individualize_preop", + ":individualize_preop_sku_earlgrey_a0", "//hw/ip/otp_ctrl/data:otp_ctrl_regs", "//hw/top_earlgrey/sw/autogen:top_earlgrey", "//sw/device/lib/base:status", diff --git a/sw/device/silicon_creator/manuf/lib/individualize_preop.c b/sw/device/silicon_creator/manuf/lib/individualize_preop.c index 12f6fab78f112..53aaa5866cf24 100644 --- a/sw/device/silicon_creator/manuf/lib/individualize_preop.c +++ b/sw/device/silicon_creator/manuf/lib/individualize_preop.c @@ -4,14 +4,14 @@ #include "sw/device/silicon_creator/manuf/lib/individualize_preop.h" -#include "otp_img_sku_earlgrey_a0_stage_individualize.h" // Generated. #include "sw/device/lib/dif/dif_otp_ctrl.h" #include "sw/device/silicon_creator/manuf/lib/otp_img.h" +#include "sw/device/silicon_creator/manuf/lib/otp_img_types.h" status_t manuf_individualize_device_sw_cfg(const dif_otp_ctrl_t *otp_ctrl) { TRY(otp_img_write(otp_ctrl, kDifOtpCtrlPartitionCreatorSwCfg, - kOtpKvCreatorSwCfg, ARRAYSIZE(kOtpKvCreatorSwCfg))); + kOtpKvCreatorSwCfg, kOtpKvCreatorSwCfgSize)); TRY(otp_img_write(otp_ctrl, kDifOtpCtrlPartitionOwnerSwCfg, kOtpKvOwnerSwCfg, - ARRAYSIZE(kOtpKvOwnerSwCfg))); + kOtpKvOwnerSwCfgSize)); return OK_STATUS(); } diff --git a/sw/device/silicon_creator/manuf/lib/individualize_preop.h b/sw/device/silicon_creator/manuf/lib/individualize_preop.h index 08652ca0148f7..6189b2b045c94 100644 --- a/sw/device/silicon_creator/manuf/lib/individualize_preop.h +++ b/sw/device/silicon_creator/manuf/lib/individualize_preop.h @@ -7,6 +7,19 @@ #include "sw/device/lib/base/status.h" #include "sw/device/lib/dif/dif_otp_ctrl.h" +#include "sw/device/silicon_creator/manuf/lib/otp_img.h" + +/** + * OTP Creator Software Configuration Partition. + */ +extern const size_t kOtpKvCreatorSwCfgSize; +extern const otp_kv_t kOtpKvCreatorSwCfg[]; + +/** + * OTP Owner Software Configuration Partition. + */ +extern const size_t kOtpKvOwnerSwCfgSize; +extern const otp_kv_t kOtpKvOwnerSwCfg[]; /** * Configures the CREATOR_SW_CFG and OWNER_SW_CFG OTP partitions. diff --git a/sw/device/silicon_creator/manuf/tests/BUILD b/sw/device/silicon_creator/manuf/tests/BUILD index 6482682d48d38..f13ab1aacae5f 100644 --- a/sw/device/silicon_creator/manuf/tests/BUILD +++ b/sw/device/silicon_creator/manuf/tests/BUILD @@ -311,7 +311,7 @@ opentitan_ram_binary( "//sw/device/lib/testing:pinmux_testutils", "//sw/device/lib/testing/test_framework:check", "//sw/device/lib/testing/test_framework:status", - "//sw/device/silicon_creator/manuf/lib:individualize_preop", + "//sw/device/silicon_creator/manuf/lib:individualize_preop_sku_earlgrey_a0", "//sw/device/silicon_creator/manuf/lib:isolated_flash_partition", "//sw/device/silicon_creator/manuf/lib:sram_program_linker_script", "//sw/device/silicon_creator/manuf/lib:sram_start", diff --git a/util/design/gen-otp-img.py b/util/design/gen-otp-img.py index 45503b15b9cf5..30a7fb9b6c55d 100755 --- a/util/design/gen-otp-img.py +++ b/util/design/gen-otp-img.py @@ -207,18 +207,18 @@ def main(): The mapping must be bijective - otherwise this will generate an error. ''') - parser.add_argument('--header-template', + parser.add_argument('--c-template', type=Path, metavar='', help=''' - Template file used to generate C header version of the OTP image. - This flag is only required when --header-out is set. + Template file used to generate C version of the OTP image. + This flag is only required when --c-out is set. ''') - parser.add_argument('--header-out', + parser.add_argument('--c-out', type=Path, metavar='', help=''' - C header output path. Requires the --header-template flag to be + C output path. Requires the --c-template flag to be set. The --out flag is ignored when this flag is set. ''') @@ -280,12 +280,10 @@ def main(): file_header = '// Generated on {} with\n// $ gen-otp-img.py {}\n//\n'.format( dtstr, argstr) - if args.header_out: - log.info(f'Generating header file: {args.header_out}') - file_body = otp_mem_img.generate_headerfile(args.header_out, - file_header, - args.header_template) - with open(args.header_out, 'wb') as outfile: + if args.c_out: + log.info(f'Generating C file: {args.c_out}') + file_body = otp_mem_img.generate_c_file(file_header, args.c_template) + with open(args.c_out, 'wb') as outfile: outfile.write(file_body.encode('utf-8')) exit(0) diff --git a/util/design/lib/OtpMemImg.py b/util/design/lib/OtpMemImg.py index 8971dfc12b33e..1df4631866cf9 100644 --- a/util/design/lib/OtpMemImg.py +++ b/util/design/lib/OtpMemImg.py @@ -529,12 +529,10 @@ def streamout_memfile(self) -> Tuple[str, int]: return _to_memfile_with_ecc(data, annotation, self.lc_state.config, self.data_perm) - def generate_headerfile(self, outfile: str, fileheader: str, - templatefile: Path) -> str: - '''Generates header file with provided `header` and `templatefile`. + def generate_c_file(self, fileheader: str, templatefile: Path) -> str: + '''Generates C file with provided `header` and `templatefile`. Args: - outfile: Output file path. Used to generate include guards. fileheader: Header to be appended to autogenerated file. templatefile: Mako template used to generate header file. Returns: @@ -573,12 +571,8 @@ def generate_headerfile(self, outfile: str, fileheader: str, 'items': items, } - include_guard = common.path_to_include_guard(outfile) - with open(templatefile, 'r') as tplfile: tpl = Template(tplfile.read()) - result = tpl.render(include_guard=include_guard, - fileheader=fileheader, - data=data) + result = tpl.render(fileheader=fileheader, data=data) return result