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

« Back to documentation index

Show/hide line numbers info.c Source File

info.c

Go to the documentation of this file.
00001 /**
00002  * @file    info.c
00003  * @brief   Implementation of info.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 "main.h"
00024 #include "info.h"
00025 #include "target_config.h"
00026 #include "read_uid.h"
00027 #include "util.h"
00028 #include "crc.h"
00029 #include "daplink.h"
00030 #include "settings.h"
00031 #include "target_board.h"
00032 
00033 static char hex_to_ascii(uint8_t x)
00034 {
00035     return ('0' + (x>9 ? x+0x27 : x));
00036 }
00037 
00038 // Constant variables
00039 static const daplink_info_t *const info_bl = (daplink_info_t *)(DAPLINK_ROM_BL_START + DAPLINK_INFO_OFFSET);
00040 static const daplink_info_t *const info_if = (daplink_info_t *)(DAPLINK_ROM_IF_START + DAPLINK_INFO_OFFSET);
00041 
00042 // Raw variables
00043 static uint32_t host_id[4];
00044 static uint32_t target_id[4];
00045 static uint32_t hic_id = DAPLINK_HIC_ID;
00046 
00047 static uint32_t crc_bootloader;
00048 static uint32_t crc_interface;
00049 static uint32_t crc_config_admin;
00050 static uint32_t crc_config_user;
00051 
00052 // Strings
00053 static char string_unique_id[48 + 1];
00054 static char string_mac[12 + 1];
00055 static char string_board_id[4 + 1];
00056 static char string_family_id[4 + 1];
00057 static char string_host_id[32 + 1];
00058 static char string_target_id[32 + 1];
00059 static char string_hic_id[8 + 1];
00060 static char string_version[4 + 1];
00061 
00062 static char usb_desc_unique_id[2 + sizeof(string_unique_id) * 2];
00063 
00064 
00065 const char *info_get_unique_id(void)
00066 {
00067     return string_unique_id;
00068 }
00069 
00070 const char *info_get_board_id(void)
00071 {
00072     return string_board_id;
00073 }
00074 
00075 const char *info_get_host_id(void)
00076 {
00077     return string_host_id;
00078 }
00079 
00080 const char *info_get_target_id(void)
00081 {
00082     return string_target_id;
00083 }
00084 
00085 const char *info_get_hic_id(void)
00086 {
00087     return string_hic_id;
00088 }
00089 const char *info_get_version(void)
00090 {
00091     return string_version;
00092 }
00093 const char *info_get_mac(void)
00094 {
00095     return string_mac;
00096 }
00097 
00098 const char *info_get_unique_id_string_descriptor(void)
00099 {
00100     return usb_desc_unique_id;
00101 }
00102 
00103 //prevent the compiler to optimize boad and family id
00104 #if (defined(__ICCARM__))
00105 #pragma optimize = none
00106 static void setup_basics(void)
00107 #elif (defined(__CC_ARM))
00108 #pragma push
00109 #pragma O0
00110 static void setup_basics(void)
00111 #elif (!defined(__GNUC__))
00112 /* #pragma GCC push_options */
00113 /* #pragma GCC optimize("O0") */
00114 static void __attribute__((optimize("O0"))) setup_basics(void)
00115 #else
00116 #error "Unknown compiler"
00117 #endif
00118 
00119 {
00120     uint8_t i = 0, idx = 0;
00121     uint16_t family_id = get_family_id();
00122     memset(string_board_id, 0, sizeof(string_board_id));
00123     memset(string_host_id, 0, sizeof(string_host_id));
00124     memset(string_target_id, 0, sizeof(string_target_id));
00125     memset(string_hic_id, 0, sizeof(string_hic_id));
00126     memset(string_board_id, 0, sizeof(string_board_id));
00127     // Host ID
00128     idx = 0;
00129 
00130     for (i = 0; i < 4; i++) {
00131         idx += util_write_hex32(string_host_id + idx, host_id[i]);
00132     }
00133 
00134     string_host_id[idx++] = 0;
00135     // Target ID
00136     idx = 0;
00137 
00138     for (i = 0; i < 4; i++) {
00139         idx += util_write_hex32(string_target_id + idx, target_id[i]);
00140     }
00141 
00142     string_target_id[idx++] = 0;
00143     // HIC ID
00144     idx = 0;
00145     idx += util_write_hex32(string_hic_id + idx, hic_id);
00146     string_hic_id[idx++] = 0;
00147     // Board ID
00148     memcpy(string_board_id, get_board_id(), 4);
00149     string_board_id[4] = 0;
00150     idx = 0;
00151     //Family ID
00152     string_family_id[idx++] = hex_to_ascii(((family_id >> 12) & 0xF));    
00153     string_family_id[idx++] = hex_to_ascii(((family_id >> 8) & 0xF));
00154 #if !(defined(DAPLINK_BL)) &&  defined(DRAG_N_DROP_SUPPORT)   //need to change the unique id when the msd is disabled 
00155     #if defined(MSC_ENDPOINT)
00156     if (config_ram_get_disable_msd() == 1 || flash_algo_valid()==0){
00157         string_family_id[idx++] = hex_to_ascii((((family_id >> 4) | 0x08) & 0xF)); 
00158     } else {
00159         string_family_id[idx++] = hex_to_ascii(((family_id >> 4) & 0xF));
00160     }
00161     #else //no msd support always have the most significant bit set for family id 2nd byte
00162         string_family_id[idx++] = hex_to_ascii((((family_id >> 4) | 0x08) & 0xF)); 
00163     #endif
00164 #else
00165     string_family_id[idx++] = hex_to_ascii(((family_id >> 4) & 0xF));
00166 #endif
00167     string_family_id[idx++] = hex_to_ascii(((family_id) & 0xF));    
00168     string_family_id[idx++] = 0;
00169     // Version
00170     idx = 0;
00171     string_version[idx++] = '0' + (DAPLINK_VERSION / 1000) % 10;
00172     string_version[idx++] = '0' + (DAPLINK_VERSION / 100) % 10;
00173     string_version[idx++] = '0' + (DAPLINK_VERSION / 10) % 10;
00174     string_version[idx++] = '0' + (DAPLINK_VERSION / 1) % 10;
00175     string_version[idx++] = 0;
00176 }
00177 
00178 static void setup_unique_id()
00179 {
00180     memset(string_unique_id, 0, sizeof(string_unique_id));
00181     strcat(string_unique_id, string_board_id);
00182     strcat(string_unique_id, string_family_id);
00183     strcat(string_unique_id, string_host_id);
00184     strcat(string_unique_id, string_hic_id);
00185 }
00186 
00187 static void setup_string_descriptor()
00188 {
00189     uint8_t i = 0, idx = 0, len = 0;
00190     len = strlen((const char *)string_unique_id);
00191     // bLength
00192     usb_desc_unique_id[idx++] = len * 2 + 2;
00193     // bDescriptorType
00194     usb_desc_unique_id[idx++] = 3;
00195 
00196     // bString
00197     for (i = 0; i < len; i++) {
00198         usb_desc_unique_id[idx++] = string_unique_id[i];
00199         usb_desc_unique_id[idx++] = 0;
00200     }
00201 }
00202 
00203 void info_init(void)
00204 {
00205     info_crc_compute();
00206     read_unique_id(host_id);
00207     setup_basics();
00208     setup_unique_id();
00209     setup_string_descriptor();
00210 }
00211 
00212 void info_set_uuid_target(uint32_t *uuid_data)
00213 {
00214     uint32_t idx = 0;
00215     // Save the target ID
00216     memcpy(target_id, uuid_data, 16);
00217     // patch for MAC use. Make sure MSB bits are set correctly
00218     uuid_data[2] |= (0x2 << 8);
00219     uuid_data[2] &= ~(0x1 << 8);
00220     idx += util_write_hex16(string_mac + idx, uuid_data[2] & 0xFFFF);
00221     idx += util_write_hex32(string_mac + idx, uuid_data[3]);
00222     string_mac[idx++] = 0;
00223 }
00224 
00225 bool info_get_bootloader_present(void)
00226 {
00227     bool present = true;
00228 
00229     if (0 == DAPLINK_ROM_BL_SIZE) {
00230         present = false;
00231     }
00232 
00233     if (DAPLINK_BUILD_KEY_BL != info_bl->build_key) {
00234         present = false;
00235     }
00236 
00237     if (DAPLINK_HIC_ID != info_bl->hic_id) {
00238         present = false;
00239     }
00240 
00241     return present;
00242 }
00243 
00244 bool info_get_interface_present(void)
00245 {
00246     bool present = true;
00247 
00248     if (0 == DAPLINK_ROM_IF_SIZE) {
00249         present = false;
00250     }
00251 
00252     if (DAPLINK_BUILD_KEY_IF != info_if->build_key) {
00253         present = false;
00254     }
00255 
00256     if (DAPLINK_HIC_ID != info_if->hic_id) {
00257         present = false;
00258     }
00259 
00260     return present;
00261 }
00262 
00263 bool info_get_config_admin_present(void)
00264 {
00265     //TODO, c1728p9 - check if admin config has been loaded
00266     return false;
00267 }
00268 
00269 bool info_get_config_user_present(void)
00270 {
00271     //TODO, c1728p9 - check if user config has a valid key
00272     return false;
00273 }
00274 
00275 uint32_t info_get_crc_bootloader()
00276 {
00277     return crc_bootloader;
00278 }
00279 
00280 uint32_t info_get_crc_interface()
00281 {
00282     return crc_interface;
00283 }
00284 
00285 uint32_t info_get_crc_config_admin()
00286 {
00287     return crc_config_admin;
00288 }
00289 
00290 uint32_t info_get_crc_config_user()
00291 {
00292     return crc_config_user;
00293 }
00294 
00295 void info_crc_compute()
00296 {
00297     crc_bootloader = 0;
00298     crc_interface = 0;
00299     crc_config_admin = 0;
00300     crc_config_user = 0;
00301 
00302     // Compute the CRCs of regions that exist
00303     if (DAPLINK_ROM_BL_SIZE > 0) {
00304         crc_bootloader = crc32((void *)DAPLINK_ROM_BL_START, DAPLINK_ROM_BL_SIZE - 4);
00305     }
00306 
00307     if (DAPLINK_ROM_IF_SIZE > 0) {
00308         crc_interface = crc32((void *)DAPLINK_ROM_IF_START, DAPLINK_ROM_IF_SIZE - 4);
00309     }
00310 
00311     if (DAPLINK_ROM_CONFIG_ADMIN_SIZE > 0) {
00312         crc_config_admin = crc32((void *)DAPLINK_ROM_CONFIG_ADMIN_START, DAPLINK_ROM_CONFIG_ADMIN_SIZE);
00313     }
00314 
00315     if (DAPLINK_ROM_CONFIG_USER_SIZE > 0) {
00316         crc_config_user = crc32((void *)DAPLINK_ROM_CONFIG_USER_START, DAPLINK_ROM_CONFIG_USER_SIZE);
00317     }
00318 }
00319 
00320 // Get version info as an integer
00321 uint32_t info_get_bootloader_version(void)
00322 {
00323     // Don't read version if image is not present
00324     if (!info_get_bootloader_present()) {
00325         return 0;
00326     }
00327 
00328     return info_bl->version;
00329 }
00330 
00331 uint32_t info_get_interface_version(void)
00332 {
00333     // Don't read version if image is not present
00334     if (!info_get_interface_present()) {
00335         return 0;
00336     }
00337 
00338     return info_if->version;
00339 }
00340 
00341 #if (defined(__CC_ARM))
00342 #pragma pop
00343 #endif
00344 #if (defined(__GNUC__))
00345 /* #pragma GCC pop_options */
00346 #endif