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
230
231
232
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id:
*
* Copyright © 2009 Michael Sparmann
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "config.h"
#include "system.h"
#include "s5l87xx.h"
#include "spi-s5l8702.h"
#include "crypto-s5l8702.h"
#include "norboot-target.h"
static void bootflash_ce(int port, bool state)
{
spi_ce(port, state);
}
void bootflash_init(int port)
{
spi_init(port, true);
bootflash_ce(port, false);
}
void bootflash_close(int port)
{
spi_init(port, false);
}
static void bootflash_wait_ready(int port)
{
while (true)
{
bootflash_ce(port, true);
spi_write(port, 5);
if (!(spi_write(port, 0xff) & 1)) break;
bootflash_ce(port, false);
}
bootflash_ce(port, false);
}
static void bootflash_enable_writing(int port, bool state)
{
if (!state)
{
bootflash_ce(port, true);
spi_write(port, 4);
bootflash_ce(port, false);
}
bootflash_ce(port, true);
spi_write(port, 0x50);
bootflash_ce(port, false);
bootflash_ce(port, true);
spi_write(port, 1);
spi_write(port, state ? 0 : 0x1c);
bootflash_ce(port, false);
if (state)
{
bootflash_ce(port, true);
spi_write(port, 6);
bootflash_ce(port, false);
}
}
void bootflash_read(int port, uint32_t addr, uint32_t size, void* buf)
{
spi_prepare(port);
bootflash_wait_ready(port);
bootflash_ce(port, true);
spi_write(port, 3);
spi_write(port, (addr >> 16) & 0xff);
spi_write(port, (addr >> 8) & 0xff);
spi_write(port, addr & 0xff);
spi_read(port, size, buf);
bootflash_ce(port, false);
spi_release(port);
}
static void bootflash_write_internal(int port, uint32_t addr, uint32_t size, void* buf)
{
uint8_t* buffer = (uint8_t*)buf;
bool first = true;
spi_prepare(port);
bootflash_wait_ready(port);
bootflash_enable_writing(port, true);
while (size)
{
bootflash_ce(port, true);
spi_write(port, 0xad);
if (first)
{
spi_write(port, (addr >> 16) & 0xff);
spi_write(port, (addr >> 8) & 0xff);
spi_write(port, addr & 0xff);
first = false;
}
spi_write(port, *buffer++);
spi_write(port, *buffer++);
bootflash_ce(port, false);
bootflash_wait_ready(port);
size -= 2;
}
bootflash_enable_writing(port, false);
spi_release(port);
}
static void bootflash_erase_internal(int port, uint32_t addr)
{
spi_prepare(port);
bootflash_wait_ready(port);
bootflash_enable_writing(port, true);
bootflash_ce(port, true);
spi_write(port, 0x20);
spi_write(port, (addr >> 16) & 0xff);
spi_write(port, (addr >> 8) & 0xff);
spi_write(port, addr & 0xff);
bootflash_ce(port, false);
bootflash_enable_writing(port, false);
spi_release(port);
}
void bootflash_erase_blocks(int port, int first, int n)
{
uint32_t offset = first << 12;
while (n--)
{
bootflash_erase_internal(port, offset);
offset += 0x1000;
}
}
int bootflash_compare(int port, int offset, void* addr, int size)
{
int i;
int result = 0;
uint8_t buf[32];
spi_prepare(port);
bootflash_wait_ready(port);
bootflash_ce(port, true);
spi_write(port, 3);
spi_write(port, (offset >> 16) & 0xff);
spi_write(port, (offset >> 8) & 0xff);
spi_write(port, offset & 0xff);
while (size > 0)
{
spi_read(port, MIN((int)sizeof(buf), size), buf);
if (memcmp((uint8_t*)addr, buf, MIN((int)sizeof(buf), size)))
result |= 1;
for (i = 0; i < MIN((int)sizeof(buf), size); i += 2)
if (buf[i] != 0xff) result |= 2;
addr = (void*)(((uint32_t)addr) + sizeof(buf));
size -= sizeof(buf);
}
bootflash_ce(port, false);
spi_release(port);
return result;
}
void bootflash_write(int port, int offset, void* addr, int size)
{
int i;
bool needswrite;
while (size > 0)
{
int remainder = MIN(0x1000 - (offset & 0xfff), size);
int contentinfo = bootflash_compare(port, offset, addr, remainder);
if (contentinfo & 1)
{
if (contentinfo & 2)
bootflash_erase_internal(port, offset & ~0xfff);
needswrite = false;
for (i = 0; i < remainder; i += 1)
if (((uint8_t*)addr)[i] != 0xff)
{
needswrite = true;
break;
}
if (needswrite)
bootflash_write_internal(port, offset, remainder, addr);
}
addr = (void*)(((uint32_t)addr) + remainder);
offset += remainder;
size -= remainder;
}
}
/*
* IM3
*/
static inline uint32_t get_uint32le(unsigned char *p)
{
return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}
// TODO: rename to bootflash_im3_sz(), norboot_im3_sz() or nb_im3_sz()
/* return full IM3 size aligned to NOR sector size */
unsigned im3_nor_sz(struct Im3Info* hinfo)
{
return ALIGN_UP(IM3HDR_SZ + get_uint32le(hinfo->data_sz), 0x1000);
}
// TODO: norboot_im3_read(), on s5l8720 we could set nandboot_im3_read(), then norboot_ and nandboot_ could go to boot-s5l8702.c
int im3_read(uint32_t offset, struct Im3Info *hinfo, void *fw_addr)
{
unsigned char hash[SIGN_SZ];
uint32_t fw_size;
/* header */
bootflash_init(SPI_PORT);
bootflash_read(SPI_PORT, offset, IM3HDR_SZ, hinfo);
bootflash_close(SPI_PORT);
if (memcmp(hinfo, IM3_IDENT, 4) != 0)
return -1; /* OF not found */
im3_sign(HWKEYAES_UKEY, hinfo, IM3INFOSIGN_SZ, hash);
if (memcmp(hash, hinfo->info_sign, SIGN_SZ) != 0)
return -2; /* corrupt header */
fw_size = get_uint32le(hinfo->data_sz);
if ((fw_size > NORBOOT_MAXSZ - IM3HDR_SZ) ||
(get_uint32le(hinfo->entry) > fw_size))
return -3; /* wrong info */
if (hinfo->enc_type != 1 && hinfo->enc_type != 2)
return -4; /* encrypt type not supported */
if (fw_addr)
{
/* body */
bootflash_init(SPI_PORT);
bootflash_read(SPI_PORT, offset + IM3HDR_SZ, fw_size, fw_addr);
bootflash_close(SPI_PORT);
if (hinfo->enc_type == 1)
im3_crypt(HWKEYAES_DECRYPT, hinfo, fw_addr);
im3_sign(HWKEYAES_UKEY, fw_addr, fw_size, hash);
if (memcmp(hash, hinfo->u.enc12.data_sign, SIGN_SZ) != 0)
return -5; /* corrupt data */
}
return 0;
}
bool im3_write(int offset, void *im3_addr)
{
bool res;
struct Im3Info *hinfo = (struct Im3Info *)im3_addr;
uint32_t im3_size = get_uint32le(hinfo->data_sz) + IM3HDR_SZ;
bootflash_init(SPI_PORT);
bootflash_write(SPI_PORT, offset, im3_addr, im3_size);
/* check if the IM3 image was written correctly */
res = !(bootflash_compare(SPI_PORT, offset, im3_addr, im3_size) & 1);
bootflash_close(SPI_PORT);
return res;
}
/*
* flsh FS
*/
unsigned flsh_get_unused(void)
{
unsigned tail = DIR_OFF;
bootflash_init(SPI_PORT);
for (int i = 0; i < MAX_ENTRY; i++)
{
image_t entry;
bootflash_read(SPI_PORT, DIR_OFF + ENTRY_SZ*i, ENTRY_SZ, &entry);
if (memcmp(entry.type, "hslf", 4))
break; /* no more entries */
if (entry.devOffset < tail)
tail = entry.devOffset;
}
bootflash_close(SPI_PORT);
return tail - (NORBOOT_OFF + NORBOOT_MAXSZ);
}
int flsh_find_file(char *name, image_t *entry)
{
int found = 0;
bootflash_init(SPI_PORT);
for (int off = 0; off < ENTRY_SZ*MAX_ENTRY; off += ENTRY_SZ)
{
bootflash_read(SPI_PORT, DIR_OFF+off, ENTRY_SZ, entry);
if (memcmp(&(entry->type), "hslf", 4))
break; /* no more entries */
if (!memcmp(&(entry->id), name, 4)) {
found = 1;
break;
}
}
bootflash_close(SPI_PORT);
return found;
}
#define KEYIDX_OFF 0x8 /* TBC */
#define DATALEN_OFF 0x14
#define DATASHA_OFF 0x1c
#define HEADSHA_OFF 0x1d4
int flsh_load_file(image_t *entry, void *hdr, void *data)
{
uint8_t orig_hash[SHA1_SZ];
uint8_t calc_hash[SHA1_SZ];
uint32_t data_len;
/* header */
bootflash_init(SPI_PORT);
bootflash_read(SPI_PORT, entry->devOffset, FILEHDR_SZ, hdr);
bootflash_close(SPI_PORT);
memcpy(orig_hash, hdr + HEADSHA_OFF, SHA1_SZ);
memset(hdr+HEADSHA_OFF, 0, SHA1_SZ);
sha1(hdr, FILEHDR_SZ, calc_hash);
if (memcmp(calc_hash, orig_hash, SHA1_SZ))
return -1; /* corrupt header */
memcpy(hdr+HEADSHA_OFF, orig_hash, SHA1_SZ);
data_len = ALIGN_UP(*(uint32_t*)(hdr+DATALEN_OFF), 0x10);
if (data_len > ALIGN_UP(entry->len, 0x10))
return -2; /* bad size */
if (data)
{
bootflash_init(SPI_PORT);
bootflash_read(SPI_PORT, entry->devOffset+FILEHDR_SZ, data_len, data);
bootflash_close(SPI_PORT);
hwkeyaes(HWKEYAES_DECRYPT, *(uint8_t*)(hdr+KEYIDX_OFF), data, data_len);
sha1(data, data_len, calc_hash);
if (memcmp(calc_hash, hdr+DATASHA_OFF, SHA1_SZ))
return -3; /* corrupt data */
}
return 0;
}
|