max32xxx_write.c 10.7 KB
Newer Older
Kevin Gillespie's avatar
Kevin Gillespie committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/*******************************************************************************
 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated
 * Products, Inc. shall not be used except as stated in the Maxim Integrated
 * Products, Inc. Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products, Inc. retains all
 * ownership rights.
 *
 *******************************************************************************
 */

/***** Includes *****/
#ifdef ALGO_TEST
#include "mxc_config.h"
#endif

#include "tpu_regs.h"
#include "gcr_regs.h"
#include "flc_regs.h"
#include "algo_options.h"

#ifdef ALGO_TEST
#include <stdio.h>
#else
#define printf(...)
#endif

/***** Definitions *****/
#define MXC_BASE_TPU                    ((uint32_t)0x40001000UL)
#define MXC_TPU                         ((mxc_tpu_regs_t*)MXC_BASE_TPU)
#define MXC_BASE_GCR                    ((uint32_t)0x40000000UL)
#define MXC_GCR                         ((mxc_gcr_regs_t*)MXC_BASE_GCR)

/******************************************************************************/
#define getbyte(temp8)                                                          \
    /* Wait for the Read FIFO to not equal the Write FIFO */                    \
    while (*read_ptr == *write_ptr);                                            \
    temp8 = **read_ptr;                                                         \
                                                                                \
    /* Increment and wrap around the read pointer */                            \
64
    if ((*read_ptr + 1) >= (uint8_t*)(work_end - 8 - 256)) {                                          \
Kevin Gillespie's avatar
Kevin Gillespie committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        *read_ptr = (uint8_t *)(work_start + 8);                                       \
    } else {                                                                    \
        (*read_ptr)++;                                                          \
    }                                                                           \
    len--;                                                                      \
    addr++;

/******************************************************************************/
#ifndef ALGO_TEST
__attribute__ ((naked, section(".algo")))
#endif
void algo_write(uint8_t *work_start, uint8_t *work_end, uint32_t len, uint32_t addr)
{

    printf(" > algo_write() starting\n");

    uint8_t * volatile *write_ptr = (uint8_t **)work_start;
    uint8_t * volatile *read_ptr = (uint8_t **)(work_start + 4);
83 84 85
    uint32_t *flc_base = (uint32_t *)(work_end - 4 - 128);
    uint32_t *options = (uint32_t *)(work_end - 8 - 128);
    uint32_t *enc_buffer = (uint32_t *)(work_end - 8 - 256);
Kevin Gillespie's avatar
Kevin Gillespie committed
86 87 88
    uint8_t temp8;
    uint32_t addr_save;
    int i;
89
    mxc_flc_regs_t * MXC_FLC=(mxc_flc_regs_t*)*flc_base;
Kevin Gillespie's avatar
Kevin Gillespie committed
90

91 92
    printf(" > w%08x r%08x o%08x f%08x b%08x b%08x\n", 
        (uint32_t)write_ptr, (uint32_t)read_ptr, (uint32_t)*options, (uint32_t)*flc_base, (uint32_t)enc_buffer, (uint32_t)(enc_buffer + 256));
Kevin Gillespie's avatar
Kevin Gillespie committed
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    if(*options & OPTIONS_ENC) {
        /* Enable Memory Protection */
        MXC_GCR->scon |= MXC_F_GCR_SCON_MEMPROT_EN;

        /* Set the keysize */
        if(*options & OPTIONS_KEYSIZE) {
            MXC_GCR->scon |= MXC_F_GCR_SCON_MEMPROT_KEYSZ;
        } else {
            MXC_GCR->scon &= ~(MXC_F_GCR_SCON_MEMPROT_KEYSZ);
        }
    } else {
        /* Disable memory protection */
        MXC_GCR->scon &= ~MXC_F_GCR_SCON_MEMPROT_EN;
    }

    if(*options & OPTIONS_ENC) {
Kevin Gillespie's avatar
Kevin Gillespie committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        // Setup the AES

        /* Enable CRYPTO clock */
        if ((MXC_GCR->clkcn & MXC_F_GCR_CLKCN_HIRC_EN) == 0) {
            MXC_GCR->clkcn |= MXC_F_GCR_CLKCN_HIRC_EN;
        }

        /* Disable CRYPTO clock gate */
        if (MXC_GCR->perckcn0 & MXC_F_GCR_PERCKCN0_CRYPTOD) {
            MXC_GCR->perckcn0 &= ~(MXC_F_GCR_PERCKCN0_CRYPTOD);
        }

        // Reset Crypto block and clear state
        MXC_TPU->ctrl = MXC_F_TPU_CTRL_RST;

        // Set the legacy bit
        MXC_TPU->ctrl |= MXC_F_TPU_CTRL_FLAG_MODE;

        // Byte swap the input and output
        MXC_TPU->ctrl |= MXC_F_TPU_CTRL_BSO;
        MXC_TPU->ctrl |= MXC_F_TPU_CTRL_BSI;
    }

    while(len) {

135
        if((*options & OPTIONS_128) == 0) {
Kevin Gillespie's avatar
Kevin Gillespie committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

            // Save the current address before we read from the working area
            addr_save = addr;

            // 32-bit write
            MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;

            enc_buffer[0] = 0;
            for(i = 0; i < 4; i++) {
                // Get data from the working area, pad with 0xFF
                if(len) {
                    getbyte(temp8);
                } else {
                    temp8 = 0xFF;
                }  
                enc_buffer[0] |= (temp8 << (i*8));
            }

154 155 156
            // 32-bit write
            MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;

Kevin Gillespie's avatar
Kevin Gillespie committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
            MXC_FLC->addr = addr_save;
            MXC_FLC->data[0] = enc_buffer[0];

            // Enable the write
            MXC_FLC->cn |= MXC_F_FLC_CN_WR;

            // Wait for the operation to complete
            while(MXC_FLC->cn & MXC_F_FLC_CN_WR) {}

            /* Check access violations */
            if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
                MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
                #ifndef ALGO_TEST
                __asm("bkpt\n");
                #else
                printf(" > Error writing to flash\n");
                return;
                #endif
            }
        } else {

            // Save the current address before we read from the working area
            addr_save = addr;

            // Fill the buffer with the plain text data from the working area
            for(i = 0; i < 4; i++) {
                // Get data from the working area, pad with 0xFF
                enc_buffer[i] = 0;
                if(len) {
                    getbyte(temp8);
                } else {
                    temp8 = 0xFF;
                }  
                enc_buffer[i] |= (temp8 << (0));
                // Get data from the working area, pad with 0xFF
                if(len) {
                    getbyte(temp8);
                } else {
                    temp8 = 0xFF;
                }  
                enc_buffer[i] |= (temp8 << (8));
                // Get data from the working area, pad with 0xFF
                if(len) {
                    getbyte(temp8);
                } else {
                    temp8 = 0xFF;
                }  
                enc_buffer[i] |= (temp8 << (16));
                // Get data from the working area, pad with 0xFF
                if(len) {
                    getbyte(temp8);
                } else {
                    temp8 = 0xFF;
                }  
                enc_buffer[i] |= (temp8 << (24));
            }

214
            if(*options & OPTIONS_ENC) {
Kevin Gillespie's avatar
Kevin Gillespie committed
215 216 217

                // XOR data with the address
                for(i = 0; i < 4; i++) {
218
                    if(*options & OPTIONS_RELATIVE_XOR) {
Kevin Gillespie's avatar
Kevin Gillespie committed
219 220 221 222 223 224 225 226 227 228 229 230 231
                        enc_buffer[i] ^= ((addr_save & 0x00FFFFFF) + i*4);
                    } else {
                        enc_buffer[i] ^= (addr_save + i*4);
                    }     
                }

                // Encrypt the plain text
                // Clear interrupt flags
                MXC_TPU->ctrl |= MXC_F_TPU_CTRL_CPH_DONE;

                MXC_TPU->cipher_ctrl = ((0x0 << MXC_F_TPU_CIPHER_CTRL_MODE_POS) |
                    (0x0 << MXC_F_TPU_CIPHER_CTRL_ENC_POS));

232
                if(*options & OPTIONS_KEYSIZE) {
Kevin Gillespie's avatar
Kevin Gillespie committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
                    // ECB, AES-256, encrypt
                    MXC_TPU->cipher_ctrl |= (0x3 << MXC_F_TPU_CIPHER_CTRL_CIPHER_POS);
                } else {
                    // ECB, AES-128, encrypt
                    MXC_TPU->cipher_ctrl |= (0x1 << MXC_F_TPU_CIPHER_CTRL_CIPHER_POS);
                }

                // Set the key source
                MXC_TPU->cipher_ctrl = ((MXC_TPU->cipher_ctrl & ~MXC_F_TPU_CIPHER_CTRL_SRC) |
                    (0x3 << MXC_F_TPU_CIPHER_CTRL_SRC_POS));
                
                // Copy data to start the operation
                MXC_TPU->din[0] = enc_buffer[0];
                MXC_TPU->din[1] = enc_buffer[1];
                MXC_TPU->din[2] = enc_buffer[2];
                MXC_TPU->din[3] = enc_buffer[3];

                // Wait until operation is complete
                while(!(MXC_TPU->ctrl & MXC_F_TPU_CTRL_CPH_DONE)) {}

                // Copy the data out
                enc_buffer[0] = MXC_TPU->dout[0];
                enc_buffer[1] = MXC_TPU->dout[1];
                enc_buffer[2] = MXC_TPU->dout[2];
                enc_buffer[3] = MXC_TPU->dout[3];
            }

            // 128-bit write
            MXC_FLC->cn &= ~MXC_F_FLC_CN_WDTH;

            MXC_FLC->addr = addr_save;
            MXC_FLC->data[0] = enc_buffer[0];
            MXC_FLC->data[1] = enc_buffer[1];
            MXC_FLC->data[2] = enc_buffer[2];
            MXC_FLC->data[3] = enc_buffer[3];

            // Enable the write
            MXC_FLC->cn |= MXC_F_FLC_CN_WR;

            // Wait for the operation to complete
            while(MXC_FLC->cn & MXC_F_FLC_CN_WR) {}
            
            /* Check access violations */
            if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
                MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
                #ifndef ALGO_TEST
                __asm("bkpt\n");
                #else
                printf(" > Error writing to flash\n");
                return;
                #endif
            }
        }
    }
       
    #ifndef ALGO_TEST
    __asm("bkpt\n");
    #else
    printf(" > algo_write returning\n");
    return;
    #endif
}