Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 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.) |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 11 | */ |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 12 | //config:config LOADFONT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 13 | //config: bool "loadfont (5.2 kb)" |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 14 | //config: default y |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 15 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | //config: This program loads a console font from standard input. |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 17 | //config: |
| 18 | //config:config SETFONT |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 19 | //config: bool "setfont (24 kb)" |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 20 | //config: default y |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 21 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 22 | //config: Allows to load console screen map. Useful for i18n. |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 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 |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 29 | //config: Support reading textual screen maps. |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 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 |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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). |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 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 |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 43 | //config: bool "Support PSF2 console fonts" |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 44 | //config: default y |
| 45 | //config: depends on LOADFONT || SETFONT |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 46 | //config: |
| 47 | //config:config FEATURE_LOADFONT_RAW |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 48 | //config: bool "Support old (raw) console fonts" |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 49 | //config: default y |
| 50 | //config: depends on LOADFONT || SETFONT |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 51 | |
Denys Vlasenko | 1b280e4 | 2017-08-06 19:05:45 +0200 | [diff] [blame] | 52 | //applet:IF_LOADFONT(APPLET_NOEXEC(loadfont, loadfont, BB_DIR_USR_SBIN, BB_SUID_DROP, loadfont)) |
| 53 | //applet:IF_SETFONT(APPLET_NOEXEC(setfont, setfont, BB_DIR_USR_SBIN, BB_SUID_DROP, setfont)) |
Denys Vlasenko | 6d93299 | 2016-11-23 10:39:27 +0100 | [diff] [blame] | 54 | |
| 55 | //kbuild:lib-$(CONFIG_LOADFONT) += loadfont.o |
| 56 | //kbuild:lib-$(CONFIG_SETFONT) += loadfont.o |
Pere Orga | 55068c4 | 2011-03-27 23:42:28 +0200 | [diff] [blame] | 57 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 58 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 59 | #include <sys/kd.h> |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 61 | #ifndef KDFONTOP |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 62 | # define KDFONTOP 0x4B72 |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 63 | struct console_font_op { |
| 64 | unsigned op; /* KD_FONT_OP_* */ |
| 65 | unsigned flags; /* KD_FONT_FLAG_* */ |
| 66 | unsigned width, height; |
| 67 | unsigned charcount; |
| 68 | unsigned char *data; /* font data with height fixed to 32 */ |
| 69 | }; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 70 | # define KD_FONT_OP_SET 0 /* Set font */ |
| 71 | # define KD_FONT_OP_GET 1 /* Get font */ |
| 72 | # define KD_FONT_OP_SET_DEFAULT 2 /* Set font to default, data points to name / NULL */ |
| 73 | # define KD_FONT_OP_COPY 3 /* Copy from another console */ |
| 74 | # define KD_FONT_FLAG_OLD 0x80000000 /* Invoked via old interface */ |
| 75 | # define KD_FONT_FLAG_DONT_RECALC 1 /* Don't call adjust_height() */ |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 76 | /* (Used internally for PIO_FONT support) */ |
| 77 | #endif /* KDFONTOP */ |
| 78 | |
| 79 | |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 80 | enum { |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 81 | PSF1_MAGIC0 = 0x36, |
| 82 | PSF1_MAGIC1 = 0x04, |
| 83 | PSF1_MODE512 = 0x01, |
| 84 | PSF1_MODEHASTAB = 0x02, |
| 85 | PSF1_MODEHASSEQ = 0x04, |
| 86 | PSF1_MAXMODE = 0x05, |
| 87 | PSF1_STARTSEQ = 0xfffe, |
| 88 | PSF1_SEPARATOR = 0xffff, |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 89 | }; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 90 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 91 | struct psf1_header { |
| 92 | unsigned char magic[2]; /* Magic number */ |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 93 | unsigned char mode; /* PSF font mode */ |
| 94 | unsigned char charsize; /* Character size */ |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 97 | #define psf1h(x) ((struct psf1_header*)(x)) |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 98 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 99 | #define PSF1_MAGIC_OK(x) ( \ |
| 100 | (x)->magic[0] == PSF1_MAGIC0 \ |
| 101 | && (x)->magic[1] == PSF1_MAGIC1 \ |
| 102 | ) |
| 103 | |
| 104 | #if ENABLE_FEATURE_LOADFONT_PSF2 |
| 105 | enum { |
| 106 | PSF2_MAGIC0 = 0x72, |
| 107 | PSF2_MAGIC1 = 0xb5, |
| 108 | PSF2_MAGIC2 = 0x4a, |
| 109 | PSF2_MAGIC3 = 0x86, |
| 110 | PSF2_HAS_UNICODE_TABLE = 0x01, |
| 111 | PSF2_MAXVERSION = 0, |
| 112 | PSF2_STARTSEQ = 0xfe, |
| 113 | PSF2_SEPARATOR = 0xff |
| 114 | }; |
| 115 | |
| 116 | struct psf2_header { |
| 117 | unsigned char magic[4]; |
| 118 | unsigned int version; |
| 119 | unsigned int headersize; /* offset of bitmaps in file */ |
| 120 | unsigned int flags; |
| 121 | unsigned int length; /* number of glyphs */ |
| 122 | unsigned int charsize; /* number of bytes for each character */ |
| 123 | unsigned int height; /* max dimensions of glyphs */ |
| 124 | unsigned int width; /* charsize = height * ((width + 7) / 8) */ |
| 125 | }; |
| 126 | |
| 127 | #define psf2h(x) ((struct psf2_header*)(x)) |
| 128 | |
| 129 | #define PSF2_MAGIC_OK(x) ( \ |
| 130 | (x)->magic[0] == PSF2_MAGIC0 \ |
| 131 | && (x)->magic[1] == PSF2_MAGIC1 \ |
| 132 | && (x)->magic[2] == PSF2_MAGIC2 \ |
| 133 | && (x)->magic[3] == PSF2_MAGIC3 \ |
| 134 | ) |
| 135 | #endif /* ENABLE_FEATURE_LOADFONT_PSF2 */ |
| 136 | |
| 137 | |
| 138 | static void do_loadfont(int fd, unsigned char *inbuf, int height, int width, int charsize, int fontsize) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 139 | { |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 140 | unsigned char *buf; |
| 141 | int charwidth = 32 * ((width+7)/8); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 142 | int i; |
| 143 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 144 | if (height < 1 || height > 32 || width < 1 || width > 32) |
| 145 | bb_error_msg_and_die("bad character size %dx%d", height, width); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 146 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 147 | buf = xzalloc(charwidth * ((fontsize < 128) ? 128 : fontsize)); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 148 | for (i = 0; i < fontsize; i++) |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 149 | memcpy(buf + (i*charwidth), inbuf + (i*charsize), charsize); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 151 | { /* KDFONTOP */ |
| 152 | struct console_font_op cfo; |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 153 | cfo.op = KD_FONT_OP_SET; |
| 154 | cfo.flags = 0; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 155 | cfo.width = width; |
| 156 | cfo.height = height; |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 157 | cfo.charcount = fontsize; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 158 | cfo.data = buf; |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 159 | xioctl(fd, KDFONTOP, &cfo); |
Denis Vlasenko | eef6077 | 2008-09-20 18:14:13 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 162 | free(buf); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 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: |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 185 | * 00C5,212B,FFFE,0041,030A,FFFF |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 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) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 194 | { |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 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 |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 201 | struct unimapinit advice; |
| 202 | struct unimapdesc ud; |
| 203 | struct unipair *up; |
| 204 | int ct = 0, maxct; |
| 205 | int glyph; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 206 | uint16_t unicode; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 207 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 208 | maxct = tailsz; /* more than enough */ |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 209 | up = xmalloc(maxct * sizeof(*up)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 210 | |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 211 | for (glyph = 0; glyph < fontsize; glyph++) { |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 212 | while (tailsz > 0) { |
| 213 | if (!psf2) { /* PSF1 */ |
| 214 | unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0]; |
| 215 | tailsz -= 2; |
| 216 | inbuf += 2; |
| 217 | if (unicode == PSF1_SEPARATOR) |
| 218 | break; |
| 219 | } else { /* PSF2 */ |
| 220 | #if ENABLE_FEATURE_LOADFONT_PSF2 |
| 221 | --tailsz; |
| 222 | unicode = *inbuf++; |
| 223 | if (unicode == PSF2_SEPARATOR) { |
| 224 | break; |
| 225 | } else if (unicode == PSF2_STARTSEQ) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 226 | bb_simple_error_msg_and_die("unicode sequences not implemented"); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 227 | } else if (unicode >= 0xC0) { |
| 228 | if (unicode >= 0xFC) |
| 229 | unicode &= 0x01, maxct = 5; |
| 230 | else if (unicode >= 0xF8) |
| 231 | unicode &= 0x03, maxct = 4; |
| 232 | else if (unicode >= 0xF0) |
| 233 | unicode &= 0x07, maxct = 3; |
| 234 | else if (unicode >= 0xE0) |
| 235 | unicode &= 0x0F, maxct = 2; |
| 236 | else |
| 237 | unicode &= 0x1F, maxct = 1; |
| 238 | do { |
| 239 | if (tailsz <= 0 || *inbuf < 0x80 || *inbuf > 0xBF) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 240 | bb_simple_error_msg_and_die("illegal UTF-8 character"); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 241 | --tailsz; |
| 242 | unicode = (unicode << 6) + (*inbuf++ & 0x3F); |
| 243 | } while (--maxct > 0); |
| 244 | } else if (unicode >= 0x80) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 245 | bb_simple_error_msg_and_die("illegal UTF-8 character"); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 246 | } |
| 247 | #else |
| 248 | return; |
| 249 | #endif |
| 250 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 251 | up[ct].unicode = unicode; |
| 252 | up[ct].fontpos = glyph; |
| 253 | ct++; |
| 254 | } |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 258 | * this printf did not work on many kernels */ |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 259 | |
| 260 | advice.advised_hashsize = 0; |
| 261 | advice.advised_hashstep = 0; |
| 262 | advice.advised_hashlevel = 0; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 263 | xioctl(fd, PIO_UNIMAPCLR, &advice); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 264 | ud.entry_ct = ct; |
| 265 | ud.entries = up; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 266 | xioctl(fd, PIO_UNIMAP, &ud); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 267 | #undef psf2 |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 270 | static void do_load(int fd, unsigned char *buffer, size_t len) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 271 | { |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 272 | int height; |
| 273 | int width = 8; |
| 274 | int charsize; |
| 275 | int fontsize = 256; |
| 276 | int has_table = 0; |
| 277 | unsigned char *font = buffer; |
| 278 | unsigned char *table; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 279 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 280 | if (len >= sizeof(struct psf1_header) && PSF1_MAGIC_OK(psf1h(buffer))) { |
| 281 | if (psf1h(buffer)->mode > PSF1_MAXMODE) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 282 | bb_simple_error_msg_and_die("unsupported psf file mode"); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 283 | if (psf1h(buffer)->mode & PSF1_MODE512) |
| 284 | fontsize = 512; |
| 285 | if (psf1h(buffer)->mode & PSF1_MODEHASTAB) |
| 286 | has_table = 1; |
| 287 | height = charsize = psf1h(buffer)->charsize; |
| 288 | font += sizeof(struct psf1_header); |
| 289 | } else |
| 290 | #if ENABLE_FEATURE_LOADFONT_PSF2 |
| 291 | if (len >= sizeof(struct psf2_header) && PSF2_MAGIC_OK(psf2h(buffer))) { |
| 292 | if (psf2h(buffer)->version > PSF2_MAXVERSION) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 293 | bb_simple_error_msg_and_die("unsupported psf file version"); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 294 | fontsize = psf2h(buffer)->length; |
| 295 | if (psf2h(buffer)->flags & PSF2_HAS_UNICODE_TABLE) |
| 296 | has_table = 2; |
| 297 | charsize = psf2h(buffer)->charsize; |
| 298 | height = psf2h(buffer)->height; |
| 299 | width = psf2h(buffer)->width; |
| 300 | font += psf2h(buffer)->headersize; |
| 301 | } else |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 302 | #endif |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 303 | #if ENABLE_FEATURE_LOADFONT_RAW |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 304 | if (len == 9780) { /* file with three code pages? */ |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 305 | charsize = height = 16; |
| 306 | font += 40; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 307 | } else if ((len & 0377) == 0) { /* bare font */ |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 308 | charsize = height = len / 256; |
| 309 | } else |
| 310 | #endif |
| 311 | { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 312 | bb_simple_error_msg_and_die("input file: bad length or unsupported font type"); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 315 | #if !defined(PIO_FONTX) || defined(__sparc__) |
| 316 | if (fontsize != 256) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 317 | bb_simple_error_msg_and_die("only fontsize 256 supported"); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 318 | #endif |
| 319 | |
| 320 | table = font + fontsize * charsize; |
| 321 | buffer += len; |
| 322 | |
| 323 | if (table > buffer || (!has_table && table != buffer)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 324 | bb_simple_error_msg_and_die("input file: bad length"); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 325 | |
| 326 | do_loadfont(fd, font, height, width, charsize, fontsize); |
| 327 | |
| 328 | if (has_table) |
| 329 | do_loadtable(fd, table, buffer - table, fontsize, has_table - 1); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 332 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 333 | #if ENABLE_LOADFONT |
Denys Vlasenko | 1b280e4 | 2017-08-06 19:05:45 +0200 | [diff] [blame] | 334 | //usage:#define loadfont_trivial_usage |
| 335 | //usage: "< font" |
| 336 | //usage:#define loadfont_full_usage "\n\n" |
| 337 | //usage: "Load a console font from stdin" |
| 338 | /* //usage: "\n -C TTY Affect TTY instead of /dev/tty" */ |
| 339 | //usage: |
| 340 | //usage:#define loadfont_example_usage |
| 341 | //usage: "$ loadfont < /etc/i18n/fontname\n" |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 342 | int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 343 | int loadfont_main(int argc UNUSED_PARAM, char **argv) |
| 344 | { |
| 345 | size_t len; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 346 | unsigned char *buffer; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 347 | |
| 348 | // no arguments allowed! |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 349 | getopt32(argv, "^" "" "\0" "=0"); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 350 | |
| 351 | /* |
| 352 | * We used to look at the length of the input file |
| 353 | * with stat(); now that we accept compressed files, |
| 354 | * just read the entire file. |
Denys Vlasenko | 2b28823 | 2016-11-03 20:57:37 +0100 | [diff] [blame] | 355 | * Len was 32k, but latarcyrheb-sun32.psfu is 34377 bytes |
| 356 | * (it has largish Unicode map). |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 357 | */ |
Denys Vlasenko | 2b28823 | 2016-11-03 20:57:37 +0100 | [diff] [blame] | 358 | len = 128*1024; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 359 | buffer = xmalloc_read(STDIN_FILENO, &len); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 360 | // xmalloc_open_zipped_read_close(filename, &len); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 361 | if (!buffer) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 362 | bb_simple_perror_msg_and_die("error reading input font"); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 363 | do_load(get_console_fd_or_die(), buffer, len); |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 364 | |
| 365 | return EXIT_SUCCESS; |
| 366 | } |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 367 | #endif |
| 368 | |
Denys Vlasenko | 1b280e4 | 2017-08-06 19:05:45 +0200 | [diff] [blame] | 369 | |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 370 | #if ENABLE_SETFONT |
Denys Vlasenko | 1b280e4 | 2017-08-06 19:05:45 +0200 | [diff] [blame] | 371 | /* kbd-1.12: |
Denis Vlasenko | 2bc5c03 | 2008-09-13 18:27:32 +0000 | [diff] [blame] | 372 | setfont [-O font+umap.orig] [-o font.orig] [-om cmap.orig] |
| 373 | [-ou umap.orig] [-N] [font.new ...] [-m cmap] [-u umap] [-C console] |
| 374 | [-hNN] [-v] [-V] |
| 375 | |
| 376 | -h NN Override font height |
| 377 | -o file |
| 378 | Save previous font in file |
| 379 | -O file |
| 380 | Save previous font and Unicode map in file |
| 381 | -om file |
| 382 | Store console map in file |
| 383 | -ou file |
| 384 | Save previous Unicode map in file |
| 385 | -m file |
| 386 | Load console map or Unicode console map from file |
| 387 | -u file |
| 388 | Load Unicode table describing the font from file |
| 389 | Example: |
| 390 | # cp866 |
| 391 | 0x00-0x7f idem |
| 392 | # |
| 393 | 0x80 U+0410 # CYRILLIC CAPITAL LETTER A |
| 394 | 0x81 U+0411 # CYRILLIC CAPITAL LETTER BE |
| 395 | 0x82 U+0412 # CYRILLIC CAPITAL LETTER VE |
| 396 | -C console |
| 397 | Set the font for the indicated console |
| 398 | -v Verbose |
| 399 | -V Version |
| 400 | */ |
Denys Vlasenko | 1b280e4 | 2017-08-06 19:05:45 +0200 | [diff] [blame] | 401 | //usage:#define setfont_trivial_usage |
Denys Vlasenko | 6f7b10c | 2021-06-13 03:51:55 +0200 | [diff] [blame] | 402 | //usage: "[-m MAPFILE] [-C TTY] FILE" |
Denys Vlasenko | 1b280e4 | 2017-08-06 19:05:45 +0200 | [diff] [blame] | 403 | //usage:#define setfont_full_usage "\n\n" |
| 404 | //usage: "Load a console font\n" |
| 405 | //usage: "\n -m MAPFILE Load console screen map" |
| 406 | //usage: "\n -C TTY Affect TTY instead of /dev/tty" |
| 407 | //usage: |
| 408 | //usage:#define setfont_example_usage |
| 409 | //usage: "$ setfont -m koi8-r /etc/i18n/fontname\n" |
Denis Vlasenko | 2bc5c03 | 2008-09-13 18:27:32 +0000 | [diff] [blame] | 410 | |
Denys Vlasenko | 1b280e4 | 2017-08-06 19:05:45 +0200 | [diff] [blame] | 411 | # if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 412 | static int ctoi(char *s) |
| 413 | { |
| 414 | if (s[0] == '\'' && s[1] != '\0' && s[2] == '\'' && s[3] == '\0') |
| 415 | return s[1]; |
| 416 | // U+ means 0x |
| 417 | if (s[0] == 'U' && s[1] == '+') { |
| 418 | s[0] = '0'; |
| 419 | s[1] = 'x'; |
| 420 | } |
| 421 | if (!isdigit(s[0])) |
| 422 | return -1; |
| 423 | return xstrtoul(s, 0); |
| 424 | } |
Denys Vlasenko | 1b280e4 | 2017-08-06 19:05:45 +0200 | [diff] [blame] | 425 | # endif |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 426 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 427 | int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 428 | int setfont_main(int argc UNUSED_PARAM, char **argv) |
| 429 | { |
| 430 | size_t len; |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 431 | unsigned opts; |
| 432 | int fd; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 433 | unsigned char *buffer; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 434 | char *mapfilename; |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 435 | const char *tty_name = CURRENT_TTY; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 436 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 437 | opts = getopt32(argv, "^" "m:C:" "\0" "=1", &mapfilename, &tty_name); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 438 | argv += optind; |
| 439 | |
Bernhard Reutner-Fischer | a483087 | 2009-10-26 23:27:08 +0100 | [diff] [blame] | 440 | fd = xopen_nonblocking(tty_name); |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 441 | |
| 442 | if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not "" |
Denis Vlasenko | 72fa70a | 2008-09-18 01:01:02 +0000 | [diff] [blame] | 443 | if (*argv[0] != '/') { |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 444 | // goto default fonts location. don't die if doesn't exist |
| 445 | chdir(CONFIG_DEFAULT_SETFONT_DIR "/consolefonts"); |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 448 | // load font |
Denys Vlasenko | 2b28823 | 2016-11-03 20:57:37 +0100 | [diff] [blame] | 449 | len = 128*1024; |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 450 | buffer = xmalloc_open_zipped_read_close(*argv, &len); |
| 451 | if (!buffer) |
Denis Vlasenko | 72fa70a | 2008-09-18 01:01:02 +0000 | [diff] [blame] | 452 | bb_simple_perror_msg_and_die(*argv); |
Harald Becker | 8ce1dc0 | 2010-02-21 13:10:26 +0100 | [diff] [blame] | 453 | do_load(fd, buffer, len); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 454 | |
| 455 | // load the screen map, if any |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 456 | if (opts & 1) { // -m |
| 457 | unsigned mode = PIO_SCRNMAP; |
| 458 | void *map; |
| 459 | |
| 460 | if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not "" |
Denis Vlasenko | 72fa70a | 2008-09-18 01:01:02 +0000 | [diff] [blame] | 461 | if (mapfilename[0] != '/') { |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 462 | // goto default keymaps location |
| 463 | chdir(CONFIG_DEFAULT_SETFONT_DIR "/consoletrans"); |
| 464 | } |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 465 | } |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 466 | // fetch keymap |
| 467 | map = xmalloc_open_zipped_read_close(mapfilename, &len); |
| 468 | if (!map) |
| 469 | bb_simple_perror_msg_and_die(mapfilename); |
| 470 | // file size is 256 or 512 bytes? -> assume binary map |
| 471 | if (len == E_TABSZ || len == 2*E_TABSZ) { |
| 472 | if (len == 2*E_TABSZ) |
| 473 | mode = PIO_UNISCRNMAP; |
| 474 | } |
Denys Vlasenko | 1b280e4 | 2017-08-06 19:05:45 +0200 | [diff] [blame] | 475 | # if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 476 | // assume textual Unicode console maps: |
| 477 | // 0x00 U+0000 # NULL (NUL) |
| 478 | // 0x01 U+0001 # START OF HEADING (SOH) |
| 479 | // 0x02 U+0002 # START OF TEXT (STX) |
| 480 | // 0x03 U+0003 # END OF TEXT (ETX) |
| 481 | else { |
| 482 | int i; |
| 483 | char *token[2]; |
| 484 | parser_t *parser; |
| 485 | |
| 486 | if (ENABLE_FEATURE_CLEAN_UP) |
| 487 | free(map); |
| 488 | map = xmalloc(E_TABSZ * sizeof(unsigned short)); |
| 489 | |
| 490 | #define unicodes ((unsigned short *)map) |
| 491 | // fill vanilla map |
| 492 | for (i = 0; i < E_TABSZ; i++) |
| 493 | unicodes[i] = 0xf000 + i; |
| 494 | |
| 495 | parser = config_open(mapfilename); |
| 496 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL | PARSE_MIN_DIE)) { |
| 497 | // parse code/value pair |
| 498 | int a = ctoi(token[0]); |
| 499 | int b = ctoi(token[1]); |
| 500 | if (a < 0 || a >= E_TABSZ |
| 501 | || b < 0 || b > 65535 |
| 502 | ) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 503 | bb_simple_error_msg_and_die("map format"); |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 504 | } |
| 505 | // patch map |
| 506 | unicodes[a] = b; |
| 507 | // unicode character is met? |
| 508 | if (b > 255) |
| 509 | mode = PIO_UNISCRNMAP; |
| 510 | } |
| 511 | if (ENABLE_FEATURE_CLEAN_UP) |
| 512 | config_close(parser); |
| 513 | |
| 514 | if (mode != PIO_UNISCRNMAP) { |
| 515 | #define asciis ((unsigned char *)map) |
| 516 | for (i = 0; i < E_TABSZ; i++) |
| 517 | asciis[i] = unicodes[i]; |
| 518 | #undef asciis |
| 519 | } |
| 520 | #undef unicodes |
| 521 | } |
Denys Vlasenko | 1b280e4 | 2017-08-06 19:05:45 +0200 | [diff] [blame] | 522 | # endif // ENABLE_FEATURE_SETFONT_TEXTUAL_MAP |
Denis Vlasenko | 53f219e | 2008-09-16 19:35:42 +0000 | [diff] [blame] | 523 | |
| 524 | // do set screen map |
| 525 | xioctl(fd, mode, map); |
| 526 | |
| 527 | if (ENABLE_FEATURE_CLEAN_UP) |
| 528 | free(map); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | return EXIT_SUCCESS; |
| 532 | } |
| 533 | #endif |