blob: 64b72561053f813cfcd4b07ff51616f5dd0c4d12 [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
33static const char loadfont_usage[] = "loadfont\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000034 "\n" "\tLoad a console font from standard input.\n" "\n";
Eric Andersen3e0fbae1999-10-19 06:02:44 +000035
Erik Andersene49d5ec2000-02-08 19:58:47 +000036struct psf_header {
37 unsigned char magic1, magic2; /* Magic number */
38 unsigned char mode; /* PSF font mode */
39 unsigned char charsize; /* Character size */
Eric Andersen3e0fbae1999-10-19 06:02:44 +000040};
41
42#define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
43
44static void loadnewfont(int fd);
45
Erik Andersene49d5ec2000-02-08 19:58:47 +000046extern int loadfont_main(int argc, char **argv)
Eric Andersen3e0fbae1999-10-19 06:02:44 +000047{
48 int fd;
49
50 fd = open("/dev/tty0", O_RDWR);
51 if (fd < 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 fprintf(stderr, "Error opening /dev/tty0: %s\n", strerror(errno));
53 return 1;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000054 }
55 loadnewfont(fd);
56
57 return 0;
58}
59
Erik Andersene49d5ec2000-02-08 19:58:47 +000060static void do_loadfont(int fd, char *inbuf, int unit, int fontsize)
61{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000062 char buf[16384];
63 int i;
64
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 memset(buf, 0, sizeof(buf));
Eric Andersen3e0fbae1999-10-19 06:02:44 +000066
67 if (unit < 1 || unit > 32) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 fprintf(stderr, "Bad character size %d\n", unit);
69 exit(1);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000070 }
71
72 for (i = 0; i < fontsize; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000074
Eric Andersene77ae3a1999-10-19 20:03:34 +000075#if defined( PIO_FONTX ) && !defined( __sparc__ )
Eric Andersen3e0fbae1999-10-19 06:02:44 +000076 {
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 struct consolefontdesc cfd;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000078
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 cfd.charcount = fontsize;
80 cfd.charheight = unit;
81 cfd.chardata = buf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000082
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
84 return; /* success */
85 perror("PIO_FONTX ioctl error (trying PIO_FONT)");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000086 }
87#endif
88 if (ioctl(fd, PIO_FONT, buf)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000089 perror("PIO_FONT ioctl error");
90 exit(1);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000091 }
92}
93
94static void
Erik Andersene49d5ec2000-02-08 19:58:47 +000095do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
96{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000097 struct unimapinit advice;
98 struct unimapdesc ud;
99 struct unipair *up;
100 int ct = 0, maxct;
101 int glyph;
102 u_short unicode;
103
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104 maxct = tailsz; /* more than enough */
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000105 up = (struct unipair *) malloc(maxct * sizeof(struct unipair));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000107 if (!up) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 fprintf(stderr, "Out of memory?\n");
109 exit(1);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000110 }
111 for (glyph = 0; glyph < fontsize; glyph++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 while (tailsz >= 2) {
113 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
114 tailsz -= 2;
115 inbuf += 2;
116 if (unicode == PSF_SEPARATOR)
117 break;
118 up[ct].unicode = unicode;
119 up[ct].fontpos = glyph;
120 ct++;
121 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000122 }
123
124 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
125 this printf did not work on many kernels */
126
127 advice.advised_hashsize = 0;
128 advice.advised_hashstep = 0;
129 advice.advised_hashlevel = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000131#ifdef ENOIOCTLCMD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 if (errno == ENOIOCTLCMD) {
133 fprintf(stderr, "It seems this kernel is older than 1.1.92\n");
134 fprintf(stderr, "No Unicode mapping table loaded.\n");
135 } else
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000136#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 perror("PIO_UNIMAPCLR");
138 exit(1);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000139 }
140 ud.entry_ct = ct;
141 ud.entries = up;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 if (ioctl(fd, PIO_UNIMAP, &ud)) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000143#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144 if (errno == ENOMEM) {
145 /* change advice parameters */
146 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000147#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 perror("PIO_UNIMAP");
149 exit(1);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000150 }
151}
152
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153static void loadnewfont(int fd)
154{
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000155 int unit;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 char inbuf[32768]; /* primitive */
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000157 int inputlth, offset;
158
159 /*
160 * We used to look at the length of the input file
161 * with stat(); now that we accept compressed files,
162 * just read the entire file.
163 */
164 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
165 if (ferror(stdin)) {
166 perror("Error reading input font");
167 exit(1);
168 }
169 /* use malloc/realloc in case of giant files;
170 maybe these do not occur: 16kB for the font,
171 and 16kB for the map leaves 32 unicode values
172 for each font position */
173 if (!feof(stdin)) {
174 perror("Font too large");
175 exit(1);
176 }
177
178 /* test for psf first */
179 {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000180 struct psf_header psfhdr;
181 int fontsize;
182 int hastable;
183 int head0, head;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000184
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185 if (inputlth < sizeof(struct psf_header))
186 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000187
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188 psfhdr = *(struct psf_header *) &inbuf[0];
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000189
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 if (!PSF_MAGIC_OK(psfhdr))
191 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000192
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193 if (psfhdr.mode > PSF_MAXMODE) {
194 fprintf(stderr, "Unsupported psf file mode\n");
195 exit(1);
196 }
197 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000198#if !defined( PIO_FONTX ) || defined( __sparc__ )
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 if (fontsize != 256) {
200 fprintf(stderr, "Only fontsize 256 supported\n");
201 exit(1);
202 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000203#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 hastable = (psfhdr.mode & PSF_MODEHASTAB);
205 unit = psfhdr.charsize;
206 head0 = sizeof(struct psf_header);
207
208 head = head0 + fontsize * unit;
209 if (head > inputlth || (!hastable && head != inputlth)) {
210 fprintf(stderr, "Input file: bad length\n");
211 exit(1);
212 }
213 do_loadfont(fd, inbuf + head0, unit, fontsize);
214 if (hastable)
215 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
216 return;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000217 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000218 no_psf:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000219
220 /* file with three code pages? */
221 if (inputlth == 9780) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222 offset = 40;
223 unit = 16;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000224 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 /* bare font */
226 if (inputlth & 0377) {
227 fprintf(stderr, "Bad input file size\n");
228 exit(1);
229 }
230 offset = 0;
231 unit = inputlth / 256;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000232 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 do_loadfont(fd, inbuf + offset, unit, 256);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000234}