| 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_or_perror(fd, PIO_FONTX, &cfd, "PIO_FONTX ioctl failed (will try PIO_FONT)"))
|
|---|
| 53 | goto ret; /* success */
|
|---|
| 54 | }
|
|---|
| 55 | #endif
|
|---|
| 56 | xioctl(fd, PIO_FONT, buf);
|
|---|
| 57 | ret:
|
|---|
| 58 | free(buf);
|
|---|
| 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;
|
|---|
| 69 | uint16_t unicode;
|
|---|
| 70 |
|
|---|
| 71 | maxct = tailsz; /* more than enough */
|
|---|
| 72 | up = xmalloc(maxct * sizeof(struct unipair));
|
|---|
| 73 |
|
|---|
| 74 | for (glyph = 0; glyph < fontsize; glyph++) {
|
|---|
| 75 | while (tailsz >= 2) {
|
|---|
| 76 | unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
|
|---|
| 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;
|
|---|
| 93 | xioctl(fd, PIO_UNIMAPCLR, &advice);
|
|---|
| 94 | ud.entry_ct = ct;
|
|---|
| 95 | ud.entries = up;
|
|---|
| 96 | xioctl(fd, PIO_UNIMAP, &ud);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | static void loadnewfont(int fd)
|
|---|
| 100 | {
|
|---|
| 101 | enum { INBUF_SIZE = 32*1024 + 1 };
|
|---|
| 102 |
|
|---|
| 103 | int unit;
|
|---|
| 104 | unsigned inputlth, offset;
|
|---|
| 105 | /* Was on stack, but 32k is a bit too much: */
|
|---|
| 106 | unsigned char *inbuf = xmalloc(INBUF_SIZE);
|
|---|
| 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 | */
|
|---|
| 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");
|
|---|
| 118 |
|
|---|
| 119 | /* test for psf first */
|
|---|
| 120 | {
|
|---|
| 121 | struct psf_header psfhdr;
|
|---|
| 122 | int fontsize;
|
|---|
| 123 | int hastable;
|
|---|
| 124 | unsigned head0, head;
|
|---|
| 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)
|
|---|
| 135 | bb_error_msg_and_die("unsupported psf file mode");
|
|---|
| 136 | fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
|
|---|
| 137 | #if !defined(PIO_FONTX) || defined(__sparc__)
|
|---|
| 138 | if (fontsize != 256)
|
|---|
| 139 | bb_error_msg_and_die("only fontsize 256 supported");
|
|---|
| 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))
|
|---|
| 147 | bb_error_msg_and_die("input file: bad length");
|
|---|
| 148 | do_loadfont(fd, inbuf + head0, unit, fontsize);
|
|---|
| 149 | if (hastable)
|
|---|
| 150 | do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
|
|---|
| 151 | return;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | no_psf:
|
|---|
| 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)
|
|---|
| 162 | bb_error_msg_and_die("bad input file size");
|
|---|
| 163 | offset = 0;
|
|---|
| 164 | unit = inputlth / 256;
|
|---|
| 165 | }
|
|---|
| 166 | do_loadfont(fd, inbuf + offset, unit, 256);
|
|---|
| 167 | }
|
|---|
| 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 | }
|
|---|