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