Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

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