Skip to content

Commit

Permalink
[rom_ext_e2e] Test owner flash configuration
Browse files Browse the repository at this point in the history
`flash_permission_test`: Fixes: #24476
Adds a test harness that verifies the flash configuration state of data
regions.

Signed-off-by: Chris Frantz <[email protected]>
(cherry picked from commit 890e5bf)
  • Loading branch information
cfrantz authored and moidx committed Oct 30, 2024
1 parent bb51827 commit 52d6e0f
Show file tree
Hide file tree
Showing 5 changed files with 503 additions and 9 deletions.
50 changes: 50 additions & 0 deletions sw/device/silicon_creator/rom_ext/e2e/ownership/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ opentitan_binary(
],
)

opentitan_binary(
name = "flash_regions",
testonly = True,
srcs = ["flash_regions.c"],
exec_env = {
"//hw/top_earlgrey:fpga_cw310_rom_ext": None,
},
rsa_key = {
"//sw/device/silicon_creator/lib/ownership/keys/dummy:app_prod": "app_prod",
},
deps = [
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
"//sw/device/lib/base:status",
"//sw/device/lib/dif:flash_ctrl",
"//sw/device/lib/testing/test_framework:ottf_main",
],
)

# rom_ext_e2e_testplan.hjson%rom_ext_e2e_transfer_any_test
ownership_transfer_test(
name = "transfer_any_test",
Expand Down Expand Up @@ -318,3 +336,35 @@ ownership_transfer_test(
test_harness = "//sw/host/tests/ownership:rescue_permission_test",
),
)

# rom_ext_e2e_testplan.hjson%rom_ext_e2e_flash_permission_test
# Note: rescue-after-activate tests that rescue correctly accesses regions with
# different scrambling/ECC properties than the default flash configuration.
ownership_transfer_test(
name = "flash_permission_test",
srcs = ["flash_regions.c"],
fpga = fpga_params(
binaries = {
":flash_regions": "flash_regions",
},
test_cmd = """
--clear-bitstream
--bootstrap={firmware}
--unlock-mode=Any
--unlock-key=$(location //sw/device/silicon_creator/lib/ownership/keys/fake:unlock_key)
--next-owner-key=$(location //sw/device/silicon_creator/lib/ownership/keys/dummy:owner_key)
--next-unlock-key=$(location //sw/device/silicon_creator/lib/ownership/keys/dummy:unlock_key)
--next-activate-key=$(location //sw/device/silicon_creator/lib/ownership/keys/dummy:activate_key)
--next-application-key=$(location //sw/device/silicon_creator/lib/ownership/keys/dummy:app_prod_pub)
--config-kind=with-flash-locked
--rescue-after-activate={flash_regions}
""",
test_harness = "//sw/host/tests/ownership:flash_permission_test",
),
deps = [
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
"//sw/device/lib/base:status",
"//sw/device/lib/dif:flash_ctrl",
"//sw/device/lib/testing/test_framework:ottf_main",
],
)
84 changes: 84 additions & 0 deletions sw/device/silicon_creator/rom_ext/e2e/ownership/flash_regions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#include "sw/device/lib/base/status.h"
#include "sw/device/lib/dif/dif_flash_ctrl.h"
#include "sw/device/lib/runtime/log.h"
#include "sw/device/lib/testing/test_framework/ottf_main.h"

#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"

OTTF_DEFINE_TEST_CONFIG();

dif_flash_ctrl_state_t flash_state;

const char *mubi_prop(multi_bit_bool_t val, const char *name) {
switch (val) {
case kMultiBitBool4True:
return name;
case kMultiBitBool4False:
return "xx";
default:
return "uu";
}
}

void flash_data_region_print(size_t index,
dif_flash_ctrl_data_region_properties_t *p,
bool locked) {
LOG_INFO("data region n=%u st=%u sz=%u %s-%s-%s-%s-%s-%s %s", index, p->base,
p->size, mubi_prop(p->properties.rd_en, "RD"),
mubi_prop(p->properties.prog_en, "WR"),
mubi_prop(p->properties.erase_en, "ER"),
mubi_prop(p->properties.scramble_en, "SC"),
mubi_prop(p->properties.ecc_en, "EC"),
mubi_prop(p->properties.high_endurance_en, "HE"),
locked ? "LK" : "UN");
}

void flash_info_region_print(dif_flash_ctrl_info_region_t region,
dif_flash_ctrl_region_properties_t *p,
bool locked) {
LOG_INFO("info region bank=%u part=%u page=%u %s-%s-%s-%s-%s-%s %s",
region.bank, region.partition_id, region.page,
mubi_prop(p->rd_en, "RD"), mubi_prop(p->prog_en, "WR"),
mubi_prop(p->erase_en, "ER"), mubi_prop(p->scramble_en, "SC"),
mubi_prop(p->ecc_en, "EC"), mubi_prop(p->high_endurance_en, "HE"),
locked ? "LK" : "UN");
}

status_t flash_regions_print(dif_flash_ctrl_state_t *f) {
for (uint32_t i = 0; i < 8; ++i) {
dif_flash_ctrl_data_region_properties_t p;
bool locked;
TRY(dif_flash_ctrl_get_data_region_properties(f, i, &p));
TRY(dif_flash_ctrl_data_region_is_locked(f, i, &locked));
flash_data_region_print(i, &p, locked);
}
for (uint32_t i = 0; i < 4; ++i) {
dif_flash_ctrl_info_region_t region = {
.bank = 0,
.partition_id = 0,
.page = 6 + i,
};
bool locked;
dif_flash_ctrl_region_properties_t p;
TRY(dif_flash_ctrl_get_info_region_properties(f, region, &p));
TRY(dif_flash_ctrl_info_region_is_locked(f, region, &locked));
flash_info_region_print(region, &p, locked);
}
return OK_STATUS();
}

bool test_main(void) {
CHECK_DIF_OK(dif_flash_ctrl_init_state(
&flash_state,
mmio_region_from_addr(TOP_EARLGREY_FLASH_CTRL_CORE_BASE_ADDR)));
status_t sts = flash_regions_print(&flash_state);

if (status_err(sts)) {
LOG_ERROR("flash_regions_print: %r", sts);
}
return status_ok(sts);
}
14 changes: 14 additions & 0 deletions sw/host/tests/ownership/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ rust_binary(
"@crate_index//:regex",
],
)

rust_binary(
name = "flash_permission_test",
srcs = ["flash_permission_test.rs"],
deps = [
":transfer_lib",
"//sw/host/opentitanlib",
"@crate_index//:anyhow",
"@crate_index//:clap",
"@crate_index//:humantime",
"@crate_index//:log",
"@crate_index//:regex",
],
)
Loading

0 comments on commit 52d6e0f

Please sign in to comment.