blob: ded1b6a95c55ab8bb1ac3ccab706519d455162c2 [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 Andersen3e0fbae1999-10-19 06:02:44 +000010#include <stdio.h>
11#include <string.h>
12#include <fcntl.h>
Eric Andersen3e0fbae1999-10-19 06:02:44 +000013#include <stdlib.h>
14#include <unistd.h>
15#include <sys/types.h>
Eric Andersen3e0fbae1999-10-19 06:02:44 +000016#include <dirent.h>
17#include <errno.h>
18#include <sys/ioctl.h>
19#include <sys/kd.h>
20#include <endian.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000021#include "busybox.h"
Eric Andersen3e0fbae1999-10-19 06:02:44 +000022
Rob Landleybc68cd12006-03-10 19:22:06 +000023enum{
24 PSF_MAGIC1 = 0x36,
25 PSF_MAGIC2 = 0x04,
Eric Andersen3e0fbae1999-10-19 06:02:44 +000026
Rob Landleybc68cd12006-03-10 19:22:06 +000027 PSF_MODE512 = 0x01,
28 PSF_MODEHASTAB = 0x02,
29 PSF_MAXMODE = 0x03,
30 PSF_SEPARATOR = 0xFFFF
31};
Eric Andersen3e0fbae1999-10-19 06:02:44 +000032
Erik Andersene49d5ec2000-02-08 19:58:47 +000033struct psf_header {
34 unsigned char magic1, magic2; /* Magic number */
35 unsigned char mode; /* PSF font mode */
36 unsigned char charsize; /* Character size */
Eric Andersen3e0fbae1999-10-19 06:02:44 +000037};
38
39#define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
40
41static void loadnewfont(int fd);
42
Rob Landleydfba7412006-03-06 20:47:33 +000043int loadfont_main(int argc, char **argv)
Eric Andersen3e0fbae1999-10-19 06:02:44 +000044{
45 int fd;
46
Matt Kraaief5529b2000-10-25 17:00:36 +000047 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 bb_show_usage();
Erik Andersena6c75222000-04-18 00:00:52 +000049
Glenn L McGrath02e03552003-12-20 06:00:08 +000050 fd = bb_xopen(CURRENT_VC, O_RDWR);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000051 loadnewfont(fd);
52
Matt Kraaief5529b2000-10-25 17:00:36 +000053 return EXIT_SUCCESS;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000054}
55
Eric Andersen5e678872006-01-30 19:48:23 +000056static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +000057{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000058 char buf[16384];
59 int i;
60
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 memset(buf, 0, sizeof(buf));
Eric Andersen3e0fbae1999-10-19 06:02:44 +000062
Matt Kraaief5529b2000-10-25 17:00:36 +000063 if (unit < 1 || unit > 32)
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 bb_error_msg_and_die("Bad character size %d", unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000065
66 for (i = 0; i < fontsize; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000068
Eric Andersene77ae3a1999-10-19 20:03:34 +000069#if defined( PIO_FONTX ) && !defined( __sparc__ )
Eric Andersen3e0fbae1999-10-19 06:02:44 +000070 {
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 struct consolefontdesc cfd;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000072
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 cfd.charcount = fontsize;
74 cfd.charheight = unit;
75 cfd.chardata = buf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000076
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
78 return; /* success */
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000080 }
81#endif
Matt Kraaief5529b2000-10-25 17:00:36 +000082 if (ioctl(fd, PIO_FONT, buf))
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 bb_perror_msg_and_die("PIO_FONT ioctl error");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000084}
85
86static void
Erik Andersene49d5ec2000-02-08 19:58:47 +000087do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
88{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000089 struct unimapinit advice;
90 struct unimapdesc ud;
91 struct unipair *up;
92 int ct = 0, maxct;
93 int glyph;
94 u_short unicode;
95
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 maxct = tailsz; /* more than enough */
Matt Kraai322ae932000-09-13 02:46:14 +000097 up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair));
Erik Andersene49d5ec2000-02-08 19:58:47 +000098
Eric Andersen3e0fbae1999-10-19 06:02:44 +000099 for (glyph = 0; glyph < fontsize; glyph++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 while (tailsz >= 2) {
101 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
102 tailsz -= 2;
103 inbuf += 2;
104 if (unicode == PSF_SEPARATOR)
105 break;
106 up[ct].unicode = unicode;
107 up[ct].fontpos = glyph;
108 ct++;
109 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000110 }
111
112 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
113 this printf did not work on many kernels */
114
115 advice.advised_hashsize = 0;
116 advice.advised_hashstep = 0;
117 advice.advised_hashlevel = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000119#ifdef ENOIOCTLCMD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 if (errno == ENOIOCTLCMD) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 bb_error_msg("It seems this kernel is older than 1.1.92");
122 bb_error_msg_and_die("No Unicode mapping table loaded.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 } else
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000124#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 bb_perror_msg_and_die("PIO_UNIMAPCLR");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000126 }
127 ud.entry_ct = ct;
128 ud.entries = up;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 if (ioctl(fd, PIO_UNIMAP, &ud)) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000130#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 if (errno == ENOMEM) {
132 /* change advice parameters */
133 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000134#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 bb_perror_msg_and_die("PIO_UNIMAP");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000136 }
137}
138
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139static void loadnewfont(int fd)
140{
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000141 int unit;
Eric Andersen5e678872006-01-30 19:48:23 +0000142 unsigned char inbuf[32768]; /* primitive */
Pavel Roskindf4532b2000-07-14 17:24:58 +0000143 unsigned int inputlth, offset;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000144
145 /*
146 * We used to look at the length of the input file
147 * with stat(); now that we accept compressed files,
148 * just read the entire file.
149 */
150 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
Matt Kraaief5529b2000-10-25 17:00:36 +0000151 if (ferror(stdin))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000152 bb_perror_msg_and_die("Error reading input font");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000153 /* use malloc/realloc in case of giant files;
154 maybe these do not occur: 16kB for the font,
155 and 16kB for the map leaves 32 unicode values
156 for each font position */
Matt Kraaief5529b2000-10-25 17:00:36 +0000157 if (!feof(stdin))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 bb_perror_msg_and_die("Font too large");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000159
160 /* test for psf first */
161 {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162 struct psf_header psfhdr;
163 int fontsize;
164 int hastable;
Pavel Roskindf4532b2000-07-14 17:24:58 +0000165 unsigned int head0, head;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000166
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 if (inputlth < sizeof(struct psf_header))
168 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000169
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170 psfhdr = *(struct psf_header *) &inbuf[0];
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000171
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172 if (!PSF_MAGIC_OK(psfhdr))
173 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000174
Matt Kraaief5529b2000-10-25 17:00:36 +0000175 if (psfhdr.mode > PSF_MAXMODE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000176 bb_error_msg_and_die("Unsupported psf file mode");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000178#if !defined( PIO_FONTX ) || defined( __sparc__ )
Matt Kraaief5529b2000-10-25 17:00:36 +0000179 if (fontsize != 256)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 bb_error_msg_and_die("Only fontsize 256 supported");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000181#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 hastable = (psfhdr.mode & PSF_MODEHASTAB);
183 unit = psfhdr.charsize;
184 head0 = sizeof(struct psf_header);
185
186 head = head0 + fontsize * unit;
Matt Kraaief5529b2000-10-25 17:00:36 +0000187 if (head > inputlth || (!hastable && head != inputlth))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 bb_error_msg_and_die("Input file: bad length");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189 do_loadfont(fd, inbuf + head0, unit, fontsize);
190 if (hastable)
191 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
192 return;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000193 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 no_psf:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000195
196 /* file with three code pages? */
197 if (inputlth == 9780) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198 offset = 40;
199 unit = 16;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000200 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000201 /* bare font */
Matt Kraaief5529b2000-10-25 17:00:36 +0000202 if (inputlth & 0377)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000203 bb_error_msg_and_die("Bad input file size");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 offset = 0;
205 unit = inputlth / 256;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000206 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 do_loadfont(fd, inbuf + offset, unit, 256);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000208}