loadfont.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * loadfont.c - Eugene Crosser & Andries Brouwer
  4. *
  5. * Version 0.96bb
  6. *
  7. * Loads the console font, and possibly the corresponding screen map(s).
  8. * (Adapted for busybox by Matej Vela.)
  9. */
  10. #include "libbb.h"
  11. #include <sys/kd.h>
  12. enum {
  13. PSF_MAGIC1 = 0x36,
  14. PSF_MAGIC2 = 0x04,
  15. PSF_MODE512 = 0x01,
  16. PSF_MODEHASTAB = 0x02,
  17. PSF_MAXMODE = 0x03,
  18. PSF_SEPARATOR = 0xFFFF
  19. };
  20. struct psf_header {
  21. unsigned char magic1, magic2; /* Magic number */
  22. unsigned char mode; /* PSF font mode */
  23. unsigned char charsize; /* Character size */
  24. };
  25. #define PSF_MAGIC_OK(x) ((x)->magic1 == PSF_MAGIC1 && (x)->magic2 == PSF_MAGIC2)
  26. static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
  27. {
  28. char *buf;
  29. int i;
  30. if (unit < 1 || unit > 32)
  31. bb_error_msg_and_die("bad character size %d", unit);
  32. buf = xzalloc(16 * 1024);
  33. for (i = 0; i < fontsize; i++)
  34. memcpy(buf + (32 * i), inbuf + (unit * i), unit);
  35. #if defined(PIO_FONTX) && !defined(__sparc__)
  36. {
  37. struct consolefontdesc cfd;
  38. cfd.charcount = fontsize;
  39. cfd.charheight = unit;
  40. cfd.chardata = buf;
  41. if (!ioctl_or_perror(fd, PIO_FONTX, &cfd, "PIO_FONTX ioctl failed (will try PIO_FONT)"))
  42. goto ret; /* success */
  43. }
  44. #endif
  45. xioctl(fd, PIO_FONT, buf);
  46. ret:
  47. free(buf);
  48. }
  49. static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
  50. {
  51. struct unimapinit advice;
  52. struct unimapdesc ud;
  53. struct unipair *up;
  54. int ct = 0, maxct;
  55. int glyph;
  56. uint16_t unicode;
  57. maxct = tailsz; /* more than enough */
  58. up = xmalloc(maxct * sizeof(struct unipair));
  59. for (glyph = 0; glyph < fontsize; glyph++) {
  60. while (tailsz >= 2) {
  61. unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
  62. tailsz -= 2;
  63. inbuf += 2;
  64. if (unicode == PSF_SEPARATOR)
  65. break;
  66. up[ct].unicode = unicode;
  67. up[ct].fontpos = glyph;
  68. ct++;
  69. }
  70. }
  71. /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
  72. this printf did not work on many kernels */
  73. advice.advised_hashsize = 0;
  74. advice.advised_hashstep = 0;
  75. advice.advised_hashlevel = 0;
  76. xioctl(fd, PIO_UNIMAPCLR, &advice);
  77. ud.entry_ct = ct;
  78. ud.entries = up;
  79. xioctl(fd, PIO_UNIMAP, &ud);
  80. }
  81. static void do_load(int fd, struct psf_header *psfhdr, size_t len)
  82. {
  83. int unit;
  84. int fontsize;
  85. int hastable;
  86. unsigned head0, head = head;
  87. /* test for psf first */
  88. if (len >= sizeof(struct psf_header) && PSF_MAGIC_OK(psfhdr)) {
  89. if (psfhdr->mode > PSF_MAXMODE)
  90. bb_error_msg_and_die("unsupported psf file mode");
  91. fontsize = ((psfhdr->mode & PSF_MODE512) ? 512 : 256);
  92. #if !defined(PIO_FONTX) || defined(__sparc__)
  93. if (fontsize != 256)
  94. bb_error_msg_and_die("only fontsize 256 supported");
  95. #endif
  96. hastable = (psfhdr->mode & PSF_MODEHASTAB);
  97. unit = psfhdr->charsize;
  98. head0 = sizeof(struct psf_header);
  99. head = head0 + fontsize * unit;
  100. if (head > len || (!hastable && head != len))
  101. bb_error_msg_and_die("input file: bad length");
  102. } else {
  103. /* file with three code pages? */
  104. if (len == 9780) {
  105. head0 = 40;
  106. unit = 16;
  107. } else {
  108. /* bare font */
  109. if (len & 0377)
  110. bb_error_msg_and_die("input file: bad length");
  111. head0 = 0;
  112. unit = len / 256;
  113. }
  114. fontsize = 256;
  115. hastable = 0;
  116. }
  117. do_loadfont(fd, (unsigned char *)psfhdr + head0, unit, fontsize);
  118. if (hastable)
  119. do_loadtable(fd, (unsigned char *)psfhdr + head, len - head, fontsize);
  120. }
  121. #if ENABLE_LOADFONT
  122. int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  123. int loadfont_main(int argc UNUSED_PARAM, char **argv)
  124. {
  125. size_t len;
  126. struct psf_header *psfhdr;
  127. // no arguments allowed!
  128. opt_complementary = "=0";
  129. getopt32(argv, "");
  130. /*
  131. * We used to look at the length of the input file
  132. * with stat(); now that we accept compressed files,
  133. * just read the entire file.
  134. */
  135. len = 32*1024; // can't be larger
  136. psfhdr = (struct psf_header *) xmalloc_read(STDIN_FILENO, &len);
  137. // xmalloc_open_zipped_read_close(filename, &len);
  138. if (!psfhdr)
  139. bb_perror_msg_and_die("error reading input font");
  140. do_load(get_console_fd_or_die(), psfhdr, len);
  141. return EXIT_SUCCESS;
  142. }
  143. #endif
  144. #if ENABLE_SETFONT
  145. int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  146. int setfont_main(int argc UNUSED_PARAM, char **argv)
  147. {
  148. size_t len;
  149. struct psf_header *psfhdr;
  150. char *mapfilename;
  151. int fd;
  152. opt_complementary = "=1";
  153. getopt32(argv, "m:", &mapfilename);
  154. argv += optind;
  155. // load font
  156. len = 32*1024; // can't be larger
  157. psfhdr = (struct psf_header *) xmalloc_open_zipped_read_close(*argv, &len);
  158. fd = get_console_fd_or_die();
  159. do_load(fd, psfhdr, len);
  160. // load the screen map, if any
  161. if (option_mask32 & 1) { // -m
  162. void *map = xmalloc_open_zipped_read_close(mapfilename, &len);
  163. if (len == E_TABSZ || len == 2*E_TABSZ) {
  164. xioctl(fd, (len == 2*E_TABSZ) ? PIO_UNISCRNMAP : PIO_SCRNMAP, map);
  165. }
  166. }
  167. return EXIT_SUCCESS;
  168. }
  169. #endif