loadfont.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /*memset(buf, 0, 16 * 1024);*/
  34. for (i = 0; i < fontsize; i++)
  35. memcpy(buf + (32 * i), inbuf + (unit * i), unit);
  36. #if defined(PIO_FONTX) && !defined(__sparc__)
  37. {
  38. struct consolefontdesc cfd;
  39. cfd.charcount = fontsize;
  40. cfd.charheight = unit;
  41. cfd.chardata = buf;
  42. if (!ioctl_or_perror(fd, PIO_FONTX, &cfd, "PIO_FONTX ioctl failed (will try PIO_FONT)"))
  43. goto ret; /* success */
  44. }
  45. #endif
  46. xioctl(fd, PIO_FONT, buf);
  47. ret:
  48. free(buf);
  49. }
  50. static void
  51. do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
  52. {
  53. struct unimapinit advice;
  54. struct unimapdesc ud;
  55. struct unipair *up;
  56. int ct = 0, maxct;
  57. int glyph;
  58. uint16_t unicode;
  59. maxct = tailsz; /* more than enough */
  60. up = xmalloc(maxct * sizeof(struct unipair));
  61. for (glyph = 0; glyph < fontsize; glyph++) {
  62. while (tailsz >= 2) {
  63. unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
  64. tailsz -= 2;
  65. inbuf += 2;
  66. if (unicode == PSF_SEPARATOR)
  67. break;
  68. up[ct].unicode = unicode;
  69. up[ct].fontpos = glyph;
  70. ct++;
  71. }
  72. }
  73. /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
  74. this printf did not work on many kernels */
  75. advice.advised_hashsize = 0;
  76. advice.advised_hashstep = 0;
  77. advice.advised_hashlevel = 0;
  78. xioctl(fd, PIO_UNIMAPCLR, &advice);
  79. ud.entry_ct = ct;
  80. ud.entries = up;
  81. xioctl(fd, PIO_UNIMAP, &ud);
  82. }
  83. static void loadnewfont(int fd)
  84. {
  85. enum { INBUF_SIZE = 32*1024 + 1 };
  86. int unit;
  87. unsigned inputlth, offset;
  88. /* Was on stack, but 32k is a bit too much: */
  89. unsigned char *inbuf = xmalloc(INBUF_SIZE);
  90. /*
  91. * We used to look at the length of the input file
  92. * with stat(); now that we accept compressed files,
  93. * just read the entire file.
  94. */
  95. inputlth = full_read(STDIN_FILENO, inbuf, INBUF_SIZE);
  96. if (inputlth < 0)
  97. bb_perror_msg_and_die("error reading input font");
  98. if (inputlth >= INBUF_SIZE)
  99. bb_error_msg_and_die("font too large");
  100. /* test for psf first */
  101. {
  102. struct psf_header psfhdr;
  103. int fontsize;
  104. int hastable;
  105. unsigned head0, head;
  106. if (inputlth < sizeof(struct psf_header))
  107. goto no_psf;
  108. psfhdr = *(struct psf_header *) &inbuf[0];
  109. if (!PSF_MAGIC_OK(psfhdr))
  110. goto no_psf;
  111. if (psfhdr.mode > PSF_MAXMODE)
  112. bb_error_msg_and_die("unsupported psf file mode");
  113. fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
  114. #if !defined(PIO_FONTX) || defined(__sparc__)
  115. if (fontsize != 256)
  116. bb_error_msg_and_die("only fontsize 256 supported");
  117. #endif
  118. hastable = (psfhdr.mode & PSF_MODEHASTAB);
  119. unit = psfhdr.charsize;
  120. head0 = sizeof(struct psf_header);
  121. head = head0 + fontsize * unit;
  122. if (head > inputlth || (!hastable && head != inputlth))
  123. bb_error_msg_and_die("input file: bad length");
  124. do_loadfont(fd, inbuf + head0, unit, fontsize);
  125. if (hastable)
  126. do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
  127. return;
  128. }
  129. no_psf:
  130. /* file with three code pages? */
  131. if (inputlth == 9780) {
  132. offset = 40;
  133. unit = 16;
  134. } else {
  135. /* bare font */
  136. if (inputlth & 0377)
  137. bb_error_msg_and_die("bad input file size");
  138. offset = 0;
  139. unit = inputlth / 256;
  140. }
  141. do_loadfont(fd, inbuf + offset, unit, 256);
  142. }
  143. int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  144. int loadfont_main(int argc, char **argv ATTRIBUTE_UNUSED)
  145. {
  146. int fd;
  147. if (argc != 1)
  148. bb_show_usage();
  149. fd = xopen(CURRENT_VC, O_RDWR);
  150. loadnewfont(fd);
  151. return EXIT_SUCCESS;
  152. }