bmp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002
  4. * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
  5. */
  6. /*
  7. * BMP handling routines
  8. */
  9. #include <common.h>
  10. #include <bmp_layout.h>
  11. #include <command.h>
  12. #include <dm.h>
  13. #include <gzip.h>
  14. #include <log.h>
  15. #include <malloc.h>
  16. #include <mapmem.h>
  17. #include <splash.h>
  18. #include <video.h>
  19. #include <asm/byteorder.h>
  20. /*
  21. * Allocate and decompress a BMP image using gunzip().
  22. *
  23. * Returns a pointer to the decompressed image data. This pointer is
  24. * aligned to 32-bit-aligned-address + 2.
  25. * See doc/README.displaying-bmps for explanation.
  26. *
  27. * The allocation address is passed to 'alloc_addr' and must be freed
  28. * by the caller after use.
  29. *
  30. * Returns NULL if decompression failed, or if the decompressed data
  31. * didn't contain a valid BMP signature or decompression is not enabled in
  32. * Kconfig.
  33. */
  34. struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
  35. void **alloc_addr)
  36. {
  37. void *dst;
  38. unsigned long len;
  39. struct bmp_image *bmp;
  40. if (!CONFIG_IS_ENABLED(VIDEO_BMP_GZIP))
  41. return NULL;
  42. /*
  43. * Decompress bmp image
  44. */
  45. len = CONFIG_VAL(VIDEO_LOGO_MAX_SIZE);
  46. /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
  47. dst = malloc(CONFIG_VAL(VIDEO_LOGO_MAX_SIZE) + 3);
  48. if (!dst) {
  49. puts("Error: malloc in gunzip failed!\n");
  50. return NULL;
  51. }
  52. /* align to 32-bit-aligned-address + 2 */
  53. bmp = dst + 2;
  54. if (gunzip(bmp, CONFIG_VAL(VIDEO_LOGO_MAX_SIZE), map_sysmem(addr, 0),
  55. &len)) {
  56. free(dst);
  57. return NULL;
  58. }
  59. if (len == CONFIG_VAL(VIDEO_LOGO_MAX_SIZE))
  60. puts("Image could be truncated (increase CONFIG_VIDEO_LOGO_MAX_SIZE)!\n");
  61. /*
  62. * Check for bmp mark 'BM'
  63. */
  64. if (!((bmp->header.signature[0] == 'B') &&
  65. (bmp->header.signature[1] == 'M'))) {
  66. free(dst);
  67. return NULL;
  68. }
  69. debug("Gzipped BMP image detected!\n");
  70. *alloc_addr = dst;
  71. return bmp;
  72. }
  73. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  74. void bmp_reloc(void)
  75. {
  76. fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
  77. }
  78. #endif
  79. int bmp_info(ulong addr)
  80. {
  81. struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
  82. void *bmp_alloc_addr = NULL;
  83. unsigned long len;
  84. if (!((bmp->header.signature[0] == 'B') &&
  85. (bmp->header.signature[1] == 'M')))
  86. bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
  87. if (!bmp) {
  88. printf("There is no valid bmp file at the given address\n");
  89. return 1;
  90. }
  91. printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
  92. le32_to_cpu(bmp->header.height));
  93. printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
  94. printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
  95. if (bmp_alloc_addr)
  96. free(bmp_alloc_addr);
  97. return 0;
  98. }
  99. int bmp_display(ulong addr, int x, int y)
  100. {
  101. struct udevice *dev;
  102. int ret;
  103. struct bmp_image *bmp = map_sysmem(addr, 0);
  104. void *bmp_alloc_addr = NULL;
  105. unsigned long len;
  106. if (!((bmp->header.signature[0] == 'B') &&
  107. (bmp->header.signature[1] == 'M')))
  108. bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
  109. if (!bmp) {
  110. printf("There is no valid bmp file at the given address\n");
  111. return 1;
  112. }
  113. addr = map_to_sysmem(bmp);
  114. ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
  115. if (!ret) {
  116. bool align = false;
  117. if (x == BMP_ALIGN_CENTER || y == BMP_ALIGN_CENTER)
  118. align = true;
  119. ret = video_bmp_display(dev, addr, x, y, align);
  120. }
  121. if (bmp_alloc_addr)
  122. free(bmp_alloc_addr);
  123. return ret ? CMD_RET_FAILURE : 0;
  124. }