2 * NXT bootstrap interface; NXT firmware handling code.
4 * Copyright 2006 David Anderson <david.anderson@calixo.net>
5 * Modified 2007 by Lawrie Griffiths <lawrie.griffiths@ntlworld.com>
6 * to support lejos NXJ firmware flashing.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/types.h>
37 #include "flash_routine.h"
40 nxt_flash_prepare(nxt_t *nxt, int unlock)
42 // Put the clock in PLL/2 mode
43 NXT_ERR(nxt_write_word(nxt, 0xFFFFFC30, 0x7));
45 // Unlock the flash chip
46 if (unlock) NXT_ERR(nxt_flash_unlock_all_regions(nxt));
48 // Send the flash writing routine
49 NXT_ERR(nxt_send_file(nxt, 0x202000, flash_bin, flash_len));
56 nxt_flash_block(nxt_t *nxt, nxt_word_t block_num, char *buf)
58 // Set the target block number
59 NXT_ERR(nxt_write_word(nxt, 0x202300, block_num));
61 // Send the block to flash
62 NXT_ERR(nxt_send_file(nxt, 0x202100, buf, 256));
64 // Jump into the flash writing routine
65 NXT_ERR(nxt_jump(nxt, 0x202000));
72 nxt_flash_finish(nxt_t *nxt)
74 return nxt_flash_wait_ready(nxt);
79 nxt_firmware_validate_fd(int fd, int max_size)
83 if (fstat(fd, &s) < 0)
84 return NXT_FILE_ERROR;
86 if (s.st_size > max_size)
87 return NXT_INVALID_FIRMWARE;
94 nxt_firmware_validate(char *fw_path, int max_pages)
99 #if defined(_WIN32) || defined(__CYGWIN32__)
100 fd = open(fw_path, O_RDONLY | O_BINARY);
102 fd = open(fw_path, O_RDONLY);
105 return NXT_FILE_ERROR;
107 err = nxt_firmware_validate_fd(fd, max_pages * 256);
115 nxt_firmware_flash(nxt_t *nxt, char *fw_path,
116 int start_page, int max_pages, int unlock, int write_len)
118 int fd, i, err, len = 0;
121 #if defined(_WIN32) || defined(__CYGWIN32__)
122 fd = open(fw_path, O_RDONLY | O_BINARY);
124 fd = open(fw_path, O_RDONLY);
127 return NXT_FILE_ERROR;
129 err = nxt_firmware_validate_fd(fd, max_pages * 256);
133 return NXT_INVALID_FIRMWARE;
136 NXT_ERR(nxt_flash_prepare(nxt, unlock));
138 for (i = start_page; i < start_page + max_pages; i++)
144 ret = read(fd, buf, 256);
148 NXT_ERR(nxt_flash_block(nxt, i, buf));
156 if (ret != -1 && write_len)
158 ((unsigned *) buf)[63] = len;
159 NXT_ERR(nxt_flash_block(nxt,start_page + max_pages -1, buf));
162 NXT_ERR(nxt_flash_finish(nxt));
164 return ret == -1 ? NXT_FILE_ERROR : NXT_OK;
172 ((unsigned *) buf)[63] = len;
173 NXT_ERR(nxt_flash_block(nxt,start_page + max_pages -1, buf));
176 NXT_ERR(nxt_flash_finish(nxt));