core, pxe: Don't push on one stack and pop from the other in pxenv
[syslinux.git] / core / graphics.c
blob471847f5330befe0d9e67267c3c479ad62186cd9
1 /*
2 * -----------------------------------------------------------------------
4 * Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * -----------------------------------------------------------------------
14 * -----------------------------------------------------------------------
15 * VGA splash screen code
16 * -----------------------------------------------------------------------
19 #include <stddef.h>
20 #include "core.h"
21 #include <sys/io.h>
22 #include <hw/vga.h>
23 #include "fs.h"
25 #include "bios.h"
26 #include "graphics.h"
27 #include <syslinux/video.h>
29 __export uint8_t UsingVGA = 0;
30 uint16_t VGAPos; /* Pointer into VGA memory */
31 __export uint16_t *VGAFilePtr; /* Pointer into VGAFileBuf */
32 __export uint16_t VGAFontSize = 16; /* Defaults to 16 byte font */
34 __export char VGAFileBuf[VGA_FILE_BUF_SIZE]; /* Unmangled VGA image name */
35 __export char VGAFileMBuf[FILENAME_MAX]; /* Mangled VGA image name */
37 static uint8_t VGARowBuffer[640 + 80]; /* Decompression buffer */
38 static uint8_t VGAPlaneBuffer[(640/8) * 4]; /* Plane buffers */
40 extern uint16_t GXPixCols;
41 extern uint16_t GXPixRows;
43 /* Maps colors to consecutive DAC registers */
44 static uint8_t linear_color[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
45 9, 10, 11, 12, 13, 14, 15, 0 };
47 static FILE *fd;
49 typedef struct {
50 uint32_t LSSMagic; /* Magic number */
51 uint16_t GraphXSize; /* Width of splash screen file */
52 uint16_t GraphYSize; /* Height of splash screen file */
53 uint8_t GraphColorMap[3*16];
54 } lssheader_t;
56 static lssheader_t LSSHeader;
58 #define LSSMagic LSSHeader.LSSMagic
59 #define GraphXSize LSSHeader.GraphXSize
60 #define GraphYSize LSSHeader.GraphYSize
63 * Enable VGA graphics, if possible. Return 0 on success.
65 static int vgasetmode(void)
67 com32sys_t ireg, oreg;
69 if (UsingVGA)
70 return 0; /* Nothing to do... */
72 memset(&ireg, 0, sizeof(ireg));
73 memset(&oreg, 0, sizeof(oreg));
75 if (UsingVGA & 0x4) {
77 * We're in VESA mode, which means VGA; use VESA call
78 * to revert the mode, and then call the conventional
79 * mode-setting for good measure...
81 ireg.eax.w[0] = 0x4F02;
82 ireg.ebx.w[0] = 0x0012;
83 __intcall(0x10, &ireg, &oreg);
84 } else {
85 /* Get video card and monitor */
86 ireg.eax.w[0] = 0x1A00;
87 __intcall(0x10, &ireg, &oreg);
88 oreg.ebx.b[0] -= 7; /* BL=07h and BL=08h OK */
90 if (oreg.ebx.b[0] > 1)
91 return -1;
95 * Set mode.
97 memset(&ireg, 0, sizeof(ireg));
98 ireg.eax.w[0] = 0x0012; /* Set mode = 640x480 VGA 16 colors */
99 __intcall(0x10, &ireg, &oreg);
101 memset(&ireg, 0, sizeof(ireg));
102 ireg.edx.w[0] = (uint32_t)linear_color;
103 ireg.eax.w[0] = 0x1002; /* Write color registers */
104 __intcall(0x10, &ireg, &oreg);
106 UsingVGA = 1;
108 /* Set GXPixCols and GXPixRows */
109 GXPixCols = 640;
110 GXPixRows = 480;
112 use_font();
113 ScrollAttribute = 0;
115 return 0;
118 static inline char getnybble(void)
120 char data = getc(fd);
122 if (data & 0x10) {
123 data &= 0x0F;
124 return data;
127 data = getc(fd);
128 return (data & 0x0F);
132 * rledecode:
133 * Decode a pixel row in RLE16 format.
135 * 'in': input (RLE16 encoded) buffer
136 * 'out': output (decoded) buffer
137 * 'count': pixel count
139 static void rledecode(uint8_t *out, size_t count)
141 uint8_t prev_pixel = 0;
142 size_t size = count;
143 uint8_t data;
144 int i;
146 again:
147 for (i = 0; i < size; i++) {
149 data = getnybble();
150 if (data == prev_pixel)
151 break;
153 *out++ = data;
154 prev_pixel = data;
157 size -= i;
158 if (!size)
159 return;
161 /* Start of run sequence */
162 data = getnybble();
163 if (data == 0) {
164 /* long run */
165 uint8_t hi;
167 data = getnybble();
168 hi = getnybble();
169 hi <<= 4;
170 data |= hi;
171 data += 16;
174 /* dorun */
175 for (i = 0; i < data; i++)
176 *out++ = prev_pixel;
178 size -= i;
179 if (size)
180 goto again;
184 * packedpixel2vga:
185 * Convert packed-pixel to VGA bitplanes
187 * 'in': packed pixel string (640 pixels)
188 * 'out': output (four planes @ 640/8 = 80 bytes)
189 * 'count': pixel count (multiple of 8)
191 static void packedpixel2vga(const uint8_t *in, uint8_t *out)
193 int i, j, k;
195 for (i = 0; i < 4; i++) {
196 const uint8_t *ip = in;
198 for (j = 0; j < 640/8; j++) {
199 uint8_t ob = 0;
201 for (k = 0; k < 8; k++) {
202 uint8_t px = *ip++;
203 ob = (ob << 1) | ((px >> i) & 1);
206 *out++ = ob;
212 * outputvga:
213 * Output four subsequent lines of VGA data
215 * 'in': four planes @ 640/8=80 bytes
216 * 'out': pointer into VGA memory
218 static void outputvga(const void *in, void *out)
220 int i;
222 /* Select the sequencer mask */
223 outb(VGA_SEQ_IX_MAP_MASK, VGA_SEQ_ADDR);
225 for (i = 1; i <= 8; i <<= 1) {
226 /* Select the bit plane to write */
227 outb(i, VGA_SEQ_DATA);
228 memcpy(out, in, 640/8);
229 in = (const char *)in + 640/8;
234 * Display a graphical splash screen.
236 __export void vgadisplayfile(FILE *_fd)
238 char *p;
239 int size;
241 fd = _fd;
244 * This is a cheap and easy way to make sure the screen is
245 * cleared in case we were in graphics mode aready.
247 syslinux_force_text_mode();
248 vgasetmode();
250 size = 4+2*2+16*3;
251 p = (char *)&LSSHeader;
253 /* Load the header */
254 while (size--)
255 *p = getc(fd);
257 if (*p != EOF) {
258 com32sys_t ireg, oreg;
259 uint16_t rows;
260 int i;
262 /* The header WILL be in the first chunk. */
263 if (LSSMagic != 0x1413f33d)
264 return;
266 memset(&ireg, 0, sizeof(ireg));
268 /* Color map offset */
269 ireg.edx.w[0] = offsetof(lssheader_t, GraphColorMap);
271 ireg.eax.w[0] = 0x1012; /* Set RGB registers */
272 ireg.ebx.w[0] = 0; /* First register number */
273 ireg.ecx.w[0] = 16; /* 16 registers */
274 __intcall(0x10, &ireg, &oreg);
276 /* Number of pixel rows */
277 rows = (GraphYSize + VGAFontSize) - 1;
278 rows = rows / VGAFontSize;
279 if (rows >= VidRows)
280 rows = VidRows - 1;
282 memset(&ireg, 0, sizeof(ireg));
284 ireg.edx.b[1] = rows;
285 ireg.eax.b[1] = 2;
286 ireg.ebx.w[0] = 0;
288 /* Set cursor below image */
289 __intcall(0x10, &ireg, &oreg);
291 rows = GraphYSize; /* Number of graphics rows */
292 VGAPos = 0;
294 for (i = 0; i < rows; i++) {
295 /* Pre-clear the row buffer */
296 memset(VGARowBuffer, 0, 640);
298 /* Decode one row */
299 rledecode(VGARowBuffer, GraphXSize);
301 packedpixel2vga(VGARowBuffer, VGAPlaneBuffer);
302 outputvga(VGAPlaneBuffer, MK_PTR(0xA000, VGAPos));
303 VGAPos += 640/8;
309 * Disable VGA graphics.
311 __export void syslinux_force_text_mode(void)
313 com32sys_t ireg, oreg;
315 /* Already in text mode? */
316 if (!UsingVGA)
317 return;
319 if (UsingVGA & 0x4) {
320 /* VESA return to normal video mode */
321 memset(&ireg, 0, sizeof(ireg));
323 ireg.eax.w[0] = 0x4F02; /* Set SuperVGA video mode */
324 ireg.ebx.w[0] = 0x0003;
325 __intcall(0x10, &ireg, &oreg);
328 /* Return to normal video mode */
329 memset(&ireg, 0, sizeof(ireg));
330 ireg.eax.w[0] = 0x0003;
331 __intcall(0x10, &ireg, &oreg);
333 UsingVGA = 0;
335 ScrollAttribute = 0x7;
336 /* Restore text font/data */
337 use_font();
340 static void vgacursorcommon(char data)
342 if (UsingVGA) {
343 com32sys_t ireg;
344 memset(&ireg, 0, sizeof(ireg));
346 ireg.eax.b[0] = data;
347 ireg.eax.b[1] = 0x09;
348 ireg.ebx.w[0] = 0x0007;
349 ireg.ecx.w[0] = 1;
350 __intcall(0x10, &ireg, NULL);
354 void vgahidecursor(void)
356 vgacursorcommon(' ');
359 void vgashowcursor(void)
361 vgacursorcommon('_');
364 __export void using_vga(uint8_t vga, uint16_t pix_cols, uint16_t pix_rows)
366 UsingVGA = vga;
367 GXPixCols = pix_cols;
368 GXPixRows = pix_rows;
370 if (!(UsingVGA & 0x08))
371 adjust_screen();
374 void pm_using_vga(com32sys_t *regs)
376 using_vga(regs->eax.b[0], regs->ecx.w[0], regs->edx.w[0]);