blob: ea04c22244ba1f0be07d4e4d2edd4d4c75744778 [file] [log] [blame]
Bill Richardsond55085d2011-02-04 15:01:37 -08001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// Utility for manipulating firmware screen block (BMPBLOCK) in GBB.
6//
7
8#include "bmpblk_utility.h"
Bill Richardson0a9977e2011-08-22 16:03:59 -07009#include "image_types.h"
Bill Richardsond55085d2011-02-04 15:01:37 -080010
11#include <assert.h>
12#include <errno.h>
13#include <getopt.h>
Tom Wai-Hong Tamee2bc912011-02-17 12:58:58 +080014#include <lzma.h>
Bill Richardsond55085d2011-02-04 15:01:37 -080015#include <stdarg.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <yaml.h>
20
Bill Richardson61362d62011-02-14 10:28:03 -080021extern "C" {
22#include "eficompress.h"
23}
24
25
Bill Richardsond55085d2011-02-04 15:01:37 -080026static void error(const char *format, ...) {
27 va_list ap;
28 va_start(ap, format);
29 fprintf(stderr, "ERROR: ");
30 vfprintf(stderr, format, ap);
31 va_end(ap);
32 exit(1);
33}
34
35///////////////////////////////////////////////////////////////////////
36// BmpBlock Utility implementation
37
38namespace vboot_reference {
39
Bill Richardson54e95822011-05-05 15:12:10 -070040 BmpBlockUtil::BmpBlockUtil(bool debug) {
41 major_version_ = BMPBLOCK_MAJOR_VERSION;
42 minor_version_ = BMPBLOCK_MINOR_VERSION;
43 config_.config_filename.clear();
44 memset(&config_.header, '\0', BMPBLOCK_SIGNATURE_SIZE);
45 config_.images_map.clear();
46 config_.screens_map.clear();
47 config_.localizations.clear();
48 bmpblock_.clear();
49 set_compression_ = false;
50 compression_ = COMPRESS_NONE;
51 debug_ = debug;
Bill Richardson0a9977e2011-08-22 16:03:59 -070052 render_hwid_ = true;
53 support_font_ = true;
54 got_font_ = false;
55 got_rtol_font_ = false;
Bill Richardsond55085d2011-02-04 15:01:37 -080056 }
57
Bill Richardson54e95822011-05-05 15:12:10 -070058 BmpBlockUtil::~BmpBlockUtil() {
Bill Richardsond55085d2011-02-04 15:01:37 -080059 }
Bill Richardsond55085d2011-02-04 15:01:37 -080060
Bill Richardson54e95822011-05-05 15:12:10 -070061 void BmpBlockUtil::force_compression(uint32_t compression) {
62 compression_ = compression;
63 set_compression_ = true;
64 }
Bill Richardsond55085d2011-02-04 15:01:37 -080065
Bill Richardson54e95822011-05-05 15:12:10 -070066 void BmpBlockUtil::load_from_config(const char *filename) {
67 load_yaml_config(filename);
68 fill_bmpblock_header();
69 load_all_image_files();
70 }
71
72 void BmpBlockUtil::load_yaml_config(const char *filename) {
73 yaml_parser_t parser;
74
75 config_.config_filename = filename;
76 config_.images_map.clear();
77 config_.screens_map.clear();
78 config_.localizations.clear();
Bill Richardson8ba3d792011-05-18 18:25:31 -070079 config_.locale_names.clear();
Bill Richardson54e95822011-05-05 15:12:10 -070080
81 FILE *fp = fopen(filename, "rb");
82 if (!fp) {
83 perror(filename);
84 exit(errno);
85 }
86
87 yaml_parser_initialize(&parser);
88 yaml_parser_set_input_file(&parser, fp);
89 parse_config(&parser);
90 yaml_parser_delete(&parser);
91 fclose(fp);
92
93
Bill Richardson2e022632011-08-22 14:54:14 -070094 // TODO: Check the yaml file for self-consistency. Warn on any problems.
Bill Richardson54e95822011-05-05 15:12:10 -070095 // All images should be used somewhere in the screens.
96 // All images referenced in the screens should be defined.
97 // All screens should be used somewhere in the localizations.
98 // All screens referenced in the localizations should be defined.
Bill Richardson8ba3d792011-05-18 18:25:31 -070099 // The number of localizations should match the number of locale_index
Bill Richardson54e95822011-05-05 15:12:10 -0700100
101 if (debug_) {
102 printf("%ld image_names\n", config_.image_names.size());
103 for (unsigned int i = 0; i < config_.image_names.size(); ++i) {
104 printf(" %d: \"%s\"\n", i, config_.image_names[i].c_str());
105 }
106 printf("%ld images_map\n", config_.images_map.size());
107 for (StrImageConfigMap::iterator it = config_.images_map.begin();
108 it != config_.images_map.end();
109 ++it) {
Bill Richardson0a9977e2011-08-22 16:03:59 -0700110 printf(" \"%s\": filename=\"%s\" offset=0x%x tag=%d fmt=%d\n",
Bill Richardson54e95822011-05-05 15:12:10 -0700111 it->first.c_str(),
112 it->second.filename.c_str(),
113 it->second.offset,
Bill Richardson0a9977e2011-08-22 16:03:59 -0700114 it->second.data.tag,
115 it->second.data.format);
Bill Richardson54e95822011-05-05 15:12:10 -0700116 }
117 printf("%ld screens_map\n", config_.screens_map.size());
118 for (StrScreenConfigMap::iterator it = config_.screens_map.begin();
119 it != config_.screens_map.end();
120 ++it) {
121 printf(" \"%s\":\n", it->first.c_str());
122 for (int k=0; k<MAX_IMAGE_IN_LAYOUT; k++) {
123 printf(" %d: \"%s\" (%d,%d) ofs=0x%x\n",
124 k,
125 it->second.image_names[k].c_str(),
126 it->second.data.images[k].x,
127 it->second.data.images[k].y,
128 it->second.data.images[k].image_info_offset);
129 }
130 }
131 }
132 }
133
134 void BmpBlockUtil::expect_event(yaml_parser_t *parser,
135 const yaml_event_type_e type) {
136 yaml_event_t event;
Bill Richardsond55085d2011-02-04 15:01:37 -0800137 yaml_parser_parse(parser, &event);
Bill Richardson54e95822011-05-05 15:12:10 -0700138 if (event.type != type) {
139 error("Syntax error.\n");
140 }
141 yaml_event_delete(&event);
142 }
143
144 void BmpBlockUtil::parse_config(yaml_parser_t *parser) {
145 expect_event(parser, YAML_STREAM_START_EVENT);
146 expect_event(parser, YAML_DOCUMENT_START_EVENT);
147 parse_first_layer(parser);
148 expect_event(parser, YAML_DOCUMENT_END_EVENT);
149 expect_event(parser, YAML_STREAM_END_EVENT);
150 }
151
152 void BmpBlockUtil::parse_first_layer(yaml_parser_t *parser) {
153 yaml_event_t event;
154 string keyword;
155 expect_event(parser, YAML_MAPPING_START_EVENT);
156 for (;;) {
157 yaml_parser_parse(parser, &event);
158 switch (event.type) {
Bill Richardsond55085d2011-02-04 15:01:37 -0800159 case YAML_SCALAR_EVENT:
160 keyword = (char*)event.data.scalar.value;
161 if (keyword == "bmpblock") {
162 parse_bmpblock(parser);
Bill Richardsona7209ee2011-02-17 14:30:14 -0800163 } else if (keyword == "compression") {
164 parse_compression(parser);
Bill Richardsond55085d2011-02-04 15:01:37 -0800165 } else if (keyword == "images") {
166 parse_images(parser);
167 } else if (keyword == "screens") {
168 parse_screens(parser);
169 } else if (keyword == "localizations") {
170 parse_localizations(parser);
Bill Richardson8ba3d792011-05-18 18:25:31 -0700171 } else if (keyword == "locale_index") {
172 parse_locale_index(parser);
Bill Richardsond55085d2011-02-04 15:01:37 -0800173 }
174 break;
175 case YAML_MAPPING_END_EVENT:
176 yaml_event_delete(&event);
177 return;
178 default:
179 error("Syntax error in parsing config file.\n");
Bill Richardson54e95822011-05-05 15:12:10 -0700180 }
181 yaml_event_delete(&event);
182 }
183 }
184
185 void BmpBlockUtil::parse_bmpblock(yaml_parser_t *parser) {
186 yaml_event_t event;
187 yaml_parser_parse(parser, &event);
188 if (event.type != YAML_SCALAR_EVENT) {
189 error("Syntax error in parsing bmpblock.\n");
190 }
191 string gotversion = (char*)event.data.scalar.value;
Dave Parker3985f942012-08-01 08:16:38 -0700192 if (gotversion != "2.0") {
Bill Richardson54e95822011-05-05 15:12:10 -0700193 error("Unsupported version specified in config file (%s)\n",
194 gotversion.c_str());
Bill Richardsond55085d2011-02-04 15:01:37 -0800195 }
196 yaml_event_delete(&event);
197 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800198
Bill Richardson54e95822011-05-05 15:12:10 -0700199 void BmpBlockUtil::parse_compression(yaml_parser_t *parser) {
200 yaml_event_t event;
Bill Richardsond55085d2011-02-04 15:01:37 -0800201 yaml_parser_parse(parser, &event);
Bill Richardson54e95822011-05-05 15:12:10 -0700202 if (event.type != YAML_SCALAR_EVENT) {
203 error("Syntax error in parsing bmpblock.\n");
204 }
205 char *comp_str = (char *)event.data.scalar.value;
206 char *e = 0;
207 uint32_t comp = (uint32_t)strtoul(comp_str, &e, 0);
208 if (!*comp_str || (e && *e) || comp >= MAX_COMPRESS) {
209 error("Invalid compression specified in config file (%d)\n", comp);
210 }
211 if (!set_compression_) {
212 compression_ = comp;
213 }
214 yaml_event_delete(&event);
215 }
216
217 void BmpBlockUtil::parse_images(yaml_parser_t *parser) {
218 yaml_event_t event;
219 string image_name, image_filename;
220 expect_event(parser, YAML_MAPPING_START_EVENT);
221 for (;;) {
222 yaml_parser_parse(parser, &event);
223 switch (event.type) {
Bill Richardsond55085d2011-02-04 15:01:37 -0800224 case YAML_SCALAR_EVENT:
225 image_name = (char*)event.data.scalar.value;
226 yaml_event_delete(&event);
227 yaml_parser_parse(parser, &event);
228 if (event.type != YAML_SCALAR_EVENT) {
229 error("Syntax error in parsing images.\n");
230 }
231 image_filename = (char*)event.data.scalar.value;
Bill Richardsonf82f9412011-02-17 08:56:33 -0800232 config_.image_names.push_back(image_name);
Bill Richardsond55085d2011-02-04 15:01:37 -0800233 config_.images_map[image_name] = ImageConfig();
234 config_.images_map[image_name].filename = image_filename;
Bill Richardson0a9977e2011-08-22 16:03:59 -0700235 if (image_name == RENDER_HWID) {
236 got_font_ = true;
237 }
238 if (image_name == RENDER_HWID_RTOL) {
239 got_rtol_font_ = true;
240 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800241 break;
242 case YAML_MAPPING_END_EVENT:
243 yaml_event_delete(&event);
244 return;
245 default:
246 error("Syntax error in parsing images.\n");
Bill Richardson54e95822011-05-05 15:12:10 -0700247 }
248 yaml_event_delete(&event);
Bill Richardsond55085d2011-02-04 15:01:37 -0800249 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800250 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800251
Bill Richardson54e95822011-05-05 15:12:10 -0700252 void BmpBlockUtil::parse_layout(yaml_parser_t *parser, ScreenConfig &screen) {
253 yaml_event_t event;
254 int depth = 0, index1 = 0, index2 = 0;
255 expect_event(parser, YAML_SEQUENCE_START_EVENT);
256 for (;;) {
257 yaml_parser_parse(parser, &event);
258 switch (event.type) {
Bill Richardsond55085d2011-02-04 15:01:37 -0800259 case YAML_SEQUENCE_START_EVENT:
260 depth++;
261 break;
262 case YAML_SCALAR_EVENT:
263 switch (index2) {
Bill Richardson54e95822011-05-05 15:12:10 -0700264 case 0:
265 screen.data.images[index1].x = atoi((char*)event.data.scalar.value);
266 break;
267 case 1:
268 screen.data.images[index1].y = atoi((char*)event.data.scalar.value);
269 break;
270 case 2:
271 screen.image_names[index1] = (char*)event.data.scalar.value;
272 // Detect the special case where we're rendering the HWID string
Bill Richardson0a9977e2011-08-22 16:03:59 -0700273 // instead of displaying a bitmap. The image name may not
274 // exist in the list of images (v1.1), but we will still need an
Bill Richardson54e95822011-05-05 15:12:10 -0700275 // ImageInfo struct to remember where to draw the text.
Bill Richardson0a9977e2011-08-22 16:03:59 -0700276 // Note that v1.2 requires that the image name DOES exist, because
277 // the corresponding file is used to hold the font glpyhs.
Bill Richardson54e95822011-05-05 15:12:10 -0700278 if (render_hwid_) {
279 if (screen.image_names[index1] == RENDER_HWID) {
280 config_.images_map[RENDER_HWID].data.tag = TAG_HWID;
Bill Richardson0a9977e2011-08-22 16:03:59 -0700281 if (support_font_ && !got_font_)
282 error("Font required in 'image:' section for %s\n",
283 RENDER_HWID);
Bill Richardson54e95822011-05-05 15:12:10 -0700284 } else if (screen.image_names[index1] == RENDER_HWID_RTOL) {
285 config_.images_map[RENDER_HWID_RTOL].data.tag = TAG_HWID_RTOL;
Bill Richardson0a9977e2011-08-22 16:03:59 -0700286 if (support_font_ && !got_rtol_font_)
287 error("Font required in 'image:' section for %s\n",
288 RENDER_HWID_RTOL);
Bill Richardson54e95822011-05-05 15:12:10 -0700289 }
290 }
291 break;
292 default:
293 error("Syntax error in parsing layout\n");
Bill Richardsond55085d2011-02-04 15:01:37 -0800294 }
295 index2++;
296 break;
297 case YAML_SEQUENCE_END_EVENT:
298 if (depth == 1) {
299 index1++;
300 index2 = 0;
301 } else if (depth == 0) {
302 yaml_event_delete(&event);
303 return;
304 }
305 depth--;
306 break;
307 default:
308 error("Syntax error in paring layout.\n");
Bill Richardson54e95822011-05-05 15:12:10 -0700309 }
310 yaml_event_delete(&event);
Bill Richardsond55085d2011-02-04 15:01:37 -0800311 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800312 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800313
Bill Richardson54e95822011-05-05 15:12:10 -0700314 void BmpBlockUtil::parse_screens(yaml_parser_t *parser) {
315 yaml_event_t event;
316 string screen_name;
317 expect_event(parser, YAML_MAPPING_START_EVENT);
318 for (;;) {
319 yaml_parser_parse(parser, &event);
320 switch (event.type) {
Bill Richardsond55085d2011-02-04 15:01:37 -0800321 case YAML_SCALAR_EVENT:
322 screen_name = (char*)event.data.scalar.value;
323 config_.screens_map[screen_name] = ScreenConfig();
324 parse_layout(parser, config_.screens_map[screen_name]);
325 break;
326 case YAML_MAPPING_END_EVENT:
327 yaml_event_delete(&event);
328 return;
329 default:
330 error("Syntax error in parsing screens.\n");
Bill Richardson54e95822011-05-05 15:12:10 -0700331 }
332 yaml_event_delete(&event);
Bill Richardsond55085d2011-02-04 15:01:37 -0800333 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800334 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800335
Bill Richardson54e95822011-05-05 15:12:10 -0700336 void BmpBlockUtil::parse_localizations(yaml_parser_t *parser) {
337 yaml_event_t event;
338 int depth = 0, index = 0;
339 expect_event(parser, YAML_SEQUENCE_START_EVENT);
340 for (;;) {
341 yaml_parser_parse(parser, &event);
342 switch (event.type) {
Bill Richardsond55085d2011-02-04 15:01:37 -0800343 case YAML_SEQUENCE_START_EVENT:
344 config_.localizations.push_back(vector<string>());
345 depth++;
346 break;
347 case YAML_SCALAR_EVENT:
348 config_.localizations[index].push_back((char*)event.data.scalar.value);
349 break;
350 case YAML_SEQUENCE_END_EVENT:
351 if (depth == 1) {
352 index++;
353 } else if (depth == 0) {
354 yaml_event_delete(&event);
355 return;
356 }
357 depth--;
358 break;
359 default:
360 error("Syntax error in parsing localizations.\n");
Bill Richardson54e95822011-05-05 15:12:10 -0700361 }
362 yaml_event_delete(&event);
Bill Richardsond55085d2011-02-04 15:01:37 -0800363 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800364 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800365
Bill Richardson8ba3d792011-05-18 18:25:31 -0700366 void BmpBlockUtil::parse_locale_index(yaml_parser_t *parser) {
367 yaml_event_t event;
368 expect_event(parser, YAML_SEQUENCE_START_EVENT);
369 for (;;) {
370 yaml_parser_parse(parser, &event);
371 switch (event.type) {
372 case YAML_SCALAR_EVENT:
373 config_.locale_names.append((char*)event.data.scalar.value);
374 config_.locale_names.append(1, (char)'\0'); // '\0' to delimit
375 break;
376 case YAML_SEQUENCE_END_EVENT:
377 yaml_event_delete(&event);
378 config_.locale_names.append(1, (char)'\0'); // double '\0' to terminate
379 return;
380 default:
381 error("Syntax error in parsing localizations.\n");
382 }
383 }
384 }
385
Bill Richardson54e95822011-05-05 15:12:10 -0700386 void BmpBlockUtil::load_all_image_files() {
387 for (unsigned int i = 0; i < config_.image_names.size(); i++) {
388 StrImageConfigMap::iterator it =
389 config_.images_map.find(config_.image_names[i]);
390 if (debug_) {
391 printf("loading image \"%s\" from \"%s\"\n",
392 config_.image_names[i].c_str(),
393 it->second.filename.c_str());
394 }
395 const string &content = read_image_file(it->second.filename.c_str());
396 it->second.raw_content = content;
397 it->second.data.original_size = content.size();
Bill Richardson0a9977e2011-08-22 16:03:59 -0700398 it->second.data.format =
399 identify_image_type(content.c_str(),
400 (uint32_t)content.size(), &it->second.data);
401 if (FORMAT_INVALID == it->second.data.format) {
Bill Richardson54e95822011-05-05 15:12:10 -0700402 error("Unsupported image format in %s\n", it->second.filename.c_str());
403 }
404 switch(compression_) {
405 case COMPRESS_NONE:
406 it->second.data.compression = compression_;
407 it->second.compressed_content = content;
408 it->second.data.compressed_size = content.size();
409 break;
410 case COMPRESS_EFIv1:
Bill Richardson61362d62011-02-14 10:28:03 -0800411 {
412 // The content will always compress smaller (so sez the docs).
413 uint32_t tmpsize = content.size();
414 uint8_t *tmpbuf = (uint8_t *)malloc(tmpsize);
415 // The size of the compressed content is also returned.
416 if (EFI_SUCCESS != EfiCompress((uint8_t *)content.c_str(), tmpsize,
417 tmpbuf, &tmpsize)) {
418 error("Unable to compress!\n");
419 }
420 it->second.data.compression = compression_;
421 it->second.compressed_content.assign((const char *)tmpbuf, tmpsize);
422 it->second.data.compressed_size = tmpsize;
423 free(tmpbuf);
424 }
425 break;
Bill Richardson54e95822011-05-05 15:12:10 -0700426 case COMPRESS_LZMA1:
Tom Wai-Hong Tamee2bc912011-02-17 12:58:58 +0800427 {
428 // Calculate the worst case of buffer size.
429 uint32_t tmpsize = lzma_stream_buffer_bound(content.size());
430 uint8_t *tmpbuf = (uint8_t *)malloc(tmpsize);
431 lzma_stream stream = LZMA_STREAM_INIT;
432 lzma_options_lzma options;
433 lzma_ret result;
434
435 lzma_lzma_preset(&options, 9);
436 result = lzma_alone_encoder(&stream, &options);
437 if (result != LZMA_OK) {
438 error("Unable to initialize easy encoder (error: %d)!\n", result);
439 }
440
441 stream.next_in = (uint8_t *)content.data();
442 stream.avail_in = content.size();
443 stream.next_out = tmpbuf;
444 stream.avail_out = tmpsize;
445 result = lzma_code(&stream, LZMA_FINISH);
446 if (result != LZMA_STREAM_END) {
447 error("Unable to encode data (error: %d)!\n", result);
448 }
449
450 it->second.data.compression = compression_;
451 it->second.compressed_content.assign((const char *)tmpbuf,
452 tmpsize - stream.avail_out);
453 it->second.data.compressed_size = tmpsize - stream.avail_out;
454 lzma_end(&stream);
455 free(tmpbuf);
456 }
457 break;
Bill Richardsond55085d2011-02-04 15:01:37 -0800458 default:
Bill Richardson54e95822011-05-05 15:12:10 -0700459 error("Unsupported compression method attempted.\n");
Bill Richardsond55085d2011-02-04 15:01:37 -0800460 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800461 }
462 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800463
Bill Richardson54e95822011-05-05 15:12:10 -0700464 const string BmpBlockUtil::read_image_file(const char *filename) {
465 string content;
466 vector<char> buffer;
Bill Richardsond55085d2011-02-04 15:01:37 -0800467
Bill Richardson54e95822011-05-05 15:12:10 -0700468 FILE *fp = fopen(filename, "rb");
469 if (!fp) {
470 perror(filename);
471 exit(errno);
472 }
473
474 if (fseek(fp, 0, SEEK_END) == 0) {
475 buffer.resize(ftell(fp));
476 rewind(fp);
477 }
478
479 if (!buffer.empty()) {
480 if(fread(&buffer[0], buffer.size(), 1, fp) != 1) {
481 perror(filename);
482 buffer.clear();
483 } else {
484 content.assign(buffer.begin(), buffer.end());
485 }
486 }
487
488 fclose(fp);
489 return content;
490 }
491
Bill Richardson54e95822011-05-05 15:12:10 -0700492 void BmpBlockUtil::fill_bmpblock_header() {
493 memset(&config_.header, '\0', sizeof(config_.header));
494 memcpy(&config_.header.signature, BMPBLOCK_SIGNATURE,
495 BMPBLOCK_SIGNATURE_SIZE);
496 config_.header.major_version = major_version_;
497 config_.header.minor_version = minor_version_;
498 config_.header.number_of_localizations = config_.localizations.size();
499 config_.header.number_of_screenlayouts = config_.localizations[0].size();
500 // HEY: this is part of the yaml consistency check
501 for (unsigned int i = 1; i < config_.localizations.size(); ++i) {
502 assert(config_.header.number_of_screenlayouts ==
503 config_.localizations[i].size());
504 }
505 config_.header.number_of_imageinfos = config_.images_map.size();
Bill Richardson8ba3d792011-05-18 18:25:31 -0700506 config_.header.locale_string_offset = 0; // Filled by pack_bmpblock()
Bill Richardson54e95822011-05-05 15:12:10 -0700507 }
508
509 void BmpBlockUtil::pack_bmpblock() {
510 bmpblock_.clear();
511
512 /* Compute the ImageInfo offsets from start of BMPBLOCK. */
513 uint32_t current_offset = sizeof(BmpBlockHeader) +
514 sizeof(ScreenLayout) * (config_.header.number_of_localizations *
515 config_.header.number_of_screenlayouts);
516 for (StrImageConfigMap::iterator it = config_.images_map.begin();
517 it != config_.images_map.end();
518 ++it) {
519 it->second.offset = current_offset;
520 if (debug_)
Bill Richardson0a9977e2011-08-22 16:03:59 -0700521 printf(" \"%s\": filename=\"%s\" offset=0x%x tag=%d fmt=%d\n",
Bill Richardson54e95822011-05-05 15:12:10 -0700522 it->first.c_str(),
523 it->second.filename.c_str(),
524 it->second.offset,
Bill Richardson0a9977e2011-08-22 16:03:59 -0700525 it->second.data.tag,
526 it->second.data.format);
Bill Richardson54e95822011-05-05 15:12:10 -0700527 current_offset += sizeof(ImageInfo) +
528 it->second.data.compressed_size;
529 /* Make it 4-byte aligned. */
530 if ((current_offset & 3) > 0) {
531 current_offset = (current_offset & ~3) + 4;
532 }
533 }
Bill Richardson8ba3d792011-05-18 18:25:31 -0700534 /* And leave room for the locale_index string */
535 if (config_.locale_names.size()) {
536 config_.header.locale_string_offset = current_offset;
537 current_offset += config_.locale_names.size();
538 }
539
Bill Richardson54e95822011-05-05 15:12:10 -0700540 bmpblock_.resize(current_offset);
541
542 /* Fill BmpBlockHeader struct. */
543 string::iterator current_filled = bmpblock_.begin();
544 std::copy(reinterpret_cast<char*>(&config_.header),
545 reinterpret_cast<char*>(&config_.header + 1),
546 current_filled);
547 current_filled += sizeof(config_.header);
548 current_offset = sizeof(config_.header);
549
550 /* Fill all ScreenLayout structs. */
551 for (unsigned int i = 0; i < config_.localizations.size(); ++i) {
552 for (unsigned int j = 0; j < config_.localizations[i].size(); ++j) {
553 ScreenConfig &screen = config_.screens_map[config_.localizations[i][j]];
554 for (unsigned int k = 0;
555 k < MAX_IMAGE_IN_LAYOUT && !screen.image_names[k].empty();
556 ++k) {
557 if (config_.images_map.find(screen.image_names[k]) ==
558 config_.images_map.end()) {
559 error("Invalid image name \"%s\"\n", screen.image_names[k].c_str());
560 }
561 if (debug_)
562 printf("i=%d j=%d k=%d=\"%s\" (%d,%d) ofs=%x\n", i,j,k,
563 screen.image_names[k].c_str(),
564 screen.data.images[k].x, screen.data.images[k].y,
565 config_.images_map[screen.image_names[k]].offset
566 );
567 screen.data.images[k].image_info_offset =
Bill Richardsond55085d2011-02-04 15:01:37 -0800568 config_.images_map[screen.image_names[k]].offset;
Bill Richardson54e95822011-05-05 15:12:10 -0700569 }
570 std::copy(reinterpret_cast<char*>(&screen.data),
571 reinterpret_cast<char*>(&screen.data + 1),
572 current_filled);
573 current_filled += sizeof(screen.data);
574 if (debug_)
575 printf("S: current offset is 0x%08x\n", current_offset);
576 current_offset += sizeof(screen.data);
Bill Richardsond55085d2011-02-04 15:01:37 -0800577 }
Bill Richardson54e95822011-05-05 15:12:10 -0700578 }
579
580 /* Fill all ImageInfo structs and image contents. */
581 for (StrImageConfigMap::iterator it = config_.images_map.begin();
582 it != config_.images_map.end();
583 ++it) {
584 current_filled = bmpblock_.begin() + it->second.offset;
585 current_offset = it->second.offset;
586 if (debug_)
587 printf("I0: current offset is 0x%08x\n", current_offset);
588 std::copy(reinterpret_cast<char*>(&it->second.data),
589 reinterpret_cast<char*>(&it->second.data + 1),
Bill Richardsond55085d2011-02-04 15:01:37 -0800590 current_filled);
Bill Richardson54e95822011-05-05 15:12:10 -0700591 current_filled += sizeof(it->second.data);
592 current_offset += sizeof(it->second.data);
593 if (debug_)
594 printf("I1: current offset is 0x%08x (len %ld)\n",
595 current_offset, it->second.compressed_content.length());
596 std::copy(it->second.compressed_content.begin(),
597 it->second.compressed_content.end(),
598 current_filled);
Bill Richardsond55085d2011-02-04 15:01:37 -0800599 }
Bill Richardson8ba3d792011-05-18 18:25:31 -0700600
601 /* Fill in locale_names. */
602 if (config_.header.locale_string_offset) {
603 current_offset = config_.header.locale_string_offset;
604 current_filled = bmpblock_.begin() + current_offset;
605 if (debug_)
606 printf("locale_names: offset 0x%08x (len %ld)\n",
607 current_offset, config_.locale_names.size());
608 std::copy(config_.locale_names.begin(),
609 config_.locale_names.end(),
610 current_filled);
611 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800612 }
613
Bill Richardson54e95822011-05-05 15:12:10 -0700614 void BmpBlockUtil::write_to_bmpblock(const char *filename) {
615 assert(!bmpblock_.empty());
Bill Richardsond55085d2011-02-04 15:01:37 -0800616
Bill Richardson54e95822011-05-05 15:12:10 -0700617 FILE *fp = fopen(filename, "wb");
618 if (!fp) {
619 perror(filename);
620 exit(errno);
621 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800622
Bill Richardson54e95822011-05-05 15:12:10 -0700623 int r = fwrite(bmpblock_.c_str(), bmpblock_.size(), 1, fp);
624 fclose(fp);
625 if (r != 1) {
626 perror(filename);
627 exit(errno);
628 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800629 }
630
Bill Richardsond55085d2011-02-04 15:01:37 -0800631} // namespace vboot_reference
632
Randall Spangler17f8d342013-01-11 10:55:11 -0800633#ifndef FOR_LIBRARY
Bill Richardsond55085d2011-02-04 15:01:37 -0800634
Bill Richardson54e95822011-05-05 15:12:10 -0700635 //////////////////////////////////////////////////////////////////////////////
636 // Command line utilities.
Bill Richardson794d4d42011-02-10 19:13:10 -0800637
Bill Richardson54e95822011-05-05 15:12:10 -0700638 extern "C" {
Bill Richardson794d4d42011-02-10 19:13:10 -0800639#include "bmpblk_util.h"
Bill Richardson54e95822011-05-05 15:12:10 -0700640 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800641
Bill Richardson54e95822011-05-05 15:12:10 -0700642 using vboot_reference::BmpBlockUtil;
Bill Richardsond55085d2011-02-04 15:01:37 -0800643
Bill Richardson54e95822011-05-05 15:12:10 -0700644 // utility function: provide usage of this utility and exit.
645 static void usagehelp_exit(const char *prog_name) {
646 printf(
647 "\n"
648 "To create a new BMPBLOCK file using config from YAML file:\n"
649 "\n"
650 " %s [-z NUM] -c YAML BMPBLOCK\n"
651 "\n"
652 " -z NUM = compression algorithm to use\n"
653 " 0 = none\n"
654 " 1 = EFIv1\n"
655 " 2 = LZMA1\n"
656 "\n", prog_name);
657 printf(
658 "To display the contents of a BMPBLOCK:\n"
659 "\n"
660 " %s [-y] BMPBLOCK\n"
661 "\n"
662 " -y = display as yaml\n"
663 "\n", prog_name);
664 printf(
665 "To unpack a BMPBLOCK file:\n"
666 "\n"
667 " %s -x [-d DIR] [-f] BMPBLOCK\n"
668 "\n"
669 " -d DIR = directory to use (default '.')\n"
670 " -f = force overwriting existing files\n"
671 "\n", prog_name);
672 exit(1);
673 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800674
Bill Richardson54e95822011-05-05 15:12:10 -0700675 ///////////////////////////////////////////////////////////////////////
676 // main
Bill Richardsond55085d2011-02-04 15:01:37 -0800677
Bill Richardson54e95822011-05-05 15:12:10 -0700678 int main(int argc, char *argv[]) {
Bill Richardsond55085d2011-02-04 15:01:37 -0800679
Bill Richardson54e95822011-05-05 15:12:10 -0700680 const char *prog_name = strrchr(argv[0], '/');
681 if (prog_name)
682 prog_name++;
683 else
684 prog_name = argv[0];
Bill Richardsond55085d2011-02-04 15:01:37 -0800685
Bill Richardson54e95822011-05-05 15:12:10 -0700686 int overwrite = 0, extract_mode = 0;
687 int compression = 0;
688 int set_compression = 0;
689 const char *config_fn = 0, *bmpblock_fn = 0, *extract_dir = ".";
690 int show_as_yaml = 0;
691 bool debug = false;
Bill Richardsond55085d2011-02-04 15:01:37 -0800692
Bill Richardson54e95822011-05-05 15:12:10 -0700693 int opt;
694 opterr = 0; // quiet
695 int errorcnt = 0;
696 char *e = 0;
697 while ((opt = getopt(argc, argv, ":c:xz:fd:yD")) != -1) {
698 switch (opt) {
699 case 'c':
700 config_fn = optarg;
701 break;
702 case 'x':
703 extract_mode = 1;
704 break;
705 case 'y':
706 show_as_yaml = 1;
707 break;
708 case 'z':
709 compression = (int)strtoul(optarg, &e, 0);
710 if (!*optarg || (e && *e)) {
711 fprintf(stderr, "%s: invalid argument to -%c: \"%s\"\n",
712 prog_name, opt, optarg);
713 errorcnt++;
714 }
715 if (compression >= MAX_COMPRESS) {
716 fprintf(stderr, "%s: compression type must be less than %d\n",
717 prog_name, MAX_COMPRESS);
718 errorcnt++;
719 }
720 set_compression = 1;
721 break;
722 case 'f':
723 overwrite = 1;
724 break;
725 case 'd':
726 extract_dir= optarg;
727 break;
728 case 'D':
729 debug = true;
730 break;
731 case ':':
732 fprintf(stderr, "%s: missing argument to -%c\n",
733 prog_name, optopt);
Bill Richardson794d4d42011-02-10 19:13:10 -0800734 errorcnt++;
Bill Richardson54e95822011-05-05 15:12:10 -0700735 break;
736 default:
737 fprintf(stderr, "%s: unrecognized switch: -%c\n",
738 prog_name, optopt);
Bill Richardson794d4d42011-02-10 19:13:10 -0800739 errorcnt++;
Bill Richardson54e95822011-05-05 15:12:10 -0700740 break;
Bill Richardson794d4d42011-02-10 19:13:10 -0800741 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800742 }
Bill Richardson54e95822011-05-05 15:12:10 -0700743 argc -= optind;
744 argv += optind;
745
746 if (argc >= 1) {
747 bmpblock_fn = argv[0];
748 } else {
749 fprintf(stderr, "%s: missing BMPBLOCK name\n", prog_name);
750 errorcnt++;
751 }
752
753 if (errorcnt)
754 usagehelp_exit(prog_name);
755
756 BmpBlockUtil util(debug);
757
758 if (config_fn) {
759 if (set_compression)
760 util.force_compression(compression);
761 util.load_from_config(config_fn);
762 util.pack_bmpblock();
763 util.write_to_bmpblock(bmpblock_fn);
764 }
765
766 else if (extract_mode) {
767 return dump_bmpblock(bmpblock_fn, 1, extract_dir, overwrite);
768 } else {
769 return dump_bmpblock(bmpblock_fn, show_as_yaml, 0, 0);
770 }
771
772 return 0;
Bill Richardsond55085d2011-02-04 15:01:37 -0800773 }
Bill Richardsond55085d2011-02-04 15:01:37 -0800774
Randall Spangler17f8d342013-01-11 10:55:11 -0800775#endif // FOR_LIBRARY