Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Built with ARM Mbed-OS |
| 4 | * |
| 5 | * Copyright (c) 2019-2021 Embedded Planet, Inc. |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #if MCUBOOT_DATA_SHARING && !MCUBOOT_BOOTLOADER_BUILD |
| 22 | |
| 23 | #include "DataShare.h" |
| 24 | #include "boot_status.h" |
| 25 | |
| 26 | #include <cstring> |
| 27 | |
| 28 | #define PTR_TO_UINT8(x) ((volatile uint8_t *) x) |
| 29 | #define PTR_TO_UINT16(x) ((volatile uint16_t *) x) |
| 30 | #define PTR_TO_UINT32(x) ((volatile uint32_t *) x) |
| 31 | |
| 32 | #define SHARED_DATA_ENTRY_BASE MCUBOOT_SHARED_DATA_BASE+SHARED_DATA_HEADER_SIZE |
| 33 | |
| 34 | DataShare::DataShare(uint8_t *shared_base) : _shared_base(shared_base) { |
| 35 | volatile uint16_t *ptr = PTR_TO_UINT16(MCUBOOT_SHARED_DATA_BASE); |
| 36 | |
| 37 | /* Validate magic word */ |
| 38 | if(*ptr++ == SHARED_DATA_TLV_INFO_MAGIC) { |
| 39 | _is_valid = true; |
| 40 | } |
| 41 | |
| 42 | _total_size = *ptr; |
| 43 | |
| 44 | } |
| 45 | |
| 46 | bool DataShare::is_valid() { |
| 47 | return _is_valid; |
| 48 | } |
| 49 | |
| 50 | uint16_t DataShare::get_total_size() { |
| 51 | if(!is_valid()) { |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | return _total_size; |
| 56 | } |
| 57 | |
| 58 | int DataShare::get_next(uint16_t *type, mbed::Span<uint8_t> buf, uint16_t *actual_size) { |
| 59 | |
| 60 | if(!is_valid()) { |
| 61 | return DATA_SHARE_ERROR_CORRUPT; |
| 62 | } |
| 63 | |
| 64 | if(_current_offset >= (_total_size - SHARED_DATA_HEADER_SIZE)) { |
| 65 | return DATA_SHARE_ERROR_EOF; |
| 66 | } |
| 67 | |
| 68 | uint16_t working_offset = _current_offset; |
| 69 | |
| 70 | /* Get the type and the length */ |
| 71 | *type = *PTR_TO_UINT16((SHARED_DATA_ENTRY_BASE + working_offset)); |
| 72 | working_offset += sizeof(uint16_t); |
| 73 | *actual_size = *PTR_TO_UINT16((SHARED_DATA_ENTRY_BASE + working_offset)); |
| 74 | working_offset += sizeof(uint16_t); |
| 75 | |
| 76 | /* Check if the output buffer is large enough */ |
| 77 | if((size_t)buf.size() < *actual_size) { |
| 78 | return DATA_SHARE_ERROR_OUT_OF_MEM; |
| 79 | } |
| 80 | |
| 81 | /* Copy data of TLV entry */ |
| 82 | memcpy(buf.data(), (const uint8_t*)PTR_TO_UINT8((SHARED_DATA_ENTRY_BASE + working_offset)), *actual_size); |
| 83 | |
| 84 | working_offset += *actual_size; |
| 85 | |
| 86 | /* Update state */ |
| 87 | _current_offset = working_offset; |
| 88 | |
| 89 | return DATA_SHARE_OK; |
| 90 | |
| 91 | } |
| 92 | |
| 93 | void DataShare::rewind() { |
| 94 | _current_offset = 0; |
| 95 | } |
| 96 | |
| 97 | #endif /* MCUBOOT_DATA_SHARING && !MCUBOOT_BOOTLOADER_BUILD */ |