Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2002 |
wdenk | b9bbd24 | 2003-06-30 16:24:52 +0000 | [diff] [blame] | 4 | * Detlev Zundel, DENX Software Engineering, dzu@denx.de. |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * BMP handling routines |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <bmp_layout.h> |
| 13 | #include <command.h> |
Simon Glass | 1a974af | 2019-08-01 09:46:36 -0600 | [diff] [blame] | 14 | #include <dm.h> |
| 15 | #include <gzip.h> |
Simon Glass | 85f1378 | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 16 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 17 | #include <log.h> |
Stefan Roese | 13b9369 | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 18 | #include <malloc.h> |
Simon Glass | cd161d8 | 2016-01-05 09:30:58 -0700 | [diff] [blame] | 19 | #include <mapmem.h> |
Anatolij Gustschin | b9b5eaf | 2013-07-02 00:04:05 +0200 | [diff] [blame] | 20 | #include <splash.h> |
Stefan Reinauer | 987d1d8 | 2012-09-28 15:11:11 +0000 | [diff] [blame] | 21 | #include <video.h> |
Simon Glass | 1a974af | 2019-08-01 09:46:36 -0600 | [diff] [blame] | 22 | #include <asm/byteorder.h> |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 23 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 24 | static int bmp_info (ulong addr); |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 25 | |
| 26 | /* |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 27 | * Allocate and decompress a BMP image using gunzip(). |
| 28 | * |
Piotr Wilczek | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 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. |
| 32 | * |
| 33 | * The allocation address is passed to 'alloc_addr' and must be freed |
| 34 | * by the caller after use. |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 35 | * |
| 36 | * Returns NULL if decompression failed, or if the decompressed data |
| 37 | * didn't contain a valid BMP signature. |
| 38 | */ |
| 39 | #ifdef CONFIG_VIDEO_BMP_GZIP |
Simon Glass | 92e1f85 | 2015-05-13 07:02:27 -0600 | [diff] [blame] | 40 | struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp, |
| 41 | void **alloc_addr) |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 42 | { |
| 43 | void *dst; |
| 44 | unsigned long len; |
Simon Glass | 92e1f85 | 2015-05-13 07:02:27 -0600 | [diff] [blame] | 45 | struct bmp_image *bmp; |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Decompress bmp image |
| 49 | */ |
Simon Glass | 858198c | 2022-10-18 06:46:08 -0600 | [diff] [blame] | 50 | len = CONFIG_VIDEO_LOGO_MAX_SIZE; |
Piotr Wilczek | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 51 | /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */ |
Simon Glass | 858198c | 2022-10-18 06:46:08 -0600 | [diff] [blame] | 52 | dst = malloc(CONFIG_VIDEO_LOGO_MAX_SIZE + 3); |
| 53 | if (!dst) { |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 54 | puts("Error: malloc in gunzip failed!\n"); |
| 55 | return NULL; |
| 56 | } |
Piotr Wilczek | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 57 | |
Piotr Wilczek | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 58 | /* align to 32-bit-aligned-address + 2 */ |
Simon Glass | 858198c | 2022-10-18 06:46:08 -0600 | [diff] [blame] | 59 | bmp = dst + 2; |
Piotr Wilczek | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 60 | |
Simon Glass | 858198c | 2022-10-18 06:46:08 -0600 | [diff] [blame] | 61 | if (gunzip(bmp, CONFIG_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0), |
| 62 | &len)) { |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 63 | free(dst); |
| 64 | return NULL; |
| 65 | } |
Simon Glass | 858198c | 2022-10-18 06:46:08 -0600 | [diff] [blame] | 66 | if (len == CONFIG_VIDEO_LOGO_MAX_SIZE) |
| 67 | puts("Image could be truncated (increase CONFIG_VIDEO_LOGO_MAX_SIZE)!\n"); |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 68 | |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 69 | /* |
| 70 | * Check for bmp mark 'BM' |
| 71 | */ |
| 72 | if (!((bmp->header.signature[0] == 'B') && |
| 73 | (bmp->header.signature[1] == 'M'))) { |
| 74 | free(dst); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
Wolfgang Denk | af42bb3 | 2011-02-09 15:11:10 +0100 | [diff] [blame] | 78 | debug("Gzipped BMP image detected!\n"); |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 79 | |
Piotr Wilczek | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 80 | *alloc_addr = dst; |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 81 | return bmp; |
| 82 | } |
| 83 | #else |
Simon Glass | 92e1f85 | 2015-05-13 07:02:27 -0600 | [diff] [blame] | 84 | struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp, |
| 85 | void **alloc_addr) |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 86 | { |
| 87 | return NULL; |
| 88 | } |
| 89 | #endif |
| 90 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 91 | static int do_bmp_info(struct cmd_tbl *cmdtp, int flag, int argc, |
| 92 | char *const argv[]) |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 93 | { |
| 94 | ulong addr; |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 95 | |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 96 | switch (argc) { |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 97 | case 1: /* use image_load_addr as default address */ |
| 98 | addr = image_load_addr; |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 99 | break; |
| 100 | case 2: /* use argument */ |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 101 | addr = hextoul(argv[1], NULL); |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 102 | break; |
| 103 | default: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 104 | return CMD_RET_USAGE; |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return (bmp_info(addr)); |
| 108 | } |
| 109 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 110 | static int do_bmp_display(struct cmd_tbl *cmdtp, int flag, int argc, |
| 111 | char *const argv[]) |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 112 | { |
| 113 | ulong addr; |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 114 | int x = 0, y = 0; |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 115 | |
Anatolij Gustschin | b9b5eaf | 2013-07-02 00:04:05 +0200 | [diff] [blame] | 116 | splash_get_pos(&x, &y); |
| 117 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 118 | switch (argc) { |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 119 | case 1: /* use image_load_addr as default address */ |
| 120 | addr = image_load_addr; |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 121 | break; |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 122 | case 2: /* use argument */ |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 123 | addr = hextoul(argv[1], NULL); |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 124 | break; |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 125 | case 4: |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 126 | addr = hextoul(argv[1], NULL); |
Patrick Delaunay | 28e07f3 | 2018-12-01 14:41:07 +0100 | [diff] [blame] | 127 | if (!strcmp(argv[2], "m")) |
| 128 | x = BMP_ALIGN_CENTER; |
| 129 | else |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 130 | x = dectoul(argv[2], NULL); |
Patrick Delaunay | 28e07f3 | 2018-12-01 14:41:07 +0100 | [diff] [blame] | 131 | if (!strcmp(argv[3], "m")) |
| 132 | y = BMP_ALIGN_CENTER; |
| 133 | else |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 134 | y = dectoul(argv[3], NULL); |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 135 | break; |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 136 | default: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 137 | return CMD_RET_USAGE; |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 140 | return (bmp_display(addr, x, y)); |
| 141 | } |
| 142 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 143 | static struct cmd_tbl cmd_bmp_sub[] = { |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 144 | U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""), |
| 145 | U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""), |
| 146 | }; |
| 147 | |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 148 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 149 | void bmp_reloc(void) { |
| 150 | fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub)); |
| 151 | } |
| 152 | #endif |
| 153 | |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 154 | /* |
| 155 | * Subroutine: do_bmp |
| 156 | * |
| 157 | * Description: Handler for 'bmp' command.. |
| 158 | * |
| 159 | * Inputs: argv[1] contains the subcommand |
| 160 | * |
| 161 | * Return: None |
| 162 | * |
| 163 | */ |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 164 | static int do_bmp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 165 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 166 | struct cmd_tbl *c; |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 167 | |
| 168 | /* Strip off leading 'bmp' command argument */ |
| 169 | argc--; |
| 170 | argv++; |
| 171 | |
| 172 | c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub)); |
| 173 | |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 174 | if (c) |
Frans Meulenbroeks | c59bc50 | 2010-03-27 11:16:10 +0100 | [diff] [blame] | 175 | return c->cmd(cmdtp, flag, argc, argv); |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 176 | else |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 177 | return CMD_RET_USAGE; |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 178 | } |
| 179 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 180 | U_BOOT_CMD( |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 181 | bmp, 5, 1, do_bmp, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 182 | "manipulate BMP image data", |
wdenk | e55402c | 2004-03-14 16:51:43 +0000 | [diff] [blame] | 183 | "info <imageAddr> - display image info\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 184 | "bmp display <imageAddr> [x y] - display image at x,y" |
wdenk | f12e396 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 185 | ); |
| 186 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 187 | /* |
| 188 | * Subroutine: bmp_info |
| 189 | * |
| 190 | * Description: Show information about bmp file in memory |
| 191 | * |
| 192 | * Inputs: addr address of the bmp file |
| 193 | * |
| 194 | * Return: None |
| 195 | * |
| 196 | */ |
| 197 | static int bmp_info(ulong addr) |
| 198 | { |
Simon Glass | cd161d8 | 2016-01-05 09:30:58 -0700 | [diff] [blame] | 199 | struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0); |
Piotr Wilczek | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 200 | void *bmp_alloc_addr = NULL; |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 201 | unsigned long len; |
Stefan Roese | 13b9369 | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 202 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 203 | if (!((bmp->header.signature[0]=='B') && |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 204 | (bmp->header.signature[1]=='M'))) |
Piotr Wilczek | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 205 | bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr); |
Stefan Roese | 13b9369 | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 206 | |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 207 | if (bmp == NULL) { |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 208 | printf("There is no valid bmp file at the given address\n"); |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 209 | return 1; |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 210 | } |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 211 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 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)); |
Stefan Roese | 13b9369 | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 216 | |
Piotr Wilczek | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 217 | if (bmp_alloc_addr) |
| 218 | free(bmp_alloc_addr); |
Stefan Roese | 13b9369 | 2005-10-08 10:19:07 +0200 | [diff] [blame] | 219 | |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 220 | return(0); |
| 221 | } |
| 222 | |
Anatolij Gustschin | 7a49e6f | 2012-04-27 04:38:06 +0000 | [diff] [blame] | 223 | int bmp_display(ulong addr, int x, int y) |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 224 | { |
Simon Glass | 68b2627 | 2016-01-18 19:52:22 -0700 | [diff] [blame] | 225 | struct udevice *dev; |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 226 | int ret; |
Simon Glass | cd161d8 | 2016-01-05 09:30:58 -0700 | [diff] [blame] | 227 | struct bmp_image *bmp = map_sysmem(addr, 0); |
Piotr Wilczek | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 228 | void *bmp_alloc_addr = NULL; |
Hans-Christian Egtvedt | 68d89dc | 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 | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 233 | bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr); |
Hans-Christian Egtvedt | 68d89dc | 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 | } |
Simon Glass | cd161d8 | 2016-01-05 09:30:58 -0700 | [diff] [blame] | 239 | addr = map_to_sysmem(bmp); |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 240 | |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 241 | ret = uclass_first_device_err(UCLASS_VIDEO, &dev); |
Simon Glass | 68b2627 | 2016-01-18 19:52:22 -0700 | [diff] [blame] | 242 | if (!ret) { |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 243 | bool align = false; |
Simon Glass | 68b2627 | 2016-01-18 19:52:22 -0700 | [diff] [blame] | 244 | |
Simon Glass | 8b883a3 | 2022-10-18 06:24:16 -0600 | [diff] [blame] | 245 | if (x == BMP_ALIGN_CENTER || y == BMP_ALIGN_CENTER) |
Patrick Delaunay | 28e07f3 | 2018-12-01 14:41:07 +0100 | [diff] [blame] | 246 | align = true; |
| 247 | |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 248 | ret = video_bmp_display(dev, addr, x, y, align); |
Simon Glass | 68b2627 | 2016-01-18 19:52:22 -0700 | [diff] [blame] | 249 | } |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 250 | |
Piotr Wilczek | e59e0ff | 2013-06-05 08:14:30 +0200 | [diff] [blame] | 251 | if (bmp_alloc_addr) |
| 252 | free(bmp_alloc_addr); |
Hans-Christian Egtvedt | 68d89dc | 2007-11-30 17:29:59 +0100 | [diff] [blame] | 253 | |
Simon Glass | 476e8af | 2016-01-30 15:45:15 -0700 | [diff] [blame] | 254 | return ret ? CMD_RET_FAILURE : 0; |
wdenk | f5f146e | 2003-04-20 16:52:09 +0000 | [diff] [blame] | 255 | } |