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