Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 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 "internal.h" |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <memory.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <unistd.h> |
| 17 | #include <sys/types.h> |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 18 | #include <dirent.h> |
| 19 | #include <errno.h> |
| 20 | #include <sys/ioctl.h> |
| 21 | #include <sys/kd.h> |
| 22 | #include <endian.h> |
| 23 | |
| 24 | #define PSF_MAGIC1 0x36 |
| 25 | #define PSF_MAGIC2 0x04 |
| 26 | |
| 27 | #define PSF_MODE512 0x01 |
| 28 | #define PSF_MODEHASTAB 0x02 |
| 29 | #define PSF_MAXMODE 0x03 |
| 30 | #define PSF_SEPARATOR 0xFFFF |
| 31 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 32 | struct psf_header { |
| 33 | unsigned char magic1, magic2; /* Magic number */ |
| 34 | unsigned char mode; /* PSF font mode */ |
| 35 | unsigned char charsize; /* Character size */ |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | #define PSF_MAGIC_OK(x) ((x).magic1 == PSF_MAGIC1 && (x).magic2 == PSF_MAGIC2) |
| 39 | |
| 40 | static void loadnewfont(int fd); |
| 41 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | extern int loadfont_main(int argc, char **argv) |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 43 | { |
| 44 | int fd; |
| 45 | |
Erik Andersen | a6c7522 | 2000-04-18 00:00:52 +0000 | [diff] [blame] | 46 | if (argc>=2 && *argv[1]=='-') { |
| 47 | usage(loadfont_usage); |
| 48 | } |
| 49 | |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 50 | fd = open("/dev/tty0", O_RDWR); |
| 51 | if (fd < 0) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 52 | errorMsg("Error opening /dev/tty0: %s\n", strerror(errno)); |
Erik Andersen | 3fe7f9f | 2000-04-19 03:59:10 +0000 | [diff] [blame] | 53 | return( FALSE); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 54 | } |
| 55 | loadnewfont(fd); |
| 56 | |
Erik Andersen | 3fe7f9f | 2000-04-19 03:59:10 +0000 | [diff] [blame] | 57 | return( TRUE); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | static void do_loadfont(int fd, char *inbuf, int unit, int fontsize) |
| 61 | { |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 62 | char buf[16384]; |
| 63 | int i; |
| 64 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | memset(buf, 0, sizeof(buf)); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 66 | |
| 67 | if (unit < 1 || unit > 32) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 68 | errorMsg("Bad character size %d\n", unit); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | exit(1); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | for (i = 0; i < fontsize; i++) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | memcpy(buf + (32 * i), inbuf + (unit * i), unit); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 74 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 75 | #if defined( PIO_FONTX ) && !defined( __sparc__ ) |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 76 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | struct consolefontdesc cfd; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 78 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | cfd.charcount = fontsize; |
| 80 | cfd.charheight = unit; |
| 81 | cfd.chardata = buf; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 82 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | if (ioctl(fd, PIO_FONTX, &cfd) == 0) |
| 84 | return; /* success */ |
| 85 | perror("PIO_FONTX ioctl error (trying PIO_FONT)"); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 86 | } |
| 87 | #endif |
| 88 | if (ioctl(fd, PIO_FONT, buf)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | perror("PIO_FONT ioctl error"); |
| 90 | exit(1); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | static void |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 95 | do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize) |
| 96 | { |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 97 | struct unimapinit advice; |
| 98 | struct unimapdesc ud; |
| 99 | struct unipair *up; |
| 100 | int ct = 0, maxct; |
| 101 | int glyph; |
| 102 | u_short unicode; |
| 103 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 104 | maxct = tailsz; /* more than enough */ |
Matt Kraai | 322ae93 | 2000-09-13 02:46:14 +0000 | [diff] [blame] | 105 | up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 107 | for (glyph = 0; glyph < fontsize; glyph++) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 108 | while (tailsz >= 2) { |
| 109 | unicode = (((u_short) inbuf[1]) << 8) + inbuf[0]; |
| 110 | tailsz -= 2; |
| 111 | inbuf += 2; |
| 112 | if (unicode == PSF_SEPARATOR) |
| 113 | break; |
| 114 | up[ct].unicode = unicode; |
| 115 | up[ct].fontpos = glyph; |
| 116 | ct++; |
| 117 | } |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP |
| 121 | this printf did not work on many kernels */ |
| 122 | |
| 123 | advice.advised_hashsize = 0; |
| 124 | advice.advised_hashstep = 0; |
| 125 | advice.advised_hashlevel = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 126 | if (ioctl(fd, PIO_UNIMAPCLR, &advice)) { |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 127 | #ifdef ENOIOCTLCMD |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 128 | if (errno == ENOIOCTLCMD) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 129 | errorMsg("It seems this kernel is older than 1.1.92\n"); |
| 130 | errorMsg("No Unicode mapping table loaded.\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 131 | } else |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 132 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 133 | perror("PIO_UNIMAPCLR"); |
| 134 | exit(1); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 135 | } |
| 136 | ud.entry_ct = ct; |
| 137 | ud.entries = up; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 138 | if (ioctl(fd, PIO_UNIMAP, &ud)) { |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 139 | #if 0 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 140 | if (errno == ENOMEM) { |
| 141 | /* change advice parameters */ |
| 142 | } |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 143 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 144 | perror("PIO_UNIMAP"); |
| 145 | exit(1); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 149 | static void loadnewfont(int fd) |
| 150 | { |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 151 | int unit; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 152 | char inbuf[32768]; /* primitive */ |
Pavel Roskin | df4532b | 2000-07-14 17:24:58 +0000 | [diff] [blame] | 153 | unsigned int inputlth, offset; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * We used to look at the length of the input file |
| 157 | * with stat(); now that we accept compressed files, |
| 158 | * just read the entire file. |
| 159 | */ |
| 160 | inputlth = fread(inbuf, 1, sizeof(inbuf), stdin); |
| 161 | if (ferror(stdin)) { |
| 162 | perror("Error reading input font"); |
| 163 | exit(1); |
| 164 | } |
| 165 | /* use malloc/realloc in case of giant files; |
| 166 | maybe these do not occur: 16kB for the font, |
| 167 | and 16kB for the map leaves 32 unicode values |
| 168 | for each font position */ |
| 169 | if (!feof(stdin)) { |
| 170 | perror("Font too large"); |
| 171 | exit(1); |
| 172 | } |
| 173 | |
| 174 | /* test for psf first */ |
| 175 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 176 | struct psf_header psfhdr; |
| 177 | int fontsize; |
| 178 | int hastable; |
Pavel Roskin | df4532b | 2000-07-14 17:24:58 +0000 | [diff] [blame] | 179 | unsigned int head0, head; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 180 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 181 | if (inputlth < sizeof(struct psf_header)) |
| 182 | goto no_psf; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 183 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 184 | psfhdr = *(struct psf_header *) &inbuf[0]; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 185 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 186 | if (!PSF_MAGIC_OK(psfhdr)) |
| 187 | goto no_psf; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 188 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 189 | if (psfhdr.mode > PSF_MAXMODE) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 190 | errorMsg("Unsupported psf file mode\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 191 | exit(1); |
| 192 | } |
| 193 | fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 194 | #if !defined( PIO_FONTX ) || defined( __sparc__ ) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 195 | if (fontsize != 256) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 196 | errorMsg("Only fontsize 256 supported\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 197 | exit(1); |
| 198 | } |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 199 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 200 | hastable = (psfhdr.mode & PSF_MODEHASTAB); |
| 201 | unit = psfhdr.charsize; |
| 202 | head0 = sizeof(struct psf_header); |
| 203 | |
| 204 | head = head0 + fontsize * unit; |
| 205 | if (head > inputlth || (!hastable && head != inputlth)) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 206 | errorMsg("Input file: bad length\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 207 | exit(1); |
| 208 | } |
| 209 | do_loadfont(fd, inbuf + head0, unit, fontsize); |
| 210 | if (hastable) |
| 211 | do_loadtable(fd, inbuf + head, inputlth - head, fontsize); |
| 212 | return; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 213 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 214 | no_psf: |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 215 | |
| 216 | /* file with three code pages? */ |
| 217 | if (inputlth == 9780) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 218 | offset = 40; |
| 219 | unit = 16; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 220 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 221 | /* bare font */ |
| 222 | if (inputlth & 0377) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 223 | errorMsg("Bad input file size\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 224 | exit(1); |
| 225 | } |
| 226 | offset = 0; |
| 227 | unit = inputlth / 256; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 228 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 229 | do_loadfont(fd, inbuf + offset, unit, 256); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 230 | } |