bmpblk_utility.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. // 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. #include <assert.h>
  8. #include <errno.h>
  9. #include <getopt.h>
  10. #include <lzma.h>
  11. #include <stdarg.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <yaml.h>
  17. #include "bmpblk_utility.h"
  18. #include "image_types.h"
  19. #include "vboot_api.h"
  20. extern "C" {
  21. #include "eficompress.h"
  22. }
  23. static void error(const char *format, ...) {
  24. va_list ap;
  25. va_start(ap, format);
  26. fprintf(stderr, "ERROR: ");
  27. vfprintf(stderr, format, ap);
  28. va_end(ap);
  29. exit(1);
  30. }
  31. ///////////////////////////////////////////////////////////////////////
  32. // BmpBlock Utility implementation
  33. namespace vboot_reference {
  34. BmpBlockUtil::BmpBlockUtil(bool debug) {
  35. major_version_ = BMPBLOCK_MAJOR_VERSION;
  36. minor_version_ = BMPBLOCK_MINOR_VERSION;
  37. config_.config_filename.clear();
  38. memset(&config_.header, '\0', BMPBLOCK_SIGNATURE_SIZE);
  39. config_.images_map.clear();
  40. config_.screens_map.clear();
  41. config_.localizations.clear();
  42. bmpblock_.clear();
  43. set_compression_ = false;
  44. compression_ = COMPRESS_NONE;
  45. debug_ = debug;
  46. render_hwid_ = true;
  47. support_font_ = true;
  48. got_font_ = false;
  49. got_rtol_font_ = false;
  50. }
  51. BmpBlockUtil::~BmpBlockUtil() {
  52. }
  53. void BmpBlockUtil::force_compression(uint32_t compression) {
  54. compression_ = compression;
  55. set_compression_ = true;
  56. }
  57. void BmpBlockUtil::load_from_config(const char *filename) {
  58. load_yaml_config(filename);
  59. fill_bmpblock_header();
  60. load_all_image_files();
  61. }
  62. void BmpBlockUtil::load_yaml_config(const char *filename) {
  63. yaml_parser_t parser;
  64. config_.config_filename = filename;
  65. config_.images_map.clear();
  66. config_.screens_map.clear();
  67. config_.localizations.clear();
  68. config_.locale_names.clear();
  69. FILE *fp = fopen(filename, "rb");
  70. if (!fp) {
  71. perror(filename);
  72. exit(errno);
  73. }
  74. yaml_parser_initialize(&parser);
  75. yaml_parser_set_input_file(&parser, fp);
  76. parse_config(&parser);
  77. yaml_parser_delete(&parser);
  78. fclose(fp);
  79. // TODO: Check the yaml file for self-consistency. Warn on any problems.
  80. // All images should be used somewhere in the screens.
  81. // All images referenced in the screens should be defined.
  82. // All screens should be used somewhere in the localizations.
  83. // All screens referenced in the localizations should be defined.
  84. // The number of localizations should match the number of locale_index
  85. if (debug_) {
  86. printf("%zd image_names\n", config_.image_names.size());
  87. for (unsigned int i = 0; i < config_.image_names.size(); ++i) {
  88. printf(" %d: \"%s\"\n", i, config_.image_names[i].c_str());
  89. }
  90. printf("%zd images_map\n", config_.images_map.size());
  91. for (StrImageConfigMap::iterator it = config_.images_map.begin();
  92. it != config_.images_map.end();
  93. ++it) {
  94. printf(" \"%s\": filename=\"%s\" offset=0x%x tag=%d fmt=%d\n",
  95. it->first.c_str(),
  96. it->second.filename.c_str(),
  97. it->second.offset,
  98. it->second.data.tag,
  99. it->second.data.format);
  100. }
  101. printf("%zd screens_map\n", config_.screens_map.size());
  102. for (StrScreenConfigMap::iterator it = config_.screens_map.begin();
  103. it != config_.screens_map.end();
  104. ++it) {
  105. printf(" \"%s\":\n", it->first.c_str());
  106. for (int k=0; k<MAX_IMAGE_IN_LAYOUT; k++) {
  107. printf(" %d: \"%s\" (%d,%d) ofs=0x%x\n",
  108. k,
  109. it->second.image_names[k].c_str(),
  110. it->second.data.images[k].x,
  111. it->second.data.images[k].y,
  112. it->second.data.images[k].image_info_offset);
  113. }
  114. }
  115. }
  116. }
  117. void BmpBlockUtil::expect_event(yaml_parser_t *parser,
  118. const yaml_event_type_e type) {
  119. yaml_event_t event;
  120. yaml_parser_parse(parser, &event);
  121. if (event.type != type) {
  122. error("Syntax error.\n");
  123. }
  124. yaml_event_delete(&event);
  125. }
  126. void BmpBlockUtil::parse_config(yaml_parser_t *parser) {
  127. expect_event(parser, YAML_STREAM_START_EVENT);
  128. expect_event(parser, YAML_DOCUMENT_START_EVENT);
  129. parse_first_layer(parser);
  130. expect_event(parser, YAML_DOCUMENT_END_EVENT);
  131. expect_event(parser, YAML_STREAM_END_EVENT);
  132. }
  133. void BmpBlockUtil::parse_first_layer(yaml_parser_t *parser) {
  134. yaml_event_t event;
  135. string keyword;
  136. expect_event(parser, YAML_MAPPING_START_EVENT);
  137. for (;;) {
  138. yaml_parser_parse(parser, &event);
  139. switch (event.type) {
  140. case YAML_SCALAR_EVENT:
  141. keyword = (char*)event.data.scalar.value;
  142. if (keyword == "bmpblock") {
  143. parse_bmpblock(parser);
  144. } else if (keyword == "compression") {
  145. parse_compression(parser);
  146. } else if (keyword == "images") {
  147. parse_images(parser);
  148. } else if (keyword == "screens") {
  149. parse_screens(parser);
  150. } else if (keyword == "localizations") {
  151. parse_localizations(parser);
  152. } else if (keyword == "locale_index") {
  153. parse_locale_index(parser);
  154. }
  155. break;
  156. case YAML_MAPPING_END_EVENT:
  157. yaml_event_delete(&event);
  158. return;
  159. default:
  160. error("Syntax error in parsing config file.\n");
  161. }
  162. yaml_event_delete(&event);
  163. }
  164. }
  165. void BmpBlockUtil::parse_bmpblock(yaml_parser_t *parser) {
  166. yaml_event_t event;
  167. yaml_parser_parse(parser, &event);
  168. if (event.type != YAML_SCALAR_EVENT) {
  169. error("Syntax error in parsing bmpblock.\n");
  170. }
  171. string gotversion = (char*)event.data.scalar.value;
  172. if (gotversion != "2.0") {
  173. error("Unsupported version specified in config file (%s)\n",
  174. gotversion.c_str());
  175. }
  176. yaml_event_delete(&event);
  177. }
  178. void BmpBlockUtil::parse_compression(yaml_parser_t *parser) {
  179. yaml_event_t event;
  180. yaml_parser_parse(parser, &event);
  181. if (event.type != YAML_SCALAR_EVENT) {
  182. error("Syntax error in parsing bmpblock.\n");
  183. }
  184. char *comp_str = (char *)event.data.scalar.value;
  185. char *e = 0;
  186. uint32_t comp = (uint32_t)strtoul(comp_str, &e, 0);
  187. if (!*comp_str || (e && *e) || comp >= MAX_COMPRESS) {
  188. error("Invalid compression specified in config file (%d)\n", comp);
  189. }
  190. if (!set_compression_) {
  191. compression_ = comp;
  192. }
  193. yaml_event_delete(&event);
  194. }
  195. void BmpBlockUtil::parse_images(yaml_parser_t *parser) {
  196. yaml_event_t event;
  197. string image_name, image_filename;
  198. expect_event(parser, YAML_MAPPING_START_EVENT);
  199. for (;;) {
  200. yaml_parser_parse(parser, &event);
  201. switch (event.type) {
  202. case YAML_SCALAR_EVENT:
  203. image_name = (char*)event.data.scalar.value;
  204. yaml_event_delete(&event);
  205. yaml_parser_parse(parser, &event);
  206. if (event.type != YAML_SCALAR_EVENT) {
  207. error("Syntax error in parsing images.\n");
  208. }
  209. image_filename = (char*)event.data.scalar.value;
  210. config_.image_names.push_back(image_name);
  211. config_.images_map[image_name] = ImageConfig();
  212. config_.images_map[image_name].filename = image_filename;
  213. if (image_name == RENDER_HWID) {
  214. got_font_ = true;
  215. }
  216. if (image_name == RENDER_HWID_RTOL) {
  217. got_rtol_font_ = true;
  218. }
  219. break;
  220. case YAML_MAPPING_END_EVENT:
  221. yaml_event_delete(&event);
  222. return;
  223. default:
  224. error("Syntax error in parsing images.\n");
  225. }
  226. yaml_event_delete(&event);
  227. }
  228. }
  229. void BmpBlockUtil::parse_layout(yaml_parser_t *parser, ScreenConfig &screen) {
  230. yaml_event_t event;
  231. int depth = 0, index1 = 0, index2 = 0;
  232. expect_event(parser, YAML_SEQUENCE_START_EVENT);
  233. for (;;) {
  234. yaml_parser_parse(parser, &event);
  235. switch (event.type) {
  236. case YAML_SEQUENCE_START_EVENT:
  237. depth++;
  238. break;
  239. case YAML_SCALAR_EVENT:
  240. switch (index2) {
  241. case 0:
  242. screen.data.images[index1].x = atoi((char*)event.data.scalar.value);
  243. break;
  244. case 1:
  245. screen.data.images[index1].y = atoi((char*)event.data.scalar.value);
  246. break;
  247. case 2:
  248. screen.image_names[index1] = (char*)event.data.scalar.value;
  249. // Detect the special case where we're rendering the HWID string
  250. // instead of displaying a bitmap. The image name may not
  251. // exist in the list of images (v1.1), but we will still need an
  252. // ImageInfo struct to remember where to draw the text.
  253. // Note that v1.2 requires that the image name DOES exist, because
  254. // the corresponding file is used to hold the font glpyhs.
  255. if (render_hwid_) {
  256. if (screen.image_names[index1] == RENDER_HWID) {
  257. config_.images_map[RENDER_HWID].data.tag = TAG_HWID;
  258. if (support_font_ && !got_font_)
  259. error("Font required in 'image:' section for %s\n",
  260. RENDER_HWID);
  261. } else if (screen.image_names[index1] == RENDER_HWID_RTOL) {
  262. config_.images_map[RENDER_HWID_RTOL].data.tag = TAG_HWID_RTOL;
  263. if (support_font_ && !got_rtol_font_)
  264. error("Font required in 'image:' section for %s\n",
  265. RENDER_HWID_RTOL);
  266. }
  267. }
  268. break;
  269. default:
  270. error("Syntax error in parsing layout\n");
  271. }
  272. index2++;
  273. break;
  274. case YAML_SEQUENCE_END_EVENT:
  275. if (depth == 1) {
  276. index1++;
  277. index2 = 0;
  278. } else if (depth == 0) {
  279. yaml_event_delete(&event);
  280. return;
  281. }
  282. depth--;
  283. break;
  284. default:
  285. error("Syntax error in paring layout.\n");
  286. }
  287. yaml_event_delete(&event);
  288. }
  289. }
  290. void BmpBlockUtil::parse_screens(yaml_parser_t *parser) {
  291. yaml_event_t event;
  292. string screen_name;
  293. expect_event(parser, YAML_MAPPING_START_EVENT);
  294. for (;;) {
  295. yaml_parser_parse(parser, &event);
  296. switch (event.type) {
  297. case YAML_SCALAR_EVENT:
  298. screen_name = (char*)event.data.scalar.value;
  299. config_.screens_map[screen_name] = ScreenConfig();
  300. parse_layout(parser, config_.screens_map[screen_name]);
  301. break;
  302. case YAML_MAPPING_END_EVENT:
  303. yaml_event_delete(&event);
  304. return;
  305. default:
  306. error("Syntax error in parsing screens.\n");
  307. }
  308. yaml_event_delete(&event);
  309. }
  310. }
  311. void BmpBlockUtil::parse_localizations(yaml_parser_t *parser) {
  312. yaml_event_t event;
  313. int depth = 0, index = 0;
  314. expect_event(parser, YAML_SEQUENCE_START_EVENT);
  315. for (;;) {
  316. yaml_parser_parse(parser, &event);
  317. switch (event.type) {
  318. case YAML_SEQUENCE_START_EVENT:
  319. config_.localizations.push_back(vector<string>());
  320. depth++;
  321. break;
  322. case YAML_SCALAR_EVENT:
  323. config_.localizations[index].push_back((char*)event.data.scalar.value);
  324. break;
  325. case YAML_SEQUENCE_END_EVENT:
  326. if (depth == 1) {
  327. index++;
  328. } else if (depth == 0) {
  329. yaml_event_delete(&event);
  330. return;
  331. }
  332. depth--;
  333. break;
  334. default:
  335. error("Syntax error in parsing localizations.\n");
  336. }
  337. yaml_event_delete(&event);
  338. }
  339. }
  340. void BmpBlockUtil::parse_locale_index(yaml_parser_t *parser) {
  341. yaml_event_t event;
  342. expect_event(parser, YAML_SEQUENCE_START_EVENT);
  343. for (;;) {
  344. yaml_parser_parse(parser, &event);
  345. switch (event.type) {
  346. case YAML_SCALAR_EVENT:
  347. config_.locale_names.append((char*)event.data.scalar.value);
  348. config_.locale_names.append(1, (char)'\0'); // '\0' to delimit
  349. break;
  350. case YAML_SEQUENCE_END_EVENT:
  351. yaml_event_delete(&event);
  352. config_.locale_names.append(1, (char)'\0'); // double '\0' to terminate
  353. return;
  354. default:
  355. error("Syntax error in parsing localizations.\n");
  356. }
  357. }
  358. }
  359. void BmpBlockUtil::load_all_image_files() {
  360. for (unsigned int i = 0; i < config_.image_names.size(); i++) {
  361. StrImageConfigMap::iterator it =
  362. config_.images_map.find(config_.image_names[i]);
  363. if (debug_) {
  364. printf("loading image \"%s\" from \"%s\"\n",
  365. config_.image_names[i].c_str(),
  366. it->second.filename.c_str());
  367. }
  368. const string &content = read_image_file(it->second.filename.c_str());
  369. it->second.raw_content = content;
  370. it->second.data.original_size = content.size();
  371. it->second.data.format =
  372. identify_image_type(content.c_str(),
  373. (uint32_t)content.size(), &it->second.data);
  374. if (FORMAT_INVALID == it->second.data.format) {
  375. error("Unsupported image format in %s\n", it->second.filename.c_str());
  376. }
  377. switch(compression_) {
  378. case COMPRESS_NONE:
  379. it->second.data.compression = compression_;
  380. it->second.compressed_content = content;
  381. it->second.data.compressed_size = content.size();
  382. break;
  383. case COMPRESS_EFIv1:
  384. {
  385. // The content will always compress smaller (so sez the docs).
  386. uint32_t tmpsize = content.size();
  387. uint8_t *tmpbuf = (uint8_t *)malloc(tmpsize);
  388. // The size of the compressed content is also returned.
  389. if (EFI_SUCCESS != EfiCompress((uint8_t *)content.c_str(), tmpsize,
  390. tmpbuf, &tmpsize)) {
  391. error("Unable to compress!\n");
  392. }
  393. it->second.data.compression = compression_;
  394. it->second.compressed_content.assign((const char *)tmpbuf, tmpsize);
  395. it->second.data.compressed_size = tmpsize;
  396. free(tmpbuf);
  397. }
  398. break;
  399. case COMPRESS_LZMA1:
  400. {
  401. // Calculate the worst case of buffer size.
  402. uint32_t tmpsize = lzma_stream_buffer_bound(content.size());
  403. uint8_t *tmpbuf = (uint8_t *)malloc(tmpsize);
  404. lzma_stream stream = LZMA_STREAM_INIT;
  405. lzma_options_lzma options;
  406. lzma_ret result;
  407. lzma_lzma_preset(&options, 9);
  408. result = lzma_alone_encoder(&stream, &options);
  409. if (result != LZMA_OK) {
  410. error("Unable to initialize easy encoder (error: %d)!\n", result);
  411. }
  412. stream.next_in = (uint8_t *)content.data();
  413. stream.avail_in = content.size();
  414. stream.next_out = tmpbuf;
  415. stream.avail_out = tmpsize;
  416. result = lzma_code(&stream, LZMA_FINISH);
  417. if (result != LZMA_STREAM_END) {
  418. error("Unable to encode data (error: %d)!\n", result);
  419. }
  420. it->second.data.compression = compression_;
  421. it->second.compressed_content.assign((const char *)tmpbuf,
  422. tmpsize - stream.avail_out);
  423. it->second.data.compressed_size = tmpsize - stream.avail_out;
  424. lzma_end(&stream);
  425. free(tmpbuf);
  426. }
  427. break;
  428. default:
  429. error("Unsupported compression method attempted.\n");
  430. }
  431. }
  432. }
  433. const string BmpBlockUtil::read_image_file(const char *filename) {
  434. string content;
  435. vector<char> buffer;
  436. FILE *fp = fopen(filename, "rb");
  437. if (!fp) {
  438. perror(filename);
  439. exit(errno);
  440. }
  441. if (fseek(fp, 0, SEEK_END) == 0) {
  442. buffer.resize(ftell(fp));
  443. rewind(fp);
  444. }
  445. if (!buffer.empty()) {
  446. if(fread(&buffer[0], buffer.size(), 1, fp) != 1) {
  447. perror(filename);
  448. buffer.clear();
  449. } else {
  450. content.assign(buffer.begin(), buffer.end());
  451. }
  452. }
  453. fclose(fp);
  454. return content;
  455. }
  456. void BmpBlockUtil::fill_bmpblock_header() {
  457. memset(&config_.header, '\0', sizeof(config_.header));
  458. memcpy(&config_.header.signature, BMPBLOCK_SIGNATURE,
  459. BMPBLOCK_SIGNATURE_SIZE);
  460. config_.header.major_version = major_version_;
  461. config_.header.minor_version = minor_version_;
  462. config_.header.number_of_localizations = config_.localizations.size();
  463. config_.header.number_of_screenlayouts = config_.localizations[0].size();
  464. // NOTE: this is part of the yaml consistency check
  465. for (unsigned int i = 1; i < config_.localizations.size(); ++i) {
  466. assert(config_.header.number_of_screenlayouts ==
  467. config_.localizations[i].size());
  468. }
  469. config_.header.number_of_imageinfos = config_.images_map.size();
  470. config_.header.locale_string_offset = 0; // Filled by pack_bmpblock()
  471. }
  472. void BmpBlockUtil::pack_bmpblock() {
  473. bmpblock_.clear();
  474. /* Compute the ImageInfo offsets from start of BMPBLOCK. */
  475. uint32_t current_offset = sizeof(BmpBlockHeader) +
  476. sizeof(ScreenLayout) * (config_.header.number_of_localizations *
  477. config_.header.number_of_screenlayouts);
  478. for (StrImageConfigMap::iterator it = config_.images_map.begin();
  479. it != config_.images_map.end();
  480. ++it) {
  481. it->second.offset = current_offset;
  482. if (debug_)
  483. printf(" \"%s\": filename=\"%s\" offset=0x%x tag=%d fmt=%d\n",
  484. it->first.c_str(),
  485. it->second.filename.c_str(),
  486. it->second.offset,
  487. it->second.data.tag,
  488. it->second.data.format);
  489. current_offset += sizeof(ImageInfo) +
  490. it->second.data.compressed_size;
  491. /* Make it 4-byte aligned. */
  492. if ((current_offset & 3) > 0) {
  493. current_offset = (current_offset & ~3) + 4;
  494. }
  495. }
  496. /* And leave room for the locale_index string */
  497. if (config_.locale_names.size()) {
  498. config_.header.locale_string_offset = current_offset;
  499. current_offset += config_.locale_names.size();
  500. }
  501. bmpblock_.resize(current_offset);
  502. /* Fill BmpBlockHeader struct. */
  503. string::iterator current_filled = bmpblock_.begin();
  504. std::copy(reinterpret_cast<char*>(&config_.header),
  505. reinterpret_cast<char*>(&config_.header + 1),
  506. current_filled);
  507. current_filled += sizeof(config_.header);
  508. current_offset = sizeof(config_.header);
  509. /* Fill all ScreenLayout structs. */
  510. for (unsigned int i = 0; i < config_.localizations.size(); ++i) {
  511. for (unsigned int j = 0; j < config_.localizations[i].size(); ++j) {
  512. ScreenConfig &screen = config_.screens_map[config_.localizations[i][j]];
  513. for (unsigned int k = 0;
  514. k < MAX_IMAGE_IN_LAYOUT && !screen.image_names[k].empty();
  515. ++k) {
  516. if (config_.images_map.find(screen.image_names[k]) ==
  517. config_.images_map.end()) {
  518. error("Invalid image name \"%s\"\n", screen.image_names[k].c_str());
  519. }
  520. if (debug_)
  521. printf("i=%d j=%d k=%d=\"%s\" (%d,%d) ofs=%x\n", i,j,k,
  522. screen.image_names[k].c_str(),
  523. screen.data.images[k].x, screen.data.images[k].y,
  524. config_.images_map[screen.image_names[k]].offset
  525. );
  526. screen.data.images[k].image_info_offset =
  527. config_.images_map[screen.image_names[k]].offset;
  528. }
  529. std::copy(reinterpret_cast<char*>(&screen.data),
  530. reinterpret_cast<char*>(&screen.data + 1),
  531. current_filled);
  532. current_filled += sizeof(screen.data);
  533. if (debug_)
  534. printf("S: current offset is 0x%08x\n", current_offset);
  535. current_offset += sizeof(screen.data);
  536. }
  537. }
  538. /* Fill all ImageInfo structs and image contents. */
  539. for (StrImageConfigMap::iterator it = config_.images_map.begin();
  540. it != config_.images_map.end();
  541. ++it) {
  542. current_filled = bmpblock_.begin() + it->second.offset;
  543. current_offset = it->second.offset;
  544. if (debug_)
  545. printf("I0: current offset is 0x%08x\n", current_offset);
  546. std::copy(reinterpret_cast<char*>(&it->second.data),
  547. reinterpret_cast<char*>(&it->second.data + 1),
  548. current_filled);
  549. current_filled += sizeof(it->second.data);
  550. current_offset += sizeof(it->second.data);
  551. if (debug_)
  552. printf("I1: current offset is 0x%08x (len %zd)\n",
  553. current_offset, it->second.compressed_content.length());
  554. std::copy(it->second.compressed_content.begin(),
  555. it->second.compressed_content.end(),
  556. current_filled);
  557. }
  558. /* Fill in locale_names. */
  559. if (config_.header.locale_string_offset) {
  560. current_offset = config_.header.locale_string_offset;
  561. current_filled = bmpblock_.begin() + current_offset;
  562. if (debug_)
  563. printf("locale_names: offset 0x%08x (len %zd)\n",
  564. current_offset, config_.locale_names.size());
  565. std::copy(config_.locale_names.begin(),
  566. config_.locale_names.end(),
  567. current_filled);
  568. }
  569. }
  570. void BmpBlockUtil::write_to_bmpblock(const char *filename) {
  571. assert(!bmpblock_.empty());
  572. FILE *fp = fopen(filename, "wb");
  573. if (!fp) {
  574. perror(filename);
  575. exit(errno);
  576. }
  577. int r = fwrite(bmpblock_.c_str(), bmpblock_.size(), 1, fp);
  578. fclose(fp);
  579. if (r != 1) {
  580. perror(filename);
  581. exit(errno);
  582. }
  583. }
  584. } // namespace vboot_reference
  585. #ifndef FOR_LIBRARY
  586. //////////////////////////////////////////////////////////////////////////////
  587. // Command line utilities.
  588. extern "C" {
  589. #include "bmpblk_util.h"
  590. }
  591. using vboot_reference::BmpBlockUtil;
  592. // utility function: provide usage of this utility and exit.
  593. static void usagehelp_exit(const char *prog_name) {
  594. printf(
  595. "\n"
  596. "To create a new BMPBLOCK file using config from YAML file:\n"
  597. "\n"
  598. " %s [-z NUM] -c YAML BMPBLOCK\n"
  599. "\n"
  600. " -z NUM = compression algorithm to use\n"
  601. " 0 = none\n"
  602. " 1 = EFIv1\n"
  603. " 2 = LZMA1\n"
  604. "\n", prog_name);
  605. printf(
  606. "To display the contents of a BMPBLOCK:\n"
  607. "\n"
  608. " %s [-y] BMPBLOCK\n"
  609. "\n"
  610. " -y = display as yaml\n"
  611. "\n", prog_name);
  612. printf(
  613. "To unpack a BMPBLOCK file:\n"
  614. "\n"
  615. " %s -x [-d DIR] [-f] BMPBLOCK\n"
  616. "\n"
  617. " -d DIR = directory to use (default '.')\n"
  618. " -f = force overwriting existing files\n"
  619. "\n", prog_name);
  620. exit(1);
  621. }
  622. ///////////////////////////////////////////////////////////////////////
  623. // main
  624. int main(int argc, char *argv[]) {
  625. const char *prog_name = strrchr(argv[0], '/');
  626. if (prog_name)
  627. prog_name++;
  628. else
  629. prog_name = argv[0];
  630. int overwrite = 0, extract_mode = 0;
  631. int compression = 0;
  632. int set_compression = 0;
  633. const char *config_fn = 0, *bmpblock_fn = 0, *extract_dir = ".";
  634. int show_as_yaml = 0;
  635. bool debug = false;
  636. int opt;
  637. opterr = 0; // quiet
  638. int errorcnt = 0;
  639. char *e = 0;
  640. while ((opt = getopt(argc, argv, ":c:xz:fd:yD")) != -1) {
  641. switch (opt) {
  642. case 'c':
  643. config_fn = optarg;
  644. break;
  645. case 'x':
  646. extract_mode = 1;
  647. break;
  648. case 'y':
  649. show_as_yaml = 1;
  650. break;
  651. case 'z':
  652. compression = (int)strtoul(optarg, &e, 0);
  653. if (!*optarg || (e && *e)) {
  654. fprintf(stderr, "%s: invalid argument to -%c: \"%s\"\n",
  655. prog_name, opt, optarg);
  656. errorcnt++;
  657. }
  658. if (compression >= MAX_COMPRESS) {
  659. fprintf(stderr, "%s: compression type must be less than %d\n",
  660. prog_name, MAX_COMPRESS);
  661. errorcnt++;
  662. }
  663. set_compression = 1;
  664. break;
  665. case 'f':
  666. overwrite = 1;
  667. break;
  668. case 'd':
  669. extract_dir= optarg;
  670. break;
  671. case 'D':
  672. debug = true;
  673. break;
  674. case ':':
  675. fprintf(stderr, "%s: missing argument to -%c\n",
  676. prog_name, optopt);
  677. errorcnt++;
  678. break;
  679. default:
  680. fprintf(stderr, "%s: unrecognized switch: -%c\n",
  681. prog_name, optopt);
  682. errorcnt++;
  683. break;
  684. }
  685. }
  686. argc -= optind;
  687. argv += optind;
  688. if (argc >= 1) {
  689. bmpblock_fn = argv[0];
  690. } else {
  691. fprintf(stderr, "%s: missing BMPBLOCK name\n", prog_name);
  692. errorcnt++;
  693. }
  694. if (errorcnt)
  695. usagehelp_exit(prog_name);
  696. BmpBlockUtil util(debug);
  697. if (config_fn) {
  698. if (set_compression)
  699. util.force_compression(compression);
  700. util.load_from_config(config_fn);
  701. util.pack_bmpblock();
  702. util.write_to_bmpblock(bmpblock_fn);
  703. }
  704. else if (extract_mode) {
  705. return dump_bmpblock(bmpblock_fn, 1, extract_dir, overwrite);
  706. } else {
  707. return dump_bmpblock(bmpblock_fn, show_as_yaml, 0, 0);
  708. }
  709. return 0;
  710. }
  711. #endif // FOR_LIBRARY