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