Skip to content

Commit 20f94c2

Browse files
committed
bootutil: Improve logging coverage
Add additional log lines to allow easier tracking potential failures in image validation. Signed-off-by: Dominik Ermel <[email protected]>
1 parent c412cdf commit 20f94c2

12 files changed

+132
-6
lines changed

boot/bootutil/src/bootutil_misc.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ int boot_trailer_scramble_offset(const struct flash_area *fa, size_t alignment,
141141
{
142142
int ret = 0;
143143

144+
BOOT_LOG_DBG("boot_trailer_scramble_offset: flash_area %p, alignment %u",
145+
fa, (unsigned int)alignment);
146+
144147
/* Not allowed to enforce alignment smaller than device allows */
145148
if (alignment < flash_area_align(fa)) {
146149
alignment = flash_area_align(fa);
@@ -162,6 +165,9 @@ int boot_trailer_scramble_offset(const struct flash_area *fa, size_t alignment,
162165
*off = flash_area_get_size(fa) - ALIGN_DOWN(boot_trailer_sz(alignment), alignment);
163166
}
164167

168+
BOOT_LOG_DBG("boot_trailer_scramble_offset: final alignment %u, offset %u",
169+
(unsigned int)alignment, (unsigned int)*off);
170+
165171
return ret;
166172
}
167173

@@ -173,6 +179,8 @@ int boot_header_scramble_off_sz(const struct flash_area *fa, int slot, size_t *o
173179
size_t loff = 0;
174180
struct flash_sector sector;
175181

182+
BOOT_LOG_DBG("boot_header_scramble_off_sz: slot %d", slot);
183+
176184
(void)slot;
177185
#if defined(MCUBOOT_SWAP_USING_OFFSET)
178186
/* In case of swap offset, header of secondary slot image is positioned
@@ -201,6 +209,8 @@ int boot_header_scramble_off_sz(const struct flash_area *fa, int slot, size_t *o
201209
}
202210
*off = loff;
203211

212+
BOOT_LOG_DBG("boot_header_scramble_off_sz: size %u", (unsigned int)*size);
213+
204214
return ret;
205215
}
206216

@@ -578,13 +588,18 @@ boot_erase_region(const struct flash_area *fa, uint32_t off, uint32_t size, bool
578588
{
579589
int rc = 0;
580590

591+
BOOT_LOG_DBG("boot_erase_region: flash_area %p, offset %d, size %d, backwards == %d",
592+
fa, off, size, (int)backwards);
593+
581594
if (off >= flash_area_get_size(fa) || (flash_area_get_size(fa) - off) < size) {
582595
rc = -1;
583596
goto end;
584597
} else if (device_requires_erase(fa)) {
585598
uint32_t end_offset = 0;
586599
struct flash_sector sector;
587600

601+
BOOT_LOG_DBG("boot_erase_region: device with erase");
602+
588603
if (backwards) {
589604
/* Get the lowest page offset first */
590605
rc = flash_area_get_sector(fa, off, &sector);
@@ -658,6 +673,8 @@ boot_erase_region(const struct flash_area *fa, uint32_t off, uint32_t size, bool
658673
off += 1;
659674
}
660675
}
676+
} else {
677+
BOOT_LOG_DBG("boot_erase_region: device without erase");
661678
}
662679

663680
end:

boot/bootutil/src/bootutil_public.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ boot_read_swap_state(const struct flash_area *fap,
248248
uint8_t swap_info;
249249
int rc;
250250

251+
BOOT_LOG_DBG("boot_read_swap_state: flash area %p", fap);
252+
251253
off = boot_magic_off(fap);
252254
rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
253255
if (rc < 0) {
@@ -262,6 +264,7 @@ boot_read_swap_state(const struct flash_area *fap,
262264
off = boot_swap_info_off(fap);
263265
rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
264266
if (rc < 0) {
267+
BOOT_LOG_DBG("boot_read_swap_state: error %d reading state", rc);
265268
return BOOT_EFLASH;
266269
}
267270

@@ -324,12 +327,13 @@ boot_write_magic(const struct flash_area *fap)
324327
memset(&magic[0], erased_val, sizeof(magic));
325328
memcpy(&magic[BOOT_MAGIC_ALIGN_SIZE - BOOT_MAGIC_SZ], BOOT_IMG_MAGIC, BOOT_MAGIC_SZ);
326329

327-
BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
330+
BOOT_LOG_DBG("boot_write_magic: fa_id=%d off=0x%lx (0x%lx)",
328331
flash_area_get_id(fap), (unsigned long)off,
329332
(unsigned long)(flash_area_get_off(fap) + off));
330333
rc = flash_area_write(fap, pad_off, &magic[0], BOOT_MAGIC_ALIGN_SIZE);
331334

332335
if (rc != 0) {
336+
BOOT_LOG_DBG("boot_write_magic: error %d on write", rc);
333337
return BOOT_EFLASH;
334338
}
335339

@@ -350,9 +354,14 @@ boot_write_trailer(const struct flash_area *fap, uint32_t off,
350354
uint32_t align;
351355
int rc;
352356

357+
BOOT_LOG_DBG("boot_write_trailer: for %p at %d, size = %d",
358+
fap, off, inlen);
359+
353360
align = flash_area_align(fap);
354361
align = ALIGN_UP(inlen, align);
355362
if (align > BOOT_MAX_ALIGN) {
363+
/* This should never happen */
364+
assert(0);
356365
return -1;
357366
}
358367
erased_val = flash_area_erased_val(fap);
@@ -596,6 +605,9 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
596605
struct boot_swap_state slot_state;
597606
int rc;
598607

608+
BOOT_LOG_DBG("boot_set_next: fa %p active == %d, confirm == %d",
609+
fa, (int)active, (int)confirm);
610+
599611
if (active) {
600612
/* The only way to set active slot for next boot is to confirm it,
601613
* as DirectXIP will conclude that, since slot has not been confirmed
@@ -606,6 +618,7 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
606618

607619
rc = boot_read_swap_state(fa, &slot_state);
608620
if (rc != 0) {
621+
BOOT_LOG_DBG("boot_set_next: error %d reading state", rc);
609622
return rc;
610623
}
611624

@@ -733,6 +746,8 @@ boot_set_confirmed_multi(int image_index)
733746

734747
rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
735748
if (rc != 0) {
749+
BOOT_LOG_DBG("boot_set_confirmed_multi: error %d opening image %d",
750+
rc, image_index);
736751
return BOOT_EFLASH;
737752
}
738753

@@ -763,10 +778,11 @@ boot_image_load_header(const struct flash_area *fa_p,
763778
uint32_t size;
764779
int rc = flash_area_read(fa_p, 0, hdr, sizeof *hdr);
765780

781+
BOOT_LOG_DBG("boot_image_load_header: from %p, result %d", fa_p, rc);
782+
766783
if (rc != 0) {
767-
rc = BOOT_EFLASH;
768784
BOOT_LOG_ERR("Failed reading image header");
769-
return BOOT_EFLASH;
785+
return BOOT_EFLASH;
770786
}
771787

772788
if (hdr->ih_magic != IMAGE_MAGIC) {
@@ -783,6 +799,7 @@ boot_image_load_header(const struct flash_area *fa_p,
783799

784800
if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size) ||
785801
size >= flash_area_get_size(fa_p)) {
802+
BOOT_LOG_ERR("Incorrect image size");
786803
return BOOT_EBADIMAGE;
787804
}
788805

boot/bootutil/src/ed25519_psa.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ int ED25519_verify(const uint8_t *message, size_t message_len,
2929
psa_key_id_t kid;
3030
int ret = 0; /* Fail by default */
3131

32+
BOOT_LOG_DBG("ED25519_verify: PSA implementation");
33+
3234
/* Initialize PSA Crypto */
3335
status = psa_crypto_init();
3436
if (status != PSA_SUCCESS) {

boot/bootutil/src/encrypted.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@
4343
#include "bootutil/enc_key.h"
4444
#include "bootutil/sign_key.h"
4545
#include "bootutil/crypto/common.h"
46+
#include "bootutil/bootutil_log.h"
4647

4748
#include "bootutil_priv.h"
4849

50+
BOOT_LOG_MODULE_DECLARE(mcuboot);
51+
4952
#define EXPECTED_ENC_LEN BOOT_ENC_TLV_SIZE
5053

5154
#if defined(MCUBOOT_ENCRYPT_RSA)
@@ -411,6 +414,8 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
411414
uint8_t *cpend;
412415
size_t olen;
413416
#endif
417+
418+
BOOT_LOG_DBG("boot_decrypt_key:");
414419
#if defined(MCUBOOT_ENCRYPT_EC256)
415420
bootutil_ecdh_p256_context ecdh_p256;
416421
#endif
@@ -625,8 +630,11 @@ boot_enc_load(struct boot_loader_state *state, int slot,
625630
#endif
626631
int rc;
627632

633+
BOOT_LOG_DBG("boot_enc_load: slot %d", slot);
634+
628635
/* Already loaded... */
629636
if (enc_state[slot].valid) {
637+
BOOT_LOG_DBG("boot_enc_load: already loaded");
630638
return 1;
631639
}
632640

boot/bootutil/src/encrypted_psa.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,11 @@ boot_decrypt_key(const uint8_t *buf, uint8_t *enckey)
146146
uint8_t iv_and_key[PSA_CIPHER_IV_LENGTH(PSA_KEY_TYPE_AES, PSA_ALG_CTR) +
147147
BOOT_ENC_KEY_SIZE];
148148

149+
BOOT_LOG_DBG("boot_decrypt_key: PSA ED25519");
150+
149151
psa_ret = psa_crypto_init();
150152
if (psa_ret != PSA_SUCCESS) {
151-
BOOT_LOG_ERR("AES crypto init failed %d", psa_ret);
153+
BOOT_LOG_ERR("PSA crypto init failed %d", psa_ret);
152154
return -1;
153155
}
154156

boot/bootutil/src/image_ecdsa.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <string.h>
2929

3030
#include "mcuboot_config/mcuboot_config.h"
31+
#include "bootutil/bootutil_log.h"
3132

3233
#if defined(MCUBOOT_SIGN_EC256) || defined(MCUBOOT_SIGN_EC384)
3334

@@ -46,6 +47,8 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
4647
uint8_t *pubkey;
4748
uint8_t *end;
4849

50+
BOOT_LOG_DBG("bootutil_verify_sig: ECDSA builtin key %d", key_id);
51+
4952
pubkey = (uint8_t *)bootutil_keys[key_id].key;
5053
end = pubkey + *bootutil_keys[key_id].len;
5154
bootutil_ecdsa_init(&ctx);
@@ -75,6 +78,8 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
7578
bootutil_ecdsa_context ctx;
7679
FIH_DECLARE(fih_rc, FIH_FAILURE);
7780

81+
BOOT_LOG_DBG("bootutil_verify_sig: ECDSA embedded key %hhd", key_id);
82+
7883
/* Use builtin key for image verification, no key parsing is required. */
7984
ctx.key_id = key_id;
8085
bootutil_ecdsa_init(&ctx);

boot/bootutil/src/image_ed25519.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
#include "bootutil/crypto/common.h"
2222
#endif
2323

24-
#include "bootutil_priv.h"
24+
#include "bootutil/bootutil_log.h"
2525
#include "bootutil/crypto/sha.h"
26+
#include "bootutil_priv.h"
27+
28+
BOOT_LOG_MODULE_DECLARE(mcuboot);
2629

2730
#define EDDSA_SIGNATURE_LENGTH 64
2831
#define NUM_ED25519_BYTES 32
@@ -90,7 +93,11 @@ bootutil_verify(uint8_t *buf, uint32_t blen,
9093
uint8_t *pubkey;
9194
uint8_t *end;
9295

96+
BOOT_LOG_DBG("bootutil_verify: ED25519 key_id %d", (int)key_id);
97+
9398
if (slen != EDDSA_SIGNATURE_LENGTH) {
99+
BOOT_LOG_DBG("bootutil_verify: expected slen %d, got %u",
100+
EDDSA_SIGNATURE_LENGTH, (unsigned int)slen);
94101
FIH_SET(fih_rc, FIH_FAILURE);
95102
goto out;
96103
}
@@ -101,6 +108,7 @@ bootutil_verify(uint8_t *buf, uint32_t blen,
101108
#if !defined(MCUBOOT_KEY_IMPORT_BYPASS_ASN)
102109
rc = bootutil_import_key(&pubkey, end);
103110
if (rc) {
111+
BOOT_LOG_DBG("bootutil_verify: import key failed %d", rc);
104112
FIH_SET(fih_rc, FIH_FAILURE);
105113
goto out;
106114
}
@@ -110,6 +118,7 @@ bootutil_verify(uint8_t *buf, uint32_t blen,
110118
* There is no check whether this is the correct key,
111119
* here, by the algorithm selected.
112120
*/
121+
BOOT_LOG_DBG("bootutil_verify: bypass ASN1");
113122
if (*bootutil_keys[key_id].len < NUM_ED25519_BYTES) {
114123
FIH_SET(fih_rc, FIH_FAILURE);
115124
goto out;
@@ -144,7 +153,11 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen,
144153
{
145154
FIH_DECLARE(fih_rc, FIH_FAILURE);
146155

156+
BOOT_LOG_DBG("bootutil_verify_sig: ED25519 key_id %d", (int)key_id);
157+
147158
if (hlen != IMAGE_HASH_SIZE) {
159+
BOOT_LOG_DBG("bootutil_verify_sig: expected hlen %d, got %d",
160+
IMAGE_HASH_SIZE, hlen);
148161
FIH_SET(fih_rc, FIH_FAILURE);
149162
goto out;
150163
}
@@ -167,6 +180,8 @@ bootutil_verify_img(uint8_t *img, uint32_t size,
167180
{
168181
FIH_DECLARE(fih_rc, FIH_FAILURE);
169182

183+
BOOT_LOG_DBG("bootutil_verify_img: ED25519 key_id %d", (int)key_id);
184+
170185
FIH_CALL(bootutil_verify, fih_rc, img, size, sig,
171186
slen, key_id);
172187

boot/bootutil/src/image_rsa.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#include <string.h>
2929

3030
#include "mcuboot_config/mcuboot_config.h"
31+
#include "bootutil/bootutil_log.h"
32+
33+
BOOT_LOG_MODULE_DECLARE(mcuboot);
3134

3235
#ifdef MCUBOOT_SIGN_RSA
3336
#include "bootutil_priv.h"
@@ -268,6 +271,8 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
268271
uint8_t *cp;
269272
uint8_t *end;
270273

274+
BOOT_LOG_DBG("bootutil_verify_sig: RSA key_id %d", key_id);
275+
271276
bootutil_rsa_init(&ctx);
272277

273278
cp = (uint8_t *)bootutil_keys[key_id].key;

0 commit comments

Comments
 (0)