loadfont.c 15 KB

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