Commit ac020196166b48371433772db645eab7de8c2ede
Committed by
Alexander Graf
1 parent
727a1afb34
Exists in
smarc_8mq_lf_v2020.04
and in
17 other branches
tools: provide a tool to convert a binary file to an include
For testing EFI disk management we need an in-memory image of a disk. The tool file2include converts a file to a C include. The file is separated into strings of 8 bytes. Only the non-zero strings are written to the include. The output format has been designed to maintain readability. #define EFI_ST_DISK_IMG { 0x00010000, { \ {0x000001b8, "\x94\x37\x69\xfc\x00\x00\x00\x00"}, /* .7i..... */ \ {0x000001c0, "\x02\x00\x83\x02\x02\x00\x01\x00"}, /* ........ */ \ {0x000001c8, "\x00\x00\x7f\x00\x00\x00\x00\x00"}, /* ........ */ \ {0x000001f8, "\x00\x00\x00\x00\x00\x00\x55\xaa"}, /* ......U. */ \ ... {0x00006000, "\x48\x65\x6c\x6c\x6f\x20\x77\x6f"}, /* Hello wo */ \ {0x00006008, "\x72\x6c\x64\x21\x0a\x00\x00\x00"}, /* rld!.... */ \ {0, NULL} } } As the disk image needed for testing contains mostly zeroes a high compression ratio can be attained. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Signed-off-by: Alexander Graf <agraf@suse.de>
Showing 4 changed files with 111 additions and 0 deletions Side-by-side Diff
MAINTAINERS
tools/.gitignore
tools/Makefile
... | ... | @@ -57,6 +57,8 @@ |
57 | 57 | hostprogs-y += dumpimage mkimage |
58 | 58 | hostprogs-$(CONFIG_FIT_SIGNATURE) += fit_info fit_check_sign |
59 | 59 | |
60 | +hostprogs-$(CONFIG_CMD_BOOTEFI_SELFTEST) += file2include | |
61 | + | |
60 | 62 | FIT_SIG_OBJS-$(CONFIG_FIT_SIGNATURE) := common/image-sig.o |
61 | 63 | |
62 | 64 | # The following files are synced with upstream DTC. |
... | ... | @@ -118,6 +120,7 @@ |
118 | 120 | mkimage-objs := $(dumpimage-mkimage-objs) mkimage.o |
119 | 121 | fit_info-objs := $(dumpimage-mkimage-objs) fit_info.o |
120 | 122 | fit_check_sign-objs := $(dumpimage-mkimage-objs) fit_check_sign.o |
123 | +file2include-objs := file2include.o | |
121 | 124 | |
122 | 125 | ifneq ($(CONFIG_MX23)$(CONFIG_MX28),) |
123 | 126 | # Add CONFIG_MXS into host CFLAGS, so we can check whether or not register |
tools/file2include.c
1 | +/* | |
2 | + * Convert a file image to a C define | |
3 | + * | |
4 | + * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> | |
5 | + * | |
6 | + * SPDX-License-Identifier: GPL-2.0+ | |
7 | + * | |
8 | + * For testing EFI disk management we need an in memory image of | |
9 | + * a disk. | |
10 | + * | |
11 | + * The tool file2include converts a file to a C include. The file | |
12 | + * is separated into strings of 8 bytes. Only the non-zero strings | |
13 | + * are written to the include. The output format has been designed | |
14 | + * to maintain readability. | |
15 | + * | |
16 | + * As the disk image needed for testing contains mostly zeroes a high | |
17 | + * compression ratio can be attained. | |
18 | + */ | |
19 | +#include <stdio.h> | |
20 | +#include <stdlib.h> | |
21 | +#include <stdint.h> | |
22 | +#include <malloc.h> | |
23 | + | |
24 | +/* Size of the blocks written to the compressed file */ | |
25 | +#define BLOCK_SIZE 8 | |
26 | + | |
27 | +int main(int argc, char *argv[]) | |
28 | +{ | |
29 | + FILE *file; | |
30 | + int ret; | |
31 | + unsigned char *buf; | |
32 | + size_t count, i, j; | |
33 | + | |
34 | + /* Provide usage help */ | |
35 | + if (argc != 2) { | |
36 | + printf("Usage:\n%s FILENAME\n", argv[0]); | |
37 | + return EXIT_FAILURE; | |
38 | + } | |
39 | + /* Open file */ | |
40 | + file = fopen(argv[1], "r"); | |
41 | + if (!file) { | |
42 | + perror("fopen"); | |
43 | + return EXIT_FAILURE; | |
44 | + } | |
45 | + /* Get file length */ | |
46 | + ret = fseek(file, 0, SEEK_END); | |
47 | + if (ret < 0) { | |
48 | + perror("fseek"); | |
49 | + return EXIT_FAILURE; | |
50 | + } | |
51 | + count = ftell(file); | |
52 | + if (!count) { | |
53 | + fprintf(stderr, "File %s has length 0\n", argv[1]); | |
54 | + return EXIT_FAILURE; | |
55 | + } | |
56 | + rewind(file); | |
57 | + /* Read file */ | |
58 | + buf = malloc(count); | |
59 | + if (!buf) { | |
60 | + perror("calloc"); | |
61 | + return EXIT_FAILURE; | |
62 | + } | |
63 | + count = fread(buf, 1, count, file); | |
64 | + | |
65 | + /* Generate output */ | |
66 | + printf("/*\n"); | |
67 | + printf(" * Non-zero %u byte strings of a disk image\n", BLOCK_SIZE); | |
68 | + printf(" *\n"); | |
69 | + printf(" * Generated with tools/file2include\n"); | |
70 | + printf(" *\n"); | |
71 | + printf(" * SPDX-License-Identifier: GPL-2.0+\n"); | |
72 | + printf(" */\n\n"); | |
73 | + printf("#define EFI_ST_DISK_IMG { 0x%08zx, { \\\n", count); | |
74 | + | |
75 | + for (i = 0; i < count; i += BLOCK_SIZE) { | |
76 | + int c = 0; | |
77 | + | |
78 | + for (j = i; j < i + BLOCK_SIZE && j < count; ++j) { | |
79 | + if (buf[j]) | |
80 | + c = 1; | |
81 | + } | |
82 | + if (!c) | |
83 | + continue; | |
84 | + printf("\t{0x%08zx, \"", i); | |
85 | + for (j = i; j < i + BLOCK_SIZE && j < count; ++j) | |
86 | + printf("\\x%02x", buf[j]); | |
87 | + printf("\"}, /* "); | |
88 | + for (j = i; j < i + BLOCK_SIZE && j < count; ++j) { | |
89 | + if (buf[j] >= 0x20 && buf[j] <= 0x7e) | |
90 | + printf("%c", buf[j]); | |
91 | + else | |
92 | + printf("."); | |
93 | + } | |
94 | + printf(" */ \\\n"); | |
95 | + } | |
96 | + printf("\t{0, NULL} } }\n"); | |
97 | + | |
98 | + /* Release resources */ | |
99 | + free(buf); | |
100 | + ret = fclose(file); | |
101 | + if (ret) { | |
102 | + perror("fclose"); | |
103 | + return EXIT_FAILURE; | |
104 | + } | |
105 | + return EXIT_SUCCESS; | |
106 | +} |