|
| 1 | +/* hifive1.c |
| 2 | + * |
| 3 | + * Copyright (C) 2019 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfBoot. |
| 6 | + * |
| 7 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfBoot is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | + |
| 22 | +#include <stdlib.h> |
| 23 | +#include <stdint.h> |
| 24 | +#include <string.h> |
| 25 | +#include "hal.h" |
| 26 | +#include "wolfboot/wolfboot.h" |
| 27 | + |
| 28 | +/* UART API's in hal/hifive1.c */ |
| 29 | +extern void uart_write(char c); |
| 30 | +extern char uart_read(void); |
| 31 | + |
| 32 | +#define MSGSIZE 16 |
| 33 | +#define PAGESIZE (0x1000) /* Flash sector: 4K */ |
| 34 | +static const char ERR='!'; |
| 35 | +static const char START='*'; |
| 36 | +static const char UPDATE='U'; |
| 37 | +static const char ACK='#'; |
| 38 | +static uint8_t msg[MSGSIZE]; |
| 39 | + |
| 40 | +uint8_t flash_page[PAGESIZE]; |
| 41 | +extern void write_page(uint32_t dst); |
| 42 | +static void ack(uint32_t _off) |
| 43 | +{ |
| 44 | + uint32_t offset = _off; |
| 45 | + uint8_t *off = (uint8_t *)(&offset); |
| 46 | + int i; |
| 47 | + uart_write(ACK); |
| 48 | + for (i = 0; i < 4; i++) { |
| 49 | + uart_write(off[i]); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +static int check(uint8_t *pkt, int size) |
| 54 | +{ |
| 55 | + int i; |
| 56 | + uint16_t c = 0; |
| 57 | + uint16_t c_rx = *((uint16_t *)(pkt + 2)); |
| 58 | + uint16_t *p = (uint16_t *)(pkt + 4); |
| 59 | + for (i = 0; i < ((size - 4) >> 1); i++) |
| 60 | + c += p[i]; |
| 61 | + if (c == c_rx) |
| 62 | + return 0; |
| 63 | + return -1; |
| 64 | +} |
| 65 | + |
| 66 | +void main(void) { |
| 67 | + uint32_t tlen = 0; |
| 68 | + volatile uint32_t recv_seq; |
| 69 | + uint32_t r_total = 0; |
| 70 | + uint32_t tot_len = 0; |
| 71 | + uint32_t next_seq = 0; |
| 72 | + uint32_t version = 0; |
| 73 | + uint8_t *v_array = (uint8_t *)&version; |
| 74 | + int i; |
| 75 | + |
| 76 | + hal_init(); /* defaults: CPU = 320MHz, Baud = 115200 */ |
| 77 | + |
| 78 | + memset(flash_page, 0xFF, PAGESIZE); |
| 79 | + |
| 80 | + version = wolfBoot_current_firmware_version(); |
| 81 | + if ((version & 0x01) == 0) |
| 82 | + wolfBoot_success(); |
| 83 | + uart_write(START); |
| 84 | + for (i = 3; i >= 0; i--) { |
| 85 | + uart_write(v_array[i]); |
| 86 | + } |
| 87 | + while (1) { |
| 88 | + r_total = 0; |
| 89 | + do { |
| 90 | + while(r_total < 2) { |
| 91 | + msg[r_total++] = uart_read(); |
| 92 | + if ((r_total == 2) && ((msg[0] != 0xA5) || msg[1] != 0x5A)) { |
| 93 | + r_total = 0; |
| 94 | + continue; |
| 95 | + } |
| 96 | + } |
| 97 | + msg[r_total++] = uart_read(); |
| 98 | + if ((tot_len == 0) && r_total == 2 + sizeof(uint32_t)) |
| 99 | + break; |
| 100 | + if ((r_total > 8) && (tot_len <= ((r_total - 8) + next_seq))) |
| 101 | + break; |
| 102 | + } while (r_total < MSGSIZE); |
| 103 | + if (tot_len == 0) { |
| 104 | + tlen = msg[2] + (msg[3] << 8) + (msg[4] << 16) + (msg[5] << 24); |
| 105 | + if (tlen > WOLFBOOT_PARTITION_SIZE - 8) { |
| 106 | + uart_write(ERR); |
| 107 | + uart_write(ERR); |
| 108 | + uart_write(ERR); |
| 109 | + uart_write(ERR); |
| 110 | + uart_write(START); |
| 111 | + recv_seq = 0; |
| 112 | + tot_len = 0; |
| 113 | + continue; |
| 114 | + } |
| 115 | + tot_len = tlen; |
| 116 | + ack(0); |
| 117 | + continue; |
| 118 | + } |
| 119 | + if (check(msg, r_total) < 0) { |
| 120 | + ack(next_seq); |
| 121 | + continue; |
| 122 | + } |
| 123 | + recv_seq = msg[4] + (msg[5] << 8) + (msg[6] << 16) + (msg[7] << 24); |
| 124 | + if (recv_seq == next_seq) |
| 125 | + { |
| 126 | + int psize = r_total - 8; |
| 127 | + int flash_page_idx = recv_seq % PAGESIZE; |
| 128 | + memcpy(&(flash_page[flash_page_idx]), msg + 8, psize); |
| 129 | + flash_page_idx += psize; |
| 130 | + if ((flash_page_idx == PAGESIZE) || (next_seq + psize >= tot_len)) { |
| 131 | + uint32_t dst = (WOLFBOOT_PARTITION_UPDATE_ADDRESS - 0x20000000) + recv_seq + psize - flash_page_idx; |
| 132 | + /* long jump */ |
| 133 | + asm volatile("mv a0, %0;" \ |
| 134 | + "la a2, write_page;" \ |
| 135 | + "jalr a2;" :: "r" (dst) : "a0","a2","a4", "memory"); |
| 136 | + asm volatile ("fence.i; fence r,r"); |
| 137 | + memset(flash_page, 0xFF, PAGESIZE); |
| 138 | + } |
| 139 | + next_seq += psize; |
| 140 | + } |
| 141 | + ack(next_seq); |
| 142 | + if (next_seq >= tot_len) { |
| 143 | + /* Update complete */ |
| 144 | + /* long jump */ |
| 145 | + asm volatile( "la a4, wolfBoot_update_trigger;" \ |
| 146 | + "jalr a4;" ::: "a4", "memory"); |
| 147 | + asm volatile ("fence.i; fence r,r"); |
| 148 | + break; |
| 149 | + } |
| 150 | + } |
| 151 | + /* Wait for reboot */ |
| 152 | + while(1) |
| 153 | + ; |
| 154 | +} |
0 commit comments