wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
wdenk | b37c7e5 | 2003-06-30 16:24:52 +0000 | [diff] [blame] | 3 | * Detlev Zundel, DENX Software Engineering, dzu@denx.de. |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * BMP handling routines |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Alessandro Rubini | 6111722 | 2009-07-19 17:52:27 +0200 | [diff] [blame] | 13 | #include <lcd.h> |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 14 | #include <bmp_layout.h> |
| 15 | #include <command.h> |
wdenk | 8655b6f | 2004-10-09 23:25:58 +0000 | [diff] [blame] | 16 | #include <asm/byteorder.h> |
Stefan Roese | c29ab9d | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 17 | #include <malloc.h> |
Anatolij Gustschin | ff8fb56 | 2013-07-02 00:04:05 +0200 | [diff] [blame] | 18 | #include <splash.h> |
Stefan Reinauer | f674f7c | 2012-09-28 15:11:11 +0000 | [diff] [blame] | 19 | #include <video.h> |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 20 | |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 21 | static int bmp_info (ulong addr); |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 22 | |
| 23 | /* |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 24 | * Allocate and decompress a BMP image using gunzip(). |
| 25 | * |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 26 | * Returns a pointer to the decompressed image data. This pointer is |
| 27 | * aligned to 32-bit-aligned-address + 2. |
| 28 | * See doc/README.displaying-bmps for explanation. |
| 29 | * |
| 30 | * The allocation address is passed to 'alloc_addr' and must be freed |
| 31 | * by the caller after use. |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 32 | * |
| 33 | * Returns NULL if decompression failed, or if the decompressed data |
| 34 | * didn't contain a valid BMP signature. |
| 35 | */ |
| 36 | #ifdef CONFIG_VIDEO_BMP_GZIP |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 37 | bmp_image_t *gunzip_bmp(unsigned long addr, unsigned long *lenp, |
| 38 | void **alloc_addr) |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 39 | { |
| 40 | void *dst; |
| 41 | unsigned long len; |
| 42 | bmp_image_t *bmp; |
| 43 | |
| 44 | /* |
| 45 | * Decompress bmp image |
| 46 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 47 | len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE; |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 48 | /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */ |
| 49 | dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3); |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 50 | if (dst == NULL) { |
| 51 | puts("Error: malloc in gunzip failed!\n"); |
| 52 | return NULL; |
| 53 | } |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 54 | |
| 55 | bmp = dst; |
| 56 | |
| 57 | /* align to 32-bit-aligned-address + 2 */ |
| 58 | bmp = (bmp_image_t *)((((unsigned int)dst + 1) & ~3) + 2); |
| 59 | |
| 60 | if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, (uchar *)addr, &len) != 0) { |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 61 | free(dst); |
| 62 | return NULL; |
| 63 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 64 | if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE) |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 65 | puts("Image could be truncated" |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 66 | " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n"); |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 67 | |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 68 | /* |
| 69 | * Check for bmp mark 'BM' |
| 70 | */ |
| 71 | if (!((bmp->header.signature[0] == 'B') && |
| 72 | (bmp->header.signature[1] == 'M'))) { |
| 73 | free(dst); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
Wolfgang Denk | 17f79e4 | 2011-02-09 15:11:10 +0100 | [diff] [blame] | 77 | debug("Gzipped BMP image detected!\n"); |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 78 | |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 79 | *alloc_addr = dst; |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 80 | return bmp; |
| 81 | } |
| 82 | #else |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 83 | bmp_image_t *gunzip_bmp(unsigned long addr, unsigned long *lenp, |
| 84 | void **alloc_addr) |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 85 | { |
| 86 | return NULL; |
| 87 | } |
| 88 | #endif |
| 89 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 90 | static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) |
Frans Meulenbroeks | 9acd4f0 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 91 | { |
| 92 | ulong addr; |
| 93 | |
| 94 | switch (argc) { |
| 95 | case 1: /* use load_addr as default address */ |
| 96 | addr = load_addr; |
| 97 | break; |
| 98 | case 2: /* use argument */ |
| 99 | addr = simple_strtoul(argv[1], NULL, 16); |
| 100 | break; |
| 101 | default: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 102 | return CMD_RET_USAGE; |
Frans Meulenbroeks | 9acd4f0 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return (bmp_info(addr)); |
| 106 | } |
| 107 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 108 | static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) |
Frans Meulenbroeks | 9acd4f0 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 109 | { |
| 110 | ulong addr; |
| 111 | int x = 0, y = 0; |
| 112 | |
Anatolij Gustschin | ff8fb56 | 2013-07-02 00:04:05 +0200 | [diff] [blame] | 113 | splash_get_pos(&x, &y); |
| 114 | |
Frans Meulenbroeks | 9acd4f0 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 115 | switch (argc) { |
| 116 | case 1: /* use load_addr as default address */ |
| 117 | addr = load_addr; |
| 118 | break; |
| 119 | case 2: /* use argument */ |
| 120 | addr = simple_strtoul(argv[1], NULL, 16); |
| 121 | break; |
| 122 | case 4: |
| 123 | addr = simple_strtoul(argv[1], NULL, 16); |
Wolfgang Denk | 93e1459 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 124 | x = simple_strtoul(argv[2], NULL, 10); |
| 125 | y = simple_strtoul(argv[3], NULL, 10); |
| 126 | break; |
Frans Meulenbroeks | 9acd4f0 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 127 | default: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 128 | return CMD_RET_USAGE; |
Frans Meulenbroeks | 9acd4f0 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | return (bmp_display(addr, x, y)); |
| 132 | } |
| 133 | |
| 134 | static cmd_tbl_t cmd_bmp_sub[] = { |
| 135 | U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""), |
| 136 | U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""), |
| 137 | }; |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 138 | |
Wolfgang Denk | 2e5167c | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 139 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 140 | void bmp_reloc(void) { |
| 141 | fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub)); |
| 142 | } |
| 143 | #endif |
| 144 | |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 145 | /* |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 146 | * Subroutine: do_bmp |
| 147 | * |
| 148 | * Description: Handler for 'bmp' command.. |
| 149 | * |
| 150 | * Inputs: argv[1] contains the subcommand |
| 151 | * |
| 152 | * Return: None |
| 153 | * |
| 154 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 155 | static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 156 | { |
Frans Meulenbroeks | 9acd4f0 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 157 | cmd_tbl_t *c; |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 158 | |
Frans Meulenbroeks | 9acd4f0 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 159 | /* Strip off leading 'bmp' command argument */ |
| 160 | argc--; |
| 161 | argv++; |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 162 | |
Frans Meulenbroeks | 9acd4f0 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 163 | c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub)); |
| 164 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 165 | if (c) |
Frans Meulenbroeks | 9acd4f0 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 166 | return c->cmd(cmdtp, flag, argc, argv); |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 167 | else |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 168 | return CMD_RET_USAGE; |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 169 | } |
| 170 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 171 | U_BOOT_CMD( |
wdenk | 4b248f3 | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 172 | bmp, 5, 1, do_bmp, |
Peter Tyser | 2fb2604 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 173 | "manipulate BMP image data", |
wdenk | 4b248f3 | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 174 | "info <imageAddr> - display image info\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 175 | "bmp display <imageAddr> [x y] - display image at x,y" |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 176 | ); |
| 177 | |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 178 | /* |
| 179 | * Subroutine: bmp_info |
| 180 | * |
| 181 | * Description: Show information about bmp file in memory |
| 182 | * |
| 183 | * Inputs: addr address of the bmp file |
| 184 | * |
| 185 | * Return: None |
| 186 | * |
| 187 | */ |
| 188 | static int bmp_info(ulong addr) |
| 189 | { |
| 190 | bmp_image_t *bmp=(bmp_image_t *)addr; |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 191 | void *bmp_alloc_addr = NULL; |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 192 | unsigned long len; |
Stefan Roese | c29ab9d | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 193 | |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 194 | if (!((bmp->header.signature[0]=='B') && |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 195 | (bmp->header.signature[1]=='M'))) |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 196 | bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr); |
Stefan Roese | c29ab9d | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 197 | |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 198 | if (bmp == NULL) { |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 199 | printf("There is no valid bmp file at the given address\n"); |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 200 | return 1; |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 201 | } |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 202 | |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 203 | printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width), |
| 204 | le32_to_cpu(bmp->header.height)); |
| 205 | printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count)); |
| 206 | printf("Compression : %d\n", le32_to_cpu(bmp->header.compression)); |
Stefan Roese | c29ab9d | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 207 | |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 208 | if (bmp_alloc_addr) |
| 209 | free(bmp_alloc_addr); |
Stefan Roese | c29ab9d | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 210 | |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 211 | return(0); |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Subroutine: bmp_display |
| 216 | * |
| 217 | * Description: Display bmp file located in memory |
| 218 | * |
| 219 | * Inputs: addr address of the bmp file |
| 220 | * |
| 221 | * Return: None |
| 222 | * |
| 223 | */ |
Anatolij Gustschin | de3b49c | 2012-04-27 04:38:06 +0000 | [diff] [blame] | 224 | int bmp_display(ulong addr, int x, int y) |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 225 | { |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 226 | int ret; |
| 227 | bmp_image_t *bmp = (bmp_image_t *)addr; |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 228 | void *bmp_alloc_addr = NULL; |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 229 | unsigned long len; |
| 230 | |
| 231 | if (!((bmp->header.signature[0]=='B') && |
| 232 | (bmp->header.signature[1]=='M'))) |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 233 | bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr); |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 234 | |
| 235 | if (!bmp) { |
| 236 | printf("There is no valid bmp file at the given address\n"); |
| 237 | return 1; |
| 238 | } |
| 239 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 240 | #if defined(CONFIG_LCD) |
Che-Liang Chiou | 0211090 | 2011-10-20 23:07:03 +0000 | [diff] [blame] | 241 | ret = lcd_display_bitmap((ulong)bmp, x, y); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 242 | #elif defined(CONFIG_VIDEO) |
Stefan Reinauer | f674f7c | 2012-09-28 15:11:11 +0000 | [diff] [blame] | 243 | ret = video_display_bitmap((unsigned long)bmp, x, y); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 244 | #else |
| 245 | # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO |
wdenk | 4b248f3 | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 246 | #endif |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 247 | |
Piotr Wilczek | f7ef9d6 | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 248 | if (bmp_alloc_addr) |
| 249 | free(bmp_alloc_addr); |
Hans-Christian Egtvedt | 43ef1c3 | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 250 | |
| 251 | return ret; |
wdenk | 059ae17 | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 252 | } |