1 // SPDX-License-Identifier: GPL-2.0+
4 * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
8 * BMP handling routines
12 #include <bmp_layout.h>
22 #include <asm/byteorder.h>
24 static int bmp_info (ulong addr);
27 * Allocate and decompress a BMP image using gunzip().
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.
33 * The allocation address is passed to 'alloc_addr' and must be freed
34 * by the caller after use.
36 * Returns NULL if decompression failed, or if the decompressed data
37 * didn't contain a valid BMP signature.
39 #ifdef CONFIG_VIDEO_BMP_GZIP
40 struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
45 struct bmp_image *bmp;
48 * Decompress bmp image
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);
54 puts("Error: malloc in gunzip failed!\n");
60 /* align to 32-bit-aligned-address + 2 */
61 bmp = (struct bmp_image *)((((uintptr_t)dst + 1) & ~3) + 2);
63 if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
68 if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
69 puts("Image could be truncated"
70 " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
73 * Check for bmp mark 'BM'
75 if (!((bmp->header.signature[0] == 'B') &&
76 (bmp->header.signature[1] == 'M'))) {
81 debug("Gzipped BMP image detected!\n");
87 struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
94 static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
99 case 1: /* use image_load_addr as default address */
100 addr = image_load_addr;
102 case 2: /* use argument */
103 addr = simple_strtoul(argv[1], NULL, 16);
106 return CMD_RET_USAGE;
109 return (bmp_info(addr));
112 static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
117 splash_get_pos(&x, &y);
120 case 1: /* use image_load_addr as default address */
121 addr = image_load_addr;
123 case 2: /* use argument */
124 addr = simple_strtoul(argv[1], NULL, 16);
127 addr = simple_strtoul(argv[1], NULL, 16);
128 if (!strcmp(argv[2], "m"))
129 x = BMP_ALIGN_CENTER;
131 x = simple_strtoul(argv[2], NULL, 10);
132 if (!strcmp(argv[3], "m"))
133 y = BMP_ALIGN_CENTER;
135 y = simple_strtoul(argv[3], NULL, 10);
138 return CMD_RET_USAGE;
141 return (bmp_display(addr, x, y));
144 static cmd_tbl_t cmd_bmp_sub[] = {
145 U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
146 U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
149 #ifdef CONFIG_NEEDS_MANUAL_RELOC
150 void bmp_reloc(void) {
151 fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
158 * Description: Handler for 'bmp' command..
160 * Inputs: argv[1] contains the subcommand
165 static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
169 /* Strip off leading 'bmp' command argument */
173 c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
176 return c->cmd(cmdtp, flag, argc, argv);
178 return CMD_RET_USAGE;
183 "manipulate BMP image data",
184 "info <imageAddr> - display image info\n"
185 "bmp display <imageAddr> [x y] - display image at x,y"
189 * Subroutine: bmp_info
191 * Description: Show information about bmp file in memory
193 * Inputs: addr address of the bmp file
198 static int bmp_info(ulong addr)
200 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
201 void *bmp_alloc_addr = NULL;
204 if (!((bmp->header.signature[0]=='B') &&
205 (bmp->header.signature[1]=='M')))
206 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
209 printf("There is no valid bmp file at the given address\n");
213 printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
214 le32_to_cpu(bmp->header.height));
215 printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
216 printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
219 free(bmp_alloc_addr);
225 * Subroutine: bmp_display
227 * Description: Display bmp file located in memory
229 * Inputs: addr address of the bmp file
234 int bmp_display(ulong addr, int x, int y)
236 #ifdef CONFIG_DM_VIDEO
240 struct bmp_image *bmp = map_sysmem(addr, 0);
241 void *bmp_alloc_addr = NULL;
244 if (!((bmp->header.signature[0]=='B') &&
245 (bmp->header.signature[1]=='M')))
246 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
249 printf("There is no valid bmp file at the given address\n");
252 addr = map_to_sysmem(bmp);
254 #ifdef CONFIG_DM_VIDEO
255 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
259 if (CONFIG_IS_ENABLED(SPLASH_SCREEN_ALIGN) ||
260 x == BMP_ALIGN_CENTER ||
261 y == BMP_ALIGN_CENTER)
264 ret = video_bmp_display(dev, addr, x, y, align);
266 #elif defined(CONFIG_LCD)
267 ret = lcd_display_bitmap(addr, x, y);
268 #elif defined(CONFIG_VIDEO)
269 ret = video_display_bitmap(addr, x, y);
271 # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
275 free(bmp_alloc_addr);
277 return ret ? CMD_RET_FAILURE : 0;