-
Notifications
You must be signed in to change notification settings - Fork 1
/
firmware.c
144 lines (113 loc) · 2.97 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
/**
* NXT bootstrap interface; NXT firmware handling code.
*
* Copyright 2006 David Anderson <[email protected]>
*
* 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 program 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "firmware.h"
#include "error.h"
#include "flash.h"
#include "flash_routine.h"
#include "lowlevel.h"
#include "samba.h"
static nxt_error_t
nxt_flash_prepare(nxt_t *nxt)
{
// Put the clock in PLL/2 mode
NXT_ERR(nxt_write_word(nxt, 0xFFFFFC30, 0x7));
// Unlock the flash chip
NXT_ERR(nxt_flash_unlock_all_regions(nxt));
// Send the flash writing routine
NXT_ERR(nxt_send_file(nxt, 0x202000, flash_bin, flash_len));
return NXT_OK;
}
static nxt_error_t
nxt_flash_block(nxt_t *nxt, nxt_word_t block_num, const uint8_t *buf)
{
// Set the target block number
NXT_ERR(nxt_write_word(nxt, 0x202300, block_num));
// Send the block to flash
NXT_ERR(nxt_send_file(nxt, 0x202100, buf, 256));
// Jump into the flash writing routine
NXT_ERR(nxt_jump(nxt, 0x202000));
return NXT_OK;
}
static nxt_error_t
nxt_flash_finish(nxt_t *nxt)
{
return nxt_flash_wait_ready(nxt);
}
static nxt_error_t
nxt_firmware_validate_fd(int fd)
{
struct stat s;
if (fstat(fd, &s) < 0)
return NXT_FILE_ERROR;
if (s.st_size > 256 * 1024)
return NXT_INVALID_FIRMWARE;
return NXT_OK;
}
nxt_error_t
nxt_firmware_validate(const char *fw_path)
{
nxt_error_t err;
int fd;
fd = open(fw_path, O_RDONLY);
if (fd < 0)
return NXT_FILE_ERROR;
err = nxt_firmware_validate_fd(fd);
close(fd);
return err;
}
nxt_error_t
nxt_firmware_flash(nxt_t *nxt, const char *fw_path)
{
int fd, i, err;
fd = open(fw_path, O_RDONLY);
if (fd < 0)
return NXT_FILE_ERROR;
err = nxt_firmware_validate_fd(fd);
if (err != NXT_OK)
{
close(fd);
return NXT_INVALID_FIRMWARE;
}
NXT_ERR(nxt_flash_prepare(nxt));
for (i = 0; i < 1024; i++)
{
uint8_t buf[256];
int ret;
memset(buf, 0, 256);
ret = read(fd, buf, 256);
if (ret != -1)
NXT_ERR(nxt_flash_block(nxt, i, buf));
if (ret < 256)
{
close(fd);
NXT_ERR(nxt_flash_finish(nxt));
return ret == -1 ? NXT_FILE_ERROR : NXT_OK;
}
}
close(fd);
NXT_ERR(nxt_flash_finish(nxt));
return NXT_OK;
}