bmp.c 5.9 KB

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