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)) {
130 bb_perror_msg_and_die("PIO_UNIMAP");
134 static void loadnewfont(int fd)
137 unsigned char inbuf[32768]; /* primitive */
138 unsigned int inputlth, offset;
141 * We used to look at the length of the input file
142 * with stat(); now that we accept compressed files,
143 * just read the entire file.
145 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
147 bb_perror_msg_and_die("Error reading input font");
148 /* use malloc/realloc in case of giant files;
149 maybe these do not occur: 16kB for the font,
150 and 16kB for the map leaves 32 unicode values
151 for each font position */
153 bb_perror_msg_and_die("Font too large");
155 /* test for psf first */
157 struct psf_header psfhdr;
160 unsigned int head0, head;
162 if (inputlth < sizeof(struct psf_header))
165 psfhdr = *(struct psf_header *) &inbuf[0];
167 if (!PSF_MAGIC_OK(psfhdr))
170 if (psfhdr.mode > PSF_MAXMODE)
171 bb_error_msg_and_die("Unsupported psf file mode");
172 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
173 #if !defined( PIO_FONTX ) || defined( __sparc__ )
175 bb_error_msg_and_die("Only fontsize 256 supported");
177 hastable = (psfhdr.mode & PSF_MODEHASTAB);
178 unit = psfhdr.charsize;
179 head0 = sizeof(struct psf_header);
181 head = head0 + fontsize * unit;
182 if (head > inputlth || (!hastable && head != inputlth))
183 bb_error_msg_and_die("Input file: bad length");
184 do_loadfont(fd, inbuf + head0, unit, fontsize);
186 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
191 /* file with three code pages? */
192 if (inputlth == 9780) {
198 bb_error_msg_and_die("Bad input file size");
200 unit = inputlth / 256;
202 do_loadfont(fd, inbuf + offset, unit, 256);