Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers validation.c Source File

validation.c

Go to the documentation of this file.
00001 /**
00002  * @file    validation.c
00003  * @brief   Implementation of validation.h
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2009-2019, ARM Limited, All Rights Reserved
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  * not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  * http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include <string.h>
00023 #include "validation.h"
00024 #include "target_config.h"
00025 #include "target_family.h"
00026 #include "target_board.h"
00027 
00028 static inline uint32_t test_range(const uint32_t test, const uint32_t min, const uint32_t max)
00029 {
00030     return ((test < min) || (test > max)) ? 0 : 1;
00031 }
00032 
00033 uint8_t validate_bin_nvic(const uint8_t *buf)
00034 {
00035     if (g_target_family && g_target_family->validate_bin_nvic ) {
00036         return g_target_family && g_target_family->validate_bin_nvic (buf);
00037     } else if (g_board_info.target_cfg) {
00038         uint32_t i = 4, nvic_val = 0;
00039         uint8_t in_range = 0;
00040         // test the initial SP value
00041         memcpy(&nvic_val, buf + 0, sizeof(nvic_val));
00042 
00043         region_info_t * region = g_board_info.target_cfg->ram_regions;
00044         for (; region->start != 0 || region->end != 0; ++region) {
00045             if (1 == test_range(nvic_val, region->start, region->end)) {
00046                 in_range = 1;
00047                 break;
00048             }
00049         }
00050 
00051         if (in_range == 0) {
00052             return 0;
00053         }
00054 
00055         // Reset_Handler
00056         // NMI_Handler
00057         // HardFault_Handler
00058         for (; i <= 12; i += 4) {
00059             in_range = 0;
00060             memcpy(&nvic_val, buf + i, sizeof(nvic_val));
00061             region_info_t * region = g_board_info.target_cfg->flash_regions;
00062             for (; region->start != 0 || region->end != 0; ++region) {
00063                 if (1 == test_range(nvic_val, region->start, region->end)) {
00064                     in_range = 1;
00065                     break;
00066                 }
00067             }
00068             if (in_range == 0) {
00069                 return 0;
00070             }
00071         }
00072 
00073         return 1;
00074 
00075     } else {
00076         return 0;
00077     }
00078 }
00079 
00080 uint8_t validate_hexfile(const uint8_t *buf)
00081 {
00082     if (g_target_family && g_target_family->validate_hexfile ) {
00083         return g_target_family->validate_hexfile (buf);
00084     } else {
00085         // look here for known hex records
00086         // add hex identifier b[0] == ':' && b[8] == {'0', '2', '3', '4', '5'}
00087         return ((buf[0] == ':') && ((buf[8] == '0') || (buf[8] == '2') || (buf[8] == '3') || (buf[8] == '4') || (buf[8] == '5'))) ? 1 : 0;
00088     }
00089 }