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