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.)
18 PSF_MODEHASTAB = 0x02,
20 PSF_SEPARATOR = 0xFFFF
24 unsigned char magic1, magic2; /* Magic number */
25 unsigned char mode; /* PSF font mode */
26 unsigned char charsize; /* Character size */
29 #define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
31 static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
36 if (unit < 1 || unit > 32)
37 bb_error_msg_and_die("bad character size %d", unit);
39 buf = xzalloc(16 * 1024);
40 /*memset(buf, 0, 16 * 1024);*/
41 for (i = 0; i < fontsize; i++)
42 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
44 #if defined(PIO_FONTX) && !defined(__sparc__)
46 struct consolefontdesc cfd;
48 cfd.charcount = fontsize;
49 cfd.charheight = unit;
52 if (!ioctl_or_perror(fd, PIO_FONTX, &cfd, "PIO_FONTX ioctl failed (will try PIO_FONT)"))
53 goto ret; /* success */
56 xioctl(fd, PIO_FONT, buf);
62 do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
64 struct unimapinit advice;
71 maxct = tailsz; /* more than enough */
72 up = xmalloc(maxct * sizeof(struct unipair));
74 for (glyph = 0; glyph < fontsize; glyph++) {
76 unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
79 if (unicode == PSF_SEPARATOR)
81 up[ct].unicode = unicode;
82 up[ct].fontpos = glyph;
87 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
88 this printf did not work on many kernels */
90 advice.advised_hashsize = 0;
91 advice.advised_hashstep = 0;
92 advice.advised_hashlevel = 0;
93 xioctl(fd, PIO_UNIMAPCLR, &advice);
96 xioctl(fd, PIO_UNIMAP, &ud);
99 static void loadnewfont(int fd)
101 enum { INBUF_SIZE = 32*1024 + 1 };
104 unsigned inputlth, offset;
105 /* Was on stack, but 32k is a bit too much: */
106 unsigned char *inbuf = xmalloc(INBUF_SIZE);
109 * We used to look at the length of the input file
110 * with stat(); now that we accept compressed files,
111 * just read the entire file.
113 inputlth = full_read(STDIN_FILENO, inbuf, INBUF_SIZE);
115 bb_perror_msg_and_die("error reading input font");
116 if (inputlth >= INBUF_SIZE)
117 bb_error_msg_and_die("font too large");
119 /* test for psf first */
121 struct psf_header psfhdr;
124 unsigned head0, head;
126 if (inputlth < sizeof(struct psf_header))
129 psfhdr = *(struct psf_header *) &inbuf[0];
131 if (!PSF_MAGIC_OK(psfhdr))
134 if (psfhdr.mode > PSF_MAXMODE)
135 bb_error_msg_and_die("unsupported psf file mode");
136 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
137 #if !defined(PIO_FONTX) || defined(__sparc__)
139 bb_error_msg_and_die("only fontsize 256 supported");
141 hastable = (psfhdr.mode & PSF_MODEHASTAB);
142 unit = psfhdr.charsize;
143 head0 = sizeof(struct psf_header);
145 head = head0 + fontsize * unit;
146 if (head > inputlth || (!hastable && head != inputlth))
147 bb_error_msg_and_die("input file: bad length");
148 do_loadfont(fd, inbuf + head0, unit, fontsize);
150 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
155 /* file with three code pages? */
156 if (inputlth == 9780) {
162 bb_error_msg_and_die("bad input file size");
164 unit = inputlth / 256;
166 do_loadfont(fd, inbuf + offset, unit, 256);
169 int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
170 int loadfont_main(int argc, char **argv UNUSED_PARAM)
177 fd = xopen(CURRENT_VC, O_RDWR);