bmp.c 5.9 KB

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