bmp.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 <image.h>
  15. #include <lcd.h>
  16. #include <malloc.h>
  17. #include <mapmem.h>
  18. #include <splash.h>
  19. #include <video.h>
  20. #include <asm/byteorder.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 *)((((uintptr_t)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 image_load_addr as default address */
  87. addr = image_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 image_load_addr as default address */
  104. addr = image_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. if (!strcmp(argv[2], "m"))
  112. x = BMP_ALIGN_CENTER;
  113. else
  114. x = simple_strtoul(argv[2], NULL, 10);
  115. if (!strcmp(argv[3], "m"))
  116. y = BMP_ALIGN_CENTER;
  117. else
  118. y = simple_strtoul(argv[3], NULL, 10);
  119. break;
  120. default:
  121. return CMD_RET_USAGE;
  122. }
  123. return (bmp_display(addr, x, y));
  124. }
  125. static cmd_tbl_t cmd_bmp_sub[] = {
  126. U_BOOT_CMD_MKENT(info, 3, 0, do_bmp_info, "", ""),
  127. U_BOOT_CMD_MKENT(display, 5, 0, do_bmp_display, "", ""),
  128. };
  129. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  130. void bmp_reloc(void) {
  131. fixup_cmdtable(cmd_bmp_sub, ARRAY_SIZE(cmd_bmp_sub));
  132. }
  133. #endif
  134. /*
  135. * Subroutine: do_bmp
  136. *
  137. * Description: Handler for 'bmp' command..
  138. *
  139. * Inputs: argv[1] contains the subcommand
  140. *
  141. * Return: None
  142. *
  143. */
  144. static int do_bmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  145. {
  146. cmd_tbl_t *c;
  147. /* Strip off leading 'bmp' command argument */
  148. argc--;
  149. argv++;
  150. c = find_cmd_tbl(argv[0], &cmd_bmp_sub[0], ARRAY_SIZE(cmd_bmp_sub));
  151. if (c)
  152. return c->cmd(cmdtp, flag, argc, argv);
  153. else
  154. return CMD_RET_USAGE;
  155. }
  156. U_BOOT_CMD(
  157. bmp, 5, 1, do_bmp,
  158. "manipulate BMP image data",
  159. "info <imageAddr> - display image info\n"
  160. "bmp display <imageAddr> [x y] - display image at x,y"
  161. );
  162. /*
  163. * Subroutine: bmp_info
  164. *
  165. * Description: Show information about bmp file in memory
  166. *
  167. * Inputs: addr address of the bmp file
  168. *
  169. * Return: None
  170. *
  171. */
  172. static int bmp_info(ulong addr)
  173. {
  174. struct bmp_image *bmp = (struct bmp_image *)map_sysmem(addr, 0);
  175. void *bmp_alloc_addr = NULL;
  176. unsigned long len;
  177. if (!((bmp->header.signature[0]=='B') &&
  178. (bmp->header.signature[1]=='M')))
  179. bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
  180. if (bmp == NULL) {
  181. printf("There is no valid bmp file at the given address\n");
  182. return 1;
  183. }
  184. printf("Image size : %d x %d\n", le32_to_cpu(bmp->header.width),
  185. le32_to_cpu(bmp->header.height));
  186. printf("Bits per pixel: %d\n", le16_to_cpu(bmp->header.bit_count));
  187. printf("Compression : %d\n", le32_to_cpu(bmp->header.compression));
  188. if (bmp_alloc_addr)
  189. free(bmp_alloc_addr);
  190. return(0);
  191. }
  192. /*
  193. * Subroutine: bmp_display
  194. *
  195. * Description: Display bmp file located in memory
  196. *
  197. * Inputs: addr address of the bmp file
  198. *
  199. * Return: None
  200. *
  201. */
  202. int bmp_display(ulong addr, int x, int y)
  203. {
  204. #ifdef CONFIG_DM_VIDEO
  205. struct udevice *dev;
  206. #endif
  207. int ret;
  208. struct bmp_image *bmp = map_sysmem(addr, 0);
  209. void *bmp_alloc_addr = NULL;
  210. unsigned long len;
  211. if (!((bmp->header.signature[0]=='B') &&
  212. (bmp->header.signature[1]=='M')))
  213. bmp = gunzip_bmp(addr, &len, &bmp_alloc_addr);
  214. if (!bmp) {
  215. printf("There is no valid bmp file at the given address\n");
  216. return 1;
  217. }
  218. addr = map_to_sysmem(bmp);
  219. #ifdef CONFIG_DM_VIDEO
  220. ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
  221. if (!ret) {
  222. bool align = false;
  223. if (CONFIG_IS_ENABLED(SPLASH_SCREEN_ALIGN) ||
  224. x == BMP_ALIGN_CENTER ||
  225. y == BMP_ALIGN_CENTER)
  226. align = true;
  227. ret = video_bmp_display(dev, addr, x, y, align);
  228. }
  229. #elif defined(CONFIG_LCD)
  230. ret = lcd_display_bitmap(addr, x, y);
  231. #elif defined(CONFIG_VIDEO)
  232. ret = video_display_bitmap(addr, x, y);
  233. #else
  234. # error bmp_display() requires CONFIG_LCD or CONFIG_VIDEO
  235. #endif
  236. if (bmp_alloc_addr)
  237. free(bmp_alloc_addr);
  238. return ret ? CMD_RET_FAILURE : 0;
  239. }