From nobody Sun May 19 05:00:09 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=mkfssion.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1640331604852570.5358018734001; Thu, 23 Dec 2021 23:40:04 -0800 (PST) Received: from localhost ([::1]:56440 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n0fBJ-0000iD-Iu for importer@patchew.org; Fri, 24 Dec 2021 02:40:03 -0500 Received: from eggs.gnu.org ([209.51.188.92]:43144) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n0exH-0001Vh-7A for qemu-devel@nongnu.org; Fri, 24 Dec 2021 02:25:33 -0500 Received: from mail-108-mta40.mxroute.com ([136.175.108.40]:34665) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1n0exD-0004Q5-Vt for qemu-devel@nongnu.org; Fri, 24 Dec 2021 02:25:29 -0500 Received: from filter004.mxroute.com ([149.28.56.236] filter004.mxroute.com) (Authenticated sender: mN4UYu2MZsgR) by mail-108-mta40.mxroute.com (ZoneMTA) with ESMTPSA id 17deb54852d000b7bf.001 for (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256); Fri, 24 Dec 2021 07:25:24 +0000 X-Zone-Loop: 040cabec25d03579ded6869be06e65df352ebfb2ea6c X-Originating-IP: [149.28.56.236] DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mkfssion.com; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date :Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=4WdwHLUvDNJNp9a++8gpKuaDXVbMYAzy/tqew3u+Pkw=; b=lQLAKQC4fXYmBRVziUvMRW6koI nHDZt8jbhoWHmBXAzgbicmS0MR4ij32BuRu58NyjjGjNdlGCJM/ESCg14jOOkkAk5oX1Zj8W8yVLy jpFoXB/DfOgKQb2Ih3QTLaqiRPzbNAWM1DkUTqBya2xuk8U/G6LJTzq4E+Tm2pull8HNOUVSuV+4+ wIF5vNxF2ANOjdCAyQsEaXe5j/OUUtRGU7e/oDzw/lmdBPtyXMolP7Whipo2UyKtSgqrl+4dW52Z0 yUh25qfcSowpMY9/iJ2Rgt4dXvmXnIF92P+6sVspLzLDwC1Jq54rcu/bN4XhCZckWXKaJGgotkznY C4lZwiMw==; From: MkfsSion To: qemu-devel@nongnu.org Subject: [PATCH v2] vl: Add support to set properties when using JSON syntax for -device via -set option Date: Fri, 24 Dec 2021 15:25:11 +0800 Message-Id: <20211224072511.63894-1-mkfssion@mkfssion.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-AuthUser: mkfssion@mkfssion.com X-Zone-Spam-Resolution: no action X-Zone-Spam-Status: No, score=5, required=15, tests=[ARC_NA=0, MID_CONTAINS_FROM=1, FROM_HAS_DN=0, RCPT_COUNT_THREE=0, TO_DN_SOME=0, R_MISSING_CHARSET=2.5, RCVD_COUNT_ZERO=0, FROM_EQ_ENVFROM=0, MIME_TRACE=0, BROKEN_CONTENT_TYPE=1.5, NEURAL_SPAM=0] Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=136.175.108.40; envelope-from=mkfssion@mkfssion.com; helo=mail-108-mta40.mxroute.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , MkfsSion Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1640331605968100001 Content-Type: text/plain; charset="utf-8" When using JSON syntax for -device, -set option can not find device specified in JSON by id field. The following commandline is an example: $ qemu-system-x86_64 -device '{"id":"foo"}' -set device.foo.bar=3D1 qemu-system-x86_64: -set device.foo.bar=3D1: there is no device "foo" defin= ed The patch fixes the above issue by trying to convert value provided by -set option to the type that the setting property actually takes. Signed-off-by: YuanYang Meng --- v2: 1.Set device option when group is 'device' only 2.Store value in type that properties actually take softmmu/vl.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/softmmu/vl.c b/softmmu/vl.c index 620a1f1367..c213e9e022 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -30,7 +30,9 @@ #include "hw/qdev-properties.h" #include "qapi/compat-policy.h" #include "qapi/error.h" +#include "qapi/qmp/qbool.h" #include "qapi/qmp/qdict.h" +#include "qapi/qmp/qnum.h" #include "qapi/qmp/qstring.h" #include "qapi/qmp/qjson.h" #include "qemu-version.h" @@ -2274,6 +2276,61 @@ static void qemu_read_default_config_file(Error **er= rp) } } =20 +static bool qemu_set_device_option_property(const char *id, const char *ke= y, + const char *value, Error **err= p) { + DeviceOption *opt; + QTAILQ_FOREACH(opt, &device_opts, next) { + const char *device_id =3D qdict_get_try_str(opt->opts, "id"); + if (device_id && (strcmp(device_id, id) =3D=3D 0)) { + QObject *obj =3D NULL; + if ((strcmp(key, "id") =3D=3D 0) || + (strcmp(key, "bus") =3D=3D 0) || + (strcmp(key, "driver") =3D=3D 0)) { + obj =3D QOBJECT(qstring_from_str(value)); + } else { + const char *driver =3D qdict_get_try_str(opt->opts, "drive= r"); + if (driver) { + ObjectClass *klass =3D object_class_by_name(driver); + ObjectProperty *prop =3D object_class_property_find(kl= ass, key); + if (prop) { + if (strcmp(prop->type, "str") =3D=3D 0) { + obj =3D QOBJECT(qstring_from_str(value)); + } else if (strcmp(prop->type, "bool") =3D=3D 0) { + bool boolean; + if (qapi_bool_parse(key, value, &boolean, errp= )) { + obj =3D QOBJECT(qbool_from_bool(boolean)); + } + } else if (strncmp(prop->type, "uint", 4) =3D=3D 0= ) { + uint64_t num; + if (parse_option_size(key, value, &num, errp))= { + obj =3D QOBJECT(qnum_from_uint(num)); + } + } else { + error_setg(errp, + "Setting property %s on device %s w= ith " + "type %s is unsupported via -set op= tion", + key, id, prop->type); + } + } else { + error_setg(errp, "Unable to find property %s on de= vice %s", + key, id); + } + } else { + error_setg(errp, "Unable to get driver for device %s",= id); + } + } + if (obj) { + qdict_del(opt->opts, key); + qdict_put_obj(opt->opts, key, obj); + return true; + } else { + return false; + } + } + } + return false; +} + static void qemu_set_option(const char *str, Error **errp) { char group[64], id[64], arg[64]; @@ -2294,6 +2351,11 @@ static void qemu_set_option(const char *str, Error *= *errp) if (list) { opts =3D qemu_opts_find(list, id); if (!opts) { + if (strcmp(group, "device") =3D=3D 0) { + if (qemu_set_device_option_property(id, arg, + str + offset + 1, = errp)) + return; + } error_setg(errp, "there is no %s \"%s\" defined", group, i= d); return; } --=20 2.34.1