source: MondoRescue/trunk/mindi-busybox/console-tools/loadfont.c@ 863

Last change on this file since 863 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 4.7 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
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 <stdio.h>
11#include <string.h>
12#include <fcntl.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <sys/types.h>
16#include <dirent.h>
17#include <errno.h>
18#include <sys/ioctl.h>
19#include <sys/kd.h>
20#include <endian.h>
21#include "busybox.h"
22
23enum{
24 PSF_MAGIC1 = 0x36,
25 PSF_MAGIC2 = 0x04,
26
27 PSF_MODE512 = 0x01,
28 PSF_MODEHASTAB = 0x02,
29 PSF_MAXMODE = 0x03,
30 PSF_SEPARATOR = 0xFFFF
31};
32
33struct psf_header {
34 unsigned char magic1, magic2; /* Magic number */
35 unsigned char mode; /* PSF font mode */
36 unsigned char charsize; /* Character size */
37};
38
39#define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
40
41static void loadnewfont(int fd);
42
43int loadfont_main(int argc, char **argv)
44{
45 int fd;
46
47 if (argc != 1)
48 bb_show_usage();
49
50 fd = bb_xopen(CURRENT_VC, O_RDWR);
51 loadnewfont(fd);
52
53 return EXIT_SUCCESS;
54}
55
56static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
57{
58 char buf[16384];
59 int i;
60
61 memset(buf, 0, sizeof(buf));
62
63 if (unit < 1 || unit > 32)
64 bb_error_msg_and_die("Bad character size %d", unit);
65
66 for (i = 0; i < fontsize; i++)
67 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
68
69#if defined( PIO_FONTX ) && !defined( __sparc__ )
70 {
71 struct consolefontdesc cfd;
72
73 cfd.charcount = fontsize;
74 cfd.charheight = unit;
75 cfd.chardata = buf;
76
77 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
78 return; /* success */
79 bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
80 }
81#endif
82 if (ioctl(fd, PIO_FONT, buf))
83 bb_perror_msg_and_die("PIO_FONT ioctl error");
84}
85
86static void
87do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
88{
89 struct unimapinit advice;
90 struct unimapdesc ud;
91 struct unipair *up;
92 int ct = 0, maxct;
93 int glyph;
94 u_short unicode;
95
96 maxct = tailsz; /* more than enough */
97 up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair));
98
99 for (glyph = 0; glyph < fontsize; glyph++) {
100 while (tailsz >= 2) {
101 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
102 tailsz -= 2;
103 inbuf += 2;
104 if (unicode == PSF_SEPARATOR)
105 break;
106 up[ct].unicode = unicode;
107 up[ct].fontpos = glyph;
108 ct++;
109 }
110 }
111
112 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
113 this printf did not work on many kernels */
114
115 advice.advised_hashsize = 0;
116 advice.advised_hashstep = 0;
117 advice.advised_hashlevel = 0;
118 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
119#ifdef ENOIOCTLCMD
120 if (errno == ENOIOCTLCMD) {
121 bb_error_msg("It seems this kernel is older than 1.1.92");
122 bb_error_msg_and_die("No Unicode mapping table loaded.");
123 } else
124#endif
125 bb_perror_msg_and_die("PIO_UNIMAPCLR");
126 }
127 ud.entry_ct = ct;
128 ud.entries = up;
129 if (ioctl(fd, PIO_UNIMAP, &ud)) {
130#if 0
131 if (errno == ENOMEM) {
132 /* change advice parameters */
133 }
134#endif
135 bb_perror_msg_and_die("PIO_UNIMAP");
136 }
137}
138
139static void loadnewfont(int fd)
140{
141 int unit;
142 unsigned char inbuf[32768]; /* primitive */
143 unsigned int inputlth, offset;
144
145 /*
146 * We used to look at the length of the input file
147 * with stat(); now that we accept compressed files,
148 * just read the entire file.
149 */
150 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
151 if (ferror(stdin))
152 bb_perror_msg_and_die("Error reading input font");
153 /* use malloc/realloc in case of giant files;
154 maybe these do not occur: 16kB for the font,
155 and 16kB for the map leaves 32 unicode values
156 for each font position */
157 if (!feof(stdin))
158 bb_perror_msg_and_die("Font too large");
159
160 /* test for psf first */
161 {
162 struct psf_header psfhdr;
163 int fontsize;
164 int hastable;
165 unsigned int head0, head;
166
167 if (inputlth < sizeof(struct psf_header))
168 goto no_psf;
169
170 psfhdr = *(struct psf_header *) &inbuf[0];
171
172 if (!PSF_MAGIC_OK(psfhdr))
173 goto no_psf;
174
175 if (psfhdr.mode > PSF_MAXMODE)
176 bb_error_msg_and_die("Unsupported psf file mode");
177 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
178#if !defined( PIO_FONTX ) || defined( __sparc__ )
179 if (fontsize != 256)
180 bb_error_msg_and_die("Only fontsize 256 supported");
181#endif
182 hastable = (psfhdr.mode & PSF_MODEHASTAB);
183 unit = psfhdr.charsize;
184 head0 = sizeof(struct psf_header);
185
186 head = head0 + fontsize * unit;
187 if (head > inputlth || (!hastable && head != inputlth))
188 bb_error_msg_and_die("Input file: bad length");
189 do_loadfont(fd, inbuf + head0, unit, fontsize);
190 if (hastable)
191 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
192 return;
193 }
194 no_psf:
195
196 /* file with three code pages? */
197 if (inputlth == 9780) {
198 offset = 40;
199 unit = 16;
200 } else {
201 /* bare font */
202 if (inputlth & 0377)
203 bb_error_msg_and_die("Bad input file size");
204 offset = 0;
205 unit = inputlth / 256;
206 }
207 do_loadfont(fd, inbuf + offset, unit, 256);
208}
Note: See TracBrowser for help on using the repository browser.