From 69b88bf1276d2cb0309e2fc96df9d33a893138e3 Mon Sep 17 00:00:00 2001 From: Sol Boucher Date: Thu, 26 Feb 2015 11:47:19 -0800 Subject: fmaptool: Introduce the fmd ("flashmap descriptor") language and compiler This adds a compiler for a language whose textual representation of flashmap regions will be used to describe the layout of flash chips that contain more than just a single CBFS. Direct integration with cbfstool (via a new command-line switch for the create action) is forthcoming but will be added separately. BUG=chromium:461875 TEST=Use Chromium OS's cros_bundle_firmware script on the fmap.dts file for panther. Using the latter file as a reference, write a corresponding fmap.fmd file and feed it through fmaptool. Run both binary output files though the flashmap project's own flashmap_decode utility. Observe only the expected differences. BRANCH=None Change-Id: I06b32d138dbef0a4e5ed43c81bd31c796fd5d669 Signed-off-by: Sol Boucher Original-Commit-Id: 005ab67eb594e21489cf31036aedaea87e0c7142 Original-Change-Id: Ia08f28688efdbbfc70c255916b8eb7eb0eb07fb2 Original-Signed-off-by: Sol Boucher Original-Reviewed-on: https://chromium-review.googlesource.com/255031 Original-Reviewed-by: Julius Werner Original-Reviewed-by: Stefan Reinauer Reviewed-on: http://review.coreboot.org/9942 Tested-by: build bot (Jenkins) Reviewed-by: Patrick Georgi --- util/cbfstool/fmaptool.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 util/cbfstool/fmaptool.c (limited to 'util/cbfstool/fmaptool.c') diff --git a/util/cbfstool/fmaptool.c b/util/cbfstool/fmaptool.c new file mode 100644 index 0000000000..961e9dc918 --- /dev/null +++ b/util/cbfstool/fmaptool.c @@ -0,0 +1,119 @@ +/* + * fmaptool, CLI utility for converting plaintext fmd files into fmap blobs + * + * Copyright (C) 2015 Google, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA + */ + +#include "common.h" +#include "fmap_from_fmd.h" + +#include +#include + +#define STDIN_FILENAME_SENTINEL "-" + +enum fmaptool_return { + FMAPTOOL_EXIT_SUCCESS = 0, + FMAPTOOL_EXIT_BAD_ARGS, + FMAPTOOL_EXIT_BAD_INPUT_PATH, + FMAPTOOL_EXIT_BAD_OUTPUT_PATH, + FMAPTOOL_EXIT_FAILED_DESCRIPTOR, + FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION, + FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE, + FMAPTOOL_EXIT_FAILED_WRITING_FILE, +}; + +bool fmd_process_annotation_impl(unused const struct flashmap_descriptor *node, + unused const char *annotation) +{ + // We always accept annotations, but never act on them. + return true; +} + +int main(int argc, char **argv) +{ + if (argc != 3) { + fputs("Convert a human-readable flashmap descriptor (fmd) file into the binary FMAP format for use in firmware images\n", + stderr); + fprintf(stderr, + "USAGE: %s \n", + argv[0]); + fprintf(stderr, + "To read from standard input, provide '%s' as the input filename.\n", + STDIN_FILENAME_SENTINEL); + return FMAPTOOL_EXIT_BAD_ARGS; + } + const char *fmd_filename = argv[1]; + const char *fmap_filename = argv[2]; + + FILE *fmd_file = stdin; + if (strcmp(fmd_filename, STDIN_FILENAME_SENTINEL) != 0) { + fmd_file = fopen(fmd_filename, "r"); + if (!fmd_file) { + fprintf(stderr, "FATAL: Unable to open file '%s'\n", + fmd_filename); + return FMAPTOOL_EXIT_BAD_INPUT_PATH; + } + } + + struct flashmap_descriptor *descriptor = fmd_create(fmd_file); + fclose(fmd_file); + if (!descriptor) { + fputs("FATAL: Failed while processing provided descriptor\n", + stderr); + return FMAPTOOL_EXIT_FAILED_DESCRIPTOR; + } + + struct fmap *flashmap = fmap_from_fmd(descriptor); + if (!flashmap) { + fputs("FATAL: Failed while constructing FMAP section\n", + stderr); + fmd_cleanup(descriptor); + return FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION; + } + + int size = fmap_size(flashmap); + if (size < 0) { + fputs("FATAL: Failed to determine FMAP section size\n", + stderr); + fmap_destroy(flashmap); + fmd_cleanup(descriptor); + return FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE; + } + + FILE *fmap_file = fopen(fmap_filename, "wb"); + if (!fmap_file) { + fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n", + fmap_filename); + fmap_destroy(flashmap); + fmd_cleanup(descriptor); + return FMAPTOOL_EXIT_BAD_OUTPUT_PATH; + } + + if (!fwrite(flashmap, size, 1, fmap_file)) { + fputs("FATAL: Failed to write final FMAP to file\n", stderr); + fclose(fmap_file); + fmap_destroy(flashmap); + fmd_cleanup(descriptor); + return FMAPTOOL_EXIT_FAILED_WRITING_FILE; + } + fclose(fmap_file); + printf("SUCCESS: Wrote %d bytes to file '%s'\n", size, fmap_filename); + + fmap_destroy(flashmap); + fmd_cleanup(descriptor); + return FMAPTOOL_EXIT_SUCCESS; +} -- cgit v1.2.3