splash_source.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <splash.h>
  12. #include <spi_flash.h>
  13. #include <spi.h>
  14. #include <bmp_layout.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 and/or placed beyond DRAM boundaries.\n");
  86. return -EFAULT;
  87. }
  88. /**
  89. * select_splash_location - return the splash location based on board support
  90. * and env variable "splashsource".
  91. *
  92. * @locations: An array of supported splash locations.
  93. * @size: Size of splash_locations array.
  94. *
  95. * @return: If a null set of splash locations is given, or
  96. * splashsource env variable is set to unsupported value
  97. * return NULL.
  98. * If splashsource env variable is not defined
  99. * return the first entry in splash_locations as default.
  100. * If splashsource env variable contains a supported value
  101. * return the location selected by splashsource.
  102. */
  103. static struct splash_location *select_splash_location(
  104. struct splash_location *locations, uint size)
  105. {
  106. int i;
  107. char *env_splashsource;
  108. if (!locations || size == 0)
  109. return NULL;
  110. env_splashsource = getenv("splashsource");
  111. if (env_splashsource == NULL)
  112. return &locations[0];
  113. for (i = 0; i < size; i++) {
  114. if (!strcmp(locations[i].name, env_splashsource))
  115. return &locations[i];
  116. }
  117. printf("splashsource env variable set to unsupported value\n");
  118. return NULL;
  119. }
  120. /**
  121. * splash_source_load - load splash image from a supported location.
  122. *
  123. * Select a splash image location based on the value of splashsource environment
  124. * variable and the board supported splash source locations, and load a
  125. * splashimage to the address pointed to by splashimage environment variable.
  126. *
  127. * @locations: An array of supported splash locations.
  128. * @size: Size of splash_locations array.
  129. *
  130. * @return: 0 on success, negative value on failure.
  131. */
  132. int splash_source_load(struct splash_location *locations, uint size)
  133. {
  134. struct splash_location *splash_location;
  135. char *env_splashimage_value;
  136. u32 bmp_load_addr;
  137. env_splashimage_value = getenv("splashimage");
  138. if (env_splashimage_value == NULL)
  139. return -ENOENT;
  140. bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
  141. if (bmp_load_addr == 0) {
  142. printf("Error: bad splashimage address specified\n");
  143. return -EFAULT;
  144. }
  145. splash_location = select_splash_location(locations, size);
  146. if (!splash_location)
  147. return -EINVAL;
  148. return splash_load_raw(splash_location, bmp_load_addr);
  149. }