blob: 0512770668d8eda121efa0ee7fd4f70cc42d42eb [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +02002
3#include <cbfs.h>
4#include <vbe.h>
5#include <console/console.h>
6#include <endian.h>
7#include <bootsplash.h>
Patrick Rudolph5ebe4312019-09-26 08:47:40 +02008#include <stdlib.h>
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +02009
10#include "jpeg.h"
11
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +020012
13void set_bootsplash(unsigned char *framebuffer, unsigned int x_resolution,
Nico Huber99eee162023-07-14 00:09:00 +020014 unsigned int y_resolution, unsigned int bytes_per_line,
15 unsigned int fb_resolution)
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +020016{
Patrick Georgi1d029b42023-10-06 20:19:15 +020017 if ((x_resolution > INT_MAX) || (y_resolution) > INT_MAX) {
18 printk(BIOS_ERR, "Display resolution way too large.\n");
19 return;
20 }
21 int xres = x_resolution, yres = y_resolution;
Johanna Schander7fc006f2019-07-28 08:48:36 +020022 printk(BIOS_INFO, "Setting up bootsplash in %dx%d@%d\n", x_resolution, y_resolution,
23 fb_resolution);
Patrick Georgi1d029b42023-10-06 20:19:15 +020024 size_t filesize;
25 unsigned char *jpeg = cbfs_map("bootsplash.jpg", &filesize);
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +020026 if (!jpeg) {
27 printk(BIOS_ERR, "Could not find bootsplash.jpg\n");
28 return;
29 }
30
Patrick Georgi1d029b42023-10-06 20:19:15 +020031 unsigned int image_width, image_height;
32 if (jpeg_fetch_size(jpeg, filesize, &image_width, &image_height) != 0) {
33 printk(BIOS_ERR, "Could not parse bootsplash.jpg\n");
34 return;
35 }
Johanna Schander7fc006f2019-07-28 08:48:36 +020036
37 printk(BIOS_DEBUG, "Bootsplash image resolution: %dx%d\n", image_width, image_height);
38
Patrick Georgi1d029b42023-10-06 20:19:15 +020039 if (image_width > xres || image_height > yres) {
Nico Huber99eee162023-07-14 00:09:00 +020040 printk(BIOS_NOTICE, "Bootsplash image can't fit framebuffer.\n");
41 cbfs_unmap(jpeg);
42 return;
43 }
44
45 /* center image: */
Patrick Georgi1d029b42023-10-06 20:19:15 +020046 framebuffer += (yres - image_height) / 2 * bytes_per_line
47 + (xres - image_width) / 2 * (fb_resolution / 8);
Nico Huber99eee162023-07-14 00:09:00 +020048
Patrick Georgi1d029b42023-10-06 20:19:15 +020049 int ret = jpeg_decode(jpeg, filesize, framebuffer, image_width, image_height,
50 bytes_per_line, fb_resolution);
Julius Werner834b3ec2020-03-04 16:52:08 -080051 cbfs_unmap(jpeg);
Johanna Schanderdb7a3ae2019-07-24 10:14:26 +020052 if (ret != 0) {
53 printk(BIOS_ERR, "Bootsplash could not be decoded. jpeg_decode returned %d.\n",
54 ret);
55 return;
56 }
57 printk(BIOS_INFO, "Bootsplash loaded\n");
58}