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