loadfont.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 loadnewfont(int fd);
  27. int loadfont_main(int argc, char **argv);
  28. int loadfont_main(int argc, char **argv)
  29. {
  30. int fd;
  31. if (argc != 1)
  32. bb_show_usage();
  33. fd = xopen(CURRENT_VC, O_RDWR);
  34. loadnewfont(fd);
  35. return EXIT_SUCCESS;
  36. }
  37. static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
  38. {
  39. char buf[16384];
  40. int i;
  41. memset(buf, 0, sizeof(buf));
  42. if (unit < 1 || unit > 32)
  43. bb_error_msg_and_die("bad character size %d", unit);
  44. for (i = 0; i < fontsize; i++)
  45. memcpy(buf + (32 * i), inbuf + (unit * i), unit);
  46. #if defined( PIO_FONTX ) && !defined( __sparc__ )
  47. {
  48. struct consolefontdesc cfd;
  49. cfd.charcount = fontsize;
  50. cfd.charheight = unit;
  51. cfd.chardata = buf;
  52. if (ioctl(fd, PIO_FONTX, &cfd) == 0)
  53. return; /* success */
  54. bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
  55. }
  56. #endif
  57. if (ioctl(fd, PIO_FONT, buf))
  58. bb_perror_msg_and_die("PIO_FONT ioctl error");
  59. }
  60. static void
  61. do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
  62. {
  63. struct unimapinit advice;
  64. struct unimapdesc ud;
  65. struct unipair *up;
  66. int ct = 0, maxct;
  67. int glyph;
  68. uint16_t unicode;
  69. maxct = tailsz; /* more than enough */
  70. up = xmalloc(maxct * sizeof(struct unipair));
  71. for (glyph = 0; glyph < fontsize; glyph++) {
  72. while (tailsz >= 2) {
  73. unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
  74. tailsz -= 2;
  75. inbuf += 2;
  76. if (unicode == PSF_SEPARATOR)
  77. break;
  78. up[ct].unicode = unicode;
  79. up[ct].fontpos = glyph;
  80. ct++;
  81. }
  82. }
  83. /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
  84. this printf did not work on many kernels */
  85. advice.advised_hashsize = 0;
  86. advice.advised_hashstep = 0;
  87. advice.advised_hashlevel = 0;
  88. if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
  89. #ifdef ENOIOCTLCMD
  90. if (errno == ENOIOCTLCMD) {
  91. bb_error_msg("it seems this kernel is older than 1.1.92");
  92. bb_error_msg_and_die("no Unicode mapping table loaded");
  93. } else
  94. #endif
  95. bb_perror_msg_and_die("PIO_UNIMAPCLR");
  96. }
  97. ud.entry_ct = ct;
  98. ud.entries = up;
  99. if (ioctl(fd, PIO_UNIMAP, &ud)) {
  100. bb_perror_msg_and_die("PIO_UNIMAP");
  101. }
  102. }
  103. static void loadnewfont(int fd)
  104. {
  105. int unit;
  106. unsigned char inbuf[32768]; /* primitive */
  107. unsigned int inputlth, offset;
  108. /*
  109. * We used to look at the length of the input file
  110. * with stat(); now that we accept compressed files,
  111. * just read the entire file.
  112. */
  113. inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
  114. if (ferror(stdin))
  115. bb_perror_msg_and_die("error reading input font");
  116. /* use malloc/realloc in case of giant files;
  117. maybe these do not occur: 16kB for the font,
  118. and 16kB for the map leaves 32 unicode values
  119. for each font position */
  120. if (!feof(stdin))
  121. bb_perror_msg_and_die("font too large");
  122. /* test for psf first */
  123. {
  124. struct psf_header psfhdr;
  125. int fontsize;
  126. int hastable;
  127. unsigned int head0, head;
  128. if (inputlth < sizeof(struct psf_header))
  129. goto no_psf;
  130. psfhdr = *(struct psf_header *) &inbuf[0];
  131. if (!PSF_MAGIC_OK(psfhdr))
  132. goto no_psf;
  133. if (psfhdr.mode > PSF_MAXMODE)
  134. bb_error_msg_and_die("unsupported psf file mode");
  135. fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
  136. #if !defined( PIO_FONTX ) || defined( __sparc__ )
  137. if (fontsize != 256)
  138. bb_error_msg_and_die("only fontsize 256 supported");
  139. #endif
  140. hastable = (psfhdr.mode & PSF_MODEHASTAB);
  141. unit = psfhdr.charsize;
  142. head0 = sizeof(struct psf_header);
  143. head = head0 + fontsize * unit;
  144. if (head > inputlth || (!hastable && head != inputlth))
  145. bb_error_msg_and_die("input file: bad length");
  146. do_loadfont(fd, inbuf + head0, unit, fontsize);
  147. if (hastable)
  148. do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
  149. return;
  150. }
  151. no_psf:
  152. /* file with three code pages? */
  153. if (inputlth == 9780) {
  154. offset = 40;
  155. unit = 16;
  156. } else {
  157. /* bare font */
  158. if (inputlth & 0377)
  159. bb_error_msg_and_die("bad input file size");
  160. offset = 0;
  161. unit = inputlth / 256;
  162. }
  163. do_loadfont(fd, inbuf + offset, unit, 256);
  164. }