OSDN Git Service

5a2817128a0b34b832f0b47b86c48efb9db24600
[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 128
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   unsigned fmcn = 50;
57   char *nxj_home;
58
59   if (argc == 1)
60     {
61       nxj_home = getenv("NXJ_HOME");
62       
63       if (nxj_home == NULL || strlen(nxj_home) == 0)
64         {
65           printf("NXJ_HOME is not defined\n");
66           exit(1);
67         }
68       printf("NXJ_HOME is %s\n", nxj_home);
69       
70       fw_file = (char *) calloc(1,256);
71       strcpy(fw_file, nxj_home);
72       strcat(fw_file,"/bin/lejos_nxt_rom.bin");
73       menu_file = calloc(1,256);
74       strcpy(menu_file,nxj_home);
75       strcat(menu_file,"/bin/StartUpText.bin");
76     }
77   else if (argc < 3 || argc > 4)
78     {
79       printf("Syntax: %s <VM binary> <java menu binary> [fmcn]\n"
80              "\n"
81              "Example: %s lejos_nxt_rom.bin Menu.bin\n", argv[0], argv[0]);
82       exit(1);
83     }
84   else
85     {
86       fw_file = argv[1];     
87       menu_file = argv[2];
88     }
89     
90   if (argc == 4) 
91     {
92           fmcn = atoi(argv[3]);
93     }
94
95   printf("Setting fmcn to %d\n", fmcn);
96   
97   printf("Checking VM %s ... ", fw_file);
98   NXT_HANDLE_ERR(nxt_firmware_validate(fw_file, MAX_VM_PAGES * 256), NULL,
99                  "Error in VM file");
100   printf("VM OK.\n");
101  
102   printf("Checking Menu %s ... ", menu_file);
103   NXT_HANDLE_ERR(nxt_firmware_validate(menu_file, (MAX_MENU_PAGES * 256) - 4), NULL,
104                  "Error in Menu file");
105   printf("Menu OK.\n");
106
107   NXT_HANDLE_ERR(nxt_init(&nxt), NULL,
108                  "Error during library initialization");
109
110   err = nxt_find(nxt);
111   if (err)
112     {
113       if (err == NXT_NOT_PRESENT)
114         printf("NXT not found. Is it properly plugged in via USB?\n");
115       else
116         NXT_HANDLE_ERR(0, NULL, "Error while scanning for NXT");
117       exit(1);
118     }
119
120   if (!nxt_in_reset_mode(nxt))
121     {
122       printf("NXT found, but not running in reset mode.\n");
123       printf("Please reset your NXT manually and restart this program.\n");
124       exit(2);
125     }
126
127   NXT_HANDLE_ERR(nxt_open(nxt), NULL, "Error while connecting to NXT");
128
129   printf("NXT device in reset mode located and opened.\n"
130          "Starting VM flash procedure now...\n");
131
132   NXT_HANDLE_ERR(nxt_firmware_flash(nxt, fw_file, 0, MAX_VM_PAGES, 1, 0), nxt,
133                  "Error flashing VM");
134   printf("VM flash complete.\n");
135
136   printf("Starting menu flash procedure now...\n");
137   
138   NXT_HANDLE_ERR(nxt_firmware_flash(nxt, menu_file, MAX_VM_PAGES, MAX_MENU_PAGES, 0, fmcn), nxt,
139                  "Error flashing menu");
140   printf("Menu flash complete.\n");
141   
142   NXT_HANDLE_ERR(nxt_jump(nxt, 0x00100000), nxt,
143                  "Error booting new firmware");
144   printf("New firmware started!\n");
145
146   NXT_HANDLE_ERR(nxt_close(nxt), NULL,
147                  "Error while closing connection to NXT");
148   return 0;
149 }