blob: aace5aea33158b0c069f46b07dc4dc89f6f65c1a [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
Sol Boucher69b88bf2015-02-26 11:47:19 -080017enum 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 Boucher023e8292015-03-18 10:13:48 -070023 FMAPTOOL_EXIT_MISSING_FMAP_SECTION,
24 FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS,
Sol Boucher69b88bf2015-02-26 11:47:19 -080025 FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION,
26 FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE,
Sol Boucher023e8292015-03-18 10:13:48 -070027 FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT,
28 FMAPTOOL_EXIT_FAILED_WRITING_HEADER,
Sol Boucher69b88bf2015-02-26 11:47:19 -080029};
30
Sol Boucher023e8292015-03-18 10:13:48 -070031static void usage(const char *invoked_as)
Sol Boucher69b88bf2015-02-26 11:47:19 -080032{
Sol Boucher023e8292015-03-18 10:13:48 -070033 fputs("fmaptool: Compiler for fmd (flashmap descriptor) files\n",
34 stderr);
35 fputs("\nUSAGE:\n", stderr);
36 fprintf(stderr,
Patrick Georgic3771b02016-01-20 15:29:30 +010037 "\t%s [-h <header output file>] [-R <region output file>] <fmd input file> <binary output file>\n",
Sol Boucher023e8292015-03-18 10:13:48 -070038 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 Georgic3771b02016-01-20 15:29:30 +010048 fprintf(stderr,
49 "-R\tAlso produce a text file listing the CBFS regions, comma separated.\n");
Sol Boucher023e8292015-03-18 10:13:48 -070050 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 Georgic3771b02016-01-20 15:29:30 +010057static void list_cbfs_section_names(FILE *out)
Sol Boucher023e8292015-03-18 10:13:48 -070058{
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 Georgic3771b02016-01-20 15:29:30 +010067 fputc(',', out);
68 fputs(cur_name, out);
Sol Boucher023e8292015-03-18 10:13:48 -070069 subsequent = true;
70 }
Patrick Georgic3771b02016-01-20 15:29:30 +010071 fputc('\n', out);
Sol Boucher023e8292015-03-18 10:13:48 -070072}
73
74static bool write_header(const char *out_fname,
Julius Wernercefe89e2019-11-06 19:29:44 -080075 const struct flashmap_descriptor *root,
76 const int fmap_size)
Sol Boucher023e8292015-03-18 10:13:48 -070077{
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 Wernercefe89e2019-11-06 19:29:44 -080093 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 Georgi919be612016-05-02 17:02:53 +080095
Sol Boucher023e8292015-03-18 10:13:48 -070096 fputs("#endif\n", header);
97
98 fclose(header);
Sol Boucher69b88bf2015-02-26 11:47:19 -080099 return true;
100}
101
Sol Boucher023e8292015-03-18 10:13:48 -0700102static 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 Boucher69b88bf2015-02-26 11:47:19 -0800111int main(int argc, char **argv)
112{
Sol Boucher023e8292015-03-18 10:13:48 -0700113 struct {
114 // Mandatory
115 const char *fmd_filename;
116 const char *fmap_filename;
117
118 // Optional
119 const char *header_filename;
Patrick Georgic3771b02016-01-20 15:29:30 +0100120 const char *region_filename;
Werner Zeh998671c2016-01-25 14:15:43 +0100121 } args = {NULL, NULL, NULL, NULL};
Sol Boucher023e8292015-03-18 10:13:48 -0700122
123 bool show_usage = false;
124 int each_arg;
Patrick Georgic3771b02016-01-20 15:29:30 +0100125 while (!show_usage && (each_arg = getopt(argc, argv, ":h:R:")) != -1) {
Sol Boucher023e8292015-03-18 10:13:48 -0700126 switch (each_arg) {
127 case 'h':
128 args.header_filename = optarg;
129 break;
Patrick Georgic3771b02016-01-20 15:29:30 +0100130 case 'R':
131 args.region_filename = optarg;
132 break;
Sol Boucher023e8292015-03-18 10:13:48 -0700133 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 Boucher69b88bf2015-02-26 11:47:19 -0800147 return FMAPTOOL_EXIT_BAD_ARGS;
148 }
Sol Boucher023e8292015-03-18 10:13:48 -0700149 args.fmd_filename = argv[optind];
150 args.fmap_filename = argv[optind + 1];
Sol Boucher69b88bf2015-02-26 11:47:19 -0800151
152 FILE *fmd_file = stdin;
Sol Boucher023e8292015-03-18 10:13:48 -0700153 if (strcmp(args.fmd_filename, STDIN_FILENAME_SENTINEL) != 0) {
154 fmd_file = fopen(args.fmd_filename, "r");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800155 if (!fmd_file) {
156 fprintf(stderr, "FATAL: Unable to open file '%s'\n",
Sol Boucher023e8292015-03-18 10:13:48 -0700157 args.fmd_filename);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800158 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 Boucher023e8292015-03-18 10:13:48 -0700167 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800168 return FMAPTOOL_EXIT_FAILED_DESCRIPTOR;
169 }
170
Sol Boucher023e8292015-03-18 10:13:48 -0700171 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 Boucher69b88bf2015-02-26 11:47:19 -0800188 struct fmap *flashmap = fmap_from_fmd(descriptor);
189 if (!flashmap) {
190 fputs("FATAL: Failed while constructing FMAP section\n",
191 stderr);
Sol Boucher023e8292015-03-18 10:13:48 -0700192 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800193 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 Boucher023e8292015-03-18 10:13:48 -0700201 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800202 return FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE;
203 }
204
Sol Boucher023e8292015-03-18 10:13:48 -0700205 FILE *fmap_file = fopen(args.fmap_filename, "wb");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800206 if (!fmap_file) {
207 fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
Sol Boucher023e8292015-03-18 10:13:48 -0700208 args.fmap_filename);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800209 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700210 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800211 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 Boucher023e8292015-03-18 10:13:48 -0700218 full_fmd_cleanup(&descriptor);
219 return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT;
Sol Boucher69b88bf2015-02-26 11:47:19 -0800220 }
221 fclose(fmap_file);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800222 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700223
224 if (args.header_filename &&
Julius Wernercefe89e2019-11-06 19:29:44 -0800225 !write_header(args.header_filename, descriptor, size)) {
Sol Boucher023e8292015-03-18 10:13:48 -0700226 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 Georgic3771b02016-01-20 15:29:30 +0100234 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 Boucher023e8292015-03-18 10:13:48 -0700243
244 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800245 return FMAPTOOL_EXIT_SUCCESS;
246}