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 loadnewfont(int fd);
33 int loadfont_main(int argc, char **argv)
40 fd = xopen(CURRENT_VC, O_RDWR);
46 static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
51 memset(buf, 0, sizeof(buf));
53 if (unit < 1 || unit > 32)
54 bb_error_msg_and_die("bad character size %d", unit);
56 for (i = 0; i < fontsize; i++)
57 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
59 #if defined( PIO_FONTX ) && !defined( __sparc__ )
61 struct consolefontdesc cfd;
63 cfd.charcount = fontsize;
64 cfd.charheight = unit;
67 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
69 bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
72 if (ioctl(fd, PIO_FONT, buf))
73 bb_perror_msg_and_die("PIO_FONT ioctl error");
77 do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
79 struct unimapinit advice;
86 maxct = tailsz; /* more than enough */
87 up = xmalloc(maxct * sizeof(struct unipair));
89 for (glyph = 0; glyph < fontsize; glyph++) {
91 unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
94 if (unicode == PSF_SEPARATOR)
96 up[ct].unicode = unicode;
97 up[ct].fontpos = glyph;
102 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
103 this printf did not work on many kernels */
105 advice.advised_hashsize = 0;
106 advice.advised_hashstep = 0;
107 advice.advised_hashlevel = 0;
108 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
110 if (errno == ENOIOCTLCMD) {
111 bb_error_msg("it seems this kernel is older than 1.1.92");
112 bb_error_msg_and_die("no Unicode mapping table loaded");
115 bb_perror_msg_and_die("PIO_UNIMAPCLR");
119 if (ioctl(fd, PIO_UNIMAP, &ud)) {
120 bb_perror_msg_and_die("PIO_UNIMAP");
124 static void loadnewfont(int fd)
127 unsigned char inbuf[32768]; /* primitive */
128 unsigned int inputlth, offset;
131 * We used to look at the length of the input file
132 * with stat(); now that we accept compressed files,
133 * just read the entire file.
135 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
137 bb_perror_msg_and_die("error reading input font");
138 /* use malloc/realloc in case of giant files;
139 maybe these do not occur: 16kB for the font,
140 and 16kB for the map leaves 32 unicode values
141 for each font position */
143 bb_perror_msg_and_die("font too large");
145 /* test for psf first */
147 struct psf_header psfhdr;
150 unsigned int head0, head;
152 if (inputlth < sizeof(struct psf_header))
155 psfhdr = *(struct psf_header *) &inbuf[0];
157 if (!PSF_MAGIC_OK(psfhdr))
160 if (psfhdr.mode > PSF_MAXMODE)
161 bb_error_msg_and_die("unsupported psf file mode");
162 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
163 #if !defined( PIO_FONTX ) || defined( __sparc__ )
165 bb_error_msg_and_die("only fontsize 256 supported");
167 hastable = (psfhdr.mode & PSF_MODEHASTAB);
168 unit = psfhdr.charsize;
169 head0 = sizeof(struct psf_header);
171 head = head0 + fontsize * unit;
172 if (head > inputlth || (!hastable && head != inputlth))
173 bb_error_msg_and_die("input file: bad length");
174 do_loadfont(fd, inbuf + head0, unit, fontsize);
176 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
181 /* file with three code pages? */
182 if (inputlth == 9780) {
188 bb_error_msg_and_die("bad input file size");
190 unit = inputlth / 256;
192 do_loadfont(fd, inbuf + offset, unit, 256);