loadfont.c 4.8 KB

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