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