bmp.c 5.7 KB

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