aboutsummaryrefslogtreecommitdiffstats
path: root/core/font.c
blob: 508f70595ca1030c8adc79f5ffc3224fca411a60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
 *   Copyright 2013 Intel Corporation
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 *
 * font.c
 *
 * VGA font handling code
 *
 */

#include <syslinux/firmware.h>
#include <syslinux/video.h>
#include <sys/io.h>
#include <stdio.h>
#include <fs.h>

#include "bios.h"
#include "graphics.h"
#include "core.h"

__export uint8_t UserFont = 0;		/* Using a user-specified font */

__export __lowmem char fontbuf[8192];

uint16_t GXPixCols = 1;		/* Graphics mode pixel columns */
uint16_t GXPixRows = 1;		/* Graphics mode pixel rows */

/*
 * loadfont:	Load a .psf font file and install it onto the VGA console
 *		(if we're not on a VGA screen then ignore.)
 */
__export void loadfont(const char *filename)
{
	struct psfheader {
		uint16_t magic;
		uint8_t mode;
		uint8_t height;
	} hdr;
	FILE *f;

	f = fopen(filename, "r");
	if (!f)
		return;

	/* Read header */
	if (_fread(&hdr, sizeof hdr, f) != sizeof hdr)
		goto fail;

	/* Magic number */
	if (hdr.magic != 0x0436)
		goto fail;

	/* File mode: font modes 0-5 supported */
	if (hdr.mode > 5)
		goto fail;

	/* VGA minimum/maximum */
	if (hdr.height < 2 || hdr.height > 32)
		goto fail;

	/* Load the actual font into the font buffer. */
	memset(fontbuf, 0, 256*32);
	if (_fread(fontbuf, 256*hdr.height, f) != 256*hdr.height)
	    goto fail;

	/* Loaded OK */
	VGAFontSize = hdr.height;
	UserFont = 1;		/* Set font flag */
	use_font();

fail:
	fclose(f);
}

/*
 * use_font:
 *	This routine activates whatever font happens to be in the
 *	vgafontbuf, and updates the bios_adjust_screen data.
 *      Must be called with CS = DS
 */
void use_font(void)
{
	com32sys_t ireg, oreg;
	uint8_t bytes = VGAFontSize;

	/* Nonstandard mode? */
	if (UsingVGA & ~0x3)
		syslinux_force_text_mode();

	memset(&ireg, 0, sizeof(ireg));

	ireg.es = SEG(fontbuf);
	ireg.ebp.w[0] = OFFS(fontbuf); /* ES:BP -> font */

	/* Are we using a user-specified font? */
	if (UserFont & 0x1) {
		/* Are we in graphics mode? */
		if (UsingVGA & 0x1) {
			uint8_t rows;

			rows = GXPixRows / bytes;
			VidRows = rows - 1;

			/* Set user character table */
			ireg.eax.w[0] = 0x1121;
			ireg.ebx.b[0] = 0;
			ireg.ecx.b[0] = bytes; /* bytes/character */
			ireg.edx.b[0] = rows;

			__intcall(0x10, &ireg, &oreg);

			/* 8 pixels/character */
			VidCols = ((GXPixCols >> 3) - 1);

			/* No need to call bios_adjust_screen */
			return;
		} else {
			ireg.eax.w[0] = 0x1110;	/* Load into VGA RAM */
			ireg.ebx.b[0] = 0;
			ireg.ebx.b[1] = bytes; /* bytes/character */
			ireg.ecx.w[0] = 256;
			ireg.edx.w[0] = 0;

			__intcall(0x10, &ireg, &oreg);

                        memset(&ireg, 0, sizeof(ireg));
			ireg.ebx.b[0] = 0;
			ireg.eax.w[0] = 0x1103; /* Select page 0 */
			__intcall(0x10, &ireg, NULL);
		}

	}

	bios_adjust_screen();
}

/*
 * bios_adjust_screen: Set the internal variables associated with the screen size.
 *		This is a subroutine in case we're loading a custom font.
 */
void bios_adjust_screen(void)
{
	com32sys_t ireg, oreg;
	volatile uint8_t *vidrows = (volatile uint8_t *)BIOS_vidrows;
	uint8_t rows, cols;

	memset(&ireg, 0, sizeof(ireg));

	rows = *vidrows;
	if (!rows) {
		/*
		 * No vidrows in BIOS, assume 25.
		 * (Remember: vidrows == rows-1)
		 */
		rows = 24;
	}

	VidRows = rows;

	ireg.eax.b[1] = 0x0f;	/* Read video state */
	__intcall(0x10, &ireg, &oreg);
	cols = oreg.eax.b[1];

	VidCols = --cols;	/* Store count-1 (same as rows) */
}

void adjust_screen(void)
{
	if (firmware->adjust_screen)
		firmware->adjust_screen();
}

void pm_adjust_screen(com32sys_t *regs __unused)
{
	adjust_screen();
}

void pm_userfont(com32sys_t *regs)
{
	regs->es = SEG(fontbuf);
	regs->ebx.w[0] = OFFS(fontbuf);
}