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 192
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;
60 nxj_home = getenv("NXJ_HOME");
62 if (nxj_home == NULL || strlen(nxj_home) == 0)
64 printf("NXJ_HOME is not defined\n");
67 printf("NXJ_HOME is %s\n", nxj_home);
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");
78 printf("Syntax: %s [<VM binary> <java menu binary>]\n"
80 "Example: %s lejos_nxt_rom.bin StartUpText.bin\n", argv[0], argv[0]);
89 printf("Checking VM %s ... ", fw_file);
90 NXT_HANDLE_ERR(nxt_firmware_validate(fw_file, MAX_VM_PAGES ), NULL,
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");
99 NXT_HANDLE_ERR(nxt_init(&nxt), NULL,
100 "Error during library initialization");
105 if (err == NXT_NOT_PRESENT)
106 printf("NXT not found. Is it properly plugged in via USB?\n");
108 NXT_HANDLE_ERR(0, NULL, "Error while scanning for NXT");
112 if (!nxt_in_reset_mode(nxt))
114 printf("NXT found, but not running in reset mode.\n");
115 printf("Please reset your NXT manually and restart this program.\n");
119 NXT_HANDLE_ERR(nxt_open(nxt), NULL, "Error while connecting to NXT");
121 printf("NXT device in reset mode located and opened.\n"
122 "Starting VM flash procedure now...\n");
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");
128 printf("Starting menu flash procedure now...\n");
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");
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");
139 NXT_HANDLE_ERR(nxt_close(nxt), NULL,
140 "Error while closing connection to NXT");