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