Skip to content

Commit b1f6bec

Browse files
committed
Fix issues with H5 SPI master driver. Fixes for TPM NCS's. Cleanup dead code.
1 parent 39e99f0 commit b1f6bec

File tree

10 files changed

+93
-42
lines changed

10 files changed

+93
-42
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,8 @@ cppcheck:
531531
cppcheck -f --enable=warning --enable=portability \
532532
--suppress="ctunullpointer" --suppress="nullPointer" \
533533
--suppress="objectIndex" --suppress="comparePointers" \
534+
--suppress="subtractPointers" --suppress="intToPointerCast" \
535+
--check-level=exhaustive \
534536
--error-exitcode=89 --std=c89 src/*.c hal/*.c hal/spi/*.c hal/uart/*.c
535537

536538
otp: tools/keytools/otp/otp-keystore-primer.bin FORCE

hal/spi/spi_drv_stm32.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void RAMFUNCTION stm_gpio_config(uint32_t base, uint32_t pin, uint32_t mode,
116116
GPIO_OSPD(base) |= (speed << (pin * 2));
117117

118118
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
119-
119+
/* TODO: Consider setting GPIO_SECCFGR(base) */
120120
#endif
121121
}
122122

@@ -375,7 +375,7 @@ int qspi_transfer(uint8_t fmode, const uint8_t cmd,
375375
#if defined(SPI_FLASH) || defined(WOLFBOOT_TPM)
376376
uint8_t RAMFUNCTION spi_read(void)
377377
{
378-
while ((SPI1_SR & SPI_SR_RX_NOTEMPTY) == 0);
378+
while (!(SPI1_SR & SPI_SR_RX_NOTEMPTY));
379379
#ifdef SPI1_RXDR
380380
return SPI1_RXDR;
381381
#else
@@ -385,12 +385,12 @@ uint8_t RAMFUNCTION spi_read(void)
385385

386386
void RAMFUNCTION spi_write(const char byte)
387387
{
388-
int i;
389-
while ((SPI1_SR & SPI_SR_TX_EMPTY) == 0);
388+
while (!(SPI1_SR & SPI_SR_TX_EMPTY));
389+
390390
#ifdef SPI1_TXDR
391-
SPI1_TXDR = byte;
391+
SPI1_TXDR = (uint8_t)byte;
392392
#else
393-
SPI1_DR = byte;
393+
SPI1_DR = (uint8_t)byte;
394394
#endif
395395
}
396396
#endif /* SPI_FLASH || WOLFBOOT_TPM */
@@ -496,17 +496,20 @@ void RAMFUNCTION spi_init(int polarity, int phase)
496496
/* Configure SPI1 for master mode */
497497
SPI1_CR1 &= ~SPI_CR1_SPI_EN;
498498
#if defined(TARGET_stm32h5)
499+
/* Clear any faults in the status register */
500+
SPI1_IFCR = (SPI_IFCR_SUSPC | SPI_IFCR_MODFC | SPI_IFCR_TIFREC |
501+
SPI_IFCR_OVRC | SPI_IFCR_UDRC);
502+
499503
/* baud rate 2 (hclk/8), data size (8-bits), CRC Size (8-bits),
500504
* FIFO threshold level (1-data) */
501505
SPI1_CFG1 = (
502506
((2 & SPI_CFG1_BAUDRATE_MASK) << SPI_CFG1_BAUDRATE_SHIFT) |
503507
((7 & SPI_CFG1_CRCSIZE_MASK) << SPI_CFG1_CRCSIZE_SHIFT) |
504508
((0 & SPI_CFG1_FTHLV_MASK) << SPI_CFG1_FTHLV_SHIFT) |
505509
((7 & SPI_CFG1_DSIZE_MASK) << SPI_CFG1_DSIZE_SHIFT));
506-
SPI1_CFG2 = SPI_CRF2_MASTER | SPI_CFG2_SSOE |
510+
SPI1_CFG2 = SPI_CFG2_MASTER | SPI_CFG2_SSOE |
507511
(polarity << SPI_CFG2_CLOCK_POL_SHIFT) |
508512
(phase << SPI_CFG2_CLOCK_PHASE_SHIFT);
509-
SPI1_CR1 |= SPI_CR1_CSTART; /* use continuous start mode */
510513
#else
511514
#ifndef TARGET_stm32l0 /* use existing/default baud for L0 */
512515
/* Baud rate 5 (hclk/6), data size 8 bits */
@@ -520,6 +523,10 @@ void RAMFUNCTION spi_init(int polarity, int phase)
520523
#endif
521524

522525
SPI1_CR1 |= SPI_CR1_SPI_EN; /* Enable SPI */
526+
527+
#ifdef SPI_CR1_CSTART
528+
SPI1_CR1 |= SPI_CR1_CSTART; /* use continuous start mode */
529+
#endif
523530
#endif /* SPI_FLASH || WOLFBOOOT_TPM */
524531
}
525532
}

hal/spi/spi_drv_stm32.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,14 @@
433433
#define SPI1_CFG1 (*(volatile uint32_t *)(SPI1_BASE + 0x08))
434434
#define SPI1_CFG2 (*(volatile uint32_t *)(SPI1_BASE + 0x0C))
435435
#define SPI1_SR (*(volatile uint32_t *)(SPI1_BASE + 0x14))
436+
#define SPI1_IFCR (*(volatile uint32_t *)(SPI1_BASE + 0x18))
436437
#define SPI1_TXDR (*(volatile uint8_t *)(SPI1_BASE + 0x20))
437438
#define SPI1_RXDR (*(volatile uint8_t *)(SPI1_BASE + 0x30))
438439

439440
#define SPI_CR1_SPI_EN (1 << 0)
440-
#define SPI_CR1_CSTART (1 << 9) /* Continous start */
441-
441+
#define SPI_CR1_MASRX (1 << 8) /* master automatic suspension in Receive mode */
442+
#define SPI_CR1_CSTART (1 << 9) /* Continous start */
443+
#define SPI_CR1_SSI (1 << 12) /* Internal slave select signal input level */
442444
#define SPI_CFG1_DSIZE_MASK (0x1F)
443445
#define SPI_CFG1_DSIZE_SHIFT (0)
444446
#define SPI_CFG1_FTHLV_MASK (0x1F)
@@ -448,18 +450,28 @@
448450
#define SPI_CFG1_BAUDRATE_MASK (0x07)
449451
#define SPI_CFG1_BAUDRATE_SHIFT (28)
450452

451-
#define SPI_CRF2_MASTER (1 << 22)
453+
#define SPI_CFG2_MASTER (1 << 22)
452454
#define SPI_CFG2_LSBFIRST (1 << 23)
453455
#define SPI_CFG2_CLOCK_PHASE_SHIFT (24)
454456
#define SPI_CFG2_CLOCK_POL_SHIFT (25)
455457
#define SPI_CFG2_SSM (1 << 26)
456458
#define SPI_CFG2_SSOE (1 << 29)
457-
#define SPI_CFG2_HW_CRC_EN (1 << 29)
458-
#define SPI_CFG2_COMM_MASK (0x3) /* 0=full duplex, 1=simplex tx, 2=simplex rx, 3=half duplex */
459+
#define SPI_CFG2_SSOM (1 << 30)
460+
#define SPI_CFG2_AFCNTR (1 << 31) /* alternate function GPIOs control */
461+
#define SPI_CFG2_COMM_MASK (0x3) /* 0=full duplex, 1=simplex tx, 2=simplex rx, 3=half duplex */
459462
#define SPI_CFG2_COMM_SHIFT (17)
460463

461-
#define SPI_SR_RX_NOTEMPTY (1UL << 0)
462-
#define SPI_SR_TX_EMPTY (1UL << 1)
464+
#define SPI_IFCR_SUSPC (1 << 11)
465+
#define SPI_IFCR_MODFC (1 << 9)
466+
#define SPI_IFCR_TIFREC (1 << 8)
467+
#define SPI_IFCR_CRCEC (1 << 7)
468+
#define SPI_IFCR_OVRC (1 << 6)
469+
#define SPI_IFCR_UDRC (1 << 5)
470+
#define SPI_IFCR_TXTFC (1 << 4)
471+
#define SPI_IFCR_EOTC (1 << 3)
472+
473+
#define SPI_SR_RX_NOTEMPTY (1 << 0)
474+
#define SPI_SR_TX_EMPTY (1 << 1)
463475

464476
#else
465477

include/tpm.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ int wolfBoot_tpm2_clear(void);
5757
/* API's that are callable from non-secure code */
5858
int CSME_NSE_API wolfBoot_tpm2_caps(WOLFTPM2_CAPS* caps);
5959
int CSME_NSE_API wolfBoot_tpm2_get_handles(TPM_HANDLE handle, TPML_HANDLE* handles);
60-
const char* CSME_NSE_API wolfBoot_tpm2_get_alg_name(TPM_ALG_ID alg);
61-
const char* CSME_NSE_API wolfBoot_tpm2_get_rc_string(int rc);
60+
const char* CSME_NSE_API wolfBoot_tpm2_get_alg_name(TPM_ALG_ID alg,
61+
char* name, int name_sz);
62+
const char* CSME_NSE_API wolfBoot_tpm2_get_rc_string(int rc,
63+
char* error, int error_sz);
6264
TPM_RC CSME_NSE_API wolfBoot_tpm2_get_capability(GetCapability_In* in, GetCapability_Out* out);
6365

6466
#if defined(WOLFBOOT_TPM_VERIFY) || defined(WOLFBOOT_TPM_SEAL)

include/wolfboot/wolfboot.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ extern "C" {
3838

3939

4040
#ifndef RAMFUNCTION
41-
#if defined(__WOLFBOOT) && defined(RAM_CODE)
42-
# if defined(ARCH_ARM)
43-
# define RAMFUNCTION __attribute__((used,section(".ramcode"),long_call))
41+
# if defined(__WOLFBOOT) && defined(RAM_CODE)
42+
# if defined(ARCH_ARM)
43+
# define RAMFUNCTION __attribute__((used,section(".ramcode"),long_call))
44+
# else
45+
# define RAMFUNCTION __attribute__((used,section(".ramcode")))
46+
# endif
4447
# else
45-
# define RAMFUNCTION __attribute__((used,section(".ramcode")))
46-
# endif
47-
#else
48-
# define RAMFUNCTION
48+
# define RAMFUNCTION
4949
#endif
5050
#endif
5151

@@ -84,7 +84,7 @@ extern "C" {
8484

8585
#ifndef XALIGNED_STACK
8686
/* Don't enforce stack alignment on IAR */
87-
#if defined (__IAR_SYSTEMS_ICC__)
87+
#if defined(__IAR_SYSTEMS_ICC__)
8888
#define XALIGNED_STACK(x)
8989
#else
9090
#define XALIGNED_STACK(x) XALIGNED(x)

src/image.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "loader.h"
3838
#include "image.h"
39+
#include "wolfboot/wolfboot.h"
3940
#include "hal.h"
4041
#include "spi_drv.h"
4142
#include "printf.h"

src/tpm.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,24 +1137,49 @@ static int wolfRNG_GetSeedCB(OS_Seed* os, uint8_t* seed, uint32_t sz)
11371137
/* API's that are callable from non-secure code */
11381138
int CSME_NSE_API wolfBoot_tpm2_caps(WOLFTPM2_CAPS* caps)
11391139
{
1140-
memset(&caps, 0, sizeof(caps));
1140+
memset(caps, 0, sizeof(*caps));
11411141
return wolfTPM2_GetCapabilities(&wolftpm_dev, caps);
11421142
}
11431143

11441144
int CSME_NSE_API wolfBoot_tpm2_get_handles(TPM_HANDLE handle, TPML_HANDLE* handles)
11451145
{
1146-
memset(&handles, 0, sizeof(handles));
1146+
memset(handles, 0, sizeof(*handles));
11471147
return wolfTPM2_GetHandles(handle, handles);
11481148
}
11491149

1150-
const char* CSME_NSE_API wolfBoot_tpm2_get_alg_name(TPM_ALG_ID alg)
1150+
const char* CSME_NSE_API wolfBoot_tpm2_get_alg_name(TPM_ALG_ID alg,
1151+
char* name, int name_sz)
11511152
{
1152-
return TPM2_GetAlgName(alg);
1153+
const char* s_name;
1154+
if (name == NULL || name_sz <= 0) {
1155+
return NULL;
1156+
}
1157+
s_name = TPM2_GetAlgName(alg);
1158+
if (s_name != NULL && name != NULL && name_sz > 0) {
1159+
strncpy(name, s_name, name_sz - 1);
1160+
name[name_sz - 1] = '\0';
1161+
}
1162+
else {
1163+
strcpy(name, "Unknown");
1164+
}
1165+
return (const char*)name;
11531166
}
11541167

1155-
const char* CSME_NSE_API wolfBoot_tpm2_get_rc_string(int rc)
1168+
const char* CSME_NSE_API wolfBoot_tpm2_get_rc_string(int rc, char* error, int error_sz)
11561169
{
1157-
return TPM2_GetRCString(rc);
1170+
const char* s_error;
1171+
if (error == NULL || error_sz <= 0) {
1172+
return NULL;
1173+
}
1174+
s_error = TPM2_GetRCString(rc);
1175+
if (s_error != NULL && error != NULL && error_sz > 0) {
1176+
strncpy(error, s_error, error_sz - 1);
1177+
error[error_sz - 1] = '\0';
1178+
}
1179+
else {
1180+
strcpy(error, "Unknown");
1181+
}
1182+
return (const char*)error;
11581183
}
11591184

11601185
TPM_RC CSME_NSE_API wolfBoot_tpm2_get_capability(GetCapability_In* in, GetCapability_Out* out)

src/update_flash.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "image.h"
2828
#include "hal.h"
2929
#include "spi_flash.h"
30-
#include "wolfboot/wolfboot.h"
3130
#include "target.h"
3231

3332
#include "delta.h"

test-app/app_stm32h5.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ static int TPM2_PCRs_Print(void)
677677
GetCapability_In capIn;
678678
GetCapability_Out capOut;
679679
TPML_PCR_SELECTION* pcrSel;
680+
char buffer[24];
680681

681682
/* List available PCR's */
682683
XMEMSET(&capIn, 0, sizeof(capIn));
@@ -686,9 +687,11 @@ static int TPM2_PCRs_Print(void)
686687
rc = wolfBoot_tpm2_get_capability(&capIn, &capOut);
687688
if (rc == TPM_RC_SUCCESS) {
688689
pcrSel = &capOut.capabilityData.data.assignedPCR;
689-
printf("Assigned PCR's:\n");
690+
printf("Assigned PCR's:\r\n");
690691
for (pcrCount=0; pcrCount < (int)pcrSel->count; pcrCount++) {
691-
printf("\t%s: ", wolfBoot_tpm2_get_alg_name(pcrSel->pcrSelections[pcrCount].hash));
692+
693+
printf("\t%s: ", wolfBoot_tpm2_get_alg_name(
694+
pcrSel->pcrSelections[pcrCount].hash, buffer, sizeof(buffer)));
692695
for (pcrIndex=0;
693696
pcrIndex<pcrSel->pcrSelections[pcrCount].sizeofSelect*8;
694697
pcrIndex++) {
@@ -697,7 +700,7 @@ static int TPM2_PCRs_Print(void)
697700
printf(" %d", pcrIndex);
698701
}
699702
}
700-
printf("\n");
703+
printf("\r\n");
701704
}
702705
}
703706
return rc;
@@ -708,13 +711,14 @@ static int cmd_tpm_info(const char *args)
708711
int rc;
709712
WOLFTPM2_CAPS caps;
710713
TPML_HANDLE handles;
714+
char error[100];
711715

712-
printf("Get TPM 2.0 module information\n");
716+
printf("Get TPM 2.0 module information\r\n");
713717

714718
rc = wolfBoot_tpm2_caps(&caps);
715719
if (rc == 0) {
716720
printf("Mfg %s (%d), Vendor %s, Fw %u.%u (0x%x), "
717-
"FIPS 140-2 %d, CC-EAL4 %d\n",
721+
"FIPS 140-2 %d, CC-EAL4 %d\r\n",
718722
caps.mfgStr, caps.mfg, caps.vendorStr, caps.fwVerMajor,
719723
caps.fwVerMinor, caps.fwVerVendor, caps.fips140_2, caps.cc_eal4);
720724
}
@@ -723,16 +727,17 @@ static int cmd_tpm_info(const char *args)
723727
rc = wolfBoot_tpm2_get_handles(PERSISTENT_FIRST, &handles);
724728
if (rc >= 0) {
725729
int i;
726-
printf("Found %d persistent handles\n", rc);
730+
printf("Found %d persistent handles\r\n", rc);
727731
for (i=0; i<(int)handles.count; i++) {
728-
printf("\tHandle 0x%x\n", (unsigned int)handles.handle[i]);
732+
printf("\tHandle 0x%x\r\n", (unsigned int)handles.handle[i]);
729733
}
730734
}
731735

732736
/* Print the available PCR's */
733737
rc = TPM2_PCRs_Print();
734738
if (rc != 0) {
735-
printf("TPM error 0x%x: %s\n", rc, wolfBoot_tpm2_get_rc_string(rc));
739+
printf("TPM error 0x%x: %s\r\n",
740+
rc, wolfBoot_tpm2_get_rc_string(rc, error, sizeof(error)));
736741
}
737742

738743
return rc;

test-app/app_stm32l5.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ void extra_led_off(void)
134134
GPIOB_BSRR |= (1 << (LED_EXTRA_PIN + 16));
135135
}
136136

137-
static char CaBuf[2048];
138-
static uint8_t my_pubkey[200];
139137

140138
extern int ecdsa_sign_verify(int devId);
141139

0 commit comments

Comments
 (0)