bmpblk_font.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright (c) 2011 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. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <getopt.h>
  7. #include <limits.h>
  8. #include <stdarg.h>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/mman.h>
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include <unistd.h>
  17. #include "bmpblk_font.h"
  18. #include "image_types.h"
  19. #include "vboot_api.h"
  20. static char *progname;
  21. static void error(const char *fmt, ...)
  22. {
  23. va_list args;
  24. va_start( args, fmt );
  25. fprintf(stderr, "%s: ", progname);
  26. vfprintf( stderr, fmt, args );
  27. va_end( args );
  28. }
  29. #define fatal(args...) do { error(args); exit(1); } while(0)
  30. /* Command line options */
  31. enum {
  32. OPT_OUTFILE = 1000,
  33. };
  34. #define DEFAULT_OUTFILE "font.bin"
  35. static struct option long_opts[] = {
  36. {"outfile", 1, 0, OPT_OUTFILE },
  37. {NULL, 0, 0, 0}
  38. };
  39. /* Print help and return error */
  40. static void HelpAndDie(void) {
  41. fprintf(stderr,
  42. "\n"
  43. "%s - Create a vboot fontfile from a set of BMP files.\n"
  44. "\n"
  45. "Usage: %s [OPTIONS] BMPFILE [BMPFILE...]\n"
  46. "\n"
  47. "Each BMP file must match *_HEX.bmp, where HEX is the hexadecimal\n"
  48. "representation of the character that the file displays. The images\n"
  49. "will be encoded in the given order. Typically the first image is\n"
  50. "reused to represent any missing characters.\n"
  51. "\n"
  52. "OPTIONS are:\n"
  53. " --outfile <filename> Output file (default is %s)\n"
  54. "\n", progname, progname, DEFAULT_OUTFILE);
  55. exit(1);
  56. }
  57. //////////////////////////////////////////////////////////////////////////////
  58. // Returns pointer to buffer containing entire file, sets length.
  59. static void *read_entire_file(const char *filename, size_t *length) {
  60. int fd;
  61. struct stat sbuf;
  62. void *ptr;
  63. *length = 0; // just in case
  64. if (0 != stat(filename, &sbuf)) {
  65. error("Unable to stat %s: %s\n", filename, strerror(errno));
  66. return 0;
  67. }
  68. if (!sbuf.st_size) {
  69. error("File %s is empty\n", filename);
  70. return 0;
  71. }
  72. fd = open(filename, O_RDONLY);
  73. if (fd < 0) {
  74. error("Unable to open %s: %s\n", filename, strerror(errno));
  75. return 0;
  76. }
  77. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  78. if (MAP_FAILED == ptr) {
  79. error("Unable to mmap %s: %s\n", filename, strerror(errno));
  80. close(fd);
  81. return 0;
  82. }
  83. *length = sbuf.st_size;
  84. close(fd);
  85. return ptr;
  86. }
  87. // Reclaims buffer from read_entire_file().
  88. static void discard_file(void *ptr, size_t length) {
  89. munmap(ptr, length);
  90. }
  91. //////////////////////////////////////////////////////////////////////////////
  92. int main(int argc, char* argv[]) {
  93. char* outfile = DEFAULT_OUTFILE;
  94. int numimages = 0;
  95. int parse_error = 0;
  96. int i;
  97. FILE *ofp;
  98. FontArrayHeader header;
  99. FontArrayEntryHeader entry;
  100. progname = strrchr(argv[0], '/');
  101. if (progname)
  102. progname++;
  103. else
  104. progname = argv[0];
  105. while ((i = getopt_long(argc, argv, "", long_opts, NULL)) != -1) {
  106. switch (i) {
  107. case OPT_OUTFILE:
  108. outfile = optarg;
  109. break;
  110. default:
  111. /* Unhandled option */
  112. printf("Unknown option\n");
  113. parse_error = 1;
  114. break;
  115. }
  116. }
  117. numimages = argc - optind;
  118. if (parse_error || numimages < 1)
  119. HelpAndDie();
  120. printf("outfile is %s\n", outfile);
  121. printf("numimages is %d\n", numimages);
  122. ofp = fopen(outfile, "wb");
  123. if (!ofp)
  124. fatal("Unable to open %s: %s\n", outfile, strerror(errno));
  125. memcpy(&header.signature, FONT_SIGNATURE, FONT_SIGNATURE_SIZE);
  126. header.num_entries = numimages;
  127. if (1 != fwrite(&header, sizeof(header), 1, ofp)) {
  128. error("Can't write header to %s: %s\n", outfile, strerror(errno));
  129. goto bad1;
  130. }
  131. for(i=0; i<numimages; i++) {
  132. char *imgfile = argv[optind+i];
  133. char *s;
  134. uint32_t ascii;
  135. void *imgdata = 0;
  136. size_t imgsize, filesize, diff;
  137. s = strrchr(imgfile, '_');
  138. if (!s || 1 != sscanf(s, "_%x.bmp", &ascii)) { // This is not foolproof.
  139. error("Unable to parse the character from filename %s\n", imgfile);
  140. goto bad1;
  141. }
  142. imgdata = read_entire_file(imgfile, &imgsize);
  143. if (!imgdata)
  144. goto bad1;
  145. if (FORMAT_BMP != identify_image_type(imgdata, imgsize, &entry.info)) {
  146. error("%s does not contain a valid BMP image\n", imgfile);
  147. goto bad1;
  148. }
  149. // Pad the image to align it on a 4-byte boundary.
  150. filesize = imgsize;
  151. if (imgsize % 4)
  152. filesize = ((imgsize + 4) / 4) * 4;
  153. diff = filesize - imgsize;
  154. entry.ascii = ascii;
  155. entry.info.tag = TAG_NONE;
  156. entry.info.compression = COMPRESS_NONE; // we'll compress it all later
  157. entry.info.original_size = filesize;
  158. entry.info.compressed_size = filesize;
  159. printf("%s => 0x%x %dx%d\n", imgfile, entry.ascii,
  160. entry.info.width, entry.info.height);
  161. if (1 != fwrite(&entry, sizeof(entry), 1, ofp)) {
  162. error("Can't write entry to %s: %s\n", outfile, strerror(errno));
  163. goto bad1;
  164. }
  165. if (1 != fwrite(imgdata, imgsize, 1, ofp)) {
  166. error("Can't write image to %s: %s\n", outfile, strerror(errno));
  167. goto bad1;
  168. }
  169. if (diff && 1 != fwrite("\0\0\0\0\0\0\0\0", diff, 1, ofp)) {
  170. error("Can't write padding to %s: %s\n", outfile, strerror(errno));
  171. goto bad1;
  172. }
  173. discard_file(imgdata, imgsize);
  174. }
  175. fclose(ofp);
  176. return 0;
  177. bad1:
  178. fclose(ofp);
  179. error("Aborting\n");
  180. (void) unlink(outfile);
  181. exit(1);
  182. }