Skip to content

Commit d3aa4e9

Browse files
gautschimiMichael Gautschi
authored andcommitted
[flash_ctrl,dv] byte/word scrambling addr fix
FlashAddrWidth is in flash words (not bytes). Passing a byte address to a function which expects word-addresses leads to a truncation of the three most significant address bits. As a result, when accessing the upper part of each flash bank the scrambling tweak was wrong which lead to a wrong data encryption and hence also to a wrong ICV calculation for the expected responses. Signed-off-by: Michael Gautschi <[email protected]>
1 parent 9581b9b commit d3aa4e9

File tree

13 files changed

+42
-32
lines changed

13 files changed

+42
-32
lines changed

hw/ip_templates/flash_ctrl/dv/env/flash_ctrl_bkdr_util.sv

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ class flash_ctrl_bkdr_util extends mem_bkdr_util;
9595
// TODO; consider changing these functions to member functions and retaining the flash_addr_
9696
// and flash_data_ keys as members.
9797
static function bit [FlashDataWidth-1:0] flash_create_masked_data(
98-
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] byte_addr,
98+
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] word_addr,
9999
bit [FlashKeySize-1:0] flash_addr_key, bit [FlashKeySize-1:0] flash_data_key);
100100
bit [FlashNumRoundsHalf-1:0][FlashDataWidth-1:0] scrambled_data;
101101
bit [FlashDataWidth-1:0] masked_data;
102102
bit [FlashDataWidth-1:0] mask;
103103

104-
mask = flash_galois_multiply(flash_addr_key, byte_addr >> FlashDataByteWidth);
104+
mask = flash_galois_multiply(flash_addr_key, word_addr);
105105
masked_data = data ^ mask;
106106

107107
crypto_dpi_prince_pkg::sv_dpi_prince_encrypt(.plaintext(masked_data), .key(flash_data_key),
@@ -110,17 +110,19 @@ class flash_ctrl_bkdr_util extends mem_bkdr_util;
110110
endfunction
111111

112112
virtual function void flash_write_scrambled(
113-
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] byte_addr,
113+
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] word_addr,
114114
bit [FlashKeySize-1:0] flash_addr_key, bit [FlashKeySize-1:0] flash_data_key);
115115
bit [FlashDataWidth-1:0] masked_data;
116116
bit [71:0] ecc_72;
117117
bit [75:0] ecc_76;
118+
bit [flash_ctrl_top_specific_pkg::BusAddrByteW-1:0] byte_addr;
118119

119-
masked_data = flash_create_masked_data(data, byte_addr, flash_addr_key, flash_data_key);
120+
masked_data = flash_create_masked_data(data, word_addr, flash_addr_key, flash_data_key);
120121

121122
// ecc functions used are hardcoded to a fixed sized.
122123
ecc_72 = prim_secded_pkg::prim_secded_hamming_72_64_enc(data[63:0]);
123124
ecc_76 = prim_secded_pkg::prim_secded_hamming_76_68_enc({ecc_72[67:64], masked_data[63:0]});
125+
byte_addr = word_addr << FlashDataByteWidth;
124126
write(byte_addr, ecc_76);
125127
endfunction
126128
endclass

hw/ip_templates/flash_ctrl/dv/env/flash_ctrl_env_pkg.sv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,11 @@ package flash_ctrl_env_pkg;
361361
bit [FlashKeySize-1:0] flash_addr_key, bit [FlashKeySize-1:0] flash_data_key,
362362
bit dis = 1);
363363
bit [FlashDataWidth-1:0] masked_data;
364+
bit [FlashAddrWidth-1:0] word_addr = byte_addr >> FlashDataByteWidth;
364365

365-
masked_data = flash_ctrl_bkdr_util::flash_create_masked_data(data, byte_addr, flash_addr_key,
366+
masked_data = flash_ctrl_bkdr_util::flash_create_masked_data(data, word_addr, flash_addr_key,
366367
flash_data_key);
367368
if (dis) begin
368-
bit [FlashAddrWidth-1:0] word_addr = byte_addr >> FlashDataByteWidth;
369369
`uvm_info("SCR_DBG", $sformatf("addr:%x mask:%x din:%x dout:%x",
370370
word_addr,
371371
flash_ctrl_bkdr_util::flash_galois_multiply(flash_addr_key,

hw/ip_templates/flash_ctrl/dv/env/flash_otf_item.sv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class flash_otf_item extends uvm_object;
139139
data = raw_fq[i][FlashDataWidth-1:0];
140140
if (ecc_en) begin
141141
data_with_icv = prim_secded_pkg::prim_secded_hamming_72_64_enc(raw_fq[i][63:0]);
142+
`uvm_info("icv", $sformatf("ICV:%4b", data_with_icv[67:64]), UVM_DEBUG)
142143
if (add_icv_err) begin
143144
`uvm_info("icv_debug", $sformatf("before:%4b after:%4b",
144145
data_with_icv[67:64], ~data_with_icv[67:64]), UVM_DEBUG)

hw/ip_templates/flash_ctrl/dv/flash_ctrl_base_sim_cfg.hjson.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@
344344
name: flash_ctrl_rw_evict_all_en
345345
uvm_test_seq: flash_ctrl_rw_evict_vseq
346346
run_opts: ["+scb_otf_en=1", "+ecc_mode=1", "+en_always_read=1",
347-
"+en_always_prog=1", "en_rnd_data=0"]
347+
"+en_always_prog=1", "+en_rnd_data=0"]
348348
reseed: 40
349349
}
350350
{

hw/top_earlgrey/dv/env/seq_lib/chip_sw_flash_init_vseq.sv

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class chip_sw_flash_init_vseq extends chip_sw_base_vseq;
3535
localparam uint CREATOR_SECRET_PAGE_ID = flash_ctrl_top_specific_pkg::CreatorInfoPage;
3636
localparam uint OWNER_SECRET_PAGE_ID = flash_ctrl_top_specific_pkg::OwnerInfoPage;
3737
localparam uint ISO_PART_PAGE_ID = flash_ctrl_top_specific_pkg::IsolatedInfoPage;
38+
localparam uint FLASH_DATA_BYTE_WIDTH = flash_ctrl_top_specific_pkg::DataByteWidth;
3839

3940
localparam uint NUM_TEST_WORDS = 16;
4041
typedef enum {
@@ -136,32 +137,32 @@ class chip_sw_flash_init_vseq extends chip_sw_base_vseq;
136137
flash_ctrl_bkdr_util data0;
137138
flash_ctrl_bkdr_util data1;
138139
flash_ctrl_bkdr_util info0;
139-
bit [15:0] base_addr_bytes;
140+
bit [15:0] base_addr_words;
140141

141142
`downcast(data0, cfg.mem_bkdr_util_h[FlashBank0Data])
142143
`downcast(data1, cfg.mem_bkdr_util_h[FlashBank1Data])
143144
`downcast(info0, cfg.mem_bkdr_util_h[FlashBank0Info])
144145

145146
for (int i = 0; i < NUM_TEST_WORDS / 2; i++) begin
146-
base_addr_bytes = 16'h0;
147+
base_addr_words = 16'h0;
147148
data0.flash_write_scrambled(
148-
{bank0_page0_data[(i*2)+1], bank0_page0_data[i*2]}, base_addr_bytes + (i * 8),
149+
{bank0_page0_data[(i*2)+1], bank0_page0_data[i*2]}, base_addr_words + i,
149150
flash_addr_key, flash_data_key);
150-
base_addr_bytes = FLASH_PAGE_SIZE_BYTES * FLASH_PAGES_PER_BANK;
151+
base_addr_words = (FLASH_PAGE_SIZE_BYTES >> FLASH_DATA_BYTE_WIDTH) * FLASH_PAGES_PER_BANK;
151152
data1.flash_write_scrambled(
152-
{bank1_page0_data[(i*2)+1], bank1_page0_data[i*2]}, base_addr_bytes + (i * 8),
153+
{bank1_page0_data[(i*2)+1], bank1_page0_data[i*2]}, base_addr_words + i,
153154
flash_addr_key, flash_data_key);
154-
base_addr_bytes = FLASH_PAGE_SIZE_BYTES * CREATOR_SECRET_PAGE_ID;
155+
base_addr_words = (FLASH_PAGE_SIZE_BYTES >> FLASH_DATA_BYTE_WIDTH) * CREATOR_SECRET_PAGE_ID;
155156
info0.flash_write_scrambled(
156-
{creator_secret_data[(i*2)+1], creator_secret_data[i*2]}, base_addr_bytes + (i * 8),
157+
{creator_secret_data[(i*2)+1], creator_secret_data[i*2]}, base_addr_words + i,
157158
flash_addr_key, flash_data_key);
158-
base_addr_bytes = FLASH_PAGE_SIZE_BYTES * OWNER_SECRET_PAGE_ID;
159+
base_addr_words = (FLASH_PAGE_SIZE_BYTES >> FLASH_DATA_BYTE_WIDTH) * OWNER_SECRET_PAGE_ID;
159160
info0.flash_write_scrambled(
160-
{owner_secret_data[(i*2)+1], owner_secret_data[i*2]}, base_addr_bytes + (i * 8),
161+
{owner_secret_data[(i*2)+1], owner_secret_data[i*2]}, base_addr_words + i,
161162
flash_addr_key, flash_data_key);
162-
base_addr_bytes = FLASH_PAGE_SIZE_BYTES * ISO_PART_PAGE_ID;
163+
base_addr_words = (FLASH_PAGE_SIZE_BYTES >> FLASH_DATA_BYTE_WIDTH) * ISO_PART_PAGE_ID;
163164
info0.flash_write_scrambled(
164-
{iso_part_data[(i*2)+1], iso_part_data[i*2]}, base_addr_bytes + (i * 8), flash_addr_key,
165+
{iso_part_data[(i*2)+1], iso_part_data[i*2]}, base_addr_words + i, flash_addr_key,
165166
flash_data_key);
166167
end
167168
endtask

hw/top_earlgrey/ip_autogen/flash_ctrl/dv/env/flash_ctrl_bkdr_util.sv

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ class flash_ctrl_bkdr_util extends mem_bkdr_util;
9595
// TODO; consider changing these functions to member functions and retaining the flash_addr_
9696
// and flash_data_ keys as members.
9797
static function bit [FlashDataWidth-1:0] flash_create_masked_data(
98-
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] byte_addr,
98+
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] word_addr,
9999
bit [FlashKeySize-1:0] flash_addr_key, bit [FlashKeySize-1:0] flash_data_key);
100100
bit [FlashNumRoundsHalf-1:0][FlashDataWidth-1:0] scrambled_data;
101101
bit [FlashDataWidth-1:0] masked_data;
102102
bit [FlashDataWidth-1:0] mask;
103103

104-
mask = flash_galois_multiply(flash_addr_key, byte_addr >> FlashDataByteWidth);
104+
mask = flash_galois_multiply(flash_addr_key, word_addr);
105105
masked_data = data ^ mask;
106106

107107
crypto_dpi_prince_pkg::sv_dpi_prince_encrypt(.plaintext(masked_data), .key(flash_data_key),
@@ -110,17 +110,19 @@ class flash_ctrl_bkdr_util extends mem_bkdr_util;
110110
endfunction
111111

112112
virtual function void flash_write_scrambled(
113-
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] byte_addr,
113+
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] word_addr,
114114
bit [FlashKeySize-1:0] flash_addr_key, bit [FlashKeySize-1:0] flash_data_key);
115115
bit [FlashDataWidth-1:0] masked_data;
116116
bit [71:0] ecc_72;
117117
bit [75:0] ecc_76;
118+
bit [flash_ctrl_top_specific_pkg::BusAddrByteW-1:0] byte_addr;
118119

119-
masked_data = flash_create_masked_data(data, byte_addr, flash_addr_key, flash_data_key);
120+
masked_data = flash_create_masked_data(data, word_addr, flash_addr_key, flash_data_key);
120121

121122
// ecc functions used are hardcoded to a fixed sized.
122123
ecc_72 = prim_secded_pkg::prim_secded_hamming_72_64_enc(data[63:0]);
123124
ecc_76 = prim_secded_pkg::prim_secded_hamming_76_68_enc({ecc_72[67:64], masked_data[63:0]});
125+
byte_addr = word_addr << FlashDataByteWidth;
124126
write(byte_addr, ecc_76);
125127
endfunction
126128
endclass

hw/top_earlgrey/ip_autogen/flash_ctrl/dv/env/flash_ctrl_env_pkg.sv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,11 @@ package flash_ctrl_env_pkg;
361361
bit [FlashKeySize-1:0] flash_addr_key, bit [FlashKeySize-1:0] flash_data_key,
362362
bit dis = 1);
363363
bit [FlashDataWidth-1:0] masked_data;
364+
bit [FlashAddrWidth-1:0] word_addr = byte_addr >> FlashDataByteWidth;
364365

365-
masked_data = flash_ctrl_bkdr_util::flash_create_masked_data(data, byte_addr, flash_addr_key,
366+
masked_data = flash_ctrl_bkdr_util::flash_create_masked_data(data, word_addr, flash_addr_key,
366367
flash_data_key);
367368
if (dis) begin
368-
bit [FlashAddrWidth-1:0] word_addr = byte_addr >> FlashDataByteWidth;
369369
`uvm_info("SCR_DBG", $sformatf("addr:%x mask:%x din:%x dout:%x",
370370
word_addr,
371371
flash_ctrl_bkdr_util::flash_galois_multiply(flash_addr_key,

hw/top_earlgrey/ip_autogen/flash_ctrl/dv/env/flash_otf_item.sv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class flash_otf_item extends uvm_object;
139139
data = raw_fq[i][FlashDataWidth-1:0];
140140
if (ecc_en) begin
141141
data_with_icv = prim_secded_pkg::prim_secded_hamming_72_64_enc(raw_fq[i][63:0]);
142+
`uvm_info("icv", $sformatf("ICV:%4b", data_with_icv[67:64]), UVM_DEBUG)
142143
if (add_icv_err) begin
143144
`uvm_info("icv_debug", $sformatf("before:%4b after:%4b",
144145
data_with_icv[67:64], ~data_with_icv[67:64]), UVM_DEBUG)

hw/top_earlgrey/ip_autogen/flash_ctrl/dv/flash_ctrl_base_sim_cfg.hjson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@
344344
name: flash_ctrl_rw_evict_all_en
345345
uvm_test_seq: flash_ctrl_rw_evict_vseq
346346
run_opts: ["+scb_otf_en=1", "+ecc_mode=1", "+en_always_read=1",
347-
"+en_always_prog=1", "en_rnd_data=0"]
347+
"+en_always_prog=1", "+en_rnd_data=0"]
348348
reseed: 40
349349
}
350350
{

hw/top_englishbreakfast/ip_autogen/flash_ctrl/dv/env/flash_ctrl_bkdr_util.sv

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ class flash_ctrl_bkdr_util extends mem_bkdr_util;
9595
// TODO; consider changing these functions to member functions and retaining the flash_addr_
9696
// and flash_data_ keys as members.
9797
static function bit [FlashDataWidth-1:0] flash_create_masked_data(
98-
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] byte_addr,
98+
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] word_addr,
9999
bit [FlashKeySize-1:0] flash_addr_key, bit [FlashKeySize-1:0] flash_data_key);
100100
bit [FlashNumRoundsHalf-1:0][FlashDataWidth-1:0] scrambled_data;
101101
bit [FlashDataWidth-1:0] masked_data;
102102
bit [FlashDataWidth-1:0] mask;
103103

104-
mask = flash_galois_multiply(flash_addr_key, byte_addr >> FlashDataByteWidth);
104+
mask = flash_galois_multiply(flash_addr_key, word_addr);
105105
masked_data = data ^ mask;
106106

107107
crypto_dpi_prince_pkg::sv_dpi_prince_encrypt(.plaintext(masked_data), .key(flash_data_key),
@@ -110,17 +110,19 @@ class flash_ctrl_bkdr_util extends mem_bkdr_util;
110110
endfunction
111111

112112
virtual function void flash_write_scrambled(
113-
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] byte_addr,
113+
bit [FlashDataWidth-1:0] data, bit [FlashAddrWidth-1:0] word_addr,
114114
bit [FlashKeySize-1:0] flash_addr_key, bit [FlashKeySize-1:0] flash_data_key);
115115
bit [FlashDataWidth-1:0] masked_data;
116116
bit [71:0] ecc_72;
117117
bit [75:0] ecc_76;
118+
bit [flash_ctrl_top_specific_pkg::BusAddrByteW-1:0] byte_addr;
118119

119-
masked_data = flash_create_masked_data(data, byte_addr, flash_addr_key, flash_data_key);
120+
masked_data = flash_create_masked_data(data, word_addr, flash_addr_key, flash_data_key);
120121

121122
// ecc functions used are hardcoded to a fixed sized.
122123
ecc_72 = prim_secded_pkg::prim_secded_hamming_72_64_enc(data[63:0]);
123124
ecc_76 = prim_secded_pkg::prim_secded_hamming_76_68_enc({ecc_72[67:64], masked_data[63:0]});
125+
byte_addr = word_addr << FlashDataByteWidth;
124126
write(byte_addr, ecc_76);
125127
endfunction
126128
endclass

0 commit comments

Comments
 (0)