blob: 882b8817ad75c3658d2d2b9c7468e87fab6998ef [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
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000052 if (!ioctl_or_perror(fd, PIO_FONTX, &cfd, "PIO_FONTX ioctl failed (will try PIO_FONT)"))
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000053 goto ret; /* success */
Eric Andersen3e0fbae1999-10-19 06:02:44 +000054 }
55#endif
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000056 xioctl(fd, PIO_FONT, buf);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000057 ret:
58 free(buf);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000059}
60
61static void
Erik Andersene49d5ec2000-02-08 19:58:47 +000062do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
63{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000064 struct unimapinit advice;
65 struct unimapdesc ud;
66 struct unipair *up;
67 int ct = 0, maxct;
68 int glyph;
Denis Vlasenko28703012006-12-19 20:32:02 +000069 uint16_t unicode;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000070
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 maxct = tailsz; /* more than enough */
Denis Vlasenkob95636c2006-12-19 23:36:04 +000072 up = xmalloc(maxct * sizeof(struct unipair));
Erik Andersene49d5ec2000-02-08 19:58:47 +000073
Eric Andersen3e0fbae1999-10-19 06:02:44 +000074 for (glyph = 0; glyph < fontsize; glyph++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 while (tailsz >= 2) {
Denis Vlasenko28703012006-12-19 20:32:02 +000076 unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 tailsz -= 2;
78 inbuf += 2;
79 if (unicode == PSF_SEPARATOR)
80 break;
81 up[ct].unicode = unicode;
82 up[ct].fontpos = glyph;
83 ct++;
84 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +000085 }
86
87 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
88 this printf did not work on many kernels */
89
90 advice.advised_hashsize = 0;
91 advice.advised_hashstep = 0;
92 advice.advised_hashlevel = 0;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000093 xioctl(fd, PIO_UNIMAPCLR, &advice);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000094 ud.entry_ct = ct;
95 ud.entries = up;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000096 xioctl(fd, PIO_UNIMAP, &ud);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000097}
98
Erik Andersene49d5ec2000-02-08 19:58:47 +000099static void loadnewfont(int fd)
100{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000101 enum { INBUF_SIZE = 32*1024 + 1 };
102
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000103 int unit;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000104 unsigned inputlth, offset;
105 /* Was on stack, but 32k is a bit too much: */
106 unsigned char *inbuf = xmalloc(INBUF_SIZE);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000107
108 /*
109 * We used to look at the length of the input file
110 * with stat(); now that we accept compressed files,
111 * just read the entire file.
112 */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000113 inputlth = full_read(STDIN_FILENO, inbuf, INBUF_SIZE);
114 if (inputlth < 0)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000115 bb_perror_msg_and_die("error reading input font");
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000116 if (inputlth >= INBUF_SIZE)
117 bb_error_msg_and_die("font too large");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000118
119 /* test for psf first */
120 {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 struct psf_header psfhdr;
122 int fontsize;
123 int hastable;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000124 unsigned head0, head;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000125
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 if (inputlth < sizeof(struct psf_header))
127 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000128
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 psfhdr = *(struct psf_header *) &inbuf[0];
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000130
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 if (!PSF_MAGIC_OK(psfhdr))
132 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000133
Matt Kraaief5529b2000-10-25 17:00:36 +0000134 if (psfhdr.mode > PSF_MAXMODE)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000135 bb_error_msg_and_die("unsupported psf file mode");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000137#if !defined(PIO_FONTX) || defined(__sparc__)
Matt Kraaief5529b2000-10-25 17:00:36 +0000138 if (fontsize != 256)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000139 bb_error_msg_and_die("only fontsize 256 supported");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000140#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 hastable = (psfhdr.mode & PSF_MODEHASTAB);
142 unit = psfhdr.charsize;
143 head0 = sizeof(struct psf_header);
144
145 head = head0 + fontsize * unit;
Matt Kraaief5529b2000-10-25 17:00:36 +0000146 if (head > inputlth || (!hastable && head != inputlth))
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000147 bb_error_msg_and_die("input file: bad length");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 do_loadfont(fd, inbuf + head0, unit, fontsize);
149 if (hastable)
150 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
151 return;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000152 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000153
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000154 no_psf:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000155 /* file with three code pages? */
156 if (inputlth == 9780) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 offset = 40;
158 unit = 16;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000159 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160 /* bare font */
Matt Kraaief5529b2000-10-25 17:00:36 +0000161 if (inputlth & 0377)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000162 bb_error_msg_and_die("bad input file size");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 offset = 0;
164 unit = inputlth / 256;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000165 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000166 do_loadfont(fd, inbuf + offset, unit, 256);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000167}
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000168
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000169int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000170int loadfont_main(int argc, char **argv)
171{
172 int fd;
173
174 if (argc != 1)
175 bb_show_usage();
176
177 fd = xopen(CURRENT_VC, O_RDWR);
178 loadnewfont(fd);
179
180 return EXIT_SUCCESS;
181}