blob: d2a39f677fda9fa567da98ef1ca67467ef0bdd40 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk059ae172003-04-20 16:52:09 +00002/*
3 * (C) Copyright 2002
wdenkb37c7e52003-06-30 16:24:52 +00004 * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
wdenk059ae172003-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 Glass0c670fc2019-08-01 09:46:36 -060014#include <dm.h>
15#include <gzip.h>
16#include <lcd.h>
Stefan Roesec29ab9d2005-10-08 10:19:07 +020017#include <malloc.h>
Simon Glass72b335e2016-01-05 09:30:58 -070018#include <mapmem.h>
Anatolij Gustschinff8fb562013-07-02 00:04:05 +020019#include <splash.h>
Stefan Reinauerf674f7c2012-09-28 15:11:11 +000020#include <video.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060021#include <asm/byteorder.h>
wdenk059ae172003-04-20 16:52:09 +000022
wdenk059ae172003-04-20 16:52:09 +000023static int bmp_info (ulong addr);
wdenk059ae172003-04-20 16:52:09 +000024
25/*
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010026 * Allocate and decompress a BMP image using gunzip().
27 *
Piotr Wilczekf7ef9d62013-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 Egtvedt43ef1c32007-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 Glass1c3dbe52015-05-13 07:02:27 -060039struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
40 void **alloc_addr)
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010041{
42 void *dst;
43 unsigned long len;
Simon Glass1c3dbe52015-05-13 07:02:27 -060044 struct bmp_image *bmp;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010045
46 /*
47 * Decompress bmp image
48 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020049 len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
Piotr Wilczekf7ef9d62013-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 Egtvedt43ef1c32007-11-30 17:29:59 +010052 if (dst == NULL) {
53 puts("Error: malloc in gunzip failed!\n");
54 return NULL;
55 }
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020056
57 bmp = dst;
58
59 /* align to 32-bit-aligned-address + 2 */
Adam Heinrich77195212018-12-31 12:11:08 +010060 bmp = (struct bmp_image *)((((uintptr_t)dst + 1) & ~3) + 2);
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020061
Simon Glass72b335e2016-01-05 09:30:58 -070062 if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
63 &len) != 0) {
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010064 free(dst);
65 return NULL;
66 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020067 if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010068 puts("Image could be truncated"
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020069 " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010070
Hans-Christian Egtvedt43ef1c32007-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 Denk17f79e42011-02-09 15:11:10 +010080 debug("Gzipped BMP image detected!\n");
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010081
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020082 *alloc_addr = dst;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010083 return bmp;
84}
85#else
Simon Glass1c3dbe52015-05-13 07:02:27 -060086struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
87 void **alloc_addr)
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010088{
89 return NULL;
90}
91#endif
92
Wolfgang Denk54841ab2010-06-28 22:00:46 +020093static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +010094{
95 ulong addr;
96
97 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 Glass4c12eeb2011-12-10 08:44:01 +0000105 return CMD_RET_USAGE;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100106 }
107
108 return (bmp_info(addr));
109}
110
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200111static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100112{
113 ulong addr;
114 int x = 0, y = 0;
115
Anatolij Gustschinff8fb562013-07-02 00:04:05 +0200116 splash_get_pos(&x, &y);
117
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100118 switch (argc) {
119 case 1: /* use load_addr as default address */
120 addr = load_addr;
121 break;
122 case 2: /* use argument */
123 addr = simple_strtoul(argv[1], NULL, 16);
124 break;
125 case 4:
126 addr = simple_strtoul(argv[1], NULL, 16);
Patrick Delaunayb0fcedb2018-12-01 14:41:07 +0100127 if (!strcmp(argv[2], "m"))
128 x = BMP_ALIGN_CENTER;
129 else
130 x = simple_strtoul(argv[2], NULL, 10);
131 if (!strcmp(argv[3], "m"))
132 y = BMP_ALIGN_CENTER;
133 else
134 y = simple_strtoul(argv[3], NULL, 10);
Wolfgang Denk93e14592013-10-04 17:43:24 +0200135 break;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100136 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000137 return CMD_RET_USAGE;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100138 }
139
140 return (bmp_display(addr, x, y));
141}
142
143static cmd_tbl_t cmd_bmp_sub[] = {
144 U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
145 U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
146};
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100147
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200148#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200149void bmp_reloc(void) {
150 fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
151}
152#endif
153
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100154/*
wdenk059ae172003-04-20 16:52:09 +0000155 * Subroutine: do_bmp
156 *
157 * Description: Handler for 'bmp' command..
158 *
159 * Inputs: argv[1] contains the subcommand
160 *
161 * Return: None
162 *
163 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200164static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk059ae172003-04-20 16:52:09 +0000165{
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100166 cmd_tbl_t *c;
wdenk059ae172003-04-20 16:52:09 +0000167
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100168 /* Strip off leading 'bmp' command argument */
169 argc--;
170 argv++;
wdenk059ae172003-04-20 16:52:09 +0000171
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100172 c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
173
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200174 if (c)
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100175 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200176 else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000177 return CMD_RET_USAGE;
wdenk059ae172003-04-20 16:52:09 +0000178}
179
wdenk0d498392003-07-01 21:06:45 +0000180U_BOOT_CMD(
wdenk4b248f32004-03-14 16:51:43 +0000181 bmp, 5, 1, do_bmp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600182 "manipulate BMP image data",
wdenk4b248f32004-03-14 16:51:43 +0000183 "info <imageAddr> - display image info\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200184 "bmp display <imageAddr> [x y] - display image at x,y"
wdenkb0fce992003-06-29 21:03:46 +0000185);
186
wdenk059ae172003-04-20 16:52:09 +0000187/*
188 * Subroutine: bmp_info
189 *
190 * Description: Show information about bmp file in memory
191 *
192 * Inputs: addr address of the bmp file
193 *
194 * Return: None
195 *
196 */
197static int bmp_info(ulong addr)
198{
Simon Glass72b335e2016-01-05 09:30:58 -0700199 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200200 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100201 unsigned long len;
Stefan Roesec29ab9d2005-10-08 10:19:07 +0200202
wdenk059ae172003-04-20 16:52:09 +0000203 if (!((bmp->header.signature[0]=='B') &&
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100204 (bmp->header.signature[1]=='M')))
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200205 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Stefan Roesec29ab9d2005-10-08 10:19:07 +0200206
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100207 if (bmp == NULL) {
wdenk059ae172003-04-20 16:52:09 +0000208 printf("There is no valid bmp file at the given address\n");
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100209 return 1;
wdenk059ae172003-04-20 16:52:09 +0000210 }
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100211
wdenk059ae172003-04-20 16:52:09 +0000212 printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
213 le32_to_cpu(bmp->header.height));
214 printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
215 printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
Stefan Roesec29ab9d2005-10-08 10:19:07 +0200216
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200217 if (bmp_alloc_addr)
218 free(bmp_alloc_addr);
Stefan Roesec29ab9d2005-10-08 10:19:07 +0200219
wdenk059ae172003-04-20 16:52:09 +0000220 return(0);
221}
222
223/*
224 * Subroutine: bmp_display
225 *
226 * Description: Display bmp file located in memory
227 *
228 * Inputs: addr address of the bmp file
229 *
230 * Return: None
231 *
232 */
Anatolij Gustschinde3b49c2012-04-27 04:38:06 +0000233int bmp_display(ulong addr, int x, int y)
wdenk059ae172003-04-20 16:52:09 +0000234{
Simon Glassb01c7922016-01-18 19:52:22 -0700235#ifdef CONFIG_DM_VIDEO
236 struct udevice *dev;
237#endif
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100238 int ret;
Simon Glass72b335e2016-01-05 09:30:58 -0700239 struct bmp_image *bmp = map_sysmem(addr, 0);
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200240 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100241 unsigned long len;
242
243 if (!((bmp->header.signature[0]=='B') &&
244 (bmp->header.signature[1]=='M')))
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200245 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100246
247 if (!bmp) {
248 printf("There is no valid bmp file at the given address\n");
249 return 1;
250 }
Simon Glass72b335e2016-01-05 09:30:58 -0700251 addr = map_to_sysmem(bmp);
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100252
Simon Glassb01c7922016-01-18 19:52:22 -0700253#ifdef CONFIG_DM_VIDEO
Simon Glass3f603cb2016-02-11 13:23:26 -0700254 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
Simon Glassb01c7922016-01-18 19:52:22 -0700255 if (!ret) {
Simon Glass3f603cb2016-02-11 13:23:26 -0700256 bool align = false;
Simon Glassb01c7922016-01-18 19:52:22 -0700257
Patrick Delaunayb0fcedb2018-12-01 14:41:07 +0100258 if (CONFIG_IS_ENABLED(SPLASH_SCREEN_ALIGN) ||
259 x == BMP_ALIGN_CENTER ||
260 y == BMP_ALIGN_CENTER)
261 align = true;
262
Simon Glass3f603cb2016-02-11 13:23:26 -0700263 ret = video_bmp_display(dev, addr, x, y, align);
Simon Glassb01c7922016-01-18 19:52:22 -0700264 }
Simon Glassb01c7922016-01-18 19:52:22 -0700265#elif defined(CONFIG_LCD)
Simon Glass72b335e2016-01-05 09:30:58 -0700266 ret = lcd_display_bitmap(addr, x, y);
wdenk281e00a2004-08-01 22:48:16 +0000267#elif defined(CONFIG_VIDEO)
Simon Glass72b335e2016-01-05 09:30:58 -0700268 ret = video_display_bitmap(addr, x, y);
wdenk281e00a2004-08-01 22:48:16 +0000269#else
270# error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
wdenk4b248f32004-03-14 16:51:43 +0000271#endif
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100272
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200273 if (bmp_alloc_addr)
274 free(bmp_alloc_addr);
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100275
Simon Glasse517db72016-01-30 15:45:15 -0700276 return ret ? CMD_RET_FAILURE : 0;
wdenk059ae172003-04-20 16:52:09 +0000277}