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);
34 int loadfont_main(int argc, char **argv)
41 fd = xopen(CURRENT_VC, O_RDWR);
47 static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
52 memset(buf, 0, sizeof(buf));
54 if (unit < 1 || unit > 32)
55 bb_error_msg_and_die("bad character size %d", unit);
57 for (i = 0; i < fontsize; i++)
58 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
60 #if defined( PIO_FONTX ) && !defined( __sparc__ )
62 struct consolefontdesc cfd;
64 cfd.charcount = fontsize;
65 cfd.charheight = unit;
68 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
70 bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
73 if (ioctl(fd, PIO_FONT, buf))
74 bb_perror_msg_and_die("PIO_FONT ioctl error");
78 do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
80 struct unimapinit advice;
87 maxct = tailsz; /* more than enough */
88 up = xmalloc(maxct * sizeof(struct unipair));
90 for (glyph = 0; glyph < fontsize; glyph++) {
92 unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
95 if (unicode == PSF_SEPARATOR)
97 up[ct].unicode = unicode;
98 up[ct].fontpos = glyph;
103 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
104 this printf did not work on many kernels */
106 advice.advised_hashsize = 0;
107 advice.advised_hashstep = 0;
108 advice.advised_hashlevel = 0;
109 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
111 if (errno == ENOIOCTLCMD) {
112 bb_error_msg("it seems this kernel is older than 1.1.92");
113 bb_error_msg_and_die("no Unicode mapping table loaded");
116 bb_perror_msg_and_die("PIO_UNIMAPCLR");
120 if (ioctl(fd, PIO_UNIMAP, &ud)) {
121 bb_perror_msg_and_die("PIO_UNIMAP");
125 static void loadnewfont(int fd)
128 unsigned char inbuf[32768]; /* primitive */
129 unsigned int inputlth, offset;
132 * We used to look at the length of the input file
133 * with stat(); now that we accept compressed files,
134 * just read the entire file.
136 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
138 bb_perror_msg_and_die("error reading input font");
139 /* use malloc/realloc in case of giant files;
140 maybe these do not occur: 16kB for the font,
141 and 16kB for the map leaves 32 unicode values
142 for each font position */
144 bb_perror_msg_and_die("font too large");
146 /* test for psf first */
148 struct psf_header psfhdr;
151 unsigned int head0, head;
153 if (inputlth < sizeof(struct psf_header))
156 psfhdr = *(struct psf_header *) &inbuf[0];
158 if (!PSF_MAGIC_OK(psfhdr))
161 if (psfhdr.mode > PSF_MAXMODE)
162 bb_error_msg_and_die("unsupported psf file mode");
163 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
164 #if !defined( PIO_FONTX ) || defined( __sparc__ )
166 bb_error_msg_and_die("only fontsize 256 supported");
168 hastable = (psfhdr.mode & PSF_MODEHASTAB);
169 unit = psfhdr.charsize;
170 head0 = sizeof(struct psf_header);
172 head = head0 + fontsize * unit;
173 if (head > inputlth || (!hastable && head != inputlth))
174 bb_error_msg_and_die("input file: bad length");
175 do_loadfont(fd, inbuf + head0, unit, fontsize);
177 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
182 /* file with three code pages? */
183 if (inputlth == 9780) {
189 bb_error_msg_and_die("bad input file size");
191 unit = inputlth / 256;
193 do_loadfont(fd, inbuf + offset, unit, 256);