]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
059ae173 WD |
2 | /* |
3 | * (C) Copyright 2002 | |
b37c7e5e | 4 | * Detlev Zundel, DENX Software Engineering, [email protected]. |
059ae173 WD |
5 | */ |
6 | ||
7 | /* | |
8 | * BMP handling routines | |
9 | */ | |
10 | ||
11 | #include <common.h> | |
12 | #include <bmp_layout.h> | |
13 | #include <command.h> | |
0c670fc1 SG |
14 | #include <dm.h> |
15 | #include <gzip.h> | |
8e8ccfe1 | 16 | #include <image.h> |
0c670fc1 | 17 | #include <lcd.h> |
f7ae49fc | 18 | #include <log.h> |
c29ab9d7 | 19 | #include <malloc.h> |
72b335e9 | 20 | #include <mapmem.h> |
ff8fb56b | 21 | #include <splash.h> |
f674f7cf | 22 | #include <video.h> |
0c670fc1 | 23 | #include <asm/byteorder.h> |
059ae173 | 24 | |
059ae173 | 25 | static int bmp_info (ulong addr); |
059ae173 | 26 | |
43ef1c38 HCE |
27 | /* |
28 | * Allocate and decompress a BMP image using gunzip(). | |
29 | * | |
f7ef9d61 PW |
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. | |
43ef1c38 HCE |
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 | |
1c3dbe56 SG |
41 | struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp, |
42 | void **alloc_addr) | |
43ef1c38 HCE |
43 | { |
44 | void *dst; | |
45 | unsigned long len; | |
1c3dbe56 | 46 | struct bmp_image *bmp; |
43ef1c38 HCE |
47 | |
48 | /* | |
49 | * Decompress bmp image | |
50 | */ | |
6d0f6bcf | 51 | len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE; |
f7ef9d61 PW |
52 | /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */ |
53 | dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3); | |
43ef1c38 HCE |
54 | if (dst == NULL) { |
55 | puts("Error: malloc in gunzip failed!\n"); | |
56 | return NULL; | |
57 | } | |
f7ef9d61 PW |
58 | |
59 | bmp = dst; | |
60 | ||
61 | /* align to 32-bit-aligned-address + 2 */ | |
77195216 | 62 | bmp = (struct bmp_image *)((((uintptr_t)dst + 1) & ~3) + 2); |
f7ef9d61 | 63 | |
72b335e9 SG |
64 | if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0), |
65 | &len) != 0) { | |
43ef1c38 HCE |
66 | free(dst); |
67 | return NULL; | |
68 | } | |
6d0f6bcf | 69 | if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE) |
43ef1c38 | 70 | puts("Image could be truncated" |
6d0f6bcf | 71 | " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n"); |
43ef1c38 | 72 | |
43ef1c38 HCE |
73 | /* |
74 | * Check for bmp mark 'BM' | |
75 | */ | |
76 | if (!((bmp->header.signature[0] == 'B') && | |
77 | (bmp->header.signature[1] == 'M'))) { | |
78 | free(dst); | |
79 | return NULL; | |
80 | } | |
81 | ||
17f79e45 | 82 | debug("Gzipped BMP image detected!\n"); |
43ef1c38 | 83 | |
f7ef9d61 | 84 | *alloc_addr = dst; |
43ef1c38 HCE |
85 | return bmp; |
86 | } | |
87 | #else | |
1c3dbe56 SG |
88 | struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp, |
89 | void **alloc_addr) | |
43ef1c38 HCE |
90 | { |
91 | return NULL; | |
92 | } | |
93 | #endif | |
94 | ||
09140113 SG |
95 | static int do_bmp_info(struct cmd_tbl *cmdtp, int flag, int argc, |
96 | char *const argv[]) | |
9acd4f0e FM |
97 | { |
98 | ulong addr; | |
43ef1c38 | 99 | |
9acd4f0e | 100 | switch (argc) { |
bb872dd9 SG |
101 | case 1: /* use image_load_addr as default address */ |
102 | addr = image_load_addr; | |
9acd4f0e FM |
103 | break; |
104 | case 2: /* use argument */ | |
105 | addr = simple_strtoul(argv[1], NULL, 16); | |
106 | break; | |
107 | default: | |
4c12eeb8 | 108 | return CMD_RET_USAGE; |
9acd4f0e FM |
109 | } |
110 | ||
111 | return (bmp_info(addr)); | |
112 | } | |
113 | ||
09140113 SG |
114 | static int do_bmp_display(struct cmd_tbl *cmdtp, int flag, int argc, |
115 | char *const argv[]) | |
059ae173 WD |
116 | { |
117 | ulong addr; | |
4b248f3f | 118 | int x = 0, y = 0; |
059ae173 | 119 | |
ff8fb56b AG |
120 | splash_get_pos(&x, &y); |
121 | ||
059ae173 | 122 | switch (argc) { |
bb872dd9 SG |
123 | case 1: /* use image_load_addr as default address */ |
124 | addr = image_load_addr; | |
059ae173 | 125 | break; |
9acd4f0e FM |
126 | case 2: /* use argument */ |
127 | addr = simple_strtoul(argv[1], NULL, 16); | |
059ae173 | 128 | break; |
9acd4f0e FM |
129 | case 4: |
130 | addr = simple_strtoul(argv[1], NULL, 16); | |
b0fcedb7 PD |
131 | if (!strcmp(argv[2], "m")) |
132 | x = BMP_ALIGN_CENTER; | |
133 | else | |
134 | x = simple_strtoul(argv[2], NULL, 10); | |
135 | if (!strcmp(argv[3], "m")) | |
136 | y = BMP_ALIGN_CENTER; | |
137 | else | |
138 | y = simple_strtoul(argv[3], NULL, 10); | |
93e14596 | 139 | break; |
059ae173 | 140 | default: |
4c12eeb8 | 141 | return CMD_RET_USAGE; |
059ae173 WD |
142 | } |
143 | ||
9acd4f0e FM |
144 | return (bmp_display(addr, x, y)); |
145 | } | |
146 | ||
09140113 | 147 | static struct cmd_tbl cmd_bmp_sub[] = { |
9acd4f0e FM |
148 | U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""), |
149 | U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""), | |
150 | }; | |
151 | ||
2e5167cc | 152 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
f1d2b313 HS |
153 | void bmp_reloc(void) { |
154 | fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub)); | |
155 | } | |
156 | #endif | |
157 | ||
9acd4f0e FM |
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 | */ | |
09140113 | 168 | static int do_bmp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
9acd4f0e | 169 | { |
09140113 | 170 | struct cmd_tbl *c; |
9acd4f0e FM |
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 | ||
47e26b1b | 178 | if (c) |
9acd4f0e | 179 | return c->cmd(cmdtp, flag, argc, argv); |
47e26b1b | 180 | else |
4c12eeb8 | 181 | return CMD_RET_USAGE; |
059ae173 WD |
182 | } |
183 | ||
0d498393 | 184 | U_BOOT_CMD( |
4b248f3f | 185 | bmp, 5, 1, do_bmp, |
2fb2604d | 186 | "manipulate BMP image data", |
4b248f3f | 187 | "info <imageAddr> - display image info\n" |
a89c33db | 188 | "bmp display <imageAddr> [x y] - display image at x,y" |
b0fce99b WD |
189 | ); |
190 | ||
059ae173 WD |
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 | { | |
72b335e9 | 203 | struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0); |
f7ef9d61 | 204 | void *bmp_alloc_addr = NULL; |
43ef1c38 | 205 | unsigned long len; |
c29ab9d7 | 206 | |
059ae173 | 207 | if (!((bmp->header.signature[0]=='B') && |
43ef1c38 | 208 | (bmp->header.signature[1]=='M'))) |
f7ef9d61 | 209 | bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr); |
c29ab9d7 | 210 | |
43ef1c38 | 211 | if (bmp == NULL) { |
059ae173 | 212 | printf("There is no valid bmp file at the given address\n"); |
43ef1c38 | 213 | return 1; |
059ae173 | 214 | } |
43ef1c38 | 215 | |
059ae173 WD |
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)); | |
c29ab9d7 | 220 | |
f7ef9d61 PW |
221 | if (bmp_alloc_addr) |
222 | free(bmp_alloc_addr); | |
c29ab9d7 | 223 | |
059ae173 WD |
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 | */ | |
de3b49c4 | 237 | int bmp_display(ulong addr, int x, int y) |
059ae173 | 238 | { |
b01c7923 SG |
239 | #ifdef CONFIG_DM_VIDEO |
240 | struct udevice *dev; | |
241 | #endif | |
43ef1c38 | 242 | int ret; |
72b335e9 | 243 | struct bmp_image *bmp = map_sysmem(addr, 0); |
f7ef9d61 | 244 | void *bmp_alloc_addr = NULL; |
43ef1c38 HCE |
245 | unsigned long len; |
246 | ||
247 | if (!((bmp->header.signature[0]=='B') && | |
248 | (bmp->header.signature[1]=='M'))) | |
f7ef9d61 | 249 | bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr); |
43ef1c38 HCE |
250 | |
251 | if (!bmp) { | |
252 | printf("There is no valid bmp file at the given address\n"); | |
253 | return 1; | |
254 | } | |
72b335e9 | 255 | addr = map_to_sysmem(bmp); |
43ef1c38 | 256 | |
b01c7923 | 257 | #ifdef CONFIG_DM_VIDEO |
3f603cbb | 258 | ret = uclass_first_device_err(UCLASS_VIDEO, &dev); |
b01c7923 | 259 | if (!ret) { |
3f603cbb | 260 | bool align = false; |
b01c7923 | 261 | |
b0fcedb7 PD |
262 | if (CONFIG_IS_ENABLED(SPLASH_SCREEN_ALIGN) || |
263 | x == BMP_ALIGN_CENTER || | |
264 | y == BMP_ALIGN_CENTER) | |
265 | align = true; | |
266 | ||
3f603cbb | 267 | ret = video_bmp_display(dev, addr, x, y, align); |
b01c7923 | 268 | } |
b01c7923 | 269 | #elif defined(CONFIG_LCD) |
72b335e9 | 270 | ret = lcd_display_bitmap(addr, x, y); |
281e00a3 | 271 | #elif defined(CONFIG_VIDEO) |
72b335e9 | 272 | ret = video_display_bitmap(addr, x, y); |
281e00a3 WD |
273 | #else |
274 | # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO | |
4b248f3f | 275 | #endif |
43ef1c38 | 276 | |
f7ef9d61 PW |
277 | if (bmp_alloc_addr) |
278 | free(bmp_alloc_addr); | |
43ef1c38 | 279 | |
e517db73 | 280 | return ret ? CMD_RET_FAILURE : 0; |
059ae173 | 281 | } |