Skip to content

Commit 33a4e9e

Browse files
committed
mock spi_mem in unit tests and in the simulator
1 parent 6062eae commit 33a4e9e

File tree

8 files changed

+90
-9
lines changed

8 files changed

+90
-9
lines changed

src/memory/spi_mem.c

+3-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
// limitations under the License.
1414

1515
#include "spi_mem.h"
16+
#include "bitbox02_pins.h"
1617
#include "util.h"
18+
#include <hal_delay.h>
19+
#include <spi_lite.h>
1720
#include <stdbool.h>
1821
#include <stdint.h>
1922
#include <stdlib.h>
20-
#ifndef TESTING
21-
#include "bitbox02_pins.h"
22-
#include <hal_delay.h>
23-
#include <spi_lite.h>
24-
#endif
2523

2624
#define SECTOR_MASK 0xFFFFF000
2725
#define MEMORY_LIMIT (SPI_MEM_MEMORY_SIZE - 1)
@@ -35,16 +33,12 @@
3533

3634
static void _spi_mem_cs_low(void)
3735
{
38-
#ifndef TESTING
3936
gpio_set_pin_level(PIN_MEM_CS, 0);
40-
#endif
4137
}
4238

4339
static void _spi_mem_cs_high(void)
4440
{
45-
#ifndef TESTING
4641
gpio_set_pin_level(PIN_MEM_CS, 1);
47-
#endif
4842
}
4943

5044
static uint8_t _spi_mem_read_sr(void)

src/rust/bitbox02-sys/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ const ALLOWLIST_FNS: &[&str] = &[
105105
"memory_setup",
106106
"menu_create",
107107
"mock_memory_factoryreset",
108+
"spi_mem_full_erase",
108109
"printf",
109110
"progress_create",
110111
"progress_set",

src/rust/bitbox02-sys/wrapper.h

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <memory/bitbox02_smarteeprom.h>
1818
#include <memory/memory.h>
1919
#include <memory/smarteeprom.h>
20+
#include <memory/spi_mem.h>
2021
#include <random.h>
2122
#include <reset.h>
2223
#include <screen.h>

src/rust/bitbox02/src/testing.rs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub fn mock_memory() {
9494

9595
bitbox02_sys::smarteeprom_bb02_config();
9696
bitbox02_sys::bitbox02_smarteeprom_init();
97+
bitbox02_sys::spi_mem_full_erase();
9798
}
9899
}
99100

test/simulator/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(IGNORE_SOURCES
3030
"src/memory/nvmctrl.c"
3131
"src/memory/smarteeprom.c"
3232
"src/memory/mpu.c"
33+
"src/memory/spi_mem.c"
3334
)
3435

3536
# Exclude some files which depends on the hardware.
@@ -61,6 +62,7 @@ add_library(bitbox_objects-simulator
6162
${ETHEREUM-SOURCES}
6263
framework/mock_cipher.c
6364
framework/mock_memory.c
65+
framework/mock_spi_mem.c
6466
framework/mock_screen.c
6567
framework/mock_smarteeprom.c
6668
framework/mock_securechip.c
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2025 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <memory/spi_mem.h>
16+
#include <stdlib.h>
17+
#include <string.h>
18+
19+
__extension__ static uint8_t _memory[] = {[0 ... SPI_MEM_MEMORY_SIZE] = 0xFF};
20+
21+
void spi_mem_full_erase(void)
22+
{
23+
memset(_memory, 0xFF, sizeof(_memory));
24+
}
25+
26+
bool spi_mem_write(uint32_t address, const uint8_t* input, size_t size)
27+
{
28+
memcpy(&_memory[address], input, size);
29+
return true;
30+
}
31+
32+
uint8_t* spi_mem_read(uint32_t address, size_t size)
33+
{
34+
uint8_t* result = (uint8_t*)malloc(size);
35+
if (!result) {
36+
return NULL;
37+
}
38+
memcpy(result, &_memory[address], size);
39+
return result;
40+
}

test/unit-test/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ set(IGNORE_SOURCES
3030
"src/memory/nvmctrl.c"
3131
"src/memory/smarteeprom.c"
3232
"src/memory/mpu.c"
33+
"src/memory/spi_mem.c"
3334
)
3435

3536
# Exclude some files which depends on the hardware.
@@ -63,6 +64,7 @@ add_library(bitbox_objects
6364
framework/mock_screen.c
6465
framework/mock_screen_stack.c
6566
framework/mock_memory.c
67+
framework/mock_spi_mem.c
6668
framework/mock_qtouch.c
6769
framework/mock_gestures.c
6870
framework/mock_component.c
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2025 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <memory/spi_mem.h>
16+
#include <stdlib.h>
17+
#include <string.h>
18+
19+
__extension__ static uint8_t _memory[] = {[0 ... SPI_MEM_MEMORY_SIZE] = 0xFF};
20+
21+
void spi_mem_full_erase(void)
22+
{
23+
memset(_memory, 0xFF, sizeof(_memory));
24+
}
25+
26+
bool spi_mem_write(uint32_t address, const uint8_t* input, size_t size)
27+
{
28+
memcpy(&_memory[address], input, size);
29+
return true;
30+
}
31+
32+
uint8_t* spi_mem_read(uint32_t address, size_t size)
33+
{
34+
uint8_t* result = (uint8_t*)malloc(size);
35+
if (!result) {
36+
return NULL;
37+
}
38+
memcpy(result, &_memory[address], size);
39+
return result;
40+
}

0 commit comments

Comments
 (0)