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 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.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 { |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 29 | #define PSF_MAGIC_OK(x) ((x)->magic1 == PSF_MAGIC1 && (x)->magic2 == PSF_MAGIC2) |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 30 | |
Eric Andersen | 5e67887 | 2006-01-30 19:48:23 +0000 | [diff] [blame] | 31 | 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] | 32 | { |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 33 | char *buf; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 34 | int i; |
| 35 | |
Matt Kraai | ef5529b | 2000-10-25 17:00:36 +0000 | [diff] [blame] | 36 | if (unit < 1 || unit > 32) |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 37 | bb_error_msg_and_die("bad character size %d", unit); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 39 | buf = xzalloc(16 * 1024); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 40 | for (i = 0; i < fontsize; i++) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | memcpy(buf + (32 * i), inbuf + (unit * i), unit); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 43 | #if defined(PIO_FONTX) && !defined(__sparc__) |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 44 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 45 | struct consolefontdesc cfd; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 46 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | cfd.charcount = fontsize; |
| 48 | cfd.charheight = unit; |
| 49 | cfd.chardata = buf; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 51 | if (!ioctl_or_perror(fd, PIO_FONTX, &cfd, "PIO_FONTX ioctl failed (will try PIO_FONT)")) |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 52 | goto ret; /* success */ |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 53 | } |
| 54 | #endif |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 55 | xioctl(fd, PIO_FONT, buf); |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 56 | ret: |
| 57 | free(buf); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 60 | static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | { |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 62 | struct unimapinit advice; |
| 63 | struct unimapdesc ud; |
| 64 | struct unipair *up; |
| 65 | int ct = 0, maxct; |
| 66 | int glyph; |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 67 | uint16_t unicode; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 69 | maxct = tailsz; /* more than enough */ |
Denis Vlasenko | b95636c | 2006-12-19 23:36:04 +0000 | [diff] [blame] | 70 | up = xmalloc(maxct * sizeof(struct unipair)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 72 | for (glyph = 0; glyph < fontsize; glyph++) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | while (tailsz >= 2) { |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 74 | unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | tailsz -= 2; |
| 76 | inbuf += 2; |
| 77 | if (unicode == PSF_SEPARATOR) |
| 78 | break; |
| 79 | up[ct].unicode = unicode; |
| 80 | up[ct].fontpos = glyph; |
| 81 | ct++; |
| 82 | } |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP |
| 86 | this printf did not work on many kernels */ |
| 87 | |
| 88 | advice.advised_hashsize = 0; |
| 89 | advice.advised_hashstep = 0; |
| 90 | advice.advised_hashlevel = 0; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 91 | xioctl(fd, PIO_UNIMAPCLR, &advice); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 92 | ud.entry_ct = ct; |
| 93 | ud.entries = up; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 94 | xioctl(fd, PIO_UNIMAP, &ud); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 97 | static void do_load(int fd, struct psf_header *psfhdr, size_t len) |
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 | int unit; |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 100 | int fontsize; |
| 101 | int hastable; |
| 102 | unsigned head0, head = head; |
| 103 | |
| 104 | /* test for psf first */ |
| 105 | if (len >= sizeof(struct psf_header) && PSF_MAGIC_OK(psfhdr)) { |
| 106 | if (psfhdr->mode > PSF_MAXMODE) |
| 107 | bb_error_msg_and_die("unsupported psf file mode"); |
| 108 | fontsize = ((psfhdr->mode & PSF_MODE512) ? 512 : 256); |
| 109 | #if !defined(PIO_FONTX) || defined(__sparc__) |
| 110 | if (fontsize != 256) |
| 111 | bb_error_msg_and_die("only fontsize 256 supported"); |
| 112 | #endif |
| 113 | hastable = (psfhdr->mode & PSF_MODEHASTAB); |
| 114 | unit = psfhdr->charsize; |
| 115 | head0 = sizeof(struct psf_header); |
| 116 | |
| 117 | head = head0 + fontsize * unit; |
| 118 | if (head > len || (!hastable && head != len)) |
| 119 | bb_error_msg_and_die("input file: bad length"); |
| 120 | } else { |
| 121 | /* file with three code pages? */ |
| 122 | if (len == 9780) { |
| 123 | head0 = 40; |
| 124 | unit = 16; |
| 125 | } else { |
| 126 | /* bare font */ |
| 127 | if (len & 0377) |
| 128 | bb_error_msg_and_die("input file: bad length"); |
| 129 | head0 = 0; |
| 130 | unit = len / 256; |
| 131 | } |
| 132 | fontsize = 256; |
| 133 | hastable = 0; |
| 134 | } |
| 135 | |
| 136 | do_loadfont(fd, (unsigned char *)psfhdr + head0, unit, fontsize); |
| 137 | if (hastable) |
| 138 | do_loadtable(fd, (unsigned char *)psfhdr + head, len - head, fontsize); |
| 139 | } |
| 140 | |
| 141 | #if ENABLE_LOADFONT |
| 142 | int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 143 | int loadfont_main(int argc UNUSED_PARAM, char **argv) |
| 144 | { |
| 145 | size_t len; |
| 146 | struct psf_header *psfhdr; |
| 147 | |
| 148 | // no arguments allowed! |
| 149 | opt_complementary = "=0"; |
| 150 | getopt32(argv, ""); |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * We used to look at the length of the input file |
| 154 | * with stat(); now that we accept compressed files, |
| 155 | * just read the entire file. |
| 156 | */ |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 157 | len = 32*1024; // can't be larger |
| 158 | psfhdr = (struct psf_header *) xmalloc_read(STDIN_FILENO, &len); |
| 159 | // xmalloc_open_zipped_read_close(filename, &len); |
| 160 | if (!psfhdr) |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 161 | bb_perror_msg_and_die("error reading input font"); |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 162 | do_load(get_console_fd_or_die(), psfhdr, len); |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 163 | |
| 164 | return EXIT_SUCCESS; |
| 165 | } |
Denis Vlasenko | c8d02aa | 2008-08-17 14:12:26 +0000 | [diff] [blame] | 166 | #endif |
| 167 | |
| 168 | #if ENABLE_SETFONT |
| 169 | int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 170 | int setfont_main(int argc UNUSED_PARAM, char **argv) |
| 171 | { |
| 172 | size_t len; |
| 173 | struct psf_header *psfhdr; |
| 174 | char *mapfilename; |
| 175 | int fd; |
| 176 | |
| 177 | opt_complementary = "=1"; |
| 178 | getopt32(argv, "m:", &mapfilename); |
| 179 | argv += optind; |
| 180 | |
| 181 | // load font |
| 182 | len = 32*1024; // can't be larger |
| 183 | psfhdr = (struct psf_header *) xmalloc_open_zipped_read_close(*argv, &len); |
| 184 | fd = get_console_fd_or_die(); |
| 185 | do_load(fd, psfhdr, len); |
| 186 | |
| 187 | // load the screen map, if any |
| 188 | if (option_mask32 & 1) { // -m |
| 189 | void *map = xmalloc_open_zipped_read_close(mapfilename, &len); |
| 190 | if (len == E_TABSZ || len == 2*E_TABSZ) { |
| 191 | xioctl(fd, (len == 2*E_TABSZ) ? PIO_UNISCRNMAP : PIO_SCRNMAP, map); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return EXIT_SUCCESS; |
| 196 | } |
| 197 | #endif |