ME11C Sample Code in Maxim Integrated Team

Dependencies:   BMI160 max32630hsp3 MemoryLCD USBDevice

Fork of Host_Software_MAX32664GWEC_SpO2_HR-_EXTE by Seyhmus Cacina

Committer:
seyhmuscacina
Date:
Mon Mar 25 07:56:34 2019 +0000
Revision:
2:3b5d2467e6c7
Parent:
0:b259fd1a88f5
Fork the repository

Who changed what in which revision?

UserRevisionLine numberNew contents of line
seyhmus.cacina 0:b259fd1a88f5 1 /*
seyhmus.cacina 0:b259fd1a88f5 2 * bootldrInterface.cpp
seyhmus.cacina 0:b259fd1a88f5 3 *
seyhmus.cacina 0:b259fd1a88f5 4 * Created on: Feb 7, 2019
seyhmus.cacina 0:b259fd1a88f5 5 * Author: Yagmur.Gok
seyhmus.cacina 0:b259fd1a88f5 6 */
seyhmus.cacina 0:b259fd1a88f5 7
seyhmus.cacina 0:b259fd1a88f5 8 #include <mbed.h> /*for type definitions*/
seyhmus.cacina 0:b259fd1a88f5 9 #include "bootldrAPI.h"
seyhmus.cacina 0:b259fd1a88f5 10 #include "SHComm.h"
seyhmus.cacina 0:b259fd1a88f5 11 #include "demoDefinitions.h"
seyhmus.cacina 0:b259fd1a88f5 12
seyhmus.cacina 0:b259fd1a88f5 13 //extern Serial daplink;
seyhmus.cacina 0:b259fd1a88f5 14
seyhmus.cacina 0:b259fd1a88f5 15 //#define SERIALOUT printf
seyhmus.cacina 0:b259fd1a88f5 16 //#define SERIALIN printf
seyhmus.cacina 0:b259fd1a88f5 17
seyhmus.cacina 0:b259fd1a88f5 18 #define COMM_SUCCESS 0
seyhmus.cacina 0:b259fd1a88f5 19 #define COMM_GENERAL_ERROR -1
seyhmus.cacina 0:b259fd1a88f5 20 #define COMM_INVALID_PARAM -254
seyhmus.cacina 0:b259fd1a88f5 21 #define COMM_NOT_RECOGNIZED -255
seyhmus.cacina 0:b259fd1a88f5 22
seyhmus.cacina 0:b259fd1a88f5 23 #define BL_FLASH_ERR_GENERAL -1
seyhmus.cacina 0:b259fd1a88f5 24 #define BL_FLASH_ERR_CHECKSUM -2
seyhmus.cacina 0:b259fd1a88f5 25 #define BL_FLASH_ERR_AUTH -3
seyhmus.cacina 0:b259fd1a88f5 26 #define BL_SET_NUM_PAGES_FAIL -4
seyhmus.cacina 0:b259fd1a88f5 27 #define BL_FLASS_ERASE_FAIL -5
seyhmus.cacina 0:b259fd1a88f5 28 #define BL_SET_IV_FAIL -6
seyhmus.cacina 0:b259fd1a88f5 29 #define BL_FLASHING_FAIL -7
seyhmus.cacina 0:b259fd1a88f5 30 #define BL_RAM_ALLOC_FAIL -8
seyhmus.cacina 0:b259fd1a88f5 31
seyhmus.cacina 0:b259fd1a88f5 32 #define AES_NONCE_SIZE 11
seyhmus.cacina 0:b259fd1a88f5 33 #define AES_AUTH_SIZE 16
seyhmus.cacina 0:b259fd1a88f5 34 #define BOOTLOADER_MAX_PAGE_SIZE (8192)
seyhmus.cacina 0:b259fd1a88f5 35 #define CHECKBYTES_SIZE (16)
seyhmus.cacina 0:b259fd1a88f5 36 #define FLASHCMDBYTES (2)
seyhmus.cacina 0:b259fd1a88f5 37
seyhmus.cacina 0:b259fd1a88f5 38 #define SS_BOOTLOADER_ERASE_DELAY 700
seyhmus.cacina 0:b259fd1a88f5 39 #define AES_NONCE_SIZE 11
seyhmus.cacina 0:b259fd1a88f5 40 #define AES_AUTH_SIZE 16
seyhmus.cacina 0:b259fd1a88f5 41 #define MAX_PAGE_SIZE 8192
seyhmus.cacina 0:b259fd1a88f5 42 #define CHECKBYTES_SIZE 16
seyhmus.cacina 0:b259fd1a88f5 43
seyhmus.cacina 0:b259fd1a88f5 44 #define PAGE_WRITE_DELAY_MS 170
seyhmus.cacina 0:b259fd1a88f5 45 #define MAX_PAGE_NUMBER 31
seyhmus.cacina 0:b259fd1a88f5 46 #define PAGE_ERASE_CMD_SLEEP_MS 50
seyhmus.cacina 0:b259fd1a88f5 47 #define BL_CFG_SAVE_CMD_SLEEP_MS 50
seyhmus.cacina 0:b259fd1a88f5 48
seyhmus.cacina 0:b259fd1a88f5 49 static int parse_iv(const char* cmd, uint8_t* iv_bytes);
seyhmus.cacina 0:b259fd1a88f5 50 static int parse_auth(const char* cmd, uint8_t *auth_bytes);
seyhmus.cacina 0:b259fd1a88f5 51 static int is_hub_ready_for_flash(void);
seyhmus.cacina 0:b259fd1a88f5 52 static void clear_state_info(void);
seyhmus.cacina 0:b259fd1a88f5 53
seyhmus.cacina 0:b259fd1a88f5 54
seyhmus.cacina 0:b259fd1a88f5 55 typedef struct {
seyhmus.cacina 0:b259fd1a88f5 56 int num_allocated_pages; /* Allocated page size */
seyhmus.cacina 0:b259fd1a88f5 57 int num_pages;
seyhmus.cacina 0:b259fd1a88f5 58 int num_received_pages;
seyhmus.cacina 0:b259fd1a88f5 59 int page_size;
seyhmus.cacina 0:b259fd1a88f5 60 uint8_t auth[AES_AUTH_SIZE];
seyhmus.cacina 0:b259fd1a88f5 61 uint8_t nonce[AES_NONCE_SIZE];
seyhmus.cacina 0:b259fd1a88f5 62 uint8_t pages[MAX_PAGE_NUMBER * (MAX_PAGE_SIZE + CHECKBYTES_SIZE)]; //TODO: Use dynamic allocation
seyhmus.cacina 0:b259fd1a88f5 63 } app_image_t;
seyhmus.cacina 0:b259fd1a88f5 64
seyhmus.cacina 0:b259fd1a88f5 65
seyhmus.cacina 0:b259fd1a88f5 66 static app_image_t *app_image = NULL;
seyhmus.cacina 0:b259fd1a88f5 67
seyhmus.cacina 0:b259fd1a88f5 68 static struct {
seyhmus.cacina 0:b259fd1a88f5 69
seyhmus.cacina 0:b259fd1a88f5 70 uint32_t num_pages;
seyhmus.cacina 0:b259fd1a88f5 71 uint32_t page_size;
seyhmus.cacina 0:b259fd1a88f5 72 uint32_t hub_mode_bootloader;
seyhmus.cacina 0:b259fd1a88f5 73 uint32_t is_iv_set;
seyhmus.cacina 0:b259fd1a88f5 74 uint32_t is_auth_done;
seyhmus.cacina 0:b259fd1a88f5 75 uint32_t is_flash_erased;
seyhmus.cacina 0:b259fd1a88f5 76 uint32_t flag_image_on_ram;
seyhmus.cacina 0:b259fd1a88f5 77 uint32_t bootcmds_delay_factor;
seyhmus.cacina 0:b259fd1a88f5 78 uint32_t ebl_mode;
seyhmus.cacina 0:b259fd1a88f5 79
seyhmus.cacina 0:b259fd1a88f5 80 }bootldrState = { 0 , 0 , 0 ,0 , 0 , 0 , 0 , 1 ,1};
seyhmus.cacina 0:b259fd1a88f5 81
seyhmus.cacina 0:b259fd1a88f5 82
seyhmus.cacina 0:b259fd1a88f5 83
seyhmus.cacina 0:b259fd1a88f5 84 int BOOTLDR_get_host_bootloader_state(const char *null_arg){
seyhmus.cacina 0:b259fd1a88f5 85
seyhmus.cacina 0:b259fd1a88f5 86 SERIALOUT(" \r\n BOOT STATE INFO: \r\n num_pages= %d \r\n page_size= %d \r\n is_iv_set= %d \r\n is_auth_done= %d \r\n is_flash_erased= %d \r\n flag_image_on_ram= %d \r\n bootcmds_delay_factor= %d \r\n ebl_mode= %d \r\n",
seyhmus.cacina 0:b259fd1a88f5 87 bootldrState.num_pages,
seyhmus.cacina 0:b259fd1a88f5 88 bootldrState.page_size,
seyhmus.cacina 0:b259fd1a88f5 89 bootldrState.is_iv_set,
seyhmus.cacina 0:b259fd1a88f5 90 bootldrState.is_auth_done,
seyhmus.cacina 0:b259fd1a88f5 91 bootldrState.is_flash_erased,
seyhmus.cacina 0:b259fd1a88f5 92 bootldrState.flag_image_on_ram,
seyhmus.cacina 0:b259fd1a88f5 93 bootldrState.bootcmds_delay_factor,
seyhmus.cacina 0:b259fd1a88f5 94 bootldrState.ebl_mode );
seyhmus.cacina 0:b259fd1a88f5 95
seyhmus.cacina 0:b259fd1a88f5 96
seyhmus.cacina 0:b259fd1a88f5 97 }
seyhmus.cacina 0:b259fd1a88f5 98
seyhmus.cacina 0:b259fd1a88f5 99 int SH_BOOTLDR_enter_blmode(const char *null_arg){
seyhmus.cacina 0:b259fd1a88f5 100
seyhmus.cacina 0:b259fd1a88f5 101 int status = 0x00;
seyhmus.cacina 0:b259fd1a88f5 102
seyhmus.cacina 0:b259fd1a88f5 103 //status = sh_put_in_bootloader();
seyhmus.cacina 0:b259fd1a88f5 104 //status = sh_set_ebl_mode((uint8_t)1); /* 1: GPIO rest 0: CMD reset*/
seyhmus.cacina 0:b259fd1a88f5 105
seyhmus.cacina 0:b259fd1a88f5 106 if( status == 0x00) {
seyhmus.cacina 0:b259fd1a88f5 107
seyhmus.cacina 0:b259fd1a88f5 108 //////////status = sh_reset_to_bootloader(); - CHECKIF PROBLEM STILL PRESENT!!
seyhmus.cacina 0:b259fd1a88f5 109 status = sh_debug_reset_to_bootloader();
seyhmus.cacina 0:b259fd1a88f5 110 //status = sh_set_sensorhub_operating_mode((uint8_t)0x08);
seyhmus.cacina 0:b259fd1a88f5 111 if (status == 0x00) {
seyhmus.cacina 0:b259fd1a88f5 112 SERIALOUT("\r\n%s err=%d\r\n", "bootldr", COMM_SUCCESS);
seyhmus.cacina 0:b259fd1a88f5 113 bootldrState.hub_mode_bootloader = 1;
seyhmus.cacina 0:b259fd1a88f5 114 }
seyhmus.cacina 0:b259fd1a88f5 115 else
seyhmus.cacina 0:b259fd1a88f5 116 SERIALOUT("\r\n%s err=%d\r\n", "bootldr", COMM_GENERAL_ERROR);
seyhmus.cacina 0:b259fd1a88f5 117 }
seyhmus.cacina 0:b259fd1a88f5 118
seyhmus.cacina 0:b259fd1a88f5 119 return status;
seyhmus.cacina 0:b259fd1a88f5 120
seyhmus.cacina 0:b259fd1a88f5 121 }
seyhmus.cacina 0:b259fd1a88f5 122
seyhmus.cacina 0:b259fd1a88f5 123
seyhmus.cacina 0:b259fd1a88f5 124 int SH_BOOTLDR_exit_blmode(const char *null_arg){
seyhmus.cacina 0:b259fd1a88f5 125
seyhmus.cacina 0:b259fd1a88f5 126 int status;
seyhmus.cacina 0:b259fd1a88f5 127 status = sh_reset_to_main_app() ; //sh_exit_from_bootloader();
seyhmus.cacina 0:b259fd1a88f5 128 if (status == 0x00)
seyhmus.cacina 0:b259fd1a88f5 129 printf("\r\n exited from bootloader mode \r\n");
seyhmus.cacina 0:b259fd1a88f5 130
seyhmus.cacina 0:b259fd1a88f5 131 if (status == 0x00) {
seyhmus.cacina 0:b259fd1a88f5 132 SERIALOUT("\r\n%s err=%d\r\n", "exit", COMM_SUCCESS);
seyhmus.cacina 0:b259fd1a88f5 133 bootldrState.hub_mode_bootloader = 0;
seyhmus.cacina 0:b259fd1a88f5 134 }
seyhmus.cacina 0:b259fd1a88f5 135 else
seyhmus.cacina 0:b259fd1a88f5 136 SERIALOUT("\r\n%s err=%d\r\n", "exit", COMM_GENERAL_ERROR);
seyhmus.cacina 0:b259fd1a88f5 137
seyhmus.cacina 0:b259fd1a88f5 138 return status;
seyhmus.cacina 0:b259fd1a88f5 139
seyhmus.cacina 0:b259fd1a88f5 140 }
seyhmus.cacina 0:b259fd1a88f5 141
seyhmus.cacina 0:b259fd1a88f5 142 int SH_BOOTLOADER_image_on_ram( const char *arg ){
seyhmus.cacina 0:b259fd1a88f5 143
seyhmus.cacina 0:b259fd1a88f5 144 int status = COMM_SUCCESS;
seyhmus.cacina 0:b259fd1a88f5 145 int tmp;
seyhmus.cacina 0:b259fd1a88f5 146 sscanf(arg, "%*s %d", &tmp );
seyhmus.cacina 0:b259fd1a88f5 147 bootldrState.flag_image_on_ram = (tmp > 0)? 1:0 ;
seyhmus.cacina 0:b259fd1a88f5 148
seyhmus.cacina 0:b259fd1a88f5 149 if(tmp == 1){
seyhmus.cacina 0:b259fd1a88f5 150
seyhmus.cacina 0:b259fd1a88f5 151 app_image = (app_image_t*) malloc(sizeof(*app_image));
seyhmus.cacina 0:b259fd1a88f5 152 if(app_image != NULL){
seyhmus.cacina 0:b259fd1a88f5 153
seyhmus.cacina 0:b259fd1a88f5 154 app_image->num_allocated_pages = MAX_PAGE_NUMBER;
seyhmus.cacina 0:b259fd1a88f5 155 app_image->num_pages = 0;
seyhmus.cacina 0:b259fd1a88f5 156 app_image->num_received_pages = 0;
seyhmus.cacina 0:b259fd1a88f5 157 app_image->page_size = MAX_PAGE_SIZE;
seyhmus.cacina 0:b259fd1a88f5 158 //app_image->nonce = {0};
seyhmus.cacina 0:b259fd1a88f5 159 //app_image->auth = {0};
seyhmus.cacina 0:b259fd1a88f5 160 //app_image->pages = {0};
seyhmus.cacina 0:b259fd1a88f5 161
seyhmus.cacina 0:b259fd1a88f5 162 }else {
seyhmus.cacina 0:b259fd1a88f5 163 SERIALOUT("Memory allocation fail for ram image \r\n");
seyhmus.cacina 0:b259fd1a88f5 164 status = BL_RAM_ALLOC_FAIL;
seyhmus.cacina 0:b259fd1a88f5 165 }
seyhmus.cacina 0:b259fd1a88f5 166
seyhmus.cacina 0:b259fd1a88f5 167 }
seyhmus.cacina 0:b259fd1a88f5 168
seyhmus.cacina 0:b259fd1a88f5 169 SERIALOUT("\r\n%s err=%d\r\n", "image_on_ram", COMM_SUCCESS);
seyhmus.cacina 0:b259fd1a88f5 170
seyhmus.cacina 0:b259fd1a88f5 171 }
seyhmus.cacina 0:b259fd1a88f5 172
seyhmus.cacina 0:b259fd1a88f5 173 int SH_BOOTLDR_get_pagesz(const char *null_arg){
seyhmus.cacina 0:b259fd1a88f5 174
seyhmus.cacina 0:b259fd1a88f5 175 int status;
seyhmus.cacina 0:b259fd1a88f5 176 int pageSize;
seyhmus.cacina 0:b259fd1a88f5 177
seyhmus.cacina 0:b259fd1a88f5 178 if (bootldrState.flag_image_on_ram) {
seyhmus.cacina 0:b259fd1a88f5 179 SERIALOUT("\r\n%s value=%d err=%d\r\n", "page_size", MAX_PAGE_SIZE, COMM_SUCCESS);
seyhmus.cacina 0:b259fd1a88f5 180 app_image->page_size = MAX_PAGE_SIZE;
seyhmus.cacina 0:b259fd1a88f5 181 bootldrState.page_size = MAX_PAGE_SIZE;
seyhmus.cacina 0:b259fd1a88f5 182 SERIALOUT("\r\n%s err=%d\r\n", "image_on_ram", 1);
seyhmus.cacina 0:b259fd1a88f5 183
seyhmus.cacina 0:b259fd1a88f5 184 }else {
seyhmus.cacina 0:b259fd1a88f5 185
seyhmus.cacina 0:b259fd1a88f5 186 status = sh_get_bootloader_pagesz(&pageSize);
seyhmus.cacina 0:b259fd1a88f5 187 if (status == 0x00 ){
seyhmus.cacina 0:b259fd1a88f5 188 if( pageSize == -2){
seyhmus.cacina 0:b259fd1a88f5 189 SERIALOUT("\r\n Page size over maximum allowable \r\n");
seyhmus.cacina 0:b259fd1a88f5 190 status = -1;
seyhmus.cacina 0:b259fd1a88f5 191 }else {
seyhmus.cacina 0:b259fd1a88f5 192 SERIALOUT("\r\n%s value=%d err=%d\r\n", "page_size", pageSize, status);
seyhmus.cacina 0:b259fd1a88f5 193 bootldrState.page_size = pageSize;
seyhmus.cacina 0:b259fd1a88f5 194 }
seyhmus.cacina 0:b259fd1a88f5 195 }else
seyhmus.cacina 0:b259fd1a88f5 196 SERIALOUT("\r\n%s err=%d\r\n", "page_size", status);
seyhmus.cacina 0:b259fd1a88f5 197
seyhmus.cacina 0:b259fd1a88f5 198 }
seyhmus.cacina 0:b259fd1a88f5 199 return status;
seyhmus.cacina 0:b259fd1a88f5 200
seyhmus.cacina 0:b259fd1a88f5 201 }
seyhmus.cacina 0:b259fd1a88f5 202
seyhmus.cacina 0:b259fd1a88f5 203 int SH_BOOTLDR_set_pagecount(const char *arg){
seyhmus.cacina 0:b259fd1a88f5 204
seyhmus.cacina 0:b259fd1a88f5 205 int status = -1;
seyhmus.cacina 0:b259fd1a88f5 206 int pageCount;
seyhmus.cacina 0:b259fd1a88f5 207
seyhmus.cacina 0:b259fd1a88f5 208 if(sscanf(arg, "%*s %d", &pageCount)){
seyhmus.cacina 0:b259fd1a88f5 209
seyhmus.cacina 0:b259fd1a88f5 210 if(bootldrState.flag_image_on_ram){
seyhmus.cacina 0:b259fd1a88f5 211 app_image->num_pages = (pageCount <= MAX_PAGE_NUMBER)? pageCount:0;
seyhmus.cacina 0:b259fd1a88f5 212 app_image->num_allocated_pages = MAX_PAGE_NUMBER;
seyhmus.cacina 0:b259fd1a88f5 213 bootldrState.num_pages = app_image->num_pages;
seyhmus.cacina 0:b259fd1a88f5 214
seyhmus.cacina 0:b259fd1a88f5 215 }else {
seyhmus.cacina 0:b259fd1a88f5 216
seyhmus.cacina 0:b259fd1a88f5 217 status = sh_set_bootloader_numberofpages(pageCount);
seyhmus.cacina 0:b259fd1a88f5 218 if (status == 0x00){
seyhmus.cacina 0:b259fd1a88f5 219 bootldrState.num_pages = pageCount;
seyhmus.cacina 0:b259fd1a88f5 220 }else
seyhmus.cacina 0:b259fd1a88f5 221 bootldrState.num_pages = 0;
seyhmus.cacina 0:b259fd1a88f5 222 }
seyhmus.cacina 0:b259fd1a88f5 223 }
seyhmus.cacina 0:b259fd1a88f5 224 SERIALOUT("\r\n%s err=%d\r\n", "num_pages", status);
seyhmus.cacina 0:b259fd1a88f5 225 return status;
seyhmus.cacina 0:b259fd1a88f5 226
seyhmus.cacina 0:b259fd1a88f5 227 }
seyhmus.cacina 0:b259fd1a88f5 228
seyhmus.cacina 0:b259fd1a88f5 229 int SH_BOOTLDR_set_iv(const char *arg) {
seyhmus.cacina 0:b259fd1a88f5 230
seyhmus.cacina 0:b259fd1a88f5 231 uint8_t iv_bytes[AES_NONCE_SIZE];
seyhmus.cacina 0:b259fd1a88f5 232 int status = parse_iv(arg, &iv_bytes[0]);
seyhmus.cacina 0:b259fd1a88f5 233 if( status == 0x00){
seyhmus.cacina 0:b259fd1a88f5 234
seyhmus.cacina 0:b259fd1a88f5 235 if(bootldrState.flag_image_on_ram){
seyhmus.cacina 0:b259fd1a88f5 236 int i=0;
seyhmus.cacina 0:b259fd1a88f5 237 for(i = 0 ; i != AES_NONCE_SIZE ; i++)
seyhmus.cacina 0:b259fd1a88f5 238 app_image->nonce[i] = iv_bytes[i];
seyhmus.cacina 0:b259fd1a88f5 239 bootldrState.is_iv_set = 1;
seyhmus.cacina 0:b259fd1a88f5 240
seyhmus.cacina 0:b259fd1a88f5 241 }else {
seyhmus.cacina 0:b259fd1a88f5 242
seyhmus.cacina 0:b259fd1a88f5 243 status = sh_set_bootloader_iv(iv_bytes);
seyhmus.cacina 0:b259fd1a88f5 244 if( status == 0x00 ) {
seyhmus.cacina 0:b259fd1a88f5 245 bootldrState.is_iv_set = 1;
seyhmus.cacina 0:b259fd1a88f5 246 }
seyhmus.cacina 0:b259fd1a88f5 247 else {
seyhmus.cacina 0:b259fd1a88f5 248 bootldrState.is_iv_set = 0;
seyhmus.cacina 0:b259fd1a88f5 249 status = COMM_GENERAL_ERROR;
seyhmus.cacina 0:b259fd1a88f5 250 }
seyhmus.cacina 0:b259fd1a88f5 251 }
seyhmus.cacina 0:b259fd1a88f5 252 }
seyhmus.cacina 0:b259fd1a88f5 253 SERIALOUT("\r\n%s err=%d\r\n", "set_iv", status);
seyhmus.cacina 0:b259fd1a88f5 254
seyhmus.cacina 0:b259fd1a88f5 255 return status;
seyhmus.cacina 0:b259fd1a88f5 256
seyhmus.cacina 0:b259fd1a88f5 257 }
seyhmus.cacina 0:b259fd1a88f5 258
seyhmus.cacina 0:b259fd1a88f5 259
seyhmus.cacina 0:b259fd1a88f5 260 int SH_BOOTLDR_set_authentication(const char *arg){
seyhmus.cacina 0:b259fd1a88f5 261
seyhmus.cacina 0:b259fd1a88f5 262 uint8_t auth_bytes[AES_AUTH_SIZE];
seyhmus.cacina 0:b259fd1a88f5 263 int status = parse_auth(arg, &auth_bytes[0]);
seyhmus.cacina 0:b259fd1a88f5 264 if( status == 0x00){
seyhmus.cacina 0:b259fd1a88f5 265
seyhmus.cacina 0:b259fd1a88f5 266 if(bootldrState.flag_image_on_ram){
seyhmus.cacina 0:b259fd1a88f5 267 int i=0;
seyhmus.cacina 0:b259fd1a88f5 268 for(i = 0 ; i != AES_AUTH_SIZE ; i++)
seyhmus.cacina 0:b259fd1a88f5 269 app_image->auth[i] = auth_bytes[i];
seyhmus.cacina 0:b259fd1a88f5 270 bootldrState.is_auth_done = 1;
seyhmus.cacina 0:b259fd1a88f5 271
seyhmus.cacina 0:b259fd1a88f5 272 }else {
seyhmus.cacina 0:b259fd1a88f5 273
seyhmus.cacina 0:b259fd1a88f5 274 status = sh_set_bootloader_auth(auth_bytes);
seyhmus.cacina 0:b259fd1a88f5 275 if( status == 0x00 ){
seyhmus.cacina 0:b259fd1a88f5 276 bootldrState.is_auth_done = 1;
seyhmus.cacina 0:b259fd1a88f5 277 }
seyhmus.cacina 0:b259fd1a88f5 278 else
seyhmus.cacina 0:b259fd1a88f5 279 status = COMM_GENERAL_ERROR;
seyhmus.cacina 0:b259fd1a88f5 280 }
seyhmus.cacina 0:b259fd1a88f5 281
seyhmus.cacina 0:b259fd1a88f5 282 }
seyhmus.cacina 0:b259fd1a88f5 283 SERIALOUT("\r\n%s err=%d\r\n", "set_auth", status);
seyhmus.cacina 0:b259fd1a88f5 284
seyhmus.cacina 0:b259fd1a88f5 285 return status;
seyhmus.cacina 0:b259fd1a88f5 286
seyhmus.cacina 0:b259fd1a88f5 287 }
seyhmus.cacina 0:b259fd1a88f5 288
seyhmus.cacina 0:b259fd1a88f5 289 int SH_BOOTLDR_eraseflash(const char *null_arg){
seyhmus.cacina 0:b259fd1a88f5 290
seyhmus.cacina 0:b259fd1a88f5 291 int status;
seyhmus.cacina 0:b259fd1a88f5 292 if(bootldrState.flag_image_on_ram){
seyhmus.cacina 0:b259fd1a88f5 293 SERIALOUT("\r\n%s err=%d\r\n", "erase", COMM_SUCCESS);
seyhmus.cacina 0:b259fd1a88f5 294 status == 0x00 /*SS_SUCCESS*/;
seyhmus.cacina 0:b259fd1a88f5 295
seyhmus.cacina 0:b259fd1a88f5 296 }else {
seyhmus.cacina 0:b259fd1a88f5 297
seyhmus.cacina 0:b259fd1a88f5 298 status = sh_set_bootloader_erase();
seyhmus.cacina 0:b259fd1a88f5 299 if(status == 0x00 /*SS_SUCCESS*/) {
seyhmus.cacina 0:b259fd1a88f5 300 bootldrState.is_flash_erased = 1;
seyhmus.cacina 0:b259fd1a88f5 301 }else{
seyhmus.cacina 0:b259fd1a88f5 302 status = COMM_GENERAL_ERROR;
seyhmus.cacina 0:b259fd1a88f5 303 }
seyhmus.cacina 0:b259fd1a88f5 304 SERIALOUT("\r\n%s err=%d\r\n", "erase", status);
seyhmus.cacina 0:b259fd1a88f5 305
seyhmus.cacina 0:b259fd1a88f5 306 }
seyhmus.cacina 0:b259fd1a88f5 307 return status;
seyhmus.cacina 0:b259fd1a88f5 308
seyhmus.cacina 0:b259fd1a88f5 309 }
seyhmus.cacina 0:b259fd1a88f5 310
seyhmus.cacina 0:b259fd1a88f5 311
seyhmus.cacina 0:b259fd1a88f5 312 int SH_BOOTLDR_receive_image_to_ram(void){
seyhmus.cacina 0:b259fd1a88f5 313
seyhmus.cacina 0:b259fd1a88f5 314
seyhmus.cacina 0:b259fd1a88f5 315 int status;
seyhmus.cacina 0:b259fd1a88f5 316 int totalBytes = 0;
seyhmus.cacina 0:b259fd1a88f5 317 int currentPage = 1;
seyhmus.cacina 0:b259fd1a88f5 318
seyhmus.cacina 0:b259fd1a88f5 319 if( app_image != NULL && app_image->num_allocated_pages > 0) {
seyhmus.cacina 0:b259fd1a88f5 320
seyhmus.cacina 0:b259fd1a88f5 321 uint8_t *page = &app_image->pages[0];
seyhmus.cacina 0:b259fd1a88f5 322 uint32_t offset = 0;
seyhmus.cacina 0:b259fd1a88f5 323 while (currentPage <= app_image->num_pages) {
seyhmus.cacina 0:b259fd1a88f5 324
seyhmus.cacina 0:b259fd1a88f5 325 while (totalBytes < (MAX_PAGE_SIZE + CHECKBYTES_SIZE)) {
seyhmus.cacina 0:b259fd1a88f5 326 page[totalBytes++] = SERIALIN(); // daplink.getc(); ; /////////////////////////////////////////////////////m_USB->_getc();
seyhmus.cacina 0:b259fd1a88f5 327 }
seyhmus.cacina 0:b259fd1a88f5 328
seyhmus.cacina 0:b259fd1a88f5 329 currentPage++;
seyhmus.cacina 0:b259fd1a88f5 330 SERIALOUT("\r\npageFlashDone err=%d\r\n", COMM_SUCCESS);
seyhmus.cacina 0:b259fd1a88f5 331
seyhmus.cacina 0:b259fd1a88f5 332 offset += MAX_PAGE_SIZE + CHECKBYTES_SIZE;
seyhmus.cacina 0:b259fd1a88f5 333 page = &app_image->pages[offset];
seyhmus.cacina 0:b259fd1a88f5 334 totalBytes = 0;
seyhmus.cacina 0:b259fd1a88f5 335 }
seyhmus.cacina 0:b259fd1a88f5 336
seyhmus.cacina 0:b259fd1a88f5 337 app_image->num_received_pages = currentPage;
seyhmus.cacina 0:b259fd1a88f5 338
seyhmus.cacina 0:b259fd1a88f5 339 status = COMM_SUCCESS;
seyhmus.cacina 0:b259fd1a88f5 340
seyhmus.cacina 0:b259fd1a88f5 341 }else
seyhmus.cacina 0:b259fd1a88f5 342 status = COMM_GENERAL_ERROR;
seyhmus.cacina 0:b259fd1a88f5 343
seyhmus.cacina 0:b259fd1a88f5 344 return status;
seyhmus.cacina 0:b259fd1a88f5 345
seyhmus.cacina 0:b259fd1a88f5 346 }
seyhmus.cacina 0:b259fd1a88f5 347
seyhmus.cacina 0:b259fd1a88f5 348 int SH_BOOTLDR_flash_pages(void){
seyhmus.cacina 0:b259fd1a88f5 349
seyhmus.cacina 0:b259fd1a88f5 350
seyhmus.cacina 0:b259fd1a88f5 351 int totalBytes = 0;
seyhmus.cacina 0:b259fd1a88f5 352 int currentPage = 1;
seyhmus.cacina 0:b259fd1a88f5 353 char charbuf_flash[256];
seyhmus.cacina 0:b259fd1a88f5 354 int data_len_flash = 0;
seyhmus.cacina 0:b259fd1a88f5 355 int status;
seyhmus.cacina 0:b259fd1a88f5 356
seyhmus.cacina 0:b259fd1a88f5 357 static uint8_t tx_buf[ BOOTLOADER_MAX_PAGE_SIZE + CHECKBYTES_SIZE + FLASHCMDBYTES] = { SS_FAM_W_BOOTLOADER, SS_CMDIDX_SENDPAGE };
seyhmus.cacina 0:b259fd1a88f5 358 uint8_t *data_buffer = &tx_buf[2];
seyhmus.cacina 0:b259fd1a88f5 359
seyhmus.cacina 0:b259fd1a88f5 360 if(!is_hub_ready_for_flash()){
seyhmus.cacina 0:b259fd1a88f5 361 printf("missing condition for flashing: no page size , no page number, iv notset , no outhentication , flash noterased or mode is not bootloader!\r\n");
seyhmus.cacina 0:b259fd1a88f5 362 clear_state_info();
seyhmus.cacina 0:b259fd1a88f5 363 return -1;
seyhmus.cacina 0:b259fd1a88f5 364 }
seyhmus.cacina 0:b259fd1a88f5 365 printf(" \r\n NOW WE ARE FLASHING THE PAGES..........\r\n");
seyhmus.cacina 0:b259fd1a88f5 366 printf(" \r\n page_size: %d \r\n", bootldrState.page_size);
seyhmus.cacina 0:b259fd1a88f5 367 printf(" \r\n num_pages: %d \r\n", bootldrState.num_pages);
seyhmus.cacina 0:b259fd1a88f5 368
seyhmus.cacina 0:b259fd1a88f5 369 while (currentPage <= bootldrState.num_pages) {
seyhmus.cacina 0:b259fd1a88f5 370
seyhmus.cacina 0:b259fd1a88f5 371 while (totalBytes < (bootldrState.page_size + CHECKBYTES_SIZE)) {
seyhmus.cacina 0:b259fd1a88f5 372 data_buffer[totalBytes++] = SERIALIN(); //daplink.getc(); //m_USB->_getc(); !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! MYG
seyhmus.cacina 0:b259fd1a88f5 373 //Here we should be able to take the data over BLE
seyhmus.cacina 0:b259fd1a88f5 374 }
seyhmus.cacina 0:b259fd1a88f5 375 printf(" \r\n NOW WE GOT A PAGE..........\r\n");
seyhmus.cacina 0:b259fd1a88f5 376 //status = sh_bootloader_flashpage(&tx_buf[0] , bootldrState.page_size);
seyhmus.cacina 0:b259fd1a88f5 377
seyhmus.cacina 0:b259fd1a88f5 378 status = sh_write_cmd(tx_buf, bootldrState.page_size + CHECKBYTES_SIZE + 2, 2000);
seyhmus.cacina 0:b259fd1a88f5 379
seyhmus.cacina 0:b259fd1a88f5 380 if (status == 0x00)
seyhmus.cacina 0:b259fd1a88f5 381 printf(" \r\n NOW WE PUSHED A PAGE..........\r\n");
seyhmus.cacina 0:b259fd1a88f5 382 /*
seyhmus.cacina 0:b259fd1a88f5 383 if (status == 0x00){
seyhmus.cacina 0:b259fd1a88f5 384 currentPage++;
seyhmus.cacina 0:b259fd1a88f5 385 SERIALOUT("\r\npageFlashDone err=%d\r\n", COMM_SUCCESS);
seyhmus.cacina 0:b259fd1a88f5 386 }else{
seyhmus.cacina 0:b259fd1a88f5 387
seyhmus.cacina 0:b259fd1a88f5 388 if (status == SS_ERR_BTLDR_CHECKSUM)
seyhmus.cacina 0:b259fd1a88f5 389 SERIALOUT("\r\npageFlashDone err=%d\r\n", BL_FLASH_ERR_CHECKSUM);
seyhmus.cacina 0:b259fd1a88f5 390 else
seyhmus.cacina 0:b259fd1a88f5 391 SERIALOUT("\r\npageFlashDone err=%d\r\n", BL_FLASH_ERR_GENERAL);
seyhmus.cacina 0:b259fd1a88f5 392
seyhmus.cacina 0:b259fd1a88f5 393 }
seyhmus.cacina 0:b259fd1a88f5 394 */
seyhmus.cacina 0:b259fd1a88f5 395
seyhmus.cacina 0:b259fd1a88f5 396 if (status == SS_ERR_BTLDR_CHECKSUM) {
seyhmus.cacina 0:b259fd1a88f5 397
seyhmus.cacina 0:b259fd1a88f5 398 data_len_flash = snprintf(charbuf_flash, sizeof(charbuf_flash), "\r\npageFlashDone err=%d\r\n", FLASH_ERR_CHECKSUM);
seyhmus.cacina 0:b259fd1a88f5 399 SERIALOUT(charbuf_flash);
seyhmus.cacina 0:b259fd1a88f5 400 } else if (status != SS_SUCCESS) {
seyhmus.cacina 0:b259fd1a88f5 401
seyhmus.cacina 0:b259fd1a88f5 402 data_len_flash = snprintf(charbuf_flash, sizeof(charbuf_flash), "\r\npageFlashDone err=%d\r\n", FLASH_ERR_GENERAL);
seyhmus.cacina 0:b259fd1a88f5 403 SERIALOUT(charbuf_flash);
seyhmus.cacina 0:b259fd1a88f5 404 } else {
seyhmus.cacina 0:b259fd1a88f5 405 currentPage++;
seyhmus.cacina 0:b259fd1a88f5 406
seyhmus.cacina 0:b259fd1a88f5 407 data_len_flash = snprintf(charbuf_flash, sizeof(charbuf_flash), "\r\npageFlashDone err=%d\r\n", COMM_SUCCESS);
seyhmus.cacina 0:b259fd1a88f5 408 SERIALOUT(charbuf_flash);
seyhmus.cacina 0:b259fd1a88f5 409 }
seyhmus.cacina 0:b259fd1a88f5 410 totalBytes = 0;
seyhmus.cacina 0:b259fd1a88f5 411
seyhmus.cacina 0:b259fd1a88f5 412 }
seyhmus.cacina 0:b259fd1a88f5 413 //SERIALOUT(" all pages are flashed \r\n");
seyhmus.cacina 0:b259fd1a88f5 414 clear_state_info();
seyhmus.cacina 0:b259fd1a88f5 415
seyhmus.cacina 0:b259fd1a88f5 416 return status;
seyhmus.cacina 0:b259fd1a88f5 417
seyhmus.cacina 0:b259fd1a88f5 418 }
seyhmus.cacina 0:b259fd1a88f5 419
seyhmus.cacina 0:b259fd1a88f5 420
seyhmus.cacina 0:b259fd1a88f5 421 int SH_BOOTLDR_flash(const char *null_arg){
seyhmus.cacina 0:b259fd1a88f5 422
seyhmus.cacina 0:b259fd1a88f5 423 int status;
seyhmus.cacina 0:b259fd1a88f5 424 if(bootldrState.flag_image_on_ram)
seyhmus.cacina 0:b259fd1a88f5 425 status = SH_BOOTLDR_receive_image_to_ram();
seyhmus.cacina 0:b259fd1a88f5 426 else
seyhmus.cacina 0:b259fd1a88f5 427 status = SH_BOOTLDR_flash_pages();
seyhmus.cacina 0:b259fd1a88f5 428
seyhmus.cacina 0:b259fd1a88f5 429 SERIALOUT("\r\n%s err=%d\r\n", "flash", status);
seyhmus.cacina 0:b259fd1a88f5 430
seyhmus.cacina 0:b259fd1a88f5 431 return status;
seyhmus.cacina 0:b259fd1a88f5 432 }
seyhmus.cacina 0:b259fd1a88f5 433
seyhmus.cacina 0:b259fd1a88f5 434
seyhmus.cacina 0:b259fd1a88f5 435 int SH_BOOTLDR_flash_appimage_from_ram(const char *null_arg){
seyhmus.cacina 0:b259fd1a88f5 436
seyhmus.cacina 0:b259fd1a88f5 437
seyhmus.cacina 0:b259fd1a88f5 438 int currentPage = 1;
seyhmus.cacina 0:b259fd1a88f5 439 uint32_t offset = 0;
seyhmus.cacina 0:b259fd1a88f5 440 int ret;
seyhmus.cacina 0:b259fd1a88f5 441
seyhmus.cacina 0:b259fd1a88f5 442 if(app_image == NULL)
seyhmus.cacina 0:b259fd1a88f5 443 return -1;
seyhmus.cacina 0:b259fd1a88f5 444
seyhmus.cacina 0:b259fd1a88f5 445 /* Put device to bootloader mode */
seyhmus.cacina 0:b259fd1a88f5 446 int status = SH_BOOTLDR_enter_blmode(NULL);
seyhmus.cacina 0:b259fd1a88f5 447 if( status != 0x00)
seyhmus.cacina 0:b259fd1a88f5 448 return -1;
seyhmus.cacina 0:b259fd1a88f5 449
seyhmus.cacina 0:b259fd1a88f5 450 wait_ms(10);
seyhmus.cacina 0:b259fd1a88f5 451
seyhmus.cacina 0:b259fd1a88f5 452 status = sh_set_bootloader_numberofpages(app_image->num_pages);
seyhmus.cacina 0:b259fd1a88f5 453 SERIALOUT("*** set_num_page... ret: %d\n", ret);
seyhmus.cacina 0:b259fd1a88f5 454 if (status != 0x00) {
seyhmus.cacina 0:b259fd1a88f5 455 return BL_SET_NUM_PAGES_FAIL;
seyhmus.cacina 0:b259fd1a88f5 456 }
seyhmus.cacina 0:b259fd1a88f5 457
seyhmus.cacina 0:b259fd1a88f5 458 status = sh_set_bootloader_iv(app_image->nonce);
seyhmus.cacina 0:b259fd1a88f5 459 SERIALOUT("*** set_iv... ret: %d\n", ret);
seyhmus.cacina 0:b259fd1a88f5 460 if (status != 0) {
seyhmus.cacina 0:b259fd1a88f5 461 return BL_SET_IV_FAIL;
seyhmus.cacina 0:b259fd1a88f5 462 }
seyhmus.cacina 0:b259fd1a88f5 463
seyhmus.cacina 0:b259fd1a88f5 464 status = sh_set_bootloader_auth(app_image->auth);
seyhmus.cacina 0:b259fd1a88f5 465 SERIALOUT("*** set_auth... ret: %d\n", ret);
seyhmus.cacina 0:b259fd1a88f5 466 if (status != 0) {
seyhmus.cacina 0:b259fd1a88f5 467 return BL_FLASH_ERR_AUTH;
seyhmus.cacina 0:b259fd1a88f5 468 }
seyhmus.cacina 0:b259fd1a88f5 469
seyhmus.cacina 0:b259fd1a88f5 470 status = sh_set_bootloader_erase() ;
seyhmus.cacina 0:b259fd1a88f5 471 SERIALOUT("*** erase app memory... ret: %d\n", ret);
seyhmus.cacina 0:b259fd1a88f5 472 if (status != 0) {
seyhmus.cacina 0:b259fd1a88f5 473 return BL_FLASS_ERASE_FAIL;
seyhmus.cacina 0:b259fd1a88f5 474 }
seyhmus.cacina 0:b259fd1a88f5 475
seyhmus.cacina 0:b259fd1a88f5 476 static uint8_t tx_buf[MAX_PAGE_SIZE + CHECKBYTES_SIZE + 2];
seyhmus.cacina 0:b259fd1a88f5 477
seyhmus.cacina 0:b259fd1a88f5 478 tx_buf[0] = SS_FAM_W_BOOTLOADER;
seyhmus.cacina 0:b259fd1a88f5 479 tx_buf[1] = SS_CMDIDX_SENDPAGE;
seyhmus.cacina 0:b259fd1a88f5 480
seyhmus.cacina 0:b259fd1a88f5 481 while (currentPage <= app_image->num_pages) {
seyhmus.cacina 0:b259fd1a88f5 482
seyhmus.cacina 0:b259fd1a88f5 483 memcpy(&tx_buf[2], &app_image->pages[offset], MAX_PAGE_SIZE + CHECKBYTES_SIZE);
seyhmus.cacina 0:b259fd1a88f5 484
seyhmus.cacina 0:b259fd1a88f5 485 status = sh_write_cmd(tx_buf, MAX_PAGE_SIZE + CHECKBYTES_SIZE + 2, bootldrState.bootcmds_delay_factor * PAGE_WRITE_DELAY_MS);
seyhmus.cacina 0:b259fd1a88f5 486
seyhmus.cacina 0:b259fd1a88f5 487 if (status == SS_ERR_BTLDR_CHECKSUM) {
seyhmus.cacina 0:b259fd1a88f5 488
seyhmus.cacina 0:b259fd1a88f5 489 SERIALOUT("\r\npageFlashDone err=%d\r\n", BL_FLASH_ERR_CHECKSUM);
seyhmus.cacina 0:b259fd1a88f5 490 break;
seyhmus.cacina 0:b259fd1a88f5 491 } else if (status != SS_SUCCESS) {
seyhmus.cacina 0:b259fd1a88f5 492
seyhmus.cacina 0:b259fd1a88f5 493 SERIALOUT("\r\npageFlashDone err=%d\r\n", BL_FLASH_ERR_GENERAL);
seyhmus.cacina 0:b259fd1a88f5 494 break;
seyhmus.cacina 0:b259fd1a88f5 495 } else {
seyhmus.cacina 0:b259fd1a88f5 496
seyhmus.cacina 0:b259fd1a88f5 497 SERIALOUT("\r\npageFlashDone err=%d\r\n", COMM_SUCCESS);
seyhmus.cacina 0:b259fd1a88f5 498 }
seyhmus.cacina 0:b259fd1a88f5 499
seyhmus.cacina 0:b259fd1a88f5 500 offset += MAX_PAGE_SIZE + CHECKBYTES_SIZE;
seyhmus.cacina 0:b259fd1a88f5 501 currentPage++;
seyhmus.cacina 0:b259fd1a88f5 502 }
seyhmus.cacina 0:b259fd1a88f5 503
seyhmus.cacina 0:b259fd1a88f5 504 return COMM_SUCCESS;
seyhmus.cacina 0:b259fd1a88f5 505
seyhmus.cacina 0:b259fd1a88f5 506 }
seyhmus.cacina 0:b259fd1a88f5 507
seyhmus.cacina 0:b259fd1a88f5 508 int SH_BOOTLDR_set_host_ebl_mode(const char *arg) {
seyhmus.cacina 0:b259fd1a88f5 509
seyhmus.cacina 0:b259fd1a88f5 510 int status;
seyhmus.cacina 0:b259fd1a88f5 511 int tmp;
seyhmus.cacina 0:b259fd1a88f5 512 sscanf(arg, "%*s %*s %*s %d", &tmp);
seyhmus.cacina 0:b259fd1a88f5 513 status = sh_set_ebl_mode(tmp);
seyhmus.cacina 0:b259fd1a88f5 514 if( status == 0x00) {
seyhmus.cacina 0:b259fd1a88f5 515 bootldrState.ebl_mode = tmp;
seyhmus.cacina 0:b259fd1a88f5 516 }else
seyhmus.cacina 0:b259fd1a88f5 517 status = COMM_INVALID_PARAM;
seyhmus.cacina 0:b259fd1a88f5 518
seyhmus.cacina 0:b259fd1a88f5 519 SERIALOUT("\r\n%s err=%d\r\n", "set_cfg host ebl",status);
seyhmus.cacina 0:b259fd1a88f5 520
seyhmus.cacina 0:b259fd1a88f5 521 return status;
seyhmus.cacina 0:b259fd1a88f5 522
seyhmus.cacina 0:b259fd1a88f5 523 }
seyhmus.cacina 0:b259fd1a88f5 524
seyhmus.cacina 0:b259fd1a88f5 525 int SH_BOOTLDR_get_host_ebl_mode(const char *null_arg){
seyhmus.cacina 0:b259fd1a88f5 526
seyhmus.cacina 0:b259fd1a88f5 527 int value;
seyhmus.cacina 0:b259fd1a88f5 528 value = sh_get_ebl_mode();
seyhmus.cacina 0:b259fd1a88f5 529 SERIALOUT("\r\n%s value=%s\r\n", "get_cfg host ebl", (value==1)? "GPIO_RST_MODE":"CMD_RST_MODE");
seyhmus.cacina 0:b259fd1a88f5 530 return 0x00;
seyhmus.cacina 0:b259fd1a88f5 531 }
seyhmus.cacina 0:b259fd1a88f5 532
seyhmus.cacina 0:b259fd1a88f5 533 int SH_BOOTLDR_set_host_bootcmds_delay_factor( const char *arg) {
seyhmus.cacina 0:b259fd1a88f5 534
seyhmus.cacina 0:b259fd1a88f5 535 int status;
seyhmus.cacina 0:b259fd1a88f5 536 int tmp;
seyhmus.cacina 0:b259fd1a88f5 537 sscanf(arg, "%*s %*s %*s %d", &tmp);
seyhmus.cacina 0:b259fd1a88f5 538 status = sh_set_bootloader_delayfactor( tmp);
seyhmus.cacina 0:b259fd1a88f5 539 if( status == 0x00) {
seyhmus.cacina 0:b259fd1a88f5 540 bootldrState.bootcmds_delay_factor = tmp;
seyhmus.cacina 0:b259fd1a88f5 541 }else
seyhmus.cacina 0:b259fd1a88f5 542 status = COMM_INVALID_PARAM;
seyhmus.cacina 0:b259fd1a88f5 543
seyhmus.cacina 0:b259fd1a88f5 544 SERIALOUT("\r\n%s err=%d\r\n", "set_cfg host cdf",status);
seyhmus.cacina 0:b259fd1a88f5 545
seyhmus.cacina 0:b259fd1a88f5 546 return status;
seyhmus.cacina 0:b259fd1a88f5 547
seyhmus.cacina 0:b259fd1a88f5 548 }
seyhmus.cacina 0:b259fd1a88f5 549
seyhmus.cacina 0:b259fd1a88f5 550 int SH_BOOTLDR_get_host_bootcmds_delay_factor( const char *null_arg){
seyhmus.cacina 0:b259fd1a88f5 551
seyhmus.cacina 0:b259fd1a88f5 552 int value;
seyhmus.cacina 0:b259fd1a88f5 553 value = sh_get_bootloader_delayfactor();
seyhmus.cacina 0:b259fd1a88f5 554 SERIALOUT("\r\n%s value=%d \r\n", "get_cfg host cdf", value);
seyhmus.cacina 0:b259fd1a88f5 555 return 0x00;
seyhmus.cacina 0:b259fd1a88f5 556
seyhmus.cacina 0:b259fd1a88f5 557 }
seyhmus.cacina 0:b259fd1a88f5 558
seyhmus.cacina 0:b259fd1a88f5 559 static int is_hub_ready_for_flash(void){
seyhmus.cacina 0:b259fd1a88f5 560
seyhmus.cacina 0:b259fd1a88f5 561 int status = 0;
seyhmus.cacina 0:b259fd1a88f5 562 if( bootldrState.hub_mode_bootloader == 1 &&
seyhmus.cacina 0:b259fd1a88f5 563 bootldrState.is_auth_done == 1 &&
seyhmus.cacina 0:b259fd1a88f5 564 bootldrState.is_iv_set == 1 &&
seyhmus.cacina 0:b259fd1a88f5 565 bootldrState.is_flash_erased == 1 &&
seyhmus.cacina 0:b259fd1a88f5 566 bootldrState.num_pages > 0 &&
seyhmus.cacina 0:b259fd1a88f5 567 bootldrState.page_size > 0 )
seyhmus.cacina 0:b259fd1a88f5 568 status = 1;
seyhmus.cacina 0:b259fd1a88f5 569
seyhmus.cacina 0:b259fd1a88f5 570 return status;
seyhmus.cacina 0:b259fd1a88f5 571 }
seyhmus.cacina 0:b259fd1a88f5 572
seyhmus.cacina 0:b259fd1a88f5 573 static void clear_state_info(void) {
seyhmus.cacina 0:b259fd1a88f5 574
seyhmus.cacina 0:b259fd1a88f5 575 bootldrState.is_auth_done = 0;
seyhmus.cacina 0:b259fd1a88f5 576 bootldrState.is_iv_set = 0;
seyhmus.cacina 0:b259fd1a88f5 577 bootldrState.is_flash_erased = 0;
seyhmus.cacina 0:b259fd1a88f5 578 bootldrState.num_pages = 0;
seyhmus.cacina 0:b259fd1a88f5 579 bootldrState.page_size = 0;
seyhmus.cacina 0:b259fd1a88f5 580 bootldrState.hub_mode_bootloader;
seyhmus.cacina 0:b259fd1a88f5 581
seyhmus.cacina 0:b259fd1a88f5 582 return;
seyhmus.cacina 0:b259fd1a88f5 583 }
seyhmus.cacina 0:b259fd1a88f5 584
seyhmus.cacina 0:b259fd1a88f5 585 static int parse_iv(const char* cmd, uint8_t* iv_bytes) {
seyhmus.cacina 0:b259fd1a88f5 586
seyhmus.cacina 0:b259fd1a88f5 587 int status = 0x00;
seyhmus.cacina 0:b259fd1a88f5 588 char cmdStr[] = "set_iv ";
seyhmus.cacina 0:b259fd1a88f5 589 int length = strlen(cmd);
seyhmus.cacina 0:b259fd1a88f5 590 int expected_length = strlen(cmdStr) + 2*AES_NONCE_SIZE;
seyhmus.cacina 0:b259fd1a88f5 591
seyhmus.cacina 0:b259fd1a88f5 592 if (length != expected_length) {
seyhmus.cacina 0:b259fd1a88f5 593 SERIALOUT("Couldn't parse IV, incorrect number of characters (len:%d, expected:%d)\n",
seyhmus.cacina 0:b259fd1a88f5 594 length, expected_length);
seyhmus.cacina 0:b259fd1a88f5 595 status = COMM_INVALID_PARAM;
seyhmus.cacina 0:b259fd1a88f5 596 }else{
seyhmus.cacina 0:b259fd1a88f5 597
seyhmus.cacina 0:b259fd1a88f5 598 const char* ivPtr = cmd + strlen(cmdStr);
seyhmus.cacina 0:b259fd1a88f5 599 int num_found;
seyhmus.cacina 0:b259fd1a88f5 600 int byteVal;
seyhmus.cacina 0:b259fd1a88f5 601 for (int ividx = 0; ividx < AES_NONCE_SIZE; ividx++) {
seyhmus.cacina 0:b259fd1a88f5 602 num_found = sscanf(ivPtr, "%2X", &byteVal);
seyhmus.cacina 0:b259fd1a88f5 603
seyhmus.cacina 0:b259fd1a88f5 604 if (num_found != 1 || byteVal > 0xFF) {
seyhmus.cacina 0:b259fd1a88f5 605 status = COMM_INVALID_PARAM;
seyhmus.cacina 0:b259fd1a88f5 606 //break; //
seyhmus.cacina 0:b259fd1a88f5 607 return status;
seyhmus.cacina 0:b259fd1a88f5 608 }
seyhmus.cacina 0:b259fd1a88f5 609 iv_bytes[ividx] = (uint8_t)byteVal;
seyhmus.cacina 0:b259fd1a88f5 610 ivPtr += 2;
seyhmus.cacina 0:b259fd1a88f5 611 }
seyhmus.cacina 0:b259fd1a88f5 612 }
seyhmus.cacina 0:b259fd1a88f5 613 return status;
seyhmus.cacina 0:b259fd1a88f5 614
seyhmus.cacina 0:b259fd1a88f5 615 }
seyhmus.cacina 0:b259fd1a88f5 616
seyhmus.cacina 0:b259fd1a88f5 617
seyhmus.cacina 0:b259fd1a88f5 618 static int parse_auth(const char* cmd, uint8_t *auth_bytes){
seyhmus.cacina 0:b259fd1a88f5 619
seyhmus.cacina 0:b259fd1a88f5 620 int status = 0x00;
seyhmus.cacina 0:b259fd1a88f5 621 char cmdStr[] = "set_auth ";
seyhmus.cacina 0:b259fd1a88f5 622 int length = strlen(cmd);
seyhmus.cacina 0:b259fd1a88f5 623 int expected_length = strlen(cmdStr) + 2*AES_AUTH_SIZE;
seyhmus.cacina 0:b259fd1a88f5 624
seyhmus.cacina 0:b259fd1a88f5 625 if (length != expected_length) {
seyhmus.cacina 0:b259fd1a88f5 626 status = -1;
seyhmus.cacina 0:b259fd1a88f5 627 }else{
seyhmus.cacina 0:b259fd1a88f5 628
seyhmus.cacina 0:b259fd1a88f5 629 const char* macPtr = cmd + strlen(cmdStr);
seyhmus.cacina 0:b259fd1a88f5 630
seyhmus.cacina 0:b259fd1a88f5 631 int num_found;
seyhmus.cacina 0:b259fd1a88f5 632 int byteVal;
seyhmus.cacina 0:b259fd1a88f5 633 for (int aidx = 0; aidx < AES_AUTH_SIZE; aidx++) {
seyhmus.cacina 0:b259fd1a88f5 634 num_found = sscanf(macPtr, "%2X", &byteVal);
seyhmus.cacina 0:b259fd1a88f5 635
seyhmus.cacina 0:b259fd1a88f5 636 if (num_found != 1 || byteVal > 0xFF) {
seyhmus.cacina 0:b259fd1a88f5 637 status = COMM_INVALID_PARAM;;
seyhmus.cacina 0:b259fd1a88f5 638 //break; //
seyhmus.cacina 0:b259fd1a88f5 639 return status;
seyhmus.cacina 0:b259fd1a88f5 640 }
seyhmus.cacina 0:b259fd1a88f5 641
seyhmus.cacina 0:b259fd1a88f5 642 auth_bytes[aidx] = (uint8_t)byteVal;
seyhmus.cacina 0:b259fd1a88f5 643 macPtr += 2;
seyhmus.cacina 0:b259fd1a88f5 644 }
seyhmus.cacina 0:b259fd1a88f5 645
seyhmus.cacina 0:b259fd1a88f5 646 }
seyhmus.cacina 0:b259fd1a88f5 647 return status;
seyhmus.cacina 0:b259fd1a88f5 648
seyhmus.cacina 0:b259fd1a88f5 649 }
seyhmus.cacina 0:b259fd1a88f5 650
seyhmus.cacina 0:b259fd1a88f5 651