blob: b046d40e38642ab7454c9f4809aace7ef06b8e67 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3e0fbae1999-10-19 06:02:44 +00002/*
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 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000011#include <sys/kd.h>
Eric Andersen3e0fbae1999-10-19 06:02:44 +000012
Denis Vlasenko83ea6432006-11-16 02:27:24 +000013enum {
Rob Landleybc68cd12006-03-10 19:22:06 +000014 PSF_MAGIC1 = 0x36,
15 PSF_MAGIC2 = 0x04,
Eric Andersen3e0fbae1999-10-19 06:02:44 +000016
Rob Landleybc68cd12006-03-10 19:22:06 +000017 PSF_MODE512 = 0x01,
18 PSF_MODEHASTAB = 0x02,
19 PSF_MAXMODE = 0x03,
20 PSF_SEPARATOR = 0xFFFF
21};
Eric Andersen3e0fbae1999-10-19 06:02:44 +000022
Erik Andersene49d5ec2000-02-08 19:58:47 +000023struct psf_header {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000024 unsigned char magic1, magic2; /* Magic number */
25 unsigned char mode; /* PSF font mode */
26 unsigned char charsize; /* Character size */
Eric Andersen3e0fbae1999-10-19 06:02:44 +000027};
28
29#define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
30
Eric Andersen5e678872006-01-30 19:48:23 +000031static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +000032{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000033 char *buf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000034 int i;
35
Matt Kraaief5529b2000-10-25 17:00:36 +000036 if (unit < 1 || unit > 32)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000037 bb_error_msg_and_die("bad character size %d", unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000038
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000039 buf = xzalloc(16 * 1024);
40 /*memset(buf, 0, 16 * 1024);*/
Eric Andersen3e0fbae1999-10-19 06:02:44 +000041 for (i = 0; i < fontsize; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000043
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000044#if defined(PIO_FONTX) && !defined(__sparc__)
Eric Andersen3e0fbae1999-10-19 06:02:44 +000045 {
Erik Andersene49d5ec2000-02-08 19:58:47 +000046 struct consolefontdesc cfd;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000047
Erik Andersene49d5ec2000-02-08 19:58:47 +000048 cfd.charcount = fontsize;
49 cfd.charheight = unit;
50 cfd.chardata = buf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000051
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000053 goto ret; /* success */
54 bb_perror_msg("PIO_FONTX ioctl (will try PIO_FONT)");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000055 }
56#endif
Matt Kraaief5529b2000-10-25 17:00:36 +000057 if (ioctl(fd, PIO_FONT, buf))
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000058 bb_perror_msg_and_die("PIO_FONT ioctl");
59 ret:
60 free(buf);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000061}
62
63static void
Erik Andersene49d5ec2000-02-08 19:58:47 +000064do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
65{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000066 struct unimapinit advice;
67 struct unimapdesc ud;
68 struct unipair *up;
69 int ct = 0, maxct;
70 int glyph;
Denis Vlasenko28703012006-12-19 20:32:02 +000071 uint16_t unicode;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000072
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 maxct = tailsz; /* more than enough */
Denis Vlasenkob95636c2006-12-19 23:36:04 +000074 up = xmalloc(maxct * sizeof(struct unipair));
Erik Andersene49d5ec2000-02-08 19:58:47 +000075
Eric Andersen3e0fbae1999-10-19 06:02:44 +000076 for (glyph = 0; glyph < fontsize; glyph++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 while (tailsz >= 2) {
Denis Vlasenko28703012006-12-19 20:32:02 +000078 unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 tailsz -= 2;
80 inbuf += 2;
81 if (unicode == PSF_SEPARATOR)
82 break;
83 up[ct].unicode = unicode;
84 up[ct].fontpos = glyph;
85 ct++;
86 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +000087 }
88
89 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
90 this printf did not work on many kernels */
91
92 advice.advised_hashsize = 0;
93 advice.advised_hashstep = 0;
94 advice.advised_hashlevel = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +000096#ifdef ENOIOCTLCMD
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 if (errno == ENOIOCTLCMD) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000098 bb_error_msg("it seems this kernel is older than 1.1.92");
99 bb_error_msg_and_die("no Unicode mapping table loaded");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 } else
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000101#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 bb_perror_msg_and_die("PIO_UNIMAPCLR");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000103 }
104 ud.entry_ct = ct;
105 ud.entries = up;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 if (ioctl(fd, PIO_UNIMAP, &ud)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107 bb_perror_msg_and_die("PIO_UNIMAP");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000108 }
109}
110
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111static void loadnewfont(int fd)
112{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000113 enum { INBUF_SIZE = 32*1024 + 1 };
114
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000115 int unit;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000116 unsigned inputlth, offset;
117 /* Was on stack, but 32k is a bit too much: */
118 unsigned char *inbuf = xmalloc(INBUF_SIZE);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000119
120 /*
121 * We used to look at the length of the input file
122 * with stat(); now that we accept compressed files,
123 * just read the entire file.
124 */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000125 inputlth = full_read(STDIN_FILENO, inbuf, INBUF_SIZE);
126 if (inputlth < 0)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000127 bb_perror_msg_and_die("error reading input font");
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000128 if (inputlth >= INBUF_SIZE)
129 bb_error_msg_and_die("font too large");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000130
131 /* test for psf first */
132 {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133 struct psf_header psfhdr;
134 int fontsize;
135 int hastable;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000136 unsigned head0, head;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000137
Erik Andersene49d5ec2000-02-08 19:58:47 +0000138 if (inputlth < sizeof(struct psf_header))
139 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000140
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 psfhdr = *(struct psf_header *) &inbuf[0];
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000142
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 if (!PSF_MAGIC_OK(psfhdr))
144 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000145
Matt Kraaief5529b2000-10-25 17:00:36 +0000146 if (psfhdr.mode > PSF_MAXMODE)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000147 bb_error_msg_and_die("unsupported psf file mode");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000149#if !defined(PIO_FONTX) || defined(__sparc__)
Matt Kraaief5529b2000-10-25 17:00:36 +0000150 if (fontsize != 256)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000151 bb_error_msg_and_die("only fontsize 256 supported");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000152#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 hastable = (psfhdr.mode & PSF_MODEHASTAB);
154 unit = psfhdr.charsize;
155 head0 = sizeof(struct psf_header);
156
157 head = head0 + fontsize * unit;
Matt Kraaief5529b2000-10-25 17:00:36 +0000158 if (head > inputlth || (!hastable && head != inputlth))
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000159 bb_error_msg_and_die("input file: bad length");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160 do_loadfont(fd, inbuf + head0, unit, fontsize);
161 if (hastable)
162 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
163 return;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000164 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000165
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000166 no_psf:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000167 /* file with three code pages? */
168 if (inputlth == 9780) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169 offset = 40;
170 unit = 16;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000171 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172 /* bare font */
Matt Kraaief5529b2000-10-25 17:00:36 +0000173 if (inputlth & 0377)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000174 bb_error_msg_and_die("bad input file size");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 offset = 0;
176 unit = inputlth / 256;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000177 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000178 do_loadfont(fd, inbuf + offset, unit, 256);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000179}
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000180
181int loadfont_main(int argc, char **argv);
182int loadfont_main(int argc, char **argv)
183{
184 int fd;
185
186 if (argc != 1)
187 bb_show_usage();
188
189 fd = xopen(CURRENT_VC, O_RDWR);
190 loadnewfont(fd);
191
192 return EXIT_SUCCESS;
193}