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(fd, PIO_FONTX, &cfd) == 0)
53 goto ret; /* success */
54 bb_perror_msg("PIO_FONTX ioctl (will try PIO_FONT)");
57 if (ioctl(fd, PIO_FONT, buf))
58 bb_perror_msg_and_die("PIO_FONT ioctl");
64 do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
66 struct unimapinit advice;
73 maxct = tailsz; /* more than enough */
74 up = xmalloc(maxct * sizeof(struct unipair));
76 for (glyph = 0; glyph < fontsize; glyph++) {
78 unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
81 if (unicode == PSF_SEPARATOR)
83 up[ct].unicode = unicode;
84 up[ct].fontpos = glyph;
89 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
90 this printf did not work on many kernels */
92 advice.advised_hashsize = 0;
93 advice.advised_hashstep = 0;
94 advice.advised_hashlevel = 0;
95 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
97 if (errno == ENOIOCTLCMD) {
98 bb_error_msg("it seems this kernel is older than 1.1.92");
99 bb_error_msg_and_die("no Unicode mapping table loaded");
102 bb_perror_msg_and_die("PIO_UNIMAPCLR");
106 if (ioctl(fd, PIO_UNIMAP, &ud)) {
107 bb_perror_msg_and_die("PIO_UNIMAP");
111 static void loadnewfont(int fd)
113 enum { INBUF_SIZE = 32*1024 + 1 };
116 unsigned inputlth, offset;
117 /* Was on stack, but 32k is a bit too much: */
118 unsigned char *inbuf = xmalloc(INBUF_SIZE);
121 * We used to look at the length of the input file
122 * with stat(); now that we accept compressed files,
123 * just read the entire file.
125 inputlth = full_read(STDIN_FILENO, inbuf, INBUF_SIZE);
127 bb_perror_msg_and_die("error reading input font");
128 if (inputlth >= INBUF_SIZE)
129 bb_error_msg_and_die("font too large");
131 /* test for psf first */
133 struct psf_header psfhdr;
136 unsigned head0, head;
138 if (inputlth < sizeof(struct psf_header))
141 psfhdr = *(struct psf_header *) &inbuf[0];
143 if (!PSF_MAGIC_OK(psfhdr))
146 if (psfhdr.mode > PSF_MAXMODE)
147 bb_error_msg_and_die("unsupported psf file mode");
148 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
149 #if !defined(PIO_FONTX) || defined(__sparc__)
151 bb_error_msg_and_die("only fontsize 256 supported");
153 hastable = (psfhdr.mode & PSF_MODEHASTAB);
154 unit = psfhdr.charsize;
155 head0 = sizeof(struct psf_header);
157 head = head0 + fontsize * unit;
158 if (head > inputlth || (!hastable && head != inputlth))
159 bb_error_msg_and_die("input file: bad length");
160 do_loadfont(fd, inbuf + head0, unit, fontsize);
162 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
167 /* file with three code pages? */
168 if (inputlth == 9780) {
174 bb_error_msg_and_die("bad input file size");
176 unit = inputlth / 256;
178 do_loadfont(fd, inbuf + offset, unit, 256);
181 int loadfont_main(int argc, char **argv);
182 int loadfont_main(int argc, char **argv)
189 fd = xopen(CURRENT_VC, O_RDWR);