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