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