1 /* vi: set sw=4 ts=4: */
3 * loadfont.c - Eugene Crosser & Andries Brouwer
7 * Loads the console font, and possibly the corresponding screen map(s).
8 * (Adapted for busybox by Matej Vela.)
16 #include <sys/types.h>
19 #include <sys/ioctl.h>
24 static const int PSF_MAGIC1 = 0x36;
25 static const int PSF_MAGIC2 = 0x04;
27 static const int PSF_MODE512 = 0x01;
28 static const int PSF_MODEHASTAB = 0x02;
29 static const int PSF_MAXMODE = 0x03;
30 static const int PSF_SEPARATOR = 0xFFFF;
33 unsigned char magic1, magic2; /* Magic number */
34 unsigned char mode; /* PSF font mode */
35 unsigned char charsize; /* Character size */
38 #define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
40 static void loadnewfont(int fd);
42 extern int loadfont_main(int argc, char **argv)
49 fd = bb_xopen(CURRENT_VC, O_RDWR);
55 static void do_loadfont(int fd, char *inbuf, int unit, int fontsize)
60 memset(buf, 0, sizeof(buf));
62 if (unit < 1 || unit > 32)
63 bb_error_msg_and_die("Bad character size %d", unit);
65 for (i = 0; i < fontsize; i++)
66 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
68 #if defined( PIO_FONTX ) && !defined( __sparc__ )
70 struct consolefontdesc cfd;
72 cfd.charcount = fontsize;
73 cfd.charheight = unit;
76 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
78 bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
81 if (ioctl(fd, PIO_FONT, buf))
82 bb_perror_msg_and_die("PIO_FONT ioctl error");
86 do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
88 struct unimapinit advice;
95 maxct = tailsz; /* more than enough */
96 up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair));
98 for (glyph = 0; glyph < fontsize; glyph++) {
100 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
103 if (unicode == PSF_SEPARATOR)
105 up[ct].unicode = unicode;
106 up[ct].fontpos = glyph;
111 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
112 this printf did not work on many kernels */
114 advice.advised_hashsize = 0;
115 advice.advised_hashstep = 0;
116 advice.advised_hashlevel = 0;
117 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
119 if (errno == ENOIOCTLCMD) {
120 bb_error_msg("It seems this kernel is older than 1.1.92");
121 bb_error_msg_and_die("No Unicode mapping table loaded.");
124 bb_perror_msg_and_die("PIO_UNIMAPCLR");
128 if (ioctl(fd, PIO_UNIMAP, &ud)) {
130 if (errno == ENOMEM) {
131 /* change advice parameters */
134 bb_perror_msg_and_die("PIO_UNIMAP");
138 static void loadnewfont(int fd)
141 char inbuf[32768]; /* primitive */
142 unsigned int inputlth, offset;
145 * We used to look at the length of the input file
146 * with stat(); now that we accept compressed files,
147 * just read the entire file.
149 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
151 bb_perror_msg_and_die("Error reading input font");
152 /* use malloc/realloc in case of giant files;
153 maybe these do not occur: 16kB for the font,
154 and 16kB for the map leaves 32 unicode values
155 for each font position */
157 bb_perror_msg_and_die("Font too large");
159 /* test for psf first */
161 struct psf_header psfhdr;
164 unsigned int head0, head;
166 if (inputlth < sizeof(struct psf_header))
169 psfhdr = *(struct psf_header *) &inbuf[0];
171 if (!PSF_MAGIC_OK(psfhdr))
174 if (psfhdr.mode > PSF_MAXMODE)
175 bb_error_msg_and_die("Unsupported psf file mode");
176 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
177 #if !defined( PIO_FONTX ) || defined( __sparc__ )
179 bb_error_msg_and_die("Only fontsize 256 supported");
181 hastable = (psfhdr.mode & PSF_MODEHASTAB);
182 unit = psfhdr.charsize;
183 head0 = sizeof(struct psf_header);
185 head = head0 + fontsize * unit;
186 if (head > inputlth || (!hastable && head != inputlth))
187 bb_error_msg_and_die("Input file: bad length");
188 do_loadfont(fd, inbuf + head0, unit, fontsize);
190 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
195 /* file with three code pages? */
196 if (inputlth == 9780) {
202 bb_error_msg_and_die("Bad input file size");
204 unit = inputlth / 256;
206 do_loadfont(fd, inbuf + offset, unit, 256);