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 22 #ifndef DATASHARE_H_ 23 #define DATASHARE_H_ 24 25 /** 26 * This is an utility intended to be used by the booted application rather 27 * than by the bootloader 28 */ 29 #if MCUBOOT_DATA_SHARING && !MCUBOOT_BOOTLOADER_BUILD 30 31 #include <stdint.h> 32 #include "platform/Span.h" 33 34 /* Error codes */ 35 #define DATA_SHARE_OK 0 36 #define DATA_SHARE_ERROR_EOF 1 /* No more TLV entries available */ 37 #define DATA_SHARE_ERROR_OUT_OF_MEM 2 /* The output buffer was not large enough to hold the contents of the TLV entry */ 38 #define DATA_SHARE_ERROR_CORRUPT 3 /* Data corruption has been detected */ 39 40 /** 41 * Class enabling iterator-style access to the TLV-encoded data shared 42 * by an mcuboot-based bootloader. 43 */ 44 class DataShare 45 { 46 public: 47 48 /** 49 * Initializes a DataShare iterator at the given base address 50 * @note the configured MCUBOOT_SHARED_DATA_BASE address is used by default 51 */ 52 DataShare(uint8_t * shared_base = ((uint8_t *) MCUBOOT_SHARED_DATA_BASE)); 53 54 /** 55 * Validates the magic number of the shared data section 56 * @return true if magic number is found, false otherwise 57 */ 58 bool is_valid(); 59 60 /** 61 * Gets the total size of the shared data region 62 * @return 0 if shared data region is not valid, otherwise the size of the shared data region 63 */ 64 uint16_t get_total_size(); 65 66 /** 67 * Attempts to get the next TLV entry in the shared data memory 68 * @param[put] type Type code of the data entry 69 * @param[out] out Output buffer span 70 * @param[out] actual_size Size of entry copied to output buffer in bytes 71 * @return err Zero if output buffer contents is valid (successful), non-zero on failure 72 */ 73 int get_next(uint16_t *type, mbed::Span<uint8_t> buf, uint16_t *actual_size); 74 75 /** 76 * Resets the iterator-like pointer to the first TLV element in the shared 77 * data region 78 */ 79 void rewind(); 80 81 protected: 82 83 uint8_t *_shared_base; 84 bool _is_valid = false; 85 uint16_t _total_size = 0; 86 uint16_t _current_offset = 0; 87 88 }; 89 90 #endif /* MCUBOOT_DATA_SHARING && !MCUBOOT_BOOTLOADER_BUILD */ 91 92 #endif /* DATASHARE_H_ */ 93