OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / libnxt / main_nxjflash.c
1 /**
2  * Main program code for the nxjflash utility.
3  *
4  * Copyright 2006 David Anderson <david.anderson@calixo.net>
5  * Modified 2007 by Lawrie Griffiths <lawrie.griffiths@ntlworld.com>
6  * to flash nxj firmware and Java menu
7  * 
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.
12  *
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.
17  *
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
21  * USA
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "error.h"
29 #include "lowlevel.h"
30 #include "samba.h"
31 #include "firmware.h"
32
33 #define MAX_VM_PAGES 128
34 #define MAX_MENU_PAGES 192
35
36 #define NXT_HANDLE_ERR(expr, nxt, msg)     \
37   do {                                     \
38     nxt_error_t nxt__err_temp = (expr);    \
39     if (nxt__err_temp)                     \
40       return handle_error(nxt, msg, nxt__err_temp);  \
41   } while(0)
42
43 static int handle_error(nxt_t *nxt, char *msg, nxt_error_t err)
44 {
45   printf("%s: %s\n", msg, nxt_str_error(err));
46   if (nxt != NULL)
47     nxt_close(nxt);
48   exit(err);
49 }
50
51 int main(int argc, char *argv[])
52 {
53   nxt_t *nxt;
54   nxt_error_t err;
55   char *fw_file, *menu_file;
56   char *nxj_home;
57
58   if (argc == 1)
59     {
60       nxj_home = getenv("NXJ_HOME");
61       
62       if (nxj_home == NULL || strlen(nxj_home) == 0)
63         {
64           printf("NXJ_HOME is not defined\n");
65           exit(1);
66         }
67       printf("NXJ_HOME is %s\n", nxj_home);
68       
69       fw_file = (char *) calloc(1,256);
70       strcpy(fw_file, nxj_home);
71       strcat(fw_file,"/bin/lejos_nxt_rom.bin");
72       menu_file = calloc(1,256);
73       strcpy(menu_file,nxj_home);
74       strcat(menu_file,"/bin/StartUpText.bin");
75     }
76   else if (argc != 3)
77     {
78       printf("Syntax: %s [<VM binary> <java menu binary>]\n"
79              "\n"
80              "Example: %s lejos_nxt_rom.bin StartUpText.bin\n", argv[0], argv[0]);
81       exit(1);
82     }
83   else
84     {
85       fw_file = argv[1];     
86       menu_file = argv[2];
87     }
88   
89   printf("Checking VM %s ... ", fw_file);
90   NXT_HANDLE_ERR(nxt_firmware_validate(fw_file, MAX_VM_PAGES ), NULL,
91                  "Error in VM file");
92   printf("VM OK.\n");
93  
94   printf("Checking Menu %s ... ", menu_file);
95   NXT_HANDLE_ERR(nxt_firmware_validate(menu_file, (MAX_MENU_PAGES ) - 4), NULL,
96                  "Error in Menu file");
97   printf("Menu OK.\n");
98
99   NXT_HANDLE_ERR(nxt_init(&nxt), NULL,
100                  "Error during library initialization");
101
102   err = nxt_find(nxt);
103   if (err)
104     {
105       if (err == NXT_NOT_PRESENT)
106         printf("NXT not found. Is it properly plugged in via USB?\n");
107       else
108         NXT_HANDLE_ERR(0, NULL, "Error while scanning for NXT");
109       exit(1);
110     }
111
112   if (!nxt_in_reset_mode(nxt))
113     {
114       printf("NXT found, but not running in reset mode.\n");
115       printf("Please reset your NXT manually and restart this program.\n");
116       exit(2);
117     }
118
119   NXT_HANDLE_ERR(nxt_open(nxt), NULL, "Error while connecting to NXT");
120
121   printf("NXT device in reset mode located and opened.\n"
122          "Starting VM flash procedure now...\n");
123
124   NXT_HANDLE_ERR(nxt_firmware_flash(nxt, fw_file, 0, MAX_VM_PAGES, 1, 0), nxt,
125                  "Error flashing VM");
126   printf("VM flash complete.\n");
127
128   printf("Starting menu flash procedure now...\n");
129   
130   NXT_HANDLE_ERR(nxt_firmware_flash(nxt, menu_file, MAX_VM_PAGES, MAX_MENU_PAGES, 0, 1), nxt,
131                  "Error flashing menu");
132   printf("Menu flash complete.\n");
133   
134   NXT_HANDLE_ERR(nxt_jump(nxt, 0x00100000), nxt,
135                  "Error booting new firmware");
136   printf("New firmware started!\n");
137   printf("If battery level reads 0.0, remove and re-insert a battery\n");
138
139   NXT_HANDLE_ERR(nxt_close(nxt), NULL,
140                  "Error while closing connection to NXT");
141   return 0;
142 }