loadfont.c 4.8 KB

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