openocd.c 7.78 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
5
 *   Copyright (C) 2007,2008 Øyvind Harboe                                 *
6
7
 *   oyvind.harboe@zylin.com                                               *
 *                                                                         *
8
9
10
 *   Copyright (C) 2008 Richard Missenden                                  *
 *   richard.missenden@googlemail.com                                      *
 *                                                                         *
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

31
#include "openocd.h"
32
33
34
#include "jtag.h"
#include "configuration.h"
#include "xsvf.h"
35
#include "svf.h"
36
37
#include "nand.h"
#include "pld.h"
38
#include "mflash.h"
39
40
41

#include "server.h"
#include "gdb_server.h"
42
#include "httpd.h"
43

44
#ifdef HAVE_STRINGS_H
45
#include <strings.h>
46
#endif
47

48
49

#define OPENOCD_VERSION \
50
		"Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
51

52
/* Give TELNET a way to find out what version this is */
53
COMMAND_HANDLER(handle_version_command)
54
{
55
	if (CMD_ARGC != 0)
oharboe's avatar
oharboe committed
56
57
		return ERROR_COMMAND_SYNTAX_ERROR;

58
	command_print(CMD_CTX, OPENOCD_VERSION);
59
60
61
62

	return ERROR_OK;
}

63
static void exit_handler(void)
64
{
65
	jtag_interface_quit();
66
67
}

Zachary T Welch's avatar
Zachary T Welch committed
68
static int log_target_callback_event_handler(struct target *target, enum target_event event, void *priv)
69
70
71
{
	switch (event)
	{
72
		case TARGET_EVENT_GDB_START:
zwelch's avatar
zwelch committed
73
			target->display = 0;
74
75
			break;
		case TARGET_EVENT_GDB_END:
zwelch's avatar
zwelch committed
76
			target->display = 1;
77
			break;
78
		case TARGET_EVENT_HALTED:
79
80
81
82
83
			if (target->display)
			{
				/* do not display information when debugger caused the halt */
				target_arch_state(target);
			}
84
85
86
87
88
89
90
91
			break;
		default:
			break;
	}

	return ERROR_OK;
}

92
int ioutil_init(struct command_context *cmd_ctx);
93

Zachary T Welch's avatar
Zachary T Welch committed
94
95
96
97
98
99
100
101
102
103
static bool init_at_startup = true;

COMMAND_HANDLER(handle_noinit_command)
{
	if (CMD_ARGC != 0)
		return ERROR_COMMAND_SYNTAX_ERROR;
	init_at_startup = false;
	return ERROR_OK;
}

104
/* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
105
COMMAND_HANDLER(handle_init_command)
106
{
oharboe's avatar
oharboe committed
107

108
	if (CMD_ARGC != 0)
oharboe's avatar
oharboe committed
109
110
		return ERROR_COMMAND_SYNTAX_ERROR;

111
	int retval;
zwelch's avatar
zwelch committed
112
	static int initialized = 0;
113
114
	if (initialized)
		return ERROR_OK;
115

zwelch's avatar
zwelch committed
116
	initialized = 1;
117

118
	atexit(exit_handler);
119

120
121
	command_context_mode(CMD_CTX, COMMAND_EXEC);

122
	if (target_init(CMD_CTX) != ERROR_OK)
123
124
125
		return ERROR_FAIL;
	LOG_DEBUG("target init complete");

126
	if ((retval = jtag_interface_init(CMD_CTX)) != ERROR_OK)
127
128
129
130
131
132
133
	{
		/* we must be able to set up the jtag interface */
		return retval;
	}
	LOG_DEBUG("jtag interface init complete");

	/* Try to initialize & examine the JTAG chain at this point, but
134
	 * continue startup regardless */
135
	if (jtag_init(CMD_CTX) == ERROR_OK)
136
137
	{
		LOG_DEBUG("jtag init complete");
ntfreak's avatar
ntfreak committed
138
		if (target_examine() == ERROR_OK)
139
140
141
142
		{
			LOG_DEBUG("jtag examine complete");
		}
	}
143

144
	if (flash_init_drivers(CMD_CTX) != ERROR_OK)
145
146
147
		return ERROR_FAIL;
	LOG_DEBUG("flash init complete");

148
	if (mflash_init_drivers(CMD_CTX) != ERROR_OK)
149
150
151
		return ERROR_FAIL;
	LOG_DEBUG("mflash init complete");

152
	if (nand_init(CMD_CTX) != ERROR_OK)
153
154
155
		return ERROR_FAIL;
	LOG_DEBUG("NAND init complete");

156
	if (pld_init(CMD_CTX) != ERROR_OK)
157
158
159
160
		return ERROR_FAIL;
	LOG_DEBUG("pld init complete");

	/* initialize telnet subsystem */
Zachary T Welch's avatar
Zachary T Welch committed
161
	gdb_target_add_all(all_targets);
162

163
	target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
164

165
166
167
	return ERROR_OK;
}

168
169
170
171
static const struct command_registration openocd_command_handlers[] = {
	{
		.name = "version",
		.handler = &handle_version_command,
Zachary T Welch's avatar
Zachary T Welch committed
172
		.mode = COMMAND_ANY,
173
174
		.help = "show program version",
	},
Zachary T Welch's avatar
Zachary T Welch committed
175
176
177
178
179
180
	{
		.name = "noinit",
		.handler = &handle_noinit_command,
		.mode = COMMAND_CONFIG,
		.help = "Prevent 'init' from being called at startup.",
	},
181
182
183
	{
		.name = "init",
		.handler = &handle_init_command,
Zachary T Welch's avatar
Zachary T Welch committed
184
		.mode = COMMAND_CONFIG,
185
		.help = "Initializes configured targets and servers.  "
Zachary T Welch's avatar
Zachary T Welch committed
186
187
188
189
			"Changes command mode from CONFIG to EXEC.  "
			"Unless 'noinit' is called, this command is "
			"called automatically at the end of startup.",

190
191
192
193
	},
	COMMAND_REGISTRATION_DONE
};

194
struct command_context *global_cmd_ctx;
195

oharboe's avatar
oharboe committed
196
/* NB! this fn can be invoked outside this file for non PC hosted builds */
197
struct command_context *setup_command_handler(void)
oharboe's avatar
oharboe committed
198
{
Zachary T Welch's avatar
Zachary T Welch committed
199
200
201
	log_init();
	LOG_DEBUG("log_init: complete");

202
	struct command_context *cmd_ctx;
203

204
	global_cmd_ctx = cmd_ctx = command_init(openocd_startup_tcl);
205

206
	register_commands(cmd_ctx, NULL, openocd_command_handlers);
207
208
209
210
211
212
	/* register subsystem commands */
	server_register_commands(cmd_ctx);
	gdb_register_commands(cmd_ctx);
	log_register_commands(cmd_ctx);
	jtag_register_commands(cmd_ctx);
	xsvf_register_commands(cmd_ctx);
213
	svf_register_commands(cmd_ctx);
214
215
216
217
	target_register_commands(cmd_ctx);
	flash_register_commands(cmd_ctx);
	nand_register_commands(cmd_ctx);
	pld_register_commands(cmd_ctx);
218
219
	mflash_register_commands(cmd_ctx);

Zachary T Welch's avatar
Zachary T Welch committed
220
	LOG_DEBUG("command registration: complete");
oharboe's avatar
oharboe committed
221

222
	LOG_OUTPUT(OPENOCD_VERSION "\n");
223

oharboe's avatar
oharboe committed
224
225
226
	return cmd_ctx;
}

oharboe's avatar
oharboe committed
227
#if !BUILD_HTTPD && !BUILD_ECOSBOARD
228
229
230
231
232
233
234
235
236
237
238
239
240
/* implementations of OpenOCD that uses multithreading needs to know when
 * OpenOCD is sleeping. No-op in vanilla OpenOCD
 */
void openocd_sleep_prelude(void)
{
}

void openocd_sleep_postlude(void)
{
}
#endif


241
242
243
/* normally this is the main() function entry, but if OpenOCD is linked
 * into application, then this fn will not be invoked, but rather that
 * application will have it's own implementation of main(). */
oharboe's avatar
oharboe committed
244
245
int openocd_main(int argc, char *argv[])
{
246
247
	int ret;

oharboe's avatar
oharboe committed
248
	/* initialize commandline interface */
249
	struct command_context *cmd_ctx;
250
251

	cmd_ctx = setup_command_handler();
252

253
254
255
256
257
258
259
#if BUILD_IOUTIL
	if (ioutil_init(cmd_ctx) != ERROR_OK)
	{
		return EXIT_FAILURE;
	}
#endif

260
261
262
	LOG_OUTPUT("For bug reports, read\n\t"
		"http://openocd.berlios.de/doc/doxygen/bugs.html"
		"\n");
263
264


265
266
	command_context_mode(cmd_ctx, COMMAND_CONFIG);
	command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
267

268
	if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
269
		return EXIT_FAILURE;
270

271
	ret = parse_config_file(cmd_ctx);
272
	if (ret != ERROR_OK)
273
274
		return EXIT_FAILURE;

275
#if BUILD_HTTPD
276
	if (httpd_start(cmd_ctx) != ERROR_OK)
277
278
279
		return EXIT_FAILURE;
#endif

280
	ret = server_init(cmd_ctx);
281
282
283
	if (ERROR_OK != ret)
		return EXIT_FAILURE;

Zachary T Welch's avatar
Zachary T Welch committed
284
	if (init_at_startup)
285
	{
286
287
288
289
		ret = command_run_line(cmd_ctx, "init");
		if (ERROR_OK != ret)
			ret = EXIT_FAILURE;
	}
290

291
292
	/* handle network connections */
	if (ERROR_OK == ret)
293
		server_loop(cmd_ctx);
294
295
296

	server_quit();

297
298
299
300
#if BUILD_HTTPD
	httpd_stop();
#endif

301
	unregister_all_commands(cmd_ctx, NULL);
302

303
304
305
	/* free commandline interface */
	command_done(cmd_ctx);

306
	return ret;
307
}