1/*
2  Copyright (c) 2020 Arduino LLC.  All right reserved.
3
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  See the GNU Lesser General Public License for more details.
13
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17*/
18
19/**************************************************************************************
20   INCLUDE
21 **************************************************************************************/
22
23#include <FlashStorage.h>
24#include <MKRGSM.h>
25
26
27/**************************************************************************************
28   DEFINE
29 **************************************************************************************/
30
31#define SSU_START    0x2000
32#define SSU_SIZE     0x8000
33
34#define SKETCH_START (uint32_t*)(SSU_START + SSU_SIZE)
35
36/**************************************************************************************
37   GLOBAL CONSTANTS
38 **************************************************************************************/
39
40static constexpr char UPDATE_FILE_NAME[] = "UPDATE.BIN";
41static constexpr char CHECK_FILE_NAME[] = "UPDATE.OK";
42
43
44/**************************************************************************************
45   GLOBAL VARIABLES
46 **************************************************************************************/
47
48FlashClass mcu_flash;
49
50GSMFileUtils  fileUtils;
51
52/**************************************************************************************
53   FUNCTION DECLARATION
54 **************************************************************************************/
55
56extern "C" void __libc_init_array(void);
57
58/**************************************************************************************
59   MAIN
60 **************************************************************************************/
61
62int main()
63{
64  init();
65
66  __libc_init_array();
67
68  delay(1);
69
70  constexpr size_t blockSize = 512;
71
72  fileUtils.begin();
73
74  bool update_success = false;
75
76  // Try to update only if update file
77  // has been download successfully.
78  if (fileUtils.listFile(CHECK_FILE_NAME) == 1) {
79    uint32_t size = fileUtils.listFile(UPDATE_FILE_NAME);
80    size_t cycles = (size / blockSize) + 1;
81
82    if (size > SSU_SIZE) {
83      size -= SSU_SIZE;
84
85      /* Erase the MCU flash */
86      uint32_t flash_address = (uint32_t)SKETCH_START;
87      mcu_flash.erase((void*)flash_address, size);
88
89      for (auto i = 0; i < cycles; i++) {
90        uint8_t block[blockSize] { 0 };
91        digitalWrite(LED_BUILTIN, LOW);
92        uint32_t read = fileUtils.readBlock(UPDATE_FILE_NAME, (i * blockSize) + SSU_SIZE, blockSize, block);
93        digitalWrite(LED_BUILTIN, HIGH);
94        mcu_flash.write((void*)flash_address, block, read);
95        flash_address += read;
96      }
97      update_success = true;
98    }
99    if (update_success) {
100      fileUtils.deleteFile(UPDATE_FILE_NAME);
101      fileUtils.deleteFile(CHECK_FILE_NAME);
102    }
103  }
104  /* Jump to the sketch */
105  __set_MSP(*SKETCH_START);
106
107  /* Reset vector table address */
108  SCB->VTOR = ((uint32_t)(SKETCH_START) & SCB_VTOR_TBLOFF_Msk);
109
110  /* Address of Reset_Handler is written by the linker at the beginning of the .text section (see linker script) */
111  uint32_t resetHandlerAddress = (uint32_t) * (SKETCH_START + 1);
112  /* Jump to reset handler */
113  asm("bx %0"::"r"(resetHandlerAddress));
114}
115