bootdevice: add validate check for qemu_boot_set()
[qemu/cris-port.git] / qemu-io.c
blob91a445a106bd19ae4a542d6ccf38132de5760c68
1 /*
2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <getopt.h>
15 #include <libgen.h>
17 #include "qemu-io.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/option.h"
20 #include "qemu/config-file.h"
21 #include "qemu/readline.h"
22 #include "sysemu/block-backend.h"
23 #include "block/block_int.h"
24 #include "trace/control.h"
26 #define CMD_NOFILE_OK 0x01
28 static char *progname;
30 static BlockBackend *qemuio_blk;
31 static BlockDriverState *qemuio_bs;
33 /* qemu-io commands passed using -c */
34 static int ncmdline;
35 static char **cmdline;
37 static ReadLineState *readline_state;
39 static int close_f(BlockDriverState *bs, int argc, char **argv)
41 blk_unref(qemuio_blk);
42 qemuio_bs = NULL;
43 qemuio_blk = NULL;
44 return 0;
47 static const cmdinfo_t close_cmd = {
48 .name = "close",
49 .altname = "c",
50 .cfunc = close_f,
51 .oneline = "close the current open file",
54 static int openfile(char *name, BlockDriver *drv, int flags, int growable,
55 QDict *opts)
57 Error *local_err = NULL;
59 if (qemuio_bs) {
60 fprintf(stderr, "file open already, try 'help close'\n");
61 QDECREF(opts);
62 return 1;
65 qemuio_blk = blk_new_with_bs("hda", &error_abort);
66 qemuio_bs = blk_bs(qemuio_blk);
68 if (growable) {
69 flags |= BDRV_O_PROTOCOL;
72 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, drv, &local_err) < 0) {
73 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
74 name ? " device " : "", name ?: "",
75 error_get_pretty(local_err));
76 error_free(local_err);
77 blk_unref(qemuio_blk);
78 qemuio_bs = NULL;
79 qemuio_blk = NULL;
80 return 1;
83 return 0;
86 static void open_help(void)
88 printf(
89 "\n"
90 " opens a new file in the requested mode\n"
91 "\n"
92 " Example:\n"
93 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
94 "\n"
95 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
96 " -r, -- open file read-only\n"
97 " -s, -- use snapshot file\n"
98 " -n, -- disable host cache\n"
99 " -g, -- allow file to grow (only applies to protocols)\n"
100 " -o, -- options to be given to the block driver"
101 "\n");
104 static int open_f(BlockDriverState *bs, int argc, char **argv);
106 static const cmdinfo_t open_cmd = {
107 .name = "open",
108 .altname = "o",
109 .cfunc = open_f,
110 .argmin = 1,
111 .argmax = -1,
112 .flags = CMD_NOFILE_OK,
113 .args = "[-Crsn] [-o options] [path]",
114 .oneline = "open the file specified by path",
115 .help = open_help,
118 static QemuOptsList empty_opts = {
119 .name = "drive",
120 .merge_lists = true,
121 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
122 .desc = {
123 /* no elements => accept any params */
124 { /* end of list */ }
128 static int open_f(BlockDriverState *bs, int argc, char **argv)
130 int flags = 0;
131 int readonly = 0;
132 int growable = 0;
133 int c;
134 QemuOpts *qopts;
135 QDict *opts;
137 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
138 switch (c) {
139 case 's':
140 flags |= BDRV_O_SNAPSHOT;
141 break;
142 case 'n':
143 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
144 break;
145 case 'r':
146 readonly = 1;
147 break;
148 case 'g':
149 growable = 1;
150 break;
151 case 'o':
152 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
153 printf("could not parse option list -- %s\n", optarg);
154 qemu_opts_reset(&empty_opts);
155 return 0;
157 break;
158 default:
159 qemu_opts_reset(&empty_opts);
160 return qemuio_command_usage(&open_cmd);
164 if (!readonly) {
165 flags |= BDRV_O_RDWR;
168 qopts = qemu_opts_find(&empty_opts, NULL);
169 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
170 qemu_opts_reset(&empty_opts);
172 if (optind == argc - 1) {
173 return openfile(argv[optind], NULL, flags, growable, opts);
174 } else if (optind == argc) {
175 return openfile(NULL, NULL, flags, growable, opts);
176 } else {
177 QDECREF(opts);
178 return qemuio_command_usage(&open_cmd);
182 static int quit_f(BlockDriverState *bs, int argc, char **argv)
184 return 1;
187 static const cmdinfo_t quit_cmd = {
188 .name = "quit",
189 .altname = "q",
190 .cfunc = quit_f,
191 .argmin = -1,
192 .argmax = -1,
193 .flags = CMD_FLAG_GLOBAL,
194 .oneline = "exit the program",
197 static void usage(const char *name)
199 printf(
200 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
201 "QEMU Disk exerciser\n"
202 "\n"
203 " -c, --cmd STRING execute command with its arguments\n"
204 " from the given string\n"
205 " -f, --format FMT specifies the block driver to use\n"
206 " -r, --read-only export read-only\n"
207 " -s, --snapshot use snapshot file\n"
208 " -n, --nocache disable host cache\n"
209 " -g, --growable allow file to grow (only applies to protocols)\n"
210 " -m, --misalign misalign allocations for O_DIRECT\n"
211 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
212 " -t, --cache=MODE use the given cache mode for the image\n"
213 " -T, --trace FILE enable trace events listed in the given file\n"
214 " -h, --help display this help and exit\n"
215 " -V, --version output version information and exit\n"
216 "\n"
217 "See '%s -c help' for information on available commands."
218 "\n",
219 name, name);
222 static char *get_prompt(void)
224 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
226 if (!prompt[0]) {
227 snprintf(prompt, sizeof(prompt), "%s> ", progname);
230 return prompt;
233 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
234 const char *fmt, ...)
236 va_list ap;
237 va_start(ap, fmt);
238 vprintf(fmt, ap);
239 va_end(ap);
242 static void readline_flush_func(void *opaque)
244 fflush(stdout);
247 static void readline_func(void *opaque, const char *str, void *readline_opaque)
249 char **line = readline_opaque;
250 *line = g_strdup(str);
253 static void completion_match(const char *cmd, void *opaque)
255 readline_add_completion(readline_state, cmd);
258 static void readline_completion_func(void *opaque, const char *str)
260 readline_set_completion_index(readline_state, strlen(str));
261 qemuio_complete_command(str, completion_match, NULL);
264 static char *fetchline_readline(void)
266 char *line = NULL;
268 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
269 while (!line) {
270 int ch = getchar();
271 if (ch == EOF) {
272 break;
274 readline_handle_byte(readline_state, ch);
276 return line;
279 #define MAXREADLINESZ 1024
280 static char *fetchline_fgets(void)
282 char *p, *line = g_malloc(MAXREADLINESZ);
284 if (!fgets(line, MAXREADLINESZ, stdin)) {
285 g_free(line);
286 return NULL;
289 p = line + strlen(line);
290 if (p != line && p[-1] == '\n') {
291 p[-1] = '\0';
294 return line;
297 static char *fetchline(void)
299 if (readline_state) {
300 return fetchline_readline();
301 } else {
302 return fetchline_fgets();
306 static void prep_fetchline(void *opaque)
308 int *fetchable = opaque;
310 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
311 *fetchable= 1;
314 static void command_loop(void)
316 int i, done = 0, fetchable = 0, prompted = 0;
317 char *input;
319 for (i = 0; !done && i < ncmdline; i++) {
320 done = qemuio_command(qemuio_bs, cmdline[i]);
322 if (cmdline) {
323 g_free(cmdline);
324 return;
327 while (!done) {
328 if (!prompted) {
329 printf("%s", get_prompt());
330 fflush(stdout);
331 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
332 prompted = 1;
335 main_loop_wait(false);
337 if (!fetchable) {
338 continue;
341 input = fetchline();
342 if (input == NULL) {
343 break;
345 done = qemuio_command(qemuio_bs, input);
346 g_free(input);
348 prompted = 0;
349 fetchable = 0;
351 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
354 static void add_user_command(char *optarg)
356 cmdline = g_renew(char *, cmdline, ++ncmdline);
357 cmdline[ncmdline-1] = optarg;
360 static void reenable_tty_echo(void)
362 qemu_set_tty_echo(STDIN_FILENO, true);
365 int main(int argc, char **argv)
367 int readonly = 0;
368 int growable = 0;
369 const char *sopt = "hVc:d:f:rsnmgkt:T:";
370 const struct option lopt[] = {
371 { "help", 0, NULL, 'h' },
372 { "version", 0, NULL, 'V' },
373 { "offset", 1, NULL, 'o' },
374 { "cmd", 1, NULL, 'c' },
375 { "format", 1, NULL, 'f' },
376 { "read-only", 0, NULL, 'r' },
377 { "snapshot", 0, NULL, 's' },
378 { "nocache", 0, NULL, 'n' },
379 { "misalign", 0, NULL, 'm' },
380 { "growable", 0, NULL, 'g' },
381 { "native-aio", 0, NULL, 'k' },
382 { "discard", 1, NULL, 'd' },
383 { "cache", 1, NULL, 't' },
384 { "trace", 1, NULL, 'T' },
385 { NULL, 0, NULL, 0 }
387 int c;
388 int opt_index = 0;
389 int flags = BDRV_O_UNMAP;
390 BlockDriver *drv = NULL;
391 Error *local_error = NULL;
393 #ifdef CONFIG_POSIX
394 signal(SIGPIPE, SIG_IGN);
395 #endif
397 progname = basename(argv[0]);
398 qemu_init_exec_dir(argv[0]);
400 bdrv_init();
402 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
403 switch (c) {
404 case 's':
405 flags |= BDRV_O_SNAPSHOT;
406 break;
407 case 'n':
408 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
409 break;
410 case 'd':
411 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
412 error_report("Invalid discard option: %s", optarg);
413 exit(1);
415 break;
416 case 'f':
417 drv = bdrv_find_format(optarg);
418 if (!drv) {
419 error_report("Invalid format '%s'", optarg);
420 exit(EXIT_FAILURE);
422 break;
423 case 'c':
424 add_user_command(optarg);
425 break;
426 case 'r':
427 readonly = 1;
428 break;
429 case 'm':
430 qemuio_misalign = true;
431 break;
432 case 'g':
433 growable = 1;
434 break;
435 case 'k':
436 flags |= BDRV_O_NATIVE_AIO;
437 break;
438 case 't':
439 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
440 error_report("Invalid cache option: %s", optarg);
441 exit(1);
443 break;
444 case 'T':
445 if (!trace_init_backends(optarg, NULL)) {
446 exit(1); /* error message will have been printed */
448 break;
449 case 'V':
450 printf("%s version %s\n", progname, QEMU_VERSION);
451 exit(0);
452 case 'h':
453 usage(progname);
454 exit(0);
455 default:
456 usage(progname);
457 exit(1);
461 if ((argc - optind) > 1) {
462 usage(progname);
463 exit(1);
466 if (qemu_init_main_loop(&local_error)) {
467 error_report("%s", error_get_pretty(local_error));
468 error_free(local_error);
469 exit(1);
472 /* initialize commands */
473 qemuio_add_command(&quit_cmd);
474 qemuio_add_command(&open_cmd);
475 qemuio_add_command(&close_cmd);
477 if (isatty(STDIN_FILENO)) {
478 readline_state = readline_init(readline_printf_func,
479 readline_flush_func,
480 NULL,
481 readline_completion_func);
482 qemu_set_tty_echo(STDIN_FILENO, false);
483 atexit(reenable_tty_echo);
486 /* open the device */
487 if (!readonly) {
488 flags |= BDRV_O_RDWR;
491 if ((argc - optind) == 1) {
492 openfile(argv[optind], drv, flags, growable, NULL);
494 command_loop();
497 * Make sure all outstanding requests complete before the program exits.
499 bdrv_drain_all();
501 blk_unref(qemuio_blk);
502 g_free(readline_state);
503 return 0;