blob: f691b11af8cdf6bdd6265376998aba7a42a223b7 [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 */
Eric Andersencbe31da2001-02-20 06:14:08 +000010#include "busybox.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 {
24 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
31static void loadnewfont(int fd);
32
Denis Vlasenko06af2162007-02-03 17:28:39 +000033int loadfont_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000034int loadfont_main(int argc, char **argv)
Eric Andersen3e0fbae1999-10-19 06:02:44 +000035{
36 int fd;
37
Matt Kraaief5529b2000-10-25 17:00:36 +000038 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 bb_show_usage();
Erik Andersena6c75222000-04-18 00:00:52 +000040
Rob Landleyd921b2e2006-08-03 15:41:12 +000041 fd = xopen(CURRENT_VC, O_RDWR);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000042 loadnewfont(fd);
43
Matt Kraaief5529b2000-10-25 17:00:36 +000044 return EXIT_SUCCESS;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000045}
46
Eric Andersen5e678872006-01-30 19:48:23 +000047static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +000048{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000049 char buf[16384];
50 int i;
51
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 memset(buf, 0, sizeof(buf));
Eric Andersen3e0fbae1999-10-19 06:02:44 +000053
Matt Kraaief5529b2000-10-25 17:00:36 +000054 if (unit < 1 || unit > 32)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000055 bb_error_msg_and_die("bad character size %d", unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000056
57 for (i = 0; i < fontsize; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000059
Eric Andersene77ae3a1999-10-19 20:03:34 +000060#if defined( PIO_FONTX ) && !defined( __sparc__ )
Eric Andersen3e0fbae1999-10-19 06:02:44 +000061 {
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 struct consolefontdesc cfd;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000063
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 cfd.charcount = fontsize;
65 cfd.charheight = unit;
66 cfd.chardata = buf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000067
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
69 return; /* success */
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000071 }
72#endif
Matt Kraaief5529b2000-10-25 17:00:36 +000073 if (ioctl(fd, PIO_FONT, buf))
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 bb_perror_msg_and_die("PIO_FONT ioctl error");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000075}
76
77static void
Erik Andersene49d5ec2000-02-08 19:58:47 +000078do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
79{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000080 struct unimapinit advice;
81 struct unimapdesc ud;
82 struct unipair *up;
83 int ct = 0, maxct;
84 int glyph;
Denis Vlasenko28703012006-12-19 20:32:02 +000085 uint16_t unicode;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000086
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 maxct = tailsz; /* more than enough */
Denis Vlasenkob95636c2006-12-19 23:36:04 +000088 up = xmalloc(maxct * sizeof(struct unipair));
Erik Andersene49d5ec2000-02-08 19:58:47 +000089
Eric Andersen3e0fbae1999-10-19 06:02:44 +000090 for (glyph = 0; glyph < fontsize; glyph++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 while (tailsz >= 2) {
Denis Vlasenko28703012006-12-19 20:32:02 +000092 unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 tailsz -= 2;
94 inbuf += 2;
95 if (unicode == PSF_SEPARATOR)
96 break;
97 up[ct].unicode = unicode;
98 up[ct].fontpos = glyph;
99 ct++;
100 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000101 }
102
103 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
104 this printf did not work on many kernels */
105
106 advice.advised_hashsize = 0;
107 advice.advised_hashstep = 0;
108 advice.advised_hashlevel = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000110#ifdef ENOIOCTLCMD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 if (errno == ENOIOCTLCMD) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000112 bb_error_msg("it seems this kernel is older than 1.1.92");
113 bb_error_msg_and_die("no Unicode mapping table loaded");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114 } else
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000115#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116 bb_perror_msg_and_die("PIO_UNIMAPCLR");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000117 }
118 ud.entry_ct = ct;
119 ud.entries = up;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 if (ioctl(fd, PIO_UNIMAP, &ud)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 bb_perror_msg_and_die("PIO_UNIMAP");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000122 }
123}
124
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125static void loadnewfont(int fd)
126{
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000127 int unit;
Eric Andersen5e678872006-01-30 19:48:23 +0000128 unsigned char inbuf[32768]; /* primitive */
Pavel Roskindf4532b2000-07-14 17:24:58 +0000129 unsigned int inputlth, offset;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000130
131 /*
132 * We used to look at the length of the input file
133 * with stat(); now that we accept compressed files,
134 * just read the entire file.
135 */
136 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
Matt Kraaief5529b2000-10-25 17:00:36 +0000137 if (ferror(stdin))
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000138 bb_perror_msg_and_die("error reading input font");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000139 /* use malloc/realloc in case of giant files;
140 maybe these do not occur: 16kB for the font,
141 and 16kB for the map leaves 32 unicode values
142 for each font position */
Matt Kraaief5529b2000-10-25 17:00:36 +0000143 if (!feof(stdin))
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000144 bb_perror_msg_and_die("font too large");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000145
146 /* test for psf first */
147 {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 struct psf_header psfhdr;
149 int fontsize;
150 int hastable;
Pavel Roskindf4532b2000-07-14 17:24:58 +0000151 unsigned int head0, head;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000152
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 if (inputlth < sizeof(struct psf_header))
154 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000155
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 psfhdr = *(struct psf_header *) &inbuf[0];
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000157
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 if (!PSF_MAGIC_OK(psfhdr))
159 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000160
Matt Kraaief5529b2000-10-25 17:00:36 +0000161 if (psfhdr.mode > PSF_MAXMODE)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000162 bb_error_msg_and_die("unsupported psf file mode");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000164#if !defined( PIO_FONTX ) || defined( __sparc__ )
Matt Kraaief5529b2000-10-25 17:00:36 +0000165 if (fontsize != 256)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000166 bb_error_msg_and_die("only fontsize 256 supported");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000167#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 hastable = (psfhdr.mode & PSF_MODEHASTAB);
169 unit = psfhdr.charsize;
170 head0 = sizeof(struct psf_header);
171
172 head = head0 + fontsize * unit;
Matt Kraaief5529b2000-10-25 17:00:36 +0000173 if (head > inputlth || (!hastable && head != inputlth))
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000174 bb_error_msg_and_die("input file: bad length");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 do_loadfont(fd, inbuf + head0, unit, fontsize);
176 if (hastable)
177 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
178 return;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000179 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000180 no_psf:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000181
182 /* file with three code pages? */
183 if (inputlth == 9780) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 offset = 40;
185 unit = 16;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000186 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 /* bare font */
Matt Kraaief5529b2000-10-25 17:00:36 +0000188 if (inputlth & 0377)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000189 bb_error_msg_and_die("bad input file size");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 offset = 0;
191 unit = inputlth / 256;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000192 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193 do_loadfont(fd, inbuf + offset, unit, 256);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000194}