installer: fix 0 used as NULL
[syslinux/sherbszt.git] / core / graphics.c
blob1efb2faf4ce8af0b6dc76d1a6892538599e220b4
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 ireg.eax.w[0] = 0x0012; /* Set mode = 640x480 VGA 16 colors */
98 __intcall(0x10, &ireg, &oreg);
100 ireg.edx.w[0] = (uint32_t)linear_color;
101 ireg.eax.w[0] = 0x1002; /* Write color registers */
102 __intcall(0x10, &ireg, &oreg);
104 UsingVGA = 1;
106 /* Set GXPixCols and GXPixRows */
107 GXPixCols = 640;
108 GXPixRows = 480;
110 use_font();
111 ScrollAttribute = 0;
113 return 0;
116 static inline char getnybble(void)
118 char data = getc(fd);
120 if (data & 0x10) {
121 data &= 0x0F;
122 return data;
125 data = getc(fd);
126 return (data & 0x0F);
130 * rledecode:
131 * Decode a pixel row in RLE16 format.
133 * 'in': input (RLE16 encoded) buffer
134 * 'out': output (decoded) buffer
135 * 'count': pixel count
137 static void rledecode(uint8_t *out, size_t count)
139 uint8_t prev_pixel = 0;
140 size_t size = count;
141 uint8_t data;
142 int i;
144 again:
145 for (i = 0; i < size; i++) {
147 data = getnybble();
148 if (data == prev_pixel)
149 break;
151 *out++ = data;
152 prev_pixel = data;
155 size -= i;
156 if (!size)
157 return;
159 /* Start of run sequence */
160 data = getnybble();
161 if (data == 0) {
162 /* long run */
163 uint8_t hi;
165 data = getnybble();
166 hi = getnybble();
167 hi <<= 4;
168 data |= hi;
169 data += 16;
172 /* dorun */
173 for (i = 0; i < data; i++)
174 *out++ = prev_pixel;
176 size -= i;
177 if (size)
178 goto again;
182 * packedpixel2vga:
183 * Convert packed-pixel to VGA bitplanes
185 * 'in': packed pixel string (640 pixels)
186 * 'out': output (four planes @ 640/8 = 80 bytes)
187 * 'count': pixel count (multiple of 8)
189 static void packedpixel2vga(const uint8_t *in, uint8_t *out)
191 int i, j, k;
193 for (i = 0; i < 4; i++) {
194 const uint8_t *ip = in;
196 for (j = 0; j < 640/8; j++) {
197 uint8_t ob = 0;
199 for (k = 0; k < 8; k++) {
200 uint8_t px = *ip++;
201 ob = (ob << 1) | ((px >> i) & 1);
204 *out++ = ob;
210 * outputvga:
211 * Output four subsequent lines of VGA data
213 * 'in': four planes @ 640/8=80 bytes
214 * 'out': pointer into VGA memory
216 static void outputvga(const void *in, void *out)
218 int i;
220 /* Select the sequencer mask */
221 outb(VGA_SEQ_IX_MAP_MASK, VGA_SEQ_ADDR);
223 for (i = 1; i <= 8; i <<= 1) {
224 /* Select the bit plane to write */
225 outb(i, VGA_SEQ_DATA);
226 memcpy(out, in, 640/8);
227 in = (const char *)in + 640/8;
232 * Display a graphical splash screen.
234 __export void vgadisplayfile(FILE *_fd)
236 char *p;
237 int size;
239 fd = _fd;
242 * This is a cheap and easy way to make sure the screen is
243 * cleared in case we were in graphics mode aready.
245 syslinux_force_text_mode();
246 vgasetmode();
248 size = 4+2*2+16*3;
249 p = (char *)&LSSHeader;
251 /* Load the header */
252 while (size--)
253 *p = getc(fd);
255 if (*p != EOF) {
256 com32sys_t ireg, oreg;
257 uint16_t rows;
258 int i;
260 /* The header WILL be in the first chunk. */
261 if (LSSMagic != 0x1413f33d)
262 return;
264 memset(&ireg, 0, sizeof(ireg));
266 /* Color map offset */
267 ireg.edx.w[0] = offsetof(lssheader_t, GraphColorMap);
269 ireg.eax.w[0] = 0x1012; /* Set RGB registers */
270 ireg.ebx.w[0] = 0; /* First register number */
271 ireg.ecx.w[0] = 16; /* 16 registers */
272 __intcall(0x10, &ireg, &oreg);
274 /* Number of pixel rows */
275 rows = (GraphYSize + VGAFontSize) - 1;
276 rows = rows / VGAFontSize;
277 if (rows >= VidRows)
278 rows = VidRows - 1;
280 memset(&ireg, 0, sizeof(ireg));
282 ireg.edx.b[1] = rows;
283 ireg.eax.b[1] = 2;
284 ireg.ebx.w[0] = 0;
286 /* Set cursor below image */
287 __intcall(0x10, &ireg, &oreg);
289 rows = GraphYSize; /* Number of graphics rows */
290 VGAPos = 0;
292 for (i = 0; i < rows; i++) {
293 /* Pre-clear the row buffer */
294 memset(VGARowBuffer, 0, 640);
296 /* Decode one row */
297 rledecode(VGARowBuffer, GraphXSize);
299 packedpixel2vga(VGARowBuffer, VGAPlaneBuffer);
300 outputvga(VGAPlaneBuffer, MK_PTR(0xA000, VGAPos));
301 VGAPos += 640/8;
307 * Disable VGA graphics.
309 __export void syslinux_force_text_mode(void)
311 com32sys_t ireg, oreg;
313 /* Already in text mode? */
314 if (!UsingVGA)
315 return;
317 if (UsingVGA & 0x4) {
318 /* VESA return to normal video mode */
319 memset(&ireg, 0, sizeof(ireg));
321 ireg.eax.w[0] = 0x4F02; /* Set SuperVGA video mode */
322 ireg.ebx.w[0] = 0x0003;
323 __intcall(0x10, &ireg, &oreg);
326 /* Return to normal video mode */
327 memset(&ireg, 0, sizeof(ireg));
328 ireg.eax.w[0] = 0x0003;
329 __intcall(0x10, &ireg, &oreg);
331 UsingVGA = 0;
333 ScrollAttribute = 0x7;
334 /* Restore text font/data */
335 use_font();
338 static void vgacursorcommon(char data)
340 if (UsingVGA) {
341 com32sys_t ireg;
343 ireg.eax.b[0] = data;
344 ireg.eax.b[1] = 0x09;
345 ireg.ebx.w[0] = 0x0007;
346 ireg.ecx.w[0] = 1;
347 __intcall(0x10, &ireg, NULL);
351 void vgahidecursor(void)
353 vgacursorcommon(' ');
356 void vgashowcursor(void)
358 vgacursorcommon('_');
361 __export void using_vga(uint8_t vga, uint16_t pix_cols, uint16_t pix_rows)
363 UsingVGA = vga;
364 GXPixCols = pix_cols;
365 GXPixRows = pix_rows;
367 if (!(UsingVGA & 0x08))
368 adjust_screen();
371 void pm_using_vga(com32sys_t *regs)
373 using_vga(regs->eax.b[0], regs->ecx.w[0], regs->edx.w[0]);