openocd.c 9.2 KB
Newer Older
1
2
3
4
/***************************************************************************
 *   Copyright (C) 2005 by Dominic Rath                                    *
 *   Dominic.Rath@gmx.de                                                   *
 *                                                                         *
5
 *   Copyright (C) 2007-2010 Ø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
#include <jtag/driver.h>
33
#include <jtag/jtag.h>
34
#include <transport/transport.h>
35
#include <helper/ioutil.h>
36
#include <helper/util.h>
37
#include <helper/configuration.h>
Zachary T Welch's avatar
Zachary T Welch committed
38
#include <flash/nor/core.h>
39
#include <flash/nand/core.h>
40
#include <pld/pld.h>
41
#include <flash/mflash.h>
42

43
#include <server/server.h>
44
#include <server/gdb_server.h>
45

46
#ifdef HAVE_STRINGS_H
47
#include <strings.h>
48
#endif
49

50
51

#define OPENOCD_VERSION \
52
		"Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
53

54
55
56
/* Give scripts and TELNET a way to find out what version this is */
static int jim_version_command(Jim_Interp *interp, int argc,
		Jim_Obj * const *argv)
57
{
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	if (argc > 2)
	{
		return JIM_ERR;
	}
	const char *str = "";
	char * version_str;
	version_str = OPENOCD_VERSION;
	
	if (argc == 2)
		str = Jim_GetString(argv[1], NULL);

	if (strcmp("git", str) == 0)
	{
		version_str = GITVERSION;
	} 
	
	Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
75

76
	return JIM_OK;
77
78
}

Zachary T Welch's avatar
Zachary T Welch committed
79
static int log_target_callback_event_handler(struct target *target, enum target_event event, void *priv)
80
81
82
{
	switch (event)
	{
83
		case TARGET_EVENT_GDB_START:
zwelch's avatar
zwelch committed
84
			target->display = 0;
85
86
			break;
		case TARGET_EVENT_GDB_END:
zwelch's avatar
zwelch committed
87
			target->display = 1;
88
			break;
89
		case TARGET_EVENT_HALTED:
90
91
92
93
94
			if (target->display)
			{
				/* do not display information when debugger caused the halt */
				target_arch_state(target);
			}
95
96
97
98
99
100
101
102
			break;
		default:
			break;
	}

	return ERROR_OK;
}

Zachary T Welch's avatar
Zachary T Welch committed
103
104
105
106
107
108
109
110
111
112
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;
}

113
/* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
114
COMMAND_HANDLER(handle_init_command)
115
{
oharboe's avatar
oharboe committed
116

117
	if (CMD_ARGC != 0)
oharboe's avatar
oharboe committed
118
119
		return ERROR_COMMAND_SYNTAX_ERROR;

120
	int retval;
zwelch's avatar
zwelch committed
121
	static int initialized = 0;
122
123
	if (initialized)
		return ERROR_OK;
124

zwelch's avatar
zwelch committed
125
	initialized = 1;
126

127
128
	retval = command_run_line(CMD_CTX, "target init");
	if (ERROR_OK != retval)
129
130
		return ERROR_FAIL;

131
	if ((retval = adapter_init(CMD_CTX)) != ERROR_OK)
132
	{
133
		/* we must be able to set up the debug adapter */
134
135
		return retval;
	}
David Brownell's avatar
David Brownell committed
136

137
	LOG_DEBUG("Debug Adapter init complete");
138

David Brownell's avatar
David Brownell committed
139
140
141
142
	/* "transport init" verifies the expected devices are present;
	 * for JTAG, it checks the list of configured TAPs against
	 * what's discoverable, possibly with help from the platform's
	 * JTAG event handlers.  (which require COMMAND_EXEC)
David Brownell's avatar
David Brownell committed
143
144
	 */
	command_context_mode(CMD_CTX, COMMAND_EXEC);
David Brownell's avatar
David Brownell committed
145
146
147
148
149
150
151
152
153

	retval = command_run_line(CMD_CTX, "transport init");
	if (ERROR_OK != retval)
		return ERROR_FAIL;

	LOG_DEBUG("Examining targets...");
	if (target_examine() != ERROR_OK)
		LOG_DEBUG("target examination failed");

David Brownell's avatar
David Brownell committed
154
	command_context_mode(CMD_CTX, COMMAND_CONFIG);
155

156
	if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
157
158
		return ERROR_FAIL;

159
	if (command_run_line(CMD_CTX, "mflash init") != ERROR_OK)
160
161
		return ERROR_FAIL;

162
	if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
163
164
		return ERROR_FAIL;

165
	if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
166
		return ERROR_FAIL;
167
	command_context_mode(CMD_CTX, COMMAND_EXEC);
168
169

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

172
	target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
173

174
175
176
	return ERROR_OK;
}

177
178
179
180
181
182
183
184
185
186
COMMAND_HANDLER(handle_add_script_search_dir_command)
{
	if (CMD_ARGC != 1)
		return ERROR_COMMAND_SYNTAX_ERROR;

	add_script_search_dir(CMD_ARGV[0]);

	return ERROR_OK;
}

187
188
189
static const struct command_registration openocd_command_handlers[] = {
	{
		.name = "version",
190
		.jim_handler = jim_version_command,
Zachary T Welch's avatar
Zachary T Welch committed
191
		.mode = COMMAND_ANY,
192
193
		.help = "show program version",
	},
Zachary T Welch's avatar
Zachary T Welch committed
194
195
196
197
198
199
	{
		.name = "noinit",
		.handler = &handle_noinit_command,
		.mode = COMMAND_CONFIG,
		.help = "Prevent 'init' from being called at startup.",
	},
200
201
202
	{
		.name = "init",
		.handler = &handle_init_command,
Zachary T Welch's avatar
Zachary T Welch committed
203
		.mode = COMMAND_ANY,
204
		.help = "Initializes configured targets and servers.  "
Zachary T Welch's avatar
Zachary T Welch committed
205
206
207
208
			"Changes command mode from CONFIG to EXEC.  "
			"Unless 'noinit' is called, this command is "
			"called automatically at the end of startup.",

209
210
211
212
213
214
215
	},
	{
		.name = "add_script_search_dir",
		.handler = &handle_add_script_search_dir_command,
		.mode = COMMAND_ANY,
		.help = "dir to search for config files and scripts",

216
217
218
219
	},
	COMMAND_REGISTRATION_DONE
};

220
static int openocd_register_commands(struct command_context *cmd_ctx)
221
222
223
224
{
	return register_commands(cmd_ctx, NULL, openocd_command_handlers);
}

225
struct command_context *global_cmd_ctx;
226

227
228
229
230
/* NB! this fn can be invoked outside this file for non PC hosted builds
 * NB! do not change to 'static'!!!!
 */
struct command_context *setup_command_handler(Jim_Interp *interp)
oharboe's avatar
oharboe committed
231
{
Zachary T Welch's avatar
Zachary T Welch committed
232
233
234
	log_init();
	LOG_DEBUG("log_init: complete");

235
236
	const char *startup = openocd_startup_tcl;
	struct command_context *cmd_ctx = command_init(startup, interp);
237

238
	/* register subsystem commands */
239
	typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
240
	static const command_registrant_t command_registrants[] = {
241
242
243
244
		&openocd_register_commands,
		&server_register_commands,
		&gdb_register_commands,
		&log_register_commands,
David Brownell's avatar
David Brownell committed
245
		&transport_register_commands,
246
		&interface_register_commands,
247
248
249
250
251
		&target_register_commands,
		&flash_register_commands,
		&nand_register_commands,
		&pld_register_commands,
		&mflash_register_commands,
252
		NULL
253
254
255
256
257
258
259
260
261
262
	};
	for (unsigned i = 0; NULL != command_registrants[i]; i++)
	{
		int retval = (*command_registrants[i])(cmd_ctx);
		if (ERROR_OK != retval)
		{
			command_done(cmd_ctx);
			return NULL;
		}
	}
Zachary T Welch's avatar
Zachary T Welch committed
263
	LOG_DEBUG("command registration: complete");
oharboe's avatar
oharboe committed
264

Øyvind Harboe's avatar
Øyvind Harboe committed
265
266
	LOG_OUTPUT(OPENOCD_VERSION "\n"
			"Licensed under GNU GPL v2\n");
267

268
269
	global_cmd_ctx = cmd_ctx;

oharboe's avatar
oharboe committed
270
271
272
	return cmd_ctx;
}

273
274
275
276
277
/** OpenOCD runtime meat that can become single-thread in future. It parse
 * commandline, reads configuration, sets up the target and starts server loop.
 * Commandline arguments are passed into this function from openocd_main().
 */
static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
oharboe's avatar
oharboe committed
278
{
279
280
	int ret;

281
	if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
282
		return EXIT_FAILURE;
283

284
285
286
	if (server_preinit() != ERROR_OK)
		return EXIT_FAILURE;

287
	ret = parse_config_file(cmd_ctx);
288
	if (ret != ERROR_OK)
289
290
		return EXIT_FAILURE;

291
	ret = server_init(cmd_ctx);
292
293
294
	if (ERROR_OK != ret)
		return EXIT_FAILURE;

Zachary T Welch's avatar
Zachary T Welch committed
295
	if (init_at_startup)
296
	{
297
298
		ret = command_run_line(cmd_ctx, "init");
		if (ERROR_OK != ret)
299
			return EXIT_FAILURE;
300
	}
301

302
	server_loop(cmd_ctx);
303
304
305

	server_quit();

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
	return ret;
}

/* 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(). */
int openocd_main(int argc, char *argv[])
{
	int ret;

	/* initialize commandline interface */
	struct command_context *cmd_ctx;

	cmd_ctx = setup_command_handler(NULL);

	if (util_init(cmd_ctx) != ERROR_OK)
		return EXIT_FAILURE;

	if (ioutil_init(cmd_ctx) != ERROR_OK)
		return EXIT_FAILURE;

	LOG_OUTPUT("For bug reports, read\n\t"
328
		"http://openocd.sourceforge.net/doc/doxygen/bugs.html"
329
330
331
332
333
		"\n");

	command_context_mode(cmd_ctx, COMMAND_CONFIG);
	command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);

334
335
	/* Start the executable meat that can evolve into thread in future. */
	ret = openocd_thread(argc, argv, cmd_ctx);
336

337
	unregister_all_commands(cmd_ctx, NULL);
338

339
340
341
	/* free commandline interface */
	command_done(cmd_ctx);

342
	adapter_quit();
343

344
	return ret;
345
}