3
0

loadfont.c 15 KB

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