2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Michael Smith <msmith@freebsd.org>
5 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/linker.h>
39 #include <dev/fb/fbreg.h>
40 #include <dev/fb/splashreg.h>
41 #include <dev/fb/vgareg.h>
43 #include <isa/isareg.h>
45 #define FADE_TIMEOUT 15 /* sec */
46 #define FADE_LEVELS 10
48 static int splash_mode = -1;
49 static int splash_on = FALSE;
51 static int bmp_start(video_adapter_t *adp);
52 static int bmp_end(video_adapter_t *adp);
53 static int bmp_splash(video_adapter_t *adp, int on);
54 static int bmp_Init(char *data, int swidth, int sheight, int sdepth);
55 static int bmp_Draw(video_adapter_t *adp);
57 static splash_decoder_t bmp_decoder = {
58 "splash_bmp", bmp_start, bmp_end, bmp_splash, SPLASH_IMAGE,
61 SPLASH_DECODER(splash_bmp, bmp_decoder);
64 bmp_start(video_adapter_t *adp)
66 /* currently only 256-color modes are supported XXX */
67 static int modes[] = {
73 * As 320x200 doesn't generally look great,
74 * it's least preferred here.
82 if ((bmp_decoder.data == NULL) || (bmp_decoder.data_size <= 0)) {
83 printf("splash_bmp: No bitmap file found\n");
86 for (i = 0; modes[i] >= 0; ++i) {
87 if ((vidd_get_info(adp, modes[i], &info) == 0) &&
88 (bmp_Init((u_char *)bmp_decoder.data, info.vi_width,
89 info.vi_height, info.vi_depth) == 0))
92 splash_mode = modes[i];
94 printf("splash_bmp: No appropriate video mode found\n");
96 printf("bmp_start(): splash_mode:%d\n", splash_mode);
97 return ((splash_mode < 0) ? ENODEV : 0);
101 bmp_end(video_adapter_t *adp)
108 bmp_splash(video_adapter_t *adp, int on)
110 static u_char pal[256*3];
111 static long time_stamp;
113 static int fading = TRUE, brightness = FADE_LEVELS;
119 /* set up the video mode and draw something */
120 if (vidd_set_mode(adp, splash_mode))
124 vidd_save_palette(adp, pal);
129 * This is a kludge to fade the image away. This section of the
130 * code takes effect only after the system is completely up.
131 * FADE_TIMEOUT should be configurable.
136 time_stamp = tv.tv_sec;
137 if (tv.tv_sec > time_stamp + FADE_TIMEOUT) {
139 if (brightness == 0) {
145 if (brightness == FADE_LEVELS) {
150 for (i = 0; i < sizeof(pal); ++i) {
151 tpal[i] = pal[i] * brightness / FADE_LEVELS;
153 vidd_load_palette(adp, tpal);
154 time_stamp = tv.tv_sec;
159 /* the video mode will be restored by the caller */
166 ** Code to handle Microsoft DIB (".BMP") format images.
168 ** Blame me (msmith@freebsd.org) if this is broken, not Soren.
171 typedef struct tagBITMAPFILEHEADER { /* bmfh */
177 } __packed BITMAPFILEHEADER;
179 typedef struct tagBITMAPINFOHEADER { /* bmih */
191 } __packed BITMAPINFOHEADER;
193 typedef struct tagRGBQUAD { /* rgbq */
200 typedef struct tagBITMAPINFO { /* bmi */
201 BITMAPINFOHEADER bmiHeader;
202 RGBQUAD bmiColors[256];
203 } __packed BITMAPINFO;
205 typedef struct tagBITMAPF
207 BITMAPFILEHEADER bmfh;
216 ** all we actually care about the image
220 int width,height; /* image dimensions */
221 int swidth,sheight; /* screen dimensions for the current mode */
222 u_char depth; /* image depth (1, 4, 8, 24 bits) */
223 u_char sdepth; /* screen depth (1, 4, 8 bpp) */
224 int ncols; /* number of colours */
225 u_char palette[256][3]; /* raw palette data */
226 u_char format; /* one of the BI_* constants above */
227 u_char *data; /* pointer to the raw data */
228 u_char *index; /* running pointer to the data while drawing */
229 u_char *vidmem; /* video memory allocated for drawing */
230 video_adapter_t *adp;
234 static BMP_INFO bmp_info;
239 ** Given (info), set the pixel at (x),(y) to (val)
243 bmp_SetPix(BMP_INFO *info, int x, int y, u_char val)
249 * range check to avoid explosions
251 if ((x < 0) || (x >= info->swidth) || (y < 0) || (y >= info->sheight))
255 * calculate offset into video memory;
256 * because 0,0 is bottom-left for DIB, we have to convert.
258 sofs = ((info->height - (y+1) + (info->sheight - info->height) / 2)
259 * info->adp->va_line_width);
260 x += (info->swidth - info->width) / 2;
262 switch(info->sdepth) {
265 /* EGA/VGA planar modes */
267 newbank = sofs/info->adp->va_window_size;
268 if (info->bank != newbank) {
269 vidd_set_win_org(info->adp, newbank*info->adp->va_window_size);
270 info->bank = newbank;
272 sofs %= info->adp->va_window_size;
273 bofs = x & 0x7; /* offset within byte */
274 outw(GDCIDX, (0x8000 >> bofs) | 0x08); /* bit mask */
275 outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
276 *(info->vidmem + sofs) ^= 0xff; /* read-modify-write */
281 newbank = sofs/info->adp->va_window_size;
282 if (info->bank != newbank) {
283 vidd_set_win_org(info->adp, newbank*info->adp->va_window_size);
284 info->bank = newbank;
286 sofs %= info->adp->va_window_size;
287 *(info->vidmem+sofs) = val;
295 ** Given (data) pointing to a line of RLE4-format data and (line) being the starting
296 ** line onscreen, decode the line.
299 bmp_DecodeRLE4(BMP_INFO *info, int line)
301 int count; /* run count */
303 int x,y; /* screen position */
305 x = 0; /* starting position */
308 /* loop reading data */
311 * encoded mode starts with a run length, and then a byte with
312 * two colour indexes to alternate between for the run
315 for (count = 0; count < *info->index; count++, x++) {
316 if (count & 1) { /* odd count, low nybble */
317 bmp_SetPix(info, x, y, *(info->index+1) & 0x0f);
318 } else { /* even count, high nybble */
319 bmp_SetPix(info, x, y, (*(info->index+1) >>4) & 0x0f);
324 * A leading zero is an escape; it may signal the end of the
325 * bitmap, a cursor move, or some absolute data.
327 } else { /* zero tag may be absolute mode or an escape */
328 switch (*(info->index+1)) {
329 case 0: /* end of line */
332 case 1: /* end of bitmap */
336 x += *(info->index + 2); /* new coords */
337 y += *(info->index + 3);
340 default: /* literal bitmap data */
341 for (count = 0; count < *(info->index + 1); count++, x++) {
342 val = *(info->index + 2 + (count / 2)); /* byte with nybbles */
344 val &= 0xf; /* get low nybble */
346 val = (val >> 4); /* get high nybble */
348 bmp_SetPix(info, x, y, val);
350 /* warning, this depends on integer truncation, do not hand-optimise! */
351 info->index += 2 + ((count + 3) / 4) * 2;
360 ** Given (data) pointing to a line of RLE8-format data and (line) being the starting
361 ** line onscreen, decode the line.
364 bmp_DecodeRLE8(BMP_INFO *info, int line)
366 int count; /* run count */
367 int x,y; /* screen position */
369 x = 0; /* starting position */
372 /* loop reading data */
375 * encoded mode starts with a run length, and then a byte with
376 * two colour indexes to alternate between for the run
379 for (count = 0; count < *info->index; count++, x++)
380 bmp_SetPix(info, x, y, *(info->index+1));
383 * A leading zero is an escape; it may signal the end of the
384 * bitmap, a cursor move, or some absolute data.
386 } else { /* zero tag may be absolute mode or an escape */
387 switch(*(info->index+1)) {
388 case 0: /* end of line */
391 case 1: /* end of bitmap */
395 x += *(info->index + 2); /* new coords */
396 y += *(info->index + 3);
399 default: /* literal bitmap data */
400 for (count = 0; count < *(info->index + 1); count++, x++)
401 bmp_SetPix(info, x, y, *(info->index + 2 + count));
402 /* must be an even count */
403 info->index += 2 + count + (count & 1);
413 ** Given (info) pointing to an image being decoded, (line) being the line currently
414 ** being displayed, decode a line of data.
417 bmp_DecodeLine(BMP_INFO *info, int line)
420 u_char val, mask, *p;
422 switch(info->format) {
424 switch(info->depth) {
426 for (x = 0; x < info->width; x++, info->index++)
427 bmp_SetPix(info, x, line, *info->index);
428 info->index += 3 - (--x % 4);
432 for (x = 0; x < info->width; x++) {
434 val = *p & 0xf; /* get low nybble */
437 val = *p >> 4; /* get high nybble */
439 bmp_SetPix(info, x, line, val);
441 /* warning, this depends on integer truncation, do not hand-optimise! */
442 info->index += ((x + 7) / 8) * 4;
447 for (x = 0; x < info->width; x++) {
448 val = (*p & mask) ? 1 : 0;
454 bmp_SetPix(info, x, line, val);
456 /* warning, this depends on integer truncation, do not hand-optimise! */
457 info->index += ((x + 31) / 32) * 4;
462 bmp_DecodeRLE4(info, line);
465 bmp_DecodeRLE8(info, line);
473 ** Given a pointer (data) to the image of a BMP file, fill in bmp_info with what
474 ** can be learnt from it. Return nonzero if the file isn't usable.
476 ** Take screen dimensions (swidth), (sheight) and (sdepth) and make sure we
477 ** can work with these.
480 bmp_Init(char *data, int swidth, int sheight, int sdepth)
482 BITMAPF *bmf = (BITMAPF *)data;
485 bmp_info.data = NULL; /* assume setup failed */
488 if (bmf->bmfh.bfType != 0x4d42) {
489 printf("splash_bmp: not a BMP file\n");
490 return(1); /* XXX check word ordering for big-endian ports? */
493 /* do we understand this bitmap format? */
494 if (bmf->bmfi.bmiHeader.biSize > sizeof(bmf->bmfi.bmiHeader)) {
495 printf("splash_bmp: unsupported BMP format (size=%d)\n",
496 bmf->bmfi.bmiHeader.biSize);
500 /* save what we know about the screen */
501 bmp_info.swidth = swidth;
502 bmp_info.sheight = sheight;
503 bmp_info.sdepth = sdepth;
505 /* where's the data? */
506 bmp_info.data = (u_char *)data + bmf->bmfh.bfOffBits;
508 /* image parameters */
509 bmp_info.width = bmf->bmfi.bmiHeader.biWidth;
510 bmp_info.height = bmf->bmfi.bmiHeader.biHeight;
511 bmp_info.depth = bmf->bmfi.bmiHeader.biBitCount;
512 bmp_info.format = bmf->bmfi.bmiHeader.biCompression;
514 switch(bmp_info.format) { /* check compression format */
520 printf("splash_bmp: unsupported compression format\n");
521 return(1); /* unsupported compression format */
524 /* palette details */
525 bmp_info.ncols = (bmf->bmfi.bmiHeader.biClrUsed);
526 bzero(bmp_info.palette,sizeof(bmp_info.palette));
527 if (bmp_info.ncols == 0) { /* uses all of them */
528 bmp_info.ncols = 1 << bmf->bmfi.bmiHeader.biBitCount;
530 if ((bmp_info.height > bmp_info.sheight) ||
531 (bmp_info.width > bmp_info.swidth) ||
532 (bmp_info.ncols > (1 << sdepth))) {
534 printf("splash_bmp: beyond screen capacity (%dx%d, %d colors)\n",
535 bmp_info.width, bmp_info.height, bmp_info.ncols);
540 for (pind = 0; pind < bmp_info.ncols; pind++) {
541 bmp_info.palette[pind][0] = bmf->bmfi.bmiColors[pind].rgbRed;
542 bmp_info.palette[pind][1] = bmf->bmfi.bmiColors[pind].rgbGreen;
543 bmp_info.palette[pind][2] = bmf->bmfi.bmiColors[pind].rgbBlue;
551 ** Render the image. Return nonzero if that's not possible.
555 bmp_Draw(video_adapter_t *adp)
562 if (bmp_info.data == NULL) { /* init failed, do nothing */
566 /* clear the screen */
567 bmp_info.vidmem = (u_char *)adp->va_window;
570 vidd_set_win_org(adp, 0);
573 /* initialise the info structure for drawing */
574 bmp_info.index = bmp_info.data;
576 /* set the palette for our image */
577 vidd_load_palette(adp, (u_char *)&bmp_info.palette);
580 /* XXX: this is ugly, but necessary for EGA/VGA 1bpp/4bpp modes */
581 if ((adp->va_type == KD_EGA) || (adp->va_type == KD_VGA)) {
582 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
585 for (i = 0; i < 16; ++i) {
589 inb(adp->va_crtc_addr + 6); /* reset flip-flop */
590 outb(ATC, 0x20); /* enable palette */
592 outw(GDCIDX, 0x0f01); /* set/reset enable */
594 if (bmp_info.sdepth == 1)
595 outw(TSIDX, 0x0102); /* unmask plane #0 */
599 for (line = 0; (line < bmp_info.height) && bmp_info.index; line++) {
600 bmp_DecodeLine(&bmp_info, line);