Skip to content

Commit 2a05d65

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 15b8991 commit 2a05d65

File tree

12 files changed

+140
-7
lines changed

12 files changed

+140
-7
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

@@ -587,13 +597,18 @@ boot_erase_region(const struct flash_area *fa, uint32_t off, uint32_t size, bool
587597
{
588598
int rc = 0;
589599

600+
BOOT_LOG_DBG("boot_erase_region: flash_area %p, offset %d, size %d, backwards == %d",
601+
fa, off, size, (int)backwards);
602+
590603
if (off >= flash_area_get_size(fa) || (flash_area_get_size(fa) - off) < size) {
591604
rc = -1;
592605
goto end;
593606
} else if (device_requires_erase(fa)) {
594607
uint32_t end_offset = 0;
595608
struct flash_sector sector;
596609

610+
BOOT_LOG_DBG("boot_erase_region: device with erase");
611+
597612
if (backwards) {
598613
/* Get the lowest page offset first */
599614
rc = flash_area_get_sector(fa, off, &sector);
@@ -667,6 +682,8 @@ boot_erase_region(const struct flash_area *fa, uint32_t off, uint32_t size, bool
667682
off += 1;
668683
}
669684
}
685+
} else {
686+
BOOT_LOG_DBG("boot_erase_region: device without erase");
670687
}
671688

672689
end:

boot/bootutil/src/bootutil_public.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ boot_write_magic(const struct flash_area *fap)
324324
memset(&magic[0], erased_val, sizeof(magic));
325325
memcpy(&magic[BOOT_MAGIC_ALIGN_SIZE - BOOT_MAGIC_SZ], BOOT_IMG_MAGIC, BOOT_MAGIC_SZ);
326326

327-
BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
327+
BOOT_LOG_DBG("boot_write_magic: fa_id=%d off=0x%lx (0x%lx)",
328328
flash_area_get_id(fap), (unsigned long)off,
329329
(unsigned long)(flash_area_get_off(fap) + off));
330330
rc = flash_area_write(fap, pad_off, &magic[0], BOOT_MAGIC_ALIGN_SIZE);
@@ -350,9 +350,14 @@ boot_write_trailer(const struct flash_area *fap, uint32_t off,
350350
uint32_t align;
351351
int rc;
352352

353+
BOOT_LOG_DBG("boot_write_trailer: for %p at %d, size = %d",
354+
fap, off, inlen);
355+
353356
align = flash_area_align(fap);
354357
align = ALIGN_UP(inlen, align);
355358
if (align > BOOT_MAX_ALIGN) {
359+
/* This should never happen */
360+
assert(0);
356361
return -1;
357362
}
358363
erased_val = flash_area_erased_val(fap);
@@ -596,6 +601,9 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
596601
struct boot_swap_state slot_state;
597602
int rc;
598603

604+
BOOT_LOG_DBG("boot_set_next: fa %p active == %d, confirm == %d",
605+
fa, (int)active, (int)confirm);
606+
599607
if (active) {
600608
/* The only way to set active slot for next boot is to confirm it,
601609
* as DirectXIP will conclude that, since slot has not been confirmed
@@ -606,6 +614,7 @@ boot_set_next(const struct flash_area *fa, bool active, bool confirm)
606614

607615
rc = boot_read_swap_state(fa, &slot_state);
608616
if (rc != 0) {
617+
BOOT_LOG_DBG("boot_set_next: error %d reading state", rc);
609618
return rc;
610619
}
611620

@@ -733,6 +742,8 @@ boot_set_confirmed_multi(int image_index)
733742

734743
rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap);
735744
if (rc != 0) {
745+
BOOT_LOG_DBG("boot_set_confirmed_multi: error %d opening image %d",
746+
rc, image_index);
736747
return BOOT_EFLASH;
737748
}
738749

@@ -760,13 +771,14 @@ int
760771
boot_image_load_header(const struct flash_area *fa_p,
761772
struct image_header *hdr)
762773
{
763-
uint32_t size;
774+
uint32_t size = 0;
764775
int rc = flash_area_read(fa_p, 0, hdr, sizeof *hdr);
765776

777+
BOOT_LOG_DBG("boot_image_load_header: from %p, result %d", fa_p, rc);
778+
766779
if (rc != 0) {
767-
rc = BOOT_EFLASH;
768780
BOOT_LOG_ERR("Failed reading image header");
769-
return BOOT_EFLASH;
781+
return BOOT_EFLASH;
770782
}
771783

772784
if (hdr->ih_magic != IMAGE_MAGIC) {
@@ -783,6 +795,8 @@ boot_image_load_header(const struct flash_area *fa_p,
783795

784796
if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size) ||
785797
size >= flash_area_get_size(fa_p)) {
798+
BOOT_LOG_ERR("Image size bigger than designated area: %lu > %lu",
799+
(unsigned long)size, (unsigned long)flash_area_get_size(fa_p));
786800
return BOOT_EBADIMAGE;
787801
}
788802

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: 7 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
#if defined(MCUBOOT_SIGN_EC256) || defined(MCUBOOT_SIGN_EC384)
3336

@@ -46,6 +49,8 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
4649
uint8_t *pubkey;
4750
uint8_t *end;
4851

52+
BOOT_LOG_DBG("bootutil_verify_sig: ECDSA builtin key %d", key_id);
53+
4954
pubkey = (uint8_t *)bootutil_keys[key_id].key;
5055
end = pubkey + *bootutil_keys[key_id].len;
5156
bootutil_ecdsa_init(&ctx);
@@ -75,6 +80,8 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
7580
bootutil_ecdsa_context ctx;
7681
FIH_DECLARE(fih_rc, FIH_FAILURE);
7782

83+
BOOT_LOG_DBG("bootutil_verify_sig: ECDSA embedded key %hhd", key_id);
84+
7885
/* Use builtin key for image verification, no key parsing is required. */
7986
ctx.key_id = key_id;
8087
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"
@@ -267,6 +270,8 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, size_t slen,
267270
uint8_t *cp;
268271
uint8_t *end;
269272

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

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

0 commit comments

Comments
 (0)