2f8d336478536af789d654599f9523d02e0ca693Kay Sievers/*
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * This program is free software; you can redistribute it and/or modify it
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * under the terms of the GNU Lesser General Public License as published by
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * the Free Software Foundation; either version 2.1 of the License, or
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * (at your option) any later version.
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers *
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * This program is distributed in the hope that it will be useful, but
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * WITHOUT ANY WARRANTY; without even the implied warranty of
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * Lesser General Public License for more details.
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers *
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * Copyright (C) 2012-2013 Kay Sievers <kay@vrfy.org>
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers * Copyright (C) 2012 Harald Hoyer <harald@redhat.com>
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers#include <efi.h>
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers#include <efilib.h>
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2bdbf32183fc6816cf34f4850e9fc5f070a34303Marcel Holtmann#include "graphics.h"
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers#include "splash.h"
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include "util.h"
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstruct bmp_file {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers CHAR8 signature[2];
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 size;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT16 reserved[2];
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 offset;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers} __attribute__((packed));
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers/* we require at least BITMAPINFOHEADER, later versions are
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers accepted, but their features ignored */
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstruct bmp_dib {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 size;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 x;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 y;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT16 planes;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT16 depth;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 compression;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 image_size;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers INT32 x_pixel_meter;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers INT32 y_pixel_meter;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 colors_used;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 colors_important;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers} __attribute__((packed));
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstruct bmp_map {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT8 blue;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT8 green;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT8 red;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT8 reserved;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers} __attribute__((packed));
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay SieversEFI_STATUS bmp_parse_header(UINT8 *bmp, UINTN size, struct bmp_dib **ret_dib,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers struct bmp_map **ret_map, UINT8 **pixmap) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers struct bmp_file *file;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers struct bmp_dib *dib;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers struct bmp_map *map;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINTN row_size;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (size < sizeof(struct bmp_file) + sizeof(struct bmp_dib))
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_INVALID_PARAMETER;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers /* check file header */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers file = (struct bmp_file *)bmp;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (file->signature[0] != 'B' || file->signature[1] != 'M')
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_INVALID_PARAMETER;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (file->size != size)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_INVALID_PARAMETER;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (file->size < file->offset)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_INVALID_PARAMETER;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers /* check device-independent bitmap */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers dib = (struct bmp_dib *)(bmp + sizeof(struct bmp_file));
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (dib->size < sizeof(struct bmp_dib))
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_UNSUPPORTED;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers switch (dib->depth) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 1:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 4:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 8:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 24:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (dib->compression != 0)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_UNSUPPORTED;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers break;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 16:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 32:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (dib->compression != 0 && dib->compression != 3)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_UNSUPPORTED;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers break;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers default:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_UNSUPPORTED;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
7c5925a448551129ec2e11157c847703def79608Zbigniew Jędrzejewski-Szmek row_size = ((UINTN) dib->depth * dib->x + 31) / 32 * 4;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (file->size - file->offset < dib->y * row_size)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_INVALID_PARAMETER;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (row_size * dib->y > 64 * 1024 * 1024)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_INVALID_PARAMETER;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers /* check color table */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers map = (struct bmp_map *)(bmp + sizeof(struct bmp_file) + dib->size);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (file->offset < sizeof(struct bmp_file) + dib->size)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_INVALID_PARAMETER;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (file->offset > sizeof(struct bmp_file) + dib->size) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 map_count;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINTN map_size;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (dib->colors_used)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers map_count = dib->colors_used;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers else {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers switch (dib->depth) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 1:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 4:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 8:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers map_count = 1 << dib->depth;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers break;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers default:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers map_count = 0;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers break;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers map_size = file->offset - (sizeof(struct bmp_file) + dib->size);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (map_size != sizeof(struct bmp_map) * map_count)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_INVALID_PARAMETER;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers *ret_map = map;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers *ret_dib = dib;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers *pixmap = bmp + file->offset;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_SUCCESS;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers}
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sieversstatic VOID pixel_blend(UINT32 *dst, const UINT32 source) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 alpha, src, src_rb, src_g, dst_rb, dst_g, rb, g;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers alpha = (source & 0xff);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers /* convert src from RGBA to XRGB */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers src = source >> 8;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers /* decompose into RB and G components */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers src_rb = (src & 0xff00ff);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers src_g = (src & 0x00ff00);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers dst_rb = (*dst & 0xff00ff);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers dst_g = (*dst & 0x00ff00);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers /* blend */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers rb = ((((src_rb - dst_rb) * alpha + 0x800080) >> 8) + dst_rb) & 0xff00ff;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers g = ((((src_g - dst_g) * alpha + 0x008000) >> 8) + dst_g) & 0x00ff00;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers *dst = (rb | g);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers}
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay SieversEFI_STATUS bmp_to_blt(EFI_GRAPHICS_OUTPUT_BLT_PIXEL *buf,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers struct bmp_dib *dib, struct bmp_map *map,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT8 *pixmap) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT8 *in;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINTN y;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers /* transform and copy pixels */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers in = pixmap;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers for (y = 0; y < dib->y; y++) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers EFI_GRAPHICS_OUTPUT_BLT_PIXEL *out;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINTN row_size;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINTN x;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out = &buf[(dib->y - y - 1) * dib->x];
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers for (x = 0; x < dib->x; x++, in++, out++) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers switch (dib->depth) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 1: {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINTN i;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers for (i = 0; i < 8 && x < dib->x; i++) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Red = map[((*in) >> (7 - i)) & 1].red;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Green = map[((*in) >> (7 - i)) & 1].green;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Blue = map[((*in) >> (7 - i)) & 1].blue;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out++;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers x++;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out--;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers x--;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers break;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 4: {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINTN i;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers i = (*in) >> 4;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Red = map[i].red;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Green = map[i].green;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Blue = map[i].blue;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (x < (dib->x - 1)) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out++;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers x++;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers i = (*in) & 0x0f;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Red = map[i].red;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Green = map[i].green;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Blue = map[i].blue;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers break;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 8:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Red = map[*in].red;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Green = map[*in].green;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Blue = map[*in].blue;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers break;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 16: {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT16 i = *(UINT16 *) in;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Red = (i & 0x7c00) >> 7;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Green = (i & 0x3e0) >> 2;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Blue = (i & 0x1f) << 3;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers in += 1;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers break;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 24:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Red = in[2];
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Green = in[1];
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers out->Blue = in[0];
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers in += 2;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers break;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers case 32: {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT32 i = *(UINT32 *) in;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers pixel_blend((UINT32 *)out, i);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers in += 3;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers break;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers /* add row padding; new lines always start at 32 bit boundary */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers row_size = in - pixmap;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers in += ((row_size + 3) & ~3) - row_size;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_SUCCESS;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers}
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay SieversEFI_STATUS graphics_splash(UINT8 *content, UINTN len, const EFI_GRAPHICS_OUTPUT_BLT_PIXEL *background) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers EFI_GRAPHICS_OUTPUT_BLT_PIXEL pixel = {};
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers struct bmp_dib *dib;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers struct bmp_map *map;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT8 *pixmap;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINT64 blt_size;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers VOID *blt = NULL;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINTN x_pos = 0;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers UINTN y_pos = 0;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers EFI_STATUS err;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (!background) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (StriCmp(L"Apple", ST->FirmwareVendor) == 0) {
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers pixel.Red = 0xc0;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers pixel.Green = 0xc0;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers pixel.Blue = 0xc0;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers background = &pixel;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers }
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers err = LibLocateProtocol(&GraphicsOutputProtocolGuid, (VOID **)&GraphicsOutput);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (EFI_ERROR(err))
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return err;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers err = bmp_parse_header(content, len, &dib, &map, &pixmap);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (EFI_ERROR(err))
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers goto err;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if(dib->x < GraphicsOutput->Mode->Info->HorizontalResolution)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers x_pos = (GraphicsOutput->Mode->Info->HorizontalResolution - dib->x) / 2;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if(dib->y < GraphicsOutput->Mode->Info->VerticalResolution)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers y_pos = (GraphicsOutput->Mode->Info->VerticalResolution - dib->y) / 2;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)background,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers EfiBltVideoFill, 0, 0, 0, 0,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers GraphicsOutput->Mode->Info->HorizontalResolution,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers GraphicsOutput->Mode->Info->VerticalResolution, 0);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers /* EFI buffer */
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers blt_size = dib->x * dib->y * sizeof(EFI_GRAPHICS_OUTPUT_BLT_PIXEL);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers blt = AllocatePool(blt_size);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (!blt)
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return EFI_OUT_OF_RESOURCES;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers err = uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers blt, EfiBltVideoToBltBuffer, x_pos, y_pos, 0, 0,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers dib->x, dib->y, 0);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (EFI_ERROR(err))
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers goto err;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers err = bmp_to_blt(blt, dib, map, pixmap);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (EFI_ERROR(err))
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers goto err;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers err = graphics_mode(TRUE);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers if (EFI_ERROR(err))
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers goto err;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers err = uefi_call_wrapper(GraphicsOutput->Blt, 10, GraphicsOutput,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers blt, EfiBltBufferToVideo, 0, 0, x_pos, y_pos,
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers dib->x, dib->y, 0);
2f8d336478536af789d654599f9523d02e0ca693Kay Sieverserr:
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers FreePool(blt);
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers return err;
2f8d336478536af789d654599f9523d02e0ca693Kay Sievers}