Merge remote-tracking branch 'remotes/stsquad/tags/pull-gitdm-next-271019-1' into...
[qemu/ar7.git] / hw / semihosting / config.c
blob9807f10cb004ba9991b693365b617463678b47fb
1 /*
2 * Semihosting configuration
4 * Copyright (c) 2015 Imagination Technologies
5 * Copyright (c) 2019 Linaro Ltd
7 * This controls the configuration of semihosting for all guest
8 * targets that support it. Architecture specific handling is handled
9 * in target/HW/HW-semi.c
11 * Semihosting is sightly strange in that it is also supported by some
12 * linux-user targets. However in that use case no configuration of
13 * the outputs and command lines is supported.
15 * The config module is common to all softmmu targets however as vl.c
16 * needs to link against the helpers.
18 * SPDX-License-Identifier: GPL-2.0-or-later
21 #include "qemu/osdep.h"
22 #include "qemu/option.h"
23 #include "qemu/config-file.h"
24 #include "qemu/error-report.h"
25 #include "hw/semihosting/semihost.h"
26 #include "chardev/char.h"
27 #include "sysemu/sysemu.h"
29 QemuOptsList qemu_semihosting_config_opts = {
30 .name = "semihosting-config",
31 .implied_opt_name = "enable",
32 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
33 .desc = {
35 .name = "enable",
36 .type = QEMU_OPT_BOOL,
37 }, {
38 .name = "target",
39 .type = QEMU_OPT_STRING,
40 }, {
41 .name = "chardev",
42 .type = QEMU_OPT_STRING,
43 }, {
44 .name = "arg",
45 .type = QEMU_OPT_STRING,
47 { /* end of list */ }
51 typedef struct SemihostingConfig {
52 bool enabled;
53 SemihostingTarget target;
54 Chardev *chardev;
55 const char **argv;
56 int argc;
57 const char *cmdline; /* concatenated argv */
58 } SemihostingConfig;
60 static SemihostingConfig semihosting;
61 static const char *semihost_chardev;
63 bool semihosting_enabled(void)
65 return semihosting.enabled;
68 SemihostingTarget semihosting_get_target(void)
70 return semihosting.target;
73 const char *semihosting_get_arg(int i)
75 if (i >= semihosting.argc) {
76 return NULL;
78 return semihosting.argv[i];
81 int semihosting_get_argc(void)
83 return semihosting.argc;
86 const char *semihosting_get_cmdline(void)
88 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
89 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
91 return semihosting.cmdline;
94 static int add_semihosting_arg(void *opaque,
95 const char *name, const char *val,
96 Error **errp)
98 SemihostingConfig *s = opaque;
99 if (strcmp(name, "arg") == 0) {
100 s->argc++;
101 /* one extra element as g_strjoinv() expects NULL-terminated array */
102 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
103 s->argv[s->argc - 1] = val;
104 s->argv[s->argc] = NULL;
106 return 0;
109 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
110 void semihosting_arg_fallback(const char *file, const char *cmd)
112 char *cmd_token;
114 /* argv[0] */
115 add_semihosting_arg(&semihosting, "arg", file, NULL);
117 /* split -append and initialize argv[1..n] */
118 cmd_token = strtok(g_strdup(cmd), " ");
119 while (cmd_token) {
120 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
121 cmd_token = strtok(NULL, " ");
125 Chardev *semihosting_get_chardev(void)
127 return semihosting.chardev;
130 void qemu_semihosting_enable(void)
132 semihosting.enabled = true;
133 semihosting.target = SEMIHOSTING_TARGET_AUTO;
136 int qemu_semihosting_config_options(const char *optarg)
138 QemuOptsList *opt_list = qemu_find_opts("semihosting-config");
139 QemuOpts *opts = qemu_opts_parse_noisily(opt_list, optarg, false);
141 semihosting.enabled = true;
143 if (opts != NULL) {
144 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
145 true);
146 const char *target = qemu_opt_get(opts, "target");
147 /* setup of chardev is deferred until they are initialised */
148 semihost_chardev = qemu_opt_get(opts, "chardev");
149 if (target != NULL) {
150 if (strcmp("native", target) == 0) {
151 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
152 } else if (strcmp("gdb", target) == 0) {
153 semihosting.target = SEMIHOSTING_TARGET_GDB;
154 } else if (strcmp("auto", target) == 0) {
155 semihosting.target = SEMIHOSTING_TARGET_AUTO;
156 } else {
157 error_report("unsupported semihosting-config %s",
158 optarg);
159 return 1;
161 } else {
162 semihosting.target = SEMIHOSTING_TARGET_AUTO;
164 /* Set semihosting argument count and vector */
165 qemu_opt_foreach(opts, add_semihosting_arg,
166 &semihosting, NULL);
167 } else {
168 error_report("unsupported semihosting-config %s", optarg);
169 return 1;
172 return 0;
175 void qemu_semihosting_connect_chardevs(void)
177 /* We had to defer this until chardevs were created */
178 if (semihost_chardev) {
179 Chardev *chr = qemu_chr_find(semihost_chardev);
180 if (chr == NULL) {
181 error_report("semihosting chardev '%s' not found",
182 semihost_chardev);
183 exit(1);
185 semihosting.chardev = chr;