OSDN Git Service

Remove as sg.exe is not a open source product.
[nxt-jsp/etrobo-atk.git] / nxtOSEK / lejos_nxj / src / libnxt / firmware.c
1 /**
2  * NXT bootstrap interface; NXT firmware handling code.
3  *
4  * Copyright 2006 David Anderson <david.anderson@calixo.net>
5  *
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.
10  *
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.
15  *
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
19  * USA
20  */
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "error.h"
31 #include "lowlevel.h"
32 #include "samba.h"
33 #include "flash.h"
34 #include "firmware.h"
35 #include "flash_routine.h"
36
37 static nxt_error_t
38 nxt_flash_prepare(nxt_t *nxt)
39 {
40   // Put the clock in PLL/2 mode
41   NXT_ERR(nxt_write_word(nxt, 0xFFFFFC30, 0x7));
42
43   // Unlock the flash chip
44   NXT_ERR(nxt_flash_unlock_all_regions(nxt));
45
46   // Send the flash writing routine
47   NXT_ERR(nxt_send_file(nxt, 0x202000, flash_bin, flash_len));
48
49   return NXT_OK;
50 }
51
52
53 static nxt_error_t
54 nxt_flash_block(nxt_t *nxt, nxt_word_t block_num, char *buf)
55 {
56   // Set the target block number
57   NXT_ERR(nxt_write_word(nxt, 0x202300, block_num));
58
59   // Send the block to flash
60   NXT_ERR(nxt_send_file(nxt, 0x202100, buf, 256));
61
62   // Jump into the flash writing routine
63   NXT_ERR(nxt_jump(nxt, 0x202000));
64
65   return NXT_OK;
66 }
67
68
69 static nxt_error_t
70 nxt_flash_finish(nxt_t *nxt)
71 {
72   return nxt_flash_wait_ready(nxt);
73 }
74
75
76 static nxt_error_t
77 nxt_firmware_validate_fd(int fd)
78 {
79   struct stat s;
80
81   if (fstat(fd, &s) < 0)
82     return NXT_FILE_ERROR;
83
84   if (s.st_size > 256*1024)
85     return NXT_INVALID_FIRMWARE;
86
87   return NXT_OK;
88 }
89
90
91 nxt_error_t
92 nxt_firmware_validate(char *fw_path)
93 {
94   nxt_error_t err;
95   int fd;
96
97   fd = open(fw_path, O_RDONLY);
98   if (fd < 0)
99     return NXT_FILE_ERROR;
100
101   err = nxt_firmware_validate_fd(fd);
102   close(fd);
103
104   return err;
105 }
106
107
108 nxt_error_t
109 nxt_firmware_flash(nxt_t *nxt, char *fw_path)
110 {
111   int fd, i, err;
112
113   fd = open(fw_path, O_RDONLY);
114   if (fd < 0)
115     return NXT_FILE_ERROR;
116
117   err = nxt_firmware_validate_fd(fd);
118   if (err != NXT_OK)
119     {
120       close(fd);
121       return NXT_INVALID_FIRMWARE;
122     }
123
124   NXT_ERR(nxt_flash_prepare(nxt));
125
126   for (i = 0; i < 1024; i++) //256*1024; i += 256)
127     {
128       char buf[256];
129       int ret;
130
131       memset(buf, 0, 256);
132       ret = read(fd, buf, 256);
133
134       if (ret != -1)
135         NXT_ERR(nxt_flash_block(nxt, i, buf));
136
137       if (ret < 256)
138         {
139           close(fd);
140           NXT_ERR(nxt_flash_finish(nxt));
141
142           return ret == -1 ? NXT_FILE_ERROR : NXT_OK;
143         }
144
145       NXT_ERR(nxt_flash_block(nxt, i, buf));
146     }
147
148   close(fd);
149   NXT_ERR(nxt_flash_finish(nxt));
150
151   return NXT_OK;
152 }