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_VIDEO_LOGO_MAX_SIZE;
52 /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
53 dst = malloc(CONFIG_VIDEO_LOGO_MAX_SIZE + 3);
55 puts("Error: malloc in gunzip failed!\n");
59 /* align to 32-bit-aligned-address + 2 */
62 if (gunzip(bmp, CONFIG_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
67 if (len == CONFIG_VIDEO_LOGO_MAX_SIZE)
68 puts("Image could be truncated (increase CONFIG_VIDEO_LOGO_MAX_SIZE)!\n");
71 * Check for bmp mark 'BM'
73 if (!((bmp->header.signature[0] == 'B') &&
74 (bmp->header.signature[1] == 'M'))) {
79 debug("Gzipped BMP image detected!\n");
85 struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
92 static int do_bmp_info(struct cmd_tbl *cmdtp, int flag, int argc,
98 case 1: /* use image_load_addr as default address */
99 addr = image_load_addr;
101 case 2: /* use argument */
102 addr = hextoul(argv[1], NULL);
105 return CMD_RET_USAGE;
108 return (bmp_info(addr));
111 static int do_bmp_display(struct cmd_tbl *cmdtp, int flag, int argc,
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 = hextoul(argv[1], NULL);
127 addr = hextoul(argv[1], NULL);
128 if (!strcmp(argv[2], "m"))
129 x = BMP_ALIGN_CENTER;
131 x = dectoul(argv[2], NULL);
132 if (!strcmp(argv[3], "m"))
133 y = BMP_ALIGN_CENTER;
135 y = dectoul(argv[3], NULL);
138 return CMD_RET_USAGE;
141 return (bmp_display(addr, x, y));
144 static struct cmd_tbl 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(struct cmd_tbl *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 (x == BMP_ALIGN_CENTER || y == BMP_ALIGN_CENTER)
262 ret = video_bmp_display(dev, addr, x, y, align);
264 #elif defined(CONFIG_LCD)
265 ret = lcd_display_bitmap(addr, x, y);
267 # error bmp_display() requires CONFIG_LCD
271 free(bmp_alloc_addr);
273 return ret ? CMD_RET_FAILURE : 0;