1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2002
4  * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
5  */
6 
7 /*
8  * BMP handling routines
9  */
10 
11 #include <common.h>
12 #include <bmp_layout.h>
13 #include <command.h>
14 #include <dm.h>
15 #include <gzip.h>
16 #include <image.h>
17 #include <lcd.h>
18 #include <log.h>
19 #include <malloc.h>
20 #include <mapmem.h>
21 #include <splash.h>
22 #include <video.h>
23 #include <asm/byteorder.h>
24 
25 static int bmp_info (ulong addr);
26 
27 /*
28  * Allocate and decompress a BMP image using gunzip().
29  *
30  * 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.
36  *
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
gunzip_bmp(unsigned long addr,unsigned long * lenp,void ** alloc_addr)41 struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
42 			     void **alloc_addr)
43 {
44 	void *dst;
45 	unsigned long len;
46 	struct bmp_image *bmp;
47 
48 	/*
49 	 * Decompress bmp image
50 	 */
51 	len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
52 	/* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
53 	dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3);
54 	if (dst == NULL) {
55 		puts("Error: malloc in gunzip failed!\n");
56 		return NULL;
57 	}
58 
59 	bmp = dst;
60 
61 	/* align to 32-bit-aligned-address + 2 */
62 	bmp = (struct bmp_image *)((((uintptr_t)dst + 1) & ~3) + 2);
63 
64 	if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
65 		   &len) != 0) {
66 		free(dst);
67 		return NULL;
68 	}
69 	if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
70 		puts("Image could be truncated"
71 				" (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
72 
73 	/*
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 
82 	debug("Gzipped BMP image detected!\n");
83 
84 	*alloc_addr = dst;
85 	return bmp;
86 }
87 #else
gunzip_bmp(unsigned long addr,unsigned long * lenp,void ** alloc_addr)88 struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
89 			     void **alloc_addr)
90 {
91 	return NULL;
92 }
93 #endif
94 
do_bmp_info(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])95 static int do_bmp_info(struct cmd_tbl *cmdtp, int flag, int argc,
96 		       char *const argv[])
97 {
98 	ulong addr;
99 
100 	switch (argc) {
101 	case 1:		/* use image_load_addr as default address */
102 		addr = image_load_addr;
103 		break;
104 	case 2:		/* use argument */
105 		addr = simple_strtoul(argv[1], NULL, 16);
106 		break;
107 	default:
108 		return CMD_RET_USAGE;
109 	}
110 
111 	return (bmp_info(addr));
112 }
113 
do_bmp_display(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])114 static int do_bmp_display(struct cmd_tbl *cmdtp, int flag, int argc,
115 			  char *const argv[])
116 {
117 	ulong addr;
118 	int x = 0, y = 0;
119 
120 	splash_get_pos(&x, &y);
121 
122 	switch (argc) {
123 	case 1:		/* use image_load_addr as default address */
124 		addr = image_load_addr;
125 		break;
126 	case 2:		/* use argument */
127 		addr = simple_strtoul(argv[1], NULL, 16);
128 		break;
129 	case 4:
130 		addr = simple_strtoul(argv[1], NULL, 16);
131 		if (!strcmp(argv[2], "m"))
132 			x = BMP_ALIGN_CENTER;
133 		else
134 			x = simple_strtoul(argv[2], NULL, 10);
135 		if (!strcmp(argv[3], "m"))
136 			y = BMP_ALIGN_CENTER;
137 		else
138 			y = simple_strtoul(argv[3], NULL, 10);
139 		break;
140 	default:
141 		return CMD_RET_USAGE;
142 	}
143 
144 	 return (bmp_display(addr, x, y));
145 }
146 
147 static struct cmd_tbl cmd_bmp_sub[] = {
148 	U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
149 	U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
150 };
151 
152 #ifdef CONFIG_NEEDS_MANUAL_RELOC
bmp_reloc(void)153 void bmp_reloc(void) {
154 	fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
155 }
156 #endif
157 
158 /*
159  * Subroutine:  do_bmp
160  *
161  * Description: Handler for 'bmp' command..
162  *
163  * Inputs:	argv[1] contains the subcommand
164  *
165  * Return:      None
166  *
167  */
do_bmp(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])168 static int do_bmp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
169 {
170 	struct cmd_tbl *c;
171 
172 	/* Strip off leading 'bmp' command argument */
173 	argc--;
174 	argv++;
175 
176 	c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
177 
178 	if (c)
179 		return  c->cmd(cmdtp, flag, argc, argv);
180 	else
181 		return CMD_RET_USAGE;
182 }
183 
184 U_BOOT_CMD(
185 	bmp,	5,	1,	do_bmp,
186 	"manipulate BMP image data",
187 	"info <imageAddr>          - display image info\n"
188 	"bmp display <imageAddr> [x y] - display image at x,y"
189 );
190 
191 /*
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  */
bmp_info(ulong addr)201 static int bmp_info(ulong addr)
202 {
203 	struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
204 	void *bmp_alloc_addr = NULL;
205 	unsigned long len;
206 
207 	if (!((bmp->header.signature[0]=='B') &&
208 	      (bmp->header.signature[1]=='M')))
209 		bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
210 
211 	if (bmp == NULL) {
212 		printf("There is no valid bmp file at the given address\n");
213 		return 1;
214 	}
215 
216 	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));
220 
221 	if (bmp_alloc_addr)
222 		free(bmp_alloc_addr);
223 
224 	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  */
bmp_display(ulong addr,int x,int y)237 int bmp_display(ulong addr, int x, int y)
238 {
239 #ifdef CONFIG_DM_VIDEO
240 	struct udevice *dev;
241 #endif
242 	int ret;
243 	struct bmp_image *bmp = map_sysmem(addr, 0);
244 	void *bmp_alloc_addr = NULL;
245 	unsigned long len;
246 
247 	if (!((bmp->header.signature[0]=='B') &&
248 	      (bmp->header.signature[1]=='M')))
249 		bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
250 
251 	if (!bmp) {
252 		printf("There is no valid bmp file at the given address\n");
253 		return 1;
254 	}
255 	addr = map_to_sysmem(bmp);
256 
257 #ifdef CONFIG_DM_VIDEO
258 	ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
259 	if (!ret) {
260 		bool align = false;
261 
262 		if (CONFIG_IS_ENABLED(SPLASH_SCREEN_ALIGN) ||
263 		    x == BMP_ALIGN_CENTER ||
264 		    y == BMP_ALIGN_CENTER)
265 			align = true;
266 
267 		ret = video_bmp_display(dev, addr, x, y, align);
268 	}
269 #elif defined(CONFIG_LCD)
270 	ret = lcd_display_bitmap(addr, x, y);
271 #elif defined(CONFIG_VIDEO)
272 	ret = video_display_bitmap(addr, x, y);
273 #else
274 # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
275 #endif
276 
277 	if (bmp_alloc_addr)
278 		free(bmp_alloc_addr);
279 
280 	return ret ? CMD_RET_FAILURE : 0;
281 }
282