-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcons.c
More file actions
208 lines (171 loc) · 4.99 KB
/
cons.c
File metadata and controls
208 lines (171 loc) · 4.99 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (C) 2025 Sylas Hollander.
* PURPOSE: General purpose video functions for linear framebuffers
* SPDX-License-Identifier: MIT
*/
#include <boot2target.h>
#include <cons.h>
#include <font.h>
#include <tinyprintf.h>
EFI_GUID gEfiGraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
void *memcpy(void *, const void *, size_t);
void *memset(void *dst, int c, size_t n);
LINEAR_FB fb;
static CONS_PRIV con;
static
void VideoPrintChar(char c, UINT32 x, UINT32 y, UINT32 fg_color, UINT32 bg_color)
{
UINT32 char_position = c * ISO_CHAR_HEIGHT;
// Set up pixel location
UINT32 *pixel = (UINT32 *) ((char *) fb.base + (y * ISO_CHAR_HEIGHT) * fb.pitch + (x * ISO_CHAR_WIDTH * 4));
// Print character to screen
for (UINT8 line = 0; line < ISO_CHAR_HEIGHT; line++)
{
UINT8 char_line = iso_font[char_position];
for (UINT8 column = 0; column < ISO_CHAR_WIDTH; column++)
{
UINT8 mask = char_line >> column;
pixel[column] = ((mask & 1) ? fg_color : bg_color);
}
char_position++;
pixel = (UINT32 *) ((char *) pixel + fb.pitch);
}
}
static
void ConsPrintChar(void *p, char c)
{
// CASE 1: Video not enabled yet.
// Return silently on this case.
if (!fb.enabled)
return;
// CASE 2: Text wrapped around.
// Add a newline in this case.
if (con.cursor_x >= con.width)
{
con.cursor_x = 0;
con.cursor_y++;
}
// CASE 3: Screen is full.
// Start over.
if (con.cursor_y >= con.height)
{
ConsClearScreen(con.bg_color);
}
// Now we print the character.
switch (c)
{
// CASE 1: newline.
case '\r': // CR
case '\n': // LF
{
con.cursor_x = 0;
con.cursor_y++;
break;
}
// CASE 2: backspace.
case '\b':
{
con.cursor_x--;
VideoPrintChar(c, con.cursor_x, con.cursor_y, con.fg_color, con.bg_color);
break;
}
// CASE 3: any other character.
default:
{
VideoPrintChar(c, con.cursor_x, con.cursor_y, con.fg_color, con.bg_color);
con.cursor_x++;
break;
}
}
}
BOOLEAN ConsClearScreen(UINT32 color)
{
if (!fb.enabled)
return FALSE;
UINT64 Color64 = (UINT64) color << 32 | color;
for (UINT32 line = 0; line < fb.height; line++)
{
UINT64 *screen = (UINT64 *) ((char *) fb.base + line * fb.pitch);
for (UINT32 column = 0; column < fb.width / 2; column++)
{
*screen++ = Color64;
}
}
con.cursor_x = 0;
con.cursor_y = 0;
return TRUE;
}
BOOLEAN ConsChangeFgColor(UINT32 fg_color)
{
if (!fb.enabled)
return FALSE;
con.fg_color = fg_color;
return TRUE;
}
BOOLEAN ConsChangeBgColor(UINT32 bg_color)
{
if (!fb.enabled)
return FALSE;
con.bg_color = bg_color;
return TRUE;
}
// Platform specific video initialization code.
BOOLEAN
InitializeConsole(VOID)
{
EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop;
EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *GopInfo;
EFI_STATUS Status;
UINTN SizeOfInfo;
UINT32 BackgroundColor;
Status = gBS->LocateProtocol(
&gEfiGraphicsOutputProtocolGuid,
NULL,
(VOID **) &Gop
);
if (EFI_ERROR(Status))
{
return FALSE;
}
Status = Gop->QueryMode(
Gop,
Gop->Mode == NULL ? 0 : Gop->Mode->Mode,
&SizeOfInfo,
&GopInfo);
if (EFI_ERROR(Status))
{
return FALSE;
}
// Assume first pixel's color is the background color.
BackgroundColor = ((UINT32 *) Gop->Mode->FrameBufferBase)[0];
memset(&fb, 0, sizeof(fb));
fb.base = Gop->Mode->FrameBufferBase;
fb.size = Gop->Mode->FrameBufferSize;
fb.pitch = GopInfo->PixelsPerScanLine * 4;
fb.width = GopInfo->HorizontalResolution;
fb.height = GopInfo->VerticalResolution;
fb.depth = 32;
fb.pixels_per_row = GopInfo->PixelsPerScanLine;
fb.red_size = 8;
fb.red_shift = 16;
fb.green_size = 8;
fb.green_shift = 8;
fb.blue_size = 8;
fb.blue_shift = 0;
fb.reserved_size = 8;
fb.reserved_shift = 0;
// set up text console
memset(&con, 0, sizeof(con));
con.cursor_x = 0;
con.cursor_y = 0;
con.width = fb.width / ISO_CHAR_WIDTH;
con.height = fb.height / ISO_CHAR_HEIGHT;
con.size = con.width * con.height;
// change FG/BG color according to the background color
con.fg_color = (BackgroundColor != COLOR_BLACK) ? COLOR_BLACK : COLOR_WHITE;
con.bg_color = BackgroundColor;
init_printf(NULL, ConsPrintChar);
fb.enabled = TRUE;
ConsClearScreen(BackgroundColor);
return TRUE;
}