blob: 46d0d916e86f06eef38a4bd79b1064a045ec2d91 [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 Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Stefan Roesec29ab9d2005-10-08 10:19:07 +020018#include <malloc.h>
Simon Glass72b335e2016-01-05 09:30:58 -070019#include <mapmem.h>
Anatolij Gustschinff8fb562013-07-02 00:04:05 +020020#include <splash.h>
Stefan Reinauerf674f7c2012-09-28 15:11:11 +000021#include <video.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060022#include <asm/byteorder.h>
wdenk059ae172003-04-20 16:52:09 +000023
wdenk059ae172003-04-20 16:52:09 +000024static int bmp_info (ulong addr);
wdenk059ae172003-04-20 16:52:09 +000025
26/*
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010027 * Allocate and decompress a BMP image using gunzip().
28 *
Piotr Wilczekf7ef9d62013-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 Egtvedt43ef1c32007-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 Glass1c3dbe52015-05-13 07:02:27 -060040struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
41 void **alloc_addr)
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010042{
43 void *dst;
44 unsigned long len;
Simon Glass1c3dbe52015-05-13 07:02:27 -060045 struct bmp_image *bmp;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010046
47 /*
48 * Decompress bmp image
49 */
Simon Glasse6550032022-10-18 06:46:08 -060050 len = CONFIG_VIDEO_LOGO_MAX_SIZE;
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020051 /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
Simon Glasse6550032022-10-18 06:46:08 -060052 dst = malloc(CONFIG_VIDEO_LOGO_MAX_SIZE + 3);
53 if (!dst) {
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010054 puts("Error: malloc in gunzip failed!\n");
55 return NULL;
56 }
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020057
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020058 /* align to 32-bit-aligned-address + 2 */
Simon Glasse6550032022-10-18 06:46:08 -060059 bmp = dst + 2;
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020060
Simon Glasse6550032022-10-18 06:46:08 -060061 if (gunzip(bmp, CONFIG_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
62 &len)) {
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010063 free(dst);
64 return NULL;
65 }
Simon Glasse6550032022-10-18 06:46:08 -060066 if (len == CONFIG_VIDEO_LOGO_MAX_SIZE)
67 puts("Image could be truncated (increase CONFIG_VIDEO_LOGO_MAX_SIZE)!\n");
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010068
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010069 /*
70 * Check for bmp mark 'BM'
71 */
72 if (!((bmp->header.signature[0] == 'B') &&
73 (bmp->header.signature[1] == 'M'))) {
74 free(dst);
75 return NULL;
76 }
77
Wolfgang Denk17f79e42011-02-09 15:11:10 +010078 debug("Gzipped BMP image detected!\n");
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010079
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020080 *alloc_addr = dst;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010081 return bmp;
82}
83#else
Simon Glass1c3dbe52015-05-13 07:02:27 -060084struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
85 void **alloc_addr)
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010086{
87 return NULL;
88}
89#endif
90
Simon Glass09140112020-05-10 11:40:03 -060091static int do_bmp_info(struct cmd_tbl *cmdtp, int flag, int argc,
92 char *const argv[])
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +010093{
94 ulong addr;
95
96 switch (argc) {
Simon Glassbb872dd2019-12-28 10:45:02 -070097 case 1: /* use image_load_addr as default address */
98 addr = image_load_addr;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +010099 break;
100 case 2: /* use argument */
Simon Glass7e5f4602021-07-24 09:03:29 -0600101 addr = hextoul(argv[1], NULL);
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100102 break;
103 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000104 return CMD_RET_USAGE;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100105 }
106
107 return (bmp_info(addr));
108}
109
Simon Glass09140112020-05-10 11:40:03 -0600110static int do_bmp_display(struct cmd_tbl *cmdtp, int flag, int argc,
111 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) {
Simon Glassbb872dd2019-12-28 10:45:02 -0700119 case 1: /* use image_load_addr as default address */
120 addr = image_load_addr;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100121 break;
122 case 2: /* use argument */
Simon Glass7e5f4602021-07-24 09:03:29 -0600123 addr = hextoul(argv[1], NULL);
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100124 break;
125 case 4:
Simon Glass7e5f4602021-07-24 09:03:29 -0600126 addr = hextoul(argv[1], NULL);
Patrick Delaunayb0fcedb2018-12-01 14:41:07 +0100127 if (!strcmp(argv[2], "m"))
128 x = BMP_ALIGN_CENTER;
129 else
Simon Glass0b1284e2021-07-24 09:03:30 -0600130 x = dectoul(argv[2], NULL);
Patrick Delaunayb0fcedb2018-12-01 14:41:07 +0100131 if (!strcmp(argv[3], "m"))
132 y = BMP_ALIGN_CENTER;
133 else
Simon Glass0b1284e2021-07-24 09:03:30 -0600134 y = dectoul(argv[3], NULL);
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
Simon Glass09140112020-05-10 11:40:03 -0600143static struct cmd_tbl cmd_bmp_sub[] = {
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100144 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 */
Simon Glass09140112020-05-10 11:40:03 -0600164static int do_bmp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenk059ae172003-04-20 16:52:09 +0000165{
Simon Glass09140112020-05-10 11:40:03 -0600166 struct cmd_tbl *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
Anatolij Gustschinde3b49c2012-04-27 04:38:06 +0000223int bmp_display(ulong addr, int x, int y)
wdenk059ae172003-04-20 16:52:09 +0000224{
Simon Glassb01c7922016-01-18 19:52:22 -0700225 struct udevice *dev;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100226 int ret;
Simon Glass72b335e2016-01-05 09:30:58 -0700227 struct bmp_image *bmp = map_sysmem(addr, 0);
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200228 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100229 unsigned long len;
230
231 if (!((bmp->header.signature[0]=='B') &&
232 (bmp->header.signature[1]=='M')))
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200233 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Hans-Christian Egtvedt43ef1c32007-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 }
Simon Glass72b335e2016-01-05 09:30:58 -0700239 addr = map_to_sysmem(bmp);
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100240
Simon Glass3f603cb2016-02-11 13:23:26 -0700241 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
Simon Glassb01c7922016-01-18 19:52:22 -0700242 if (!ret) {
Simon Glass3f603cb2016-02-11 13:23:26 -0700243 bool align = false;
Simon Glassb01c7922016-01-18 19:52:22 -0700244
Simon Glass988d19d2022-10-18 06:24:16 -0600245 if (x == BMP_ALIGN_CENTER || y == BMP_ALIGN_CENTER)
Patrick Delaunayb0fcedb2018-12-01 14:41:07 +0100246 align = true;
247
Simon Glass3f603cb2016-02-11 13:23:26 -0700248 ret = video_bmp_display(dev, addr, x, y, align);
Simon Glassb01c7922016-01-18 19:52:22 -0700249 }
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100250
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200251 if (bmp_alloc_addr)
252 free(bmp_alloc_addr);
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100253
Simon Glasse517db72016-01-30 15:45:15 -0700254 return ret ? CMD_RET_FAILURE : 0;
wdenk059ae172003-04-20 16:52:09 +0000255}