3
0

loadfont.c 15 KB

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