[821] | 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 | */
|
---|
[1765] | 10 | #include "libbb.h"
|
---|
[821] | 11 | #include <sys/kd.h>
|
---|
| 12 |
|
---|
[1765] | 13 | enum {
|
---|
[821] | 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 {
|
---|
[1765] | 24 | unsigned char magic1, magic2; /* Magic number */
|
---|
| 25 | unsigned char mode; /* PSF font mode */
|
---|
| 26 | unsigned char charsize; /* Character size */
|
---|
[821] | 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 | {
|
---|
[1765] | 33 | char *buf;
|
---|
[821] | 34 | int i;
|
---|
| 35 |
|
---|
| 36 | if (unit < 1 || unit > 32)
|
---|
[1765] | 37 | bb_error_msg_and_die("bad character size %d", unit);
|
---|
[821] | 38 |
|
---|
[1765] | 39 | buf = xzalloc(16 * 1024);
|
---|
| 40 | /*memset(buf, 0, 16 * 1024);*/
|
---|
[821] | 41 | for (i = 0; i < fontsize; i++)
|
---|
| 42 | memcpy(buf + (32 * i), inbuf + (unit * i), unit);
|
---|
| 43 |
|
---|
[1765] | 44 | #if defined(PIO_FONTX) && !defined(__sparc__)
|
---|
[821] | 45 | {
|
---|
| 46 | struct consolefontdesc cfd;
|
---|
| 47 |
|
---|
| 48 | cfd.charcount = fontsize;
|
---|
| 49 | cfd.charheight = unit;
|
---|
| 50 | cfd.chardata = buf;
|
---|
| 51 |
|
---|
[1765] | 52 | if (!ioctl_or_perror(fd, PIO_FONTX, &cfd, "PIO_FONTX ioctl failed (will try PIO_FONT)"))
|
---|
| 53 | goto ret; /* success */
|
---|
[821] | 54 | }
|
---|
| 55 | #endif
|
---|
[1765] | 56 | xioctl(fd, PIO_FONT, buf);
|
---|
| 57 | ret:
|
---|
| 58 | free(buf);
|
---|
[821] | 59 | }
|
---|
| 60 |
|
---|
| 61 | static void
|
---|
| 62 | do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
|
---|
| 63 | {
|
---|
| 64 | struct unimapinit advice;
|
---|
| 65 | struct unimapdesc ud;
|
---|
| 66 | struct unipair *up;
|
---|
| 67 | int ct = 0, maxct;
|
---|
| 68 | int glyph;
|
---|
[1765] | 69 | uint16_t unicode;
|
---|
[821] | 70 |
|
---|
| 71 | maxct = tailsz; /* more than enough */
|
---|
[1765] | 72 | up = xmalloc(maxct * sizeof(struct unipair));
|
---|
[821] | 73 |
|
---|
| 74 | for (glyph = 0; glyph < fontsize; glyph++) {
|
---|
| 75 | while (tailsz >= 2) {
|
---|
[1765] | 76 | unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
|
---|
[821] | 77 | tailsz -= 2;
|
---|
| 78 | inbuf += 2;
|
---|
| 79 | if (unicode == PSF_SEPARATOR)
|
---|
| 80 | break;
|
---|
| 81 | up[ct].unicode = unicode;
|
---|
| 82 | up[ct].fontpos = glyph;
|
---|
| 83 | ct++;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
|
---|
| 88 | this printf did not work on many kernels */
|
---|
| 89 |
|
---|
| 90 | advice.advised_hashsize = 0;
|
---|
| 91 | advice.advised_hashstep = 0;
|
---|
| 92 | advice.advised_hashlevel = 0;
|
---|
[1765] | 93 | xioctl(fd, PIO_UNIMAPCLR, &advice);
|
---|
[821] | 94 | ud.entry_ct = ct;
|
---|
| 95 | ud.entries = up;
|
---|
[1765] | 96 | xioctl(fd, PIO_UNIMAP, &ud);
|
---|
[821] | 97 | }
|
---|
| 98 |
|
---|
| 99 | static void loadnewfont(int fd)
|
---|
| 100 | {
|
---|
[1765] | 101 | enum { INBUF_SIZE = 32*1024 + 1 };
|
---|
| 102 |
|
---|
[821] | 103 | int unit;
|
---|
[1765] | 104 | unsigned inputlth, offset;
|
---|
| 105 | /* Was on stack, but 32k is a bit too much: */
|
---|
| 106 | unsigned char *inbuf = xmalloc(INBUF_SIZE);
|
---|
[821] | 107 |
|
---|
| 108 | /*
|
---|
| 109 | * We used to look at the length of the input file
|
---|
| 110 | * with stat(); now that we accept compressed files,
|
---|
| 111 | * just read the entire file.
|
---|
| 112 | */
|
---|
[1765] | 113 | inputlth = full_read(STDIN_FILENO, inbuf, INBUF_SIZE);
|
---|
| 114 | if (inputlth < 0)
|
---|
| 115 | bb_perror_msg_and_die("error reading input font");
|
---|
| 116 | if (inputlth >= INBUF_SIZE)
|
---|
| 117 | bb_error_msg_and_die("font too large");
|
---|
[821] | 118 |
|
---|
| 119 | /* test for psf first */
|
---|
| 120 | {
|
---|
| 121 | struct psf_header psfhdr;
|
---|
| 122 | int fontsize;
|
---|
| 123 | int hastable;
|
---|
[1765] | 124 | unsigned head0, head;
|
---|
[821] | 125 |
|
---|
| 126 | if (inputlth < sizeof(struct psf_header))
|
---|
| 127 | goto no_psf;
|
---|
| 128 |
|
---|
| 129 | psfhdr = *(struct psf_header *) &inbuf[0];
|
---|
| 130 |
|
---|
| 131 | if (!PSF_MAGIC_OK(psfhdr))
|
---|
| 132 | goto no_psf;
|
---|
| 133 |
|
---|
| 134 | if (psfhdr.mode > PSF_MAXMODE)
|
---|
[1765] | 135 | bb_error_msg_and_die("unsupported psf file mode");
|
---|
[821] | 136 | fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
|
---|
[1765] | 137 | #if !defined(PIO_FONTX) || defined(__sparc__)
|
---|
[821] | 138 | if (fontsize != 256)
|
---|
[1765] | 139 | bb_error_msg_and_die("only fontsize 256 supported");
|
---|
[821] | 140 | #endif
|
---|
| 141 | hastable = (psfhdr.mode & PSF_MODEHASTAB);
|
---|
| 142 | unit = psfhdr.charsize;
|
---|
| 143 | head0 = sizeof(struct psf_header);
|
---|
| 144 |
|
---|
| 145 | head = head0 + fontsize * unit;
|
---|
| 146 | if (head > inputlth || (!hastable && head != inputlth))
|
---|
[1765] | 147 | bb_error_msg_and_die("input file: bad length");
|
---|
[821] | 148 | do_loadfont(fd, inbuf + head0, unit, fontsize);
|
---|
| 149 | if (hastable)
|
---|
| 150 | do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
|
---|
| 151 | return;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[1765] | 154 | no_psf:
|
---|
[821] | 155 | /* file with three code pages? */
|
---|
| 156 | if (inputlth == 9780) {
|
---|
| 157 | offset = 40;
|
---|
| 158 | unit = 16;
|
---|
| 159 | } else {
|
---|
| 160 | /* bare font */
|
---|
| 161 | if (inputlth & 0377)
|
---|
[1765] | 162 | bb_error_msg_and_die("bad input file size");
|
---|
[821] | 163 | offset = 0;
|
---|
| 164 | unit = inputlth / 256;
|
---|
| 165 | }
|
---|
| 166 | do_loadfont(fd, inbuf + offset, unit, 256);
|
---|
| 167 | }
|
---|
[1765] | 168 |
|
---|
| 169 | int loadfont_main(int argc, char **argv);
|
---|
| 170 | int loadfont_main(int argc, char **argv)
|
---|
| 171 | {
|
---|
| 172 | int fd;
|
---|
| 173 |
|
---|
| 174 | if (argc != 1)
|
---|
| 175 | bb_show_usage();
|
---|
| 176 |
|
---|
| 177 | fd = xopen(CURRENT_VC, O_RDWR);
|
---|
| 178 | loadnewfont(fd);
|
---|
| 179 |
|
---|
| 180 | return EXIT_SUCCESS;
|
---|
| 181 | }
|
---|