Skip to content

Commit 724ee7e

Browse files
michalek-nonvlsianpu
authored andcommitted
samples: compression_test
Stand alone tool that extracts compressed stream from signed.bin, decompresses it and compares against uncompressed application binary. Signed-off-by: Mateusz Michalek <[email protected]>
1 parent 9a02794 commit 724ee7e

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

samples/compression_test/README.txt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Independent LZMA test
2+
---------------------
3+
4+
This tool finds and extracts compressed stream, decompresses it and verifies if
5+
decompressed one is identical as before compression.
6+
7+
Building and running:
8+
9+
change directory to the top of the repos:
10+
11+
cd $ZEPHYR_BASE
12+
cd ..
13+
14+
build tool:
15+
16+
g++ bootloader/mcuboot/samples/compression_test/independent_cmp.c -o indcmp
17+
18+
build example application:
19+
20+
west build -b nrf54l15dk/nrf54l15/cpuapp -p
21+
-s zephyr/samples/hello_world/ --
22+
-DSB_CONFIG_BOOTLOADER_MCUBOOT=y
23+
-DSB_CONFIG_MCUBOOT_MODE_OVERWRITE_ONLY=y
24+
-DSB_CONFIG_MCUBOOT_COMPRESSED_IMAGE_SUPPORT=y
25+
26+
27+
compare application image with the one carried by signed binary:
28+
29+
./indcmp build/hello_world/zephyr/zephyr.signed.bin
30+
build/hello_world/zephyr/zephyr.bin
31+
32+
note: order of arguments matter. Compressed goes first.
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <unistd.h>
8+
#include <fcntl.h>
9+
#include <cstdint>
10+
#include <cstdio>
11+
#include <cstdlib>
12+
13+
#define EXPECTED_MAGIC 0x96f3b83d
14+
#define LZMA_HEADER_SIZE 2
15+
#define FLAG_LZMA2 0x400
16+
#define FLAG_ARM_THUMB 0x800
17+
18+
struct __attribute__((__packed__)) image_header {
19+
uint32_t magic;
20+
uint32_t unused0;
21+
uint16_t hdr_size;
22+
uint16_t unused1;
23+
uint32_t img_size;
24+
uint32_t flags;
25+
};
26+
27+
int main(int argc, char *argv[])
28+
{
29+
if (argc != 3) {
30+
printf("needs 2 parameters: signed image file and application binary file\n\r");
31+
return EXIT_FAILURE;
32+
}
33+
34+
int app_fd = open(argv[2], O_NONBLOCK);
35+
36+
if (app_fd < 0) {
37+
printf("Opening signed image failed.\n\r");
38+
return EXIT_FAILURE;
39+
}
40+
int signed_fd = open(argv[1], O_NONBLOCK);
41+
42+
if (signed_fd < 0) {
43+
printf("Opening signed image failed.\n\r");
44+
return EXIT_FAILURE;
45+
}
46+
system("mkdir -p tmp; rm -rf tmp/stream*");
47+
struct image_header ih;
48+
size_t rc = pread(signed_fd, &ih, sizeof(struct image_header), 0);
49+
50+
if (ih.magic != EXPECTED_MAGIC) {
51+
printf("Expected magic value at the start of signed image.\n\r");
52+
printf("Input files in wrong order?\n\r");
53+
return EXIT_FAILURE;
54+
}
55+
if (!ih.flags & FLAG_LZMA2) {
56+
printf("Signed image is not compressed with LZMA2.\n\r");
57+
return EXIT_FAILURE;
58+
}
59+
int lzma_stream_size = ih.img_size - LZMA_HEADER_SIZE;
60+
int lzma_stream_offset = ih.hdr_size + LZMA_HEADER_SIZE;
61+
uint8_t *lzma_buf = (uint8_t *)malloc(lzma_stream_size);
62+
63+
rc = pread(signed_fd, lzma_buf, lzma_stream_size, lzma_stream_offset);
64+
if (rc != lzma_stream_size) {
65+
printf("Error while reading compressed stream from signed image.\n\r");
66+
return EXIT_FAILURE;
67+
}
68+
int lzma_fd = creat("tmp/stream.lzma", 0600);
69+
70+
write(lzma_fd, lzma_buf, lzma_stream_size);
71+
close(lzma_fd);
72+
if (ih.flags & FLAG_ARM_THUMB) {
73+
system("unlzma --armthumb --lzma2 --format=raw --suffix=.lzma tmp/stream.lzma");
74+
} else {
75+
system("unlzma --lzma2 --format=raw --suffix=.lzma tmp/stream.lzma");
76+
}
77+
int unlzma_fd = open("tmp/stream", O_NONBLOCK);
78+
int unlzma_size = lseek(unlzma_fd, 0L, SEEK_END);
79+
int app_size = lseek(app_fd, 0L, SEEK_END);
80+
81+
if (app_size != unlzma_size) {
82+
printf("Decompressed stream size and application size mismatch.\n\r");
83+
return EXIT_FAILURE;
84+
}
85+
uint8_t *unlzma_buf = (uint8_t *)malloc(unlzma_size);
86+
uint8_t *app_buf = (uint8_t *)malloc(app_size);
87+
88+
rc = pread(app_fd, app_buf, app_size, 0);
89+
if (rc != app_size) {
90+
printf("Error while loading application binary.\n\r");
91+
return EXIT_FAILURE;
92+
}
93+
rc = pread(unlzma_fd, unlzma_buf, unlzma_size, 0);
94+
if (rc != unlzma_size) {
95+
printf("Error while loading decompressed stream.\n\r");
96+
return EXIT_FAILURE;
97+
}
98+
for (int i = 0; i < app_size; i++) {
99+
if (app_buf[i] != unlzma_buf[i]) {
100+
printf("Diff at %d\r\n", i);
101+
return EXIT_FAILURE;
102+
}
103+
}
104+
close(unlzma_fd);
105+
close(app_fd);
106+
printf("All checks OK.\n\r");
107+
return 0;
108+
}

0 commit comments

Comments
 (0)