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