blob: 02bdf48b4d422ced97dea7587f07d74c0aee6d6e [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>
Simon Glass68b26272016-01-18 19:52:22 -070012#include <dm.h>
Alessandro Rubini465c45a2009-07-19 17:52:27 +020013#include <lcd.h>
Simon Glass68b26272016-01-18 19:52:22 -070014#include <mapmem.h>
wdenkf5f146e2003-04-20 16:52:09 +000015#include <bmp_layout.h>
16#include <command.h>
wdenk9ca7bbc2004-10-09 23:25:58 +000017#include <asm/byteorder.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>
wdenkf5f146e2003-04-20 16:52:09 +000022
wdenkf5f146e2003-04-20 16:52:09 +000023static int bmp_info (ulong addr);
wdenkf5f146e2003-04-20 16:52:09 +000024
25/*
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010026 * Allocate and decompress a BMP image using gunzip().
27 *
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020028 * Returns a pointer to the decompressed image data. This pointer is
29 * aligned to 32-bit-aligned-address + 2.
30 * See doc/README.displaying-bmps for explanation.
31 *
32 * The allocation address is passed to 'alloc_addr' and must be freed
33 * by the caller after use.
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010034 *
35 * Returns NULL if decompression failed, or if the decompressed data
36 * didn't contain a valid BMP signature.
37 */
38#ifdef CONFIG_VIDEO_BMP_GZIP
Simon Glass92e1f852015-05-13 07:02:27 -060039struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
40 void **alloc_addr)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010041{
42 void *dst;
43 unsigned long len;
Simon Glass92e1f852015-05-13 07:02:27 -060044 struct bmp_image *bmp;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010045
46 /*
47 * Decompress bmp image
48 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020049 len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020050 /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
51 dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010052 if (dst == NULL) {
53 puts("Error: malloc in gunzip failed!\n");
54 return NULL;
55 }
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020056
57 bmp = dst;
58
59 /* align to 32-bit-aligned-address + 2 */
Simon Glass92e1f852015-05-13 07:02:27 -060060 bmp = (struct bmp_image *)((((unsigned int)dst + 1) & ~3) + 2);
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020061
Simon Glasscd161d82016-01-05 09:30:58 -070062 if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
63 &len) != 0) {
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010064 free(dst);
65 return NULL;
66 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020067 if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010068 puts("Image could be truncated"
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020069 " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010070
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010071 /*
72 * Check for bmp mark 'BM'
73 */
74 if (!((bmp->header.signature[0] == 'B') &&
75 (bmp->header.signature[1] == 'M'))) {
76 free(dst);
77 return NULL;
78 }
79
Wolfgang Denkaf42bb32011-02-09 15:11:10 +010080 debug("Gzipped BMP image detected!\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010081
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020082 *alloc_addr = dst;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010083 return bmp;
84}
85#else
Simon Glass92e1f852015-05-13 07:02:27 -060086struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
87 void **alloc_addr)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010088{
89 return NULL;
90}
91#endif
92
Wolfgang Denk6262d0212010-06-28 22:00:46 +020093static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +010094{
95 ulong addr;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010096
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +010097 switch (argc) {
98 case 1: /* use load_addr as default address */
99 addr = load_addr;
100 break;
101 case 2: /* use argument */
102 addr = simple_strtoul(argv[1], NULL, 16);
103 break;
104 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000105 return CMD_RET_USAGE;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100106 }
107
108 return (bmp_info(addr));
109}
110
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200111static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkf5f146e2003-04-20 16:52:09 +0000112{
113 ulong addr;
wdenke55402c2004-03-14 16:51:43 +0000114 int x = 0, y = 0;
wdenkf5f146e2003-04-20 16:52:09 +0000115
Anatolij Gustschinb9b5eaf2013-07-02 00:04:05 +0200116 splash_get_pos(&x, &y);
117
wdenkf5f146e2003-04-20 16:52:09 +0000118 switch (argc) {
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100119 case 1: /* use load_addr as default address */
wdenkf5f146e2003-04-20 16:52:09 +0000120 addr = load_addr;
121 break;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100122 case 2: /* use argument */
123 addr = simple_strtoul(argv[1], NULL, 16);
wdenkf5f146e2003-04-20 16:52:09 +0000124 break;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100125 case 4:
126 addr = simple_strtoul(argv[1], NULL, 16);
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200127 x = simple_strtoul(argv[2], NULL, 10);
128 y = simple_strtoul(argv[3], NULL, 10);
129 break;
wdenkf5f146e2003-04-20 16:52:09 +0000130 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000131 return CMD_RET_USAGE;
wdenkf5f146e2003-04-20 16:52:09 +0000132 }
133
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100134 return (bmp_display(addr, x, y));
135}
136
137static cmd_tbl_t cmd_bmp_sub[] = {
138 U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
139 U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
140};
141
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200142#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocheraeb29912010-09-17 13:10:39 +0200143void bmp_reloc(void) {
144 fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
145}
146#endif
147
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100148/*
149 * Subroutine: do_bmp
150 *
151 * Description: Handler for 'bmp' command..
152 *
153 * Inputs: argv[1] contains the subcommand
154 *
155 * Return: None
156 *
157 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200158static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100159{
160 cmd_tbl_t *c;
161
162 /* Strip off leading 'bmp' command argument */
163 argc--;
164 argv++;
165
166 c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
167
Wolfgang Denk3b683112010-07-17 01:06:04 +0200168 if (c)
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100169 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200170 else
Simon Glassa06dfc72011-12-10 08:44:01 +0000171 return CMD_RET_USAGE;
wdenkf5f146e2003-04-20 16:52:09 +0000172}
173
wdenkf287a242003-07-01 21:06:45 +0000174U_BOOT_CMD(
wdenke55402c2004-03-14 16:51:43 +0000175 bmp, 5, 1, do_bmp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600176 "manipulate BMP image data",
wdenke55402c2004-03-14 16:51:43 +0000177 "info <imageAddr> - display image info\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200178 "bmp display <imageAddr> [x y] - display image at x,y"
wdenkf12e3962003-06-29 21:03:46 +0000179);
180
wdenkf5f146e2003-04-20 16:52:09 +0000181/*
182 * Subroutine: bmp_info
183 *
184 * Description: Show information about bmp file in memory
185 *
186 * Inputs: addr address of the bmp file
187 *
188 * Return: None
189 *
190 */
191static int bmp_info(ulong addr)
192{
Simon Glasscd161d82016-01-05 09:30:58 -0700193 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200194 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100195 unsigned long len;
Stefan Roese13b93692005-10-08 10:19:07 +0200196
wdenkf5f146e2003-04-20 16:52:09 +0000197 if (!((bmp->header.signature[0]=='B') &&
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100198 (bmp->header.signature[1]=='M')))
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200199 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Stefan Roese13b93692005-10-08 10:19:07 +0200200
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100201 if (bmp == NULL) {
wdenkf5f146e2003-04-20 16:52:09 +0000202 printf("There is no valid bmp file at the given address\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100203 return 1;
wdenkf5f146e2003-04-20 16:52:09 +0000204 }
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100205
wdenkf5f146e2003-04-20 16:52:09 +0000206 printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
207 le32_to_cpu(bmp->header.height));
208 printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
209 printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
Stefan Roese13b93692005-10-08 10:19:07 +0200210
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200211 if (bmp_alloc_addr)
212 free(bmp_alloc_addr);
Stefan Roese13b93692005-10-08 10:19:07 +0200213
wdenkf5f146e2003-04-20 16:52:09 +0000214 return(0);
215}
216
217/*
218 * Subroutine: bmp_display
219 *
220 * Description: Display bmp file located in memory
221 *
222 * Inputs: addr address of the bmp file
223 *
224 * Return: None
225 *
226 */
Anatolij Gustschin7a49e6f2012-04-27 04:38:06 +0000227int bmp_display(ulong addr, int x, int y)
wdenkf5f146e2003-04-20 16:52:09 +0000228{
Simon Glass68b26272016-01-18 19:52:22 -0700229#ifdef CONFIG_DM_VIDEO
230 struct udevice *dev;
231#endif
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100232 int ret;
Simon Glasscd161d82016-01-05 09:30:58 -0700233 struct bmp_image *bmp = map_sysmem(addr, 0);
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200234 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100235 unsigned long len;
236
237 if (!((bmp->header.signature[0]=='B') &&
238 (bmp->header.signature[1]=='M')))
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200239 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100240
241 if (!bmp) {
242 printf("There is no valid bmp file at the given address\n");
243 return 1;
244 }
Simon Glasscd161d82016-01-05 09:30:58 -0700245 addr = map_to_sysmem(bmp);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100246
Simon Glass68b26272016-01-18 19:52:22 -0700247#ifdef CONFIG_DM_VIDEO
Simon Glassc7298e72016-02-11 13:23:26 -0700248 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
Simon Glass68b26272016-01-18 19:52:22 -0700249 if (!ret) {
Simon Glassc7298e72016-02-11 13:23:26 -0700250 bool align = false;
Simon Glass68b26272016-01-18 19:52:22 -0700251
252# ifdef CONFIG_SPLASH_SCREEN_ALIGN
Simon Glassc7298e72016-02-11 13:23:26 -0700253 align = true;
Simon Glass68b26272016-01-18 19:52:22 -0700254# endif /* CONFIG_SPLASH_SCREEN_ALIGN */
Simon Glassc7298e72016-02-11 13:23:26 -0700255 ret = video_bmp_display(dev, addr, x, y, align);
Simon Glass68b26272016-01-18 19:52:22 -0700256 }
Simon Glass68b26272016-01-18 19:52:22 -0700257#elif defined(CONFIG_LCD)
Simon Glasscd161d82016-01-05 09:30:58 -0700258 ret = lcd_display_bitmap(addr, x, y);
wdenk7ac16102004-08-01 22:48:16 +0000259#elif defined(CONFIG_VIDEO)
Simon Glasscd161d82016-01-05 09:30:58 -0700260 ret = video_display_bitmap(addr, x, y);
wdenk7ac16102004-08-01 22:48:16 +0000261#else
262# error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
wdenke55402c2004-03-14 16:51:43 +0000263#endif
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100264
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200265 if (bmp_alloc_addr)
266 free(bmp_alloc_addr);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100267
Simon Glass476e8af2016-01-30 15:45:15 -0700268 return ret ? CMD_RET_FAILURE : 0;
wdenkf5f146e2003-04-20 16:52:09 +0000269}