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