Skip to content

Commit 277cbbe

Browse files
authored
Merge pull request #814 from danielinux/fenrir-fixes-2026-07-02
Fenrir fixes 2026 07 02
2 parents ca2c7f6 + d383fc1 commit 277cbbe

39 files changed

Lines changed: 2087 additions & 60 deletions

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ tools/unit-tests/unit-linux-loader-e820
200200
tools/unit-tests/unit-linux-loader-syssize
201201
tools/unit-tests/unit-mpusize
202202
tools/unit-tests/unit-otp-keystore
203+
tools/unit-tests/unit-otp-keystore-gen-zeroize
203204
tools/unit-tests/unit-tpm-api-names
204205
tools/unit-tests/unit-elf-bss-guard
205206
tools/unit-tests/unit-fit-fpga
@@ -210,6 +211,20 @@ tools/unit-tests/unit-flash-erase-u3
210211
tools/unit-tests/unit-flash-erase-wb
211212
tools/unit-tests/unit-fwtpm-nv-oob
212213
tools/unit-tests/unit-x86-paging-oob
214+
tools/unit-tests/unit-ahci-unlock-panic
215+
tools/unit-tests/unit-ata-security-passphrase-zeroize
216+
tools/unit-tests/unit-arm-tee-psa-ipc
217+
tools/unit-tests/unit-flash-write-mcxa
218+
tools/unit-tests/unit-flash-write-same51
219+
tools/unit-tests/unit-flash-write-samr21
220+
tools/unit-tests/unit-image-elf-scatter
221+
tools/unit-tests/unit-image-hybrid
222+
tools/unit-tests/unit-imx-rt-cache-align
223+
tools/unit-tests/unit-update-disk-oob
224+
tools/unit-tests/unit-update-ram-enc
225+
tools/unit-tests/unit-update-ram-enc-nopart
226+
tools/unit-tests/unit-va416x0-fram
227+
tools/unit-tests/unit-wolfhsm_flash_hal
213228

214229

215230

hal/imx_rt.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <target.h>
2727
#include "image.h"
2828
#include "printf.h"
29+
#include "imx_rt.h"
2930
#include "fsl_cache.h"
3031
#include "fsl_common.h"
3132
#include "fsl_iomuxc.h"
@@ -966,8 +967,8 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
966967
* (see definition of DCACHE_InvalidateByRange).
967968
* To ensure all data is included we align the address downwards, and the length upwards.
968969
*/
969-
uint32_t aligned_address = address - (address % 32);
970-
uint32_t aligned_len = len + (32 - (len % 32));
970+
uint32_t aligned_address, aligned_len;
971+
hal_flash_cache_align_range(address, (uint32_t)len, &aligned_address, &aligned_len);
971972
DCACHE_InvalidateByRange(aligned_address, aligned_len);
972973
/* Re-enable interrupts */
973974
asm volatile("cpsie i");
@@ -1010,8 +1011,8 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
10101011
* (see definition of DCACHE_InvalidateByRange).
10111012
* To ensure all data is included we align the address downwards, and the length upwards.
10121013
*/
1013-
uint32_t aligned_address = address - (address % 32);
1014-
uint32_t aligned_len = len + (32 - (len % 32));
1014+
uint32_t aligned_address, aligned_len;
1015+
hal_flash_cache_align_range(address, (uint32_t)len, &aligned_address, &aligned_len);
10151016
DCACHE_InvalidateByRange(aligned_address, aligned_len);
10161017
/* Re-enable interrupts */
10171018
asm volatile("cpsie i");

hal/imx_rt.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* imx_rt.h
2+
*
3+
* Support routines for the i.MX RT HAL, kept free of NXP MCUXpresso SDK
4+
* dependencies so they can be exercised directly in the host unit tests.
5+
*
6+
* Copyright (C) 2026 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+
#ifndef IMX_RT_H
25+
#define IMX_RT_H
26+
27+
#include <stdint.h>
28+
29+
/* Flash is memory mapped, so after a program/erase, the affected range must
30+
* be invalidated in the data cache to ensure coherency. The cache line size
31+
* is 32 bytes, so both the address and length passed to
32+
* DCACHE_InvalidateByRange() must be 32-byte aligned: the address is rounded
33+
* down, and the length is rounded up by the same down-alignment offset plus
34+
* "len", so that the invalidated range always fully covers
35+
* [address, address + len). */
36+
static inline void hal_flash_cache_align_range(uint32_t address, uint32_t len,
37+
uint32_t *aligned_address, uint32_t *aligned_len)
38+
{
39+
uint32_t start = address - (address % 32);
40+
uint32_t unaligned_len = len + (address - start);
41+
42+
*aligned_address = start;
43+
*aligned_len = unaligned_len + ((32 - (unaligned_len % 32)) % 32);
44+
}
45+
46+
#endif /* IMX_RT_H */

hal/kinetis.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,14 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
330330
if (ret != kStatus_FTFx_Success)
331331
return -1;
332332
}
333-
address += i;
334-
len -= i;
333+
address = address_align + i;
334+
len -= (int)(i - start_off);
335335
} else {
336336
uint32_t len_align = len - (len & 0x07);
337337
ret = FLASH_Program(&pflash, address, (uint8_t*)data + w, len_align);
338338
if (ret != kStatus_FTFx_Success)
339339
return -1;
340+
w += len_align;
340341
len -= len_align;
341342
address += len_align;
342343
}

hal/mcxa.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
9393
if (ret != kStatus_Success)
9494
return -1;
9595
}
96-
address += i;
97-
len -= i;
96+
address = address_align + i;
97+
len -= (int)(i - start_off);
9898
}
9999
else {
100100
uint32_t len_align = len - (len & 0x0F);
101101
ret = FLASH_ProgramPhrase(&pflash, address, (uint8_t*)data + w, len_align);
102102
if (ret != kStatus_Success)
103103
return -1;
104+
w += len_align;
104105
len -= len_align;
105106
address += len_align;
106107
}

hal/mcxw.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
155155
int w = 0;
156156
const uint32_t flash_word_size = 16;
157157
const uint32_t empty_qword[4] = {
158-
0xFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
158+
0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
159159
};
160160

161161
while (len > 0) {
@@ -172,8 +172,8 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
172172
if (memcmp(aligned_qword, empty_qword, flash_word_size) != 0) {
173173
write_flash_qword((uint32_t *)address_align, aligned_qword);
174174
}
175-
address += i;
176-
len -= i;
175+
address = address_align + i;
176+
len -= (int)(i - start_off);
177177
}
178178
else {
179179
uint32_t i;
@@ -183,6 +183,7 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
183183
write_flash_qword((uint32_t *)(address + i),
184184
(const uint32_t *)(data + w + i));
185185
}
186+
w += len_align;
186187
len -= len_align;
187188
address += len_align;
188189
}

hal/same51.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ void hal_init(void)
280280
/* Turn off watchdog */
281281
WDT_CTRL &= (~WDT_EN);
282282
/* Run the bootloader with interrupts off */
283+
#ifndef WOLFBOOT_UNIT_TEST_FLASH_WRITE
283284
__asm__ volatile ("cpsid i");
285+
#endif
284286

285287
/* Initialize clock */
286288
clock_init();
@@ -357,17 +359,16 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
357359
} else {
358360
uint32_t val;
359361
uint8_t *vbytes = (uint8_t *)(&val);
360-
uint32_t off = (address % 4);
361-
dst = (uint32_t *)(address - off);
362-
uint32_t dst_idx = (i + off) >> 2;
363-
val = dst[dst_idx];
362+
uint32_t off = ((address + i) % 4);
363+
dst = (uint32_t *)(address + i - off);
364+
val = *dst;
364365
while (off < 4) {
365366
if (i < len)
366367
vbytes[off++] = data[i++];
367368
else
368369
off++;
369370
}
370-
dst[dst_idx] = val;
371+
*dst = val;
371372
}
372373
if ((i == len) || ((i % 16)== 0))
373374
NVMCTRLB = (NVMCMD_WQW | NVMCMD_KEY);

hal/samr21.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ void hal_init(void)
106106
{
107107

108108
WDT_CTRL &= (~WDT_EN);
109+
#ifndef WOLFBOOT_UNIT_TEST_FLASH_WRITE
109110
__asm__ volatile ("cpsid i");
111+
#endif
110112
uint32_t i, reg;
111113
/* enable clocks for the power, sysctrl and gclk modules */
112114
APBAMASK_REG = APBAMASK_PM_EN | APBAMASK_SYSCTRL_EN | APBAMASK_GCLK_EN;
@@ -176,17 +178,16 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
176178
} else {
177179
uint32_t val;
178180
uint8_t *vbytes = (uint8_t *)(&val);
179-
uint32_t off = (address % 4);
180-
dst = (uint32_t *)(address - off);
181-
uint32_t dst_idx = (i + off) >> 2;
182-
val = dst[dst_idx];
181+
uint32_t off = ((address + i) % 4);
182+
dst = (uint32_t *)(address + i - off);
183+
val = *dst;
183184
while (off < 4) {
184185
if (i < len)
185186
vbytes[off++] = data[i++];
186187
else
187188
off++;
188189
}
189-
dst[dst_idx] = val;
190+
*dst = val;
190191
}
191192
}
192193
/* Enable write protection */

options.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ endif
181181

182182
## DSA Settings
183183
ifeq ($(SIGN),NONE)
184+
$(warning SIGN=NONE / WOLFBOOT_NO_SIGN=1 disables firmware signature verification; images are NOT authenticated. Do not use in production.)
184185
SIGN_OPTIONS+=--no-sign
185186
ifeq ($(HASH),SHA384)
186187
STACK_USAGE=3760
@@ -1003,6 +1004,7 @@ WOLFBOOT_LOAD_FPGA_ADDRESS ?= 0
10031004
# logged warning that continues the boot.
10041005
FPGA_NONFATAL ?= 0
10051006
ifeq ($(FPGA_NONFATAL),1)
1007+
$(warning FPGA_NONFATAL=1 continues booting when the FPGA bitstream fails to load; the device may run without security-relevant programmable logic)
10061008
CFLAGS+=-DWOLFBOOT_FPGA_NONFATAL
10071009
endif
10081010
# FIT_CONFIG_SELECT=1 lets the target pick a per-board FIT configuration

src/delta.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,15 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
303303
blk_start = pa - ctx->src_a;
304304
if (blk_start > BLOCK_OFF_MAX)
305305
return -1;
306+
if (((blk_start >> 16) & 0xFF) == ESC) {
307+
/* The most-significant offset byte would collide with
308+
* the escaped-literal marker (ESC ESC), making the
309+
* header indistinguishable from a literal ESC byte on
310+
* decode. Skip this candidate and keep searching.
311+
*/
312+
pa++;
313+
continue;
314+
}
306315
b_start = ctx->off_b;
307316
pa+= BLOCK_HDR_SIZE;
308317
ctx->off_b += BLOCK_HDR_SIZE;
@@ -367,6 +376,13 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
367376
blk_start = pb - ctx->src_b;
368377
if (blk_start > BLOCK_OFF_MAX)
369378
return -1;
379+
if (((blk_start >> 16) & 0xFF) == ESC) {
380+
/* Same ESC-collision hazard as the forward-match
381+
* path: skip this candidate and keep searching.
382+
*/
383+
pb++;
384+
continue;
385+
}
370386
pb+= BLOCK_HDR_SIZE;
371387
ctx->off_b += BLOCK_HDR_SIZE;
372388
while ((pb < pb_limit) &&

0 commit comments

Comments
 (0)