test

Dependencies:   BMI160 max32630hsp3 MemoryLCD USBDevice

Committer:
seyhmuscacina
Date:
Mon Mar 25 07:40:37 2019 +0000
Revision:
3:b8989dab0f88
Parent:
0:ac4dea3e2894
Fork the repository

Who changed what in which revision?

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