Skip to content

Commit

Permalink
lib/jpeg: Replace decoder with Wuffs' implementation
Browse files Browse the repository at this point in the history
To quote its repo[0]: Wuffs is a memory-safe programming language (and
a standard library written in that language) for Wrangling Untrusted
File Formats Safely. Wrangling includes parsing, decoding and encoding.

It compiles its library, written in its own language, to a C/C++ source
file that can then be used independently without needing support for
the language. That library is now imported to src/vendorcode/wuffs/.

This change modifies our linters to ignore that directory because
it's supposed to contain the wuffs compiler's result verbatim.

Nigel Tao provided an initial wrapper around wuffs' jpeg decoder
that implements our JPEG API. I further changed it a bit regarding
data placement, dropped stuff from our API that wasn't ever used,
or isn't used anymore, and generally made it fit coreboot a bit
better. Features are Nigel's, bugs are mine.

This commit also adapts our jpeg fuzz test to work with the modified
API. After limiting it to deal only with approximately screen sized
inputs, it fuzzed for 25 hours CPU time without a single hang or
crash. This is a notable improvement over running the test with our
old decoder which crashes within a minute.

Finally, I tried the new parser with a pretty-much-random JPEG file
I got from the internet, and it just showed it (once the resolution
matched), which is also a notable improvement over the old decoder
which is very particular about the subset of JPEG it supports.

In terms of code size, a QEmu build's ramstage increases
from 128060 bytes decompressed (64121 bytes after LZMA)
  to 172304 bytes decompressed (82734 bytes after LZMA).

[0] https://github.com/google/wuffs

Change-Id: If8fa7da69da1ad412f27c2c5e882393c7739bc82
Signed-off-by: Patrick Georgi <[email protected]>
Based-on-work-by: Nigel Tao <[email protected]>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/78271
Reviewed-by: Paul Menzel <[email protected]>
Reviewed-by: Martin L Roth <[email protected]>
Tested-by: build bot (Jenkins) <[email protected]>
  • Loading branch information
Patrick Georgi authored and felixheld committed Dec 13, 2023
1 parent 5466261 commit 1d029b4
Show file tree
Hide file tree
Showing 7 changed files with 64,311 additions and 1,008 deletions.
28 changes: 17 additions & 11 deletions src/lib/bootsplash.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,40 @@ void set_bootsplash(unsigned char *framebuffer, unsigned int x_resolution,
unsigned int y_resolution, unsigned int bytes_per_line,
unsigned int fb_resolution)
{
if ((x_resolution > INT_MAX) || (y_resolution) > INT_MAX) {
printk(BIOS_ERR, "Display resolution way too large.\n");
return;
}
int xres = x_resolution, yres = y_resolution;
printk(BIOS_INFO, "Setting up bootsplash in %dx%d@%d\n", x_resolution, y_resolution,
fb_resolution);
struct jpeg_decdata *decdata;
unsigned char *jpeg = cbfs_map("bootsplash.jpg", NULL);
size_t filesize;
unsigned char *jpeg = cbfs_map("bootsplash.jpg", &filesize);
if (!jpeg) {
printk(BIOS_ERR, "Could not find bootsplash.jpg\n");
return;
}

int image_width, image_height;
jpeg_fetch_size(jpeg, &image_width, &image_height);
unsigned int image_width, image_height;
if (jpeg_fetch_size(jpeg, filesize, &image_width, &image_height) != 0) {
printk(BIOS_ERR, "Could not parse bootsplash.jpg\n");
return;
}

printk(BIOS_DEBUG, "Bootsplash image resolution: %dx%d\n", image_width, image_height);

if (image_width > x_resolution || image_height > y_resolution) {
if (image_width > xres || image_height > yres) {
printk(BIOS_NOTICE, "Bootsplash image can't fit framebuffer.\n");
cbfs_unmap(jpeg);
return;
}

/* center image: */
framebuffer += (y_resolution - image_height) / 2 * bytes_per_line +
(x_resolution - image_width) / 2 * (fb_resolution / 8);
framebuffer += (yres - image_height) / 2 * bytes_per_line
+ (xres - image_width) / 2 * (fb_resolution / 8);

decdata = malloc(sizeof(*decdata));
int ret = jpeg_decode(jpeg, framebuffer, image_width, image_height,
bytes_per_line, fb_resolution, decdata);
free(decdata);
int ret = jpeg_decode(jpeg, filesize, framebuffer, image_width, image_height,
bytes_per_line, fb_resolution);
cbfs_unmap(jpeg);
if (ret != 0) {
printk(BIOS_ERR, "Bootsplash could not be decoded. jpeg_decode returned %d.\n",
Expand Down
Loading

0 comments on commit 1d029b4

Please sign in to comment.