loadfont.c 4.4 KB

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