2015-02-26 20:47:19 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
2015-03-18 18:13:48 +01:00
|
|
|
#include "cbfs_sections.h"
|
2015-02-26 20:47:19 +01:00
|
|
|
#include "fmap_from_fmd.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2015-03-18 18:13:48 +01:00
|
|
|
#include <unistd.h>
|
2015-02-26 20:47:19 +01:00
|
|
|
|
|
|
|
#define STDIN_FILENAME_SENTINEL "-"
|
|
|
|
|
2015-03-18 18:13:48 +01:00
|
|
|
#define HEADER_FMAP_OFFSET "FMAP_OFFSET"
|
|
|
|
|
2015-02-26 20:47:19 +01:00
|
|
|
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,
|
2015-03-18 18:13:48 +01:00
|
|
|
FMAPTOOL_EXIT_MISSING_FMAP_SECTION,
|
|
|
|
FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS,
|
2015-02-26 20:47:19 +01:00
|
|
|
FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION,
|
|
|
|
FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE,
|
2015-03-18 18:13:48 +01:00
|
|
|
FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT,
|
|
|
|
FMAPTOOL_EXIT_FAILED_WRITING_HEADER,
|
2015-02-26 20:47:19 +01:00
|
|
|
};
|
|
|
|
|
2015-03-18 18:13:48 +01:00
|
|
|
static void usage(const char *invoked_as)
|
2015-02-26 20:47:19 +01:00
|
|
|
{
|
2015-03-18 18:13:48 +01:00
|
|
|
fputs("fmaptool: Compiler for fmd (flashmap descriptor) files\n",
|
|
|
|
stderr);
|
|
|
|
fputs("\nUSAGE:\n", stderr);
|
|
|
|
fprintf(stderr,
|
2016-01-20 15:29:30 +01:00
|
|
|
"\t%s [-h <header output file>] [-R <region output file>] <fmd input file> <binary output file>\n",
|
2015-03-18 18:13:48 +01:00
|
|
|
invoked_as);
|
|
|
|
fputs("\nMANDATORY ARGUMENTS:\n", stderr);
|
|
|
|
fprintf(stderr,
|
|
|
|
"<fmd input file> may be '%s' to read from standard input\n",
|
|
|
|
STDIN_FILENAME_SENTINEL);
|
|
|
|
fputs("<binary output file> must be a regular file\n", stderr);
|
|
|
|
fputs("\nOPTIONAL SWITCHES:\n", stderr);
|
|
|
|
fprintf(stderr,
|
|
|
|
"-h\tAlso produce a C header defining %s to the FMAP section's flash offset.\n",
|
|
|
|
HEADER_FMAP_OFFSET);
|
2016-01-20 15:29:30 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"-R\tAlso produce a text file listing the CBFS regions, comma separated.\n");
|
2015-03-18 18:13:48 +01:00
|
|
|
fputs("\nOUTPUT:\n", stderr);
|
|
|
|
fputs("A successful invocation prints a summary of work done to standard error, and a comma-separated list\n",
|
|
|
|
stderr);
|
|
|
|
fputs("of those sections that contain CBFSes, starting with the primary such section, to standard output.\n",
|
|
|
|
stderr);
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:29:30 +01:00
|
|
|
static void list_cbfs_section_names(FILE *out)
|
2015-03-18 18:13:48 +01:00
|
|
|
{
|
|
|
|
cbfs_section_iterator_t cbfs_it = cbfs_sections_iterator();
|
|
|
|
assert(cbfs_it);
|
|
|
|
|
|
|
|
bool subsequent = false;
|
|
|
|
while (cbfs_it) {
|
|
|
|
const char *cur_name =
|
|
|
|
cbfs_sections_iterator_deref(cbfs_it)->name;
|
|
|
|
if (cbfs_sections_iterator_advance(&cbfs_it) && subsequent)
|
2016-01-20 15:29:30 +01:00
|
|
|
fputc(',', out);
|
|
|
|
fputs(cur_name, out);
|
2015-03-18 18:13:48 +01:00
|
|
|
subsequent = true;
|
|
|
|
}
|
2016-01-20 15:29:30 +01:00
|
|
|
fputc('\n', out);
|
2015-03-18 18:13:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool write_header(const char *out_fname,
|
|
|
|
const struct flashmap_descriptor *root)
|
|
|
|
{
|
|
|
|
assert(out_fname);
|
|
|
|
|
|
|
|
FILE *header = fopen(out_fname, "w");
|
|
|
|
if (!header) {
|
|
|
|
fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
|
|
|
|
out_fname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned fmap_offset =
|
|
|
|
fmd_calc_absolute_offset(root, SECTION_NAME_FMAP);
|
|
|
|
assert(fmap_offset != FMD_NOTFOUND);
|
|
|
|
|
|
|
|
fputs("#ifndef FMAPTOOL_GENERATED_HEADER_H_\n", header);
|
|
|
|
fputs("#define FMAPTOOL_GENERATED_HEADER_H_\n\n", header);
|
|
|
|
fprintf(header, "#define %s %#x\n\n", HEADER_FMAP_OFFSET, fmap_offset);
|
|
|
|
fputs("#endif\n", header);
|
|
|
|
|
|
|
|
fclose(header);
|
2015-02-26 20:47:19 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-18 18:13:48 +01:00
|
|
|
static void full_fmd_cleanup(struct flashmap_descriptor **victim)
|
|
|
|
{
|
|
|
|
assert(victim);
|
|
|
|
|
|
|
|
cbfs_sections_cleanup();
|
|
|
|
fmd_cleanup(*victim);
|
|
|
|
*victim = NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-26 20:47:19 +01:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2015-03-18 18:13:48 +01:00
|
|
|
struct {
|
|
|
|
// Mandatory
|
|
|
|
const char *fmd_filename;
|
|
|
|
const char *fmap_filename;
|
|
|
|
|
|
|
|
// Optional
|
|
|
|
const char *header_filename;
|
2016-01-20 15:29:30 +01:00
|
|
|
const char *region_filename;
|
2016-01-25 14:15:43 +01:00
|
|
|
} args = {NULL, NULL, NULL, NULL};
|
2015-03-18 18:13:48 +01:00
|
|
|
|
|
|
|
bool show_usage = false;
|
|
|
|
int each_arg;
|
2016-01-20 15:29:30 +01:00
|
|
|
while (!show_usage && (each_arg = getopt(argc, argv, ":h:R:")) != -1) {
|
2015-03-18 18:13:48 +01:00
|
|
|
switch (each_arg) {
|
|
|
|
case 'h':
|
|
|
|
args.header_filename = optarg;
|
|
|
|
break;
|
2016-01-20 15:29:30 +01:00
|
|
|
case 'R':
|
|
|
|
args.region_filename = optarg;
|
|
|
|
break;
|
2015-03-18 18:13:48 +01:00
|
|
|
case ':':
|
|
|
|
fprintf(stderr, "-%c: Expected an accompanying value\n",
|
|
|
|
optopt);
|
|
|
|
show_usage = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "-%c: Unexpected command-line switch\n",
|
|
|
|
optopt);
|
|
|
|
show_usage = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (show_usage || argc - optind != 2) {
|
|
|
|
usage(argv[0]);
|
2015-02-26 20:47:19 +01:00
|
|
|
return FMAPTOOL_EXIT_BAD_ARGS;
|
|
|
|
}
|
2015-03-18 18:13:48 +01:00
|
|
|
args.fmd_filename = argv[optind];
|
|
|
|
args.fmap_filename = argv[optind + 1];
|
2015-02-26 20:47:19 +01:00
|
|
|
|
|
|
|
FILE *fmd_file = stdin;
|
2015-03-18 18:13:48 +01:00
|
|
|
if (strcmp(args.fmd_filename, STDIN_FILENAME_SENTINEL) != 0) {
|
|
|
|
fmd_file = fopen(args.fmd_filename, "r");
|
2015-02-26 20:47:19 +01:00
|
|
|
if (!fmd_file) {
|
|
|
|
fprintf(stderr, "FATAL: Unable to open file '%s'\n",
|
2015-03-18 18:13:48 +01:00
|
|
|
args.fmd_filename);
|
2015-02-26 20:47:19 +01:00
|
|
|
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);
|
2015-03-18 18:13:48 +01:00
|
|
|
full_fmd_cleanup(&descriptor);
|
2015-02-26 20:47:19 +01:00
|
|
|
return FMAPTOOL_EXIT_FAILED_DESCRIPTOR;
|
|
|
|
}
|
|
|
|
|
2015-03-18 18:13:48 +01:00
|
|
|
if (!fmd_find_node(descriptor, SECTION_NAME_FMAP)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"FATAL: Flashmap descriptor must have an '%s' section\n",
|
|
|
|
SECTION_NAME_FMAP);
|
|
|
|
full_fmd_cleanup(&descriptor);
|
|
|
|
return FMAPTOOL_EXIT_MISSING_FMAP_SECTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cbfs_sections_primary_cbfs_accounted_for()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"FATAL: Flashmap descriptor must have a '%s' section that is annotated with '(%s)'\n",
|
|
|
|
SECTION_NAME_PRIMARY_CBFS,
|
|
|
|
SECTION_ANNOTATION_CBFS);
|
|
|
|
full_fmd_cleanup(&descriptor);
|
|
|
|
return FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS;
|
|
|
|
}
|
|
|
|
|
2015-02-26 20:47:19 +01:00
|
|
|
struct fmap *flashmap = fmap_from_fmd(descriptor);
|
|
|
|
if (!flashmap) {
|
|
|
|
fputs("FATAL: Failed while constructing FMAP section\n",
|
|
|
|
stderr);
|
2015-03-18 18:13:48 +01:00
|
|
|
full_fmd_cleanup(&descriptor);
|
2015-02-26 20:47:19 +01:00
|
|
|
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);
|
2015-03-18 18:13:48 +01:00
|
|
|
full_fmd_cleanup(&descriptor);
|
2015-02-26 20:47:19 +01:00
|
|
|
return FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE;
|
|
|
|
}
|
|
|
|
|
2015-03-18 18:13:48 +01:00
|
|
|
FILE *fmap_file = fopen(args.fmap_filename, "wb");
|
2015-02-26 20:47:19 +01:00
|
|
|
if (!fmap_file) {
|
|
|
|
fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
|
2015-03-18 18:13:48 +01:00
|
|
|
args.fmap_filename);
|
2015-02-26 20:47:19 +01:00
|
|
|
fmap_destroy(flashmap);
|
2015-03-18 18:13:48 +01:00
|
|
|
full_fmd_cleanup(&descriptor);
|
2015-02-26 20:47:19 +01:00
|
|
|
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);
|
2015-03-18 18:13:48 +01:00
|
|
|
full_fmd_cleanup(&descriptor);
|
|
|
|
return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT;
|
2015-02-26 20:47:19 +01:00
|
|
|
}
|
|
|
|
fclose(fmap_file);
|
|
|
|
fmap_destroy(flashmap);
|
2015-03-18 18:13:48 +01:00
|
|
|
|
|
|
|
if (args.header_filename &&
|
|
|
|
!write_header(args.header_filename, descriptor)) {
|
|
|
|
full_fmd_cleanup(&descriptor);
|
|
|
|
return FMAPTOOL_EXIT_FAILED_WRITING_HEADER;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "SUCCESS: Wrote %d bytes to file '%s'%s\n", size,
|
|
|
|
args.fmap_filename,
|
|
|
|
args.header_filename ? " (and generated header)" : "");
|
|
|
|
fputs("The sections containing CBFSes are: ", stderr);
|
2016-01-20 15:29:30 +01:00
|
|
|
list_cbfs_section_names(stdout);
|
|
|
|
if (args.region_filename) {
|
|
|
|
FILE *region_file = fopen(args.region_filename, "w");
|
|
|
|
if (region_file == NULL)
|
|
|
|
return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT;
|
|
|
|
|
|
|
|
list_cbfs_section_names(region_file);
|
|
|
|
fclose(region_file);
|
|
|
|
}
|
2015-03-18 18:13:48 +01:00
|
|
|
|
|
|
|
full_fmd_cleanup(&descriptor);
|
2015-02-26 20:47:19 +01:00
|
|
|
return FMAPTOOL_EXIT_SUCCESS;
|
|
|
|
}
|