From patchwork Wed Jan 17 19:16:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 862528 X-Patchwork-Delegate: agraf@suse.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 3zMHBc4DCKz9t5g for ; Thu, 18 Jan 2018 06:26:16 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 2A5EFC21E3C; Wed, 17 Jan 2018 19:22:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_MSPIKE_H2 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 6CD31C21E4B; Wed, 17 Jan 2018 19:16:52 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 09E52C21EEB; Wed, 17 Jan 2018 19:16:30 +0000 (UTC) Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) by lists.denx.de (Postfix) with ESMTPS id E21B5C21DFA for ; Wed, 17 Jan 2018 19:16:25 +0000 (UTC) Received: from workstation4.fritz.box ([95.222.46.37]) by mail.gmx.com (mrgmx002 [212.227.17.184]) with ESMTPSA (Nemesis) id 0MKZLb-1eZyBj1Bix-001wzm; Wed, 17 Jan 2018 20:16:24 +0100 From: Heinrich Schuchardt To: Alexander Graf Date: Wed, 17 Jan 2018 20:16:09 +0100 Message-Id: <20180117191612.17108-16-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20180117191612.17108-1-xypron.glpk@gmx.de> References: <20180117191612.17108-1-xypron.glpk@gmx.de> X-Provags-ID: V03:K0:uKUPysALefkKQ7iTWJew0Kb5pJyn/Pno2po1wMpXbAujA38g3pd GdQhWgJqnPFoS3IgCWVS2nkRzRKXgwUi+yhtwGOd90oeHdMVSuv0xhdN03YL4sih2MDdnrn 06dCh6KmJnoo8MCTpVjZpPrWElLi9T7dwGBovp3FYp8HZnObWDF8MtlChLkZVMWDLKr3LHs X0PkeSNM2gu7Ov5OYsCJw== X-UI-Out-Filterresults: notjunk:1; V01:K0:6gn/SacEJuA=:C5k8It1JrL0JarywhvS2PH cEmadLw5U9/IH30EWoRYPwX3zRwaf5jyhF1en46uYPSHj9jzmp6IASq+F5VdiMfuUFkEjjYo7 +DRt6lpdTpljlTD/ADmGk/XXmTbypLWSrISrEmc4W1oijtGZdwOtWGuLfJauYpF4njgfcCuKi ibsr0eFmtiQww5dmxoiHL0clw2FjWHlZMCy7/ft4fg07FNhf3kE2DTZOBxbIDYyJjtcHkWoKx ZQucF5Yu94pm+DJh4df9OnRfwPjr9V5D9/XtMCT3AcVDJ+bY0/Ok2GhptAckTb9Jy9soJg771 5FXk6qOrpYhIdJY1TRP6AbhVrfITCelBmje7JYxk7/KJ9sUJS4zwDdHWpSS2XuwEvTDZvK3Lp Wl0G08EFOoIpvsi98wx5WhCtsXaK+TmEnn62JLwySkDFKJO4xoh/VsDPF8kEAwgSxX9OrbdxP u876n09Rad7/M2NXlHDRLO4N6f4C38eL7zXLPO5qqlQOAG5CfPlAte7JT8WWD50bs0fVl94vv Yj9tdCt+1OHQLR+M1/WyBiq5iha9LgnD7EE3KsTid8jSCaHClLIVq4mCVXUlaqvtvVuEZu50z AFYiSc32fbuDGBh2cgedjqBYyqT2MyQTChuo4CaI/y56fDLofPqyYlTgZNuEuUdImrFnVSg0r aiRRd3ZBDeJGCOJbL1yoaywNNQUDO8I/x26nnCtNaH1QHvcm1FWo7MLI5ht1E9dwq4JXH8KKS /Wng/2D7Se+a3CPZDnCZ55qWTTWczks6ep3JQmfFHczQQAmw8sLtRmLuVDlvaYOGYkH8ZX0I5 MhicH4HrccfpciWMu54hHb4PG7gUQ== Cc: Heinrich Schuchardt , Tom Rini , u-boot@lists.denx.de Subject: [U-Boot] [PATCH v2 15/18] tools: provide a tool to convert a binary file to an include X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" 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 --- v2 Add .gitignore entry for tools/file2include --- MAINTAINERS | 1 + tools/.gitignore | 1 + tools/Makefile | 3 ++ tools/file2include.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 tools/file2include.c diff --git a/MAINTAINERS b/MAINTAINERS index e399008e23..d459153503 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -290,6 +290,7 @@ F: include/efi* F: lib/efi*/ F: test/py/tests/test_efi* F: cmd/bootefi.c +F: tools/file2include.c FLATTENED DEVICE TREE M: Simon Glass diff --git a/tools/.gitignore b/tools/.gitignore index 6a487d2202..c8cdaef90c 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -6,6 +6,7 @@ /easylogo/easylogo /envcrc /fdtgrep +/file2include /fit_check_sign /fit_info /gdb/gdbcont diff --git a/tools/Makefile b/tools/Makefile index 571f571ec9..b7d7d418ee 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -57,6 +57,8 @@ mkenvimage-objs := mkenvimage.o os_support.o lib/crc32.o hostprogs-y += dumpimage mkimage hostprogs-$(CONFIG_FIT_SIGNATURE) += fit_info fit_check_sign +hostprogs-$(CONFIG_CMD_BOOTEFI_SELFTEST) += file2include + FIT_SIG_OBJS-$(CONFIG_FIT_SIGNATURE) := common/image-sig.o # The following files are synced with upstream DTC. @@ -118,6 +120,7 @@ dumpimage-objs := $(dumpimage-mkimage-objs) dumpimage.o mkimage-objs := $(dumpimage-mkimage-objs) mkimage.o fit_info-objs := $(dumpimage-mkimage-objs) fit_info.o fit_check_sign-objs := $(dumpimage-mkimage-objs) fit_check_sign.o +file2include-objs := file2include.o ifneq ($(CONFIG_MX23)$(CONFIG_MX28),) # Add CONFIG_MXS into host CFLAGS, so we can check whether or not register diff --git a/tools/file2include.c b/tools/file2include.c new file mode 100644 index 0000000000..9145f0845a --- /dev/null +++ b/tools/file2include.c @@ -0,0 +1,106 @@ +/* + * Convert a file image to a C define + * + * Copyright (c) 2017 Heinrich Schuchardt + * + * SPDX-License-Identifier: GPL-2.0+ + * + * 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. + * + * As the disk image needed for testing contains mostly zeroes a high + * compression ratio can be attained. + */ +#include +#include +#include +#include + +/* Size of the blocks written to the compressed file */ +#define BLOCK_SIZE 8 + +int main(int argc, char *argv[]) +{ + FILE *file; + int ret; + unsigned char *buf; + size_t count, i, j; + + /* Provide usage help */ + if (argc != 2) { + printf("Usage:\n%s FILENAME\n", argv[0]); + return EXIT_FAILURE; + } + /* Open file */ + file = fopen(argv[1], "r"); + if (!file) { + perror("fopen"); + return EXIT_FAILURE; + } + /* Get file length */ + ret = fseek(file, 0, SEEK_END); + if (ret < 0) { + perror("fseek"); + return EXIT_FAILURE; + } + count = ftell(file); + if (!count) { + fprintf(stderr, "File %s has length 0\n", argv[1]); + return EXIT_FAILURE; + } + rewind(file); + /* Read file */ + buf = malloc(count); + if (!buf) { + perror("calloc"); + return EXIT_FAILURE; + } + count = fread(buf, 1, count, file); + + /* Generate output */ + printf("/*\n"); + printf(" * Non-zero %u byte strings of a disk image\n", BLOCK_SIZE); + printf(" *\n"); + printf(" * Generated with tools/file2include\n"); + printf(" *\n"); + printf(" * SPDX-License-Identifier: GPL-2.0+\n"); + printf(" */\n\n"); + printf("#define EFI_ST_DISK_IMG { 0x%08zx, { \\\n", count); + + for (i = 0; i < count; i += BLOCK_SIZE) { + int c = 0; + + for (j = i; j < i + BLOCK_SIZE && j < count; ++j) { + if (buf[j]) + c = 1; + } + if (!c) + continue; + printf("\t{0x%08zx, \"", i); + for (j = i; j < i + BLOCK_SIZE && j < count; ++j) + printf("\\x%02x", buf[j]); + printf("\"}, /* "); + for (j = i; j < i + BLOCK_SIZE && j < count; ++j) { + if (buf[j] >= 0x20 && buf[j] <= 0x7e) + printf("%c", buf[j]); + else + printf("."); + } + printf(" */ \\\n"); + } + printf("\t{0, NULL} } }\n"); + + /* Release resources */ + free(buf); + ret = fclose(file); + if (ret) { + perror("fclose"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +}