blob: edf4f564266bf9eddbe068c6765ad12ede49fb22 [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>
Stefan Roese13b93692005-10-08 10:19:07 +020018#include <malloc.h>
Simon Glasscd161d82016-01-05 09:30:58 -070019#include <mapmem.h>
Anatolij Gustschinb9b5eaf2013-07-02 00:04:05 +020020#include <splash.h>
Stefan Reinauer987d1d82012-09-28 15:11:11 +000021#include <video.h>
Simon Glass1a974af2019-08-01 09:46:36 -060022#include <asm/byteorder.h>
wdenkf5f146e2003-04-20 16:52:09 +000023
wdenkf5f146e2003-04-20 16:52:09 +000024static int bmp_info (ulong addr);
wdenkf5f146e2003-04-20 16:52:09 +000025
26/*
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010027 * Allocate and decompress a BMP image using gunzip().
28 *
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020029 * Returns a pointer to the decompressed image data. This pointer is
30 * aligned to 32-bit-aligned-address + 2.
31 * See doc/README.displaying-bmps for explanation.
32 *
33 * The allocation address is passed to 'alloc_addr' and must be freed
34 * by the caller after use.
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010035 *
36 * Returns NULL if decompression failed, or if the decompressed data
37 * didn't contain a valid BMP signature.
38 */
39#ifdef CONFIG_VIDEO_BMP_GZIP
Simon Glass92e1f852015-05-13 07:02:27 -060040struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
41 void **alloc_addr)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010042{
43 void *dst;
44 unsigned long len;
Simon Glass92e1f852015-05-13 07:02:27 -060045 struct bmp_image *bmp;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010046
47 /*
48 * Decompress bmp image
49 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020050 len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020051 /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
52 dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010053 if (dst == NULL) {
54 puts("Error: malloc in gunzip failed!\n");
55 return NULL;
56 }
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020057
58 bmp = dst;
59
60 /* align to 32-bit-aligned-address + 2 */
Adam Heinrichfedd94c2018-12-31 12:11:08 +010061 bmp = (struct bmp_image *)((((uintptr_t)dst + 1) & ~3) + 2);
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020062
Simon Glasscd161d82016-01-05 09:30:58 -070063 if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
64 &len) != 0) {
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010065 free(dst);
66 return NULL;
67 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020068 if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010069 puts("Image could be truncated"
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020070 " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010071
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010072 /*
73 * Check for bmp mark 'BM'
74 */
75 if (!((bmp->header.signature[0] == 'B') &&
76 (bmp->header.signature[1] == 'M'))) {
77 free(dst);
78 return NULL;
79 }
80
Wolfgang Denkaf42bb32011-02-09 15:11:10 +010081 debug("Gzipped BMP image detected!\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010082
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020083 *alloc_addr = dst;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010084 return bmp;
85}
86#else
Simon Glass92e1f852015-05-13 07:02:27 -060087struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
88 void **alloc_addr)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010089{
90 return NULL;
91}
92#endif
93
Wolfgang Denk6262d0212010-06-28 22:00:46 +020094static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +010095{
96 ulong addr;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010097
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +010098 switch (argc) {
Simon Glass892265d2019-12-28 10:45:02 -070099 case 1: /* use image_load_addr as default address */
100 addr = image_load_addr;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100101 break;
102 case 2: /* use argument */
103 addr = simple_strtoul(argv[1], NULL, 16);
104 break;
105 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000106 return CMD_RET_USAGE;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100107 }
108
109 return (bmp_info(addr));
110}
111
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200112static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkf5f146e2003-04-20 16:52:09 +0000113{
114 ulong addr;
wdenke55402c2004-03-14 16:51:43 +0000115 int x = 0, y = 0;
wdenkf5f146e2003-04-20 16:52:09 +0000116
Anatolij Gustschinb9b5eaf2013-07-02 00:04:05 +0200117 splash_get_pos(&x, &y);
118
wdenkf5f146e2003-04-20 16:52:09 +0000119 switch (argc) {
Simon Glass892265d2019-12-28 10:45:02 -0700120 case 1: /* use image_load_addr as default address */
121 addr = image_load_addr;
wdenkf5f146e2003-04-20 16:52:09 +0000122 break;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100123 case 2: /* use argument */
124 addr = simple_strtoul(argv[1], NULL, 16);
wdenkf5f146e2003-04-20 16:52:09 +0000125 break;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100126 case 4:
127 addr = simple_strtoul(argv[1], NULL, 16);
Patrick Delaunay28e07f32018-12-01 14:41:07 +0100128 if (!strcmp(argv[2], "m"))
129 x = BMP_ALIGN_CENTER;
130 else
131 x = simple_strtoul(argv[2], NULL, 10);
132 if (!strcmp(argv[3], "m"))
133 y = BMP_ALIGN_CENTER;
134 else
135 y = simple_strtoul(argv[3], NULL, 10);
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200136 break;
wdenkf5f146e2003-04-20 16:52:09 +0000137 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000138 return CMD_RET_USAGE;
wdenkf5f146e2003-04-20 16:52:09 +0000139 }
140
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100141 return (bmp_display(addr, x, y));
142}
143
144static cmd_tbl_t cmd_bmp_sub[] = {
145 U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
146 U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
147};
148
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200149#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocheraeb29912010-09-17 13:10:39 +0200150void bmp_reloc(void) {
151 fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
152}
153#endif
154
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100155/*
156 * Subroutine: do_bmp
157 *
158 * Description: Handler for 'bmp' command..
159 *
160 * Inputs: argv[1] contains the subcommand
161 *
162 * Return: None
163 *
164 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200165static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100166{
167 cmd_tbl_t *c;
168
169 /* Strip off leading 'bmp' command argument */
170 argc--;
171 argv++;
172
173 c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
174
Wolfgang Denk3b683112010-07-17 01:06:04 +0200175 if (c)
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100176 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200177 else
Simon Glassa06dfc72011-12-10 08:44:01 +0000178 return CMD_RET_USAGE;
wdenkf5f146e2003-04-20 16:52:09 +0000179}
180
wdenkf287a242003-07-01 21:06:45 +0000181U_BOOT_CMD(
wdenke55402c2004-03-14 16:51:43 +0000182 bmp, 5, 1, do_bmp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600183 "manipulate BMP image data",
wdenke55402c2004-03-14 16:51:43 +0000184 "info <imageAddr> - display image info\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200185 "bmp display <imageAddr> [x y] - display image at x,y"
wdenkf12e3962003-06-29 21:03:46 +0000186);
187
wdenkf5f146e2003-04-20 16:52:09 +0000188/*
189 * Subroutine: bmp_info
190 *
191 * Description: Show information about bmp file in memory
192 *
193 * Inputs: addr address of the bmp file
194 *
195 * Return: None
196 *
197 */
198static int bmp_info(ulong addr)
199{
Simon Glasscd161d82016-01-05 09:30:58 -0700200 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200201 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100202 unsigned long len;
Stefan Roese13b93692005-10-08 10:19:07 +0200203
wdenkf5f146e2003-04-20 16:52:09 +0000204 if (!((bmp->header.signature[0]=='B') &&
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100205 (bmp->header.signature[1]=='M')))
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200206 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Stefan Roese13b93692005-10-08 10:19:07 +0200207
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100208 if (bmp == NULL) {
wdenkf5f146e2003-04-20 16:52:09 +0000209 printf("There is no valid bmp file at the given address\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100210 return 1;
wdenkf5f146e2003-04-20 16:52:09 +0000211 }
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100212
wdenkf5f146e2003-04-20 16:52:09 +0000213 printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
214 le32_to_cpu(bmp->header.height));
215 printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
216 printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
Stefan Roese13b93692005-10-08 10:19:07 +0200217
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200218 if (bmp_alloc_addr)
219 free(bmp_alloc_addr);
Stefan Roese13b93692005-10-08 10:19:07 +0200220
wdenkf5f146e2003-04-20 16:52:09 +0000221 return(0);
222}
223
224/*
225 * Subroutine: bmp_display
226 *
227 * Description: Display bmp file located in memory
228 *
229 * Inputs: addr address of the bmp file
230 *
231 * Return: None
232 *
233 */
Anatolij Gustschin7a49e6f2012-04-27 04:38:06 +0000234int bmp_display(ulong addr, int x, int y)
wdenkf5f146e2003-04-20 16:52:09 +0000235{
Simon Glass68b26272016-01-18 19:52:22 -0700236#ifdef CONFIG_DM_VIDEO
237 struct udevice *dev;
238#endif
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100239 int ret;
Simon Glasscd161d82016-01-05 09:30:58 -0700240 struct bmp_image *bmp = map_sysmem(addr, 0);
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200241 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100242 unsigned long len;
243
244 if (!((bmp->header.signature[0]=='B') &&
245 (bmp->header.signature[1]=='M')))
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200246 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100247
248 if (!bmp) {
249 printf("There is no valid bmp file at the given address\n");
250 return 1;
251 }
Simon Glasscd161d82016-01-05 09:30:58 -0700252 addr = map_to_sysmem(bmp);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100253
Simon Glass68b26272016-01-18 19:52:22 -0700254#ifdef CONFIG_DM_VIDEO
Simon Glassc7298e72016-02-11 13:23:26 -0700255 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
Simon Glass68b26272016-01-18 19:52:22 -0700256 if (!ret) {
Simon Glassc7298e72016-02-11 13:23:26 -0700257 bool align = false;
Simon Glass68b26272016-01-18 19:52:22 -0700258
Patrick Delaunay28e07f32018-12-01 14:41:07 +0100259 if (CONFIG_IS_ENABLED(SPLASH_SCREEN_ALIGN) ||
260 x == BMP_ALIGN_CENTER ||
261 y == BMP_ALIGN_CENTER)
262 align = true;
263
Simon Glassc7298e72016-02-11 13:23:26 -0700264 ret = video_bmp_display(dev, addr, x, y, align);
Simon Glass68b26272016-01-18 19:52:22 -0700265 }
Simon Glass68b26272016-01-18 19:52:22 -0700266#elif defined(CONFIG_LCD)
Simon Glasscd161d82016-01-05 09:30:58 -0700267 ret = lcd_display_bitmap(addr, x, y);
wdenk7ac16102004-08-01 22:48:16 +0000268#elif defined(CONFIG_VIDEO)
Simon Glasscd161d82016-01-05 09:30:58 -0700269 ret = video_display_bitmap(addr, x, y);
wdenk7ac16102004-08-01 22:48:16 +0000270#else
271# error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
wdenke55402c2004-03-14 16:51:43 +0000272#endif
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100273
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200274 if (bmp_alloc_addr)
275 free(bmp_alloc_addr);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100276
Simon Glass476e8af2016-01-30 15:45:15 -0700277 return ret ? CMD_RET_FAILURE : 0;
wdenkf5f146e2003-04-20 16:52:09 +0000278}