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 <log.h>
17 #include <malloc.h>
18 #include <mapmem.h>
19 #include <splash.h>
20 #include <video.h>
21 #include <asm/byteorder.h>
22 
23 /*
24  * Allocate and decompress a BMP image using gunzip().
25  *
26  * Returns a pointer to the decompressed image data. This pointer is
27  * aligned to 32-bit-aligned-address + 2.
28  * See doc/README.displaying-bmps for explanation.
29  *
30  * The allocation address is passed to 'alloc_addr' and must be freed
31  * by the caller after use.
32  *
33  * Returns NULL if decompression failed, or if the decompressed data
34  * didn't contain a valid BMP signature or decompression is not enabled in
35  * Kconfig.
36  */
gunzip_bmp(unsigned long addr,unsigned long * lenp,void ** alloc_addr)37 struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
38 			     void **alloc_addr)
39 {
40 	void *dst;
41 	unsigned long len;
42 	struct bmp_image *bmp;
43 
44 	if (!CONFIG_IS_ENABLED(VIDEO_BMP_GZIP))
45 		return NULL;
46 
47 	/*
48 	 * Decompress bmp image
49 	 */
50 	len = CONFIG_VAL(VIDEO_LOGO_MAX_SIZE);
51 	/* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
52 	dst = malloc(CONFIG_VAL(VIDEO_LOGO_MAX_SIZE) + 3);
53 	if (!dst) {
54 		puts("Error: malloc in gunzip failed!\n");
55 		return NULL;
56 	}
57 
58 	/* align to 32-bit-aligned-address + 2 */
59 	bmp = dst + 2;
60 
61 	if (gunzip(bmp, CONFIG_VAL(VIDEO_LOGO_MAX_SIZE), map_sysmem(addr, 0),
62 		   &len)) {
63 		free(dst);
64 		return NULL;
65 	}
66 	if (len == CONFIG_VAL(VIDEO_LOGO_MAX_SIZE))
67 		puts("Image could be truncated (increase CONFIG_VIDEO_LOGO_MAX_SIZE)!\n");
68 
69 	/*
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 
78 	debug("Gzipped BMP image detected!\n");
79 
80 	*alloc_addr = dst;
81 	return bmp;
82 }
83 
84 #ifdef CONFIG_NEEDS_MANUAL_RELOC
bmp_reloc(void)85 void bmp_reloc(void)
86 {
87 	fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
88 }
89 #endif
90 
bmp_info(ulong addr)91 int bmp_info(ulong addr)
92 {
93 	struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
94 	void *bmp_alloc_addr = NULL;
95 	unsigned long len;
96 
97 	if (!((bmp->header.signature[0] == 'B') &&
98 	      (bmp->header.signature[1] == 'M')))
99 		bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
100 
101 	if (!bmp) {
102 		printf("There is no valid bmp file at the given address\n");
103 		return 1;
104 	}
105 
106 	printf("Image size    : %d x %d\n", le32_to_cpu(bmp->header.width),
107 	       le32_to_cpu(bmp->header.height));
108 	printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
109 	printf("Compression   : %d\n", le32_to_cpu(bmp->header.compression));
110 
111 	if (bmp_alloc_addr)
112 		free(bmp_alloc_addr);
113 
114 	return 0;
115 }
116 
bmp_display(ulong addr,int x,int y)117 int bmp_display(ulong addr, int x, int y)
118 {
119 	struct udevice *dev;
120 	int ret;
121 	struct bmp_image *bmp = map_sysmem(addr, 0);
122 	void *bmp_alloc_addr = NULL;
123 	unsigned long len;
124 
125 	if (!((bmp->header.signature[0] == 'B') &&
126 	      (bmp->header.signature[1] == 'M')))
127 		bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
128 
129 	if (!bmp) {
130 		printf("There is no valid bmp file at the given address\n");
131 		return 1;
132 	}
133 	addr = map_to_sysmem(bmp);
134 
135 	ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
136 	if (!ret) {
137 		bool align = false;
138 
139 		if (x == BMP_ALIGN_CENTER || y == BMP_ALIGN_CENTER)
140 			align = true;
141 
142 		ret = video_bmp_display(dev, addr, x, y, align);
143 	}
144 
145 	if (bmp_alloc_addr)
146 		free(bmp_alloc_addr);
147 
148 	return ret ? CMD_RET_FAILURE : 0;
149 }
150