forked from mist-devel/mist-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firmware.c
229 lines (194 loc) · 7.22 KB
/
firmware.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Copyright 2008, 2009 Jakub Bednarski
This file is part of Minimig
Minimig is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Minimig is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdio.h"
#include "string.h"
#include "errors.h"
#include "hardware.h"
#include "irqflags.h"
#include "barriers.h"
#include "fat_compat.h"
#include "firmware.h"
#ifndef FW_ID
#define FW_ID "MNMGUPG"
#endif
static DWORD clmt[99];
unsigned long CalculateCRC32(unsigned long crc, unsigned char *pBuffer, unsigned long nSize) {
int i, j;
unsigned long byte, mask;
while (nSize--) {
byte = *pBuffer++; // Get next byte.
crc = crc ^ byte;
for (j = 7; j >= 0; j--) { // Do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
}
return crc;
}
unsigned char CheckFirmware(char *name)
{
UPGRADE *pUpgrade = (UPGRADE*)sector_buffer;
unsigned long crc;
unsigned long size;
unsigned long rom_size;
unsigned long rom_crc;
unsigned long read_size;
FIL file;
Error = ERROR_FILE_NOT_FOUND;
if (f_open(&file, name, FA_READ) == FR_OK)
{
Error = ERROR_INVALID_DATA;
iprintf("Upgrade file size : %llu\r", f_size(&file));
iprintf("Upgrade header size : %lu\r", (unsigned long)sizeof(UPGRADE));
if (f_size(&file) >= sizeof(UPGRADE))
{
clmt[0] = 99;
file.cltbl = clmt;
if (f_lseek(&file, CREATE_LINKMAP) == FR_OK) {
FileReadNextBlock(&file, sector_buffer);
crc = ~CalculateCRC32(-1, sector_buffer, sizeof(UPGRADE) - 4);
iprintf("Upgrade ROM size : %lu\r", pUpgrade->rom.size);
iprintf("Upgrade header CRC : %08lX\r", pUpgrade->crc);
iprintf("Calculated header CRC : %08lX\r", crc);
if (pUpgrade->crc == crc)
{
if (strncmp((const char*)pUpgrade->id, FW_ID, 7) == 0 && pUpgrade->id[7] == 0)
{
if (pUpgrade->rom.size == f_size(&file) - sizeof(UPGRADE))
{
rom_size = pUpgrade->rom.size;
rom_crc = pUpgrade->rom.crc;
crc = -1; // initial CRC32 value
size = rom_size;
while (size)
{
if (size > 512)
read_size = 512;
else
read_size = size;
FileReadNextBlock(&file, sector_buffer);
crc = CalculateCRC32(crc, sector_buffer, read_size);
size -= read_size;
}
iprintf("Calculated ROM CRC : %08lX\r", ~crc);
iprintf("ROM CRC from header : %08lX\r", rom_crc);
if (~crc == rom_crc)
{ // upgrade file CRC is OK so go back to the beginning of the file
f_close(&file);
Error = ERROR_NONE;
return 1;
}
else iprintf("ROM CRC mismatch! from header: %08lX, calculated: %08lX\r", rom_crc, ~crc);
}
else iprintf("ROM size mismatch! from header: %lu, from file: %llu\r", pUpgrade->rom.size, f_size(&file)-sizeof(UPGRADE));
}
else iprintf("Invalid upgrade file header!\r");
}
else iprintf("Header CRC mismatch! from header: %08lX, calculated: %08lX\r", pUpgrade->crc, crc);
}
else iprintf("Error creating linkmap\r");
}
else iprintf("Upgrade file size too small: %llu\r", f_size(&file));
f_close(&file);
}
else iprintf("Cannot open firmware file!\r");
return 0;
}
char *GetFirmwareVersion(char *name) {
static char v[16];
FIL file;
if ((f_open(&file, name, FA_READ) != FR_OK) || (f_size(&file) < sizeof(UPGRADE)))
return NULL;
FileReadBlock(&file, sector_buffer);
strncpy(v, ((UPGRADE*)sector_buffer)->version, 16);
v[15] = 0;
f_close(&file);
return v;
}
// enable some nasty hacks to prevent gcc calling memset/memcpy during flash as these
// are library functions placed in flash and thus must not be called while flash is being
// overwritten
#define GCC_OPTIMZES_TOO_MUCH
#pragma section_code_init
RAMFUNC void WriteFirmware(char *name)
{
unsigned long read_size;
unsigned long i;
unsigned long k;
unsigned long page;
unsigned long *pSrc;
unsigned long *pDst;
FSIZE_t size;
FIL file;
// Since the file may have changed in the meantime, it needs to be
// opened again...
if (f_open(&file, name, FA_READ) != FR_OK) return;
clmt[0] = 99;
file.cltbl = clmt;
if ((f_lseek(&file, CREATE_LINKMAP) != FR_OK) ||
(f_lseek(&file, sizeof(UPGRADE)) != FR_OK) ||
(f_tell(&file) != sizeof(UPGRADE))) {
f_close(&file);
return;
}
size = f_size(&file) - sizeof(UPGRADE);
// All interrupts have to be disabled.
arch_irq_disable();
// asm volatile ("mrs r12, CPSR; orr r12, r12, #0xC0; msr CPSR_c, r12"
// : /* No outputs */
// : /* No inputs */
// : "r12", "cc");
// Hack to foul FatFs to not handle a final partial sector (to avoid a memcpy)
file.obj.objsize = (file.obj.objsize + 511) & 0xfffffe00;
page = 0;
pDst = 0;
UnlockFlash();
while (size)
{
if (size > 512)
read_size = 512;
else
read_size = size;
// On _any_ error the upgrade will fail :-(
// then the firmware needs to be upgraded by another way!
FileReadNextBlock(&file, sector_buffer);
#ifndef GCC_OPTIMZES_TOO_MUCH // the latest gcc 4.8.0 calls memset for this
// it doesn't hurt to not do this at all
// fill the rest of buffer
for (i = read_size; i < 512; i++)
sector_buffer[i] = 0xFF;
#endif
// programming time: 13.2 ms per disk sector (512B)
pSrc = (unsigned long*)sector_buffer;
k = 512/FLASH_PAGESIZE;
while (k--)
{
if(size & 2048) DISKLED_ON
else DISKLED_OFF;
i = FLASH_PAGESIZE / 4;
while (i--) {
*pDst++ = *pSrc++;
dmb();
}
WriteFlash(page);
page++;
}
size -= read_size;
}
DISKLED_OFF;
MCUReset(); // restart
for(;;);
}
#pragma section_no_code_init