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.)
15 #include <sys/types.h>
18 #include <sys/ioctl.h>
28 PSF_MODEHASTAB = 0x02,
30 PSF_SEPARATOR = 0xFFFF
34 unsigned char magic1, magic2; /* Magic number */
35 unsigned char mode; /* PSF font mode */
36 unsigned char charsize; /* Character size */
39 #define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
41 static void loadnewfont(int fd);
43 int loadfont_main(int argc, char **argv)
50 fd = bb_xopen(CURRENT_VC, O_RDWR);
56 static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
61 memset(buf, 0, sizeof(buf));
63 if (unit < 1 || unit > 32)
64 bb_error_msg_and_die("Bad character size %d", unit);
66 for (i = 0; i < fontsize; i++)
67 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
69 #if defined( PIO_FONTX ) && !defined( __sparc__ )
71 struct consolefontdesc cfd;
73 cfd.charcount = fontsize;
74 cfd.charheight = unit;
77 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
79 bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
82 if (ioctl(fd, PIO_FONT, buf))
83 bb_perror_msg_and_die("PIO_FONT ioctl error");
87 do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
89 struct unimapinit advice;
96 maxct = tailsz; /* more than enough */
97 up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair));
99 for (glyph = 0; glyph < fontsize; glyph++) {
100 while (tailsz >= 2) {
101 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
104 if (unicode == PSF_SEPARATOR)
106 up[ct].unicode = unicode;
107 up[ct].fontpos = glyph;
112 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
113 this printf did not work on many kernels */
115 advice.advised_hashsize = 0;
116 advice.advised_hashstep = 0;
117 advice.advised_hashlevel = 0;
118 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
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.");
125 bb_perror_msg_and_die("PIO_UNIMAPCLR");
129 if (ioctl(fd, PIO_UNIMAP, &ud)) {
131 if (errno == ENOMEM) {
132 /* change advice parameters */
135 bb_perror_msg_and_die("PIO_UNIMAP");
139 static void loadnewfont(int fd)
142 unsigned char inbuf[32768]; /* primitive */
143 unsigned int inputlth, offset;
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.
150 inputlth = fread(inbuf, 1, sizeof(inbuf), 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 */
158 bb_perror_msg_and_die("Font too large");
160 /* test for psf first */
162 struct psf_header psfhdr;
165 unsigned int head0, head;
167 if (inputlth < sizeof(struct psf_header))
170 psfhdr = *(struct psf_header *) &inbuf[0];
172 if (!PSF_MAGIC_OK(psfhdr))
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__ )
180 bb_error_msg_and_die("Only fontsize 256 supported");
182 hastable = (psfhdr.mode & PSF_MODEHASTAB);
183 unit = psfhdr.charsize;
184 head0 = sizeof(struct psf_header);
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);
191 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
196 /* file with three code pages? */
197 if (inputlth == 9780) {
203 bb_error_msg_and_die("Bad input file size");
205 unit = inputlth / 256;
207 do_loadfont(fd, inbuf + offset, unit, 256);