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