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.)
17 #include <sys/types.h>
20 #include <sys/ioctl.h>
24 #define PSF_MAGIC1 0x36
25 #define PSF_MAGIC2 0x04
27 #define PSF_MODE512 0x01
28 #define PSF_MODEHASTAB 0x02
29 #define PSF_MAXMODE 0x03
30 #define 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)
46 if (argc>=2 && *argv[1]=='-') {
47 usage(loadfont_usage);
50 fd = open("/dev/tty0", O_RDWR);
52 errorMsg("Error opening /dev/tty0: %s\n", strerror(errno));
60 static void do_loadfont(int fd, char *inbuf, int unit, int fontsize)
65 memset(buf, 0, sizeof(buf));
67 if (unit < 1 || unit > 32) {
68 errorMsg("Bad character size %d\n", unit);
72 for (i = 0; i < fontsize; i++)
73 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
75 #if defined( PIO_FONTX ) && !defined( __sparc__ )
77 struct consolefontdesc cfd;
79 cfd.charcount = fontsize;
80 cfd.charheight = unit;
83 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
85 perror("PIO_FONTX ioctl error (trying PIO_FONT)");
88 if (ioctl(fd, PIO_FONT, buf)) {
89 perror("PIO_FONT ioctl error");
95 do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
97 struct unimapinit advice;
104 maxct = tailsz; /* more than enough */
105 up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair));
107 for (glyph = 0; glyph < fontsize; glyph++) {
108 while (tailsz >= 2) {
109 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
112 if (unicode == PSF_SEPARATOR)
114 up[ct].unicode = unicode;
115 up[ct].fontpos = glyph;
120 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
121 this printf did not work on many kernels */
123 advice.advised_hashsize = 0;
124 advice.advised_hashstep = 0;
125 advice.advised_hashlevel = 0;
126 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
128 if (errno == ENOIOCTLCMD) {
129 errorMsg("It seems this kernel is older than 1.1.92\n");
130 errorMsg("No Unicode mapping table loaded.\n");
133 perror("PIO_UNIMAPCLR");
138 if (ioctl(fd, PIO_UNIMAP, &ud)) {
140 if (errno == ENOMEM) {
141 /* change advice parameters */
144 perror("PIO_UNIMAP");
149 static void loadnewfont(int fd)
152 char inbuf[32768]; /* primitive */
153 unsigned int inputlth, offset;
156 * We used to look at the length of the input file
157 * with stat(); now that we accept compressed files,
158 * just read the entire file.
160 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
162 perror("Error reading input font");
165 /* use malloc/realloc in case of giant files;
166 maybe these do not occur: 16kB for the font,
167 and 16kB for the map leaves 32 unicode values
168 for each font position */
170 perror("Font too large");
174 /* test for psf first */
176 struct psf_header psfhdr;
179 unsigned int head0, head;
181 if (inputlth < sizeof(struct psf_header))
184 psfhdr = *(struct psf_header *) &inbuf[0];
186 if (!PSF_MAGIC_OK(psfhdr))
189 if (psfhdr.mode > PSF_MAXMODE) {
190 errorMsg("Unsupported psf file mode\n");
193 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
194 #if !defined( PIO_FONTX ) || defined( __sparc__ )
195 if (fontsize != 256) {
196 errorMsg("Only fontsize 256 supported\n");
200 hastable = (psfhdr.mode & PSF_MODEHASTAB);
201 unit = psfhdr.charsize;
202 head0 = sizeof(struct psf_header);
204 head = head0 + fontsize * unit;
205 if (head > inputlth || (!hastable && head != inputlth)) {
206 errorMsg("Input file: bad length\n");
209 do_loadfont(fd, inbuf + head0, unit, fontsize);
211 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
216 /* file with three code pages? */
217 if (inputlth == 9780) {
222 if (inputlth & 0377) {
223 errorMsg("Bad input file size\n");
227 unit = inputlth / 256;
229 do_loadfont(fd, inbuf + offset, unit, 256);