Skip to content

Commit 5234c36

Browse files
nordicjmanangl
authored andcommitted
bootloader: bl_validation: Do not output on external use
Output uses logging, which has no guarantee of even being enabled or supported Signed-off-by: Jamie McCrae <[email protected]>
1 parent df262ac commit 5234c36

File tree

1 file changed

+95
-40
lines changed

1 file changed

+95
-40
lines changed

subsys/bootloader/bl_validation/bl_validation.c

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,19 @@ static bool validate_signature(const uint32_t fw_src_address, const uint32_t fw_
202202
int init_retval = bl_crypto_init();
203203

204204
if (init_retval) {
205-
LOG_ERR("bl_crypto_init() returned %d.", init_retval);
205+
if (!external) {
206+
LOG_ERR("bl_crypto_init() returned %d.", init_retval);
207+
}
206208
return false;
207209
}
208210

209211
init_retval = verify_public_keys();
210212
if (init_retval) {
211-
LOG_ERR("verify_public_keys() returned %d.", init_retval);
212-
if (init_retval == -EHASHFF) {
213-
LOG_INF("A public key contains 0xFFFF, which is "
214-
"unsupported");
213+
if (!external) {
214+
LOG_ERR("verify_public_keys() returned %d.", init_retval);
215+
if (init_retval == -EHASHFF) {
216+
LOG_INF("A public key contains 0xFFFF, which is unsupported");
217+
}
215218
}
216219
return false;
217220
}
@@ -230,19 +233,25 @@ static bool validate_signature(const uint32_t fw_src_address, const uint32_t fw_
230233

231234
if (read_retval != SB_PUBLIC_KEY_HASH_LEN) {
232235
if (read_retval == -EINVAL) {
233-
LOG_INF("Key %d has been invalidated, try next.",
234-
key_data_idx);
236+
if (!external) {
237+
LOG_INF("Key %d has been invalidated, try next.",
238+
key_data_idx);
239+
}
235240
continue;
236241
} else {
237-
LOG_ERR("public_key_data_read failed: %d.",
238-
read_retval);
242+
if (!external) {
243+
LOG_ERR("public_key_data_read failed: %d.",
244+
read_retval);
245+
}
239246
return false;
240247
}
241248
}
242249

243-
LOG_INF("Verifying signature against key %d.", key_data_idx);
244-
LOG_INF("Hash: 0x%02x...%02x", key_data[0],
245-
key_data[SB_PUBLIC_KEY_HASH_LEN-1]);
250+
if (!external) {
251+
LOG_INF("Verifying signature against key %d.", key_data_idx);
252+
LOG_INF("Hash: 0x%02x...%02x", key_data[0],
253+
key_data[SB_PUBLIC_KEY_HASH_LEN-1]);
254+
}
246255
int retval = rot_verify(fw_val_info->public_key,
247256
key_data,
248257
fw_val_info->signature,
@@ -251,22 +260,32 @@ static bool validate_signature(const uint32_t fw_src_address, const uint32_t fw_
251260

252261
if (retval == 0) {
253262
for (uint32_t i = 0; i < key_data_idx; i++) {
254-
LOG_INF("Invalidating key %d.", i);
263+
if (!external) {
264+
LOG_INF("Invalidating key %d.", i);
265+
}
255266
invalidate_public_key(i);
256267
}
257-
LOG_INF("Firmware signature verified.");
268+
if (!external) {
269+
LOG_INF("Firmware signature verified.");
270+
}
258271
return true;
259272
} else if (retval == -EHASHINV) {
260-
LOG_WRN("Public key didn't match, try next.");
273+
if (!external) {
274+
LOG_WRN("Public key didn't match, try next.");
275+
}
261276
continue;
262277
} else {
263-
LOG_ERR("Firmware validation failed with error %d.",
264-
retval);
278+
if (!external) {
279+
LOG_ERR("Firmware validation failed with error %d.",
280+
retval);
281+
}
265282
return false;
266283
}
267284
}
268285

269-
LOG_ERR("Failed to validate signature.");
286+
if (!external) {
287+
LOG_ERR("Failed to validate signature.");
288+
}
270289
return false;
271290
}
272291

@@ -279,20 +298,26 @@ static bool validate_hash(const uint32_t fw_src_address, const uint32_t fw_size,
279298
int retval = bl_crypto_init();
280299

281300
if (retval) {
282-
LOG_ERR("bl_crypto_init() returned %d.", retval);
301+
if (!external) {
302+
LOG_ERR("bl_crypto_init() returned %d.", retval);
303+
}
283304
return false;
284305
}
285306

286307
retval = bl_sha256_verify((const uint8_t *)fw_src_address, fw_size,
287308
fw_val_info->hash);
288309

289310
if (retval != 0) {
290-
LOG_ERR("Firmware validation failed with error %d.",
291-
retval);
311+
if (!external) {
312+
LOG_ERR("Firmware validation failed with error %d.",
313+
retval);
314+
}
292315
return false;
293316
}
294317

295-
LOG_INF("Firmware hash verified.");
318+
if (!external) {
319+
LOG_INF("Firmware hash verified.");
320+
}
296321

297322
return true;
298323
}
@@ -309,37 +334,51 @@ static bool validate_firmware(uint32_t fw_dst_address, uint32_t fw_src_address,
309334
const uint32_t fw_src_end = (fw_src_address + fwinfo->size);
310335

311336
if (!fwinfo) {
312-
LOG_ERR("NULL parameter.");
337+
if (!external) {
338+
LOG_ERR("NULL parameter.");
339+
}
313340
return false;
314341
}
315342

316343
if (!fw_info_check((uint32_t)fwinfo)) {
317-
LOG_ERR("Invalid firmware info format.");
344+
if (!external) {
345+
LOG_ERR("Invalid firmware info format.");
346+
}
318347
return false;
319348
}
320349

321350
if (fw_dst_address != fwinfo->address) {
322-
LOG_ERR("The firmware doesn't belong at destination addr.");
351+
if (!external) {
352+
LOG_ERR("The firmware doesn't belong at destination addr.");
353+
}
323354
return false;
324355
}
325356

326357
if (!external && (fw_src_address != fw_dst_address)) {
327-
LOG_ERR("src and dst must be equal for local calls.");
358+
if (!external) {
359+
LOG_ERR("src and dst must be equal for local calls.");
360+
}
328361
return false;
329362
}
330363

331364
if (fw_info_find(fw_src_address) != fwinfo) {
332-
LOG_ERR("Firmware info doesn't point to itself.");
365+
if (!external) {
366+
LOG_ERR("Firmware info doesn't point to itself.");
367+
}
333368
return false;
334369
}
335370

336371
if (fwinfo->valid != CONFIG_FW_INFO_VALID_VAL) {
337-
LOG_ERR("Firmware has been invalidated: 0x%x.",
338-
fwinfo->valid);
372+
if (!external) {
373+
LOG_ERR("Firmware has been invalidated: 0x%x.",
374+
fwinfo->valid);
375+
}
339376
return false;
340377
}
341378

342-
LOG_INF("Trying to get Firmware version");
379+
if (!external) {
380+
LOG_INF("Trying to get Firmware version");
381+
}
343382

344383
#if defined(CONFIG_NRFX_NVMC)
345384
uint16_t stored_version;
@@ -350,8 +389,10 @@ static bool validate_firmware(uint32_t fw_dst_address, uint32_t fw_src_address,
350389
int err = get_monotonic_version(&stored_version);
351390

352391
if (err) {
353-
LOG_ERR("Cannot read the firmware version. %d", err);
354-
LOG_INF("We assume this is due to the firmware version not being enabled.");
392+
if (!external) {
393+
LOG_ERR("Cannot read the firmware version. %d", err);
394+
LOG_INF("We assume this is due to the firmware version not being enabled.");
395+
}
355396

356397
/*
357398
* Errors in reading the firmware version are assumed to be
@@ -365,8 +406,10 @@ static bool validate_firmware(uint32_t fw_dst_address, uint32_t fw_src_address,
365406
}
366407

367408
if (fwinfo->version < stored_version) {
368-
LOG_ERR("Firmware version (%u) is smaller than monotonic counter (%u).",
369-
fwinfo->version, stored_version);
409+
if (!external) {
410+
LOG_ERR("Firmware version (%u) is smaller than monotonic counter (%u).",
411+
fwinfo->version, stored_version);
412+
}
370413
return false;
371414
}
372415

@@ -375,19 +418,25 @@ static bool validate_firmware(uint32_t fw_dst_address, uint32_t fw_src_address,
375418
"B0's slots aren't the same size. Check pm.yml.");
376419
if ((fwinfo->size > (PM_S0_SIZE))
377420
|| (fwinfo->total_size > fwinfo->size)) {
378-
LOG_ERR("Invalid size or total_size in firmware info.");
421+
if (!external) {
422+
LOG_ERR("Invalid size or total_size in firmware info.");
423+
}
379424
return false;
380425
}
381426
#endif
382427

383428
if (!region_within(fwinfo_address, fwinfo_end,
384429
fw_src_address, fw_src_end)) {
385-
LOG_ERR("Firmware info is not within signed region.");
430+
if (!external) {
431+
LOG_ERR("Firmware info is not within signed region.");
432+
}
386433
return false;
387434
}
388435

389436
if (!within(fwinfo->boot_address, fw_dst_address, fw_dst_end)) {
390-
LOG_ERR("Boot address is not within signed region.");
437+
if (!external) {
438+
LOG_ERR("Boot address is not within signed region.");
439+
}
391440
return false;
392441
}
393442

@@ -398,19 +447,25 @@ static bool validate_firmware(uint32_t fw_dst_address, uint32_t fw_src_address,
398447
const uint32_t reset_vector = ((const uint32_t *)(fw_src_address + stack_ptr_offset))[1];
399448

400449
if (!within(reset_vector, fw_dst_address, fw_dst_end)) {
401-
LOG_ERR("Reset handler is not within signed region.");
450+
if (!external) {
451+
LOG_ERR("Reset handler is not within signed region.");
452+
}
402453
return false;
403454
}
404455

405456
fw_val_info = validation_info_find(fw_src_address + fwinfo->size, 4);
406457

407458
if (!fw_val_info) {
408-
LOG_ERR("Could not find valid firmware validation info.");
459+
if (!external) {
460+
LOG_ERR("Could not find valid firmware validation info.");
461+
}
409462
return false;
410463
}
411464

412465
if (fw_val_info->address != fwinfo->address) {
413-
LOG_ERR("Validation info doesn't belong to this firmware.");
466+
if (!external) {
467+
LOG_ERR("Validation info doesn't belong to this firmware.");
468+
}
414469
return false;
415470
}
416471

0 commit comments

Comments
 (0)