Skip to content

Commit 6c31838

Browse files
committed
Improve otp_keystore checks, add explicit TARGET_sim check
1 parent c02c273 commit 6c31838

File tree

3 files changed

+139
-14
lines changed

3 files changed

+139
-14
lines changed

include/otp_keystore.h

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@
3030
/* Specific includes for supported targets
3131
* (needed for OTP_SIZE)
3232
*/
33-
#ifdef TARGET_stm32h7
33+
#if defined(TARGET_stm32h7)
3434
#include "hal/stm32h7.h"
35-
#elif defined TARGET_stm32h5
35+
#elif defined(TARGET_stm32h5)
3636
#include "hal/stm32h5.h"
37+
#elif defined(TARGET_sim)
38+
#include "hal_host_sim_stub.h"
39+
#else
40+
#error "Define a target"
3741
#endif
3842

3943
#include "keystore.h"
@@ -47,25 +51,35 @@
4751
#define KEYSTORE_HDR_PACKED
4852
#endif
4953

50-
struct KEYSTORE_HDR_PACKED wolfBoot_otp_hdr {
51-
char keystore_hdr_magic[8];
52-
uint16_t item_count;
53-
uint16_t flags;
54-
uint32_t version;
55-
};
56-
57-
static const char KEYSTORE_HDR_MAGIC[8] = "WOLFBOOT";
54+
#if !defined(OTP_SIZE) || (OTP_SIZE <= 0)
55+
/* See TARGET_[device] */
56+
#error "WRONG OTP SIZE"
57+
#endif
5858

59-
#define KEYSTORE_MAX_PUBKEYS ((OTP_SIZE - OTP_HDR_SIZE) / SIZEOF_KEYSTORE_SLOT)
59+
#ifndef SIZEOF_KEYSTORE_SLOT
60+
#error "SIZEOF_KEYSTORE_SLOT must be defined"
61+
#endif
6062

61-
#if (OTP_SIZE == 0)
62-
#error WRONG OTP SIZE
63+
#if (OTP_HDR_SIZE >= OTP_SIZE)
64+
#error "Bad OTP_HDR_SIZE or OTP_SIZE"
6365
#endif
6466

67+
#define KEYSTORE_MAX_PUBKEYS ((OTP_SIZE - OTP_HDR_SIZE) / SIZEOF_KEYSTORE_SLOT)
68+
6569
#if (KEYSTORE_MAX_PUBKEYS < 1)
6670
#error "No space for any keystores in OTP with current algorithm"
6771
#endif
6872

69-
#endif /* FLASH_OTP_KEYSTORE */
73+
struct KEYSTORE_HDR_PACKED wolfBoot_otp_hdr {
74+
char keystore_hdr_magic[8];
75+
uint16_t item_count;
76+
uint16_t flags;
77+
uint32_t version;
78+
};
79+
80+
/* KEYSTORE_HDR_MAGIC = "WOLFBOOT" exactly 8 bytes, no nul terminator */
81+
static const char KEYSTORE_HDR_MAGIC[8] = { 'W','O','L','F','B','O','O','T' };
82+
83+
#endif /* FLASH_OTP_KEYSTORE */
7084

7185
#endif /* OTP_KEYSTORE_H */
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* hal_host_sim_stub.c
2+
*
3+
* Helper for storing/retrieving Trust Anchor to/from OTP flash
4+
*
5+
*
6+
* Copyright (C) 2025 wolfSSL Inc.
7+
*
8+
* This file is part of wolfBoot.
9+
*
10+
* wolfBoot is free software; you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation; either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* wolfBoot is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program; if not, write to the Free Software
22+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
23+
*/
24+
25+
/* hal_host_stub.c - host-only placeholders for HAL used by otp-keystore-primer */
26+
#include <stdint.h>
27+
#include <stdio.h>
28+
#include <string.h>
29+
//
30+
//#ifndef HAL_H
31+
//#define HAL_H
32+
///* For host builds, redirect hal.h to our sim stub */
33+
//#include "hal_host_sim_stub.h"
34+
//#endif
35+
36+
/* Minimal mirror of what primer expects. If these normally come from hal.h/target.h,
37+
define the bare minimum here so the host build can link. */
38+
#ifndef FLASH_OTP_BASE
39+
#define FLASH_OTP_BASE 0u
40+
#endif
41+
42+
void hal_init(void)
43+
{
44+
/* No hardware on host. */
45+
fprintf(stderr, "[hal_host_stub] hal_init() called\n");
46+
}
47+
48+
/* Return 0 on success like many wolfBoot HAL funcs. Adjust signature to match your hal.h. */
49+
int hal_flash_otp_write(uint32_t flashAddress, const void* data, uint16_t length)
50+
{
51+
(void)flashAddress;
52+
(void)data;
53+
(void)length;
54+
fprintf(stderr, "[hal_host_stub] hal_flash_otp_write(addr=%lu, len=%lu)\n",
55+
(unsigned long)flashAddress, length);
56+
return 0;
57+
}
58+
59+
int hal_flash_otp_set_readonly(uint32_t flashAddress, uint16_t length)
60+
{
61+
(void)flashAddress;
62+
(void)length;
63+
fprintf(stderr, "[hal_host_stub] hal_flash_otp_set_readonly(addr=%lu, len=%lu)\n",
64+
(unsigned long)flashAddress, length);
65+
return 0;
66+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* hal_host_sim_stub.f
2+
*
3+
* Helper for storing/retrieving Trust Anchor to/from OTP flash
4+
*
5+
*
6+
* Copyright (C) 2025 wolfSSL Inc.
7+
*
8+
* This file is part of wolfBoot.
9+
*
10+
* wolfBoot is free software; you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation; either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* wolfBoot is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program; if not, write to the Free Software
22+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
23+
*/
24+
25+
#ifndef HAL_HOST_SIM_STUB_H
26+
#define HAL_HOST_SIM_STUB_H
27+
28+
#include <stdint.h>
29+
30+
#ifndef FLASH_OTP_BASE
31+
#define FLASH_OTP_BASE 0u
32+
#endif
33+
34+
#ifndef OTP_SIZE
35+
/* Define a generic max OTP size to appease otp_keystore.h */
36+
#define OTP_SIZE 4096
37+
#endif
38+
39+
/* See actual implementation in [WOLFBOOT_ROOT]/hal; Optionally define your own sim stubs: */
40+
41+
void hal_init(void);
42+
int hal_flash_otp_write(uint32_t flashAddress, const void* data, uint16_t length);
43+
int hal_flash_otp_set_readonly(uint32_t flashAddress, uint16_t length);
44+
45+
#endif /* HAL_HOST_SIM_STUB_H */

0 commit comments

Comments
 (0)