video: Rename CONFIG_SYS_VIDEO_LOGO_MAX_SIZE
[platform/kernel/u-boot.git] / cmd / bmp.c
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
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_VIDEO_LOGO_MAX_SIZE;
52         /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
53         dst = malloc(CONFIG_VIDEO_LOGO_MAX_SIZE + 3);
54         if (!dst) {
55                 puts("Error: malloc in gunzip failed!\n");
56                 return NULL;
57         }
58
59         /* align to 32-bit-aligned-address + 2 */
60         bmp = dst + 2;
61
62         if (gunzip(bmp, CONFIG_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
63                    &len)) {
64                 free(dst);
65                 return NULL;
66         }
67         if (len == CONFIG_VIDEO_LOGO_MAX_SIZE)
68                 puts("Image could be truncated (increase CONFIG_VIDEO_LOGO_MAX_SIZE)!\n");
69
70         /*
71          * Check for bmp mark 'BM'
72          */
73         if (!((bmp->header.signature[0] == 'B') &&
74               (bmp->header.signature[1] == 'M'))) {
75                 free(dst);
76                 return NULL;
77         }
78
79         debug("Gzipped BMP image detected!\n");
80
81         *alloc_addr = dst;
82         return bmp;
83 }
84 #else
85 struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
86                              void **alloc_addr)
87 {
88         return NULL;
89 }
90 #endif
91
92 static int do_bmp_info(struct cmd_tbl *cmdtp, int flag, int argc,
93                        char *const argv[])
94 {
95         ulong addr;
96
97         switch (argc) {
98         case 1:         /* use image_load_addr as default address */
99                 addr = image_load_addr;
100                 break;
101         case 2:         /* use argument */
102                 addr = hextoul(argv[1], NULL);
103                 break;
104         default:
105                 return CMD_RET_USAGE;
106         }
107
108         return (bmp_info(addr));
109 }
110
111 static int do_bmp_display(struct cmd_tbl *cmdtp, int flag, int argc,
112                           char *const argv[])
113 {
114         ulong addr;
115         int x = 0, y = 0;
116
117         splash_get_pos(&x, &y);
118
119         switch (argc) {
120         case 1:         /* use image_load_addr as default address */
121                 addr = image_load_addr;
122                 break;
123         case 2:         /* use argument */
124                 addr = hextoul(argv[1], NULL);
125                 break;
126         case 4:
127                 addr = hextoul(argv[1], NULL);
128                 if (!strcmp(argv[2], "m"))
129                         x = BMP_ALIGN_CENTER;
130                 else
131                         x = dectoul(argv[2], NULL);
132                 if (!strcmp(argv[3], "m"))
133                         y = BMP_ALIGN_CENTER;
134                 else
135                         y = dectoul(argv[3], NULL);
136                 break;
137         default:
138                 return CMD_RET_USAGE;
139         }
140
141          return (bmp_display(addr, x, y));
142 }
143
144 static struct cmd_tbl cmd_bmp_sub[] = {
145         U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
146         U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
147 };
148
149 #ifdef CONFIG_NEEDS_MANUAL_RELOC
150 void bmp_reloc(void) {
151         fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
152 }
153 #endif
154
155 /*
156  * Subroutine:  do_bmp
157  *
158  * Description: Handler for 'bmp' command..
159  *
160  * Inputs:      argv[1] contains the subcommand
161  *
162  * Return:      None
163  *
164  */
165 static int do_bmp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
166 {
167         struct cmd_tbl *c;
168
169         /* Strip off leading 'bmp' command argument */
170         argc--;
171         argv++;
172
173         c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
174
175         if (c)
176                 return  c->cmd(cmdtp, flag, argc, argv);
177         else
178                 return CMD_RET_USAGE;
179 }
180
181 U_BOOT_CMD(
182         bmp,    5,      1,      do_bmp,
183         "manipulate BMP image data",
184         "info <imageAddr>          - display image info\n"
185         "bmp display <imageAddr> [x y] - display image at x,y"
186 );
187
188 /*
189  * Subroutine:  bmp_info
190  *
191  * Description: Show information about bmp file in memory
192  *
193  * Inputs:      addr            address of the bmp file
194  *
195  * Return:      None
196  *
197  */
198 static int bmp_info(ulong addr)
199 {
200         struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
201         void *bmp_alloc_addr = NULL;
202         unsigned long len;
203
204         if (!((bmp->header.signature[0]=='B') &&
205               (bmp->header.signature[1]=='M')))
206                 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
207
208         if (bmp == NULL) {
209                 printf("There is no valid bmp file at the given address\n");
210                 return 1;
211         }
212
213         printf("Image size    : %d x %d\n", le32_to_cpu(bmp->header.width),
214                le32_to_cpu(bmp->header.height));
215         printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
216         printf("Compression   : %d\n", le32_to_cpu(bmp->header.compression));
217
218         if (bmp_alloc_addr)
219                 free(bmp_alloc_addr);
220
221         return(0);
222 }
223
224 /*
225  * Subroutine:  bmp_display
226  *
227  * Description: Display bmp file located in memory
228  *
229  * Inputs:      addr            address of the bmp file
230  *
231  * Return:      None
232  *
233  */
234 int bmp_display(ulong addr, int x, int y)
235 {
236 #ifdef CONFIG_DM_VIDEO
237         struct udevice *dev;
238 #endif
239         int ret;
240         struct bmp_image *bmp = map_sysmem(addr, 0);
241         void *bmp_alloc_addr = NULL;
242         unsigned long len;
243
244         if (!((bmp->header.signature[0]=='B') &&
245               (bmp->header.signature[1]=='M')))
246                 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
247
248         if (!bmp) {
249                 printf("There is no valid bmp file at the given address\n");
250                 return 1;
251         }
252         addr = map_to_sysmem(bmp);
253
254 #ifdef CONFIG_DM_VIDEO
255         ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
256         if (!ret) {
257                 bool align = false;
258
259                 if (x == BMP_ALIGN_CENTER || y == BMP_ALIGN_CENTER)
260                         align = true;
261
262                 ret = video_bmp_display(dev, addr, x, y, align);
263         }
264 #elif defined(CONFIG_LCD)
265         ret = lcd_display_bitmap(addr, x, y);
266 #else
267 # error bmp_display() requires CONFIG_LCD
268 #endif
269
270         if (bmp_alloc_addr)
271                 free(bmp_alloc_addr);
272
273         return ret ? CMD_RET_FAILURE : 0;
274 }