bmp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <dm.h>
  11. #include <lcd.h>
  12. #include <mapmem.h>
  13. #include <bmp_layout.h>
  14. #include <command.h>
  15. #include <asm/byteorder.h>
  16. #include <malloc.h>
  17. #include <mapmem.h>
  18. #include <splash.h>
  19. #include <video.h>
  20. static int bmp_info (ulong addr);
  21. /*
  22. * Allocate and decompress a BMP image using gunzip().
  23. *
  24. * Returns a pointer to the decompressed image data. This pointer is
  25. * aligned to 32-bit-aligned-address + 2.
  26. * See doc/README.displaying-bmps for explanation.
  27. *
  28. * The allocation address is passed to 'alloc_addr' and must be freed
  29. * by the caller after use.
  30. *
  31. * Returns NULL if decompression failed, or if the decompressed data
  32. * didn't contain a valid BMP signature.
  33. */
  34. #ifdef CONFIG_VIDEO_BMP_GZIP
  35. struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
  36. void **alloc_addr)
  37. {
  38. void *dst;
  39. unsigned long len;
  40. struct bmp_image *bmp;
  41. /*
  42. * Decompress bmp image
  43. */
  44. len = CONFIG_SYS_VIDEO_LOGO_MAX_SIZE;
  45. /* allocate extra 3 bytes for 32-bit-aligned-address + 2 alignment */
  46. dst = malloc(CONFIG_SYS_VIDEO_LOGO_MAX_SIZE + 3);
  47. if (dst == NULL) {
  48. puts("Error: malloc in gunzip failed!\n");
  49. return NULL;
  50. }
  51. bmp = dst;
  52. /* align to 32-bit-aligned-address + 2 */
  53. bmp = (struct bmp_image *)((((unsigned int)dst + 1) & ~3) + 2);
  54. if (gunzip(bmp, CONFIG_SYS_VIDEO_LOGO_MAX_SIZE, map_sysmem(addr, 0),
  55. &len) != 0) {
  56. free(dst);
  57. return NULL;
  58. }
  59. if (len == CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)
  60. puts("Image could be truncated"
  61. " (increase CONFIG_SYS_VIDEO_LOGO_MAX_SIZE)!\n");
  62. /*
  63. * Check for bmp mark 'BM'
  64. */
  65. if (!((bmp->header.signature[0] == 'B') &&
  66. (bmp->header.signature[1] == 'M'))) {
  67. free(dst);
  68. return NULL;
  69. }
  70. debug("Gzipped BMP image detected!\n");
  71. *alloc_addr = dst;
  72. return bmp;
  73. }
  74. #else
  75. struct bmp_image *gunzip_bmp(unsigned long addr, unsigned long *lenp,
  76. void **alloc_addr)
  77. {
  78. return NULL;
  79. }
  80. #endif
  81. static int do_bmp_info(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  82. {
  83. ulong addr;
  84. switch (argc) {
  85. case 1: /* use load_addr as default address */
  86. addr = load_addr;
  87. break;
  88. case 2: /* use argument */
  89. addr = simple_strtoul(argv[1], NULL, 16);
  90. break;
  91. default:
  92. return CMD_RET_USAGE;
  93. }
  94. return (bmp_info(addr));
  95. }
  96. static int do_bmp_display(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  97. {
  98. ulong addr;
  99. int x = 0, y = 0;
  100. splash_get_pos(&x, &y);
  101. switch (argc) {
  102. case 1: /* use load_addr as default address */
  103. addr = load_addr;
  104. break;
  105. case 2: /* use argument */
  106. addr = simple_strtoul(argv[1], NULL, 16);
  107. break;
  108. case 4:
  109. addr = simple_strtoul(argv[1], NULL, 16);
  110. x = simple_strtoul(argv[2], NULL, 10);
  111. y = simple_strtoul(argv[3], NULL, 10);
  112. break;
  113. default:
  114. return CMD_RET_USAGE;
  115. }
  116. return (bmp_display(addr, x, y));
  117. }
  118. static cmd_tbl_t cmd_bmp_sub[] = {
  119. U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
  120. U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
  121. };
  122. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  123. void bmp_reloc(void) {
  124. fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
  125. }
  126. #endif
  127. /*
  128. * Subroutine: do_bmp
  129. *
  130. * Description: Handler for 'bmp' command..
  131. *
  132. * Inputs: argv[1] contains the subcommand
  133. *
  134. * Return: None
  135. *
  136. */
  137. static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  138. {
  139. cmd_tbl_t *c;
  140. /* Strip off leading 'bmp' command argument */
  141. argc--;
  142. argv++;
  143. c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
  144. if (c)
  145. return c->cmd(cmdtp, flag, argc, argv);
  146. else
  147. return CMD_RET_USAGE;
  148. }
  149. U_BOOT_CMD(
  150. bmp, 5, 1, do_bmp,
  151. "manipulate BMP image data",
  152. "info <imageAddr> - display image info\n"
  153. "bmp display <imageAddr> [x y] - display image at x,y"
  154. );
  155. /*
  156. * Subroutine: bmp_info
  157. *
  158. * Description: Show information about bmp file in memory
  159. *
  160. * Inputs: addr address of the bmp file
  161. *
  162. * Return: None
  163. *
  164. */
  165. static int bmp_info(ulong addr)
  166. {
  167. struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
  168. void *bmp_alloc_addr = NULL;
  169. unsigned long len;
  170. if (!((bmp->header.signature[0]=='B') &&
  171. (bmp->header.signature[1]=='M')))
  172. bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
  173. if (bmp == NULL) {
  174. printf("There is no valid bmp file at the given address\n");
  175. return 1;
  176. }
  177. printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
  178. le32_to_cpu(bmp->header.height));
  179. printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
  180. printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
  181. if (bmp_alloc_addr)
  182. free(bmp_alloc_addr);
  183. return(0);
  184. }
  185. /*
  186. * Subroutine: bmp_display
  187. *
  188. * Description: Display bmp file located in memory
  189. *
  190. * Inputs: addr address of the bmp file
  191. *
  192. * Return: None
  193. *
  194. */
  195. int bmp_display(ulong addr, int x, int y)
  196. {
  197. #ifdef CONFIG_DM_VIDEO
  198. struct udevice *dev;
  199. #endif
  200. int ret;
  201. struct bmp_image *bmp = map_sysmem(addr, 0);
  202. void *bmp_alloc_addr = NULL;
  203. unsigned long len;
  204. if (!((bmp->header.signature[0]=='B') &&
  205. (bmp->header.signature[1]=='M')))
  206. bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
  207. if (!bmp) {
  208. printf("There is no valid bmp file at the given address\n");
  209. return 1;
  210. }
  211. addr = map_to_sysmem(bmp);
  212. #ifdef CONFIG_DM_VIDEO
  213. ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
  214. if (!ret) {
  215. bool align = false;
  216. # ifdef CONFIG_SPLASH_SCREEN_ALIGN
  217. align = true;
  218. # endif /* CONFIG_SPLASH_SCREEN_ALIGN */
  219. ret = video_bmp_display(dev, addr, x, y, align);
  220. }
  221. #elif defined(CONFIG_LCD)
  222. ret = lcd_display_bitmap(addr, x, y);
  223. #elif defined(CONFIG_VIDEO)
  224. ret = video_display_bitmap(addr, x, y);
  225. #else
  226. # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
  227. #endif
  228. if (bmp_alloc_addr)
  229. free(bmp_alloc_addr);
  230. return ret ? CMD_RET_FAILURE : 0;
  231. }