blob: cb1f07119b4f6c7963590c394a779f4339a2934e [file] [log] [blame]
wdenkf5f146e2003-04-20 16:52:09 +00001/*
2 * (C) Copyright 2002
wdenkb9bbd242003-06-30 16:24:52 +00003 * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
wdenkf5f146e2003-04-20 16:52:09 +00004 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkf5f146e2003-04-20 16:52:09 +00006 */
7
8/*
9 * BMP handling routines
10 */
11
12#include <common.h>
Alessandro Rubini465c45a2009-07-19 17:52:27 +020013#include <lcd.h>
wdenkf5f146e2003-04-20 16:52:09 +000014#include <bmp_layout.h>
15#include <command.h>
wdenk9ca7bbc2004-10-09 23:25:58 +000016#include <asm/byteorder.h>
Stefan Roese13b93692005-10-08 10:19:07 +020017#include <malloc.h>
Anatolij Gustschinb9b5eaf2013-07-02 00:04:05 +020018#include <splash.h>
Stefan Reinauer987d1d82012-09-28 15:11:11 +000019#include <video.h>
wdenkf5f146e2003-04-20 16:52:09 +000020
wdenkf5f146e2003-04-20 16:52:09 +000021static int bmp_info (ulong addr);
wdenkf5f146e2003-04-20 16:52:09 +000022
23/*
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010024 * Allocate and decompress a BMP image using gunzip().
25 *
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020026 * Returns a pointer to the decompressed image data. This pointer is
27 * aligned to 32-bit-aligned-address + 2.
28 * See doc/README.displaying-bmps for explanation.
29 *
30 * The allocation address is passed to 'alloc_addr' and must be freed
31 * by the caller after use.
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010032 *
33 * Returns NULL if decompression failed, or if the decompressed data
34 * didn't contain a valid BMP signature.
35 */
36#ifdef CONFIG_VIDEO_BMP_GZIP
Simon Glass92e1f852015-05-13 07:02:27 -060037struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
38 void **alloc_addr)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010039{
40 void *dst;
41 unsigned long len;
Simon Glass92e1f852015-05-13 07:02:27 -060042 struct bmp_image *bmp;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010043
44 /*
45 * Decompress bmp image
46 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020047 len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020048 /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
49 dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010050 if (dst == NULL) {
51 puts("Error: malloc in gunzip failed!\n");
52 return NULL;
53 }
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020054
55 bmp = dst;
56
57 /* align to 32-bit-aligned-address + 2 */
Simon Glass92e1f852015-05-13 07:02:27 -060058 bmp = (struct bmp_image *)((((unsigned int)dst + 1) & ~3) + 2);
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020059
60 if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, (uchar *)addr, &len) != 0) {
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010061 free(dst);
62 return NULL;
63 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020064 if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010065 puts("Image could be truncated"
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020066 " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010067
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010068 /*
69 * Check for bmp mark 'BM'
70 */
71 if (!((bmp->header.signature[0] == 'B') &&
72 (bmp->header.signature[1] == 'M'))) {
73 free(dst);
74 return NULL;
75 }
76
Wolfgang Denkaf42bb32011-02-09 15:11:10 +010077 debug("Gzipped BMP image detected!\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010078
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +020079 *alloc_addr = dst;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010080 return bmp;
81}
82#else
Simon Glass92e1f852015-05-13 07:02:27 -060083struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
84 void **alloc_addr)
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010085{
86 return NULL;
87}
88#endif
89
Wolfgang Denk6262d0212010-06-28 22:00:46 +020090static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +010091{
92 ulong addr;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +010093
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +010094 switch (argc) {
95 case 1: /* use load_addr as default address */
96 addr = load_addr;
97 break;
98 case 2: /* use argument */
99 addr = simple_strtoul(argv[1], NULL, 16);
100 break;
101 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000102 return CMD_RET_USAGE;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100103 }
104
105 return (bmp_info(addr));
106}
107
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200108static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
wdenkf5f146e2003-04-20 16:52:09 +0000109{
110 ulong addr;
wdenke55402c2004-03-14 16:51:43 +0000111 int x = 0, y = 0;
wdenkf5f146e2003-04-20 16:52:09 +0000112
Anatolij Gustschinb9b5eaf2013-07-02 00:04:05 +0200113 splash_get_pos(&x, &y);
114
wdenkf5f146e2003-04-20 16:52:09 +0000115 switch (argc) {
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100116 case 1: /* use load_addr as default address */
wdenkf5f146e2003-04-20 16:52:09 +0000117 addr = load_addr;
118 break;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100119 case 2: /* use argument */
120 addr = simple_strtoul(argv[1], NULL, 16);
wdenkf5f146e2003-04-20 16:52:09 +0000121 break;
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100122 case 4:
123 addr = simple_strtoul(argv[1], NULL, 16);
Wolfgang Denkec7fbf52013-10-04 17:43:24 +0200124 x = simple_strtoul(argv[2], NULL, 10);
125 y = simple_strtoul(argv[3], NULL, 10);
126 break;
wdenkf5f146e2003-04-20 16:52:09 +0000127 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000128 return CMD_RET_USAGE;
wdenkf5f146e2003-04-20 16:52:09 +0000129 }
130
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100131 return (bmp_display(addr, x, y));
132}
133
134static cmd_tbl_t cmd_bmp_sub[] = {
135 U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
136 U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
137};
138
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200139#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocheraeb29912010-09-17 13:10:39 +0200140void bmp_reloc(void) {
141 fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
142}
143#endif
144
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100145/*
146 * Subroutine: do_bmp
147 *
148 * Description: Handler for 'bmp' command..
149 *
150 * Inputs: argv[1] contains the subcommand
151 *
152 * Return: None
153 *
154 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200155static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100156{
157 cmd_tbl_t *c;
158
159 /* Strip off leading 'bmp' command argument */
160 argc--;
161 argv++;
162
163 c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
164
Wolfgang Denk3b683112010-07-17 01:06:04 +0200165 if (c)
Frans Meulenbroeksc59bc502010-03-27 11:16:10 +0100166 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk3b683112010-07-17 01:06:04 +0200167 else
Simon Glassa06dfc72011-12-10 08:44:01 +0000168 return CMD_RET_USAGE;
wdenkf5f146e2003-04-20 16:52:09 +0000169}
170
wdenkf287a242003-07-01 21:06:45 +0000171U_BOOT_CMD(
wdenke55402c2004-03-14 16:51:43 +0000172 bmp, 5, 1, do_bmp,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600173 "manipulate BMP image data",
wdenke55402c2004-03-14 16:51:43 +0000174 "info <imageAddr> - display image info\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200175 "bmp display <imageAddr> [x y] - display image at x,y"
wdenkf12e3962003-06-29 21:03:46 +0000176);
177
wdenkf5f146e2003-04-20 16:52:09 +0000178/*
179 * Subroutine: bmp_info
180 *
181 * Description: Show information about bmp file in memory
182 *
183 * Inputs: addr address of the bmp file
184 *
185 * Return: None
186 *
187 */
188static int bmp_info(ulong addr)
189{
Simon Glass92e1f852015-05-13 07:02:27 -0600190 struct bmp_image *bmp = (struct bmp_image *)addr;
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200191 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100192 unsigned long len;
Stefan Roese13b93692005-10-08 10:19:07 +0200193
wdenkf5f146e2003-04-20 16:52:09 +0000194 if (!((bmp->header.signature[0]=='B') &&
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100195 (bmp->header.signature[1]=='M')))
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200196 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Stefan Roese13b93692005-10-08 10:19:07 +0200197
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100198 if (bmp == NULL) {
wdenkf5f146e2003-04-20 16:52:09 +0000199 printf("There is no valid bmp file at the given address\n");
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100200 return 1;
wdenkf5f146e2003-04-20 16:52:09 +0000201 }
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100202
wdenkf5f146e2003-04-20 16:52:09 +0000203 printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
204 le32_to_cpu(bmp->header.height));
205 printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
206 printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
Stefan Roese13b93692005-10-08 10:19:07 +0200207
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200208 if (bmp_alloc_addr)
209 free(bmp_alloc_addr);
Stefan Roese13b93692005-10-08 10:19:07 +0200210
wdenkf5f146e2003-04-20 16:52:09 +0000211 return(0);
212}
213
214/*
215 * Subroutine: bmp_display
216 *
217 * Description: Display bmp file located in memory
218 *
219 * Inputs: addr address of the bmp file
220 *
221 * Return: None
222 *
223 */
Anatolij Gustschin7a49e6f2012-04-27 04:38:06 +0000224int bmp_display(ulong addr, int x, int y)
wdenkf5f146e2003-04-20 16:52:09 +0000225{
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100226 int ret;
Simon Glass92e1f852015-05-13 07:02:27 -0600227 struct bmp_image *bmp = (struct bmp_image *)addr;
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200228 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100229 unsigned long len;
230
231 if (!((bmp->header.signature[0]=='B') &&
232 (bmp->header.signature[1]=='M')))
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200233 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100234
235 if (!bmp) {
236 printf("There is no valid bmp file at the given address\n");
237 return 1;
238 }
239
wdenk7ac16102004-08-01 22:48:16 +0000240#if defined(CONFIG_LCD)
Che-Liang Chiou2444b172011-10-20 23:07:03 +0000241 ret = lcd_display_bitmap((ulong)bmp, x, y);
wdenk7ac16102004-08-01 22:48:16 +0000242#elif defined(CONFIG_VIDEO)
Stefan Reinauer987d1d82012-09-28 15:11:11 +0000243 ret = video_display_bitmap((unsigned long)bmp, x, y);
wdenk7ac16102004-08-01 22:48:16 +0000244#else
245# error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
wdenke55402c2004-03-14 16:51:43 +0000246#endif
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100247
Piotr Wilczeke59e0ff2013-06-05 08:14:30 +0200248 if (bmp_alloc_addr)
249 free(bmp_alloc_addr);
Hans-Christian Egtvedt68d89dc2007-11-30 17:29:59 +0100250
251 return ret;
wdenkf5f146e2003-04-20 16:52:09 +0000252}