loadfont.c 9.2 KB

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