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.
23 /* Size of the blocks written to the compressed file */
26 int main(int argc, char *argv[])
33 /* Provide usage help */
35 printf("Usage:\n%s FILENAME\n", argv[0]);
39 file = fopen(argv[1], "r");
45 ret = fseek(file, 0, SEEK_END);
52 fprintf(stderr, "File %s has length 0\n", argv[1]);
62 count = fread(buf, 1, count, file);
65 printf("/* SPDX-License-Identifier: GPL-2.0+ */\n");
67 printf(" * Non-zero %u byte strings of a disk image\n", BLOCK_SIZE);
69 printf(" * Generated with tools/file2include\n");
71 printf("#define EFI_ST_DISK_IMG { 0x%08zx, { \\\n", count);
73 for (i = 0; i < count; i += BLOCK_SIZE) {
76 for (j = i; j < i + BLOCK_SIZE && j < count; ++j) {
82 printf("\t{0x%08zx, \"", i);
83 for (j = i; j < i + BLOCK_SIZE && j < count; ++j)
84 printf("\\x%02x", buf[j]);
86 for (j = i; j < i + BLOCK_SIZE && j < count; ++j) {
87 if (buf[j] != '*' && buf[j] >= 0x20 && buf[j] <= 0x7e)
94 printf("\t{0, NULL} } }\n");
96 /* Release resources */