blob: 1a724ca85e17cc07356a17844af904bc365641fb [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>
13#include <memory.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <sys/types.h>
Eric Andersen3e0fbae1999-10-19 06:02:44 +000017#include <dirent.h>
18#include <errno.h>
19#include <sys/ioctl.h>
20#include <sys/kd.h>
21#include <endian.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000022#include "busybox.h"
Eric Andersen3e0fbae1999-10-19 06:02:44 +000023
Mark Whitley59ab0252001-01-23 22:30:04 +000024static const int PSF_MAGIC1 = 0x36;
25static const int PSF_MAGIC2 = 0x04;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000026
Mark Whitley59ab0252001-01-23 22:30:04 +000027static const int PSF_MODE512 = 0x01;
28static const int PSF_MODEHASTAB = 0x02;
29static const int PSF_MAXMODE = 0x03;
30static const int PSF_SEPARATOR = 0xFFFF;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000031
Erik Andersene49d5ec2000-02-08 19:58:47 +000032struct psf_header {
33 unsigned char magic1, magic2; /* Magic number */
34 unsigned char mode; /* PSF font mode */
35 unsigned char charsize; /* Character size */
Eric Andersen3e0fbae1999-10-19 06:02:44 +000036};
37
38#define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
39
40static void loadnewfont(int fd);
41
Erik Andersene49d5ec2000-02-08 19:58:47 +000042extern int loadfont_main(int argc, char **argv)
Eric Andersen3e0fbae1999-10-19 06:02:44 +000043{
44 int fd;
45
Matt Kraaief5529b2000-10-25 17:00:36 +000046 if (argc != 1)
Eric Andersen67991cf2001-02-14 21:23:06 +000047 show_usage();
Erik Andersena6c75222000-04-18 00:00:52 +000048
Eric Andersen3e0fbae1999-10-19 06:02:44 +000049 fd = open("/dev/tty0", O_RDWR);
Matt Kraaief5529b2000-10-25 17:00:36 +000050 if (fd < 0)
Mark Whitleyf57c9442000-12-07 19:56:48 +000051 perror_msg_and_die("Error opening /dev/tty0");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000052 loadnewfont(fd);
53
Matt Kraaief5529b2000-10-25 17:00:36 +000054 return EXIT_SUCCESS;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000055}
56
Erik Andersene49d5ec2000-02-08 19:58:47 +000057static void do_loadfont(int fd, char *inbuf, int unit, int fontsize)
58{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000059 char buf[16384];
60 int i;
61
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 memset(buf, 0, sizeof(buf));
Eric Andersen3e0fbae1999-10-19 06:02:44 +000063
Matt Kraaief5529b2000-10-25 17:00:36 +000064 if (unit < 1 || unit > 32)
Matt Kraaidd19c692001-01-31 19:00:21 +000065 error_msg_and_die("Bad character size %d", unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000066
67 for (i = 0; i < fontsize; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000069
Eric Andersene77ae3a1999-10-19 20:03:34 +000070#if defined( PIO_FONTX ) && !defined( __sparc__ )
Eric Andersen3e0fbae1999-10-19 06:02:44 +000071 {
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 struct consolefontdesc cfd;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000073
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 cfd.charcount = fontsize;
75 cfd.charheight = unit;
76 cfd.chardata = buf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000077
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
79 return; /* success */
Mark Whitleyf57c9442000-12-07 19:56:48 +000080 perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000081 }
82#endif
Matt Kraaief5529b2000-10-25 17:00:36 +000083 if (ioctl(fd, PIO_FONT, buf))
Mark Whitleyf57c9442000-12-07 19:56:48 +000084 perror_msg_and_die("PIO_FONT ioctl error");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000085}
86
87static void
Erik Andersene49d5ec2000-02-08 19:58:47 +000088do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
89{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000090 struct unimapinit advice;
91 struct unimapdesc ud;
92 struct unipair *up;
93 int ct = 0, maxct;
94 int glyph;
95 u_short unicode;
96
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 maxct = tailsz; /* more than enough */
Matt Kraai322ae932000-09-13 02:46:14 +000098 up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair));
Erik Andersene49d5ec2000-02-08 19:58:47 +000099
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000100 for (glyph = 0; glyph < fontsize; glyph++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 while (tailsz >= 2) {
102 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
103 tailsz -= 2;
104 inbuf += 2;
105 if (unicode == PSF_SEPARATOR)
106 break;
107 up[ct].unicode = unicode;
108 up[ct].fontpos = glyph;
109 ct++;
110 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000111 }
112
113 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
114 this printf did not work on many kernels */
115
116 advice.advised_hashsize = 0;
117 advice.advised_hashstep = 0;
118 advice.advised_hashlevel = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000120#ifdef ENOIOCTLCMD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 if (errno == ENOIOCTLCMD) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000122 error_msg("It seems this kernel is older than 1.1.92");
123 error_msg_and_die("No Unicode mapping table loaded.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 } else
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000125#endif
Mark Whitleyf57c9442000-12-07 19:56:48 +0000126 perror_msg_and_die("PIO_UNIMAPCLR");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000127 }
128 ud.entry_ct = ct;
129 ud.entries = up;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 if (ioctl(fd, PIO_UNIMAP, &ud)) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000131#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 if (errno == ENOMEM) {
133 /* change advice parameters */
134 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000135#endif
Mark Whitleyf57c9442000-12-07 19:56:48 +0000136 perror_msg_and_die("PIO_UNIMAP");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000137 }
138}
139
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140static void loadnewfont(int fd)
141{
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000142 int unit;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 char inbuf[32768]; /* primitive */
Pavel Roskindf4532b2000-07-14 17:24:58 +0000144 unsigned int inputlth, offset;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000145
146 /*
147 * We used to look at the length of the input file
148 * with stat(); now that we accept compressed files,
149 * just read the entire file.
150 */
151 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
Matt Kraaief5529b2000-10-25 17:00:36 +0000152 if (ferror(stdin))
Mark Whitleyf57c9442000-12-07 19:56:48 +0000153 perror_msg_and_die("Error reading input font");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000154 /* use malloc/realloc in case of giant files;
155 maybe these do not occur: 16kB for the font,
156 and 16kB for the map leaves 32 unicode values
157 for each font position */
Matt Kraaief5529b2000-10-25 17:00:36 +0000158 if (!feof(stdin))
Mark Whitleyf57c9442000-12-07 19:56:48 +0000159 perror_msg_and_die("Font too large");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000160
161 /* test for psf first */
162 {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 struct psf_header psfhdr;
164 int fontsize;
165 int hastable;
Pavel Roskindf4532b2000-07-14 17:24:58 +0000166 unsigned int head0, head;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000167
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 if (inputlth < sizeof(struct psf_header))
169 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000170
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 psfhdr = *(struct psf_header *) &inbuf[0];
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000172
Erik Andersene49d5ec2000-02-08 19:58:47 +0000173 if (!PSF_MAGIC_OK(psfhdr))
174 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000175
Matt Kraaief5529b2000-10-25 17:00:36 +0000176 if (psfhdr.mode > PSF_MAXMODE)
Matt Kraaidd19c692001-01-31 19:00:21 +0000177 error_msg_and_die("Unsupported psf file mode");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000178 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000179#if !defined( PIO_FONTX ) || defined( __sparc__ )
Matt Kraaief5529b2000-10-25 17:00:36 +0000180 if (fontsize != 256)
Matt Kraaidd19c692001-01-31 19:00:21 +0000181 error_msg_and_die("Only fontsize 256 supported");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000182#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 hastable = (psfhdr.mode & PSF_MODEHASTAB);
184 unit = psfhdr.charsize;
185 head0 = sizeof(struct psf_header);
186
187 head = head0 + fontsize * unit;
Matt Kraaief5529b2000-10-25 17:00:36 +0000188 if (head > inputlth || (!hastable && head != inputlth))
Matt Kraaidd19c692001-01-31 19:00:21 +0000189 error_msg_and_die("Input file: bad length");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 do_loadfont(fd, inbuf + head0, unit, fontsize);
191 if (hastable)
192 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
193 return;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000194 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000195 no_psf:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000196
197 /* file with three code pages? */
198 if (inputlth == 9780) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 offset = 40;
200 unit = 16;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000201 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 /* bare font */
Matt Kraaief5529b2000-10-25 17:00:36 +0000203 if (inputlth & 0377)
Matt Kraaidd19c692001-01-31 19:00:21 +0000204 error_msg_and_die("Bad input file size");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 offset = 0;
206 unit = inputlth / 256;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000207 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208 do_loadfont(fd, inbuf + offset, unit, 256);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000209}