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_VIDEO_LOGO_MAX_SIZE;
51 /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
52 dst = malloc(CONFIG_VIDEO_LOGO_MAX_SIZE + 3);
54 puts("Error: malloc in gunzip failed!\n");
58 /* align to 32-bit-aligned-address + 2 */
61 if (gunzip(bmp, CONFIG_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
66 if (len == CONFIG_VIDEO_LOGO_MAX_SIZE)
67 puts("Image could be truncated (increase CONFIG_VIDEO_LOGO_MAX_SIZE)!\n");
70 * Check for bmp mark 'BM'
72 if (!((bmp->header.signature[0] == 'B') &&
73 (bmp->header.signature[1] == 'M'))) {
78 debug("Gzipped BMP image detected!\n");
84 struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
91 static int do_bmp_info(struct cmd_tbl *cmdtp, int flag, int argc,
97 case 1: /* use image_load_addr as default address */
98 addr = image_load_addr;
100 case 2: /* use argument */
101 addr = hextoul(argv[1], NULL);
104 return CMD_RET_USAGE;
107 return (bmp_info(addr));
110 static int do_bmp_display(struct cmd_tbl *cmdtp, int flag, int argc,
116 splash_get_pos(&x, &y);
119 case 1: /* use image_load_addr as default address */
120 addr = image_load_addr;
122 case 2: /* use argument */
123 addr = hextoul(argv[1], NULL);
126 addr = hextoul(argv[1], NULL);
127 if (!strcmp(argv[2], "m"))
128 x = BMP_ALIGN_CENTER;
130 x = dectoul(argv[2], NULL);
131 if (!strcmp(argv[3], "m"))
132 y = BMP_ALIGN_CENTER;
134 y = dectoul(argv[3], NULL);
137 return CMD_RET_USAGE;
140 return (bmp_display(addr, x, y));
143 static struct cmd_tbl cmd_bmp_sub[] = {
144 U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
145 U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
148 #ifdef CONFIG_NEEDS_MANUAL_RELOC
149 void bmp_reloc(void) {
150 fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
157 * Description: Handler for 'bmp' command..
159 * Inputs: argv[1] contains the subcommand
164 static int do_bmp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
168 /* Strip off leading 'bmp' command argument */
172 c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
175 return c->cmd(cmdtp, flag, argc, argv);
177 return CMD_RET_USAGE;
182 "manipulate BMP image data",
183 "info <imageAddr> - display image info\n"
184 "bmp display <imageAddr> [x y] - display image at x,y"
188 * Subroutine: bmp_info
190 * Description: Show information about bmp file in memory
192 * Inputs: addr address of the bmp file
197 static int bmp_info(ulong addr)
199 struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
200 void *bmp_alloc_addr = NULL;
203 if (!((bmp->header.signature[0]=='B') &&
204 (bmp->header.signature[1]=='M')))
205 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
208 printf("There is no valid bmp file at the given address\n");
212 printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
213 le32_to_cpu(bmp->header.height));
214 printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
215 printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
218 free(bmp_alloc_addr);
223 int bmp_display(ulong addr, int x, int y)
227 struct bmp_image *bmp = map_sysmem(addr, 0);
228 void *bmp_alloc_addr = NULL;
231 if (!((bmp->header.signature[0]=='B') &&
232 (bmp->header.signature[1]=='M')))
233 bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
236 printf("There is no valid bmp file at the given address\n");
239 addr = map_to_sysmem(bmp);
241 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
245 if (x == BMP_ALIGN_CENTER || y == BMP_ALIGN_CENTER)
248 ret = video_bmp_display(dev, addr, x, y, align);
252 free(bmp_alloc_addr);
254 return ret ? CMD_RET_FAILURE : 0;