blob: 3d8588565c1ac0ffc87941015250f4d5efcd228d [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
Rob Landleybc68cd12006-03-10 19:22:06 +000013enum{
14 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
Rob Landleydfba7412006-03-06 20:47:33 +000033int loadfont_main(int argc, char **argv)
Eric Andersen3e0fbae1999-10-19 06:02:44 +000034{
35 int fd;
36
Matt Kraaief5529b2000-10-25 17:00:36 +000037 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 bb_show_usage();
Erik Andersena6c75222000-04-18 00:00:52 +000039
Rob Landleyd921b2e2006-08-03 15:41:12 +000040 fd = xopen(CURRENT_VC, O_RDWR);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000041 loadnewfont(fd);
42
Matt Kraaief5529b2000-10-25 17:00:36 +000043 return EXIT_SUCCESS;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000044}
45
Eric Andersen5e678872006-01-30 19:48:23 +000046static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +000047{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000048 char buf[16384];
49 int i;
50
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 memset(buf, 0, sizeof(buf));
Eric Andersen3e0fbae1999-10-19 06:02:44 +000052
Matt Kraaief5529b2000-10-25 17:00:36 +000053 if (unit < 1 || unit > 32)
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 bb_error_msg_and_die("Bad character size %d", unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000055
56 for (i = 0; i < fontsize; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000058
Eric Andersene77ae3a1999-10-19 20:03:34 +000059#if defined( PIO_FONTX ) && !defined( __sparc__ )
Eric Andersen3e0fbae1999-10-19 06:02:44 +000060 {
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 struct consolefontdesc cfd;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000062
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 cfd.charcount = fontsize;
64 cfd.charheight = unit;
65 cfd.chardata = buf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000066
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
68 return; /* success */
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000070 }
71#endif
Matt Kraaief5529b2000-10-25 17:00:36 +000072 if (ioctl(fd, PIO_FONT, buf))
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 bb_perror_msg_and_die("PIO_FONT ioctl error");
Eric Andersen3e0fbae1999-10-19 06:02:44 +000074}
75
76static void
Erik Andersene49d5ec2000-02-08 19:58:47 +000077do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
78{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000079 struct unimapinit advice;
80 struct unimapdesc ud;
81 struct unipair *up;
82 int ct = 0, maxct;
83 int glyph;
84 u_short unicode;
85
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 maxct = tailsz; /* more than enough */
Matt Kraai322ae932000-09-13 02:46:14 +000087 up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair));
Erik Andersene49d5ec2000-02-08 19:58:47 +000088
Eric Andersen3e0fbae1999-10-19 06:02:44 +000089 for (glyph = 0; glyph < fontsize; glyph++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 while (tailsz >= 2) {
91 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
92 tailsz -= 2;
93 inbuf += 2;
94 if (unicode == PSF_SEPARATOR)
95 break;
96 up[ct].unicode = unicode;
97 up[ct].fontpos = glyph;
98 ct++;
99 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000100 }
101
102 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
103 this printf did not work on many kernels */
104
105 advice.advised_hashsize = 0;
106 advice.advised_hashstep = 0;
107 advice.advised_hashlevel = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000109#ifdef ENOIOCTLCMD
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 if (errno == ENOIOCTLCMD) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111 bb_error_msg("It seems this kernel is older than 1.1.92");
112 bb_error_msg_and_die("No Unicode mapping table loaded.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113 } else
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000114#endif
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 bb_perror_msg_and_die("PIO_UNIMAPCLR");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000116 }
117 ud.entry_ct = ct;
118 ud.entries = up;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 if (ioctl(fd, PIO_UNIMAP, &ud)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000120 bb_perror_msg_and_die("PIO_UNIMAP");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000121 }
122}
123
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124static void loadnewfont(int fd)
125{
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000126 int unit;
Eric Andersen5e678872006-01-30 19:48:23 +0000127 unsigned char inbuf[32768]; /* primitive */
Pavel Roskindf4532b2000-07-14 17:24:58 +0000128 unsigned int inputlth, offset;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000129
130 /*
131 * We used to look at the length of the input file
132 * with stat(); now that we accept compressed files,
133 * just read the entire file.
134 */
135 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
Matt Kraaief5529b2000-10-25 17:00:36 +0000136 if (ferror(stdin))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000137 bb_perror_msg_and_die("Error reading input font");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000138 /* use malloc/realloc in case of giant files;
139 maybe these do not occur: 16kB for the font,
140 and 16kB for the map leaves 32 unicode values
141 for each font position */
Matt Kraaief5529b2000-10-25 17:00:36 +0000142 if (!feof(stdin))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 bb_perror_msg_and_die("Font too large");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000144
145 /* test for psf first */
146 {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 struct psf_header psfhdr;
148 int fontsize;
149 int hastable;
Pavel Roskindf4532b2000-07-14 17:24:58 +0000150 unsigned int head0, head;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000151
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 if (inputlth < sizeof(struct psf_header))
153 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000154
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155 psfhdr = *(struct psf_header *) &inbuf[0];
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000156
Erik Andersene49d5ec2000-02-08 19:58:47 +0000157 if (!PSF_MAGIC_OK(psfhdr))
158 goto no_psf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000159
Matt Kraaief5529b2000-10-25 17:00:36 +0000160 if (psfhdr.mode > PSF_MAXMODE)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000161 bb_error_msg_and_die("Unsupported psf file mode");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000163#if !defined( PIO_FONTX ) || defined( __sparc__ )
Matt Kraaief5529b2000-10-25 17:00:36 +0000164 if (fontsize != 256)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000165 bb_error_msg_and_die("Only fontsize 256 supported");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000166#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 hastable = (psfhdr.mode & PSF_MODEHASTAB);
168 unit = psfhdr.charsize;
169 head0 = sizeof(struct psf_header);
170
171 head = head0 + fontsize * unit;
Matt Kraaief5529b2000-10-25 17:00:36 +0000172 if (head > inputlth || (!hastable && head != inputlth))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000173 bb_error_msg_and_die("Input file: bad length");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000174 do_loadfont(fd, inbuf + head0, unit, fontsize);
175 if (hastable)
176 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
177 return;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000178 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000179 no_psf:
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000180
181 /* file with three code pages? */
182 if (inputlth == 9780) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 offset = 40;
184 unit = 16;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000185 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000186 /* bare font */
Matt Kraaief5529b2000-10-25 17:00:36 +0000187 if (inputlth & 0377)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 bb_error_msg_and_die("Bad input file size");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189 offset = 0;
190 unit = inputlth / 256;
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000191 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000192 do_loadfont(fd, inbuf + offset, unit, 256);
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000193}