b046d40e38642ab7454c9f4809aace7ef06b8e67
[oweals/busybox.git] / console-tools / loadfont.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * loadfont.c - Eugene Crosser & Andries Brouwer
4  *
5  * Version 0.96bb
6  *
7  * Loads the console font, and possibly the corresponding screen map(s).
8  * (Adapted for busybox by Matej Vela.)
9  */
10 #include "libbb.h"
11 #include <sys/kd.h>
12
13 enum {
14         PSF_MAGIC1 = 0x36,
15         PSF_MAGIC2 = 0x04,
16
17         PSF_MODE512 = 0x01,
18         PSF_MODEHASTAB = 0x02,
19         PSF_MAXMODE = 0x03,
20         PSF_SEPARATOR = 0xFFFF
21 };
22
23 struct psf_header {
24         unsigned char magic1, magic2;   /* Magic number */
25         unsigned char mode;             /* PSF font mode */
26         unsigned char charsize;         /* Character size */
27 };
28
29 #define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
30
31 static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
32 {
33         char *buf;
34         int i;
35
36         if (unit < 1 || unit > 32)
37                 bb_error_msg_and_die("bad character size %d", unit);
38
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);
43
44 #if defined(PIO_FONTX) && !defined(__sparc__)
45         {
46                 struct consolefontdesc cfd;
47
48                 cfd.charcount = fontsize;
49                 cfd.charheight = unit;
50                 cfd.chardata = buf;
51
52                 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
53                         goto ret;                       /* success */
54                 bb_perror_msg("PIO_FONTX ioctl (will try PIO_FONT)");
55         }
56 #endif
57         if (ioctl(fd, PIO_FONT, buf))
58                 bb_perror_msg_and_die("PIO_FONT ioctl");
59  ret:
60         free(buf);
61 }
62
63 static void
64 do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
65 {
66         struct unimapinit advice;
67         struct unimapdesc ud;
68         struct unipair *up;
69         int ct = 0, maxct;
70         int glyph;
71         uint16_t unicode;
72
73         maxct = tailsz;                         /* more than enough */
74         up = xmalloc(maxct * sizeof(struct unipair));
75
76         for (glyph = 0; glyph < fontsize; glyph++) {
77                 while (tailsz >= 2) {
78                         unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
79                         tailsz -= 2;
80                         inbuf += 2;
81                         if (unicode == PSF_SEPARATOR)
82                                 break;
83                         up[ct].unicode = unicode;
84                         up[ct].fontpos = glyph;
85                         ct++;
86                 }
87         }
88
89         /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
90            this printf did not work on many kernels */
91
92         advice.advised_hashsize = 0;
93         advice.advised_hashstep = 0;
94         advice.advised_hashlevel = 0;
95         if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
96 #ifdef ENOIOCTLCMD
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");
100                 } else
101 #endif
102                         bb_perror_msg_and_die("PIO_UNIMAPCLR");
103         }
104         ud.entry_ct = ct;
105         ud.entries = up;
106         if (ioctl(fd, PIO_UNIMAP, &ud)) {
107                 bb_perror_msg_and_die("PIO_UNIMAP");
108         }
109 }
110
111 static void loadnewfont(int fd)
112 {
113         enum { INBUF_SIZE = 32*1024 + 1 };
114
115         int unit;
116         unsigned inputlth, offset;
117         /* Was on stack, but 32k is a bit too much: */
118         unsigned char *inbuf = xmalloc(INBUF_SIZE);
119
120         /*
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.
124          */
125         inputlth = full_read(STDIN_FILENO, inbuf, INBUF_SIZE);
126         if (inputlth < 0)
127                 bb_perror_msg_and_die("error reading input font");
128         if (inputlth >= INBUF_SIZE)
129                 bb_error_msg_and_die("font too large");
130
131         /* test for psf first */
132         {
133                 struct psf_header psfhdr;
134                 int fontsize;
135                 int hastable;
136                 unsigned head0, head;
137
138                 if (inputlth < sizeof(struct psf_header))
139                         goto no_psf;
140
141                 psfhdr = *(struct psf_header *) &inbuf[0];
142
143                 if (!PSF_MAGIC_OK(psfhdr))
144                         goto no_psf;
145
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__)
150                 if (fontsize != 256)
151                         bb_error_msg_and_die("only fontsize 256 supported");
152 #endif
153                 hastable = (psfhdr.mode & PSF_MODEHASTAB);
154                 unit = psfhdr.charsize;
155                 head0 = sizeof(struct psf_header);
156
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);
161                 if (hastable)
162                         do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
163                 return;
164         }
165
166  no_psf:
167         /* file with three code pages? */
168         if (inputlth == 9780) {
169                 offset = 40;
170                 unit = 16;
171         } else {
172                 /* bare font */
173                 if (inputlth & 0377)
174                         bb_error_msg_and_die("bad input file size");
175                 offset = 0;
176                 unit = inputlth / 256;
177         }
178         do_loadfont(fd, inbuf + offset, unit, 256);
179 }
180
181 int loadfont_main(int argc, char **argv);
182 int loadfont_main(int argc, char **argv)
183 {
184         int fd;
185
186         if (argc != 1)
187                 bb_show_usage();
188
189         fd = xopen(CURRENT_VC, O_RDWR);
190         loadnewfont(fd);
191
192         return EXIT_SUCCESS;
193 }