2 * loadfont.c - Eugene Crosser & Andries Brouwer
6 * Loads the console font, and possibly the corresponding screen map(s).
7 * (Adapted for busybox by Matej Vela.)
16 #include <sys/types.h>
20 #include <sys/ioctl.h>
24 #define PSF_MAGIC1 0x36
25 #define PSF_MAGIC2 0x04
27 #define PSF_MODE512 0x01
28 #define PSF_MODEHASTAB 0x02
29 #define PSF_MAXMODE 0x03
30 #define PSF_SEPARATOR 0xFFFF
32 static const char loadfont_usage[] = "loadfont\n"
34 "\tLoad a console font from standard input.\n"
39 unsigned char magic1, magic2; /* Magic number */
40 unsigned char mode; /* PSF font mode */
41 unsigned char charsize; /* Character size */
44 #define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
46 static void loadnewfont(int fd);
49 loadfont_main(int argc, char **argv)
53 fd = open("/dev/tty0", O_RDWR);
55 fprintf(stderr, "Error opening /dev/tty0: %s\n", strerror(errno));
64 do_loadfont(int fd, char *inbuf, int unit, int fontsize) {
68 memset(buf,0,sizeof(buf));
70 if (unit < 1 || unit > 32) {
71 fprintf(stderr, "Bad character size %d\n", unit);
75 for (i = 0; i < fontsize; i++)
76 memcpy(buf+(32*i), inbuf+(unit*i), unit);
78 #if defined( PIO_FONTX ) && !defined( __sparc__ )
80 struct consolefontdesc cfd;
82 cfd.charcount = fontsize;
83 cfd.charheight = unit;
86 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
88 perror("PIO_FONTX ioctl error (trying PIO_FONT)");
91 if (ioctl(fd, PIO_FONT, buf)) {
92 perror("PIO_FONT ioctl error");
98 do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize) {
99 struct unimapinit advice;
100 struct unimapdesc ud;
106 maxct = tailsz; /* more than enough */
107 up = (struct unipair *) malloc(maxct * sizeof(struct unipair));
109 fprintf(stderr, "Out of memory?\n");
112 for (glyph = 0; glyph < fontsize; glyph++) {
113 while (tailsz >= 2) {
114 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
117 if (unicode == PSF_SEPARATOR)
119 up[ct].unicode = unicode;
120 up[ct].fontpos = glyph;
125 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
126 this printf did not work on many kernels */
128 advice.advised_hashsize = 0;
129 advice.advised_hashstep = 0;
130 advice.advised_hashlevel = 0;
131 if(ioctl(fd, PIO_UNIMAPCLR, &advice)) {
133 if (errno == ENOIOCTLCMD) {
134 fprintf(stderr, "It seems this kernel is older than 1.1.92\n");
135 fprintf(stderr, "No Unicode mapping table loaded.\n");
138 perror("PIO_UNIMAPCLR");
143 if(ioctl(fd, PIO_UNIMAP, &ud)) {
145 if (errno == ENOMEM) {
146 /* change advice parameters */
149 perror("PIO_UNIMAP");
155 loadnewfont(int fd) {
157 char inbuf[32768]; /* primitive */
158 int inputlth, offset;
161 * We used to look at the length of the input file
162 * with stat(); now that we accept compressed files,
163 * just read the entire file.
165 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
167 perror("Error reading input font");
170 /* use malloc/realloc in case of giant files;
171 maybe these do not occur: 16kB for the font,
172 and 16kB for the map leaves 32 unicode values
173 for each font position */
175 perror("Font too large");
179 /* test for psf first */
181 struct psf_header psfhdr;
186 if (inputlth < sizeof(struct psf_header))
189 psfhdr = * (struct psf_header *) &inbuf[0];
191 if (!PSF_MAGIC_OK(psfhdr))
194 if (psfhdr.mode > PSF_MAXMODE) {
195 fprintf(stderr, "Unsupported psf file mode\n");
198 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
199 #if !defined( PIO_FONTX ) || defined( __sparc__ )
200 if (fontsize != 256) {
201 fprintf(stderr, "Only fontsize 256 supported\n");
205 hastable = (psfhdr.mode & PSF_MODEHASTAB);
206 unit = psfhdr.charsize;
207 head0 = sizeof(struct psf_header);
208 head = head0 + fontsize*unit;
209 if (head > inputlth || (!hastable && head != inputlth)) {
210 fprintf(stderr, "Input file: bad length\n");
213 do_loadfont(fd, inbuf + head0, unit, fontsize);
215 do_loadtable(fd, inbuf + head, inputlth-head, fontsize);
220 /* file with three code pages? */
221 if (inputlth == 9780) {
226 if (inputlth & 0377) {
227 fprintf(stderr, "Bad input file size\n");
233 do_loadfont(fd, inbuf+offset, unit, 256);