bmp.c 5.75 KB
Newer Older
1 2
/*
 * (C) Copyright 2002
3
 * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
4
 *
5
 * SPDX-License-Identifier:	GPL-2.0+
6 7 8 9 10 11 12
 */

/*
 * BMP handling routines
 */

#include <common.h>
13
#include <dm.h>
14
#include <lcd.h>
15
#include <mapmem.h>
16 17
#include <bmp_layout.h>
#include <command.h>
18
#include <asm/byteorder.h>
19
#include <malloc.h>
20
#include <mapmem.h>
21
#include <splash.h>
22
#include <video.h>
23 24 25

static int bmp_info (ulong addr);

26 27 28
/*
 * Allocate and decompress a BMP image using gunzip().
 *
29 30 31 32 33 34
 * Returns a pointer to the decompressed image data. This pointer is
 * aligned to 32-bit-aligned-address + 2.
 * See doc/README.displaying-bmps for explanation.
 *
 * The allocation address is passed to 'alloc_addr' and must be freed
 * by the caller after use.
35 36 37 38 39
 *
 * Returns NULL if decompression failed, or if the decompressed data
 * didn't contain a valid BMP signature.
 */
#ifdef CONFIG_VIDEO_BMP_GZIP
40 41
struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
			     void **alloc_addr)
42 43 44
{
	void *dst;
	unsigned long len;
45
	struct bmp_image *bmp;
46 47 48 49

	/*
	 * Decompress bmp image
	 */
50
	len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
51 52
	/* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
	dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3);
53 54 55 56
	if (dst == NULL) {
		puts("Error: malloc in gunzip failed!\n");
		return NULL;
	}
57 58 59 60

	bmp = dst;

	/* align to 32-bit-aligned-address + 2 */
61
	bmp = (struct bmp_image *)((((unsigned int)dst + 1) & ~3) + 2);
62

63 64
	if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
		   &len) != 0) {
65 66 67
		free(dst);
		return NULL;
	}
68
	if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
69
		puts("Image could be truncated"
70
				" (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
71 72 73 74 75 76 77 78 79 80

	/*
	 * Check for bmp mark 'BM'
	 */
	if (!((bmp->header.signature[0] == 'B') &&
	      (bmp->header.signature[1] == 'M'))) {
		free(dst);
		return NULL;
	}

81
	debug("Gzipped BMP image detected!\n");
82

83
	*alloc_addr = dst;
84 85 86
	return bmp;
}
#else
87 88
struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
			     void **alloc_addr)
89 90 91 92 93
{
	return NULL;
}
#endif

94
static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
95 96
{
	ulong addr;
97

98 99 100 101 102 103 104 105
	switch (argc) {
	case 1:		/* use load_addr as default address */
		addr = load_addr;
		break;
	case 2:		/* use argument */
		addr = simple_strtoul(argv[1], NULL, 16);
		break;
	default:
106
		return CMD_RET_USAGE;
107 108 109 110 111
	}

	return (bmp_info(addr));
}

112
static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
113 114
{
	ulong addr;
115
	int x = 0, y = 0;
116

117 118
	splash_get_pos(&x, &y);

119
	switch (argc) {
120
	case 1:		/* use load_addr as default address */
121 122
		addr = load_addr;
		break;
123 124
	case 2:		/* use argument */
		addr = simple_strtoul(argv[1], NULL, 16);
125
		break;
126 127
	case 4:
		addr = simple_strtoul(argv[1], NULL, 16);
128 129 130
		x = simple_strtoul(argv[2], NULL, 10);
		y = simple_strtoul(argv[3], NULL, 10);
		break;
131
	default:
132
		return CMD_RET_USAGE;
133 134
	}

135 136 137 138 139 140 141 142
	 return (bmp_display(addr, x, y));
}

static cmd_tbl_t cmd_bmp_sub[] = {
	U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
	U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
};

143
#ifdef CONFIG_NEEDS_MANUAL_RELOC
144 145 146 147 148
void bmp_reloc(void) {
	fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
}
#endif

149 150 151 152 153 154 155 156 157 158
/*
 * Subroutine:  do_bmp
 *
 * Description: Handler for 'bmp' command..
 *
 * Inputs:	argv[1] contains the subcommand
 *
 * Return:      None
 *
 */
159
static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
160 161 162 163 164 165 166 167 168
{
	cmd_tbl_t *c;

	/* Strip off leading 'bmp' command argument */
	argc--;
	argv++;

	c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));

169
	if (c)
170
		return  c->cmd(cmdtp, flag, argc, argv);
171
	else
172
		return CMD_RET_USAGE;
173 174
}

175
U_BOOT_CMD(
176
	bmp,	5,	1,	do_bmp,
Peter Tyser's avatar
Peter Tyser committed
177
	"manipulate BMP image data",
178
	"info <imageAddr>          - display image info\n"
179
	"bmp display <imageAddr> [x y] - display image at x,y"
180 181
);

182 183 184 185 186 187 188 189 190 191 192 193
/*
 * Subroutine:  bmp_info
 *
 * Description: Show information about bmp file in memory
 *
 * Inputs:	addr		address of the bmp file
 *
 * Return:      None
 *
 */
static int bmp_info(ulong addr)
{
194
	struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
195
	void *bmp_alloc_addr = NULL;
196
	unsigned long len;
197

198
	if (!((bmp->header.signature[0]=='B') &&
199
	      (bmp->header.signature[1]=='M')))
200
		bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
201

202
	if (bmp == NULL) {
203
		printf("There is no valid bmp file at the given address\n");
204
		return 1;
205
	}
206

207 208 209 210
	printf("Image size    : %d x %d\n", le32_to_cpu(bmp->header.width),
	       le32_to_cpu(bmp->header.height));
	printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
	printf("Compression   : %d\n", le32_to_cpu(bmp->header.compression));
211

212 213
	if (bmp_alloc_addr)
		free(bmp_alloc_addr);
214

215 216 217 218 219 220 221 222 223 224 225 226 227
	return(0);
}

/*
 * Subroutine:  bmp_display
 *
 * Description: Display bmp file located in memory
 *
 * Inputs:	addr		address of the bmp file
 *
 * Return:      None
 *
 */
228
int bmp_display(ulong addr, int x, int y)
229
{
230 231 232
#ifdef CONFIG_DM_VIDEO
	struct udevice *dev;
#endif
233
	int ret;
234
	struct bmp_image *bmp = map_sysmem(addr, 0);
235
	void *bmp_alloc_addr = NULL;
236 237 238 239
	unsigned long len;

	if (!((bmp->header.signature[0]=='B') &&
	      (bmp->header.signature[1]=='M')))
240
		bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
241 242 243 244 245

	if (!bmp) {
		printf("There is no valid bmp file at the given address\n");
		return 1;
	}
246
	addr = map_to_sysmem(bmp);
247

248
#ifdef CONFIG_DM_VIDEO
249
	ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
250
	if (!ret) {
251
		bool align = false;
252 253

# ifdef CONFIG_SPLASH_SCREEN_ALIGN
254
		align = true;
255
# endif /* CONFIG_SPLASH_SCREEN_ALIGN */
256
		ret = video_bmp_display(dev, addr, x, y, align);
257 258
	}
#elif defined(CONFIG_LCD)
259
	ret = lcd_display_bitmap(addr, x, y);
wdenk's avatar
wdenk committed
260
#elif defined(CONFIG_VIDEO)
261
	ret = video_display_bitmap(addr, x, y);
wdenk's avatar
wdenk committed
262 263
#else
# error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
264
#endif
265

266 267
	if (bmp_alloc_addr)
		free(bmp_alloc_addr);
268

269
	return ret ? CMD_RET_FAILURE : 0;
270
}