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
|
purpose: User interface for reflashing SPI FLASH parts
\ See license at end of file
\ This code is concerned with the user interface for getting
\ a new firmware image into memory and using it to program
\ a FLASH device from that image. The details of how to actually
\ access the FLASH device are defined elsewhere.
h# 4000 constant /chunk \ Convenient sized piece for progress reports
defer spi-progress ' 2drop to spi-progress ( offset size -- )
: write-flash-range ( adr end-offset start-offset -- )
." Writing" cr
?do ( adr )
\ Save time - don't write if the data is the same
i .x (cr ( adr )
spi-us d# 20 >= if ( adr )
\ Just write if reading is slow
true ( adr must-write? )
else ( adr )
dup /flash-block i flash-verify ( adr must-write? )
then ( adr must-write? )
if
i flash-erase-block
dup /flash-block i flash-write ( adr )
then
i /flash spi-progress ( adr )
/flash-block + ( adr' )
/flash-block +loop ( adr )
cr drop ( )
;
: verify-flash-range ( adr end-offset start-offset -- )
." Verifying" cr
?do ( adr )
i .x (cr
dup /flash-block i flash-verify abort" Verify failed"
/flash-block + ( adr' )
/flash-block +loop ( adr )
cr drop ( )
;
\ Perform a series of sanity checks on the new firmware image.
0 value file-loaded?
: crc ( adr len -- crc ) 0 crctab 2swap ($crc) ;
: ?crc ( -- )
." Checking integrity ..." cr
flash-buf crc-offset + ( crc-adr )
dup l@ >r ( crc-adr r: crc )
-1 over l! ( crc-adr r: crc )
flash-buf /flash crc ( crc-adr calc-crc r: crc )
r@ rot l! ( calc-crc r: crc )
r> <> abort" Firmware image has bad internal CRC"
;
: ?image-valid ( len -- )
/flash <> abort" Image file is the wrong length"
flash-buf signature-offset +
signature$ comp abort" Wrong machine signature"
?crc
;
: $get-file ( "filename" -- )
$read-open
flash-buf h# 40.0000 ifd @ fgets ( len )
ifd @ fclose
?image-valid
true to file-loaded?
;
: ?file ( -- )
file-loaded? 0= if
." You must first load a valid FLASH image file with" cr
." get-file filename" cr
abort
then
;
: read-flash ( "filename" -- )
writing
/flash 0 do
i .x (cr
flash-buf i + /chunk i flash-read
/chunk +loop
flash-buf /flash ofd @ fputs
ofd @ fclose
;
: verify ( -- ) ?file flash-buf /flash 0 verify-flash-range ;
: write-firmware ( -- )
flash-buf /flash 0 write-flash-range \ Write first part
;
: verify-firmware ( -- )
flash-buf /flash 0 verify-flash-range \ Verify first part
;
: .verify-msg ( -- )
." Type verify if you want to verify the data just written." cr
." Verification will take about 17 minutes if the host is running Linux" cr
." or about 5 minutes if the host is running OFW." cr
;
: reflash ( -- ) \ Flash from data already in memory
?file
flash-write-enable
write-firmware
spi-us d# 20 < if
['] verify-firmware catch if
." Verify failed. Retrying once" cr
spi-identify
write-firmware
verify-firmware
then
/flash dup spi-progress
flash-write-disable
else
.verify-msg
then
;
defer fw-filename$ ' null$ to fw-filename$
: get-file ( ["filename"] -- )
parse-word ( adr len )
dup 0= if 2drop fw-filename$ then ( adr len )
." Reading " 2dup type cr ( adr len )
$get-file
;
: flash ( ["filename"] -- ) get-file reflash ;
: safe-flash-read ( -- )
flash-buf /flash 0 flash-read
;
\ LICENSE_BEGIN
\ Copyright (c) 2006 FirmWorks
\
\ 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 THE AUTHORS OR COPYRIGHT HOLDERS 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.
\
\ LICENSE_END
|