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>
23 #include <asm/byteorder.h>
25 static int bmp_info (ulong addr);
28 * Allocate and decompress a BMP image using gunzip().
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.
34 * The allocation address is passed to 'alloc_addr' and must be freed
35 * by the caller after use.
37 * Returns NULL if decompression failed, or if the decompressed data
38 * didn't contain a valid BMP signature.
40 #ifdef CONFIG_VIDEO_BMP_GZIP
41 struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
46 struct bmp_image *bmp;
49 * Decompress bmp image
51 len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
52 /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
53 dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3);
55 puts("Error: malloc in gunzip failed!\n");
61 /* align to 32-bit-aligned-address + 2 */
62 bmp = (struct bmp_image *)((((uintptr_t)dst + 1) & ~3) + 2);
64 if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
69 if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
70 puts("Image could be truncated"
71 " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
74 * Check for bmp mark 'BM'
76 if (!((bmp->header.signature[0] == 'B') &&
77 (bmp->header.signature[1] == 'M'))) {
82 debug("Gzipped BMP image detected!\n");
88 struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
95 static int do_bmp_info(struct cmd_tbl *cmdtp, int flag, int argc,
101 case 1: /* use image_load_addr as default address */
102 addr = image_load_addr;
104 case 2: /* use argument */
105 addr = simple_strtoul(argv[1], NULL, 16);
108 return CMD_RET_USAGE;
111 return (bmp_info(addr));
114 static int do_bmp_display(struct cmd_tbl *cmdtp, int flag, int argc,
120 splash_get_pos(&x, &y);
123 case 1: /* use image_load_addr as default address */
124 addr = image_load_addr;
126 case 2: /* use argument */
127 addr = simple_strtoul(argv[1], NULL, 16);
130 addr = simple_strtoul(argv[1], NULL, 16);
131 if (!strcmp(argv[2], "m"))
132 x = BMP_ALIGN_CENTER;
134 x = simple_strtoul(argv[2], NULL, 10);
135 if (!strcmp(argv[3], "m"))
136 y = BMP_ALIGN_CENTER;
138 y = simple_strtoul(argv[3], NULL, 10);
141 return CMD_RET_USAGE;
144 return (bmp_display(addr, x, y));
147 static struct cmd_tbl 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, "", ""),
152 #ifdef CONFIG_NEEDS_MANUAL_RELOC
153 void bmp_reloc(void) {
154 fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
161 * Description: Handler for 'bmp' command..
163 * Inputs: argv[1] contains the subcommand
168 static int do_bmp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
172 /* Strip off leading 'bmp' command argument */
176 c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
179 return c->cmd(cmdtp, flag, argc, argv);
181 return CMD_RET_USAGE;
186 "manipulate BMP image data",
187 "info <imageAddr> - display image info\n"
188 "bmp display <imageAddr> [x y] - display image at x,y"
192 * Subroutine: bmp_info
194 * Description: Show information about bmp file in memory
196 * Inputs: addr address of the bmp file
201 static int bmp_info(ulong addr)
203 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
204 void *bmp_alloc_addr = NULL;
207 if (!((bmp->header.signature[0]=='B') &&
208 (bmp->header.signature[1]=='M')))
209 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
212 printf("There is no valid bmp file at the given address\n");
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));
222 free(bmp_alloc_addr);
228 * Subroutine: bmp_display
230 * Description: Display bmp file located in memory
232 * Inputs: addr address of the bmp file
237 int bmp_display(ulong addr, int x, int y)
239 #ifdef CONFIG_DM_VIDEO
243 struct bmp_image *bmp = map_sysmem(addr, 0);
244 void *bmp_alloc_addr = NULL;
247 if (!((bmp->header.signature[0]=='B') &&
248 (bmp->header.signature[1]=='M')))
249 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
252 printf("There is no valid bmp file at the given address\n");
255 addr = map_to_sysmem(bmp);
257 #ifdef CONFIG_DM_VIDEO
258 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
262 if (CONFIG_IS_ENABLED(SPLASH_SCREEN_ALIGN) ||
263 x == BMP_ALIGN_CENTER ||
264 y == BMP_ALIGN_CENTER)
267 ret = video_bmp_display(dev, addr, x, y, align);
269 #elif defined(CONFIG_LCD)
270 ret = lcd_display_bitmap(addr, x, y);
271 #elif defined(CONFIG_VIDEO)
272 ret = video_display_bitmap(addr, x, y);
274 # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
278 free(bmp_alloc_addr);
280 return ret ? CMD_RET_FAILURE : 0;