loadfont.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 source tree.
  11. */
  12. //config:config LOADFONT
  13. //config: bool "loadfont (5.4 kb)"
  14. //config: default y
  15. //config: help
  16. //config: This program loads a console font from standard input.
  17. //config:
  18. //config:config SETFONT
  19. //config: bool "setfont (24 kb)"
  20. //config: default y
  21. //config: help
  22. //config: Allows to load console screen map. Useful for i18n.
  23. //config:
  24. //config:config FEATURE_SETFONT_TEXTUAL_MAP
  25. //config: bool "Support reading textual screen maps"
  26. //config: default y
  27. //config: depends on SETFONT
  28. //config: help
  29. //config: Support reading textual screen maps.
  30. //config:
  31. //config:config DEFAULT_SETFONT_DIR
  32. //config: string "Default directory for console-tools files"
  33. //config: default ""
  34. //config: depends on SETFONT
  35. //config: help
  36. //config: Directory to use if setfont's params are simple filenames
  37. //config: (not /path/to/file or ./file). Default is "" (no default directory).
  38. //config:
  39. //config:comment "Common options for loadfont and setfont"
  40. //config: depends on LOADFONT || SETFONT
  41. //config:
  42. //config:config FEATURE_LOADFONT_PSF2
  43. //config: bool "Support PSF2 console fonts"
  44. //config: default y
  45. //config: depends on LOADFONT || SETFONT
  46. //config:
  47. //config:config FEATURE_LOADFONT_RAW
  48. //config: bool "Support old (raw) console fonts"
  49. //config: default y
  50. //config: depends on LOADFONT || SETFONT
  51. //applet:IF_LOADFONT(APPLET_NOEXEC(loadfont, loadfont, BB_DIR_USR_SBIN, BB_SUID_DROP, loadfont))
  52. //applet:IF_SETFONT(APPLET_NOEXEC(setfont, setfont, BB_DIR_USR_SBIN, BB_SUID_DROP, setfont))
  53. //kbuild:lib-$(CONFIG_LOADFONT) += loadfont.o
  54. //kbuild:lib-$(CONFIG_SETFONT) += loadfont.o
  55. #include "libbb.h"
  56. #include <sys/kd.h>
  57. #ifndef KDFONTOP
  58. # define KDFONTOP 0x4B72
  59. struct console_font_op {
  60. unsigned op; /* KD_FONT_OP_* */
  61. unsigned flags; /* KD_FONT_FLAG_* */
  62. unsigned width, height;
  63. unsigned charcount;
  64. unsigned char *data; /* font data with height fixed to 32 */
  65. };
  66. # define KD_FONT_OP_SET 0 /* Set font */
  67. # define KD_FONT_OP_GET 1 /* Get font */
  68. # define KD_FONT_OP_SET_DEFAULT 2 /* Set font to default, data points to name / NULL */
  69. # define KD_FONT_OP_COPY 3 /* Copy from another console */
  70. # define KD_FONT_FLAG_OLD 0x80000000 /* Invoked via old interface */
  71. # define KD_FONT_FLAG_DONT_RECALC 1 /* Don't call adjust_height() */
  72. /* (Used internally for PIO_FONT support) */
  73. #endif /* KDFONTOP */
  74. enum {
  75. PSF1_MAGIC0 = 0x36,
  76. PSF1_MAGIC1 = 0x04,
  77. PSF1_MODE512 = 0x01,
  78. PSF1_MODEHASTAB = 0x02,
  79. PSF1_MODEHASSEQ = 0x04,
  80. PSF1_MAXMODE = 0x05,
  81. PSF1_STARTSEQ = 0xfffe,
  82. PSF1_SEPARATOR = 0xffff,
  83. };
  84. struct psf1_header {
  85. unsigned char magic[2]; /* Magic number */
  86. unsigned char mode; /* PSF font mode */
  87. unsigned char charsize; /* Character size */
  88. };
  89. #define psf1h(x) ((struct psf1_header*)(x))
  90. #define PSF1_MAGIC_OK(x) ( \
  91. (x)->magic[0] == PSF1_MAGIC0 \
  92. && (x)->magic[1] == PSF1_MAGIC1 \
  93. )
  94. #if ENABLE_FEATURE_LOADFONT_PSF2
  95. enum {
  96. PSF2_MAGIC0 = 0x72,
  97. PSF2_MAGIC1 = 0xb5,
  98. PSF2_MAGIC2 = 0x4a,
  99. PSF2_MAGIC3 = 0x86,
  100. PSF2_HAS_UNICODE_TABLE = 0x01,
  101. PSF2_MAXVERSION = 0,
  102. PSF2_STARTSEQ = 0xfe,
  103. PSF2_SEPARATOR = 0xff
  104. };
  105. struct psf2_header {
  106. unsigned char magic[4];
  107. unsigned int version;
  108. unsigned int headersize; /* offset of bitmaps in file */
  109. unsigned int flags;
  110. unsigned int length; /* number of glyphs */
  111. unsigned int charsize; /* number of bytes for each character */
  112. unsigned int height; /* max dimensions of glyphs */
  113. unsigned int width; /* charsize = height * ((width + 7) / 8) */
  114. };
  115. #define psf2h(x) ((struct psf2_header*)(x))
  116. #define PSF2_MAGIC_OK(x) ( \
  117. (x)->magic[0] == PSF2_MAGIC0 \
  118. && (x)->magic[1] == PSF2_MAGIC1 \
  119. && (x)->magic[2] == PSF2_MAGIC2 \
  120. && (x)->magic[3] == PSF2_MAGIC3 \
  121. )
  122. #endif /* ENABLE_FEATURE_LOADFONT_PSF2 */
  123. static void do_loadfont(int fd, unsigned char *inbuf, int height, int width, int charsize, int fontsize)
  124. {
  125. unsigned char *buf;
  126. int charwidth = 32 * ((width+7)/8);
  127. int i;
  128. if (height < 1 || height > 32 || width < 1 || width > 32)
  129. bb_error_msg_and_die("bad character size %dx%d", height, width);
  130. buf = xzalloc(charwidth * ((fontsize < 128) ? 128 : fontsize));
  131. for (i = 0; i < fontsize; i++)
  132. memcpy(buf + (i*charwidth), inbuf + (i*charsize), charsize);
  133. { /* KDFONTOP */
  134. struct console_font_op cfo;
  135. cfo.op = KD_FONT_OP_SET;
  136. cfo.flags = 0;
  137. cfo.width = width;
  138. cfo.height = height;
  139. cfo.charcount = fontsize;
  140. cfo.data = buf;
  141. xioctl(fd, KDFONTOP, &cfo);
  142. }
  143. free(buf);
  144. }
  145. /*
  146. * Format of the Unicode information:
  147. *
  148. * For each font position <uc>*<seq>*<term>
  149. * where <uc> is a 2-byte little endian Unicode value (PSF1)
  150. * or an UTF-8 coded value (PSF2),
  151. * <seq> = <ss><uc><uc>*, <ss> = psf1 ? 0xFFFE : 0xFE,
  152. * <term> = psf1 ? 0xFFFF : 0xFF.
  153. * and * denotes zero or more occurrences of the preceding item.
  154. *
  155. * Semantics:
  156. * The leading <uc>* part gives Unicode symbols that are all
  157. * represented by this font position. The following sequences
  158. * are sequences of Unicode symbols - probably a symbol
  159. * together with combining accents - also represented by
  160. * this font position.
  161. *
  162. * Example:
  163. * At the font position for a capital A-ring glyph, we
  164. * may have:
  165. * 00C5,212B,FFFE,0041,030A,FFFF
  166. * Some font positions may be described by sequences only,
  167. * namely when there is no precomposed Unicode value for the glyph.
  168. */
  169. #if !ENABLE_FEATURE_LOADFONT_PSF2
  170. #define do_loadtable(fd, inbuf, tailsz, fontsize, psf2) \
  171. do_loadtable(fd, inbuf, tailsz, fontsize)
  172. #endif
  173. static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize, int psf2)
  174. {
  175. #if !ENABLE_FEATURE_LOADFONT_PSF2
  176. /* gcc 4.3.1 code size: */
  177. # define psf2 0 /* +0 bytes */
  178. // const int psf2 = 0; /* +8 bytes */
  179. // enum { psf2 = 0 }; /* +13 bytes */
  180. #endif
  181. struct unimapinit advice;
  182. struct unimapdesc ud;
  183. struct unipair *up;
  184. int ct = 0, maxct;
  185. int glyph;
  186. uint16_t unicode;
  187. maxct = tailsz; /* more than enough */
  188. up = xmalloc(maxct * sizeof(*up));
  189. for (glyph = 0; glyph < fontsize; glyph++) {
  190. while (tailsz > 0) {
  191. if (!psf2) { /* PSF1 */
  192. unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
  193. tailsz -= 2;
  194. inbuf += 2;
  195. if (unicode == PSF1_SEPARATOR)
  196. break;
  197. } else { /* PSF2 */
  198. #if ENABLE_FEATURE_LOADFONT_PSF2
  199. --tailsz;
  200. unicode = *inbuf++;
  201. if (unicode == PSF2_SEPARATOR) {
  202. break;
  203. } else if (unicode == PSF2_STARTSEQ) {
  204. bb_simple_error_msg_and_die("unicode sequences not implemented");
  205. } else if (unicode >= 0xC0) {
  206. if (unicode >= 0xFC)
  207. unicode &= 0x01, maxct = 5;
  208. else if (unicode >= 0xF8)
  209. unicode &= 0x03, maxct = 4;
  210. else if (unicode >= 0xF0)
  211. unicode &= 0x07, maxct = 3;
  212. else if (unicode >= 0xE0)
  213. unicode &= 0x0F, maxct = 2;
  214. else
  215. unicode &= 0x1F, maxct = 1;
  216. do {
  217. if (tailsz <= 0 || *inbuf < 0x80 || *inbuf > 0xBF)
  218. bb_simple_error_msg_and_die("illegal UTF-8 character");
  219. --tailsz;
  220. unicode = (unicode << 6) + (*inbuf++ & 0x3F);
  221. } while (--maxct > 0);
  222. } else if (unicode >= 0x80) {
  223. bb_simple_error_msg_and_die("illegal UTF-8 character");
  224. }
  225. #else
  226. return;
  227. #endif
  228. }
  229. up[ct].unicode = unicode;
  230. up[ct].fontpos = glyph;
  231. ct++;
  232. }
  233. }
  234. /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
  235. * this printf did not work on many kernels */
  236. advice.advised_hashsize = 0;
  237. advice.advised_hashstep = 0;
  238. advice.advised_hashlevel = 0;
  239. xioctl(fd, PIO_UNIMAPCLR, &advice);
  240. ud.entry_ct = ct;
  241. ud.entries = up;
  242. xioctl(fd, PIO_UNIMAP, &ud);
  243. #undef psf2
  244. }
  245. static void do_load(int fd, unsigned char *buffer, size_t len)
  246. {
  247. int height;
  248. int width = 8;
  249. int charsize;
  250. int fontsize = 256;
  251. int has_table = 0;
  252. unsigned char *font = buffer;
  253. unsigned char *table;
  254. if (len >= sizeof(struct psf1_header) && PSF1_MAGIC_OK(psf1h(buffer))) {
  255. if (psf1h(buffer)->mode > PSF1_MAXMODE)
  256. bb_simple_error_msg_and_die("unsupported psf file mode");
  257. if (psf1h(buffer)->mode & PSF1_MODE512)
  258. fontsize = 512;
  259. if (psf1h(buffer)->mode & PSF1_MODEHASTAB)
  260. has_table = 1;
  261. height = charsize = psf1h(buffer)->charsize;
  262. font += sizeof(struct psf1_header);
  263. } else
  264. #if ENABLE_FEATURE_LOADFONT_PSF2
  265. if (len >= sizeof(struct psf2_header) && PSF2_MAGIC_OK(psf2h(buffer))) {
  266. if (psf2h(buffer)->version > PSF2_MAXVERSION)
  267. bb_simple_error_msg_and_die("unsupported psf file version");
  268. fontsize = psf2h(buffer)->length;
  269. if (psf2h(buffer)->flags & PSF2_HAS_UNICODE_TABLE)
  270. has_table = 2;
  271. charsize = psf2h(buffer)->charsize;
  272. height = psf2h(buffer)->height;
  273. width = psf2h(buffer)->width;
  274. font += psf2h(buffer)->headersize;
  275. } else
  276. #endif
  277. #if ENABLE_FEATURE_LOADFONT_RAW
  278. if (len == 9780) { /* file with three code pages? */
  279. charsize = height = 16;
  280. font += 40;
  281. } else if ((len & 0377) == 0) { /* bare font */
  282. charsize = height = len / 256;
  283. } else
  284. #endif
  285. {
  286. bb_simple_error_msg_and_die("input file: bad length or unsupported font type");
  287. }
  288. #if !defined(PIO_FONTX) || defined(__sparc__)
  289. if (fontsize != 256)
  290. bb_simple_error_msg_and_die("only fontsize 256 supported");
  291. #endif
  292. table = font + fontsize * charsize;
  293. buffer += len;
  294. if (table > buffer || (!has_table && table != buffer))
  295. bb_simple_error_msg_and_die("input file: bad length");
  296. do_loadfont(fd, font, height, width, charsize, fontsize);
  297. if (has_table)
  298. do_loadtable(fd, table, buffer - table, fontsize, has_table - 1);
  299. }
  300. #if ENABLE_LOADFONT
  301. //usage:#define loadfont_trivial_usage
  302. //usage: "< font"
  303. //usage:#define loadfont_full_usage "\n\n"
  304. //usage: "Load a console font from stdin"
  305. /* //usage: "\n -C TTY Affect TTY instead of /dev/tty" */
  306. //usage:
  307. //usage:#define loadfont_example_usage
  308. //usage: "$ loadfont < /etc/i18n/fontname\n"
  309. int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  310. int loadfont_main(int argc UNUSED_PARAM, char **argv)
  311. {
  312. size_t len;
  313. unsigned char *buffer;
  314. // no arguments allowed!
  315. getopt32(argv, "^" "" "\0" "=0");
  316. /*
  317. * We used to look at the length of the input file
  318. * with stat(); now that we accept compressed files,
  319. * just read the entire file.
  320. * Len was 32k, but latarcyrheb-sun32.psfu is 34377 bytes
  321. * (it has largish Unicode map).
  322. */
  323. len = 128*1024;
  324. buffer = xmalloc_read(STDIN_FILENO, &len);
  325. // xmalloc_open_zipped_read_close(filename, &len);
  326. if (!buffer)
  327. bb_simple_perror_msg_and_die("error reading input font");
  328. do_load(get_console_fd_or_die(), buffer, len);
  329. return EXIT_SUCCESS;
  330. }
  331. #endif
  332. #if ENABLE_SETFONT
  333. /* kbd-1.12:
  334. setfont [-O font+umap.orig] [-o font.orig] [-om cmap.orig]
  335. [-ou umap.orig] [-N] [font.new ...] [-m cmap] [-u umap] [-C console]
  336. [-hNN] [-v] [-V]
  337. -h NN Override font height
  338. -o file
  339. Save previous font in file
  340. -O file
  341. Save previous font and Unicode map in file
  342. -om file
  343. Store console map in file
  344. -ou file
  345. Save previous Unicode map in file
  346. -m file
  347. Load console map or Unicode console map from file
  348. -u file
  349. Load Unicode table describing the font from file
  350. Example:
  351. # cp866
  352. 0x00-0x7f idem
  353. #
  354. 0x80 U+0410 # CYRILLIC CAPITAL LETTER A
  355. 0x81 U+0411 # CYRILLIC CAPITAL LETTER BE
  356. 0x82 U+0412 # CYRILLIC CAPITAL LETTER VE
  357. -C console
  358. Set the font for the indicated console
  359. -v Verbose
  360. -V Version
  361. */
  362. //usage:#define setfont_trivial_usage
  363. //usage: "[-m MAPFILE] [-C TTY] FILE"
  364. //usage:#define setfont_full_usage "\n\n"
  365. //usage: "Load a console font\n"
  366. //usage: "\n -m MAPFILE Load console screen map"
  367. //usage: "\n -C TTY Affect TTY instead of /dev/tty"
  368. //usage:
  369. //usage:#define setfont_example_usage
  370. //usage: "$ setfont -m koi8-r /etc/i18n/fontname\n"
  371. # if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
  372. static int ctoi(char *s)
  373. {
  374. if (s[0] == '\'' && s[1] != '\0' && s[2] == '\'' && s[3] == '\0')
  375. return s[1];
  376. // U+ means 0x
  377. if (s[0] == 'U' && s[1] == '+') {
  378. s[0] = '0';
  379. s[1] = 'x';
  380. }
  381. if (!isdigit(s[0]))
  382. return -1;
  383. return xstrtoul(s, 0);
  384. }
  385. # endif
  386. int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  387. int setfont_main(int argc UNUSED_PARAM, char **argv)
  388. {
  389. size_t len;
  390. unsigned opts;
  391. int fd;
  392. unsigned char *buffer;
  393. char *mapfilename;
  394. const char *tty_name = CURRENT_TTY;
  395. opts = getopt32(argv, "^" "m:C:" "\0" "=1", &mapfilename, &tty_name);
  396. argv += optind;
  397. fd = xopen_nonblocking(tty_name);
  398. if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not ""
  399. if (*argv[0] != '/') {
  400. // goto default fonts location. don't die if doesn't exist
  401. chdir(CONFIG_DEFAULT_SETFONT_DIR "/consolefonts");
  402. }
  403. }
  404. // load font
  405. len = 128*1024;
  406. buffer = xmalloc_open_zipped_read_close(*argv, &len);
  407. if (!buffer)
  408. bb_simple_perror_msg_and_die(*argv);
  409. do_load(fd, buffer, len);
  410. // load the screen map, if any
  411. if (opts & 1) { // -m
  412. unsigned mode = PIO_SCRNMAP;
  413. void *map;
  414. if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not ""
  415. if (mapfilename[0] != '/') {
  416. // goto default keymaps location
  417. chdir(CONFIG_DEFAULT_SETFONT_DIR "/consoletrans");
  418. }
  419. }
  420. // fetch keymap
  421. map = xmalloc_open_zipped_read_close(mapfilename, &len);
  422. if (!map)
  423. bb_simple_perror_msg_and_die(mapfilename);
  424. // file size is 256 or 512 bytes? -> assume binary map
  425. if (len == E_TABSZ || len == 2*E_TABSZ) {
  426. if (len == 2*E_TABSZ)
  427. mode = PIO_UNISCRNMAP;
  428. }
  429. # if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
  430. // assume textual Unicode console maps:
  431. // 0x00 U+0000 # NULL (NUL)
  432. // 0x01 U+0001 # START OF HEADING (SOH)
  433. // 0x02 U+0002 # START OF TEXT (STX)
  434. // 0x03 U+0003 # END OF TEXT (ETX)
  435. else {
  436. int i;
  437. char *token[2];
  438. parser_t *parser;
  439. if (ENABLE_FEATURE_CLEAN_UP)
  440. free(map);
  441. map = xmalloc(E_TABSZ * sizeof(unsigned short));
  442. #define unicodes ((unsigned short *)map)
  443. // fill vanilla map
  444. for (i = 0; i < E_TABSZ; i++)
  445. unicodes[i] = 0xf000 + i;
  446. parser = config_open(mapfilename);
  447. while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL | PARSE_MIN_DIE)) {
  448. // parse code/value pair
  449. int a = ctoi(token[0]);
  450. int b = ctoi(token[1]);
  451. if (a < 0 || a >= E_TABSZ
  452. || b < 0 || b > 65535
  453. ) {
  454. bb_simple_error_msg_and_die("map format");
  455. }
  456. // patch map
  457. unicodes[a] = b;
  458. // unicode character is met?
  459. if (b > 255)
  460. mode = PIO_UNISCRNMAP;
  461. }
  462. if (ENABLE_FEATURE_CLEAN_UP)
  463. config_close(parser);
  464. if (mode != PIO_UNISCRNMAP) {
  465. #define asciis ((unsigned char *)map)
  466. for (i = 0; i < E_TABSZ; i++)
  467. asciis[i] = unicodes[i];
  468. #undef asciis
  469. }
  470. #undef unicodes
  471. }
  472. # endif // ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
  473. // do set screen map
  474. xioctl(fd, mode, map);
  475. if (ENABLE_FEATURE_CLEAN_UP)
  476. free(map);
  477. }
  478. return EXIT_SUCCESS;
  479. }
  480. #endif