2 * Main program code for the nxjflash utility.
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
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
33 #define MAX_VM_PAGES 128
34 #define MAX_MENU_PAGES 128
36 #define NXT_HANDLE_ERR(expr, nxt, msg) \
38 nxt_error_t nxt__err_temp = (expr); \
40 return handle_error(nxt, msg, nxt__err_temp); \
43 static int handle_error(nxt_t *nxt, char *msg, nxt_error_t err)
45 printf("%s: %s\n", msg, nxt_str_error(err));
51 int main(int argc, char *argv[])
55 char *fw_file, *menu_file;
61 nxj_home = getenv("NXJ_HOME");
63 if (nxj_home == NULL || strlen(nxj_home) == 0)
65 printf("NXJ_HOME is not defined\n");
68 printf("NXJ_HOME is %s\n", nxj_home);
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");
77 else if (argc < 3 || argc > 4)
79 printf("Syntax: %s <VM binary> <java menu binary> [fmcn]\n"
81 "Example: %s lejos_nxt_rom.bin Menu.bin\n", argv[0], argv[0]);
95 printf("Setting fmcn to %d\n", fmcn);
97 printf("Checking VM %s ... ", fw_file);
98 NXT_HANDLE_ERR(nxt_firmware_validate(fw_file, MAX_VM_PAGES * 256), NULL,
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");
107 NXT_HANDLE_ERR(nxt_init(&nxt), NULL,
108 "Error during library initialization");
113 if (err == NXT_NOT_PRESENT)
114 printf("NXT not found. Is it properly plugged in via USB?\n");
116 NXT_HANDLE_ERR(0, NULL, "Error while scanning for NXT");
120 if (!nxt_in_reset_mode(nxt))
122 printf("NXT found, but not running in reset mode.\n");
123 printf("Please reset your NXT manually and restart this program.\n");
127 NXT_HANDLE_ERR(nxt_open(nxt), NULL, "Error while connecting to NXT");
129 printf("NXT device in reset mode located and opened.\n"
130 "Starting VM flash procedure now...\n");
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");
136 printf("Starting menu flash procedure now...\n");
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");
142 NXT_HANDLE_ERR(nxt_jump(nxt, 0x00100000), nxt,
143 "Error booting new firmware");
144 printf("New firmware started!\n");
146 NXT_HANDLE_ERR(nxt_close(nxt), NULL,
147 "Error while closing connection to NXT");