cmd_bmp.c 5.3 KB

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