lcd: align bmp header when uncopmressing image
[oweals/u-boot.git] / common / cmd_bmp.c
1 /*
2  * (C) Copyright 2002
3  * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * BMP handling routines
26  */
27
28 #include <common.h>
29 #include <lcd.h>
30 #include <bmp_layout.h>
31 #include <command.h>
32 #include <asm/byteorder.h>
33 #include <malloc.h>
34 #include <video.h>
35
36 static int bmp_info (ulong addr);
37
38 /*
39  * Allocate and decompress a BMP image using gunzip().
40  *
41  * Returns a pointer to the decompressed image data. This pointer is
42  * aligned to 32-bit-aligned-address + 2.
43  * See doc/README.displaying-bmps for explanation.
44  *
45  * The allocation address is passed to 'alloc_addr' and must be freed
46  * by the caller after use.
47  *
48  * Returns NULL if decompression failed, or if the decompressed data
49  * didn't contain a valid BMP signature.
50  */
51 #ifdef CONFIG_VIDEO_BMP_GZIP
52 bmp_image_t *gunzip_bmp(unsigned long addr, unsigned long *lenp,
53                         void **alloc_addr)
54 {
55         void *dst;
56         unsigned long len;
57         bmp_image_t *bmp;
58
59         /*
60          * Decompress bmp image
61          */
62         len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
63         /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
64         dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3);
65         if (dst == NULL) {
66                 puts("Error: malloc in gunzip failed!\n");
67                 return NULL;
68         }
69
70         bmp = dst;
71
72         /* align to 32-bit-aligned-address + 2 */
73         bmp = (bmp_image_t *)((((unsigned int)dst + 1) & ~3) + 2);
74
75         if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, (uchar *)addr, &len) != 0) {
76                 free(dst);
77                 return NULL;
78         }
79         if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
80                 puts("Image could be truncated"
81                                 " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
82
83         /*
84          * Check for bmp mark 'BM'
85          */
86         if (!((bmp->header.signature[0] == 'B') &&
87               (bmp->header.signature[1] == 'M'))) {
88                 free(dst);
89                 return NULL;
90         }
91
92         debug("Gzipped BMP image detected!\n");
93
94         *alloc_addr = dst;
95         return bmp;
96 }
97 #else
98 bmp_image_t *gunzip_bmp(unsigned long addr, unsigned long *lenp,
99                         void **alloc_addr)
100 {
101         return NULL;
102 }
103 #endif
104
105 static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
106 {
107         ulong addr;
108
109         switch (argc) {
110         case 1:         /* use load_addr as default address */
111                 addr = load_addr;
112                 break;
113         case 2:         /* use argument */
114                 addr = simple_strtoul(argv[1], NULL, 16);
115                 break;
116         default:
117                 return CMD_RET_USAGE;
118         }
119
120         return (bmp_info(addr));
121 }
122
123 static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
124 {
125         ulong addr;
126         int x = 0, y = 0;
127
128         switch (argc) {
129         case 1:         /* use load_addr as default address */
130                 addr = load_addr;
131                 break;
132         case 2:         /* use argument */
133                 addr = simple_strtoul(argv[1], NULL, 16);
134                 break;
135         case 4:
136                 addr = simple_strtoul(argv[1], NULL, 16);
137                 x = simple_strtoul(argv[2], NULL, 10);
138                 y = simple_strtoul(argv[3], NULL, 10);
139                 break;
140         default:
141                 return CMD_RET_USAGE;
142         }
143
144          return (bmp_display(addr, x, y));
145 }
146
147 static cmd_tbl_t cmd_bmp_sub[] = {
148         U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
149         U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
150 };
151
152 #ifdef CONFIG_NEEDS_MANUAL_RELOC
153 void bmp_reloc(void) {
154         fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
155 }
156 #endif
157
158 /*
159  * Subroutine:  do_bmp
160  *
161  * Description: Handler for 'bmp' command..
162  *
163  * Inputs:      argv[1] contains the subcommand
164  *
165  * Return:      None
166  *
167  */
168 static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
169 {
170         cmd_tbl_t *c;
171
172         /* Strip off leading 'bmp' command argument */
173         argc--;
174         argv++;
175
176         c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
177
178         if (c)
179                 return  c->cmd(cmdtp, flag, argc, argv);
180         else
181                 return CMD_RET_USAGE;
182 }
183
184 U_BOOT_CMD(
185         bmp,    5,      1,      do_bmp,
186         "manipulate BMP image data",
187         "info <imageAddr>          - display image info\n"
188         "bmp display <imageAddr> [x y] - display image at x,y"
189 );
190
191 /*
192  * Subroutine:  bmp_info
193  *
194  * Description: Show information about bmp file in memory
195  *
196  * Inputs:      addr            address of the bmp file
197  *
198  * Return:      None
199  *
200  */
201 static int bmp_info(ulong addr)
202 {
203         bmp_image_t *bmp=(bmp_image_t *)addr;
204         void *bmp_alloc_addr = NULL;
205         unsigned long len;
206
207         if (!((bmp->header.signature[0]=='B') &&
208               (bmp->header.signature[1]=='M')))
209                 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
210
211         if (bmp == NULL) {
212                 printf("There is no valid bmp file at the given address\n");
213                 return 1;
214         }
215
216         printf("Image size    : %d x %d\n", le32_to_cpu(bmp->header.width),
217                le32_to_cpu(bmp->header.height));
218         printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
219         printf("Compression   : %d\n", le32_to_cpu(bmp->header.compression));
220
221         if (bmp_alloc_addr)
222                 free(bmp_alloc_addr);
223
224         return(0);
225 }
226
227 /*
228  * Subroutine:  bmp_display
229  *
230  * Description: Display bmp file located in memory
231  *
232  * Inputs:      addr            address of the bmp file
233  *
234  * Return:      None
235  *
236  */
237 int bmp_display(ulong addr, int x, int y)
238 {
239         int ret;
240         bmp_image_t *bmp = (bmp_image_t *)addr;
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
253 #if defined(CONFIG_LCD)
254         ret = lcd_display_bitmap((ulong)bmp, x, y);
255 #elif defined(CONFIG_VIDEO)
256         ret = video_display_bitmap((unsigned long)bmp, x, y);
257 #else
258 # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
259 #endif
260
261         if (bmp_alloc_addr)
262                 free(bmp_alloc_addr);
263
264         return ret;
265 }