leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 2 // Copyright 2016-2017 ARM Ltd.
leothedragon 0:8f0bb79ddd48 3 //
leothedragon 0:8f0bb79ddd48 4 // SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 5 //
leothedragon 0:8f0bb79ddd48 6 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:8f0bb79ddd48 7 // you may not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 8 // You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 9 //
leothedragon 0:8f0bb79ddd48 10 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 11 //
leothedragon 0:8f0bb79ddd48 12 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 13 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:8f0bb79ddd48 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 15 // See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 16 // limitations under the License.
leothedragon 0:8f0bb79ddd48 17 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 #include "arm_uc_mmCommon.h"
leothedragon 0:8f0bb79ddd48 20 #include "arm_uc_mmConfig.h"
leothedragon 0:8f0bb79ddd48 21 #include "arm_uc_mmStateSelector.h"
leothedragon 0:8f0bb79ddd48 22 #include "arm_uc_mmInit.h"
leothedragon 0:8f0bb79ddd48 23
leothedragon 0:8f0bb79ddd48 24 #include "update-client-manifest-manager/update-client-manifest-manager.h"
leothedragon 0:8f0bb79ddd48 25 #include "update-client-manifest-manager/update-client-manifest-manager-context.h"
leothedragon 0:8f0bb79ddd48 26 #include "update-client-manifest-manager/update-client-manifest-types.h"
leothedragon 0:8f0bb79ddd48 27 #include "update-client-manifest-manager/arm-pal-kv.h"
leothedragon 0:8f0bb79ddd48 28
leothedragon 0:8f0bb79ddd48 29 #include "update-client-common/arm_uc_scheduler.h"
leothedragon 0:8f0bb79ddd48 30 #include "update-client-common/arm_uc_utilities.h"
leothedragon 0:8f0bb79ddd48 31 #include "update-client-common/arm_uc_error.h"
leothedragon 0:8f0bb79ddd48 32
leothedragon 0:8f0bb79ddd48 33 #include <stdint.h>
leothedragon 0:8f0bb79ddd48 34 #include <stdio.h>
leothedragon 0:8f0bb79ddd48 35 #include <stddef.h>
leothedragon 0:8f0bb79ddd48 36 #include <string.h>
leothedragon 0:8f0bb79ddd48 37
leothedragon 0:8f0bb79ddd48 38 /**
leothedragon 0:8f0bb79ddd48 39 * @file manifest_manager.c
leothedragon 0:8f0bb79ddd48 40 * @brief Manifest Manager API
leothedragon 0:8f0bb79ddd48 41 * @details This file specifies the API used to interact with the manifest manager
leothedragon 0:8f0bb79ddd48 42 */
leothedragon 0:8f0bb79ddd48 43
leothedragon 0:8f0bb79ddd48 44 arm_uc_error_t ARM_UC_mmInit(arm_uc_mmContext_t **mmCtx, void (*event_handler)(uintptr_t),
leothedragon 0:8f0bb79ddd48 45 const arm_pal_key_value_api *api)
leothedragon 0:8f0bb79ddd48 46 {
leothedragon 0:8f0bb79ddd48 47 arm_uc_error_t err = {ERR_NONE};
leothedragon 0:8f0bb79ddd48 48 if (mmCtx == NULL || *mmCtx == NULL) {
leothedragon 0:8f0bb79ddd48 49 return (arm_uc_error_t) {MFST_ERR_NULL_PTR};
leothedragon 0:8f0bb79ddd48 50 }
leothedragon 0:8f0bb79ddd48 51
leothedragon 0:8f0bb79ddd48 52 arm_uc_mmPersistentContext.ctx = mmCtx;
leothedragon 0:8f0bb79ddd48 53 arm_uc_mmPersistentContext.applicationEventHandler = event_handler;
leothedragon 0:8f0bb79ddd48 54 arm_uc_mmPersistentContext.testFSM = NULL;
leothedragon 0:8f0bb79ddd48 55
leothedragon 0:8f0bb79ddd48 56 // initialize callback node
leothedragon 0:8f0bb79ddd48 57 arm_uc_mmPersistentContext.applicationCallbackStorage.lock = 0;
leothedragon 0:8f0bb79ddd48 58
leothedragon 0:8f0bb79ddd48 59 ARM_UC_PostCallback(&arm_uc_mmPersistentContext.applicationCallbackStorage, event_handler, ARM_UC_MM_RC_DONE);
leothedragon 0:8f0bb79ddd48 60 // This code will be re-enabled when storage is available
leothedragon 0:8f0bb79ddd48 61 #if 0
leothedragon 0:8f0bb79ddd48 62 ARM_UC_mmCfStoreInit(api);
leothedragon 0:8f0bb79ddd48 63
leothedragon 0:8f0bb79ddd48 64 // Initialize the Init FSM
leothedragon 0:8f0bb79ddd48 65 arm_uc_mmContext_t *ctx = *mmCtx;
leothedragon 0:8f0bb79ddd48 66 ctx->init.state = ARM_UC_MM_INIT_BEGIN;
leothedragon 0:8f0bb79ddd48 67
leothedragon 0:8f0bb79ddd48 68 err = ARM_UC_mmSetState(ARM_UC_MM_STATE_INIT);
leothedragon 0:8f0bb79ddd48 69 if (err.code != ERR_NONE) {
leothedragon 0:8f0bb79ddd48 70 return err;
leothedragon 0:8f0bb79ddd48 71 }
leothedragon 0:8f0bb79ddd48 72 // Start the Init FSM
leothedragon 0:8f0bb79ddd48 73 ARM_UC_PostCallback(&ctx->init.callbackStorage, ARM_UC_mmCallbackFSMEntry, ARM_UC_MM_EVENT_BEGIN);
leothedragon 0:8f0bb79ddd48 74 #endif
leothedragon 0:8f0bb79ddd48 75 return err;
leothedragon 0:8f0bb79ddd48 76 }
leothedragon 0:8f0bb79ddd48 77
leothedragon 0:8f0bb79ddd48 78 arm_uc_error_t ARM_UC_mmInsert(arm_uc_mmContext_t **ctx, arm_uc_buffer_t *buffer, arm_uc_buffer_t *certificateStorage,
leothedragon 0:8f0bb79ddd48 79 arm_uc_manifest_handle_t *ID)
leothedragon 0:8f0bb79ddd48 80 {
leothedragon 0:8f0bb79ddd48 81 if (ctx == NULL || *ctx == NULL || buffer == NULL) {
leothedragon 0:8f0bb79ddd48 82 return (arm_uc_error_t) {MFST_ERR_NULL_PTR};
leothedragon 0:8f0bb79ddd48 83 }
leothedragon 0:8f0bb79ddd48 84 arm_uc_mmPersistentContext.ctx = ctx;
leothedragon 0:8f0bb79ddd48 85 // Setup the state machine
leothedragon 0:8f0bb79ddd48 86 arm_uc_error_t err = ARM_UC_mmSetState(ARM_UC_MM_STATE_INSERTING);
leothedragon 0:8f0bb79ddd48 87 if (err.code != ERR_NONE) {
leothedragon 0:8f0bb79ddd48 88 return err;
leothedragon 0:8f0bb79ddd48 89 }
leothedragon 0:8f0bb79ddd48 90 struct arm_uc_mmInsertContext_t *insertCtx = &(*arm_uc_mmPersistentContext.ctx)->insert;
leothedragon 0:8f0bb79ddd48 91 // Store the buffer pointer
leothedragon 0:8f0bb79ddd48 92 ARM_UC_buffer_shallow_copy(&insertCtx->manifest, buffer);
leothedragon 0:8f0bb79ddd48 93 insertCtx->state = ARM_UC_MM_INS_STATE_BEGIN;
leothedragon 0:8f0bb79ddd48 94 // Store the ID pointer
leothedragon 0:8f0bb79ddd48 95 insertCtx->ID = ID;
leothedragon 0:8f0bb79ddd48 96 // Store the certificate buffer
leothedragon 0:8f0bb79ddd48 97 ARM_UC_buffer_shallow_copy(&insertCtx->certificateStorage, certificateStorage);
leothedragon 0:8f0bb79ddd48 98
leothedragon 0:8f0bb79ddd48 99 // initialize callback node
leothedragon 0:8f0bb79ddd48 100 insertCtx->callbackStorage.lock = 0;
leothedragon 0:8f0bb79ddd48 101
leothedragon 0:8f0bb79ddd48 102 // Start the FSM
leothedragon 0:8f0bb79ddd48 103 ARM_UC_PostCallback(&insertCtx->callbackStorage, ARM_UC_mmCallbackFSMEntry, ARM_UC_MM_EVENT_BEGIN);
leothedragon 0:8f0bb79ddd48 104 return (arm_uc_error_t) {ERR_NONE};
leothedragon 0:8f0bb79ddd48 105 }
leothedragon 0:8f0bb79ddd48 106 arm_uc_error_t ARM_UC_mmFetchFirmwareInfo(arm_uc_mmContext_t **ctx, struct manifest_firmware_info_t *info,
leothedragon 0:8f0bb79ddd48 107 const arm_uc_manifest_handle_t *ID)
leothedragon 0:8f0bb79ddd48 108 {
leothedragon 0:8f0bb79ddd48 109 if (ctx == NULL || *ctx == NULL || info == NULL) {
leothedragon 0:8f0bb79ddd48 110 return (arm_uc_error_t) {MFST_ERR_NULL_PTR};
leothedragon 0:8f0bb79ddd48 111 }
leothedragon 0:8f0bb79ddd48 112 arm_uc_mmPersistentContext.ctx = ctx;
leothedragon 0:8f0bb79ddd48 113 // Initialize the state machine
leothedragon 0:8f0bb79ddd48 114 arm_uc_error_t err = ARM_UC_mmSetState(ARM_UC_MM_STATE_FWINFO);
leothedragon 0:8f0bb79ddd48 115 if (err.code != ERR_NONE) {
leothedragon 0:8f0bb79ddd48 116 return err;
leothedragon 0:8f0bb79ddd48 117 }
leothedragon 0:8f0bb79ddd48 118 struct arm_uc_mm_fw_context_t *fwCtx = &(*arm_uc_mmPersistentContext.ctx)->getFw;
leothedragon 0:8f0bb79ddd48 119 fwCtx->state = ARM_UC_MM_FW_STATE_BEGIN;
leothedragon 0:8f0bb79ddd48 120 fwCtx->info = info;
leothedragon 0:8f0bb79ddd48 121
leothedragon 0:8f0bb79ddd48 122 // initialize callback node
leothedragon 0:8f0bb79ddd48 123 fwCtx->callbackStorage.lock = 0;
leothedragon 0:8f0bb79ddd48 124
leothedragon 0:8f0bb79ddd48 125 // Start the state machine
leothedragon 0:8f0bb79ddd48 126 ARM_UC_PostCallback(&fwCtx->callbackStorage, ARM_UC_mmCallbackFSMEntry, ARM_UC_MM_EVENT_BEGIN);
leothedragon 0:8f0bb79ddd48 127
leothedragon 0:8f0bb79ddd48 128 return (arm_uc_error_t) {ERR_NONE};
leothedragon 0:8f0bb79ddd48 129 }
leothedragon 0:8f0bb79ddd48 130 arm_uc_error_t ARM_UC_mmFetchNextFirmwareInfo(struct manifest_firmware_info_t *info)
leothedragon 0:8f0bb79ddd48 131 {
leothedragon 0:8f0bb79ddd48 132 struct arm_uc_mm_fw_context_t *fwCtx = &(*arm_uc_mmPersistentContext.ctx)->getFw;
leothedragon 0:8f0bb79ddd48 133 fwCtx->info = info;
leothedragon 0:8f0bb79ddd48 134
leothedragon 0:8f0bb79ddd48 135 // initialize callback node
leothedragon 0:8f0bb79ddd48 136 fwCtx->callbackStorage.lock = 0;
leothedragon 0:8f0bb79ddd48 137
leothedragon 0:8f0bb79ddd48 138 // Continue the state machine
leothedragon 0:8f0bb79ddd48 139 ARM_UC_PostCallback(&fwCtx->callbackStorage, ARM_UC_mmCallbackFSMEntry, ARM_UC_MM_EVENT_BEGIN);
leothedragon 0:8f0bb79ddd48 140 return (arm_uc_error_t) {ERR_NONE};
leothedragon 0:8f0bb79ddd48 141 }
leothedragon 0:8f0bb79ddd48 142 arm_uc_error_t ARM_UC_mmGetError()
leothedragon 0:8f0bb79ddd48 143 {
leothedragon 0:8f0bb79ddd48 144 return arm_uc_mmPersistentContext.reportedError;
leothedragon 0:8f0bb79ddd48 145 }
leothedragon 0:8f0bb79ddd48 146
leothedragon 0:8f0bb79ddd48 147 int ARM_UC_mmCheckFormatUint32(manifest_guid_t* format, uint32_t expected)
leothedragon 0:8f0bb79ddd48 148 {
leothedragon 0:8f0bb79ddd48 149 manifest_guid_t local;
leothedragon 0:8f0bb79ddd48 150 // Fix alignment problems
leothedragon 0:8f0bb79ddd48 151 memcpy(&local, format, sizeof(local));
leothedragon 0:8f0bb79ddd48 152
leothedragon 0:8f0bb79ddd48 153 int fail = 0;
leothedragon 0:8f0bb79ddd48 154 fail = fail || local.words[0] != 0;
leothedragon 0:8f0bb79ddd48 155 fail = fail || local.words[1] != 0;
leothedragon 0:8f0bb79ddd48 156 fail = fail || local.words[2] != 0;
leothedragon 0:8f0bb79ddd48 157 fail = fail || local.words[3] != htobe(expected);
leothedragon 0:8f0bb79ddd48 158 return !fail;
leothedragon 0:8f0bb79ddd48 159 }
leothedragon 0:8f0bb79ddd48 160
leothedragon 0:8f0bb79ddd48 161 #if ARM_UC_MM_ENABLE_TEST_VECTORS
leothedragon 0:8f0bb79ddd48 162 arm_uc_error_t ARM_UC_mmRegisterTestHook(ARM_UC_mmTestHook_t hook)
leothedragon 0:8f0bb79ddd48 163 {
leothedragon 0:8f0bb79ddd48 164 arm_uc_error_t err = {ERR_NONE};
leothedragon 0:8f0bb79ddd48 165 arm_uc_mmPersistentContext.testHook = hook;
leothedragon 0:8f0bb79ddd48 166 return err;
leothedragon 0:8f0bb79ddd48 167 }
leothedragon 0:8f0bb79ddd48 168 #endif