blob: faf65081fd24b985598f5c789b89db3bb1aa33cc [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.
Sol Boucher69b88bf2015-02-26 11:47:19 -080014 */
15
16#include "common.h"
Sol Boucher023e8292015-03-18 10:13:48 -070017#include "cbfs_sections.h"
Sol Boucher69b88bf2015-02-26 11:47:19 -080018#include "fmap_from_fmd.h"
19
20#include <stdio.h>
21#include <string.h>
Sol Boucher023e8292015-03-18 10:13:48 -070022#include <unistd.h>
Sol Boucher69b88bf2015-02-26 11:47:19 -080023
24#define STDIN_FILENAME_SENTINEL "-"
25
Sol Boucher023e8292015-03-18 10:13:48 -070026#define HEADER_FMAP_OFFSET "FMAP_OFFSET"
27
Sol Boucher69b88bf2015-02-26 11:47:19 -080028enum fmaptool_return {
29 FMAPTOOL_EXIT_SUCCESS = 0,
30 FMAPTOOL_EXIT_BAD_ARGS,
31 FMAPTOOL_EXIT_BAD_INPUT_PATH,
32 FMAPTOOL_EXIT_BAD_OUTPUT_PATH,
33 FMAPTOOL_EXIT_FAILED_DESCRIPTOR,
Sol Boucher023e8292015-03-18 10:13:48 -070034 FMAPTOOL_EXIT_MISSING_FMAP_SECTION,
35 FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS,
Sol Boucher69b88bf2015-02-26 11:47:19 -080036 FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION,
37 FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE,
Sol Boucher023e8292015-03-18 10:13:48 -070038 FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT,
39 FMAPTOOL_EXIT_FAILED_WRITING_HEADER,
Sol Boucher69b88bf2015-02-26 11:47:19 -080040};
41
Sol Boucher023e8292015-03-18 10:13:48 -070042static void usage(const char *invoked_as)
Sol Boucher69b88bf2015-02-26 11:47:19 -080043{
Sol Boucher023e8292015-03-18 10:13:48 -070044 fputs("fmaptool: Compiler for fmd (flashmap descriptor) files\n",
45 stderr);
46 fputs("\nUSAGE:\n", stderr);
47 fprintf(stderr,
Patrick Georgic3771b02016-01-20 15:29:30 +010048 "\t%s [-h <header output file>] [-R <region output file>] <fmd input file> <binary output file>\n",
Sol Boucher023e8292015-03-18 10:13:48 -070049 invoked_as);
50 fputs("\nMANDATORY ARGUMENTS:\n", stderr);
51 fprintf(stderr,
52 "<fmd input file> may be '%s' to read from standard input\n",
53 STDIN_FILENAME_SENTINEL);
54 fputs("<binary output file> must be a regular file\n", stderr);
55 fputs("\nOPTIONAL SWITCHES:\n", stderr);
56 fprintf(stderr,
57 "-h\tAlso produce a C header defining %s to the FMAP section's flash offset.\n",
58 HEADER_FMAP_OFFSET);
Patrick Georgic3771b02016-01-20 15:29:30 +010059 fprintf(stderr,
60 "-R\tAlso produce a text file listing the CBFS regions, comma separated.\n");
Sol Boucher023e8292015-03-18 10:13:48 -070061 fputs("\nOUTPUT:\n", stderr);
62 fputs("A successful invocation prints a summary of work done to standard error, and a comma-separated list\n",
63 stderr);
64 fputs("of those sections that contain CBFSes, starting with the primary such section, to standard output.\n",
65 stderr);
66}
67
Patrick Georgic3771b02016-01-20 15:29:30 +010068static void list_cbfs_section_names(FILE *out)
Sol Boucher023e8292015-03-18 10:13:48 -070069{
70 cbfs_section_iterator_t cbfs_it = cbfs_sections_iterator();
71 assert(cbfs_it);
72
73 bool subsequent = false;
74 while (cbfs_it) {
75 const char *cur_name =
76 cbfs_sections_iterator_deref(cbfs_it)->name;
77 if (cbfs_sections_iterator_advance(&cbfs_it) && subsequent)
Patrick Georgic3771b02016-01-20 15:29:30 +010078 fputc(',', out);
79 fputs(cur_name, out);
Sol Boucher023e8292015-03-18 10:13:48 -070080 subsequent = true;
81 }
Patrick Georgic3771b02016-01-20 15:29:30 +010082 fputc('\n', out);
Sol Boucher023e8292015-03-18 10:13:48 -070083}
84
85static bool write_header(const char *out_fname,
86 const struct flashmap_descriptor *root)
87{
88 assert(out_fname);
89
90 FILE *header = fopen(out_fname, "w");
91 if (!header) {
92 fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
93 out_fname);
94 return false;
95 }
96
97 unsigned fmap_offset =
98 fmd_calc_absolute_offset(root, SECTION_NAME_FMAP);
99 assert(fmap_offset != FMD_NOTFOUND);
100
101 fputs("#ifndef FMAPTOOL_GENERATED_HEADER_H_\n", header);
102 fputs("#define FMAPTOOL_GENERATED_HEADER_H_\n\n", header);
103 fprintf(header, "#define %s %#x\n\n", HEADER_FMAP_OFFSET, fmap_offset);
Patrick Georgi919be612016-05-02 17:02:53 +0800104
105 /* also add defines for each CBFS-carrying fmap region: base and size */
106 cbfs_section_iterator_t cbfs_it = cbfs_sections_iterator();
107 while (cbfs_it) {
108 const struct flashmap_descriptor *item =
109 cbfs_sections_iterator_deref(cbfs_it);
110 assert(item->offset_known && item->size_known);
Julius Werner119dcee2016-06-20 13:21:36 -0700111 unsigned abs_base = fmd_calc_absolute_offset(root, item->name);
Patrick Georgi919be612016-05-02 17:02:53 +0800112 fprintf(header, "#define ___FMAP__%s_BASE 0x%x\n",
Julius Werner119dcee2016-06-20 13:21:36 -0700113 item->name, abs_base);
Patrick Georgi919be612016-05-02 17:02:53 +0800114 fprintf(header, "#define ___FMAP__%s_SIZE 0x%x\n",
115 item->name, item->size);
116 cbfs_sections_iterator_advance(&cbfs_it);
117 }
118
Sol Boucher023e8292015-03-18 10:13:48 -0700119 fputs("#endif\n", header);
120
121 fclose(header);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800122 return true;
123}
124
Sol Boucher023e8292015-03-18 10:13:48 -0700125static void full_fmd_cleanup(struct flashmap_descriptor **victim)
126{
127 assert(victim);
128
129 cbfs_sections_cleanup();
130 fmd_cleanup(*victim);
131 *victim = NULL;
132}
133
Sol Boucher69b88bf2015-02-26 11:47:19 -0800134int main(int argc, char **argv)
135{
Sol Boucher023e8292015-03-18 10:13:48 -0700136 struct {
137 // Mandatory
138 const char *fmd_filename;
139 const char *fmap_filename;
140
141 // Optional
142 const char *header_filename;
Patrick Georgic3771b02016-01-20 15:29:30 +0100143 const char *region_filename;
Werner Zeh998671c2016-01-25 14:15:43 +0100144 } args = {NULL, NULL, NULL, NULL};
Sol Boucher023e8292015-03-18 10:13:48 -0700145
146 bool show_usage = false;
147 int each_arg;
Patrick Georgic3771b02016-01-20 15:29:30 +0100148 while (!show_usage && (each_arg = getopt(argc, argv, ":h:R:")) != -1) {
Sol Boucher023e8292015-03-18 10:13:48 -0700149 switch (each_arg) {
150 case 'h':
151 args.header_filename = optarg;
152 break;
Patrick Georgic3771b02016-01-20 15:29:30 +0100153 case 'R':
154 args.region_filename = optarg;
155 break;
Sol Boucher023e8292015-03-18 10:13:48 -0700156 case ':':
157 fprintf(stderr, "-%c: Expected an accompanying value\n",
158 optopt);
159 show_usage = true;
160 break;
161 default:
162 fprintf(stderr, "-%c: Unexpected command-line switch\n",
163 optopt);
164 show_usage = true;
165 }
166 }
167
168 if (show_usage || argc - optind != 2) {
169 usage(argv[0]);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800170 return FMAPTOOL_EXIT_BAD_ARGS;
171 }
Sol Boucher023e8292015-03-18 10:13:48 -0700172 args.fmd_filename = argv[optind];
173 args.fmap_filename = argv[optind + 1];
Sol Boucher69b88bf2015-02-26 11:47:19 -0800174
175 FILE *fmd_file = stdin;
Sol Boucher023e8292015-03-18 10:13:48 -0700176 if (strcmp(args.fmd_filename, STDIN_FILENAME_SENTINEL) != 0) {
177 fmd_file = fopen(args.fmd_filename, "r");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800178 if (!fmd_file) {
179 fprintf(stderr, "FATAL: Unable to open file '%s'\n",
Sol Boucher023e8292015-03-18 10:13:48 -0700180 args.fmd_filename);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800181 return FMAPTOOL_EXIT_BAD_INPUT_PATH;
182 }
183 }
184
185 struct flashmap_descriptor *descriptor = fmd_create(fmd_file);
186 fclose(fmd_file);
187 if (!descriptor) {
188 fputs("FATAL: Failed while processing provided descriptor\n",
189 stderr);
Sol Boucher023e8292015-03-18 10:13:48 -0700190 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800191 return FMAPTOOL_EXIT_FAILED_DESCRIPTOR;
192 }
193
Sol Boucher023e8292015-03-18 10:13:48 -0700194 if (!fmd_find_node(descriptor, SECTION_NAME_FMAP)) {
195 fprintf(stderr,
196 "FATAL: Flashmap descriptor must have an '%s' section\n",
197 SECTION_NAME_FMAP);
198 full_fmd_cleanup(&descriptor);
199 return FMAPTOOL_EXIT_MISSING_FMAP_SECTION;
200 }
201
202 if (!cbfs_sections_primary_cbfs_accounted_for()) {
203 fprintf(stderr,
204 "FATAL: Flashmap descriptor must have a '%s' section that is annotated with '(%s)'\n",
205 SECTION_NAME_PRIMARY_CBFS,
206 SECTION_ANNOTATION_CBFS);
207 full_fmd_cleanup(&descriptor);
208 return FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS;
209 }
210
Sol Boucher69b88bf2015-02-26 11:47:19 -0800211 struct fmap *flashmap = fmap_from_fmd(descriptor);
212 if (!flashmap) {
213 fputs("FATAL: Failed while constructing FMAP section\n",
214 stderr);
Sol Boucher023e8292015-03-18 10:13:48 -0700215 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800216 return FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION;
217 }
218
219 int size = fmap_size(flashmap);
220 if (size < 0) {
221 fputs("FATAL: Failed to determine FMAP section size\n",
222 stderr);
223 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700224 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800225 return FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE;
226 }
227
Sol Boucher023e8292015-03-18 10:13:48 -0700228 FILE *fmap_file = fopen(args.fmap_filename, "wb");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800229 if (!fmap_file) {
230 fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
Sol Boucher023e8292015-03-18 10:13:48 -0700231 args.fmap_filename);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800232 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700233 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800234 return FMAPTOOL_EXIT_BAD_OUTPUT_PATH;
235 }
236
237 if (!fwrite(flashmap, size, 1, fmap_file)) {
238 fputs("FATAL: Failed to write final FMAP to file\n", stderr);
239 fclose(fmap_file);
240 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700241 full_fmd_cleanup(&descriptor);
242 return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT;
Sol Boucher69b88bf2015-02-26 11:47:19 -0800243 }
244 fclose(fmap_file);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800245 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700246
247 if (args.header_filename &&
248 !write_header(args.header_filename, descriptor)) {
249 full_fmd_cleanup(&descriptor);
250 return FMAPTOOL_EXIT_FAILED_WRITING_HEADER;
251 }
252
253 fprintf(stderr, "SUCCESS: Wrote %d bytes to file '%s'%s\n", size,
254 args.fmap_filename,
255 args.header_filename ? " (and generated header)" : "");
256 fputs("The sections containing CBFSes are: ", stderr);
Patrick Georgic3771b02016-01-20 15:29:30 +0100257 list_cbfs_section_names(stdout);
258 if (args.region_filename) {
259 FILE *region_file = fopen(args.region_filename, "w");
260 if (region_file == NULL)
261 return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT;
262
263 list_cbfs_section_names(region_file);
264 fclose(region_file);
265 }
Sol Boucher023e8292015-03-18 10:13:48 -0700266
267 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800268 return FMAPTOOL_EXIT_SUCCESS;
269}