Skip to content

Commit c005ba6

Browse files
authored
Merge pull request #417 from danielinux/fix-tlv-alignment
Custom TLV: enforce 8B alignment for all fields
2 parents 01e22ed + 5ecd2f7 commit c005ba6

File tree

7 files changed

+95
-17
lines changed

7 files changed

+95
-17
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Custom TLV - test with simulator target
2+
3+
on:
4+
push:
5+
branches: [ 'master', 'main', 'release/**' ]
6+
pull_request:
7+
branches: [ '*' ]
8+
9+
jobs:
10+
custom_tlv_simulator_tests:
11+
runs-on: ubuntu-20.04
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
with:
16+
submodules: true
17+
18+
- name: make clean
19+
run: |
20+
make distclean
21+
22+
- name: Select config
23+
run: |
24+
cp config/examples/sim.config .config
25+
26+
- name: Build tools
27+
run: |
28+
make -C tools/keytools && make -C tools/bin-assemble
29+
30+
- name: Build wolfboot.elf and test-app/image.elf
31+
run: |
32+
make clean && make
33+
34+
- name: Sign the image with a custom TLV
35+
run: |
36+
tools/keytools/sign --ed25519 --custom-tlv-buffer 0x0034 AABBCCDDEEFF0011223344 test-app/image.elf wolfboot_signing_private_key.der 1
37+
38+
- name: Re-assemble the internal_flash.dd image file
39+
run: |
40+
make assemble_internal_flash.dd
41+
42+
- name: Run get_tlv simulator test
43+
run: |
44+
[ x`./wolfboot.elf get_tlv 2>/dev/null| tail -1` = xAABBCCDDEEFF0011223344 ]

Makefile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,18 @@ test-app/image.elf: wolfboot.elf
198198
$(Q)$(MAKE) -C test-app WOLFBOOT_ROOT="$(WOLFBOOT_ROOT)" image.elf
199199
$(Q)$(SIZE) test-app/image.elf
200200

201-
internal_flash.dd: $(BINASSEMBLE) wolfboot.bin $(BOOT_IMG) $(PRIVATE_KEY) test-app/image_v1_signed.bin
202-
@echo "\t[MERGE] internal_flash.dd"
203-
$(Q)dd if=/dev/zero bs=1 count=$$(($(WOLFBOOT_SECTOR_SIZE))) > /tmp/swap
204-
$(Q)$(BINASSEMBLE) $@ \
201+
assemble_internal_flash.dd: FORCE
202+
$(Q)$(BINASSEMBLE) internal_flash.dd \
205203
0 wolfboot.bin \
206204
$$(($(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET))) test-app/image_v1_signed.bin \
207205
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS)-$(ARCH_FLASH_OFFSET))) /tmp/swap \
208206
$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS)-$(ARCH_FLASH_OFFSET))) /tmp/swap
209207

208+
internal_flash.dd: $(BINASSEMBLE) wolfboot.bin $(BOOT_IMG) $(PRIVATE_KEY) test-app/image_v1_signed.bin
209+
@echo "\t[MERGE] internal_flash.dd"
210+
$(Q)dd if=/dev/zero bs=1 count=$$(($(WOLFBOOT_SECTOR_SIZE))) > /tmp/swap
211+
make assemble_internal_flash.dd
212+
210213
factory.bin: $(BINASSEMBLE) wolfboot.bin $(BOOT_IMG) $(PRIVATE_KEY) test-app/image_v1_signed.bin
211214
@echo "\t[MERGE] $@"
212215
$(Q)$(BINASSEMBLE) $@ \

docs/firmware_image.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,16 @@ From the bootloader code, we can then retrieve the value of the custom field usi
8080
uint32_t value;
8181
uint8_t* ptr = NULL;
8282
uint16_t tlv = 0x34;
83-
uint8_t* imageHdr = (uint8_t*)WOLFBOOT_PARTITION_BOOT_ADDRESS; /* WOLFBOOT_PARTITION_UPDATE_ADDRESS */
83+
uint8_t* imageHdr = (uint8_t*)WOLFBOOT_PARTITION_BOOT_ADDRESS + IMAGE_HEADER_OFFSET;
8484
uint16_t size = wolfBoot_find_header(imageHdr, tlv, &ptr);
85-
if (size != sizeof(uint32_t) || ptr == NULL) {
86-
/* Error: the field is not present or has the wrong size */
85+
if (size > 0 && ptr != NULL) {
86+
/* Found field and ptr points to value 0xAABBCCDD */
87+
memcpy(&value, ptr, size);
88+
printf("TLV 0x%x=0x%x\n", tlv, value);
89+
}
90+
else {
91+
/* Error: the field is not found */
8792
}
88-
89-
/* From here, the value 0xAABBCCDD is at ptr */
90-
memcpy(&value, ptr, size);
91-
printf("TLV 0x%x=0x%x\n", tlv, value);
9293
```
9394

9495
### Image signing tool

include/image.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,6 @@ int wolfBoot_set_update_sector_flag(uint16_t sector, uint8_t newflag);
565565
uint8_t* wolfBoot_peek_image(struct wolfBoot_image *img, uint32_t offset,
566566
uint32_t* sz);
567567

568-
569-
/* Defined in libwolfboot */
570-
uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr);
571-
572568
/* get header type for image */
573569
uint16_t wolfBoot_get_header(struct wolfBoot_image *img, uint16_t type, uint8_t **ptr);
574570

include/wolfboot/wolfboot.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ uint32_t wolfBoot_get_blob_version(uint8_t *blob);
275275
uint16_t wolfBoot_get_blob_type(uint8_t *blob);
276276
uint32_t wolfBoot_get_blob_diffbase_version(uint8_t *blob);
277277

278+
uint16_t wolfBoot_find_header(uint8_t *haystack, uint16_t type, uint8_t **ptr);
279+
278280
/* Get partition ID from manifest header */
279281
static inline uint8_t wolfBoot_get_blob_partition_id(uint8_t *blob) {
280282
return wolfBoot_get_blob_type(blob) & HDR_IMG_TYPE_PART_MASK;

test-app/app_sim.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,33 @@ int do_cmd(const char *cmd)
7070
if (strcmp(cmd, "reset") == 0) {
7171
exit(0);
7272
}
73+
if (strncmp(cmd, "get_tlv",7) == 0) {
74+
/* boot partition and skip the image header offset (8 bytes) */
75+
uint8_t* imageHdr = (uint8_t*)WOLFBOOT_PARTITION_BOOT_ADDRESS + IMAGE_HEADER_OFFSET;
76+
uint8_t* ptr = NULL;
77+
uint16_t tlv = 0x34; /* default */
78+
int size;
79+
int i;
7380

81+
const char* tlvStr = strstr(cmd, "get_tlv=");
82+
if (tlvStr) {
83+
tlvStr += strlen("get_tlv=");
84+
tlv = (uint16_t)atoi(tlvStr);
85+
}
86+
87+
size = wolfBoot_find_header(imageHdr, tlv, &ptr);
88+
if (size > 0 && ptr != NULL) {
89+
/* From here, the value 0xAABBCCDD is at ptr */
90+
printf("TLV 0x%x: found (size %d):\n", tlv, size);
91+
for (i=0; i<size; i++) {
92+
printf("%02X", ptr[i]);
93+
}
94+
printf("\n");
95+
return 0;
96+
} else {
97+
printf("TLV 0x%x: not found!\r\n", tlv);
98+
}
99+
}
74100
/* wrong command */
75101
return -1;
76102
}

tools/keytools/sign.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,10 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
10851085
uint32_t i;
10861086
for (i = 0; i < CMD.custom_tlvs; i++) {
10871087
/* require 8-byte alignment */
1088-
while ((header_idx % 8) != 0)
1088+
/* The offset '4' takes into account 2B Tag + 2B Len, so that the
1089+
* Value starts at (addr % 8 == 0) position.
1090+
*/
1091+
while ((header_idx % 8) != 4)
10891092
header_idx++;
10901093

10911094
if (CMD.custom_tlv[i].buffer == NULL) {
@@ -1099,7 +1102,10 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
10991102
}
11001103

11011104
/* Add padding bytes. Sha-3 val field requires 8-byte alignment */
1102-
while ((header_idx % 8) != 0)
1105+
/* The offset '4' takes into account 2B Tag + 2B Len, so that the Value
1106+
* starts at (addr % 8 == 0) position.
1107+
*/
1108+
while ((header_idx % 8) != 4)
11031109
header_idx++;
11041110

11051111
/* Calculate hashes */

0 commit comments

Comments
 (0)