blob: 622142925a53ace8a342fdfaf2335841737a2b37 [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 */
10#include "internal.h"
11#include <stdio.h>
12#include <string.h>
13#include <fcntl.h>
14#include <memory.h>
15#include <stdlib.h>
16#include <unistd.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <dirent.h>
20#include <errno.h>
21#include <sys/ioctl.h>
22#include <sys/kd.h>
23#include <endian.h>
24
25#define PSF_MAGIC1 0x36
26#define PSF_MAGIC2 0x04
27
28#define PSF_MODE512 0x01
29#define PSF_MODEHASTAB 0x02
30#define PSF_MAXMODE 0x03
31#define PSF_SEPARATOR 0xFFFF
32
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
Erik Andersene49d5ec2000-02-08 19:58:47 +000043extern int loadfont_main(int argc, char **argv)
Eric Andersen3e0fbae1999-10-19 06:02:44 +000044{
45 int fd;
46
Erik Andersena6c75222000-04-18 00:00:52 +000047 if (argc>=2 && *argv[1]=='-') {
48 usage(loadfont_usage);
49 }
50
Eric Andersen3e0fbae1999-10-19 06:02:44 +000051 fd = open("/dev/tty0", O_RDWR);
52 if (fd < 0) {
Matt Kraaid537a952000-07-14 01:51:25 +000053 errorMsg("Error opening /dev/tty0: %s\n", strerror(errno));
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000054 return( FALSE);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000055 }
56 loadnewfont(fd);
57
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000058 return( TRUE);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000059}
60
Erik Andersene49d5ec2000-02-08 19:58:47 +000061static void do_loadfont(int fd, char *inbuf, int unit, int fontsize)
62{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000063 char buf[16384];
64 int i;
65
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 memset(buf, 0, sizeof(buf));
Eric Andersen3e0fbae1999-10-19 06:02:44 +000067
68 if (unit < 1 || unit > 32) {
Matt Kraaid537a952000-07-14 01:51:25 +000069 errorMsg("Bad character size %d\n", unit);
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 exit(1);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000071 }
72
73 for (i = 0; i < fontsize; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000075
Eric Andersene77ae3a1999-10-19 20:03:34 +000076#if defined( PIO_FONTX ) && !defined( __sparc__ )
Eric Andersen3e0fbae1999-10-19 06:02:44 +000077 {
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 struct consolefontdesc cfd;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000079
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 cfd.charcount = fontsize;
81 cfd.charheight = unit;
82 cfd.chardata = buf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000083
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
85 return; /* success */
86 perror("PIO_FONTX ioctl error (trying PIO_FONT)");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000087 }
88#endif
89 if (ioctl(fd, PIO_FONT, buf)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 perror("PIO_FONT ioctl error");
91 exit(1);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000092 }
93}
94
95static void
Erik Andersene49d5ec2000-02-08 19:58:47 +000096do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
97{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000098 struct unimapinit advice;
99 struct unimapdesc ud;
100 struct unipair *up;
101 int ct = 0, maxct;
102 int glyph;
103 u_short unicode;
104
Erik Andersene49d5ec2000-02-08 19:58:47 +0000105 maxct = tailsz; /* more than enough */
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000106 up = (struct unipair *) malloc(maxct * sizeof(struct unipair));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000108 if (!up) {
Matt Kraaid537a952000-07-14 01:51:25 +0000109 errorMsg("Out of memory?\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 exit(1);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000111 }
112 for (glyph = 0; glyph < fontsize; glyph++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 while (tailsz >= 2) {
114 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
115 tailsz -= 2;
116 inbuf += 2;
117 if (unicode == PSF_SEPARATOR)
118 break;
119 up[ct].unicode = unicode;
120 up[ct].fontpos = glyph;
121 ct++;
122 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000123 }
124
125 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
126 this printf did not work on many kernels */
127
128 advice.advised_hashsize = 0;
129 advice.advised_hashstep = 0;
130 advice.advised_hashlevel = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000132#ifdef ENOIOCTLCMD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133 if (errno == ENOIOCTLCMD) {
Matt Kraaid537a952000-07-14 01:51:25 +0000134 errorMsg("It seems this kernel is older than 1.1.92\n");
135 errorMsg("No Unicode mapping table loaded.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 } else
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000137#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000138 perror("PIO_UNIMAPCLR");
139 exit(1);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000140 }
141 ud.entry_ct = ct;
142 ud.entries = up;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 if (ioctl(fd, PIO_UNIMAP, &ud)) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000144#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 if (errno == ENOMEM) {
146 /* change advice parameters */
147 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000148#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149 perror("PIO_UNIMAP");
150 exit(1);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000151 }
152}
153
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154static void loadnewfont(int fd)
155{
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000156 int unit;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 char inbuf[32768]; /* primitive */
Pavel Roskindf4532b2000-07-14 17:24:58 +0000158 unsigned int inputlth, offset;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000159
160 /*
161 * We used to look at the length of the input file
162 * with stat(); now that we accept compressed files,
163 * just read the entire file.
164 */
165 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
166 if (ferror(stdin)) {
167 perror("Error reading input font");
168 exit(1);
169 }
170 /* use malloc/realloc in case of giant files;
171 maybe these do not occur: 16kB for the font,
172 and 16kB for the map leaves 32 unicode values
173 for each font position */
174 if (!feof(stdin)) {
175 perror("Font too large");
176 exit(1);
177 }
178
179 /* test for psf first */
180 {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 struct psf_header psfhdr;
182 int fontsize;
183 int hastable;
Pavel Roskindf4532b2000-07-14 17:24:58 +0000184 unsigned int head0, head;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000185
Erik Andersene49d5ec2000-02-08 19:58:47 +0000186 if (inputlth < sizeof(struct psf_header))
187 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000188
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189 psfhdr = *(struct psf_header *) &inbuf[0];
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000190
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191 if (!PSF_MAGIC_OK(psfhdr))
192 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000193
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 if (psfhdr.mode > PSF_MAXMODE) {
Matt Kraaid537a952000-07-14 01:51:25 +0000195 errorMsg("Unsupported psf file mode\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 exit(1);
197 }
198 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000199#if !defined( PIO_FONTX ) || defined( __sparc__ )
Erik Andersene49d5ec2000-02-08 19:58:47 +0000200 if (fontsize != 256) {
Matt Kraaid537a952000-07-14 01:51:25 +0000201 errorMsg("Only fontsize 256 supported\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 exit(1);
203 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000204#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 hastable = (psfhdr.mode & PSF_MODEHASTAB);
206 unit = psfhdr.charsize;
207 head0 = sizeof(struct psf_header);
208
209 head = head0 + fontsize * unit;
210 if (head > inputlth || (!hastable && head != inputlth)) {
Matt Kraaid537a952000-07-14 01:51:25 +0000211 errorMsg("Input file: bad length\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 exit(1);
213 }
214 do_loadfont(fd, inbuf + head0, unit, fontsize);
215 if (hastable)
216 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
217 return;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000218 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000219 no_psf:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000220
221 /* file with three code pages? */
222 if (inputlth == 9780) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223 offset = 40;
224 unit = 16;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000225 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226 /* bare font */
227 if (inputlth & 0377) {
Matt Kraaid537a952000-07-14 01:51:25 +0000228 errorMsg("Bad input file size\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 exit(1);
230 }
231 offset = 0;
232 unit = inputlth / 256;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000233 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000234 do_loadfont(fd, inbuf + offset, unit, 256);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000235}