1 // SPDX-License-Identifier: GPL-2.0+
3 * Convert a file image to a C define
5 * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de>
7 * For testing EFI disk management we need an in memory image of
10 * The tool file2include converts a file to a C include. The file
11 * is separated into strings of 8 bytes. Only the non-zero strings
12 * are written to the include. The output format has been designed
13 * to maintain readability.
15 * As the disk image needed for testing contains mostly zeroes a high
16 * compression ratio can be attained.
22 /* Size of the blocks written to the compressed file */
25 int main(int argc, char *argv[])
32 /* Provide usage help */
34 printf("Usage:\n%s FILENAME\n", argv[0]);
38 file = fopen(argv[1], "r");
44 ret = fseek(file, 0, SEEK_END);
51 fprintf(stderr, "File %s has length 0\n", argv[1]);
61 count = fread(buf, 1, count, file);
64 printf("/* SPDX-License-Identifier: GPL-2.0+ */\n");
66 printf(" * Non-zero %u byte strings of a disk image\n", BLOCK_SIZE);
68 printf(" * Generated with tools/file2include\n");
70 printf("#define EFI_ST_DISK_IMG { 0x%08zx, { \\\n", count);
72 for (i = 0; i < count; i += BLOCK_SIZE) {
75 for (j = i; j < i + BLOCK_SIZE && j < count; ++j) {
81 printf("\t{0x%08zx, \"", i);
82 for (j = i; j < i + BLOCK_SIZE && j < count; ++j)
83 printf("\\x%02x", buf[j]);
85 for (j = i; j < i + BLOCK_SIZE && j < count; ++j) {
86 if (buf[j] != '*' && buf[j] >= 0x20 && buf[j] <= 0x7e)
93 printf("\t{0, NULL} } }\n");
95 /* Release resources */