2 * NXT bootstrap interface; NXT firmware handling code.
4 * Copyright 2006 David Anderson <david.anderson@calixo.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 #include <sys/types.h>
35 #include "flash_routine.h"
38 nxt_flash_prepare(nxt_t *nxt)
40 // Put the clock in PLL/2 mode
41 NXT_ERR(nxt_write_word(nxt, 0xFFFFFC30, 0x7));
43 // Unlock the flash chip
44 NXT_ERR(nxt_flash_unlock_all_regions(nxt));
46 // Send the flash writing routine
47 NXT_ERR(nxt_send_file(nxt, 0x202000, flash_bin, flash_len));
54 nxt_flash_block(nxt_t *nxt, nxt_word_t block_num, char *buf)
56 // Set the target block number
57 NXT_ERR(nxt_write_word(nxt, 0x202300, block_num));
59 // Send the block to flash
60 NXT_ERR(nxt_send_file(nxt, 0x202100, buf, 256));
62 // Jump into the flash writing routine
63 NXT_ERR(nxt_jump(nxt, 0x202000));
70 nxt_flash_finish(nxt_t *nxt)
72 return nxt_flash_wait_ready(nxt);
77 nxt_firmware_validate_fd(int fd)
81 if (fstat(fd, &s) < 0)
82 return NXT_FILE_ERROR;
84 if (s.st_size > 256*1024)
85 return NXT_INVALID_FIRMWARE;
92 nxt_firmware_validate(char *fw_path)
97 fd = open(fw_path, O_RDONLY);
99 return NXT_FILE_ERROR;
101 err = nxt_firmware_validate_fd(fd);
109 nxt_firmware_flash(nxt_t *nxt, char *fw_path)
113 fd = open(fw_path, O_RDONLY);
115 return NXT_FILE_ERROR;
117 err = nxt_firmware_validate_fd(fd);
121 return NXT_INVALID_FIRMWARE;
124 NXT_ERR(nxt_flash_prepare(nxt));
126 for (i = 0; i < 1024; i++) //256*1024; i += 256)
132 ret = read(fd, buf, 256);
135 NXT_ERR(nxt_flash_block(nxt, i, buf));
140 NXT_ERR(nxt_flash_finish(nxt));
142 return ret == -1 ? NXT_FILE_ERROR : NXT_OK;
145 NXT_ERR(nxt_flash_block(nxt, i, buf));
149 NXT_ERR(nxt_flash_finish(nxt));