splash.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il>
  3. *
  4. * Authors: Igor Grinberg <grinberg@compulab.co.il>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <nand.h>
  10. #include <errno.h>
  11. #include <spi_flash.h>
  12. #include <spi.h>
  13. #include <bmp_layout.h>
  14. #include "common.h"
  15. DECLARE_GLOBAL_DATA_PTR;
  16. #ifdef CONFIG_SPI_FLASH
  17. static struct spi_flash *sf;
  18. static int splash_sf_read(u32 bmp_load_addr, int offset, size_t read_size)
  19. {
  20. if (!sf) {
  21. sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
  22. CONFIG_SF_DEFAULT_CS,
  23. CONFIG_SF_DEFAULT_SPEED,
  24. CONFIG_SF_DEFAULT_MODE);
  25. if (!sf)
  26. return -ENODEV;
  27. }
  28. return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
  29. }
  30. #else
  31. static int splash_sf_read(u32 bmp_load_addr, int offset, size_t read_size)
  32. {
  33. debug("%s: sf support not available\n", __func__);
  34. return -ENOSYS;
  35. }
  36. #endif
  37. #ifdef CONFIG_CMD_NAND
  38. static int splash_nand_read(u32 bmp_load_addr, int offset, size_t read_size)
  39. {
  40. return nand_read_skip_bad(&nand_info[nand_curr_device], offset,
  41. &read_size, NULL,
  42. nand_info[nand_curr_device].size,
  43. (u_char *)bmp_load_addr);
  44. }
  45. #else
  46. static int splash_nand_read(u32 bmp_load_addr, int offset, size_t read_size)
  47. {
  48. debug("%s: nand support not available\n", __func__);
  49. return -ENOSYS;
  50. }
  51. #endif
  52. static int splash_storage_read(struct splash_location *location,
  53. u32 bmp_load_addr, size_t read_size)
  54. {
  55. u32 offset;
  56. if (!location)
  57. return -EINVAL;
  58. offset = location->offset;
  59. switch (location->storage) {
  60. case SPLASH_STORAGE_NAND:
  61. return splash_nand_read(bmp_load_addr, offset, read_size);
  62. case SPLASH_STORAGE_SF:
  63. return splash_sf_read(bmp_load_addr, offset, read_size);
  64. default:
  65. printf("Unknown splash location\n");
  66. }
  67. return -EINVAL;
  68. }
  69. static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
  70. {
  71. struct bmp_header *bmp_hdr;
  72. int res;
  73. size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
  74. if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
  75. goto splash_address_too_high;
  76. res = splash_storage_read(location, bmp_load_addr, bmp_header_size);
  77. if (res < 0)
  78. return res;
  79. bmp_hdr = (struct bmp_header *)bmp_load_addr;
  80. bmp_size = le32_to_cpu(bmp_hdr->file_size);
  81. if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
  82. goto splash_address_too_high;
  83. return splash_storage_read(location, bmp_load_addr, bmp_size);
  84. splash_address_too_high:
  85. printf("Error: splashimage address too high. Data overwrites U-Boot "
  86. "and/or placed beyond DRAM boundaries.\n");
  87. return -EFAULT;
  88. }
  89. /**
  90. * select_splash_location - return the splash location based on board support
  91. * and env variable "splashsource".
  92. *
  93. * @locations: An array of supported splash locations.
  94. * @size: Size of splash_locations array.
  95. *
  96. * @return: If a null set of splash locations is given, or
  97. * splashsource env variable is set to unsupported value
  98. * return NULL.
  99. * If splashsource env variable is not defined
  100. * return the first entry in splash_locations as default.
  101. * If splashsource env variable contains a supported value
  102. * return the location selected by splashsource.
  103. */
  104. static struct splash_location *select_splash_location(
  105. struct splash_location *locations, uint size)
  106. {
  107. int i;
  108. char *env_splashsource;
  109. if (!locations || size == 0)
  110. return NULL;
  111. env_splashsource = getenv("splashsource");
  112. if (env_splashsource == NULL)
  113. return &locations[0];
  114. for (i = 0; i < size; i++) {
  115. if (!strcmp(locations[i].name, env_splashsource))
  116. return &locations[i];
  117. }
  118. printf("splashsource env variable set to unsupported value\n");
  119. return NULL;
  120. }
  121. int cl_splash_screen_prepare(struct splash_location *locations, uint size)
  122. {
  123. struct splash_location *splash_location;
  124. char *env_splashimage_value;
  125. u32 bmp_load_addr;
  126. env_splashimage_value = getenv("splashimage");
  127. if (env_splashimage_value == NULL)
  128. return -ENOENT;
  129. bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
  130. if (bmp_load_addr == 0) {
  131. printf("Error: bad splashimage address specified\n");
  132. return -EFAULT;
  133. }
  134. splash_location = select_splash_location(locations, size);
  135. if (!splash_location)
  136. return -EINVAL;
  137. return splash_load_raw(splash_location, bmp_load_addr);
  138. }