blob: 4ba5967b846905a08dfbcc8b4f4eb9a3c0ebfbf8 [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* fmaptool, CLI utility for converting plaintext fmd files into fmap blobs */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Sol Boucher69b88bf2015-02-26 11:47:19 -08003
4#include "common.h"
Sol Boucher023e8292015-03-18 10:13:48 -07005#include "cbfs_sections.h"
Sol Boucher69b88bf2015-02-26 11:47:19 -08006#include "fmap_from_fmd.h"
7
8#include <stdio.h>
9#include <string.h>
Sol Boucher023e8292015-03-18 10:13:48 -070010#include <unistd.h>
Sol Boucher69b88bf2015-02-26 11:47:19 -080011
12#define STDIN_FILENAME_SENTINEL "-"
13
Sol Boucher023e8292015-03-18 10:13:48 -070014#define HEADER_FMAP_OFFSET "FMAP_OFFSET"
Julius Wernercefe89e2019-11-06 19:29:44 -080015#define HEADER_FMAP_SIZE "FMAP_SIZE"
Sol Boucher023e8292015-03-18 10:13:48 -070016
Furquan Shaikh73982ed2020-11-21 14:54:13 -080017/*
18 * Macro name used in the generated C header file to provide list of section names that do not
19 * have any sub-sections.
20 */
21#define HEADER_FMAP_TERMINAL_SECTIONS "FMAP_TERMINAL_SECTIONS"
22
Sol Boucher69b88bf2015-02-26 11:47:19 -080023enum fmaptool_return {
24 FMAPTOOL_EXIT_SUCCESS = 0,
25 FMAPTOOL_EXIT_BAD_ARGS,
26 FMAPTOOL_EXIT_BAD_INPUT_PATH,
27 FMAPTOOL_EXIT_BAD_OUTPUT_PATH,
28 FMAPTOOL_EXIT_FAILED_DESCRIPTOR,
Sol Boucher023e8292015-03-18 10:13:48 -070029 FMAPTOOL_EXIT_MISSING_FMAP_SECTION,
30 FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS,
Sol Boucher69b88bf2015-02-26 11:47:19 -080031 FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION,
32 FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE,
Sol Boucher023e8292015-03-18 10:13:48 -070033 FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT,
34 FMAPTOOL_EXIT_FAILED_WRITING_HEADER,
Sol Boucher69b88bf2015-02-26 11:47:19 -080035};
36
Sol Boucher023e8292015-03-18 10:13:48 -070037static void usage(const char *invoked_as)
Sol Boucher69b88bf2015-02-26 11:47:19 -080038{
Sol Boucher023e8292015-03-18 10:13:48 -070039 fputs("fmaptool: Compiler for fmd (flashmap descriptor) files\n",
40 stderr);
41 fputs("\nUSAGE:\n", stderr);
42 fprintf(stderr,
Patrick Georgic3771b02016-01-20 15:29:30 +010043 "\t%s [-h <header output file>] [-R <region output file>] <fmd input file> <binary output file>\n",
Sol Boucher023e8292015-03-18 10:13:48 -070044 invoked_as);
45 fputs("\nMANDATORY ARGUMENTS:\n", stderr);
46 fprintf(stderr,
47 "<fmd input file> may be '%s' to read from standard input\n",
48 STDIN_FILENAME_SENTINEL);
49 fputs("<binary output file> must be a regular file\n", stderr);
50 fputs("\nOPTIONAL SWITCHES:\n", stderr);
51 fprintf(stderr,
52 "-h\tAlso produce a C header defining %s to the FMAP section's flash offset.\n",
53 HEADER_FMAP_OFFSET);
Patrick Georgic3771b02016-01-20 15:29:30 +010054 fprintf(stderr,
55 "-R\tAlso produce a text file listing the CBFS regions, comma separated.\n");
Sol Boucher023e8292015-03-18 10:13:48 -070056 fputs("\nOUTPUT:\n", stderr);
57 fputs("A successful invocation prints a summary of work done to standard error, and a comma-separated list\n",
58 stderr);
59 fputs("of those sections that contain CBFSes, starting with the primary such section, to standard output.\n",
60 stderr);
61}
62
Patrick Georgic3771b02016-01-20 15:29:30 +010063static void list_cbfs_section_names(FILE *out)
Sol Boucher023e8292015-03-18 10:13:48 -070064{
65 cbfs_section_iterator_t cbfs_it = cbfs_sections_iterator();
66 assert(cbfs_it);
67
68 bool subsequent = false;
69 while (cbfs_it) {
70 const char *cur_name =
71 cbfs_sections_iterator_deref(cbfs_it)->name;
72 if (cbfs_sections_iterator_advance(&cbfs_it) && subsequent)
Patrick Georgic3771b02016-01-20 15:29:30 +010073 fputc(',', out);
74 fputs(cur_name, out);
Sol Boucher023e8292015-03-18 10:13:48 -070075 subsequent = true;
76 }
Patrick Georgic3771b02016-01-20 15:29:30 +010077 fputc('\n', out);
Sol Boucher023e8292015-03-18 10:13:48 -070078}
79
Furquan Shaikh73982ed2020-11-21 14:54:13 -080080static void write_header_fmap_terminal_section_names(FILE *header,
81 const struct flashmap_descriptor *root)
82{
83 assert(root);
84
85 if (root->list_len == 0) {
86 fprintf(header, "%s ", root->name);
87 return;
88 }
89
90 fmd_foreach_child(child, root)
91 write_header_fmap_terminal_section_names(header, child);
92}
93
Felix Heldd57c1282020-09-18 19:01:16 +020094static void write_header_fmap_sections(FILE *header, const struct flashmap_descriptor *root,
95 unsigned int offset)
96{
97 assert(root);
98 /*
99 * The offset may only be unknown for the root node in a system where the flash isn't
100 * memory-mapped.
101 */
102 if (!root->offset_known && offset != 0)
103 return;
104
105 const unsigned int current_offset = offset + (root->offset_known ? root->offset : 0);
106 fprintf(header, "#define FMAP_SECTION_%s_START %#x\n", root->name, current_offset);
107
108 if (!root->size_known)
109 return;
110
111 fprintf(header, "#define FMAP_SECTION_%s_SIZE %#x\n", root->name, root->size);
112
113 fmd_foreach_child(child, root)
114 write_header_fmap_sections(header, child, current_offset);
115}
116
Sol Boucher023e8292015-03-18 10:13:48 -0700117static bool write_header(const char *out_fname,
Julius Wernercefe89e2019-11-06 19:29:44 -0800118 const struct flashmap_descriptor *root,
119 const int fmap_size)
Sol Boucher023e8292015-03-18 10:13:48 -0700120{
121 assert(out_fname);
122
123 FILE *header = fopen(out_fname, "w");
124 if (!header) {
125 fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
126 out_fname);
127 return false;
128 }
129
130 unsigned fmap_offset =
131 fmd_calc_absolute_offset(root, SECTION_NAME_FMAP);
132 assert(fmap_offset != FMD_NOTFOUND);
133
134 fputs("#ifndef FMAPTOOL_GENERATED_HEADER_H_\n", header);
135 fputs("#define FMAPTOOL_GENERATED_HEADER_H_\n\n", header);
Julius Wernercefe89e2019-11-06 19:29:44 -0800136 fprintf(header, "#define %s %#x\n", HEADER_FMAP_OFFSET, fmap_offset);
137 fprintf(header, "#define %s %#x\n\n", HEADER_FMAP_SIZE, fmap_size);
Patrick Georgi919be612016-05-02 17:02:53 +0800138
Furquan Shaikh73982ed2020-11-21 14:54:13 -0800139 fprintf(header, "#define %s \"", HEADER_FMAP_TERMINAL_SECTIONS);
140 write_header_fmap_terminal_section_names(header, root);
141 fprintf(header, "\"\n\n");
142
Felix Heldd57c1282020-09-18 19:01:16 +0200143 write_header_fmap_sections(header, root, 0);
144 fputs("\n", header);
145
Sol Boucher023e8292015-03-18 10:13:48 -0700146 fputs("#endif\n", header);
147
148 fclose(header);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800149 return true;
150}
151
Sol Boucher023e8292015-03-18 10:13:48 -0700152static void full_fmd_cleanup(struct flashmap_descriptor **victim)
153{
154 assert(victim);
155
156 cbfs_sections_cleanup();
157 fmd_cleanup(*victim);
158 *victim = NULL;
159}
160
Sol Boucher69b88bf2015-02-26 11:47:19 -0800161int main(int argc, char **argv)
162{
Sol Boucher023e8292015-03-18 10:13:48 -0700163 struct {
164 // Mandatory
165 const char *fmd_filename;
166 const char *fmap_filename;
167
168 // Optional
169 const char *header_filename;
Patrick Georgic3771b02016-01-20 15:29:30 +0100170 const char *region_filename;
Werner Zeh998671c2016-01-25 14:15:43 +0100171 } args = {NULL, NULL, NULL, NULL};
Sol Boucher023e8292015-03-18 10:13:48 -0700172
173 bool show_usage = false;
174 int each_arg;
Patrick Georgic3771b02016-01-20 15:29:30 +0100175 while (!show_usage && (each_arg = getopt(argc, argv, ":h:R:")) != -1) {
Sol Boucher023e8292015-03-18 10:13:48 -0700176 switch (each_arg) {
177 case 'h':
178 args.header_filename = optarg;
179 break;
Patrick Georgic3771b02016-01-20 15:29:30 +0100180 case 'R':
181 args.region_filename = optarg;
182 break;
Sol Boucher023e8292015-03-18 10:13:48 -0700183 case ':':
184 fprintf(stderr, "-%c: Expected an accompanying value\n",
185 optopt);
186 show_usage = true;
187 break;
188 default:
189 fprintf(stderr, "-%c: Unexpected command-line switch\n",
190 optopt);
191 show_usage = true;
192 }
193 }
194
195 if (show_usage || argc - optind != 2) {
196 usage(argv[0]);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800197 return FMAPTOOL_EXIT_BAD_ARGS;
198 }
Sol Boucher023e8292015-03-18 10:13:48 -0700199 args.fmd_filename = argv[optind];
200 args.fmap_filename = argv[optind + 1];
Sol Boucher69b88bf2015-02-26 11:47:19 -0800201
202 FILE *fmd_file = stdin;
Sol Boucher023e8292015-03-18 10:13:48 -0700203 if (strcmp(args.fmd_filename, STDIN_FILENAME_SENTINEL) != 0) {
204 fmd_file = fopen(args.fmd_filename, "r");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800205 if (!fmd_file) {
206 fprintf(stderr, "FATAL: Unable to open file '%s'\n",
Sol Boucher023e8292015-03-18 10:13:48 -0700207 args.fmd_filename);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800208 return FMAPTOOL_EXIT_BAD_INPUT_PATH;
209 }
210 }
211
212 struct flashmap_descriptor *descriptor = fmd_create(fmd_file);
213 fclose(fmd_file);
214 if (!descriptor) {
215 fputs("FATAL: Failed while processing provided descriptor\n",
216 stderr);
Sol Boucher023e8292015-03-18 10:13:48 -0700217 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800218 return FMAPTOOL_EXIT_FAILED_DESCRIPTOR;
219 }
220
Sol Boucher023e8292015-03-18 10:13:48 -0700221 if (!fmd_find_node(descriptor, SECTION_NAME_FMAP)) {
222 fprintf(stderr,
223 "FATAL: Flashmap descriptor must have an '%s' section\n",
224 SECTION_NAME_FMAP);
225 full_fmd_cleanup(&descriptor);
226 return FMAPTOOL_EXIT_MISSING_FMAP_SECTION;
227 }
228
229 if (!cbfs_sections_primary_cbfs_accounted_for()) {
230 fprintf(stderr,
231 "FATAL: Flashmap descriptor must have a '%s' section that is annotated with '(%s)'\n",
232 SECTION_NAME_PRIMARY_CBFS,
233 SECTION_ANNOTATION_CBFS);
234 full_fmd_cleanup(&descriptor);
235 return FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS;
236 }
237
Sol Boucher69b88bf2015-02-26 11:47:19 -0800238 struct fmap *flashmap = fmap_from_fmd(descriptor);
239 if (!flashmap) {
240 fputs("FATAL: Failed while constructing FMAP section\n",
241 stderr);
Sol Boucher023e8292015-03-18 10:13:48 -0700242 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800243 return FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION;
244 }
245
246 int size = fmap_size(flashmap);
247 if (size < 0) {
248 fputs("FATAL: Failed to determine FMAP section size\n",
249 stderr);
250 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700251 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800252 return FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE;
253 }
254
Sol Boucher023e8292015-03-18 10:13:48 -0700255 FILE *fmap_file = fopen(args.fmap_filename, "wb");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800256 if (!fmap_file) {
257 fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
Sol Boucher023e8292015-03-18 10:13:48 -0700258 args.fmap_filename);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800259 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700260 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800261 return FMAPTOOL_EXIT_BAD_OUTPUT_PATH;
262 }
263
264 if (!fwrite(flashmap, size, 1, fmap_file)) {
265 fputs("FATAL: Failed to write final FMAP to file\n", stderr);
266 fclose(fmap_file);
267 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700268 full_fmd_cleanup(&descriptor);
269 return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT;
Sol Boucher69b88bf2015-02-26 11:47:19 -0800270 }
271 fclose(fmap_file);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800272 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700273
274 if (args.header_filename &&
Julius Wernercefe89e2019-11-06 19:29:44 -0800275 !write_header(args.header_filename, descriptor, size)) {
Sol Boucher023e8292015-03-18 10:13:48 -0700276 full_fmd_cleanup(&descriptor);
277 return FMAPTOOL_EXIT_FAILED_WRITING_HEADER;
278 }
279
280 fprintf(stderr, "SUCCESS: Wrote %d bytes to file '%s'%s\n", size,
281 args.fmap_filename,
282 args.header_filename ? " (and generated header)" : "");
283 fputs("The sections containing CBFSes are: ", stderr);
Patrick Georgic3771b02016-01-20 15:29:30 +0100284 list_cbfs_section_names(stdout);
285 if (args.region_filename) {
286 FILE *region_file = fopen(args.region_filename, "w");
287 if (region_file == NULL)
288 return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT;
289
290 list_cbfs_section_names(region_file);
291 fclose(region_file);
292 }
Sol Boucher023e8292015-03-18 10:13:48 -0700293
294 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800295 return FMAPTOOL_EXIT_SUCCESS;
296}