Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * fmaptool, CLI utility for converting plaintext fmd files into fmap blobs |
| 3 | * |
| 4 | * Copyright (C) 2015 Google, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "common.h" |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 17 | #include "cbfs_sections.h" |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 18 | #include "fmap_from_fmd.h" |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 23 | |
| 24 | #define STDIN_FILENAME_SENTINEL "-" |
| 25 | |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 26 | #define HEADER_FMAP_OFFSET "FMAP_OFFSET" |
| 27 | |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 28 | enum fmaptool_return { |
| 29 | FMAPTOOL_EXIT_SUCCESS = 0, |
| 30 | FMAPTOOL_EXIT_BAD_ARGS, |
| 31 | FMAPTOOL_EXIT_BAD_INPUT_PATH, |
| 32 | FMAPTOOL_EXIT_BAD_OUTPUT_PATH, |
| 33 | FMAPTOOL_EXIT_FAILED_DESCRIPTOR, |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 34 | FMAPTOOL_EXIT_MISSING_FMAP_SECTION, |
| 35 | FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS, |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 36 | FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION, |
| 37 | FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE, |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 38 | FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT, |
| 39 | FMAPTOOL_EXIT_FAILED_WRITING_HEADER, |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 42 | static void usage(const char *invoked_as) |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 43 | { |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 44 | fputs("fmaptool: Compiler for fmd (flashmap descriptor) files\n", |
| 45 | stderr); |
| 46 | fputs("\nUSAGE:\n", stderr); |
| 47 | fprintf(stderr, |
Patrick Georgi | c3771b0 | 2016-01-20 15:29:30 +0100 | [diff] [blame] | 48 | "\t%s [-h <header output file>] [-R <region output file>] <fmd input file> <binary output file>\n", |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 49 | invoked_as); |
| 50 | fputs("\nMANDATORY ARGUMENTS:\n", stderr); |
| 51 | fprintf(stderr, |
| 52 | "<fmd input file> may be '%s' to read from standard input\n", |
| 53 | STDIN_FILENAME_SENTINEL); |
| 54 | fputs("<binary output file> must be a regular file\n", stderr); |
| 55 | fputs("\nOPTIONAL SWITCHES:\n", stderr); |
| 56 | fprintf(stderr, |
| 57 | "-h\tAlso produce a C header defining %s to the FMAP section's flash offset.\n", |
| 58 | HEADER_FMAP_OFFSET); |
Patrick Georgi | c3771b0 | 2016-01-20 15:29:30 +0100 | [diff] [blame] | 59 | fprintf(stderr, |
| 60 | "-R\tAlso produce a text file listing the CBFS regions, comma separated.\n"); |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 61 | fputs("\nOUTPUT:\n", stderr); |
| 62 | fputs("A successful invocation prints a summary of work done to standard error, and a comma-separated list\n", |
| 63 | stderr); |
| 64 | fputs("of those sections that contain CBFSes, starting with the primary such section, to standard output.\n", |
| 65 | stderr); |
| 66 | } |
| 67 | |
Patrick Georgi | c3771b0 | 2016-01-20 15:29:30 +0100 | [diff] [blame] | 68 | static void list_cbfs_section_names(FILE *out) |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 69 | { |
| 70 | cbfs_section_iterator_t cbfs_it = cbfs_sections_iterator(); |
| 71 | assert(cbfs_it); |
| 72 | |
| 73 | bool subsequent = false; |
| 74 | while (cbfs_it) { |
| 75 | const char *cur_name = |
| 76 | cbfs_sections_iterator_deref(cbfs_it)->name; |
| 77 | if (cbfs_sections_iterator_advance(&cbfs_it) && subsequent) |
Patrick Georgi | c3771b0 | 2016-01-20 15:29:30 +0100 | [diff] [blame] | 78 | fputc(',', out); |
| 79 | fputs(cur_name, out); |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 80 | subsequent = true; |
| 81 | } |
Patrick Georgi | c3771b0 | 2016-01-20 15:29:30 +0100 | [diff] [blame] | 82 | fputc('\n', out); |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static bool write_header(const char *out_fname, |
| 86 | const struct flashmap_descriptor *root) |
| 87 | { |
| 88 | assert(out_fname); |
| 89 | |
| 90 | FILE *header = fopen(out_fname, "w"); |
| 91 | if (!header) { |
| 92 | fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n", |
| 93 | out_fname); |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | unsigned fmap_offset = |
| 98 | fmd_calc_absolute_offset(root, SECTION_NAME_FMAP); |
| 99 | assert(fmap_offset != FMD_NOTFOUND); |
| 100 | |
| 101 | fputs("#ifndef FMAPTOOL_GENERATED_HEADER_H_\n", header); |
| 102 | fputs("#define FMAPTOOL_GENERATED_HEADER_H_\n\n", header); |
| 103 | fprintf(header, "#define %s %#x\n\n", HEADER_FMAP_OFFSET, fmap_offset); |
Patrick Georgi | 919be61 | 2016-05-02 17:02:53 +0800 | [diff] [blame] | 104 | |
| 105 | /* also add defines for each CBFS-carrying fmap region: base and size */ |
| 106 | cbfs_section_iterator_t cbfs_it = cbfs_sections_iterator(); |
| 107 | while (cbfs_it) { |
| 108 | const struct flashmap_descriptor *item = |
| 109 | cbfs_sections_iterator_deref(cbfs_it); |
| 110 | assert(item->offset_known && item->size_known); |
Julius Werner | 119dcee | 2016-06-20 13:21:36 -0700 | [diff] [blame] | 111 | unsigned abs_base = fmd_calc_absolute_offset(root, item->name); |
Patrick Georgi | 919be61 | 2016-05-02 17:02:53 +0800 | [diff] [blame] | 112 | fprintf(header, "#define ___FMAP__%s_BASE 0x%x\n", |
Julius Werner | 119dcee | 2016-06-20 13:21:36 -0700 | [diff] [blame] | 113 | item->name, abs_base); |
Patrick Georgi | 919be61 | 2016-05-02 17:02:53 +0800 | [diff] [blame] | 114 | fprintf(header, "#define ___FMAP__%s_SIZE 0x%x\n", |
| 115 | item->name, item->size); |
| 116 | cbfs_sections_iterator_advance(&cbfs_it); |
| 117 | } |
| 118 | |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 119 | fputs("#endif\n", header); |
| 120 | |
| 121 | fclose(header); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 125 | static void full_fmd_cleanup(struct flashmap_descriptor **victim) |
| 126 | { |
| 127 | assert(victim); |
| 128 | |
| 129 | cbfs_sections_cleanup(); |
| 130 | fmd_cleanup(*victim); |
| 131 | *victim = NULL; |
| 132 | } |
| 133 | |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 134 | int main(int argc, char **argv) |
| 135 | { |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 136 | struct { |
| 137 | // Mandatory |
| 138 | const char *fmd_filename; |
| 139 | const char *fmap_filename; |
| 140 | |
| 141 | // Optional |
| 142 | const char *header_filename; |
Patrick Georgi | c3771b0 | 2016-01-20 15:29:30 +0100 | [diff] [blame] | 143 | const char *region_filename; |
Werner Zeh | 998671c | 2016-01-25 14:15:43 +0100 | [diff] [blame] | 144 | } args = {NULL, NULL, NULL, NULL}; |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 145 | |
| 146 | bool show_usage = false; |
| 147 | int each_arg; |
Patrick Georgi | c3771b0 | 2016-01-20 15:29:30 +0100 | [diff] [blame] | 148 | while (!show_usage && (each_arg = getopt(argc, argv, ":h:R:")) != -1) { |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 149 | switch (each_arg) { |
| 150 | case 'h': |
| 151 | args.header_filename = optarg; |
| 152 | break; |
Patrick Georgi | c3771b0 | 2016-01-20 15:29:30 +0100 | [diff] [blame] | 153 | case 'R': |
| 154 | args.region_filename = optarg; |
| 155 | break; |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 156 | case ':': |
| 157 | fprintf(stderr, "-%c: Expected an accompanying value\n", |
| 158 | optopt); |
| 159 | show_usage = true; |
| 160 | break; |
| 161 | default: |
| 162 | fprintf(stderr, "-%c: Unexpected command-line switch\n", |
| 163 | optopt); |
| 164 | show_usage = true; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (show_usage || argc - optind != 2) { |
| 169 | usage(argv[0]); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 170 | return FMAPTOOL_EXIT_BAD_ARGS; |
| 171 | } |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 172 | args.fmd_filename = argv[optind]; |
| 173 | args.fmap_filename = argv[optind + 1]; |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 174 | |
| 175 | FILE *fmd_file = stdin; |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 176 | if (strcmp(args.fmd_filename, STDIN_FILENAME_SENTINEL) != 0) { |
| 177 | fmd_file = fopen(args.fmd_filename, "r"); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 178 | if (!fmd_file) { |
| 179 | fprintf(stderr, "FATAL: Unable to open file '%s'\n", |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 180 | args.fmd_filename); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 181 | return FMAPTOOL_EXIT_BAD_INPUT_PATH; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | struct flashmap_descriptor *descriptor = fmd_create(fmd_file); |
| 186 | fclose(fmd_file); |
| 187 | if (!descriptor) { |
| 188 | fputs("FATAL: Failed while processing provided descriptor\n", |
| 189 | stderr); |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 190 | full_fmd_cleanup(&descriptor); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 191 | return FMAPTOOL_EXIT_FAILED_DESCRIPTOR; |
| 192 | } |
| 193 | |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 194 | if (!fmd_find_node(descriptor, SECTION_NAME_FMAP)) { |
| 195 | fprintf(stderr, |
| 196 | "FATAL: Flashmap descriptor must have an '%s' section\n", |
| 197 | SECTION_NAME_FMAP); |
| 198 | full_fmd_cleanup(&descriptor); |
| 199 | return FMAPTOOL_EXIT_MISSING_FMAP_SECTION; |
| 200 | } |
| 201 | |
| 202 | if (!cbfs_sections_primary_cbfs_accounted_for()) { |
| 203 | fprintf(stderr, |
| 204 | "FATAL: Flashmap descriptor must have a '%s' section that is annotated with '(%s)'\n", |
| 205 | SECTION_NAME_PRIMARY_CBFS, |
| 206 | SECTION_ANNOTATION_CBFS); |
| 207 | full_fmd_cleanup(&descriptor); |
| 208 | return FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS; |
| 209 | } |
| 210 | |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 211 | struct fmap *flashmap = fmap_from_fmd(descriptor); |
| 212 | if (!flashmap) { |
| 213 | fputs("FATAL: Failed while constructing FMAP section\n", |
| 214 | stderr); |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 215 | full_fmd_cleanup(&descriptor); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 216 | return FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION; |
| 217 | } |
| 218 | |
| 219 | int size = fmap_size(flashmap); |
| 220 | if (size < 0) { |
| 221 | fputs("FATAL: Failed to determine FMAP section size\n", |
| 222 | stderr); |
| 223 | fmap_destroy(flashmap); |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 224 | full_fmd_cleanup(&descriptor); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 225 | return FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE; |
| 226 | } |
| 227 | |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 228 | FILE *fmap_file = fopen(args.fmap_filename, "wb"); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 229 | if (!fmap_file) { |
| 230 | fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n", |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 231 | args.fmap_filename); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 232 | fmap_destroy(flashmap); |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 233 | full_fmd_cleanup(&descriptor); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 234 | return FMAPTOOL_EXIT_BAD_OUTPUT_PATH; |
| 235 | } |
| 236 | |
| 237 | if (!fwrite(flashmap, size, 1, fmap_file)) { |
| 238 | fputs("FATAL: Failed to write final FMAP to file\n", stderr); |
| 239 | fclose(fmap_file); |
| 240 | fmap_destroy(flashmap); |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 241 | full_fmd_cleanup(&descriptor); |
| 242 | return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT; |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 243 | } |
| 244 | fclose(fmap_file); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 245 | fmap_destroy(flashmap); |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 246 | |
| 247 | if (args.header_filename && |
| 248 | !write_header(args.header_filename, descriptor)) { |
| 249 | full_fmd_cleanup(&descriptor); |
| 250 | return FMAPTOOL_EXIT_FAILED_WRITING_HEADER; |
| 251 | } |
| 252 | |
| 253 | fprintf(stderr, "SUCCESS: Wrote %d bytes to file '%s'%s\n", size, |
| 254 | args.fmap_filename, |
| 255 | args.header_filename ? " (and generated header)" : ""); |
| 256 | fputs("The sections containing CBFSes are: ", stderr); |
Patrick Georgi | c3771b0 | 2016-01-20 15:29:30 +0100 | [diff] [blame] | 257 | list_cbfs_section_names(stdout); |
| 258 | if (args.region_filename) { |
| 259 | FILE *region_file = fopen(args.region_filename, "w"); |
| 260 | if (region_file == NULL) |
| 261 | return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT; |
| 262 | |
| 263 | list_cbfs_section_names(region_file); |
| 264 | fclose(region_file); |
| 265 | } |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 266 | |
| 267 | full_fmd_cleanup(&descriptor); |
Sol Boucher | 69b88bf | 2015-02-26 11:47:19 -0800 | [diff] [blame] | 268 | return FMAPTOOL_EXIT_SUCCESS; |
| 269 | } |