blob: 09b68d23bfad1cb7adeeeee4e34dbac01f261a26 [file] [log] [blame]
Sol Boucher69b88bf2015-02-26 11:47:19 -08001/*
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
20#include "common.h"
Sol Boucher023e8292015-03-18 10:13:48 -070021#include "cbfs_sections.h"
Sol Boucher69b88bf2015-02-26 11:47:19 -080022#include "fmap_from_fmd.h"
23
24#include <stdio.h>
25#include <string.h>
Sol Boucher023e8292015-03-18 10:13:48 -070026#include <unistd.h>
Sol Boucher69b88bf2015-02-26 11:47:19 -080027
28#define STDIN_FILENAME_SENTINEL "-"
29
Sol Boucher023e8292015-03-18 10:13:48 -070030#define HEADER_FMAP_OFFSET "FMAP_OFFSET"
31
Sol Boucher69b88bf2015-02-26 11:47:19 -080032enum fmaptool_return {
33 FMAPTOOL_EXIT_SUCCESS = 0,
34 FMAPTOOL_EXIT_BAD_ARGS,
35 FMAPTOOL_EXIT_BAD_INPUT_PATH,
36 FMAPTOOL_EXIT_BAD_OUTPUT_PATH,
37 FMAPTOOL_EXIT_FAILED_DESCRIPTOR,
Sol Boucher023e8292015-03-18 10:13:48 -070038 FMAPTOOL_EXIT_MISSING_FMAP_SECTION,
39 FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS,
Sol Boucher69b88bf2015-02-26 11:47:19 -080040 FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION,
41 FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE,
Sol Boucher023e8292015-03-18 10:13:48 -070042 FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT,
43 FMAPTOOL_EXIT_FAILED_WRITING_HEADER,
Sol Boucher69b88bf2015-02-26 11:47:19 -080044};
45
Sol Boucher023e8292015-03-18 10:13:48 -070046static void usage(const char *invoked_as)
Sol Boucher69b88bf2015-02-26 11:47:19 -080047{
Sol Boucher023e8292015-03-18 10:13:48 -070048 fputs("fmaptool: Compiler for fmd (flashmap descriptor) files\n",
49 stderr);
50 fputs("\nUSAGE:\n", stderr);
51 fprintf(stderr,
52 "\t%s [-h <header output file>] <fmd input file> <binary output file>\n",
53 invoked_as);
54 fputs("\nMANDATORY ARGUMENTS:\n", stderr);
55 fprintf(stderr,
56 "<fmd input file> may be '%s' to read from standard input\n",
57 STDIN_FILENAME_SENTINEL);
58 fputs("<binary output file> must be a regular file\n", stderr);
59 fputs("\nOPTIONAL SWITCHES:\n", stderr);
60 fprintf(stderr,
61 "-h\tAlso produce a C header defining %s to the FMAP section's flash offset.\n",
62 HEADER_FMAP_OFFSET);
63 fputs("\nOUTPUT:\n", stderr);
64 fputs("A successful invocation prints a summary of work done to standard error, and a comma-separated list\n",
65 stderr);
66 fputs("of those sections that contain CBFSes, starting with the primary such section, to standard output.\n",
67 stderr);
68}
69
70static void list_cbfs_section_names(void)
71{
72 cbfs_section_iterator_t cbfs_it = cbfs_sections_iterator();
73 assert(cbfs_it);
74
75 bool subsequent = false;
76 while (cbfs_it) {
77 const char *cur_name =
78 cbfs_sections_iterator_deref(cbfs_it)->name;
79 if (cbfs_sections_iterator_advance(&cbfs_it) && subsequent)
80 putchar(',');
81 fputs(cur_name, stdout);
82 subsequent = true;
83 }
84 putchar('\n');
85}
86
87static bool write_header(const char *out_fname,
88 const struct flashmap_descriptor *root)
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);
105 fprintf(header, "#define %s %#x\n\n", HEADER_FMAP_OFFSET, fmap_offset);
106 fputs("#endif\n", header);
107
108 fclose(header);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800109 return true;
110}
111
Sol Boucher023e8292015-03-18 10:13:48 -0700112static void full_fmd_cleanup(struct flashmap_descriptor **victim)
113{
114 assert(victim);
115
116 cbfs_sections_cleanup();
117 fmd_cleanup(*victim);
118 *victim = NULL;
119}
120
Sol Boucher69b88bf2015-02-26 11:47:19 -0800121int main(int argc, char **argv)
122{
Sol Boucher023e8292015-03-18 10:13:48 -0700123 struct {
124 // Mandatory
125 const char *fmd_filename;
126 const char *fmap_filename;
127
128 // Optional
129 const char *header_filename;
130 } args = {NULL, NULL, NULL};
131
132 bool show_usage = false;
133 int each_arg;
134 while (!show_usage && (each_arg = getopt(argc, argv, ":h:")) != -1) {
135 switch (each_arg) {
136 case 'h':
137 args.header_filename = optarg;
138 break;
139 case ':':
140 fprintf(stderr, "-%c: Expected an accompanying value\n",
141 optopt);
142 show_usage = true;
143 break;
144 default:
145 fprintf(stderr, "-%c: Unexpected command-line switch\n",
146 optopt);
147 show_usage = true;
148 }
149 }
150
151 if (show_usage || argc - optind != 2) {
152 usage(argv[0]);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800153 return FMAPTOOL_EXIT_BAD_ARGS;
154 }
Sol Boucher023e8292015-03-18 10:13:48 -0700155 args.fmd_filename = argv[optind];
156 args.fmap_filename = argv[optind + 1];
Sol Boucher69b88bf2015-02-26 11:47:19 -0800157
158 FILE *fmd_file = stdin;
Sol Boucher023e8292015-03-18 10:13:48 -0700159 if (strcmp(args.fmd_filename, STDIN_FILENAME_SENTINEL) != 0) {
160 fmd_file = fopen(args.fmd_filename, "r");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800161 if (!fmd_file) {
162 fprintf(stderr, "FATAL: Unable to open file '%s'\n",
Sol Boucher023e8292015-03-18 10:13:48 -0700163 args.fmd_filename);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800164 return FMAPTOOL_EXIT_BAD_INPUT_PATH;
165 }
166 }
167
168 struct flashmap_descriptor *descriptor = fmd_create(fmd_file);
169 fclose(fmd_file);
170 if (!descriptor) {
171 fputs("FATAL: Failed while processing provided descriptor\n",
172 stderr);
Sol Boucher023e8292015-03-18 10:13:48 -0700173 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800174 return FMAPTOOL_EXIT_FAILED_DESCRIPTOR;
175 }
176
Sol Boucher023e8292015-03-18 10:13:48 -0700177 if (!fmd_find_node(descriptor, SECTION_NAME_FMAP)) {
178 fprintf(stderr,
179 "FATAL: Flashmap descriptor must have an '%s' section\n",
180 SECTION_NAME_FMAP);
181 full_fmd_cleanup(&descriptor);
182 return FMAPTOOL_EXIT_MISSING_FMAP_SECTION;
183 }
184
185 if (!cbfs_sections_primary_cbfs_accounted_for()) {
186 fprintf(stderr,
187 "FATAL: Flashmap descriptor must have a '%s' section that is annotated with '(%s)'\n",
188 SECTION_NAME_PRIMARY_CBFS,
189 SECTION_ANNOTATION_CBFS);
190 full_fmd_cleanup(&descriptor);
191 return FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS;
192 }
193
Sol Boucher69b88bf2015-02-26 11:47:19 -0800194 struct fmap *flashmap = fmap_from_fmd(descriptor);
195 if (!flashmap) {
196 fputs("FATAL: Failed while constructing FMAP section\n",
197 stderr);
Sol Boucher023e8292015-03-18 10:13:48 -0700198 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800199 return FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION;
200 }
201
202 int size = fmap_size(flashmap);
203 if (size < 0) {
204 fputs("FATAL: Failed to determine FMAP section size\n",
205 stderr);
206 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700207 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800208 return FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE;
209 }
210
Sol Boucher023e8292015-03-18 10:13:48 -0700211 FILE *fmap_file = fopen(args.fmap_filename, "wb");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800212 if (!fmap_file) {
213 fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
Sol Boucher023e8292015-03-18 10:13:48 -0700214 args.fmap_filename);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800215 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700216 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800217 return FMAPTOOL_EXIT_BAD_OUTPUT_PATH;
218 }
219
220 if (!fwrite(flashmap, size, 1, fmap_file)) {
221 fputs("FATAL: Failed to write final FMAP to file\n", stderr);
222 fclose(fmap_file);
223 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700224 full_fmd_cleanup(&descriptor);
225 return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT;
Sol Boucher69b88bf2015-02-26 11:47:19 -0800226 }
227 fclose(fmap_file);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800228 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700229
230 if (args.header_filename &&
231 !write_header(args.header_filename, descriptor)) {
232 full_fmd_cleanup(&descriptor);
233 return FMAPTOOL_EXIT_FAILED_WRITING_HEADER;
234 }
235
236 fprintf(stderr, "SUCCESS: Wrote %d bytes to file '%s'%s\n", size,
237 args.fmap_filename,
238 args.header_filename ? " (and generated header)" : "");
239 fputs("The sections containing CBFSes are: ", stderr);
240 list_cbfs_section_names();
241
242 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800243 return FMAPTOOL_EXIT_SUCCESS;
244}