blob: c8397073f24348e72e6a2f55bbea0b60cea76805 [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,
48 "\t%s [-h <header output file>] <fmd input file> <binary output file>\n",
49 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);
59 fputs("\nOUTPUT:\n", stderr);
60 fputs("A successful invocation prints a summary of work done to standard error, and a comma-separated list\n",
61 stderr);
62 fputs("of those sections that contain CBFSes, starting with the primary such section, to standard output.\n",
63 stderr);
64}
65
66static void list_cbfs_section_names(void)
67{
68 cbfs_section_iterator_t cbfs_it = cbfs_sections_iterator();
69 assert(cbfs_it);
70
71 bool subsequent = false;
72 while (cbfs_it) {
73 const char *cur_name =
74 cbfs_sections_iterator_deref(cbfs_it)->name;
75 if (cbfs_sections_iterator_advance(&cbfs_it) && subsequent)
76 putchar(',');
77 fputs(cur_name, stdout);
78 subsequent = true;
79 }
80 putchar('\n');
81}
82
83static bool write_header(const char *out_fname,
84 const struct flashmap_descriptor *root)
85{
86 assert(out_fname);
87
88 FILE *header = fopen(out_fname, "w");
89 if (!header) {
90 fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
91 out_fname);
92 return false;
93 }
94
95 unsigned fmap_offset =
96 fmd_calc_absolute_offset(root, SECTION_NAME_FMAP);
97 assert(fmap_offset != FMD_NOTFOUND);
98
99 fputs("#ifndef FMAPTOOL_GENERATED_HEADER_H_\n", header);
100 fputs("#define FMAPTOOL_GENERATED_HEADER_H_\n\n", header);
101 fprintf(header, "#define %s %#x\n\n", HEADER_FMAP_OFFSET, fmap_offset);
102 fputs("#endif\n", header);
103
104 fclose(header);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800105 return true;
106}
107
Sol Boucher023e8292015-03-18 10:13:48 -0700108static void full_fmd_cleanup(struct flashmap_descriptor **victim)
109{
110 assert(victim);
111
112 cbfs_sections_cleanup();
113 fmd_cleanup(*victim);
114 *victim = NULL;
115}
116
Sol Boucher69b88bf2015-02-26 11:47:19 -0800117int main(int argc, char **argv)
118{
Sol Boucher023e8292015-03-18 10:13:48 -0700119 struct {
120 // Mandatory
121 const char *fmd_filename;
122 const char *fmap_filename;
123
124 // Optional
125 const char *header_filename;
126 } args = {NULL, NULL, NULL};
127
128 bool show_usage = false;
129 int each_arg;
130 while (!show_usage && (each_arg = getopt(argc, argv, ":h:")) != -1) {
131 switch (each_arg) {
132 case 'h':
133 args.header_filename = optarg;
134 break;
135 case ':':
136 fprintf(stderr, "-%c: Expected an accompanying value\n",
137 optopt);
138 show_usage = true;
139 break;
140 default:
141 fprintf(stderr, "-%c: Unexpected command-line switch\n",
142 optopt);
143 show_usage = true;
144 }
145 }
146
147 if (show_usage || argc - optind != 2) {
148 usage(argv[0]);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800149 return FMAPTOOL_EXIT_BAD_ARGS;
150 }
Sol Boucher023e8292015-03-18 10:13:48 -0700151 args.fmd_filename = argv[optind];
152 args.fmap_filename = argv[optind + 1];
Sol Boucher69b88bf2015-02-26 11:47:19 -0800153
154 FILE *fmd_file = stdin;
Sol Boucher023e8292015-03-18 10:13:48 -0700155 if (strcmp(args.fmd_filename, STDIN_FILENAME_SENTINEL) != 0) {
156 fmd_file = fopen(args.fmd_filename, "r");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800157 if (!fmd_file) {
158 fprintf(stderr, "FATAL: Unable to open file '%s'\n",
Sol Boucher023e8292015-03-18 10:13:48 -0700159 args.fmd_filename);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800160 return FMAPTOOL_EXIT_BAD_INPUT_PATH;
161 }
162 }
163
164 struct flashmap_descriptor *descriptor = fmd_create(fmd_file);
165 fclose(fmd_file);
166 if (!descriptor) {
167 fputs("FATAL: Failed while processing provided descriptor\n",
168 stderr);
Sol Boucher023e8292015-03-18 10:13:48 -0700169 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800170 return FMAPTOOL_EXIT_FAILED_DESCRIPTOR;
171 }
172
Sol Boucher023e8292015-03-18 10:13:48 -0700173 if (!fmd_find_node(descriptor, SECTION_NAME_FMAP)) {
174 fprintf(stderr,
175 "FATAL: Flashmap descriptor must have an '%s' section\n",
176 SECTION_NAME_FMAP);
177 full_fmd_cleanup(&descriptor);
178 return FMAPTOOL_EXIT_MISSING_FMAP_SECTION;
179 }
180
181 if (!cbfs_sections_primary_cbfs_accounted_for()) {
182 fprintf(stderr,
183 "FATAL: Flashmap descriptor must have a '%s' section that is annotated with '(%s)'\n",
184 SECTION_NAME_PRIMARY_CBFS,
185 SECTION_ANNOTATION_CBFS);
186 full_fmd_cleanup(&descriptor);
187 return FMAPTOOL_EXIT_MISSING_PRIMARY_CBFS;
188 }
189
Sol Boucher69b88bf2015-02-26 11:47:19 -0800190 struct fmap *flashmap = fmap_from_fmd(descriptor);
191 if (!flashmap) {
192 fputs("FATAL: Failed while constructing FMAP section\n",
193 stderr);
Sol Boucher023e8292015-03-18 10:13:48 -0700194 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800195 return FMAPTOOL_EXIT_FAILED_FMAP_CONVERSION;
196 }
197
198 int size = fmap_size(flashmap);
199 if (size < 0) {
200 fputs("FATAL: Failed to determine FMAP section size\n",
201 stderr);
202 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700203 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800204 return FMAPTOOL_EXIT_UNKNOWN_FMAP_SIZE;
205 }
206
Sol Boucher023e8292015-03-18 10:13:48 -0700207 FILE *fmap_file = fopen(args.fmap_filename, "wb");
Sol Boucher69b88bf2015-02-26 11:47:19 -0800208 if (!fmap_file) {
209 fprintf(stderr, "FATAL: Unable to open file '%s' for writing\n",
Sol Boucher023e8292015-03-18 10:13:48 -0700210 args.fmap_filename);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800211 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700212 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800213 return FMAPTOOL_EXIT_BAD_OUTPUT_PATH;
214 }
215
216 if (!fwrite(flashmap, size, 1, fmap_file)) {
217 fputs("FATAL: Failed to write final FMAP to file\n", stderr);
218 fclose(fmap_file);
219 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700220 full_fmd_cleanup(&descriptor);
221 return FMAPTOOL_EXIT_FAILED_WRITING_OUTPUT;
Sol Boucher69b88bf2015-02-26 11:47:19 -0800222 }
223 fclose(fmap_file);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800224 fmap_destroy(flashmap);
Sol Boucher023e8292015-03-18 10:13:48 -0700225
226 if (args.header_filename &&
227 !write_header(args.header_filename, descriptor)) {
228 full_fmd_cleanup(&descriptor);
229 return FMAPTOOL_EXIT_FAILED_WRITING_HEADER;
230 }
231
232 fprintf(stderr, "SUCCESS: Wrote %d bytes to file '%s'%s\n", size,
233 args.fmap_filename,
234 args.header_filename ? " (and generated header)" : "");
235 fputs("The sections containing CBFSes are: ", stderr);
236 list_cbfs_section_names();
237
238 full_fmd_cleanup(&descriptor);
Sol Boucher69b88bf2015-02-26 11:47:19 -0800239 return FMAPTOOL_EXIT_SUCCESS;
240}