blob: 071ba90b4354e57424d2b80c4d75025630755a52 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkf5f146e2003-04-20 16:52:09 +00002/*
3 * (C) Copyright 2002
wdenkb9bbd242003-06-30 16:24:52 +00004 * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
wdenkf5f146e2003-04-20 16:52:09 +00005 */
6
7/*
8 * BMP handling routines
9 */
10
11#include <common.h>
12#include <bmp_layout.h>
13#include <command.h>
Simon Glass1a974af2019-08-01 09:46:36 -060014#include <dm.h>
15#include <gzip.h>
Simon Glass85f13782019-12-28 10:45:03 -070016#include <image.h>
Simon Glass1a974af2019-08-01 09:46:36 -060017#include <lcd.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Stefan Roese13b93692005-10-08 10:19:07 +020019#include <malloc.h>
Simon Glasscd161d82016-01-05 09:30:58 -070020#include <mapmem.h>
Anatolij Gustschinb9b5eaf2013-07-02 00:04:05 +020021#include <splash.h>
Stefan Reinauer987d1d82012-09-28 15:11:11 +000022#include <video.h>
Simon Glass1a974af2019-08-01 09:46:36 -060023#include <asm/byteorder.h>
wdenkf5f146e2003-04-20 16:52:09 +000024
wdenkf5f146e2003-04-20 16:52:09 +000025static int bmp_info (ulong addr);
wdenkf5f146e2003-04-20 16:52:09 +000026
27/*
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010028 * Allocate and decompress a BMP image using gunzip().
29 *
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020030 * Returns a pointer to the decompressed image data. This pointer is
31 * aligned to 32-bit-aligned-address + 2.
32 * See doc/README.displaying-bmps for explanation.
33 *
34 * The allocation address is passed to 'alloc_addr' and must be freed
35 * by the caller after use.
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010036 *
37 * Returns NULL if decompression failed, or if the decompressed data
38 * didn't contain a valid BMP signature.
39 */
40#ifdef CONFIG_VIDEO_BMP_GZIP
Simon Glass92e1f852015-05-13 07:02:27 -060041struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
42 void **alloc_addr)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010043{
44 void *dst;
45 unsigned long len;
Simon Glass92e1f852015-05-13 07:02:27 -060046 struct bmp_image *bmp;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010047
48 /*
49 * Decompress bmp image
50 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020051 len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020052 /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
53 dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010054 if (dst == NULL) {
55 puts("Error: malloc in gunzip failed!\n");
56 return NULL;
57 }
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020058
59 bmp = dst;
60
61 /* align to 32-bit-aligned-address + 2 */
Adam Heinrichfedd94c2018-12-31 12:11:08 +010062 bmp = (struct bmp_image *)((((uintptr_t)dst + 1) & ~3) + 2);
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020063
Simon Glasscd161d82016-01-05 09:30:58 -070064 if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
65 &len) != 0) {
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010066 free(dst);
67 return NULL;
68 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020069 if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010070 puts("Image could be truncated"
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020071 " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010072
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010073 /*
74 * Check for bmp mark 'BM'
75 */
76 if (!((bmp->header.signature[0] == 'B') &&
77 (bmp->header.signature[1] == 'M'))) {
78 free(dst);
79 return NULL;
80 }
81
Wolfgang Denkaf42bb32011-02-09 15:11:10 +010082 debug("Gzipped BMP image detected!\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010083
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020084 *alloc_addr = dst;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010085 return bmp;
86}
87#else
Simon Glass92e1f852015-05-13 07:02:27 -060088struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
89 void **alloc_addr)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010090{
91 return NULL;
92}
93#endif
94
Simon Glassed38aef2020-05-10 11:40:03 -060095static int do_bmp_info(struct cmd_tbl *cmdtp, int flag, int argc,
96 char *const argv[])
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +010097{
98 ulong addr;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010099
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100100 switch (argc) {
Simon Glass892265d2019-12-28 10:45:02 -0700101 case 1: /* use image_load_addr as default address */
102 addr = image_load_addr;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100103 break;
104 case 2: /* use argument */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600105 addr = hextoul(argv[1], NULL);
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100106 break;
107 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000108 return CMD_RET_USAGE;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100109 }
110
111 return (bmp_info(addr));
112}
113
Simon Glassed38aef2020-05-10 11:40:03 -0600114static int do_bmp_display(struct cmd_tbl *cmdtp, int flag, int argc,
115 char *const argv[])
wdenkf5f146e2003-04-20 16:52:09 +0000116{
117 ulong addr;
wdenke55402c2004-03-14 16:51:43 +0000118 int x = 0, y = 0;
wdenkf5f146e2003-04-20 16:52:09 +0000119
Anatolij Gustschinb9b5eaf2013-07-02 00:04:05 +0200120 splash_get_pos(&x, &y);
121
wdenkf5f146e2003-04-20 16:52:09 +0000122 switch (argc) {
Simon Glass892265d2019-12-28 10:45:02 -0700123 case 1: /* use image_load_addr as default address */
124 addr = image_load_addr;
wdenkf5f146e2003-04-20 16:52:09 +0000125 break;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100126 case 2: /* use argument */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600127 addr = hextoul(argv[1], NULL);
wdenkf5f146e2003-04-20 16:52:09 +0000128 break;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100129 case 4:
Simon Glass3ff49ec2021-07-24 09:03:29 -0600130 addr = hextoul(argv[1], NULL);
Patrick Delaunay28e07f32018-12-01 14:41:07 +0100131 if (!strcmp(argv[2], "m"))
132 x = BMP_ALIGN_CENTER;
133 else
Simon Glassff9b9032021-07-24 09:03:30 -0600134 x = dectoul(argv[2], NULL);
Patrick Delaunay28e07f32018-12-01 14:41:07 +0100135 if (!strcmp(argv[3], "m"))
136 y = BMP_ALIGN_CENTER;
137 else
Simon Glassff9b9032021-07-24 09:03:30 -0600138 y = dectoul(argv[3], NULL);
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200139 break;
wdenkf5f146e2003-04-20 16:52:09 +0000140 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000141 return CMD_RET_USAGE;
wdenkf5f146e2003-04-20 16:52:09 +0000142 }
143
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100144 return (bmp_display(addr, x, y));
145}
146
Simon Glassed38aef2020-05-10 11:40:03 -0600147static struct cmd_tbl cmd_bmp_sub[] = {
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100148 U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
149 U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
150};
151
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200152#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocheraeb29912010-09-17 13:10:39 +0200153void bmp_reloc(void) {
154 fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
155}
156#endif
157
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100158/*
159 * Subroutine: do_bmp
160 *
161 * Description: Handler for 'bmp' command..
162 *
163 * Inputs: argv[1] contains the subcommand
164 *
165 * Return: None
166 *
167 */
Simon Glassed38aef2020-05-10 11:40:03 -0600168static int do_bmp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100169{
Simon Glassed38aef2020-05-10 11:40:03 -0600170 struct cmd_tbl *c;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100171
172 /* Strip off leading 'bmp' command argument */
173 argc--;
174 argv++;
175
176 c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
177
Wolfgang Denk3b683112010-07-17 01:06:04 +0200178 if (c)
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100179 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200180 else
Simon Glassa06dfc72011-12-10 08:44:01 +0000181 return CMD_RET_USAGE;
wdenkf5f146e2003-04-20 16:52:09 +0000182}
183
wdenkf287a242003-07-01 21:06:45 +0000184U_BOOT_CMD(
wdenke55402c2004-03-14 16:51:43 +0000185 bmp, 5, 1, do_bmp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600186 "manipulate BMP image data",
wdenke55402c2004-03-14 16:51:43 +0000187 "info <imageAddr> - display image info\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200188 "bmp display <imageAddr> [x y] - display image at x,y"
wdenkf12e3962003-06-29 21:03:46 +0000189);
190
wdenkf5f146e2003-04-20 16:52:09 +0000191/*
192 * Subroutine: bmp_info
193 *
194 * Description: Show information about bmp file in memory
195 *
196 * Inputs: addr address of the bmp file
197 *
198 * Return: None
199 *
200 */
201static int bmp_info(ulong addr)
202{
Simon Glasscd161d82016-01-05 09:30:58 -0700203 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200204 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100205 unsigned long len;
Stefan Roese13b93692005-10-08 10:19:07 +0200206
wdenkf5f146e2003-04-20 16:52:09 +0000207 if (!((bmp->header.signature[0]=='B') &&
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100208 (bmp->header.signature[1]=='M')))
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200209 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Stefan Roese13b93692005-10-08 10:19:07 +0200210
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100211 if (bmp == NULL) {
wdenkf5f146e2003-04-20 16:52:09 +0000212 printf("There is no valid bmp file at the given address\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100213 return 1;
wdenkf5f146e2003-04-20 16:52:09 +0000214 }
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100215
wdenkf5f146e2003-04-20 16:52:09 +0000216 printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
217 le32_to_cpu(bmp->header.height));
218 printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
219 printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
Stefan Roese13b93692005-10-08 10:19:07 +0200220
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200221 if (bmp_alloc_addr)
222 free(bmp_alloc_addr);
Stefan Roese13b93692005-10-08 10:19:07 +0200223
wdenkf5f146e2003-04-20 16:52:09 +0000224 return(0);
225}
226
227/*
228 * Subroutine: bmp_display
229 *
230 * Description: Display bmp file located in memory
231 *
232 * Inputs: addr address of the bmp file
233 *
234 * Return: None
235 *
236 */
Anatolij Gustschin7a49e6f2012-04-27 04:38:06 +0000237int bmp_display(ulong addr, int x, int y)
wdenkf5f146e2003-04-20 16:52:09 +0000238{
Simon Glass68b26272016-01-18 19:52:22 -0700239#ifdef CONFIG_DM_VIDEO
240 struct udevice *dev;
241#endif
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100242 int ret;
Simon Glasscd161d82016-01-05 09:30:58 -0700243 struct bmp_image *bmp = map_sysmem(addr, 0);
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200244 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100245 unsigned long len;
246
247 if (!((bmp->header.signature[0]=='B') &&
248 (bmp->header.signature[1]=='M')))
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200249 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100250
251 if (!bmp) {
252 printf("There is no valid bmp file at the given address\n");
253 return 1;
254 }
Simon Glasscd161d82016-01-05 09:30:58 -0700255 addr = map_to_sysmem(bmp);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100256
Simon Glass68b26272016-01-18 19:52:22 -0700257#ifdef CONFIG_DM_VIDEO
Simon Glassc7298e72016-02-11 13:23:26 -0700258 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
Simon Glass68b26272016-01-18 19:52:22 -0700259 if (!ret) {
Simon Glassc7298e72016-02-11 13:23:26 -0700260 bool align = false;
Simon Glass68b26272016-01-18 19:52:22 -0700261
Patrick Delaunay28e07f32018-12-01 14:41:07 +0100262 if (CONFIG_IS_ENABLED(SPLASH_SCREEN_ALIGN) ||
263 x == BMP_ALIGN_CENTER ||
264 y == BMP_ALIGN_CENTER)
265 align = true;
266
Simon Glassc7298e72016-02-11 13:23:26 -0700267 ret = video_bmp_display(dev, addr, x, y, align);
Simon Glass68b26272016-01-18 19:52:22 -0700268 }
Simon Glass68b26272016-01-18 19:52:22 -0700269#elif defined(CONFIG_LCD)
Simon Glasscd161d82016-01-05 09:30:58 -0700270 ret = lcd_display_bitmap(addr, x, y);
wdenk7ac16102004-08-01 22:48:16 +0000271#elif defined(CONFIG_VIDEO)
Simon Glasscd161d82016-01-05 09:30:58 -0700272 ret = video_display_bitmap(addr, x, y);
wdenk7ac16102004-08-01 22:48:16 +0000273#else
274# error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
wdenke55402c2004-03-14 16:51:43 +0000275#endif
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100276
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200277 if (bmp_alloc_addr)
278 free(bmp_alloc_addr);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100279
Simon Glass476e8af2016-01-30 15:45:15 -0700280 return ret ? CMD_RET_FAILURE : 0;
wdenkf5f146e2003-04-20 16:52:09 +0000281}