blob: fb6fe3f286c5434f1cd15eba5ee28b8f4166fbd4 [file] [log] [blame]
wdenk059ae172003-04-20 16:52:09 +00001/*
2 * (C) Copyright 2002
wdenkb37c7e52003-06-30 16:24:52 +00003 * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
wdenk059ae172003-04-20 16:52:09 +00004 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk059ae172003-04-20 16:52:09 +00006 */
7
8/*
9 * BMP handling routines
10 */
11
12#include <common.h>
Simon Glassb01c7922016-01-18 19:52:22 -070013#include <dm.h>
Alessandro Rubini61117222009-07-19 17:52:27 +020014#include <lcd.h>
Simon Glassb01c7922016-01-18 19:52:22 -070015#include <mapmem.h>
wdenk059ae172003-04-20 16:52:09 +000016#include <bmp_layout.h>
17#include <command.h>
wdenk8655b6f2004-10-09 23:25:58 +000018#include <asm/byteorder.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>
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 */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020050 len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020051 /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
52 dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3);
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010053 if (dst == NULL) {
54 puts("Error: malloc in gunzip failed!\n");
55 return NULL;
56 }
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020057
58 bmp = dst;
59
60 /* align to 32-bit-aligned-address + 2 */
Simon Glass1c3dbe52015-05-13 07:02:27 -060061 bmp = (struct bmp_image *)((((unsigned int)dst + 1) & ~3) + 2);
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020062
Simon Glass72b335e2016-01-05 09:30:58 -070063 if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
64 &len) != 0) {
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010065 free(dst);
66 return NULL;
67 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020068 if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010069 puts("Image could be truncated"
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020070 " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010071
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010072 /*
73 * Check for bmp mark 'BM'
74 */
75 if (!((bmp->header.signature[0] == 'B') &&
76 (bmp->header.signature[1] == 'M'))) {
77 free(dst);
78 return NULL;
79 }
80
Wolfgang Denk17f79e42011-02-09 15:11:10 +010081 debug("Gzipped BMP image detected!\n");
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010082
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +020083 *alloc_addr = dst;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010084 return bmp;
85}
86#else
Simon Glass1c3dbe52015-05-13 07:02:27 -060087struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
88 void **alloc_addr)
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +010089{
90 return NULL;
91}
92#endif
93
Wolfgang Denk54841ab2010-06-28 22:00:46 +020094static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +010095{
96 ulong addr;
97
98 switch (argc) {
99 case 1: /* use load_addr as default address */
100 addr = load_addr;
101 break;
102 case 2: /* use argument */
103 addr = simple_strtoul(argv[1], NULL, 16);
104 break;
105 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000106 return CMD_RET_USAGE;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100107 }
108
109 return (bmp_info(addr));
110}
111
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200112static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100113{
114 ulong addr;
115 int x = 0, y = 0;
116
Anatolij Gustschinff8fb562013-07-02 00:04:05 +0200117 splash_get_pos(&x, &y);
118
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100119 switch (argc) {
120 case 1: /* use load_addr as default address */
121 addr = load_addr;
122 break;
123 case 2: /* use argument */
124 addr = simple_strtoul(argv[1], NULL, 16);
125 break;
126 case 4:
127 addr = simple_strtoul(argv[1], NULL, 16);
Wolfgang Denk93e14592013-10-04 17:43:24 +0200128 x = simple_strtoul(argv[2], NULL, 10);
129 y = simple_strtoul(argv[3], NULL, 10);
130 break;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100131 default:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000132 return CMD_RET_USAGE;
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100133 }
134
135 return (bmp_display(addr, x, y));
136}
137
138static cmd_tbl_t cmd_bmp_sub[] = {
139 U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
140 U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
141};
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100142
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200143#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200144void bmp_reloc(void) {
145 fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
146}
147#endif
148
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100149/*
wdenk059ae172003-04-20 16:52:09 +0000150 * Subroutine: do_bmp
151 *
152 * Description: Handler for 'bmp' command..
153 *
154 * Inputs: argv[1] contains the subcommand
155 *
156 * Return: None
157 *
158 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200159static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk059ae172003-04-20 16:52:09 +0000160{
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100161 cmd_tbl_t *c;
wdenk059ae172003-04-20 16:52:09 +0000162
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100163 /* Strip off leading 'bmp' command argument */
164 argc--;
165 argv++;
wdenk059ae172003-04-20 16:52:09 +0000166
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100167 c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
168
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200169 if (c)
Frans Meulenbroeks9acd4f02010-03-27 11:16:10 +0100170 return c->cmd(cmdtp, flag, argc, argv);
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200171 else
Simon Glass4c12eeb2011-12-10 08:44:01 +0000172 return CMD_RET_USAGE;
wdenk059ae172003-04-20 16:52:09 +0000173}
174
wdenk0d498392003-07-01 21:06:45 +0000175U_BOOT_CMD(
wdenk4b248f32004-03-14 16:51:43 +0000176 bmp, 5, 1, do_bmp,
Peter Tyser2fb26042009-01-27 18:03:12 -0600177 "manipulate BMP image data",
wdenk4b248f32004-03-14 16:51:43 +0000178 "info <imageAddr> - display image info\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200179 "bmp display <imageAddr> [x y] - display image at x,y"
wdenkb0fce992003-06-29 21:03:46 +0000180);
181
wdenk059ae172003-04-20 16:52:09 +0000182/*
183 * Subroutine: bmp_info
184 *
185 * Description: Show information about bmp file in memory
186 *
187 * Inputs: addr address of the bmp file
188 *
189 * Return: None
190 *
191 */
192static int bmp_info(ulong addr)
193{
Simon Glass72b335e2016-01-05 09:30:58 -0700194 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200195 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100196 unsigned long len;
Stefan Roesec29ab9d2005-10-08 10:19:07 +0200197
wdenk059ae172003-04-20 16:52:09 +0000198 if (!((bmp->header.signature[0]=='B') &&
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100199 (bmp->header.signature[1]=='M')))
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200200 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Stefan Roesec29ab9d2005-10-08 10:19:07 +0200201
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100202 if (bmp == NULL) {
wdenk059ae172003-04-20 16:52:09 +0000203 printf("There is no valid bmp file at the given address\n");
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100204 return 1;
wdenk059ae172003-04-20 16:52:09 +0000205 }
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100206
wdenk059ae172003-04-20 16:52:09 +0000207 printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
208 le32_to_cpu(bmp->header.height));
209 printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
210 printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
Stefan Roesec29ab9d2005-10-08 10:19:07 +0200211
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200212 if (bmp_alloc_addr)
213 free(bmp_alloc_addr);
Stefan Roesec29ab9d2005-10-08 10:19:07 +0200214
wdenk059ae172003-04-20 16:52:09 +0000215 return(0);
216}
217
218/*
219 * Subroutine: bmp_display
220 *
221 * Description: Display bmp file located in memory
222 *
223 * Inputs: addr address of the bmp file
224 *
225 * Return: None
226 *
227 */
Anatolij Gustschinde3b49c2012-04-27 04:38:06 +0000228int bmp_display(ulong addr, int x, int y)
wdenk059ae172003-04-20 16:52:09 +0000229{
Simon Glassb01c7922016-01-18 19:52:22 -0700230#ifdef CONFIG_DM_VIDEO
231 struct udevice *dev;
232#endif
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100233 int ret;
Simon Glass72b335e2016-01-05 09:30:58 -0700234 struct bmp_image *bmp = map_sysmem(addr, 0);
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200235 void *bmp_alloc_addr = NULL;
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100236 unsigned long len;
237
238 if (!((bmp->header.signature[0]=='B') &&
239 (bmp->header.signature[1]=='M')))
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200240 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100241
242 if (!bmp) {
243 printf("There is no valid bmp file at the given address\n");
244 return 1;
245 }
Simon Glass72b335e2016-01-05 09:30:58 -0700246 addr = map_to_sysmem(bmp);
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100247
Simon Glassb01c7922016-01-18 19:52:22 -0700248#ifdef CONFIG_DM_VIDEO
Simon Glass3f603cb2016-02-11 13:23:26 -0700249 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
Simon Glassb01c7922016-01-18 19:52:22 -0700250 if (!ret) {
Simon Glass3f603cb2016-02-11 13:23:26 -0700251 bool align = false;
Simon Glassb01c7922016-01-18 19:52:22 -0700252
253# ifdef CONFIG_SPLASH_SCREEN_ALIGN
Simon Glass3f603cb2016-02-11 13:23:26 -0700254 align = true;
Simon Glassb01c7922016-01-18 19:52:22 -0700255# endif /* CONFIG_SPLASH_SCREEN_ALIGN */
Simon Glass3f603cb2016-02-11 13:23:26 -0700256 ret = video_bmp_display(dev, addr, x, y, align);
Simon Glassb01c7922016-01-18 19:52:22 -0700257 }
Simon Glassb01c7922016-01-18 19:52:22 -0700258#elif defined(CONFIG_LCD)
Simon Glass72b335e2016-01-05 09:30:58 -0700259 ret = lcd_display_bitmap(addr, x, y);
wdenk281e00a2004-08-01 22:48:16 +0000260#elif defined(CONFIG_VIDEO)
Simon Glass72b335e2016-01-05 09:30:58 -0700261 ret = video_display_bitmap(addr, x, y);
wdenk281e00a2004-08-01 22:48:16 +0000262#else
263# error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
wdenk4b248f32004-03-14 16:51:43 +0000264#endif
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100265
Piotr Wilczekf7ef9d62013-06-05 08:14:30 +0200266 if (bmp_alloc_addr)
267 free(bmp_alloc_addr);
Hans-Christian Egtvedt43ef1c32007-11-30 17:29:59 +0100268
Simon Glasse517db72016-01-30 15:45:15 -0700269 return ret ? CMD_RET_FAILURE : 0;
wdenk059ae172003-04-20 16:52:09 +0000270}