3
0

loadfont.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. #ifndef KDFONTOP
  13. #define KDFONTOP 0x4B72
  14. struct console_font_op {
  15. unsigned op; /* KD_FONT_OP_* */
  16. unsigned flags; /* KD_FONT_FLAG_* */
  17. unsigned width, height;
  18. unsigned charcount;
  19. unsigned char *data; /* font data with height fixed to 32 */
  20. };
  21. #define KD_FONT_OP_SET 0 /* Set font */
  22. #define KD_FONT_OP_GET 1 /* Get font */
  23. #define KD_FONT_OP_SET_DEFAULT 2 /* Set font to default,
  24. data points to name / NULL */
  25. #define KD_FONT_OP_COPY 3 /* Copy from another console */
  26. #define KD_FONT_FLAG_OLD 0x80000000 /* Invoked via old interface */
  27. #define KD_FONT_FLAG_DONT_RECALC 1 /* Don't call adjust_height() */
  28. /* (Used internally for PIO_FONT support) */
  29. #endif /* KDFONTOP */
  30. enum {
  31. PSF_MAGIC1 = 0x36,
  32. PSF_MAGIC2 = 0x04,
  33. PSF_MODE512 = 0x01,
  34. PSF_MODEHASTAB = 0x02,
  35. PSF_MAXMODE = 0x03,
  36. PSF_SEPARATOR = 0xffff
  37. };
  38. struct psf_header {
  39. unsigned char magic1, magic2; /* Magic number */
  40. unsigned char mode; /* PSF font mode */
  41. unsigned char charsize; /* Character size */
  42. };
  43. #define PSF_MAGIC_OK(x) ((x)->magic1 == PSF_MAGIC1 && (x)->magic2 == PSF_MAGIC2)
  44. static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
  45. {
  46. char *buf;
  47. int i;
  48. if (unit < 1 || unit > 32)
  49. bb_error_msg_and_die("bad character size %d", unit);
  50. buf = xzalloc(16 * 1024);
  51. for (i = 0; i < fontsize; i++)
  52. memcpy(buf + (32 * i), inbuf + (unit * i), unit);
  53. { /* KDFONTOP */
  54. struct console_font_op cfo;
  55. cfo.op = KD_FONT_OP_SET;
  56. cfo.flags = 0;
  57. cfo.width = 8;
  58. cfo.height = unit;
  59. cfo.charcount = fontsize;
  60. cfo.data = (void*)buf;
  61. #if 0
  62. if (!ioctl_or_perror(fd, KDFONTOP, &cfo, "KDFONTOP ioctl failed (will try PIO_FONTX)"))
  63. goto ret; /* success */
  64. #else
  65. xioctl(fd, KDFONTOP, &cfo);
  66. #endif
  67. }
  68. #if 0
  69. /* These ones do not honour -C tty (they set font on current tty regardless)
  70. * On x86, this distinction is visible on framebuffer consoles
  71. * (regular character consoles may have only one shared font anyway)
  72. */
  73. #if defined(PIO_FONTX) && !defined(__sparc__)
  74. {
  75. struct consolefontdesc cfd;
  76. cfd.charcount = fontsize;
  77. cfd.charheight = unit;
  78. cfd.chardata = buf;
  79. if (!ioctl_or_perror(fd, PIO_FONTX, &cfd, "PIO_FONTX ioctl failed (will try PIO_FONT)"))
  80. goto ret; /* success */
  81. }
  82. #endif
  83. xioctl(fd, PIO_FONT, buf);
  84. ret:
  85. #endif /* 0 */
  86. free(buf);
  87. }
  88. static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
  89. {
  90. struct unimapinit advice;
  91. struct unimapdesc ud;
  92. struct unipair *up;
  93. int ct = 0, maxct;
  94. int glyph;
  95. uint16_t unicode;
  96. maxct = tailsz; /* more than enough */
  97. up = xmalloc(maxct * sizeof(struct unipair));
  98. for (glyph = 0; glyph < fontsize; glyph++) {
  99. while (tailsz >= 2) {
  100. unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
  101. tailsz -= 2;
  102. inbuf += 2;
  103. if (unicode == PSF_SEPARATOR)
  104. break;
  105. up[ct].unicode = unicode;
  106. up[ct].fontpos = glyph;
  107. ct++;
  108. }
  109. }
  110. /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
  111. this printf did not work on many kernels */
  112. advice.advised_hashsize = 0;
  113. advice.advised_hashstep = 0;
  114. advice.advised_hashlevel = 0;
  115. xioctl(fd, PIO_UNIMAPCLR, &advice);
  116. ud.entry_ct = ct;
  117. ud.entries = up;
  118. xioctl(fd, PIO_UNIMAP, &ud);
  119. }
  120. static void do_load(int fd, struct psf_header *psfhdr, size_t len)
  121. {
  122. int unit;
  123. int fontsize;
  124. int hastable;
  125. unsigned head0, head = head;
  126. /* test for psf first */
  127. if (len >= sizeof(struct psf_header) && PSF_MAGIC_OK(psfhdr)) {
  128. if (psfhdr->mode > PSF_MAXMODE)
  129. bb_error_msg_and_die("unsupported psf file mode");
  130. fontsize = ((psfhdr->mode & PSF_MODE512) ? 512 : 256);
  131. #if !defined(PIO_FONTX) || defined(__sparc__)
  132. if (fontsize != 256)
  133. bb_error_msg_and_die("only fontsize 256 supported");
  134. #endif
  135. hastable = (psfhdr->mode & PSF_MODEHASTAB);
  136. unit = psfhdr->charsize;
  137. head0 = sizeof(struct psf_header);
  138. head = head0 + fontsize * unit;
  139. if (head > len || (!hastable && head != len))
  140. bb_error_msg_and_die("input file: bad length");
  141. } else {
  142. /* file with three code pages? */
  143. if (len == 9780) {
  144. head0 = 40;
  145. unit = 16;
  146. } else {
  147. /* bare font */
  148. if (len & 0377)
  149. bb_error_msg_and_die("input file: bad length");
  150. head0 = 0;
  151. unit = len / 256;
  152. }
  153. fontsize = 256;
  154. hastable = 0;
  155. }
  156. do_loadfont(fd, (unsigned char *)psfhdr + head0, unit, fontsize);
  157. if (hastable)
  158. do_loadtable(fd, (unsigned char *)psfhdr + head, len - head, fontsize);
  159. }
  160. #if ENABLE_LOADFONT
  161. int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  162. int loadfont_main(int argc UNUSED_PARAM, char **argv)
  163. {
  164. size_t len;
  165. struct psf_header *psfhdr;
  166. // no arguments allowed!
  167. opt_complementary = "=0";
  168. getopt32(argv, "");
  169. /*
  170. * We used to look at the length of the input file
  171. * with stat(); now that we accept compressed files,
  172. * just read the entire file.
  173. */
  174. len = 32*1024; // can't be larger
  175. psfhdr = xmalloc_read(STDIN_FILENO, &len);
  176. // xmalloc_open_zipped_read_close(filename, &len);
  177. if (!psfhdr)
  178. bb_perror_msg_and_die("error reading input font");
  179. do_load(get_console_fd_or_die(), psfhdr, len);
  180. return EXIT_SUCCESS;
  181. }
  182. #endif
  183. #if ENABLE_SETFONT
  184. /*
  185. kbd-1.12:
  186. setfont [-O font+umap.orig] [-o font.orig] [-om cmap.orig]
  187. [-ou umap.orig] [-N] [font.new ...] [-m cmap] [-u umap] [-C console]
  188. [-hNN] [-v] [-V]
  189. -h NN Override font height
  190. -o file
  191. Save previous font in file
  192. -O file
  193. Save previous font and Unicode map in file
  194. -om file
  195. Store console map in file
  196. -ou file
  197. Save previous Unicode map in file
  198. -m file
  199. Load console map or Unicode console map from file
  200. -u file
  201. Load Unicode table describing the font from file
  202. Example:
  203. # cp866
  204. 0x00-0x7f idem
  205. #
  206. 0x80 U+0410 # CYRILLIC CAPITAL LETTER A
  207. 0x81 U+0411 # CYRILLIC CAPITAL LETTER BE
  208. 0x82 U+0412 # CYRILLIC CAPITAL LETTER VE
  209. -C console
  210. Set the font for the indicated console
  211. -v Verbose
  212. -V Version
  213. */
  214. #if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
  215. static int ctoi(char *s)
  216. {
  217. if (s[0] == '\'' && s[1] != '\0' && s[2] == '\'' && s[3] == '\0')
  218. return s[1];
  219. // U+ means 0x
  220. if (s[0] == 'U' && s[1] == '+') {
  221. s[0] = '0';
  222. s[1] = 'x';
  223. }
  224. if (!isdigit(s[0]))
  225. return -1;
  226. return xstrtoul(s, 0);
  227. }
  228. #endif
  229. int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  230. int setfont_main(int argc UNUSED_PARAM, char **argv)
  231. {
  232. size_t len;
  233. unsigned opts;
  234. int fd;
  235. struct psf_header *psfhdr;
  236. char *mapfilename;
  237. const char *tty_name = CURRENT_TTY;
  238. opt_complementary = "=1";
  239. opts = getopt32(argv, "m:C:", &mapfilename, &tty_name);
  240. argv += optind;
  241. fd = xopen(tty_name, O_NONBLOCK);
  242. if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not ""
  243. if (*argv[0] != '/') {
  244. // goto default fonts location. don't die if doesn't exist
  245. chdir(CONFIG_DEFAULT_SETFONT_DIR "/consolefonts");
  246. }
  247. }
  248. // load font
  249. len = 32*1024; // can't be larger
  250. psfhdr = xmalloc_open_zipped_read_close(*argv, &len);
  251. if (!psfhdr)
  252. bb_simple_perror_msg_and_die(*argv);
  253. do_load(fd, psfhdr, len);
  254. // load the screen map, if any
  255. if (opts & 1) { // -m
  256. unsigned mode = PIO_SCRNMAP;
  257. void *map;
  258. if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not ""
  259. if (mapfilename[0] != '/') {
  260. // goto default keymaps location
  261. chdir(CONFIG_DEFAULT_SETFONT_DIR "/consoletrans");
  262. }
  263. }
  264. // fetch keymap
  265. map = xmalloc_open_zipped_read_close(mapfilename, &len);
  266. if (!map)
  267. bb_simple_perror_msg_and_die(mapfilename);
  268. // file size is 256 or 512 bytes? -> assume binary map
  269. if (len == E_TABSZ || len == 2*E_TABSZ) {
  270. if (len == 2*E_TABSZ)
  271. mode = PIO_UNISCRNMAP;
  272. }
  273. #if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
  274. // assume textual Unicode console maps:
  275. // 0x00 U+0000 # NULL (NUL)
  276. // 0x01 U+0001 # START OF HEADING (SOH)
  277. // 0x02 U+0002 # START OF TEXT (STX)
  278. // 0x03 U+0003 # END OF TEXT (ETX)
  279. else {
  280. int i;
  281. char *token[2];
  282. parser_t *parser;
  283. if (ENABLE_FEATURE_CLEAN_UP)
  284. free(map);
  285. map = xmalloc(E_TABSZ * sizeof(unsigned short));
  286. #define unicodes ((unsigned short *)map)
  287. // fill vanilla map
  288. for (i = 0; i < E_TABSZ; i++)
  289. unicodes[i] = 0xf000 + i;
  290. parser = config_open(mapfilename);
  291. while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL | PARSE_MIN_DIE)) {
  292. // parse code/value pair
  293. int a = ctoi(token[0]);
  294. int b = ctoi(token[1]);
  295. if (a < 0 || a >= E_TABSZ
  296. || b < 0 || b > 65535
  297. ) {
  298. bb_error_msg_and_die("map format");
  299. }
  300. // patch map
  301. unicodes[a] = b;
  302. // unicode character is met?
  303. if (b > 255)
  304. mode = PIO_UNISCRNMAP;
  305. }
  306. if (ENABLE_FEATURE_CLEAN_UP)
  307. config_close(parser);
  308. if (mode != PIO_UNISCRNMAP) {
  309. #define asciis ((unsigned char *)map)
  310. for (i = 0; i < E_TABSZ; i++)
  311. asciis[i] = unicodes[i];
  312. #undef asciis
  313. }
  314. #undef unicodes
  315. }
  316. #endif // ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
  317. // do set screen map
  318. xioctl(fd, mode, map);
  319. if (ENABLE_FEATURE_CLEAN_UP)
  320. free(map);
  321. }
  322. return EXIT_SUCCESS;
  323. }
  324. #endif