blob: 45f4c1296de746c9d657d0266f58b8bbb9bfd4a1 [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>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070016#include <image.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060017#include <lcd.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060018#include <log.h>
Stefan Roesec29ab9d2005-10-08 10:19:07 +020019#include <malloc.h>
Simon Glass72b335e2016-01-05 09:30:58 -070020#include <mapmem.h>
Anatolij Gustschinff8fb562013-07-02 00:04:05 +020021#include <splash.h>
Stefan Reinauerf674f7c2012-09-28 15:11:11 +000022#include <video.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060023#include <asm/byteorder.h>
wdenk059ae172003-04-20 16:52:09 +000024
wdenk059ae172003-04-20 16:52:09 +000025static int bmp_info (ulong addr);
wdenk059ae172003-04-20 16:52:09 +000026
27/*
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010028 * Allocate and decompress a BMP image using gunzip().
29 *
Piotr Wilczekf7ef9d62013-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 Egtvedt43ef1c32007-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 Glass1c3dbe52015-05-13 07:02:27 -060041struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
42 void **alloc_addr)
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010043{
44 void *dst;
45 unsigned long len;
Simon Glass1c3dbe52015-05-13 07:02:27 -060046 struct bmp_image *bmp;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010047
48 /*
49 * Decompress bmp image
50 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051 len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
Piotr Wilczekf7ef9d62013-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 Egtvedt43ef1c32007-11-30 17:29:59 +010054 if (dst == NULL) {
55 puts("Error: malloc in gunzip failed!\n");
56 return NULL;
57 }
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020058
59 bmp = dst;
60
61 /* align to 32-bit-aligned-address + 2 */
Adam Heinrich77195212018-12-31 12:11:08 +010062 bmp = (struct bmp_image *)((((uintptr_t)dst + 1) & ~3) + 2);
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020063
Simon Glass72b335e2016-01-05 09:30:58 -070064 if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
65 &len) != 0) {
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010066 free(dst);
67 return NULL;
68 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020069 if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010070 puts("Image could be truncated"
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020071 " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010072
Hans-Christian Egtvedt43ef1c32007-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 Denk17f79e42011-02-09 15:11:10 +010082 debug("Gzipped BMP image detected!\n");
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010083
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020084 *alloc_addr = dst;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010085 return bmp;
86}
87#else
Simon Glass1c3dbe52015-05-13 07:02:27 -060088struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
89 void **alloc_addr)
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010090{
91 return NULL;
92}
93#endif
94
Simon Glass09140112020-05-10 11:40:03 -060095static int do_bmp_info(struct cmd_tbl *cmdtp, int flag, int argc,
96 char *const argv[])
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +010097{
98 ulong addr;
99
100 switch (argc) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700101 case 1: /* use image_load_addr as default address */
102 addr = image_load_addr;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100103 break;
104 case 2: /* use argument */
Simon Glass7e5f4602021-07-24 09:03:29 -0600105 addr = hextoul(argv[1], NULL);
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100106 break;
107 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000108 return CMD_RET_USAGE;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100109 }
110
111 return (bmp_info(addr));
112}
113
Simon Glass09140112020-05-10 11:40:03 -0600114static int do_bmp_display(struct cmd_tbl *cmdtp, int flag, int argc,
115 char *const argv[])
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100116{
117 ulong addr;
118 int x = 0, y = 0;
119
Anatolij Gustschinff8fb562013-07-02 00:04:05 +0200120 splash_get_pos(&x, &y);
121
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100122 switch (argc) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700123 case 1: /* use image_load_addr as default address */
124 addr = image_load_addr;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100125 break;
126 case 2: /* use argument */
Simon Glass7e5f4602021-07-24 09:03:29 -0600127 addr = hextoul(argv[1], NULL);
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100128 break;
129 case 4:
Simon Glass7e5f4602021-07-24 09:03:29 -0600130 addr = hextoul(argv[1], NULL);
Patrick Delaunayb0fcedb2018-12-01 14:41:07 +0100131 if (!strcmp(argv[2], "m"))
132 x = BMP_ALIGN_CENTER;
133 else
Simon Glass0b1284e2021-07-24 09:03:30 -0600134 x = dectoul(argv[2], NULL);
Patrick Delaunayb0fcedb2018-12-01 14:41:07 +0100135 if (!strcmp(argv[3], "m"))
136 y = BMP_ALIGN_CENTER;
137 else
Simon Glass0b1284e2021-07-24 09:03:30 -0600138 y = dectoul(argv[3], NULL);
Wolfgang Denk93e14592013-10-04 17:43:24 +0200139 break;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100140 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000141 return CMD_RET_USAGE;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100142 }
143
144 return (bmp_display(addr, x, y));
145}
146
Simon Glass09140112020-05-10 11:40:03 -0600147static struct cmd_tbl cmd_bmp_sub[] = {
Frans Meulenbroeks9acd4f02010-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};
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100151
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200152#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200153void bmp_reloc(void) {
154 fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
155}
156#endif
157
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100158/*
wdenk059ae172003-04-20 16:52:09 +0000159 * 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 Glass09140112020-05-10 11:40:03 -0600168static int do_bmp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk059ae172003-04-20 16:52:09 +0000169{
Simon Glass09140112020-05-10 11:40:03 -0600170 struct cmd_tbl *c;
wdenk059ae172003-04-20 16:52:09 +0000171
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100172 /* Strip off leading 'bmp' command argument */
173 argc--;
174 argv++;
wdenk059ae172003-04-20 16:52:09 +0000175
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100176 c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
177
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200178 if (c)
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100179 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200180 else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000181 return CMD_RET_USAGE;
wdenk059ae172003-04-20 16:52:09 +0000182}
183
wdenk0d498392003-07-01 21:06:45 +0000184U_BOOT_CMD(
wdenk4b248f32004-03-14 16:51:43 +0000185 bmp, 5, 1, do_bmp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600186 "manipulate BMP image data",
wdenk4b248f32004-03-14 16:51:43 +0000187 "info <imageAddr> - display image info\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200188 "bmp display <imageAddr> [x y] - display image at x,y"
wdenkb0fce992003-06-29 21:03:46 +0000189);
190
wdenk059ae172003-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 Glass72b335e2016-01-05 09:30:58 -0700203 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200204 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100205 unsigned long len;
Stefan Roesec29ab9d2005-10-08 10:19:07 +0200206
wdenk059ae172003-04-20 16:52:09 +0000207 if (!((bmp->header.signature[0]=='B') &&
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100208 (bmp->header.signature[1]=='M')))
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200209 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Stefan Roesec29ab9d2005-10-08 10:19:07 +0200210
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100211 if (bmp == NULL) {
wdenk059ae172003-04-20 16:52:09 +0000212 printf("There is no valid bmp file at the given address\n");
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100213 return 1;
wdenk059ae172003-04-20 16:52:09 +0000214 }
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100215
wdenk059ae172003-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 Roesec29ab9d2005-10-08 10:19:07 +0200220
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200221 if (bmp_alloc_addr)
222 free(bmp_alloc_addr);
Stefan Roesec29ab9d2005-10-08 10:19:07 +0200223
wdenk059ae172003-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 Gustschinde3b49c2012-04-27 04:38:06 +0000237int bmp_display(ulong addr, int x, int y)
wdenk059ae172003-04-20 16:52:09 +0000238{
Simon Glassb01c7922016-01-18 19:52:22 -0700239#ifdef CONFIG_DM_VIDEO
240 struct udevice *dev;
241#endif
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100242 int ret;
Simon Glass72b335e2016-01-05 09:30:58 -0700243 struct bmp_image *bmp = map_sysmem(addr, 0);
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200244 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100245 unsigned long len;
246
247 if (!((bmp->header.signature[0]=='B') &&
248 (bmp->header.signature[1]=='M')))
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200249 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Hans-Christian Egtvedt43ef1c32007-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 Glass72b335e2016-01-05 09:30:58 -0700255 addr = map_to_sysmem(bmp);
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100256
Simon Glassb01c7922016-01-18 19:52:22 -0700257#ifdef CONFIG_DM_VIDEO
Simon Glass3f603cb2016-02-11 13:23:26 -0700258 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
Simon Glassb01c7922016-01-18 19:52:22 -0700259 if (!ret) {
Simon Glass3f603cb2016-02-11 13:23:26 -0700260 bool align = false;
Simon Glassb01c7922016-01-18 19:52:22 -0700261
Patrick Delaunayb0fcedb2018-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 Glass3f603cb2016-02-11 13:23:26 -0700267 ret = video_bmp_display(dev, addr, x, y, align);
Simon Glassb01c7922016-01-18 19:52:22 -0700268 }
Simon Glassb01c7922016-01-18 19:52:22 -0700269#elif defined(CONFIG_LCD)
Simon Glass72b335e2016-01-05 09:30:58 -0700270 ret = lcd_display_bitmap(addr, x, y);
wdenk281e00a2004-08-01 22:48:16 +0000271#else
Simon Glass1fa43ca2022-01-23 07:04:08 -0700272# error bmp_display() requires CONFIG_LCD
wdenk4b248f32004-03-14 16:51:43 +0000273#endif
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100274
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200275 if (bmp_alloc_addr)
276 free(bmp_alloc_addr);
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100277
Simon Glasse517db72016-01-30 15:45:15 -0700278 return ret ? CMD_RET_FAILURE : 0;
wdenk059ae172003-04-20 16:52:09 +0000279}