xref: /OK3568_Linux_fs/u-boot/cmd/bmp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2002
3*4882a593Smuzhiyun  * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  * BMP handling routines
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <common.h>
13*4882a593Smuzhiyun #include <dm.h>
14*4882a593Smuzhiyun #include <lcd.h>
15*4882a593Smuzhiyun #include <mapmem.h>
16*4882a593Smuzhiyun #include <bmp_layout.h>
17*4882a593Smuzhiyun #include <command.h>
18*4882a593Smuzhiyun #include <asm/byteorder.h>
19*4882a593Smuzhiyun #include <malloc.h>
20*4882a593Smuzhiyun #include <mapmem.h>
21*4882a593Smuzhiyun #include <splash.h>
22*4882a593Smuzhiyun #include <video.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun static int bmp_info (ulong addr);
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun  * Allocate and decompress a BMP image using gunzip().
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * Returns a pointer to the decompressed image data. This pointer is
30*4882a593Smuzhiyun  * aligned to 32-bit-aligned-address + 2.
31*4882a593Smuzhiyun  * See doc/README.displaying-bmps for explanation.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * The allocation address is passed to 'alloc_addr' and must be freed
34*4882a593Smuzhiyun  * by the caller after use.
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * Returns NULL if decompression failed, or if the decompressed data
37*4882a593Smuzhiyun  * didn't contain a valid BMP signature.
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun #ifdef CONFIG_VIDEO_BMP_GZIP
gunzip_bmp(unsigned long addr,unsigned long * lenp,void ** alloc_addr)40*4882a593Smuzhiyun struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
41*4882a593Smuzhiyun 			     void **alloc_addr)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	void *dst;
44*4882a593Smuzhiyun 	unsigned long len;
45*4882a593Smuzhiyun 	struct bmp_image *bmp;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	/*
48*4882a593Smuzhiyun 	 * Decompress bmp image
49*4882a593Smuzhiyun 	 */
50*4882a593Smuzhiyun 	len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
51*4882a593Smuzhiyun 	/* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
52*4882a593Smuzhiyun 	dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3);
53*4882a593Smuzhiyun 	if (dst == NULL) {
54*4882a593Smuzhiyun 		puts("Error: malloc in gunzip failed!\n");
55*4882a593Smuzhiyun 		return NULL;
56*4882a593Smuzhiyun 	}
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	bmp = dst;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	/* align to 32-bit-aligned-address + 2 */
61*4882a593Smuzhiyun 	bmp = (struct bmp_image *)((((unsigned int)dst + 1) & ~3) + 2);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
64*4882a593Smuzhiyun 		   &len) != 0) {
65*4882a593Smuzhiyun 		free(dst);
66*4882a593Smuzhiyun 		return NULL;
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 	if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
69*4882a593Smuzhiyun 		puts("Image could be truncated"
70*4882a593Smuzhiyun 				" (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	/*
73*4882a593Smuzhiyun 	 * Check for bmp mark 'BM'
74*4882a593Smuzhiyun 	 */
75*4882a593Smuzhiyun 	if (!((bmp->header.signature[0] == 'B') &&
76*4882a593Smuzhiyun 	      (bmp->header.signature[1] == 'M'))) {
77*4882a593Smuzhiyun 		free(dst);
78*4882a593Smuzhiyun 		return NULL;
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	debug("Gzipped BMP image detected!\n");
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	*alloc_addr = dst;
84*4882a593Smuzhiyun 	return bmp;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun #else
gunzip_bmp(unsigned long addr,unsigned long * lenp,void ** alloc_addr)87*4882a593Smuzhiyun struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
88*4882a593Smuzhiyun 			     void **alloc_addr)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	return NULL;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun #endif
93*4882a593Smuzhiyun 
do_bmp_info(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])94*4882a593Smuzhiyun static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	ulong addr;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	switch (argc) {
99*4882a593Smuzhiyun 	case 1:		/* use load_addr as default address */
100*4882a593Smuzhiyun 		addr = load_addr;
101*4882a593Smuzhiyun 		break;
102*4882a593Smuzhiyun 	case 2:		/* use argument */
103*4882a593Smuzhiyun 		addr = simple_strtoul(argv[1], NULL, 16);
104*4882a593Smuzhiyun 		break;
105*4882a593Smuzhiyun 	default:
106*4882a593Smuzhiyun 		return CMD_RET_USAGE;
107*4882a593Smuzhiyun 	}
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	return (bmp_info(addr));
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
do_bmp_display(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])112*4882a593Smuzhiyun static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	ulong addr;
115*4882a593Smuzhiyun 	int x = 0, y = 0;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	splash_get_pos(&x, &y);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	switch (argc) {
120*4882a593Smuzhiyun 	case 1:		/* use load_addr as default address */
121*4882a593Smuzhiyun 		addr = load_addr;
122*4882a593Smuzhiyun 		break;
123*4882a593Smuzhiyun 	case 2:		/* use argument */
124*4882a593Smuzhiyun 		addr = simple_strtoul(argv[1], NULL, 16);
125*4882a593Smuzhiyun 		break;
126*4882a593Smuzhiyun 	case 4:
127*4882a593Smuzhiyun 		addr = simple_strtoul(argv[1], NULL, 16);
128*4882a593Smuzhiyun 		x = simple_strtoul(argv[2], NULL, 10);
129*4882a593Smuzhiyun 		y = simple_strtoul(argv[3], NULL, 10);
130*4882a593Smuzhiyun 		break;
131*4882a593Smuzhiyun 	default:
132*4882a593Smuzhiyun 		return CMD_RET_USAGE;
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	 return (bmp_display(addr, x, y));
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun static cmd_tbl_t cmd_bmp_sub[] = {
139*4882a593Smuzhiyun 	U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
140*4882a593Smuzhiyun 	U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
141*4882a593Smuzhiyun };
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun #ifdef CONFIG_NEEDS_MANUAL_RELOC
bmp_reloc(void)144*4882a593Smuzhiyun void bmp_reloc(void) {
145*4882a593Smuzhiyun 	fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun #endif
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun /*
150*4882a593Smuzhiyun  * Subroutine:  do_bmp
151*4882a593Smuzhiyun  *
152*4882a593Smuzhiyun  * Description: Handler for 'bmp' command..
153*4882a593Smuzhiyun  *
154*4882a593Smuzhiyun  * Inputs:	argv[1] contains the subcommand
155*4882a593Smuzhiyun  *
156*4882a593Smuzhiyun  * Return:      None
157*4882a593Smuzhiyun  *
158*4882a593Smuzhiyun  */
do_bmp(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])159*4882a593Smuzhiyun static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun 	cmd_tbl_t *c;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	/* Strip off leading 'bmp' command argument */
164*4882a593Smuzhiyun 	argc--;
165*4882a593Smuzhiyun 	argv++;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	if (c)
170*4882a593Smuzhiyun 		return  c->cmd(cmdtp, flag, argc, argv);
171*4882a593Smuzhiyun 	else
172*4882a593Smuzhiyun 		return CMD_RET_USAGE;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun U_BOOT_CMD(
176*4882a593Smuzhiyun 	bmp,	5,	1,	do_bmp,
177*4882a593Smuzhiyun 	"manipulate BMP image data",
178*4882a593Smuzhiyun 	"info <imageAddr>          - display image info\n"
179*4882a593Smuzhiyun 	"bmp display <imageAddr> [x y] - display image at x,y"
180*4882a593Smuzhiyun );
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun  * Subroutine:  bmp_info
184*4882a593Smuzhiyun  *
185*4882a593Smuzhiyun  * Description: Show information about bmp file in memory
186*4882a593Smuzhiyun  *
187*4882a593Smuzhiyun  * Inputs:	addr		address of the bmp file
188*4882a593Smuzhiyun  *
189*4882a593Smuzhiyun  * Return:      None
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  */
bmp_info(ulong addr)192*4882a593Smuzhiyun static int bmp_info(ulong addr)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
195*4882a593Smuzhiyun 	void *bmp_alloc_addr = NULL;
196*4882a593Smuzhiyun 	unsigned long len;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	if (!((bmp->header.signature[0]=='B') &&
199*4882a593Smuzhiyun 	      (bmp->header.signature[1]=='M')))
200*4882a593Smuzhiyun 		bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	if (bmp == NULL) {
203*4882a593Smuzhiyun 		printf("There is no valid bmp file at the given address\n");
204*4882a593Smuzhiyun 		return 1;
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	printf("Image size    : %d x %d\n", le32_to_cpu(bmp->header.width),
208*4882a593Smuzhiyun 	       le32_to_cpu(bmp->header.height));
209*4882a593Smuzhiyun 	printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
210*4882a593Smuzhiyun 	printf("Compression   : %d\n", le32_to_cpu(bmp->header.compression));
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	if (bmp_alloc_addr)
213*4882a593Smuzhiyun 		free(bmp_alloc_addr);
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	return(0);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /*
219*4882a593Smuzhiyun  * Subroutine:  bmp_display
220*4882a593Smuzhiyun  *
221*4882a593Smuzhiyun  * Description: Display bmp file located in memory
222*4882a593Smuzhiyun  *
223*4882a593Smuzhiyun  * Inputs:	addr		address of the bmp file
224*4882a593Smuzhiyun  *
225*4882a593Smuzhiyun  * Return:      None
226*4882a593Smuzhiyun  *
227*4882a593Smuzhiyun  */
bmp_display(ulong addr,int x,int y)228*4882a593Smuzhiyun int bmp_display(ulong addr, int x, int y)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun #ifdef CONFIG_DM_VIDEO
231*4882a593Smuzhiyun 	struct udevice *dev;
232*4882a593Smuzhiyun #endif
233*4882a593Smuzhiyun 	int ret;
234*4882a593Smuzhiyun 	struct bmp_image *bmp = map_sysmem(addr, 0);
235*4882a593Smuzhiyun 	void *bmp_alloc_addr = NULL;
236*4882a593Smuzhiyun 	unsigned long len;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	if (!((bmp->header.signature[0]=='B') &&
239*4882a593Smuzhiyun 	      (bmp->header.signature[1]=='M')))
240*4882a593Smuzhiyun 		bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	if (!bmp) {
243*4882a593Smuzhiyun 		printf("There is no valid bmp file at the given address\n");
244*4882a593Smuzhiyun 		return 1;
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 	addr = map_to_sysmem(bmp);
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun #ifdef CONFIG_DM_VIDEO
249*4882a593Smuzhiyun 	ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
250*4882a593Smuzhiyun 	if (!ret) {
251*4882a593Smuzhiyun 		bool align = false;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun # ifdef CONFIG_SPLASH_SCREEN_ALIGN
254*4882a593Smuzhiyun 		align = true;
255*4882a593Smuzhiyun # endif /* CONFIG_SPLASH_SCREEN_ALIGN */
256*4882a593Smuzhiyun 		ret = video_bmp_display(dev, addr, x, y, align);
257*4882a593Smuzhiyun 	}
258*4882a593Smuzhiyun #elif defined(CONFIG_LCD)
259*4882a593Smuzhiyun 	ret = lcd_display_bitmap(addr, x, y);
260*4882a593Smuzhiyun #elif defined(CONFIG_VIDEO)
261*4882a593Smuzhiyun 	ret = video_display_bitmap(addr, x, y);
262*4882a593Smuzhiyun #else
263*4882a593Smuzhiyun # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
264*4882a593Smuzhiyun #endif
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	if (bmp_alloc_addr)
267*4882a593Smuzhiyun 		free(bmp_alloc_addr);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	return ret ? CMD_RET_FAILURE : 0;
270*4882a593Smuzhiyun }
271