Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved. |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | #include <string.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | |
| 12 | #include <platform_def.h> |
| 13 | |
| 14 | #include <common/debug.h> |
| 15 | #include <drivers/io/io_driver.h> |
| 16 | #include <drivers/io/io_storage.h> |
| 17 | #include <drivers/st/io_stm32image.h> |
| 18 | #include <lib/utils.h> |
| 19 | #include <plat/common/platform.h> |
| 20 | |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 21 | static uintptr_t backend_dev_handle; |
| 22 | static uintptr_t backend_image_spec; |
| 23 | static uint32_t *stm32_img; |
| 24 | static uint8_t first_lba_buffer[MAX_LBA_SIZE] __aligned(4); |
| 25 | static struct stm32image_part_info *current_part; |
| 26 | |
| 27 | /* STM32 Image driver functions */ |
| 28 | static int stm32image_dev_open(const uintptr_t init_params, |
| 29 | io_dev_info_t **dev_info); |
| 30 | static int stm32image_partition_open(io_dev_info_t *dev_info, |
| 31 | const uintptr_t spec, io_entity_t *entity); |
| 32 | static int stm32image_partition_size(io_entity_t *entity, size_t *length); |
| 33 | static int stm32image_partition_read(io_entity_t *entity, uintptr_t buffer, |
| 34 | size_t length, size_t *length_read); |
| 35 | static int stm32image_partition_close(io_entity_t *entity); |
| 36 | static int stm32image_dev_init(io_dev_info_t *dev_info, |
| 37 | const uintptr_t init_params); |
| 38 | static int stm32image_dev_close(io_dev_info_t *dev_info); |
| 39 | |
| 40 | /* Identify the device type as a virtual driver */ |
| 41 | static io_type_t device_type_stm32image(void) |
| 42 | { |
| 43 | return IO_TYPE_STM32IMAGE; |
| 44 | } |
| 45 | |
| 46 | static const io_dev_connector_t stm32image_dev_connector = { |
| 47 | .dev_open = stm32image_dev_open |
| 48 | }; |
| 49 | |
| 50 | static const io_dev_funcs_t stm32image_dev_funcs = { |
| 51 | .type = device_type_stm32image, |
| 52 | .open = stm32image_partition_open, |
| 53 | .size = stm32image_partition_size, |
| 54 | .read = stm32image_partition_read, |
| 55 | .close = stm32image_partition_close, |
| 56 | .dev_init = stm32image_dev_init, |
| 57 | .dev_close = stm32image_dev_close, |
| 58 | }; |
| 59 | |
| 60 | static io_dev_info_t stm32image_dev_info = { |
| 61 | .funcs = &stm32image_dev_funcs, |
| 62 | .info = (uintptr_t)0, |
| 63 | }; |
| 64 | |
| 65 | static struct stm32image_device_info stm32image_dev; |
| 66 | |
| 67 | static int get_part_idx_by_binary_type(uint32_t binary_type) |
| 68 | { |
| 69 | int i; |
| 70 | |
| 71 | for (i = 0; i < STM32_PART_NUM; i++) { |
| 72 | if (stm32image_dev.part_info[i].binary_type == binary_type) { |
| 73 | return i; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return -EINVAL; |
| 78 | } |
| 79 | |
| 80 | /* Open a connection to the STM32IMAGE device */ |
| 81 | static int stm32image_dev_open(const uintptr_t init_params, |
| 82 | io_dev_info_t **dev_info) |
| 83 | { |
| 84 | int i; |
| 85 | struct stm32image_device_info *device_info = |
| 86 | (struct stm32image_device_info *)init_params; |
| 87 | |
| 88 | assert(dev_info != NULL); |
| 89 | *dev_info = (io_dev_info_t *)&stm32image_dev_info; |
| 90 | |
| 91 | stm32image_dev.device_size = device_info->device_size; |
| 92 | stm32image_dev.lba_size = device_info->lba_size; |
| 93 | |
| 94 | for (i = 0; i < STM32_PART_NUM; i++) { |
| 95 | memcpy(stm32image_dev.part_info[i].name, |
| 96 | device_info->part_info[i].name, MAX_PART_NAME_SIZE); |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 97 | stm32image_dev.part_info[i].binary_type = |
| 98 | device_info->part_info[i].binary_type; |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 99 | stm32image_dev.part_info[i].part_offset = |
| 100 | device_info->part_info[i].part_offset; |
| 101 | stm32image_dev.part_info[i].bkp_offset = |
| 102 | device_info->part_info[i].bkp_offset; |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /* Do some basic package checks */ |
| 109 | static int stm32image_dev_init(io_dev_info_t *dev_info, |
| 110 | const uintptr_t init_params) |
| 111 | { |
| 112 | int result; |
| 113 | |
| 114 | if ((backend_dev_handle != 0U) || (backend_image_spec != 0U)) { |
| 115 | ERROR("STM32 Image io supports only one session\n"); |
| 116 | return -ENOMEM; |
| 117 | } |
| 118 | |
| 119 | /* Obtain a reference to the image by querying the platform layer */ |
| 120 | result = plat_get_image_source(STM32_IMAGE_ID, &backend_dev_handle, |
| 121 | &backend_image_spec); |
| 122 | if (result != 0) { |
| 123 | ERROR("STM32 image error (%i)\n", result); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | |
| 127 | return result; |
| 128 | } |
| 129 | |
| 130 | /* Close a connection to the STM32 Image device */ |
| 131 | static int stm32image_dev_close(io_dev_info_t *dev_info) |
| 132 | { |
| 133 | backend_dev_handle = 0U; |
| 134 | backend_image_spec = 0U; |
| 135 | stm32_img = NULL; |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | /* Open a partition */ |
| 141 | static int stm32image_partition_open(io_dev_info_t *dev_info, |
| 142 | const uintptr_t spec, io_entity_t *entity) |
| 143 | { |
| 144 | const struct stm32image_part_info *partition_spec; |
| 145 | int idx; |
| 146 | |
| 147 | assert(entity != NULL); |
| 148 | |
| 149 | partition_spec = (struct stm32image_part_info *)spec; |
| 150 | assert(partition_spec != NULL); |
| 151 | |
| 152 | idx = get_part_idx_by_binary_type(partition_spec->binary_type); |
| 153 | if ((idx < 0) || (idx > STM32_PART_NUM)) { |
| 154 | ERROR("Wrong partition index (%d)\n", idx); |
| 155 | return -EINVAL; |
| 156 | } |
| 157 | |
| 158 | current_part = &stm32image_dev.part_info[idx]; |
| 159 | stm32_img = (uint32_t *)¤t_part->part_offset; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | /* Return the size of a partition */ |
| 165 | static int stm32image_partition_size(io_entity_t *entity, size_t *length) |
| 166 | { |
| 167 | int result; |
| 168 | uintptr_t backend_handle; |
| 169 | size_t bytes_read; |
| 170 | boot_api_image_header_t *header = |
| 171 | (boot_api_image_header_t *)first_lba_buffer; |
| 172 | |
| 173 | assert(entity != NULL); |
| 174 | assert(length != NULL); |
| 175 | |
| 176 | /* Attempt to access the image */ |
| 177 | result = io_open(backend_dev_handle, backend_image_spec, |
| 178 | &backend_handle); |
| 179 | |
| 180 | if (result < 0) { |
| 181 | ERROR("%s: io_open (%i)\n", __func__, result); |
| 182 | return result; |
| 183 | } |
| 184 | |
| 185 | /* Reset magic header value */ |
| 186 | header->magic = 0; |
| 187 | |
| 188 | while (header->magic == 0U) { |
| 189 | result = io_seek(backend_handle, IO_SEEK_SET, *stm32_img); |
| 190 | if (result != 0) { |
| 191 | ERROR("%s: io_seek (%i)\n", __func__, result); |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | result = io_read(backend_handle, (uintptr_t)header, |
| 196 | MAX_LBA_SIZE, (size_t *)&bytes_read); |
| 197 | if (result != 0) { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 198 | if (current_part->bkp_offset == 0U) { |
| 199 | ERROR("%s: io_read (%i)\n", __func__, result); |
| 200 | } |
| 201 | header->magic = 0; |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if ((header->magic != BOOT_API_IMAGE_HEADER_MAGIC_NB) || |
| 205 | (header->binary_type != current_part->binary_type) || |
| 206 | (header->image_length >= stm32image_dev.device_size)) { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 207 | VERBOSE("%s: partition %s not found at %x\n", |
| 208 | __func__, current_part->name, *stm32_img); |
| 209 | |
| 210 | if (current_part->bkp_offset == 0U) { |
| 211 | result = -ENOMEM; |
| 212 | break; |
| 213 | } |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 214 | |
| 215 | /* Header not correct, check next offset for backup */ |
| 216 | *stm32_img += current_part->bkp_offset; |
| 217 | if (*stm32_img > stm32image_dev.device_size) { |
| 218 | /* No backup found, end of device reached */ |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 219 | WARN("%s : partition %s not found\n", |
| 220 | __func__, current_part->name); |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 221 | result = -ENOMEM; |
| 222 | break; |
| 223 | } |
| 224 | header->magic = 0; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | io_close(backend_handle); |
| 229 | |
| 230 | if (result != 0) { |
| 231 | return result; |
| 232 | } |
| 233 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 234 | if (header->image_length < stm32image_dev.lba_size) { |
| 235 | *length = stm32image_dev.lba_size; |
| 236 | } else { |
| 237 | *length = header->image_length; |
| 238 | } |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 239 | |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 240 | INFO("STM32 Image size : %lu\n", (unsigned long)*length); |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 245 | /* Read data from a partition */ |
| 246 | static int stm32image_partition_read(io_entity_t *entity, uintptr_t buffer, |
| 247 | size_t length, size_t *length_read) |
| 248 | { |
Lionel Debieve | 7bd96f4 | 2019-09-03 12:22:23 +0200 | [diff] [blame] | 249 | int result; |
Yann Gautier | 3dcc5f1 | 2020-03-18 14:50:50 +0100 | [diff] [blame] | 250 | uint8_t *local_buffer; |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 251 | boot_api_image_header_t *header = |
| 252 | (boot_api_image_header_t *)first_lba_buffer; |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 253 | |
| 254 | assert(entity != NULL); |
| 255 | assert(buffer != 0U); |
| 256 | assert(length_read != NULL); |
| 257 | |
Yann Gautier | 3dcc5f1 | 2020-03-18 14:50:50 +0100 | [diff] [blame] | 258 | local_buffer = (uint8_t *)buffer; |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 259 | *length_read = 0U; |
| 260 | |
| 261 | while (*length_read == 0U) { |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 262 | int offset; |
| 263 | int local_length; |
| 264 | uintptr_t backend_handle; |
| 265 | |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 266 | if (header->magic != BOOT_API_IMAGE_HEADER_MAGIC_NB) { |
| 267 | /* Check for backup as image is corrupted */ |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 268 | if (current_part->bkp_offset == 0U) { |
| 269 | result = -ENOMEM; |
| 270 | break; |
| 271 | } |
| 272 | |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 273 | *stm32_img += current_part->bkp_offset; |
| 274 | if (*stm32_img >= stm32image_dev.device_size) { |
| 275 | /* End of device reached */ |
| 276 | result = -ENOMEM; |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | local_buffer = (uint8_t *)buffer; |
| 281 | |
| 282 | result = stm32image_partition_size(entity, &length); |
| 283 | if (result != 0) { |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* Part of image already loaded with the header */ |
| 289 | memcpy(local_buffer, (uint8_t *)first_lba_buffer + |
| 290 | sizeof(boot_api_image_header_t), |
| 291 | MAX_LBA_SIZE - sizeof(boot_api_image_header_t)); |
| 292 | local_buffer += MAX_LBA_SIZE - sizeof(boot_api_image_header_t); |
| 293 | offset = MAX_LBA_SIZE; |
| 294 | |
| 295 | /* New image length to be read */ |
| 296 | local_length = round_up(length - |
| 297 | ((MAX_LBA_SIZE) - |
| 298 | sizeof(boot_api_image_header_t)), |
| 299 | stm32image_dev.lba_size); |
| 300 | |
| 301 | if ((header->load_address != 0U) && |
| 302 | (header->load_address != buffer)) { |
| 303 | ERROR("Wrong load address\n"); |
| 304 | panic(); |
| 305 | } |
| 306 | |
| 307 | result = io_open(backend_dev_handle, backend_image_spec, |
| 308 | &backend_handle); |
| 309 | |
| 310 | if (result != 0) { |
| 311 | ERROR("%s: io_open (%i)\n", __func__, result); |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | result = io_seek(backend_handle, IO_SEEK_SET, |
| 316 | *stm32_img + offset); |
| 317 | |
| 318 | if (result != 0) { |
| 319 | ERROR("%s: io_seek (%i)\n", __func__, result); |
| 320 | *length_read = 0; |
| 321 | io_close(backend_handle); |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | result = io_read(backend_handle, (uintptr_t)local_buffer, |
| 326 | local_length, length_read); |
| 327 | |
| 328 | /* Adding part of size already read from header */ |
| 329 | *length_read += MAX_LBA_SIZE - sizeof(boot_api_image_header_t); |
| 330 | |
| 331 | if (result != 0) { |
| 332 | ERROR("%s: io_read (%i)\n", __func__, result); |
| 333 | *length_read = 0; |
Yann Gautier | 1a3fc9f | 2019-01-17 14:35:22 +0100 | [diff] [blame] | 334 | header->magic = 0; |
| 335 | continue; |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Yann Gautier | e97b663 | 2019-04-19 10:48:36 +0200 | [diff] [blame] | 338 | result = stm32mp_check_header(header, buffer); |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 339 | if (result != 0) { |
| 340 | ERROR("Header check failed\n"); |
| 341 | *length_read = 0; |
| 342 | header->magic = 0; |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Lionel Debieve | 7bd96f4 | 2019-09-03 12:22:23 +0200 | [diff] [blame] | 345 | result = stm32mp_auth_image(header, buffer); |
| 346 | if (result != 0) { |
| 347 | ERROR("Authentication Failed (%i)\n", result); |
| 348 | return result; |
| 349 | } |
| 350 | |
Yann Gautier | eb16b47 | 2018-10-15 09:36:32 +0200 | [diff] [blame] | 351 | io_close(backend_handle); |
| 352 | } |
| 353 | |
| 354 | return result; |
| 355 | } |
| 356 | |
| 357 | /* Close a partition */ |
| 358 | static int stm32image_partition_close(io_entity_t *entity) |
| 359 | { |
| 360 | current_part = NULL; |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | /* Register the stm32image driver with the IO abstraction */ |
| 366 | int register_io_dev_stm32image(const io_dev_connector_t **dev_con) |
| 367 | { |
| 368 | int result; |
| 369 | |
| 370 | assert(dev_con != NULL); |
| 371 | |
| 372 | result = io_register_device(&stm32image_dev_info); |
| 373 | if (result == 0) { |
| 374 | *dev_con = &stm32image_dev_connector; |
| 375 | } |
| 376 | |
| 377 | return result; |
| 378 | } |