blob: 78700706f3bca337139307c06e51e49d6eee5a11 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3e0fbae1999-10-19 06:02:44 +00002/*
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 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000011#include <sys/kd.h>
Eric Andersen3e0fbae1999-10-19 06:02:44 +000012
Denis Vlasenko83ea6432006-11-16 02:27:24 +000013enum {
Rob Landleybc68cd12006-03-10 19:22:06 +000014 PSF_MAGIC1 = 0x36,
15 PSF_MAGIC2 = 0x04,
Eric Andersen3e0fbae1999-10-19 06:02:44 +000016
Rob Landleybc68cd12006-03-10 19:22:06 +000017 PSF_MODE512 = 0x01,
18 PSF_MODEHASTAB = 0x02,
19 PSF_MAXMODE = 0x03,
20 PSF_SEPARATOR = 0xFFFF
21};
Eric Andersen3e0fbae1999-10-19 06:02:44 +000022
Erik Andersene49d5ec2000-02-08 19:58:47 +000023struct psf_header {
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000024 unsigned char magic1, magic2; /* Magic number */
25 unsigned char mode; /* PSF font mode */
26 unsigned char charsize; /* Character size */
Eric Andersen3e0fbae1999-10-19 06:02:44 +000027};
28
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +000029#define PSF_MAGIC_OK(x) ((x)->magic1 == PSF_MAGIC1 && (x)->magic2 == PSF_MAGIC2)
Eric Andersen3e0fbae1999-10-19 06:02:44 +000030
Eric Andersen5e678872006-01-30 19:48:23 +000031static void do_loadfont(int fd, unsigned char *inbuf, int unit, int fontsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +000032{
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000033 char *buf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000034 int i;
35
Matt Kraaief5529b2000-10-25 17:00:36 +000036 if (unit < 1 || unit > 32)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000037 bb_error_msg_and_die("bad character size %d", unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000038
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000039 buf = xzalloc(16 * 1024);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000040 for (i = 0; i < fontsize; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000042
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000043#if defined(PIO_FONTX) && !defined(__sparc__)
Eric Andersen3e0fbae1999-10-19 06:02:44 +000044 {
Erik Andersene49d5ec2000-02-08 19:58:47 +000045 struct consolefontdesc cfd;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000046
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 cfd.charcount = fontsize;
48 cfd.charheight = unit;
49 cfd.chardata = buf;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000050
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000051 if (!ioctl_or_perror(fd, PIO_FONTX, &cfd, "PIO_FONTX ioctl failed (will try PIO_FONT)"))
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000052 goto ret; /* success */
Eric Andersen3e0fbae1999-10-19 06:02:44 +000053 }
54#endif
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000055 xioctl(fd, PIO_FONT, buf);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000056 ret:
57 free(buf);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000058}
59
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +000060static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +000061{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000062 struct unimapinit advice;
63 struct unimapdesc ud;
64 struct unipair *up;
65 int ct = 0, maxct;
66 int glyph;
Denis Vlasenko28703012006-12-19 20:32:02 +000067 uint16_t unicode;
Eric Andersen3e0fbae1999-10-19 06:02:44 +000068
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +000069 maxct = tailsz; /* more than enough */
Denis Vlasenkob95636c2006-12-19 23:36:04 +000070 up = xmalloc(maxct * sizeof(struct unipair));
Erik Andersene49d5ec2000-02-08 19:58:47 +000071
Eric Andersen3e0fbae1999-10-19 06:02:44 +000072 for (glyph = 0; glyph < fontsize; glyph++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 while (tailsz >= 2) {
Denis Vlasenko28703012006-12-19 20:32:02 +000074 unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 tailsz -= 2;
76 inbuf += 2;
77 if (unicode == PSF_SEPARATOR)
78 break;
79 up[ct].unicode = unicode;
80 up[ct].fontpos = glyph;
81 ct++;
82 }
Eric Andersen3e0fbae1999-10-19 06:02:44 +000083 }
84
85 /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
86 this printf did not work on many kernels */
87
88 advice.advised_hashsize = 0;
89 advice.advised_hashstep = 0;
90 advice.advised_hashlevel = 0;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000091 xioctl(fd, PIO_UNIMAPCLR, &advice);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000092 ud.entry_ct = ct;
93 ud.entries = up;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000094 xioctl(fd, PIO_UNIMAP, &ud);
Eric Andersen3e0fbae1999-10-19 06:02:44 +000095}
96
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +000097static void do_load(int fd, struct psf_header *psfhdr, size_t len)
Erik Andersene49d5ec2000-02-08 19:58:47 +000098{
Eric Andersen3e0fbae1999-10-19 06:02:44 +000099 int unit;
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000100 int fontsize;
101 int hastable;
102 unsigned head0, head = head;
103
104 /* test for psf first */
105 if (len >= sizeof(struct psf_header) && PSF_MAGIC_OK(psfhdr)) {
106 if (psfhdr->mode > PSF_MAXMODE)
107 bb_error_msg_and_die("unsupported psf file mode");
108 fontsize = ((psfhdr->mode & PSF_MODE512) ? 512 : 256);
109#if !defined(PIO_FONTX) || defined(__sparc__)
110 if (fontsize != 256)
111 bb_error_msg_and_die("only fontsize 256 supported");
112#endif
113 hastable = (psfhdr->mode & PSF_MODEHASTAB);
114 unit = psfhdr->charsize;
115 head0 = sizeof(struct psf_header);
116
117 head = head0 + fontsize * unit;
118 if (head > len || (!hastable && head != len))
119 bb_error_msg_and_die("input file: bad length");
120 } else {
121 /* file with three code pages? */
122 if (len == 9780) {
123 head0 = 40;
124 unit = 16;
125 } else {
126 /* bare font */
127 if (len & 0377)
128 bb_error_msg_and_die("input file: bad length");
129 head0 = 0;
130 unit = len / 256;
131 }
132 fontsize = 256;
133 hastable = 0;
134 }
135
136 do_loadfont(fd, (unsigned char *)psfhdr + head0, unit, fontsize);
137 if (hastable)
138 do_loadtable(fd, (unsigned char *)psfhdr + head, len - head, fontsize);
139}
140
141#if ENABLE_LOADFONT
142int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
143int loadfont_main(int argc UNUSED_PARAM, char **argv)
144{
145 size_t len;
146 struct psf_header *psfhdr;
147
148 // no arguments allowed!
149 opt_complementary = "=0";
150 getopt32(argv, "");
Eric Andersen3e0fbae1999-10-19 06:02:44 +0000151
152 /*
153 * We used to look at the length of the input file
154 * with stat(); now that we accept compressed files,
155 * just read the entire file.
156 */
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000157 len = 32*1024; // can't be larger
158 psfhdr = (struct psf_header *) xmalloc_read(STDIN_FILENO, &len);
159 // xmalloc_open_zipped_read_close(filename, &len);
160 if (!psfhdr)
Denis Vlasenkod3d004d2006-10-27 09:02:31 +0000161 bb_perror_msg_and_die("error reading input font");
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000162 do_load(get_console_fd_or_die(), psfhdr, len);
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000163
164 return EXIT_SUCCESS;
165}
Denis Vlasenkoc8d02aa2008-08-17 14:12:26 +0000166#endif
167
168#if ENABLE_SETFONT
169int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
170int setfont_main(int argc UNUSED_PARAM, char **argv)
171{
172 size_t len;
173 struct psf_header *psfhdr;
174 char *mapfilename;
175 int fd;
176
177 opt_complementary = "=1";
178 getopt32(argv, "m:", &mapfilename);
179 argv += optind;
180
181 // load font
182 len = 32*1024; // can't be larger
183 psfhdr = (struct psf_header *) xmalloc_open_zipped_read_close(*argv, &len);
184 fd = get_console_fd_or_die();
185 do_load(fd, psfhdr, len);
186
187 // load the screen map, if any
188 if (option_mask32 & 1) { // -m
189 void *map = xmalloc_open_zipped_read_close(mapfilename, &len);
190 if (len == E_TABSZ || len == 2*E_TABSZ) {
191 xioctl(fd, (len == 2*E_TABSZ) ? PIO_UNISCRNMAP : PIO_SCRNMAP, map);
192 }
193 }
194
195 return EXIT_SUCCESS;
196}
197#endif