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.)
17 #include <sys/types.h>
21 #include <sys/ioctl.h>
25 #define PSF_MAGIC1 0x36
26 #define PSF_MAGIC2 0x04
28 #define PSF_MODE512 0x01
29 #define PSF_MODEHASTAB 0x02
30 #define PSF_MAXMODE 0x03
31 #define PSF_SEPARATOR 0xFFFF
33 static const char loadfont_usage[] = "loadfont\n"
34 #ifndef BB_FEATURE_TRIVIAL_HELP
35 "Loads a console font from standard input.\n"
40 unsigned char magic1, magic2; /* Magic number */
41 unsigned char mode; /* PSF font mode */
42 unsigned char charsize; /* Character size */
45 #define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2)
47 static void loadnewfont(int fd);
49 extern int loadfont_main(int argc, char **argv)
53 if (argc>=2 && *argv[1]=='-') {
54 usage(loadfont_usage);
57 fd = open("/dev/tty0", O_RDWR);
59 fprintf(stderr, "Error opening /dev/tty0: %s\n", strerror(errno));
67 static void do_loadfont(int fd, char *inbuf, int unit, int fontsize)
72 memset(buf, 0, sizeof(buf));
74 if (unit < 1 || unit > 32) {
75 fprintf(stderr, "Bad character size %d\n", unit);
79 for (i = 0; i < fontsize; i++)
80 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
82 #if defined( PIO_FONTX ) && !defined( __sparc__ )
84 struct consolefontdesc cfd;
86 cfd.charcount = fontsize;
87 cfd.charheight = unit;
90 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
92 perror("PIO_FONTX ioctl error (trying PIO_FONT)");
95 if (ioctl(fd, PIO_FONT, buf)) {
96 perror("PIO_FONT ioctl error");
102 do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
104 struct unimapinit advice;
105 struct unimapdesc ud;
111 maxct = tailsz; /* more than enough */
112 up = (struct unipair *) malloc(maxct * sizeof(struct unipair));
115 fprintf(stderr, "Out of memory?\n");
118 for (glyph = 0; glyph < fontsize; glyph++) {
119 while (tailsz >= 2) {
120 unicode = (((u_short) inbuf[1]) << 8) + inbuf[0];
123 if (unicode == PSF_SEPARATOR)
125 up[ct].unicode = unicode;
126 up[ct].fontpos = glyph;
131 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
132 this printf did not work on many kernels */
134 advice.advised_hashsize = 0;
135 advice.advised_hashstep = 0;
136 advice.advised_hashlevel = 0;
137 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
139 if (errno == ENOIOCTLCMD) {
140 fprintf(stderr, "It seems this kernel is older than 1.1.92\n");
141 fprintf(stderr, "No Unicode mapping table loaded.\n");
144 perror("PIO_UNIMAPCLR");
149 if (ioctl(fd, PIO_UNIMAP, &ud)) {
151 if (errno == ENOMEM) {
152 /* change advice parameters */
155 perror("PIO_UNIMAP");
160 static void loadnewfont(int fd)
163 char inbuf[32768]; /* primitive */
164 int inputlth, offset;
167 * We used to look at the length of the input file
168 * with stat(); now that we accept compressed files,
169 * just read the entire file.
171 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
173 perror("Error reading input font");
176 /* use malloc/realloc in case of giant files;
177 maybe these do not occur: 16kB for the font,
178 and 16kB for the map leaves 32 unicode values
179 for each font position */
181 perror("Font too large");
185 /* test for psf first */
187 struct psf_header psfhdr;
192 if (inputlth < sizeof(struct psf_header))
195 psfhdr = *(struct psf_header *) &inbuf[0];
197 if (!PSF_MAGIC_OK(psfhdr))
200 if (psfhdr.mode > PSF_MAXMODE) {
201 fprintf(stderr, "Unsupported psf file mode\n");
204 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
205 #if !defined( PIO_FONTX ) || defined( __sparc__ )
206 if (fontsize != 256) {
207 fprintf(stderr, "Only fontsize 256 supported\n");
211 hastable = (psfhdr.mode & PSF_MODEHASTAB);
212 unit = psfhdr.charsize;
213 head0 = sizeof(struct psf_header);
215 head = head0 + fontsize * unit;
216 if (head > inputlth || (!hastable && head != inputlth)) {
217 fprintf(stderr, "Input file: bad length\n");
220 do_loadfont(fd, inbuf + head0, unit, fontsize);
222 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
227 /* file with three code pages? */
228 if (inputlth == 9780) {
233 if (inputlth & 0377) {
234 fprintf(stderr, "Bad input file size\n");
238 unit = inputlth / 256;
240 do_loadfont(fd, inbuf + offset, unit, 256);