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 
main(int argc,char * argv[])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 }
109