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