From nobody Sun May 10 22:40:19 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51A56C433FE for ; Thu, 21 Apr 2022 21:22:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441940AbiDUVZE (ORCPT ); Thu, 21 Apr 2022 17:25:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1441929AbiDUVZA (ORCPT ); Thu, 21 Apr 2022 17:25:00 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D11A04BFE3 for ; Thu, 21 Apr 2022 14:22:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1650576128; x=1682112128; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bhT7Q8/ADSBtr63pT3T/HE2mvjBAH3w7Yptf5Y/H4HU=; b=ZZv4q1arXrsizz7D1pJHKVn61Gz4Mm92zoKgIUcjGYDwXH2QDd3ADXxc h59YW2SlJ9c4rYTGdTMu0mXxSl1ve5gLhPikdmcjJ2bzei6l73X4bA+wo 9UG9eefYzPNZTo8wFfV4M8ZmuxocqbxT38NphPM3D4ytvFBMil2bj2urc +H5NJ0ChkGjiPE+citzS50HhEZrp1/2QMbaMHfzweptktYZER7bIP38CC AhZcJp0ds9AZ4wIumnNWjpMm+6DKFLevMU6mYGMzzKzPUwTTcPwpKb7tt 0rnhWGlp1U+pPV43fp/XK2cI2/P6hzvQIrJW8ktwOJ1y6OWdLhVXSIgPF w==; X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="350924990" X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="350924990" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:08 -0700 X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="658704071" Received: from rhweight-mobl.amr.corp.intel.com (HELO rhweight-mobl.ra.intel.com) ([10.209.56.239]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:07 -0700 From: Russ Weight To: mcgrof@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, linux-kernel@vger.kernel.org Cc: trix@redhat.com, marpagan@redhat.com, lgoncalv@redhat.com, yilun.xu@intel.com, hao.wu@intel.com, matthew.gerlach@linux.intel.com, basheer.ahmed.muddebihal@intel.com, tianfei.zhang@intel.com, Russ Weight Subject: [PATCH v5 1/8] firmware_loader: Clear data and size in fw_free_paged_buf Date: Thu, 21 Apr 2022 14:21:57 -0700 Message-Id: <20220421212204.36052-2-russell.h.weight@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220421212204.36052-1-russell.h.weight@intel.com> References: <20220421212204.36052-1-russell.h.weight@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The fw_free_paged_buf() function resets the paged buffer information in the fw_priv data structure. Additionally, clear the data and size members of fw_priv in order to facilitate the reuse of fw_priv. This is being done in preparation for enabling userspace to initiate multiple firmware uploads using this sysfs interface. Reviewed-by: Luis Chamberlain Reviewed-by: Tianfei zhang Tested-by: Matthew Gerlach Signed-off-by: Russ Weight --- v1: - No change from RFC patch v2: - No change v3: - Added Reviewed-by tag v4: - Added Reviewed-by tag v5: - Added Tested-by tag --- drivers/base/firmware_loader/main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_lo= ader/main.c index 94d1789a233e..2cc11d93753a 100644 --- a/drivers/base/firmware_loader/main.c +++ b/drivers/base/firmware_loader/main.c @@ -253,6 +253,8 @@ void fw_free_paged_buf(struct fw_priv *fw_priv) fw_priv->pages =3D NULL; fw_priv->page_array_size =3D 0; fw_priv->nr_pages =3D 0; + fw_priv->data =3D NULL; + fw_priv->size =3D 0; } =20 int fw_grow_paged_buf(struct fw_priv *fw_priv, int pages_needed) --=20 2.25.1 From nobody Sun May 10 22:40:19 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A0D9CC433EF for ; Thu, 21 Apr 2022 21:22:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441953AbiDUVZK (ORCPT ); Thu, 21 Apr 2022 17:25:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1441935AbiDUVZB (ORCPT ); Thu, 21 Apr 2022 17:25:01 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90C604CD57 for ; Thu, 21 Apr 2022 14:22:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1650576130; x=1682112130; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/HmQwukft7k0R0Bh/oA/3+W8nZoO+4laBhkqeylZciA=; b=cox8fdDuNIKGZ2vAkOTaRbq7hztwc5rB29sLyEDtqnjEDA/db1wT3zmr QFUYwYfQPzO6bCoT5z3WapmOnYTUqdLN6Dmk2oNmTQQ7HrdTrlxPvwy9Z FI3a+9iCHXWYF9cm+UD1bwIHW5PkNS+YEA3btHFIuamhG/P9HGIn6AR3x xzImKW64GxZREvphgSGzc59hHRzoBP1UK4YzzpkJpEBgNCdO0KqMJrVM0 Y0SdJkSY1MryroyTV/i/RffmhHKWWHvS1GEZMJi/xB6OCfL9peWIjHnKI B36mAIbdvir1k0IpKYlZjO2QT00zaflMUoRMPo+2BNWuGBPTH5IOEC1bk Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="350924994" X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="350924994" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:09 -0700 X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="658704075" Received: from rhweight-mobl.amr.corp.intel.com (HELO rhweight-mobl.ra.intel.com) ([10.209.56.239]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:08 -0700 From: Russ Weight To: mcgrof@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, linux-kernel@vger.kernel.org Cc: trix@redhat.com, marpagan@redhat.com, lgoncalv@redhat.com, yilun.xu@intel.com, hao.wu@intel.com, matthew.gerlach@linux.intel.com, basheer.ahmed.muddebihal@intel.com, tianfei.zhang@intel.com, Russ Weight Subject: [PATCH v5 2/8] firmware_loader: Check fw_state_is_done in loading_store Date: Thu, 21 Apr 2022 14:21:58 -0700 Message-Id: <20220421212204.36052-3-russell.h.weight@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220421212204.36052-1-russell.h.weight@intel.com> References: <20220421212204.36052-1-russell.h.weight@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Rename fw_sysfs_done() and fw_sysfs_loading() to fw_state_is_done() and fw_state_is_loading() respectively, and place them along side companion functions in drivers/base/firmware_loader/firmware.h. Use the fw_state_is_done() function to exit early from firmware_loading_store() if the state is already "done". This is being done in preparation for supporting persistent sysfs nodes to allow userspace to upload firmware to a device, potentially reusing the sysfs loading and data files multiple times. Reviewed-by: Luis Chamberlain Reviewed-by: Tianfei zhang Tested-by: Matthew Gerlach Signed-off-by: Russ Weight --- v1: - No change from RFC patch v2: - It was discovered that the new function in v1, fw_state_is_done(), is equivalent to the existing fw_sysfs_done() function. Renamed fw_sysfs_done() and fw_sysfs_loading() to fw_state_is_done() and fw_state_is_loading() respectively, and placed them along side companion functions in drivers/base/firmware_loader/firmware.h. - Removed the "if !fw_sysfs_done(fw_priv))" condition in switch case 1 of firmware_loading_store(). It is rendered unnecessary by other changes to the function by this patch. v3: - Added Reviewed-by tag v4: - Added Reviewed-by tag v5: - Added Tested-by tag --- drivers/base/firmware_loader/fallback.c | 28 +++++++------------------ drivers/base/firmware_loader/firmware.h | 10 +++++++++ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/drivers/base/firmware_loader/fallback.c b/drivers/base/firmwar= e_loader/fallback.c index 4afb0e9312c0..8063eb595719 100644 --- a/drivers/base/firmware_loader/fallback.c +++ b/drivers/base/firmware_loader/fallback.c @@ -58,16 +58,6 @@ static long firmware_loading_timeout(void) __firmware_loading_timeout() * HZ : MAX_JIFFY_OFFSET; } =20 -static inline bool fw_sysfs_done(struct fw_priv *fw_priv) -{ - return __fw_state_check(fw_priv, FW_STATUS_DONE); -} - -static inline bool fw_sysfs_loading(struct fw_priv *fw_priv) -{ - return __fw_state_check(fw_priv, FW_STATUS_LOADING); -} - static inline int fw_sysfs_wait_timeout(struct fw_priv *fw_priv, long tim= eout) { return __fw_state_wait_common(fw_priv, timeout); @@ -91,7 +81,7 @@ static void __fw_load_abort(struct fw_priv *fw_priv) * There is a small window in which user can write to 'loading' * between loading done/aborted and disappearance of 'loading' */ - if (fw_state_is_aborted(fw_priv) || fw_sysfs_done(fw_priv)) + if (fw_state_is_aborted(fw_priv) || fw_state_is_done(fw_priv)) return; =20 fw_state_aborted(fw_priv); @@ -220,7 +210,7 @@ static ssize_t firmware_loading_show(struct device *dev, =20 mutex_lock(&fw_lock); if (fw_sysfs->fw_priv) - loading =3D fw_sysfs_loading(fw_sysfs->fw_priv); + loading =3D fw_state_is_loading(fw_sysfs->fw_priv); mutex_unlock(&fw_lock); =20 return sysfs_emit(buf, "%d\n", loading); @@ -250,19 +240,17 @@ static ssize_t firmware_loading_store(struct device *= dev, =20 mutex_lock(&fw_lock); fw_priv =3D fw_sysfs->fw_priv; - if (fw_state_is_aborted(fw_priv)) + if (fw_state_is_aborted(fw_priv) || fw_state_is_done(fw_priv)) goto out; =20 switch (loading) { case 1: /* discarding any previous partial load */ - if (!fw_sysfs_done(fw_priv)) { - fw_free_paged_buf(fw_priv); - fw_state_start(fw_priv); - } + fw_free_paged_buf(fw_priv); + fw_state_start(fw_priv); break; case 0: - if (fw_sysfs_loading(fw_priv)) { + if (fw_state_is_loading(fw_priv)) { int rc; =20 /* @@ -350,7 +338,7 @@ static ssize_t firmware_data_read(struct file *filp, st= ruct kobject *kobj, =20 mutex_lock(&fw_lock); fw_priv =3D fw_sysfs->fw_priv; - if (!fw_priv || fw_sysfs_done(fw_priv)) { + if (!fw_priv || fw_state_is_done(fw_priv)) { ret_count =3D -ENODEV; goto out; } @@ -410,7 +398,7 @@ static ssize_t firmware_data_write(struct file *filp, s= truct kobject *kobj, =20 mutex_lock(&fw_lock); fw_priv =3D fw_sysfs->fw_priv; - if (!fw_priv || fw_sysfs_done(fw_priv)) { + if (!fw_priv || fw_state_is_done(fw_priv)) { retval =3D -ENODEV; goto out; } diff --git a/drivers/base/firmware_loader/firmware.h b/drivers/base/firmwar= e_loader/firmware.h index 2889f446ad41..d5ff32a1ba2d 100644 --- a/drivers/base/firmware_loader/firmware.h +++ b/drivers/base/firmware_loader/firmware.h @@ -149,6 +149,16 @@ static inline void fw_state_done(struct fw_priv *fw_pr= iv) __fw_state_set(fw_priv, FW_STATUS_DONE); } =20 +static inline bool fw_state_is_done(struct fw_priv *fw_priv) +{ + return __fw_state_check(fw_priv, FW_STATUS_DONE); +} + +static inline bool fw_state_is_loading(struct fw_priv *fw_priv) +{ + return __fw_state_check(fw_priv, FW_STATUS_LOADING); +} + int assign_fw(struct firmware *fw, struct device *device); =20 #ifdef CONFIG_FW_LOADER --=20 2.25.1 From nobody Sun May 10 22:40:19 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B79CFC433EF for ; Thu, 21 Apr 2022 21:22:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441969AbiDUVZP (ORCPT ); Thu, 21 Apr 2022 17:25:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1441936AbiDUVZD (ORCPT ); Thu, 21 Apr 2022 17:25:03 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 341AF4BFE3 for ; Thu, 21 Apr 2022 14:22:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1650576131; x=1682112131; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iDfUiAkTLuxKaiLoZ/W1bN84abIQAF3UsXAdfAml3o4=; b=hnaX0AoU6AbSzadV339PsxpxNNeOU3z1018mptzD1BP+LYO7AnkPO+7k g5okhtHbjOrmiPdsyssCXUuxkBrw+8YqUY/wMbprwphPGak7qiHDBGMbi MZNneOjMfpKVkQkbhAl8LhR41rNydF4hDTfRNiPcqLsxofGA6iLk5kAmj qzIFPo5jctvWKBVJvwpMVK+cMEatoE8OumY4dKemx99XVWGUlsB/lDwxf 1MEUku2DgqizZ/889xt5QJ5VRu0nvqIhpNU8odfBSW0AD3tkWI3F7c3fc ZNpFcU3Y7u0avWXW1y1YfBjvyZG8LOME27/J26jrs7ZRPprbV9wop6zY/ g==; X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="350924996" X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="350924996" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:10 -0700 X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="658704080" Received: from rhweight-mobl.amr.corp.intel.com (HELO rhweight-mobl.ra.intel.com) ([10.209.56.239]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:09 -0700 From: Russ Weight To: mcgrof@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, linux-kernel@vger.kernel.org Cc: trix@redhat.com, marpagan@redhat.com, lgoncalv@redhat.com, yilun.xu@intel.com, hao.wu@intel.com, matthew.gerlach@linux.intel.com, basheer.ahmed.muddebihal@intel.com, tianfei.zhang@intel.com, Russ Weight Subject: [PATCH v5 3/8] firmware_loader: Split sysfs support from fallback Date: Thu, 21 Apr 2022 14:21:59 -0700 Message-Id: <20220421212204.36052-4-russell.h.weight@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220421212204.36052-1-russell.h.weight@intel.com> References: <20220421212204.36052-1-russell.h.weight@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In preparation for sharing the "loading" and "data" sysfs nodes with the new firmware upload support, split out sysfs functionality from fallback.c and fallback.h into sysfs.c and sysfs.h. This includes the firmware class driver code that is associated with the sysfs files and the fw_fallback_config support for the timeout sysfs node. CONFIG_FW_LOADER_SYSFS is created and is selected by CONFIG_FW_LOADER_USER_HELPER in order to include sysfs.o in firmware_class-objs. This is mostly just a code reorganization. There are a few symbols that change in scope, and these can be identified by looking at the header file changes. A few white-space warnings from checkpatch are also addressed in this patch. Reviewed-by: Luis Chamberlain Reviewed-by: Tianfei zhang Tested-by: Matthew Gerlach Signed-off-by: Russ Weight --- v1: - Renamed files fw_sysfs.c and fw_sysfs.h to sysfs.c and sysfs.h - Moved "MODULE_IMPORT_NS(FIRMWARE_LOADER_PRIVATE);" from sysfs.c to sysfs.h to address an error identified by the kernel test robot v2: - No change (other than adapting to changes in patch 3) v3: - Added Reviewed-by tag v4: - Added Reviewed-by tag - Fixed errors reported by the kernel test robot by adjus= ting the functions that are bound by "#ifdef CONFIG_FW_LOADER_USER_HELPER". v5: - Added Tested-by tag - More cleanup for "#ifdef CONFIG_FW_LOADER_USER_HELPER". Error reported by the kernel test robot . --- drivers/base/firmware_loader/Kconfig | 4 + drivers/base/firmware_loader/Makefile | 1 + drivers/base/firmware_loader/fallback.c | 418 ------------------------ drivers/base/firmware_loader/fallback.h | 46 +-- drivers/base/firmware_loader/sysfs.c | 401 +++++++++++++++++++++++ drivers/base/firmware_loader/sysfs.h | 96 ++++++ 6 files changed, 503 insertions(+), 463 deletions(-) create mode 100644 drivers/base/firmware_loader/sysfs.c create mode 100644 drivers/base/firmware_loader/sysfs.h diff --git a/drivers/base/firmware_loader/Kconfig b/drivers/base/firmware_l= oader/Kconfig index 38f3b66bf52b..9e03178eee00 100644 --- a/drivers/base/firmware_loader/Kconfig +++ b/drivers/base/firmware_loader/Kconfig @@ -29,6 +29,9 @@ if FW_LOADER config FW_LOADER_PAGED_BUF bool =20 +config FW_LOADER_SYSFS + bool + config EXTRA_FIRMWARE string "Build named firmware blobs into the kernel binary" help @@ -72,6 +75,7 @@ config EXTRA_FIRMWARE_DIR =20 config FW_LOADER_USER_HELPER bool "Enable the firmware sysfs fallback mechanism" + select FW_LOADER_SYSFS select FW_LOADER_PAGED_BUF help This option enables a sysfs loading facility to enable firmware diff --git a/drivers/base/firmware_loader/Makefile b/drivers/base/firmware_= loader/Makefile index e87843408fe6..aab213f82288 100644 --- a/drivers/base/firmware_loader/Makefile +++ b/drivers/base/firmware_loader/Makefile @@ -6,5 +6,6 @@ obj-$(CONFIG_FW_LOADER) +=3D firmware_class.o firmware_class-objs :=3D main.o firmware_class-$(CONFIG_FW_LOADER_USER_HELPER) +=3D fallback.o firmware_class-$(CONFIG_EFI_EMBEDDED_FIRMWARE) +=3D fallback_platform.o +firmware_class-$(CONFIG_FW_LOADER_SYSFS) +=3D sysfs.o =20 obj-y +=3D builtin/ diff --git a/drivers/base/firmware_loader/fallback.c b/drivers/base/firmwar= e_loader/fallback.c index 8063eb595719..bf68e3947814 100644 --- a/drivers/base/firmware_loader/fallback.c +++ b/drivers/base/firmware_loader/fallback.c @@ -3,12 +3,9 @@ #include #include #include -#include #include -#include #include #include -#include #include =20 #include "fallback.h" @@ -18,22 +15,6 @@ * firmware fallback mechanism */ =20 -MODULE_IMPORT_NS(FIRMWARE_LOADER_PRIVATE); - -extern struct firmware_fallback_config fw_fallback_config; - -/* These getters are vetted to use int properly */ -static inline int __firmware_loading_timeout(void) -{ - return fw_fallback_config.loading_timeout; -} - -/* These setters are vetted to use int properly */ -static void __fw_fallback_set_timeout(int timeout) -{ - fw_fallback_config.loading_timeout =3D timeout; -} - /* * use small loading timeout for caching devices' firmware because all the= se * firmware images have been loaded successfully at lease once, also syste= m is @@ -63,37 +44,6 @@ static inline int fw_sysfs_wait_timeout(struct fw_priv *= fw_priv, long timeout) return __fw_state_wait_common(fw_priv, timeout); } =20 -struct fw_sysfs { - bool nowait; - struct device dev; - struct fw_priv *fw_priv; - struct firmware *fw; -}; - -static struct fw_sysfs *to_fw_sysfs(struct device *dev) -{ - return container_of(dev, struct fw_sysfs, dev); -} - -static void __fw_load_abort(struct fw_priv *fw_priv) -{ - /* - * There is a small window in which user can write to 'loading' - * between loading done/aborted and disappearance of 'loading' - */ - if (fw_state_is_aborted(fw_priv) || fw_state_is_done(fw_priv)) - return; - - fw_state_aborted(fw_priv); -} - -static void fw_load_abort(struct fw_sysfs *fw_sysfs) -{ - struct fw_priv *fw_priv =3D fw_sysfs->fw_priv; - - __fw_load_abort(fw_priv); -} - static LIST_HEAD(pending_fw_head); =20 void kill_pending_fw_fallback_reqs(bool only_kill_custom) @@ -110,374 +60,6 @@ void kill_pending_fw_fallback_reqs(bool only_kill_cust= om) mutex_unlock(&fw_lock); } =20 -static ssize_t timeout_show(struct class *class, struct class_attribute *a= ttr, - char *buf) -{ - return sysfs_emit(buf, "%d\n", __firmware_loading_timeout()); -} - -/** - * timeout_store() - set number of seconds to wait for firmware - * @class: device class pointer - * @attr: device attribute pointer - * @buf: buffer to scan for timeout value - * @count: number of bytes in @buf - * - * Sets the number of seconds to wait for the firmware. Once - * this expires an error will be returned to the driver and no - * firmware will be provided. - * - * Note: zero means 'wait forever'. - **/ -static ssize_t timeout_store(struct class *class, struct class_attribute *= attr, - const char *buf, size_t count) -{ - int tmp_loading_timeout =3D simple_strtol(buf, NULL, 10); - - if (tmp_loading_timeout < 0) - tmp_loading_timeout =3D 0; - - __fw_fallback_set_timeout(tmp_loading_timeout); - - return count; -} -static CLASS_ATTR_RW(timeout); - -static struct attribute *firmware_class_attrs[] =3D { - &class_attr_timeout.attr, - NULL, -}; -ATTRIBUTE_GROUPS(firmware_class); - -static void fw_dev_release(struct device *dev) -{ - struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); - - kfree(fw_sysfs); -} - -static int do_firmware_uevent(struct fw_sysfs *fw_sysfs, struct kobj_ueven= t_env *env) -{ - if (add_uevent_var(env, "FIRMWARE=3D%s", fw_sysfs->fw_priv->fw_name)) - return -ENOMEM; - if (add_uevent_var(env, "TIMEOUT=3D%i", __firmware_loading_timeout())) - return -ENOMEM; - if (add_uevent_var(env, "ASYNC=3D%d", fw_sysfs->nowait)) - return -ENOMEM; - - return 0; -} - -static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env) -{ - struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); - int err =3D 0; - - mutex_lock(&fw_lock); - if (fw_sysfs->fw_priv) - err =3D do_firmware_uevent(fw_sysfs, env); - mutex_unlock(&fw_lock); - return err; -} - -static struct class firmware_class =3D { - .name =3D "firmware", - .class_groups =3D firmware_class_groups, - .dev_uevent =3D firmware_uevent, - .dev_release =3D fw_dev_release, -}; - -int register_sysfs_loader(void) -{ - int ret =3D class_register(&firmware_class); - - if (ret !=3D 0) - return ret; - return register_firmware_config_sysctl(); -} - -void unregister_sysfs_loader(void) -{ - unregister_firmware_config_sysctl(); - class_unregister(&firmware_class); -} - -static ssize_t firmware_loading_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); - int loading =3D 0; - - mutex_lock(&fw_lock); - if (fw_sysfs->fw_priv) - loading =3D fw_state_is_loading(fw_sysfs->fw_priv); - mutex_unlock(&fw_lock); - - return sysfs_emit(buf, "%d\n", loading); -} - -/** - * firmware_loading_store() - set value in the 'loading' control file - * @dev: device pointer - * @attr: device attribute pointer - * @buf: buffer to scan for loading control value - * @count: number of bytes in @buf - * - * The relevant values are: - * - * 1: Start a load, discarding any previous partial load. - * 0: Conclude the load and hand the data to the driver code. - * -1: Conclude the load with an error and discard any written data. - **/ -static ssize_t firmware_loading_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); - struct fw_priv *fw_priv; - ssize_t written =3D count; - int loading =3D simple_strtol(buf, NULL, 10); - - mutex_lock(&fw_lock); - fw_priv =3D fw_sysfs->fw_priv; - if (fw_state_is_aborted(fw_priv) || fw_state_is_done(fw_priv)) - goto out; - - switch (loading) { - case 1: - /* discarding any previous partial load */ - fw_free_paged_buf(fw_priv); - fw_state_start(fw_priv); - break; - case 0: - if (fw_state_is_loading(fw_priv)) { - int rc; - - /* - * Several loading requests may be pending on - * one same firmware buf, so let all requests - * see the mapped 'buf->data' once the loading - * is completed. - * */ - rc =3D fw_map_paged_buf(fw_priv); - if (rc) - dev_err(dev, "%s: map pages failed\n", - __func__); - else - rc =3D security_kernel_post_load_data(fw_priv->data, - fw_priv->size, - LOADING_FIRMWARE, "blob"); - - /* - * Same logic as fw_load_abort, only the DONE bit - * is ignored and we set ABORT only on failure. - */ - if (rc) { - fw_state_aborted(fw_priv); - written =3D rc; - } else { - fw_state_done(fw_priv); - } - break; - } - fallthrough; - default: - dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading); - fallthrough; - case -1: - fw_load_abort(fw_sysfs); - break; - } -out: - mutex_unlock(&fw_lock); - return written; -} - -static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_= store); - -static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer, - loff_t offset, size_t count, bool read) -{ - if (read) - memcpy(buffer, fw_priv->data + offset, count); - else - memcpy(fw_priv->data + offset, buffer, count); -} - -static void firmware_rw(struct fw_priv *fw_priv, char *buffer, - loff_t offset, size_t count, bool read) -{ - while (count) { - void *page_data; - int page_nr =3D offset >> PAGE_SHIFT; - int page_ofs =3D offset & (PAGE_SIZE-1); - int page_cnt =3D min_t(size_t, PAGE_SIZE - page_ofs, count); - - page_data =3D kmap(fw_priv->pages[page_nr]); - - if (read) - memcpy(buffer, page_data + page_ofs, page_cnt); - else - memcpy(page_data + page_ofs, buffer, page_cnt); - - kunmap(fw_priv->pages[page_nr]); - buffer +=3D page_cnt; - offset +=3D page_cnt; - count -=3D page_cnt; - } -} - -static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, - char *buffer, loff_t offset, size_t count) -{ - struct device *dev =3D kobj_to_dev(kobj); - struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); - struct fw_priv *fw_priv; - ssize_t ret_count; - - mutex_lock(&fw_lock); - fw_priv =3D fw_sysfs->fw_priv; - if (!fw_priv || fw_state_is_done(fw_priv)) { - ret_count =3D -ENODEV; - goto out; - } - if (offset > fw_priv->size) { - ret_count =3D 0; - goto out; - } - if (count > fw_priv->size - offset) - count =3D fw_priv->size - offset; - - ret_count =3D count; - - if (fw_priv->data) - firmware_rw_data(fw_priv, buffer, offset, count, true); - else - firmware_rw(fw_priv, buffer, offset, count, true); - -out: - mutex_unlock(&fw_lock); - return ret_count; -} - -static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size) -{ - int err; - - err =3D fw_grow_paged_buf(fw_sysfs->fw_priv, - PAGE_ALIGN(min_size) >> PAGE_SHIFT); - if (err) - fw_load_abort(fw_sysfs); - return err; -} - -/** - * firmware_data_write() - write method for firmware - * @filp: open sysfs file - * @kobj: kobject for the device - * @bin_attr: bin_attr structure - * @buffer: buffer being written - * @offset: buffer offset for write in total data store area - * @count: buffer size - * - * Data written to the 'data' attribute will be later handed to - * the driver as a firmware image. - **/ -static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, - char *buffer, loff_t offset, size_t count) -{ - struct device *dev =3D kobj_to_dev(kobj); - struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); - struct fw_priv *fw_priv; - ssize_t retval; - - if (!capable(CAP_SYS_RAWIO)) - return -EPERM; - - mutex_lock(&fw_lock); - fw_priv =3D fw_sysfs->fw_priv; - if (!fw_priv || fw_state_is_done(fw_priv)) { - retval =3D -ENODEV; - goto out; - } - - if (fw_priv->data) { - if (offset + count > fw_priv->allocated_size) { - retval =3D -ENOMEM; - goto out; - } - firmware_rw_data(fw_priv, buffer, offset, count, false); - retval =3D count; - } else { - retval =3D fw_realloc_pages(fw_sysfs, offset + count); - if (retval) - goto out; - - retval =3D count; - firmware_rw(fw_priv, buffer, offset, count, false); - } - - fw_priv->size =3D max_t(size_t, offset + count, fw_priv->size); -out: - mutex_unlock(&fw_lock); - return retval; -} - -static struct bin_attribute firmware_attr_data =3D { - .attr =3D { .name =3D "data", .mode =3D 0644 }, - .size =3D 0, - .read =3D firmware_data_read, - .write =3D firmware_data_write, -}; - -static struct attribute *fw_dev_attrs[] =3D { - &dev_attr_loading.attr, - NULL -}; - -static struct bin_attribute *fw_dev_bin_attrs[] =3D { - &firmware_attr_data, - NULL -}; - -static const struct attribute_group fw_dev_attr_group =3D { - .attrs =3D fw_dev_attrs, - .bin_attrs =3D fw_dev_bin_attrs, -}; - -static const struct attribute_group *fw_dev_attr_groups[] =3D { - &fw_dev_attr_group, - NULL -}; - -static struct fw_sysfs * -fw_create_instance(struct firmware *firmware, const char *fw_name, - struct device *device, u32 opt_flags) -{ - struct fw_sysfs *fw_sysfs; - struct device *f_dev; - - fw_sysfs =3D kzalloc(sizeof(*fw_sysfs), GFP_KERNEL); - if (!fw_sysfs) { - fw_sysfs =3D ERR_PTR(-ENOMEM); - goto exit; - } - - fw_sysfs->nowait =3D !!(opt_flags & FW_OPT_NOWAIT); - fw_sysfs->fw =3D firmware; - f_dev =3D &fw_sysfs->dev; - - device_initialize(f_dev); - dev_set_name(f_dev, "%s", fw_name); - f_dev->parent =3D device; - f_dev->class =3D &firmware_class; - f_dev->groups =3D fw_dev_attr_groups; -exit: - return fw_sysfs; -} - /** * fw_load_sysfs_fallback() - load a firmware via the sysfs fallback mecha= nism * @fw_sysfs: firmware sysfs information for the firmware to load diff --git a/drivers/base/firmware_loader/fallback.h b/drivers/base/firmwar= e_loader/fallback.h index 9f3055d3b4ca..144148595660 100644 --- a/drivers/base/firmware_loader/fallback.h +++ b/drivers/base/firmware_loader/fallback.h @@ -6,29 +6,7 @@ #include =20 #include "firmware.h" - -/** - * struct firmware_fallback_config - firmware fallback configuration setti= ngs - * - * Helps describe and fine tune the fallback mechanism. - * - * @force_sysfs_fallback: force the sysfs fallback mechanism to be used - * as if one had enabled CONFIG_FW_LOADER_USER_HELPER_FALLBACK=3Dy. - * Useful to help debug a CONFIG_FW_LOADER_USER_HELPER_FALLBACK=3Dy - * functionality on a kernel where that config entry has been disabled. - * @ignore_sysfs_fallback: force to disable the sysfs fallback mechanism. - * This emulates the behaviour as if we had set the kernel - * config CONFIG_FW_LOADER_USER_HELPER=3Dn. - * @old_timeout: for internal use - * @loading_timeout: the timeout to wait for the fallback mechanism before - * giving up, in seconds. - */ -struct firmware_fallback_config { - unsigned int force_sysfs_fallback; - unsigned int ignore_sysfs_fallback; - int old_timeout; - int loading_timeout; -}; +#include "sysfs.h" =20 #ifdef CONFIG_FW_LOADER_USER_HELPER int firmware_fallback_sysfs(struct firmware *fw, const char *name, @@ -40,19 +18,6 @@ void kill_pending_fw_fallback_reqs(bool only_kill_custom= ); void fw_fallback_set_cache_timeout(void); void fw_fallback_set_default_timeout(void); =20 -int register_sysfs_loader(void); -void unregister_sysfs_loader(void); -#ifdef CONFIG_SYSCTL -extern int register_firmware_config_sysctl(void); -extern void unregister_firmware_config_sysctl(void); -#else -static inline int register_firmware_config_sysctl(void) -{ - return 0; -} -static inline void unregister_firmware_config_sysctl(void) { } -#endif /* CONFIG_SYSCTL */ - #else /* CONFIG_FW_LOADER_USER_HELPER */ static inline int firmware_fallback_sysfs(struct firmware *fw, const char = *name, struct device *device, @@ -66,15 +31,6 @@ static inline int firmware_fallback_sysfs(struct firmwar= e *fw, const char *name, static inline void kill_pending_fw_fallback_reqs(bool only_kill_custom) { } static inline void fw_fallback_set_cache_timeout(void) { } static inline void fw_fallback_set_default_timeout(void) { } - -static inline int register_sysfs_loader(void) -{ - return 0; -} - -static inline void unregister_sysfs_loader(void) -{ -} #endif /* CONFIG_FW_LOADER_USER_HELPER */ =20 #ifdef CONFIG_EFI_EMBEDDED_FIRMWARE diff --git a/drivers/base/firmware_loader/sysfs.c b/drivers/base/firmware_l= oader/sysfs.c new file mode 100644 index 000000000000..1509cb6b6e60 --- /dev/null +++ b/drivers/base/firmware_loader/sysfs.c @@ -0,0 +1,401 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include + +#include "firmware.h" +#include "sysfs.h" + +/* + * sysfs support for firmware loader + */ + +void __fw_load_abort(struct fw_priv *fw_priv) +{ + /* + * There is a small window in which user can write to 'loading' + * between loading done/aborted and disappearance of 'loading' + */ + if (fw_state_is_aborted(fw_priv) || fw_state_is_done(fw_priv)) + return; + + fw_state_aborted(fw_priv); +} + +#ifdef CONFIG_FW_LOADER_USER_HELPER +static ssize_t timeout_show(struct class *class, struct class_attribute *a= ttr, + char *buf) +{ + return sysfs_emit(buf, "%d\n", __firmware_loading_timeout()); +} + +/** + * timeout_store() - set number of seconds to wait for firmware + * @class: device class pointer + * @attr: device attribute pointer + * @buf: buffer to scan for timeout value + * @count: number of bytes in @buf + * + * Sets the number of seconds to wait for the firmware. Once + * this expires an error will be returned to the driver and no + * firmware will be provided. + * + * Note: zero means 'wait forever'. + **/ +static ssize_t timeout_store(struct class *class, struct class_attribute *= attr, + const char *buf, size_t count) +{ + int tmp_loading_timeout =3D simple_strtol(buf, NULL, 10); + + if (tmp_loading_timeout < 0) + tmp_loading_timeout =3D 0; + + __fw_fallback_set_timeout(tmp_loading_timeout); + + return count; +} +static CLASS_ATTR_RW(timeout); + +static struct attribute *firmware_class_attrs[] =3D { + &class_attr_timeout.attr, + NULL, +}; +ATTRIBUTE_GROUPS(firmware_class); + +static int do_firmware_uevent(struct fw_sysfs *fw_sysfs, struct kobj_ueven= t_env *env) +{ + if (add_uevent_var(env, "FIRMWARE=3D%s", fw_sysfs->fw_priv->fw_name)) + return -ENOMEM; + if (add_uevent_var(env, "TIMEOUT=3D%i", __firmware_loading_timeout())) + return -ENOMEM; + if (add_uevent_var(env, "ASYNC=3D%d", fw_sysfs->nowait)) + return -ENOMEM; + + return 0; +} + +static int firmware_uevent(struct device *dev, struct kobj_uevent_env *env) +{ + struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); + int err =3D 0; + + mutex_lock(&fw_lock); + if (fw_sysfs->fw_priv) + err =3D do_firmware_uevent(fw_sysfs, env); + mutex_unlock(&fw_lock); + return err; +} +#endif /* CONFIG_FW_LOADER_USER_HELPER */ + +static void fw_dev_release(struct device *dev) +{ + struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); + + kfree(fw_sysfs); +} + +static struct class firmware_class =3D { + .name =3D "firmware", +#ifdef CONFIG_FW_LOADER_USER_HELPER + .class_groups =3D firmware_class_groups, + .dev_uevent =3D firmware_uevent, +#endif + .dev_release =3D fw_dev_release, +}; + +#ifdef CONFIG_FW_LOADER_USER_HELPER +int register_sysfs_loader(void) +{ + int ret =3D class_register(&firmware_class); + + if (ret !=3D 0) + return ret; + return register_firmware_config_sysctl(); +} + +void unregister_sysfs_loader(void) +{ + unregister_firmware_config_sysctl(); + class_unregister(&firmware_class); +} +#endif + +static ssize_t firmware_loading_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); + int loading =3D 0; + + mutex_lock(&fw_lock); + if (fw_sysfs->fw_priv) + loading =3D fw_state_is_loading(fw_sysfs->fw_priv); + mutex_unlock(&fw_lock); + + return sysfs_emit(buf, "%d\n", loading); +} + +/** + * firmware_loading_store() - set value in the 'loading' control file + * @dev: device pointer + * @attr: device attribute pointer + * @buf: buffer to scan for loading control value + * @count: number of bytes in @buf + * + * The relevant values are: + * + * 1: Start a load, discarding any previous partial load. + * 0: Conclude the load and hand the data to the driver code. + * -1: Conclude the load with an error and discard any written data. + **/ +static ssize_t firmware_loading_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); + struct fw_priv *fw_priv; + ssize_t written =3D count; + int loading =3D simple_strtol(buf, NULL, 10); + + mutex_lock(&fw_lock); + fw_priv =3D fw_sysfs->fw_priv; + if (fw_state_is_aborted(fw_priv) || fw_state_is_done(fw_priv)) + goto out; + + switch (loading) { + case 1: + /* discarding any previous partial load */ + fw_free_paged_buf(fw_priv); + fw_state_start(fw_priv); + break; + case 0: + if (fw_state_is_loading(fw_priv)) { + int rc; + + /* + * Several loading requests may be pending on + * one same firmware buf, so let all requests + * see the mapped 'buf->data' once the loading + * is completed. + */ + rc =3D fw_map_paged_buf(fw_priv); + if (rc) + dev_err(dev, "%s: map pages failed\n", + __func__); + else + rc =3D security_kernel_post_load_data(fw_priv->data, + fw_priv->size, + LOADING_FIRMWARE, + "blob"); + + /* + * Same logic as fw_load_abort, only the DONE bit + * is ignored and we set ABORT only on failure. + */ + if (rc) { + fw_state_aborted(fw_priv); + written =3D rc; + } else { + fw_state_done(fw_priv); + } + break; + } + fallthrough; + default: + dev_err(dev, "%s: unexpected value (%d)\n", __func__, loading); + fallthrough; + case -1: + fw_load_abort(fw_sysfs); + break; + } +out: + mutex_unlock(&fw_lock); + return written; +} + +static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_= store); + +static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer, + loff_t offset, size_t count, bool read) +{ + if (read) + memcpy(buffer, fw_priv->data + offset, count); + else + memcpy(fw_priv->data + offset, buffer, count); +} + +static void firmware_rw(struct fw_priv *fw_priv, char *buffer, + loff_t offset, size_t count, bool read) +{ + while (count) { + void *page_data; + int page_nr =3D offset >> PAGE_SHIFT; + int page_ofs =3D offset & (PAGE_SIZE - 1); + int page_cnt =3D min_t(size_t, PAGE_SIZE - page_ofs, count); + + page_data =3D kmap(fw_priv->pages[page_nr]); + + if (read) + memcpy(buffer, page_data + page_ofs, page_cnt); + else + memcpy(page_data + page_ofs, buffer, page_cnt); + + kunmap(fw_priv->pages[page_nr]); + buffer +=3D page_cnt; + offset +=3D page_cnt; + count -=3D page_cnt; + } +} + +static ssize_t firmware_data_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, + char *buffer, loff_t offset, size_t count) +{ + struct device *dev =3D kobj_to_dev(kobj); + struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); + struct fw_priv *fw_priv; + ssize_t ret_count; + + mutex_lock(&fw_lock); + fw_priv =3D fw_sysfs->fw_priv; + if (!fw_priv || fw_state_is_done(fw_priv)) { + ret_count =3D -ENODEV; + goto out; + } + if (offset > fw_priv->size) { + ret_count =3D 0; + goto out; + } + if (count > fw_priv->size - offset) + count =3D fw_priv->size - offset; + + ret_count =3D count; + + if (fw_priv->data) + firmware_rw_data(fw_priv, buffer, offset, count, true); + else + firmware_rw(fw_priv, buffer, offset, count, true); + +out: + mutex_unlock(&fw_lock); + return ret_count; +} + +static int fw_realloc_pages(struct fw_sysfs *fw_sysfs, int min_size) +{ + int err; + + err =3D fw_grow_paged_buf(fw_sysfs->fw_priv, + PAGE_ALIGN(min_size) >> PAGE_SHIFT); + if (err) + fw_load_abort(fw_sysfs); + return err; +} + +/** + * firmware_data_write() - write method for firmware + * @filp: open sysfs file + * @kobj: kobject for the device + * @bin_attr: bin_attr structure + * @buffer: buffer being written + * @offset: buffer offset for write in total data store area + * @count: buffer size + * + * Data written to the 'data' attribute will be later handed to + * the driver as a firmware image. + **/ +static ssize_t firmware_data_write(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, + char *buffer, loff_t offset, size_t count) +{ + struct device *dev =3D kobj_to_dev(kobj); + struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); + struct fw_priv *fw_priv; + ssize_t retval; + + if (!capable(CAP_SYS_RAWIO)) + return -EPERM; + + mutex_lock(&fw_lock); + fw_priv =3D fw_sysfs->fw_priv; + if (!fw_priv || fw_state_is_done(fw_priv)) { + retval =3D -ENODEV; + goto out; + } + + if (fw_priv->data) { + if (offset + count > fw_priv->allocated_size) { + retval =3D -ENOMEM; + goto out; + } + firmware_rw_data(fw_priv, buffer, offset, count, false); + retval =3D count; + } else { + retval =3D fw_realloc_pages(fw_sysfs, offset + count); + if (retval) + goto out; + + retval =3D count; + firmware_rw(fw_priv, buffer, offset, count, false); + } + + fw_priv->size =3D max_t(size_t, offset + count, fw_priv->size); +out: + mutex_unlock(&fw_lock); + return retval; +} + +static struct bin_attribute firmware_attr_data =3D { + .attr =3D { .name =3D "data", .mode =3D 0644 }, + .size =3D 0, + .read =3D firmware_data_read, + .write =3D firmware_data_write, +}; + +static struct attribute *fw_dev_attrs[] =3D { + &dev_attr_loading.attr, + NULL +}; + +static struct bin_attribute *fw_dev_bin_attrs[] =3D { + &firmware_attr_data, + NULL +}; + +static const struct attribute_group fw_dev_attr_group =3D { + .attrs =3D fw_dev_attrs, + .bin_attrs =3D fw_dev_bin_attrs, +}; + +static const struct attribute_group *fw_dev_attr_groups[] =3D { + &fw_dev_attr_group, + NULL +}; + +struct fw_sysfs * +fw_create_instance(struct firmware *firmware, const char *fw_name, + struct device *device, u32 opt_flags) +{ + struct fw_sysfs *fw_sysfs; + struct device *f_dev; + + fw_sysfs =3D kzalloc(sizeof(*fw_sysfs), GFP_KERNEL); + if (!fw_sysfs) { + fw_sysfs =3D ERR_PTR(-ENOMEM); + goto exit; + } + + fw_sysfs->nowait =3D !!(opt_flags & FW_OPT_NOWAIT); + fw_sysfs->fw =3D firmware; + f_dev =3D &fw_sysfs->dev; + + device_initialize(f_dev); + dev_set_name(f_dev, "%s", fw_name); + f_dev->parent =3D device; + f_dev->class =3D &firmware_class; + f_dev->groups =3D fw_dev_attr_groups; +exit: + return fw_sysfs; +} diff --git a/drivers/base/firmware_loader/sysfs.h b/drivers/base/firmware_l= oader/sysfs.h new file mode 100644 index 000000000000..b7b5c1766052 --- /dev/null +++ b/drivers/base/firmware_loader/sysfs.h @@ -0,0 +1,96 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __FIRMWARE_SYSFS_H +#define __FIRMWARE_SYSFS_H + +#include + +MODULE_IMPORT_NS(FIRMWARE_LOADER_PRIVATE); + +extern struct firmware_fallback_config fw_fallback_config; + +#ifdef CONFIG_FW_LOADER_USER_HELPER +/** + * struct firmware_fallback_config - firmware fallback configuration setti= ngs + * + * Helps describe and fine tune the fallback mechanism. + * + * @force_sysfs_fallback: force the sysfs fallback mechanism to be used + * as if one had enabled CONFIG_FW_LOADER_USER_HELPER_FALLBACK=3Dy. + * Useful to help debug a CONFIG_FW_LOADER_USER_HELPER_FALLBACK=3Dy + * functionality on a kernel where that config entry has been disabled. + * @ignore_sysfs_fallback: force to disable the sysfs fallback mechanism. + * This emulates the behaviour as if we had set the kernel + * config CONFIG_FW_LOADER_USER_HELPER=3Dn. + * @old_timeout: for internal use + * @loading_timeout: the timeout to wait for the fallback mechanism before + * giving up, in seconds. + */ +struct firmware_fallback_config { + unsigned int force_sysfs_fallback; + unsigned int ignore_sysfs_fallback; + int old_timeout; + int loading_timeout; +}; + +/* These getters are vetted to use int properly */ +static inline int __firmware_loading_timeout(void) +{ + return fw_fallback_config.loading_timeout; +} + +/* These setters are vetted to use int properly */ +static inline void __fw_fallback_set_timeout(int timeout) +{ + fw_fallback_config.loading_timeout =3D timeout; +} + +int register_sysfs_loader(void); +void unregister_sysfs_loader(void); +#ifdef CONFIG_SYSCTL +int register_firmware_config_sysctl(void); +void unregister_firmware_config_sysctl(void); +#else +static inline int register_firmware_config_sysctl(void) +{ + return 0; +} + +static inline void unregister_firmware_config_sysctl(void) { } +#endif /* CONFIG_SYSCTL */ +#else /* CONFIG_FW_LOADER_USER_HELPER */ +static inline int register_sysfs_loader(void) +{ + return 0; +} + +static inline void unregister_sysfs_loader(void) +{ +} +#endif /* CONFIG_FW_LOADER_USER_HELPER */ + +struct fw_sysfs { + bool nowait; + struct device dev; + struct fw_priv *fw_priv; + struct firmware *fw; +}; + +static inline struct fw_sysfs *to_fw_sysfs(struct device *dev) +{ + return container_of(dev, struct fw_sysfs, dev); +} + +void __fw_load_abort(struct fw_priv *fw_priv); + +static inline void fw_load_abort(struct fw_sysfs *fw_sysfs) +{ + struct fw_priv *fw_priv =3D fw_sysfs->fw_priv; + + __fw_load_abort(fw_priv); +} + +struct fw_sysfs * +fw_create_instance(struct firmware *firmware, const char *fw_name, + struct device *device, u32 opt_flags); + +#endif /* __FIRMWARE_SYSFS_H */ --=20 2.25.1 From nobody Sun May 10 22:40:19 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9ED8EC433F5 for ; Thu, 21 Apr 2022 21:22:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441990AbiDUVZ3 (ORCPT ); Thu, 21 Apr 2022 17:25:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1441948AbiDUVZH (ORCPT ); Thu, 21 Apr 2022 17:25:07 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4972F4D252 for ; Thu, 21 Apr 2022 14:22:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1650576135; x=1682112135; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Q74jdr1nZgJYOy/IM/sn5wGiVuQusnc4KWvwDeLME2g=; b=cT/LK4yp3cnSBDEhkiKMNpn9cwTej+Q04uTlscJtq8WL2ZxAlEryUNO1 yjsqWZzMjJTq7W9gwk4gyFCi24G1F897+moR3s4VCcAEi4SIuTIO+vW4d 2QC7p7klcFfMCBFuOjVl42DshccTbam13+u7IQMQI4uF6OE78hadjpSK+ Ni+y0oAL4pNSoPOgGrTbxKMG+w9Ao8yVirR0rFUU/C7rO3F4nxdedARov 47UsKmX3r62hfZwIKkTRfkeuG9prEm0J9irJDMjQ+T4BxgmtiBQs4sn/O Sk5LiITHUZxs39Z2eEsJkb9i2qyQVoovjYI1esLcfXcaKzA/yeZwo0mfJ A==; X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="350924997" X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="350924997" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:11 -0700 X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="658704088" Received: from rhweight-mobl.amr.corp.intel.com (HELO rhweight-mobl.ra.intel.com) ([10.209.56.239]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:10 -0700 From: Russ Weight To: mcgrof@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, linux-kernel@vger.kernel.org Cc: trix@redhat.com, marpagan@redhat.com, lgoncalv@redhat.com, yilun.xu@intel.com, hao.wu@intel.com, matthew.gerlach@linux.intel.com, basheer.ahmed.muddebihal@intel.com, tianfei.zhang@intel.com, Russ Weight Subject: [PATCH v5 4/8] firmware_loader: Add firmware-upload support Date: Thu, 21 Apr 2022 14:22:00 -0700 Message-Id: <20220421212204.36052-5-russell.h.weight@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220421212204.36052-1-russell.h.weight@intel.com> References: <20220421212204.36052-1-russell.h.weight@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Extend the firmware subsystem to support a persistent sysfs interface that userspace may use to initiate a firmware update. For example, FPGA based PCIe cards load firmware and FPGA images from local FLASH when the card boots. The images in FLASH may be updated with new images provided by the user at his/her convenience. A device driver may call firmware_upload_register() to expose persistent "loading" and "data" sysfs files. These files are used in the same way as the fallback sysfs "loading" and "data" files. When 0 is written to "loading" to complete the write of firmware data, the data is transferred to the lower-level driver using pre-registered call-back functions. The data transfer is done in the context of a kernel worker thread. Reviewed-by: Luis Chamberlain Reviewed-by: Tianfei zhang Tested-by: Matthew Gerlach Signed-off-by: Russ Weight --- v1: - renamed fw_upload_register() and fw_upload_unregister() to firmware_upload_register() and fw_upload_unregister(). - Moved ifdef'd section of code out of firmware_loading_store() in sysfs.c into a new function, fw_upload_start(), in sysfs_upload.c. - Changed #defines to enums for error codes and progress states - Added additional kernel-doc supported symbols into the documentation. Some rewording in documentation as well. - Added module reference counting for the parent module in the firmware_upload_register() and firmware_upload_unregister() functions to fix problems found when testing with test_firmware module. - Removed unnecessary module reference counting for THIS_MODULE. This module holds a reference count for the parent module, and the parent module has symbol dependencies on this module. v2: =20 - Updated documentation Date and KernelVersion fields to July 2022 and 5.19. - Unconditionally set fw_priv->is_paged_buf to true in firmware_upload_register(); v3: - Added Reviewed-by tag - Added kdoc support for enum fw_upload_prog progress codes v4: - Added Reviewed-by tag v5: - Added Tested-by tag --- .../ABI/testing/sysfs-class-firmware | 32 ++ .../driver-api/firmware/fw_upload.rst | 107 +++++++ Documentation/driver-api/firmware/index.rst | 1 + drivers/base/firmware_loader/Kconfig | 14 + drivers/base/firmware_loader/Makefile | 1 + drivers/base/firmware_loader/firmware.h | 6 + drivers/base/firmware_loader/main.c | 16 +- drivers/base/firmware_loader/sysfs.c | 19 +- drivers/base/firmware_loader/sysfs.h | 4 + drivers/base/firmware_loader/sysfs_upload.c | 276 ++++++++++++++++++ drivers/base/firmware_loader/sysfs_upload.h | 49 ++++ include/linux/firmware.h | 82 ++++++ 12 files changed, 595 insertions(+), 12 deletions(-) create mode 100644 Documentation/ABI/testing/sysfs-class-firmware create mode 100644 Documentation/driver-api/firmware/fw_upload.rst create mode 100644 drivers/base/firmware_loader/sysfs_upload.c create mode 100644 drivers/base/firmware_loader/sysfs_upload.h diff --git a/Documentation/ABI/testing/sysfs-class-firmware b/Documentation= /ABI/testing/sysfs-class-firmware new file mode 100644 index 000000000000..18336c23b70d --- /dev/null +++ b/Documentation/ABI/testing/sysfs-class-firmware @@ -0,0 +1,32 @@ +What: /sys/class/firmware/.../data +Date: July 2022 +KernelVersion: 5.19 +Contact: Russ Weight +Description: The data sysfs file is used for firmware-fallback and for + firmware uploads. Cat a firmware image to this sysfs file + after you echo 1 to the loading sysfs file. When the firmware + image write is complete, echo 0 to the loading sysfs file. This + sequence will signal the completion of the firmware write and + signal the lower-level driver that the firmware data is + available. + +What: /sys/class/firmware/.../loading +Date: July 2022 +KernelVersion: 5.19 +Contact: Russ Weight +Description: The loading sysfs file is used for both firmware-fallback and + for firmware uploads. Echo 1 onto the loading file to indicate + you are writing a firmware file to the data sysfs node. Echo + -1 onto this file to abort the data write or echo 0 onto this + file to indicate that the write is complete. For firmware + uploads, the zero value also triggers the transfer of the + firmware data to the lower-level device driver. + +What: /sys/class/firmware/.../timeout +Date: July 2022 +KernelVersion: 5.19 +Contact: Russ Weight +Description: This file supports the timeout mechanism for firmware + fallback. This file has no affect on firmware uploads. For + more information on timeouts please see the documentation + for firmware fallback. diff --git a/Documentation/driver-api/firmware/fw_upload.rst b/Documentatio= n/driver-api/firmware/fw_upload.rst new file mode 100644 index 000000000000..afbd8baca0d7 --- /dev/null +++ b/Documentation/driver-api/firmware/fw_upload.rst @@ -0,0 +1,107 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +Firmware Upload API +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +A device driver that registers with the firmware loader will expose +persistent sysfs nodes to enable users to initiate firmware updates for +that device. It is the responsibility of the device driver and/or the +device itself to perform any validation on the data received. Firmware +upload uses the same *loading* and *data* sysfs files described in the +documentation for firmware fallback. + +Register for firmware upload +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D + +A device driver registers for firmware upload by calling +firmware_upload_register(). Among the parameter list is a name to +identify the device under /sys/class/firmware. A user may initiate a +firmware upload by echoing a 1 to the *loading* sysfs file for the target +device. Next, the user writes the firmware image to the *data* sysfs +file. After writing the firmware data, the user echos 0 to the *loading* +sysfs file to signal completion. Echoing 0 to *loading* also triggers the +transfer of the firmware to the lower-lever device driver in the context +of a kernel worker thread. + +To use the firmware upload API, write a driver that implements a set of +ops. The probe function calls firmware_upload_register() and the remove +function calls firmware_upload_unregister() such as:: + + static const struct fw_upload_ops m10bmc_ops =3D { + .prepare =3D m10bmc_sec_prepare, + .write =3D m10bmc_sec_write, + .poll_complete =3D m10bmc_sec_poll_complete, + .cancel =3D m10bmc_sec_cancel, + .cleanup =3D m10bmc_sec_cleanup, + }; + + static int m10bmc_sec_probe(struct platform_device *pdev) + { + const char *fw_name, *truncate; + struct m10bmc_sec *sec; + struct fw_upload *fwl; + unsigned int len; + + sec =3D devm_kzalloc(&pdev->dev, sizeof(*sec), GFP_KERNEL); + if (!sec) + return -ENOMEM; + + sec->dev =3D &pdev->dev; + sec->m10bmc =3D dev_get_drvdata(pdev->dev.parent); + dev_set_drvdata(&pdev->dev, sec); + + fw_name =3D dev_name(sec->dev); + truncate =3D strstr(fw_name, ".auto"); + len =3D (truncate) ? truncate - fw_name : strlen(fw_name); + sec->fw_name =3D kmemdup_nul(fw_name, len, GFP_KERNEL); + + fwl =3D firmware_upload_register(sec->dev, sec->fw_name, &m10bmc_ops, se= c); + if (IS_ERR(fwl)) { + dev_err(sec->dev, "Firmware Upload driver failed to start\n"); + kfree(sec->fw_name); + return PTR_ERR(fwl); + } + + sec->fwl =3D fwl; + return 0; + } + + static int m10bmc_sec_remove(struct platform_device *pdev) + { + struct m10bmc_sec *sec =3D dev_get_drvdata(&pdev->dev); + + firmware_upload_unregister(sec->fwl); + kfree(sec->fw_name); + return 0; + } + +firmware_upload_register +------------------------ +.. kernel-doc:: drivers/base/firmware_loader/sysfs_upload.c + :identifiers: firmware_upload_register + +firmware_upload_unregister +-------------------------- +.. kernel-doc:: drivers/base/firmware_loader/sysfs_upload.c + :identifiers: firmware_upload_unregister + +Firmware Upload Ops +------------------- +.. kernel-doc:: include/linux/firmware.h + :identifiers: fw_upload_ops + +Firmware Upload Progress Codes +------------------------------ +The following progress codes are used internally by the firmware loader: + +.. kernel-doc:: drivers/base/firmware_loader/sysfs_upload.h + :identifiers: fw_upload_prog + +Firmware Upload Error Codes +--------------------------- +The following error codes may be returned by the driver ops in case of +failure: + +.. kernel-doc:: include/linux/firmware.h + :identifiers: fw_upload_err diff --git a/Documentation/driver-api/firmware/index.rst b/Documentation/dr= iver-api/firmware/index.rst index 57415d657173..9d2c19dc8e36 100644 --- a/Documentation/driver-api/firmware/index.rst +++ b/Documentation/driver-api/firmware/index.rst @@ -8,6 +8,7 @@ Linux Firmware API core efi/index request_firmware + fw_upload other_interfaces =20 .. only:: subproject and html diff --git a/drivers/base/firmware_loader/Kconfig b/drivers/base/firmware_l= oader/Kconfig index 9e03178eee00..adf2b182d74d 100644 --- a/drivers/base/firmware_loader/Kconfig +++ b/drivers/base/firmware_loader/Kconfig @@ -190,5 +190,19 @@ config FW_CACHE =20 If unsure, say Y. =20 +config FW_UPLOAD + bool "Enable users to initiate firmware updates using sysfs" + select FW_LOADER_SYSFS + select FW_LOADER_PAGED_BUF + help + Enabling this option will allow device drivers to expose a persistent + sysfs interface that allows firmware updates to be initiated from + userspace. For example, FPGA based PCIe cards load firmware and FPGA + images from local FLASH when the card boots. The images in FLASH may + be updated with new images provided by the user. Enable this device + to support cards that rely on user-initiated updates for firmware files. + + If unsure, say N. + endif # FW_LOADER endmenu diff --git a/drivers/base/firmware_loader/Makefile b/drivers/base/firmware_= loader/Makefile index aab213f82288..60d19f9e0ddc 100644 --- a/drivers/base/firmware_loader/Makefile +++ b/drivers/base/firmware_loader/Makefile @@ -7,5 +7,6 @@ firmware_class-objs :=3D main.o firmware_class-$(CONFIG_FW_LOADER_USER_HELPER) +=3D fallback.o firmware_class-$(CONFIG_EFI_EMBEDDED_FIRMWARE) +=3D fallback_platform.o firmware_class-$(CONFIG_FW_LOADER_SYSFS) +=3D sysfs.o +firmware_class-$(CONFIG_FW_UPLOAD) +=3D sysfs_upload.o =20 obj-y +=3D builtin/ diff --git a/drivers/base/firmware_loader/firmware.h b/drivers/base/firmwar= e_loader/firmware.h index d5ff32a1ba2d..fe77e91c38a2 100644 --- a/drivers/base/firmware_loader/firmware.h +++ b/drivers/base/firmware_loader/firmware.h @@ -87,6 +87,7 @@ struct fw_priv { }; =20 extern struct mutex fw_lock; +extern struct firmware_cache fw_cache; =20 static inline bool __fw_state_check(struct fw_priv *fw_priv, enum fw_status status) @@ -159,7 +160,12 @@ static inline bool fw_state_is_loading(struct fw_priv = *fw_priv) return __fw_state_check(fw_priv, FW_STATUS_LOADING); } =20 +int alloc_lookup_fw_priv(const char *fw_name, struct firmware_cache *fwc, + struct fw_priv **fw_priv, void *dbuf, size_t size, + size_t offset, u32 opt_flags); int assign_fw(struct firmware *fw, struct device *device); +void free_fw_priv(struct fw_priv *fw_priv); +void fw_state_init(struct fw_priv *fw_priv); =20 #ifdef CONFIG_FW_LOADER bool firmware_is_builtin(const struct firmware *fw); diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_lo= ader/main.c index 2cc11d93753a..874a5ef31c56 100644 --- a/drivers/base/firmware_loader/main.c +++ b/drivers/base/firmware_loader/main.c @@ -91,9 +91,9 @@ static inline struct fw_priv *to_fw_priv(struct kref *ref) * guarding for corner cases a global lock should be OK */ DEFINE_MUTEX(fw_lock); =20 -static struct firmware_cache fw_cache; +struct firmware_cache fw_cache; =20 -static void fw_state_init(struct fw_priv *fw_priv) +void fw_state_init(struct fw_priv *fw_priv) { struct fw_state *fw_st =3D &fw_priv->fw_st; =20 @@ -163,13 +163,9 @@ static struct fw_priv *__lookup_fw_priv(const char *fw= _name) } =20 /* Returns 1 for batching firmware requests with the same name */ -static int alloc_lookup_fw_priv(const char *fw_name, - struct firmware_cache *fwc, - struct fw_priv **fw_priv, - void *dbuf, - size_t size, - size_t offset, - u32 opt_flags) +int alloc_lookup_fw_priv(const char *fw_name, struct firmware_cache *fwc, + struct fw_priv **fw_priv, void *dbuf, size_t size, + size_t offset, u32 opt_flags) { struct fw_priv *tmp; =20 @@ -224,7 +220,7 @@ static void __free_fw_priv(struct kref *ref) kfree(fw_priv); } =20 -static void free_fw_priv(struct fw_priv *fw_priv) +void free_fw_priv(struct fw_priv *fw_priv) { struct firmware_cache *fwc =3D fw_priv->fwc; spin_lock(&fwc->lock); diff --git a/drivers/base/firmware_loader/sysfs.c b/drivers/base/firmware_l= oader/sysfs.c index 1509cb6b6e60..4a956cc3b7ea 100644 --- a/drivers/base/firmware_loader/sysfs.c +++ b/drivers/base/firmware_loader/sysfs.c @@ -6,8 +6,8 @@ #include #include =20 -#include "firmware.h" #include "sysfs.h" +#include "sysfs_upload.h" =20 /* * sysfs support for firmware loader @@ -94,6 +94,10 @@ static void fw_dev_release(struct device *dev) { struct fw_sysfs *fw_sysfs =3D to_fw_sysfs(dev); =20 + if (fw_sysfs->fw_upload_priv) { + free_fw_priv(fw_sysfs->fw_priv); + kfree(fw_sysfs->fw_upload_priv); + } kfree(fw_sysfs); } =20 @@ -199,6 +203,14 @@ static ssize_t firmware_loading_store(struct device *d= ev, written =3D rc; } else { fw_state_done(fw_priv); + + /* + * If this is a user-initiated firmware upload + * then start the upload in a worker thread now. + */ + rc =3D fw_upload_start(fw_sysfs); + if (rc) + written =3D rc; } break; } @@ -208,6 +220,9 @@ static ssize_t firmware_loading_store(struct device *de= v, fallthrough; case -1: fw_load_abort(fw_sysfs); + if (fw_sysfs->fw_upload_priv) + fw_state_init(fw_sysfs->fw_priv); + break; } out: @@ -215,7 +230,7 @@ static ssize_t firmware_loading_store(struct device *de= v, return written; } =20 -static DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_= store); +DEVICE_ATTR(loading, 0644, firmware_loading_show, firmware_loading_store); =20 static void firmware_rw_data(struct fw_priv *fw_priv, char *buffer, loff_t offset, size_t count, bool read) diff --git a/drivers/base/firmware_loader/sysfs.h b/drivers/base/firmware_l= oader/sysfs.h index b7b5c1766052..c21bcfe374ff 100644 --- a/drivers/base/firmware_loader/sysfs.h +++ b/drivers/base/firmware_loader/sysfs.h @@ -4,9 +4,12 @@ =20 #include =20 +#include "firmware.h" + MODULE_IMPORT_NS(FIRMWARE_LOADER_PRIVATE); =20 extern struct firmware_fallback_config fw_fallback_config; +extern struct device_attribute dev_attr_loading; =20 #ifdef CONFIG_FW_LOADER_USER_HELPER /** @@ -73,6 +76,7 @@ struct fw_sysfs { struct device dev; struct fw_priv *fw_priv; struct firmware *fw; + void *fw_upload_priv; }; =20 static inline struct fw_sysfs *to_fw_sysfs(struct device *dev) diff --git a/drivers/base/firmware_loader/sysfs_upload.c b/drivers/base/fir= mware_loader/sysfs_upload.c new file mode 100644 index 000000000000..0a6450d1974f --- /dev/null +++ b/drivers/base/firmware_loader/sysfs_upload.c @@ -0,0 +1,276 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include + +#include "sysfs.h" +#include "sysfs_upload.h" + +/* + * Support for user-space to initiate a firmware upload to a device. + */ + +static void fw_upload_update_progress(struct fw_upload_priv *fwlp, + enum fw_upload_prog new_progress) +{ + mutex_lock(&fwlp->lock); + fwlp->progress =3D new_progress; + mutex_unlock(&fwlp->lock); +} + +static void fw_upload_set_error(struct fw_upload_priv *fwlp, + enum fw_upload_err err_code) +{ + mutex_lock(&fwlp->lock); + fwlp->err_progress =3D fwlp->progress; + fwlp->err_code =3D err_code; + mutex_unlock(&fwlp->lock); +} + +static void fw_upload_prog_complete(struct fw_upload_priv *fwlp) +{ + mutex_lock(&fwlp->lock); + fwlp->progress =3D FW_UPLOAD_PROG_IDLE; + mutex_unlock(&fwlp->lock); +} + +static void fw_upload_main(struct work_struct *work) +{ + struct fw_upload_priv *fwlp; + struct fw_sysfs *fw_sysfs; + u32 written =3D 0, offset =3D 0; + enum fw_upload_err ret; + struct device *fw_dev; + struct fw_upload *fwl; + + fwlp =3D container_of(work, struct fw_upload_priv, work); + fwl =3D fwlp->fw_upload; + fw_sysfs =3D (struct fw_sysfs *)fwl->priv; + fw_dev =3D &fw_sysfs->dev; + + fw_upload_update_progress(fwlp, FW_UPLOAD_PROG_PREPARING); + ret =3D fwlp->ops->prepare(fwl, fwlp->data, fwlp->remaining_size); + if (ret !=3D FW_UPLOAD_ERR_NONE) { + fw_upload_set_error(fwlp, ret); + goto putdev_exit; + } + + fw_upload_update_progress(fwlp, FW_UPLOAD_PROG_TRANSFERRING); + while (fwlp->remaining_size) { + ret =3D fwlp->ops->write(fwl, fwlp->data, offset, + fwlp->remaining_size, &written); + if (ret !=3D FW_UPLOAD_ERR_NONE || !written) { + if (ret =3D=3D FW_UPLOAD_ERR_NONE) { + dev_warn(fw_dev, "write-op wrote zero data\n"); + ret =3D FW_UPLOAD_ERR_RW_ERROR; + } + fw_upload_set_error(fwlp, ret); + goto done; + } + + fwlp->remaining_size -=3D written; + offset +=3D written; + } + + fw_upload_update_progress(fwlp, FW_UPLOAD_PROG_PROGRAMMING); + ret =3D fwlp->ops->poll_complete(fwl); + if (ret !=3D FW_UPLOAD_ERR_NONE) + fw_upload_set_error(fwlp, ret); + +done: + if (fwlp->ops->cleanup) + fwlp->ops->cleanup(fwl); + +putdev_exit: + put_device(fw_dev->parent); + + /* + * Note: fwlp->remaining_size is left unmodified here to provide + * additional information on errors. It will be reinitialized when + * the next firmeware upload begins. + */ + mutex_lock(&fw_lock); + fw_free_paged_buf(fw_sysfs->fw_priv); + fw_state_init(fw_sysfs->fw_priv); + mutex_unlock(&fw_lock); + fwlp->data =3D NULL; + fw_upload_prog_complete(fwlp); +} + +/* + * Start a worker thread to upload data to the parent driver. + * Must be called with fw_lock held. + */ +int fw_upload_start(struct fw_sysfs *fw_sysfs) +{ + struct fw_priv *fw_priv =3D fw_sysfs->fw_priv; + struct device *fw_dev =3D &fw_sysfs->dev; + struct fw_upload_priv *fwlp; + + if (!fw_sysfs->fw_upload_priv) + return 0; + + if (!fw_priv->size) { + fw_free_paged_buf(fw_priv); + fw_state_init(fw_sysfs->fw_priv); + return 0; + } + + fwlp =3D fw_sysfs->fw_upload_priv; + mutex_lock(&fwlp->lock); + + /* Do not interfere with an on-going fw_upload */ + if (fwlp->progress !=3D FW_UPLOAD_PROG_IDLE) { + mutex_unlock(&fwlp->lock); + return -EBUSY; + } + + get_device(fw_dev->parent); /* released in fw_upload_main */ + + fwlp->progress =3D FW_UPLOAD_PROG_RECEIVING; + fwlp->err_code =3D 0; + fwlp->remaining_size =3D fw_priv->size; + fwlp->data =3D fw_priv->data; + + pr_debug("%s: fw-%s fw_priv=3D%p data=3D%p size=3D%u\n", + __func__, fw_priv->fw_name, + fw_priv, fw_priv->data, + (unsigned int)fw_priv->size); + + queue_work(system_long_wq, &fwlp->work); + mutex_unlock(&fwlp->lock); + + return 0; +} + +/** + * firmware_upload_register() - register for the firmware upload sysfs API + * @parent: parent device instantiating firmware upload + * @name: firmware name to be associated with this device + * @ops: pointer to structure of firmware upload ops + * @dd_handle: pointer to parent driver private data + * + * @name must be unique among all users of firmware upload. The firmware + * sysfs files for this device will be found at /sys/class/firmware/@name. + * + * Return: struct fw_upload pointer or ERR_PTR() + * + **/ +struct fw_upload * +firmware_upload_register(struct module *module, struct device *parent, + const char *name, const struct fw_upload_ops *ops, + void *dd_handle) +{ + u32 opt_flags =3D FW_OPT_NOCACHE; + struct fw_upload *fw_upload; + struct fw_upload_priv *fw_upload_priv; + struct fw_sysfs *fw_sysfs; + struct fw_priv *fw_priv; + struct device *fw_dev; + int ret; + + if (!name || name[0] =3D=3D '\0') + return ERR_PTR(-EINVAL); + + if (!ops || !ops->cancel || !ops->prepare || + !ops->write || !ops->poll_complete) { + dev_err(parent, "Attempt to register without all required ops\n"); + return ERR_PTR(-EINVAL); + } + + if (!try_module_get(module)) + return ERR_PTR(-EFAULT); + + fw_upload =3D kzalloc(sizeof(*fw_upload), GFP_KERNEL); + if (!fw_upload) { + ret =3D -ENOMEM; + goto exit_module_put; + } + + fw_upload_priv =3D kzalloc(sizeof(*fw_upload_priv), GFP_KERNEL); + if (!fw_upload_priv) { + ret =3D -ENOMEM; + goto free_fw_upload; + } + + fw_upload_priv->fw_upload =3D fw_upload; + fw_upload_priv->ops =3D ops; + mutex_init(&fw_upload_priv->lock); + fw_upload_priv->module =3D module; + fw_upload_priv->name =3D name; + fw_upload_priv->err_code =3D 0; + fw_upload_priv->progress =3D FW_UPLOAD_PROG_IDLE; + INIT_WORK(&fw_upload_priv->work, fw_upload_main); + fw_upload->dd_handle =3D dd_handle; + + fw_sysfs =3D fw_create_instance(NULL, name, parent, opt_flags); + if (IS_ERR(fw_sysfs)) { + ret =3D PTR_ERR(fw_sysfs); + goto free_fw_upload_priv; + } + fw_upload->priv =3D fw_sysfs; + fw_sysfs->fw_upload_priv =3D fw_upload_priv; + fw_dev =3D &fw_sysfs->dev; + + ret =3D alloc_lookup_fw_priv(name, &fw_cache, &fw_priv, NULL, 0, 0, + FW_OPT_NOCACHE); + if (ret !=3D 0) { + if (ret > 0) + ret =3D -EINVAL; + goto free_fw_sysfs; + } + fw_priv->is_paged_buf =3D true; + fw_sysfs->fw_priv =3D fw_priv; + + ret =3D device_add(fw_dev); + if (ret) { + dev_err(fw_dev, "%s: device_register failed\n", __func__); + put_device(fw_dev); + goto exit_module_put; + } + + return fw_upload; + +free_fw_sysfs: + kfree(fw_sysfs); + +free_fw_upload_priv: + kfree(fw_upload_priv); + +free_fw_upload: + kfree(fw_upload); + +exit_module_put: + module_put(module); + + return ERR_PTR(ret); +} +EXPORT_SYMBOL_GPL(firmware_upload_register); + +/** + * firmware_upload_unregister() - Unregister firmware upload interface + * @fw_upload: pointer to struct fw_upload + **/ +void firmware_upload_unregister(struct fw_upload *fw_upload) +{ + struct fw_sysfs *fw_sysfs =3D fw_upload->priv; + struct fw_upload_priv *fw_upload_priv =3D fw_sysfs->fw_upload_priv; + + mutex_lock(&fw_upload_priv->lock); + if (fw_upload_priv->progress =3D=3D FW_UPLOAD_PROG_IDLE) { + mutex_unlock(&fw_upload_priv->lock); + goto unregister; + } + + fw_upload_priv->ops->cancel(fw_upload); + mutex_unlock(&fw_upload_priv->lock); + + /* Ensure lower-level device-driver is finished */ + flush_work(&fw_upload_priv->work); + +unregister: + device_unregister(&fw_sysfs->dev); + module_put(fw_upload_priv->module); +} +EXPORT_SYMBOL_GPL(firmware_upload_unregister); diff --git a/drivers/base/firmware_loader/sysfs_upload.h b/drivers/base/fir= mware_loader/sysfs_upload.h new file mode 100644 index 000000000000..18bd4d99f064 --- /dev/null +++ b/drivers/base/firmware_loader/sysfs_upload.h @@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __FIRMWARE_UPLOAD_H +#define __FIRMWARE_UPLOAD_H + +#include + +/** + * enum fw_upload_prog - firmware upload progress codes + * @FW_UPLOAD_PROG_IDLE: there is no firmware upload in progress + * @FW_UPLOAD_PROG_RECEIVING: worker thread is receiving firmware data + * @FW_UPLOAD_PROG_PREPARING: target device is preparing for firmware uplo= ad + * @FW_UPLOAD_PROG_TRANSFERRING: data is being copied to the device + * @FW_UPLOAD_PROG_PROGRAMMING: device is performing the firmware update + * @FW_UPLOAD_PROG_MAX: Maximum progress code marker + */ +enum fw_upload_prog { + FW_UPLOAD_PROG_IDLE, + FW_UPLOAD_PROG_RECEIVING, + FW_UPLOAD_PROG_PREPARING, + FW_UPLOAD_PROG_TRANSFERRING, + FW_UPLOAD_PROG_PROGRAMMING, + FW_UPLOAD_PROG_MAX +}; + +struct fw_upload_priv { + struct fw_upload *fw_upload; + struct module *module; + const char *name; + const struct fw_upload_ops *ops; + struct mutex lock; /* protect data structure contents */ + struct work_struct work; + const u8 *data; /* pointer to update data */ + u32 remaining_size; /* size remaining to transfer */ + enum fw_upload_prog progress; + enum fw_upload_prog err_progress; /* progress at time of failure */ + enum fw_upload_err err_code; /* security manager error code */ +}; + +#ifdef CONFIG_FW_UPLOAD +int fw_upload_start(struct fw_sysfs *fw_sysfs); +umode_t fw_upload_is_visible(struct kobject *kobj, struct attribute *attr,= int n); +#else +static inline int fw_upload_start(struct fw_sysfs *fw_sysfs) +{ + return 0; +} +#endif + +#endif /* __FIRMWARE_UPLOAD_H */ diff --git a/include/linux/firmware.h b/include/linux/firmware.h index ec2ccfebef65..de7fea3bca51 100644 --- a/include/linux/firmware.h +++ b/include/linux/firmware.h @@ -17,6 +17,64 @@ struct firmware { void *priv; }; =20 +/** + * enum fw_upload_err - firmware upload error codes + * @FW_UPLOAD_ERR_NONE: returned to indicate success + * @FW_UPLOAD_ERR_HW_ERROR: error signalled by hardware, see kernel log + * @FW_UPLOAD_ERR_TIMEOUT: SW timed out on handshake with HW/firmware + * @FW_UPLOAD_ERR_CANCELED: upload was cancelled by the user + * @FW_UPLOAD_ERR_BUSY: there is an upload operation already in progress + * @FW_UPLOAD_ERR_INVALID_SIZE: invalid firmware image size + * @FW_UPLOAD_ERR_RW_ERROR: read or write to HW failed, see kernel log + * @FW_UPLOAD_ERR_WEAROUT: FLASH device is approaching wear-out, wait & re= try + * @FW_UPLOAD_ERR_MAX: Maximum error code marker + */ +enum fw_upload_err { + FW_UPLOAD_ERR_NONE, + FW_UPLOAD_ERR_HW_ERROR, + FW_UPLOAD_ERR_TIMEOUT, + FW_UPLOAD_ERR_CANCELED, + FW_UPLOAD_ERR_BUSY, + FW_UPLOAD_ERR_INVALID_SIZE, + FW_UPLOAD_ERR_RW_ERROR, + FW_UPLOAD_ERR_WEAROUT, + FW_UPLOAD_ERR_MAX +}; + +struct fw_upload { + void *dd_handle; /* reference to parent driver */ + void *priv; /* firmware loader private fields */ +}; + +/** + * struct fw_upload_ops - device specific operations to support firmware u= pload + * @prepare: Required: Prepare secure update + * @write: Required: The write() op receives the remaining + * size to be written and must return the actual + * size written or a negative error code. The write() + * op will be called repeatedly until all data is + * written. + * @poll_complete: Required: Check for the completion of the + * HW authentication/programming process. + * @cancel: Required: Request cancellation of update. This op + * is called from the context of a different kernel + * thread, so race conditions need to be considered. + * @cleanup: Optional: Complements the prepare() + * function and is called at the completion + * of the update, on success or failure, if the + * prepare function succeeded. + */ +struct fw_upload_ops { + enum fw_upload_err (*prepare)(struct fw_upload *fw_upload, + const u8 *data, u32 size); + enum fw_upload_err (*write)(struct fw_upload *fw_upload, + const u8 *data, u32 offset, + u32 size, u32 *written); + enum fw_upload_err (*poll_complete)(struct fw_upload *fw_upload); + void (*cancel)(struct fw_upload *fw_upload); + void (*cleanup)(struct fw_upload *fw_upload); +}; + struct module; struct device; =20 @@ -112,6 +170,30 @@ static inline int request_partial_firmware_into_buf =20 #endif =20 +#ifdef CONFIG_FW_UPLOAD + +struct fw_upload * +firmware_upload_register(struct module *module, struct device *parent, + const char *name, const struct fw_upload_ops *ops, + void *dd_handle); +void firmware_upload_unregister(struct fw_upload *fw_upload); + +#else + +static inline struct fw_upload * +firmware_upload_register(struct module *module, struct device *parent, + const char *name, const struct fw_upload_ops *ops, + void *dd_handle) +{ + return ERR_PTR(-EINVAL); +} + +static inline void firmware_upload_unregister(struct fw_upload *fw_upload) +{ +} + +#endif + int firmware_request_cache(struct device *device, const char *name); =20 #endif --=20 2.25.1 From nobody Sun May 10 22:40:19 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 958B2C433F5 for ; Thu, 21 Apr 2022 21:22:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441974AbiDUVZS (ORCPT ); Thu, 21 Apr 2022 17:25:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1441941AbiDUVZF (ORCPT ); Thu, 21 Apr 2022 17:25:05 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14D4E4CD57 for ; Thu, 21 Apr 2022 14:22:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1650576134; x=1682112134; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Lp4lZoIIwBDJEpUgLrHuKBe5p/Lh3s+lAXSNF1q3h9A=; b=mVwgOM31iA2vcz9WkPsSV9hS4rkgaxDAZPoxiHaalq4rE8cClZhDqV0F KduVTTK+IaJLhobCzc5iZEP2XgWHgn8LWsnGUkOGRYGpGoKah2pvC05wG zH/eTOHEPE5FMvCTcfKMyePG2dYVzkU2Z6tzu8PUl69ux8aWq3VguZmDq D/cbVDj/6skYbEhzOk1vgyG+qMH7Ixmt/1hrD70j5bBKB6/XS6+k46vcH MaQrD2LsleU9sqAI2SCXM6YFGG7DQpHcvhWsFwgYeM95h1JOqKnl1K1Gw L89Tczuy4y7AxCAHryC+lPhd2gupoLtFcaG+4G4glBxIYNZEDSBB7YBk4 Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="350925001" X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="350925001" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:12 -0700 X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="658704095" Received: from rhweight-mobl.amr.corp.intel.com (HELO rhweight-mobl.ra.intel.com) ([10.209.56.239]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:11 -0700 From: Russ Weight To: mcgrof@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, linux-kernel@vger.kernel.org Cc: trix@redhat.com, marpagan@redhat.com, lgoncalv@redhat.com, yilun.xu@intel.com, hao.wu@intel.com, matthew.gerlach@linux.intel.com, basheer.ahmed.muddebihal@intel.com, tianfei.zhang@intel.com, Russ Weight Subject: [PATCH v5 5/8] firmware_loader: Add sysfs nodes to monitor fw_upload Date: Thu, 21 Apr 2022 14:22:01 -0700 Message-Id: <20220421212204.36052-6-russell.h.weight@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220421212204.36052-1-russell.h.weight@intel.com> References: <20220421212204.36052-1-russell.h.weight@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add additional sysfs nodes to monitor the transfer of firmware upload data to the target device: cancel: Write 1 to cancel the data transfer error: Display error status for a failed firmware upload remaining_size: Display the remaining amount of data to be transferred status: Display the progress of the firmware upload Reviewed-by: Luis Chamberlain Reviewed-by: Tianfei zhang Tested-by: Matthew Gerlach Signed-off-by: Russ Weight --- v1: - Adapted to enums and filename changes. Otherwise no changes. v2: - Updated documentation Date and KernelVersion fields to July 2022 and 5.19. v3: - Added Reviewed-by tag - Added additional documentation text regarding progress codes v4: - Added Reviewed-by tag v5: - Added Tested-by tag --- .../ABI/testing/sysfs-class-firmware | 45 +++++++ .../driver-api/firmware/fw_upload.rst | 23 +++- drivers/base/firmware_loader/sysfs.c | 9 ++ drivers/base/firmware_loader/sysfs_upload.c | 121 ++++++++++++++++++ drivers/base/firmware_loader/sysfs_upload.h | 5 + 5 files changed, 201 insertions(+), 2 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-class-firmware b/Documentation= /ABI/testing/sysfs-class-firmware index 18336c23b70d..978d3d500400 100644 --- a/Documentation/ABI/testing/sysfs-class-firmware +++ b/Documentation/ABI/testing/sysfs-class-firmware @@ -10,6 +10,30 @@ Description: The data sysfs file is used for firmware-fa= llback and for signal the lower-level driver that the firmware data is available. =20 +What: /sys/class/firmware/.../cancel +Date: July 2022 +KernelVersion: 5.19 +Contact: Russ Weight +Description: Write-only. For firmware uploads, write a "1" to this file to + request that the transfer of firmware data to the lower-level + device be canceled. This request will be rejected (EBUSY) if + the update cannot be canceled (e.g. a FLASH write is in + progress) or (ENODEV) if there is no firmware update in progress. + +What: /sys/class/firmware/.../error +Date: July 2022 +KernelVersion: 5.19 +Contact: Russ Weight +Description: Read-only. Returns a string describing a failed firmware + upload. This string will be in the form of :, + where will be one of the status strings described + for the status sysfs file and will be one of the + following: "hw-error", "timeout", "user-abort", "device-busy", + "invalid-file-size", "read-write-error", "flash-wearout". The + error sysfs file is only meaningful when the current firmware + upload status is "idle". If this file is read while a firmware + transfer is in progress, then the read will fail with EBUSY. + What: /sys/class/firmware/.../loading Date: July 2022 KernelVersion: 5.19 @@ -22,6 +46,27 @@ Description: The loading sysfs file is used for both fir= mware-fallback and uploads, the zero value also triggers the transfer of the firmware data to the lower-level device driver. =20 +What: /sys/class/firmware/.../remaining_size +Date: July 2022 +KernelVersion: 5.19 +Contact: Russ Weight +Description: Read-only. For firmware upload, this file contains the size + of the firmware data that remains to be transferred to the + lower-level device driver. The size value is initialized to + the full size of the firmware image that was previously + written to the data sysfs file. This value is periodically + updated during the "transferring" phase of the firmware + upload. + Format: "%u". + +What: /sys/class/firmware/.../status +Date: July 2022 +KernelVersion: 5.19 +Contact: Russ Weight +Description: Read-only. Returns a string describing the current status of + a firmware upload. The string will be one of the following: + idle, "receiving", "preparing", "transferring", "programming". + What: /sys/class/firmware/.../timeout Date: July 2022 KernelVersion: 5.19 diff --git a/Documentation/driver-api/firmware/fw_upload.rst b/Documentatio= n/driver-api/firmware/fw_upload.rst index afbd8baca0d7..76922591e446 100644 --- a/Documentation/driver-api/firmware/fw_upload.rst +++ b/Documentation/driver-api/firmware/fw_upload.rst @@ -9,7 +9,8 @@ persistent sysfs nodes to enable users to initiate firmware= updates for that device. It is the responsibility of the device driver and/or the device itself to perform any validation on the data received. Firmware upload uses the same *loading* and *data* sysfs files described in the -documentation for firmware fallback. +documentation for firmware fallback. It also adds additional sysfs files +to provide status on the transfer of the firmware image to the device. =20 Register for firmware upload =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D @@ -93,7 +94,9 @@ Firmware Upload Ops =20 Firmware Upload Progress Codes ------------------------------ -The following progress codes are used internally by the firmware loader: +The following progress codes are used internally by the firmware loader. +Corresponding strings are reported through the status sysfs node that +is described below and are documented in the ABI documentation. =20 .. kernel-doc:: drivers/base/firmware_loader/sysfs_upload.h :identifiers: fw_upload_prog @@ -105,3 +108,19 @@ failure: =20 .. kernel-doc:: include/linux/firmware.h :identifiers: fw_upload_err + +Sysfs Attributes +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +In addition to the *loading* and *data* sysfs files, there are additional +sysfs files to monitor the status of the data transfer to the target +device and to determine the final pass/fail status of the transfer. +Depending on the device and the size of the firmware image, a firmware +update could take milliseconds or minutes. + +The additional sysfs files are: + +* status - provides an indication of the progress of a firmware update +* error - provides error information for a failed firmware update +* remaining_size - tracks the data transfer portion of an update +* cancel - echo 1 to this file to cancel the update diff --git a/drivers/base/firmware_loader/sysfs.c b/drivers/base/firmware_l= oader/sysfs.c index 4a956cc3b7ea..c09fcebeada9 100644 --- a/drivers/base/firmware_loader/sysfs.c +++ b/drivers/base/firmware_loader/sysfs.c @@ -371,6 +371,12 @@ static struct bin_attribute firmware_attr_data =3D { =20 static struct attribute *fw_dev_attrs[] =3D { &dev_attr_loading.attr, +#ifdef CONFIG_FW_UPLOAD + &dev_attr_cancel.attr, + &dev_attr_status.attr, + &dev_attr_error.attr, + &dev_attr_remaining_size.attr, +#endif NULL }; =20 @@ -382,6 +388,9 @@ static struct bin_attribute *fw_dev_bin_attrs[] =3D { static const struct attribute_group fw_dev_attr_group =3D { .attrs =3D fw_dev_attrs, .bin_attrs =3D fw_dev_bin_attrs, +#ifdef CONFIG_FW_UPLOAD + .is_visible =3D fw_upload_is_visible, +#endif }; =20 static const struct attribute_group *fw_dev_attr_groups[] =3D { diff --git a/drivers/base/firmware_loader/sysfs_upload.c b/drivers/base/fir= mware_loader/sysfs_upload.c index 0a6450d1974f..c504dae00dbe 100644 --- a/drivers/base/firmware_loader/sysfs_upload.c +++ b/drivers/base/firmware_loader/sysfs_upload.c @@ -11,6 +11,127 @@ * Support for user-space to initiate a firmware upload to a device. */ =20 +static const char * const fw_upload_prog_str[] =3D { + [FW_UPLOAD_PROG_IDLE] =3D "idle", + [FW_UPLOAD_PROG_RECEIVING] =3D "receiving", + [FW_UPLOAD_PROG_PREPARING] =3D "preparing", + [FW_UPLOAD_PROG_TRANSFERRING] =3D "transferring", + [FW_UPLOAD_PROG_PROGRAMMING] =3D "programming" +}; + +static const char * const fw_upload_err_str[] =3D { + [FW_UPLOAD_ERR_NONE] =3D "none", + [FW_UPLOAD_ERR_HW_ERROR] =3D "hw-error", + [FW_UPLOAD_ERR_TIMEOUT] =3D "timeout", + [FW_UPLOAD_ERR_CANCELED] =3D "user-abort", + [FW_UPLOAD_ERR_BUSY] =3D "device-busy", + [FW_UPLOAD_ERR_INVALID_SIZE] =3D "invalid-file-size", + [FW_UPLOAD_ERR_RW_ERROR] =3D "read-write-error", + [FW_UPLOAD_ERR_WEAROUT] =3D "flash-wearout", +}; + +static const char *fw_upload_progress(struct device *dev, + enum fw_upload_prog prog) +{ + const char *status =3D "unknown-status"; + + if (prog < FW_UPLOAD_PROG_MAX) + status =3D fw_upload_prog_str[prog]; + else + dev_err(dev, "Invalid status during secure update: %d\n", prog); + + return status; +} + +static const char *fw_upload_error(struct device *dev, + enum fw_upload_err err_code) +{ + const char *error =3D "unknown-error"; + + if (err_code < FW_UPLOAD_ERR_MAX) + error =3D fw_upload_err_str[err_code]; + else + dev_err(dev, "Invalid error code during secure update: %d\n", + err_code); + + return error; +} + +static ssize_t +status_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct fw_upload_priv *fwlp =3D to_fw_sysfs(dev)->fw_upload_priv; + + return sysfs_emit(buf, "%s\n", fw_upload_progress(dev, fwlp->progress)); +} +DEVICE_ATTR_RO(status); + +static ssize_t +error_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct fw_upload_priv *fwlp =3D to_fw_sysfs(dev)->fw_upload_priv; + int ret; + + mutex_lock(&fwlp->lock); + + if (fwlp->progress !=3D FW_UPLOAD_PROG_IDLE) + ret =3D -EBUSY; + else if (!fwlp->err_code) + ret =3D 0; + else + ret =3D sysfs_emit(buf, "%s:%s\n", + fw_upload_progress(dev, fwlp->err_progress), + fw_upload_error(dev, fwlp->err_code)); + + mutex_unlock(&fwlp->lock); + + return ret; +} +DEVICE_ATTR_RO(error); + +static ssize_t cancel_store(struct device *dev, struct device_attribute *a= ttr, + const char *buf, size_t count) +{ + struct fw_upload_priv *fwlp =3D to_fw_sysfs(dev)->fw_upload_priv; + int ret =3D count; + bool cancel; + + if (kstrtobool(buf, &cancel) || !cancel) + return -EINVAL; + + mutex_lock(&fwlp->lock); + if (fwlp->progress =3D=3D FW_UPLOAD_PROG_IDLE) + ret =3D -ENODEV; + + fwlp->ops->cancel(fwlp->fw_upload); + mutex_unlock(&fwlp->lock); + + return ret; +} +DEVICE_ATTR_WO(cancel); + +static ssize_t remaining_size_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct fw_upload_priv *fwlp =3D to_fw_sysfs(dev)->fw_upload_priv; + + return sysfs_emit(buf, "%u\n", fwlp->remaining_size); +} +DEVICE_ATTR_RO(remaining_size); + +umode_t +fw_upload_is_visible(struct kobject *kobj, struct attribute *attr, int n) +{ + static struct fw_sysfs *fw_sysfs; + + fw_sysfs =3D to_fw_sysfs(kobj_to_dev(kobj)); + + if (fw_sysfs->fw_upload_priv || attr =3D=3D &dev_attr_loading.attr) + return attr->mode; + + return 0; +} + static void fw_upload_update_progress(struct fw_upload_priv *fwlp, enum fw_upload_prog new_progress) { diff --git a/drivers/base/firmware_loader/sysfs_upload.h b/drivers/base/fir= mware_loader/sysfs_upload.h index 18bd4d99f064..9edd47d3f36a 100644 --- a/drivers/base/firmware_loader/sysfs_upload.h +++ b/drivers/base/firmware_loader/sysfs_upload.h @@ -37,6 +37,11 @@ struct fw_upload_priv { }; =20 #ifdef CONFIG_FW_UPLOAD +extern struct device_attribute dev_attr_status; +extern struct device_attribute dev_attr_error; +extern struct device_attribute dev_attr_cancel; +extern struct device_attribute dev_attr_remaining_size; + int fw_upload_start(struct fw_sysfs *fw_sysfs); umode_t fw_upload_is_visible(struct kobject *kobj, struct attribute *attr,= int n); #else --=20 2.25.1 From nobody Sun May 10 22:40:19 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4B3E8C433EF for ; Thu, 21 Apr 2022 21:22:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441996AbiDUVZe (ORCPT ); Thu, 21 Apr 2022 17:25:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1388645AbiDUVZH (ORCPT ); Thu, 21 Apr 2022 17:25:07 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FCED4D62A for ; Thu, 21 Apr 2022 14:22:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1650576136; x=1682112136; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xZi6TKTjcwV3mUOQUK9fpnAfu9BV2UuNn6oIwFVtv3M=; b=DfhBfvqTgQ+Mq025tV6UGR3HlVQwvsNoRbqvrRPX3lQFYlxOYpFf9aLI Zu98aaz04DFVLw8VlTtiG3xHn0YayqnAMfHcOXn9xd43yapg0Yv1V4dMZ FZ1sIVYaXfYLTJ7bW1WjSWtAEc1tExP6BWNZ/iqH0AAWWZl73TXslKdkZ R5MjYB/n+g692HRXgp6CKaZb60EpoiFmwZdOH3sDN3F5XxxbkDKLrCa25 xzvkngdlrHqx8RiQyxkHd+lh5T9pp1fr5wMKzTuB3Ju7O24dz0neT35Dw mjsRdQxTNEUtaZaCFvm+0E8wAnbVqWeyGELeatf8rwTSBMvWLT4CiiS3x g==; X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="350925003" X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="350925003" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:12 -0700 X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="658704099" Received: from rhweight-mobl.amr.corp.intel.com (HELO rhweight-mobl.ra.intel.com) ([10.209.56.239]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:11 -0700 From: Russ Weight To: mcgrof@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, linux-kernel@vger.kernel.org Cc: trix@redhat.com, marpagan@redhat.com, lgoncalv@redhat.com, yilun.xu@intel.com, hao.wu@intel.com, matthew.gerlach@linux.intel.com, basheer.ahmed.muddebihal@intel.com, tianfei.zhang@intel.com, Russ Weight Subject: [PATCH v5 6/8] test_firmware: Add test support for firmware upload Date: Thu, 21 Apr 2022 14:22:02 -0700 Message-Id: <20220421212204.36052-7-russell.h.weight@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220421212204.36052-1-russell.h.weight@intel.com> References: <20220421212204.36052-1-russell.h.weight@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add support for testing the firmware upload driver. There are four sysfs nodes added: upload_register: write-only Write the name of the firmware device node to be created upload_unregister: write-only Write the name of the firmware device node to be destroyed config_upload_name: read/write Set the name to be used by upload_read upload_read: read-only Read back the data associated with the firmware device node named in config_upload_name You can create multiple, concurrent firmware device nodes for firmware upload testing. Read firmware back and validate it using config_upload_name and upload_red. Example: $ cd /sys/devices/virtual/misc/test_firmware $ echo -n fw1 > upload_register $ ls fw1 cancel data device error loading power remaining_size status subsystem uevent $ dd if=3D/dev/urandom of=3D/tmp/random-firmware.bin bs=3D512 count=3D4 4+0 records in 4+0 records out 2048 bytes (2.0 kB, 2.0 KiB) copied, 0.000131959 s, 15.5 MB/s $ echo 1 > fw1/loading $ cat /tmp/random-firmware.bin > fw1/data $ echo 0 > fw1/loading $ cat fw1/status idle $ cat fw1/error $ echo -n fw1 > config_upload_name $ cmp /tmp/random-firmware.bin upload_read $ echo $? 0 $ echo -n fw1 > upload_unregister Reviewed-by: Luis Chamberlain Reviewed-by: Tianfei zhang Tested-by: Matthew Gerlach Signed-off-by: Russ Weight --- v2: - No changes from v1 v3: - Added Reviewed-by tag v4: - Added Reviewed-by tag v5: - Added Tested-by tag --- lib/test_firmware.c | 261 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) diff --git a/lib/test_firmware.c b/lib/test_firmware.c index 1bccd6cd5f48..2b8c56d7bf37 100644 --- a/lib/test_firmware.c +++ b/lib/test_firmware.c @@ -31,9 +31,12 @@ MODULE_IMPORT_NS(TEST_FIRMWARE); #define TEST_FIRMWARE_NAME "test-firmware.bin" #define TEST_FIRMWARE_NUM_REQS 4 #define TEST_FIRMWARE_BUF_SIZE SZ_1K +#define TEST_UPLOAD_MAX_SIZE SZ_2K +#define TEST_UPLOAD_BLK_SIZE 37 /* Avoid powers of two in testing */ =20 static DEFINE_MUTEX(test_fw_mutex); static const struct firmware *test_firmware; +static LIST_HEAD(test_upload_list); =20 struct test_batched_req { u8 idx; @@ -63,6 +66,7 @@ struct test_batched_req { * @reqs: stores all requests information * @read_fw_idx: index of thread from which we want to read firmware resul= ts * from through the read_fw trigger. + * @upload_name: firmware name to be used with upload_read sysfs node * @test_result: a test may use this to collect the result from the call * of the request_firmware*() calls used in their tests. In order of * priority we always keep first any setup error. If no setup errors were @@ -101,6 +105,7 @@ struct test_config { bool send_uevent; u8 num_requests; u8 read_fw_idx; + char *upload_name; =20 /* * These below don't belong her but we'll move them once we create @@ -112,8 +117,28 @@ struct test_config { struct device *device); }; =20 +struct test_firmware_upload { + char *name; + struct list_head node; + char *buf; + size_t size; + bool cancel_request; + struct fw_upload *fwl; +}; + static struct test_config *test_fw_config; =20 +static struct test_firmware_upload *upload_lookup_name(const char *name) +{ + struct test_firmware_upload *tst; + + list_for_each_entry(tst, &test_upload_list, node) + if (strncmp(name, tst->name, strlen(tst->name)) =3D=3D 0) + return tst; + + return NULL; +} + static ssize_t test_fw_misc_read(struct file *f, char __user *buf, size_t size, loff_t *offset) { @@ -198,6 +223,7 @@ static int __test_firmware_config_init(void) test_fw_config->req_firmware =3D request_firmware; test_fw_config->test_result =3D 0; test_fw_config->reqs =3D NULL; + test_fw_config->upload_name =3D NULL; =20 return 0; =20 @@ -277,6 +303,13 @@ static ssize_t config_show(struct device *dev, test_fw_config->sync_direct ? "true" : "false"); len +=3D scnprintf(buf + len, PAGE_SIZE - len, "read_fw_idx:\t%u\n", test_fw_config->read_fw_idx); + if (test_fw_config->upload_name) + len +=3D scnprintf(buf + len, PAGE_SIZE - len, + "upload_name:\t%s\n", + test_fw_config->upload_name); + else + len +=3D scnprintf(buf + len, PAGE_SIZE - len, + "upload_name:\tEMTPY\n"); =20 mutex_unlock(&test_fw_mutex); =20 @@ -392,6 +425,32 @@ static ssize_t config_name_show(struct device *dev, } static DEVICE_ATTR_RW(config_name); =20 +static ssize_t config_upload_name_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct test_firmware_upload *tst; + int ret =3D count; + + mutex_lock(&test_fw_mutex); + tst =3D upload_lookup_name(buf); + if (tst) + test_fw_config->upload_name =3D tst->name; + else + ret =3D -EINVAL; + mutex_unlock(&test_fw_mutex); + + return ret; +} + +static ssize_t config_upload_name_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + return config_test_show_str(buf, test_fw_config->upload_name); +} +static DEVICE_ATTR_RW(config_upload_name); + static ssize_t config_num_requests_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) @@ -989,6 +1048,167 @@ ssize_t trigger_batched_requests_async_store(struct = device *dev, } static DEVICE_ATTR_WO(trigger_batched_requests_async); =20 +static void upload_release(struct test_firmware_upload *tst) +{ + firmware_upload_unregister(tst->fwl); + kfree(tst->buf); + kfree(tst->name); + kfree(tst); +} + +static void upload_release_all(void) +{ + struct test_firmware_upload *tst, *tmp; + + list_for_each_entry_safe(tst, tmp, &test_upload_list, node) { + list_del(&tst->node); + upload_release(tst); + } + test_fw_config->upload_name =3D NULL; +} + +static enum fw_upload_err test_fw_upload_prepare(struct fw_upload *fwl, + const u8 *data, u32 size) +{ + struct test_firmware_upload *tst =3D fwl->dd_handle; + + tst->cancel_request =3D false; + + if (!size || size > TEST_UPLOAD_MAX_SIZE) + return FW_UPLOAD_ERR_INVALID_SIZE; + + memset(tst->buf, 0, TEST_UPLOAD_MAX_SIZE); + tst->size =3D size; + + return FW_UPLOAD_ERR_NONE; +} + +static enum fw_upload_err test_fw_upload_write(struct fw_upload *fwl, + const u8 *data, u32 offset, + u32 size, u32 *written) +{ + struct test_firmware_upload *tst =3D fwl->dd_handle; + u32 blk_size; + + if (tst->cancel_request) + return FW_UPLOAD_ERR_CANCELED; + + blk_size =3D min_t(u32, TEST_UPLOAD_BLK_SIZE, size); + memcpy(tst->buf + offset, data + offset, blk_size); + + *written =3D blk_size; + return FW_UPLOAD_ERR_NONE; +} + +static enum fw_upload_err test_fw_upload_complete(struct fw_upload *fwl) +{ + struct test_firmware_upload *tst =3D fwl->dd_handle; + + if (tst->cancel_request) + return FW_UPLOAD_ERR_CANCELED; + + return FW_UPLOAD_ERR_NONE; +} + +static void test_fw_upload_cancel(struct fw_upload *fwl) +{ + struct test_firmware_upload *tst =3D fwl->dd_handle; + + tst->cancel_request =3D true; +} + +static const struct fw_upload_ops upload_test_ops =3D { + .prepare =3D test_fw_upload_prepare, + .write =3D test_fw_upload_write, + .poll_complete =3D test_fw_upload_complete, + .cancel =3D test_fw_upload_cancel, +}; + +static ssize_t upload_register_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct test_firmware_upload *tst; + struct fw_upload *fwl; + char *name; + int ret; + + name =3D kstrndup(buf, count, GFP_KERNEL); + if (!name) + return -ENOMEM; + + mutex_lock(&test_fw_mutex); + tst =3D upload_lookup_name(name); + if (tst) { + ret =3D -EEXIST; + goto free_name; + } + + tst =3D kzalloc(sizeof(*tst), GFP_KERNEL); + if (!tst) { + ret =3D -ENOMEM; + goto free_name; + } + + tst->name =3D name; + tst->buf =3D kzalloc(TEST_UPLOAD_MAX_SIZE, GFP_KERNEL); + if (!tst->buf) { + ret =3D -ENOMEM; + goto free_tst; + } + + fwl =3D firmware_upload_register(THIS_MODULE, dev, tst->name, + &upload_test_ops, tst); + if (IS_ERR(fwl)) { + ret =3D PTR_ERR(fwl); + goto free_buf; + } + + tst->fwl =3D fwl; + list_add_tail(&tst->node, &test_upload_list); + mutex_unlock(&test_fw_mutex); + return count; + +free_buf: + kfree(tst->buf); + +free_tst: + kfree(tst); + +free_name: + mutex_unlock(&test_fw_mutex); + kfree(name); + + return ret; +} +static DEVICE_ATTR_WO(upload_register); + +static ssize_t upload_unregister_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct test_firmware_upload *tst; + int ret =3D count; + + mutex_lock(&test_fw_mutex); + tst =3D upload_lookup_name(buf); + if (!tst) { + ret =3D -EINVAL; + goto out; + } + + if (test_fw_config->upload_name =3D=3D tst->name) + test_fw_config->upload_name =3D NULL; + + list_del(&tst->node); + upload_release(tst); + +out: + mutex_unlock(&test_fw_mutex); + return ret; +} +static DEVICE_ATTR_WO(upload_unregister); + static ssize_t test_result_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -1051,6 +1271,42 @@ static ssize_t read_firmware_show(struct device *dev, } static DEVICE_ATTR_RO(read_firmware); =20 +static ssize_t upload_read_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct test_firmware_upload *tst; + int ret =3D -EINVAL; + + if (!test_fw_config->upload_name) { + pr_err("Set config_upload_name before using upload_read\n"); + return -EINVAL; + } + + mutex_lock(&test_fw_mutex); + list_for_each_entry(tst, &test_upload_list, node) + if (tst->name =3D=3D test_fw_config->upload_name) + break; + + if (tst->name !=3D test_fw_config->upload_name) { + pr_err("Firmware name not found: %s\n", + test_fw_config->upload_name); + goto out; + } + + if (tst->size > PAGE_SIZE) { + pr_err("Testing interface must use PAGE_SIZE firmware for now\n"); + goto out; + } + + memcpy(buf, tst->buf, tst->size); + ret =3D tst->size; +out: + mutex_unlock(&test_fw_mutex); + return ret; +} +static DEVICE_ATTR_RO(upload_read); + #define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr =20 static struct attribute *test_dev_attrs[] =3D { @@ -1066,6 +1322,7 @@ static struct attribute *test_dev_attrs[] =3D { TEST_FW_DEV_ATTR(config_sync_direct), TEST_FW_DEV_ATTR(config_send_uevent), TEST_FW_DEV_ATTR(config_read_fw_idx), + TEST_FW_DEV_ATTR(config_upload_name), =20 /* These don't use the config at all - they could be ported! */ TEST_FW_DEV_ATTR(trigger_request), @@ -1082,6 +1339,9 @@ static struct attribute *test_dev_attrs[] =3D { TEST_FW_DEV_ATTR(release_all_firmware), TEST_FW_DEV_ATTR(test_result), TEST_FW_DEV_ATTR(read_firmware), + TEST_FW_DEV_ATTR(upload_read), + TEST_FW_DEV_ATTR(upload_register), + TEST_FW_DEV_ATTR(upload_unregister), NULL, }; =20 @@ -1128,6 +1388,7 @@ static void __exit test_firmware_exit(void) mutex_lock(&test_fw_mutex); release_firmware(test_firmware); misc_deregister(&test_fw_misc_device); + upload_release_all(); __test_firmware_config_free(); kfree(test_fw_config); mutex_unlock(&test_fw_mutex); --=20 2.25.1 From nobody Sun May 10 22:40:19 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90A84C433EF for ; Thu, 21 Apr 2022 21:22:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441979AbiDUVZW (ORCPT ); Thu, 21 Apr 2022 17:25:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1441945AbiDUVZG (ORCPT ); Thu, 21 Apr 2022 17:25:06 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 669A54D255 for ; Thu, 21 Apr 2022 14:22:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1650576135; x=1682112135; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=A3kko49KgvW7vKzlVVL7d6lNSZ6+miNVwAF7TG45IYQ=; b=M/wAe6+VP25Wam7GLxMpYIQZOuv9NjycQUo9yK9xC84DHRfWrx32GS6U 0Bv62Z2LjS8k7EKjgWDddFwsVIRCQGauSfjqtxJy8e/Q4jKFDD/WaS+QZ n+Ej5A25EmurcjfQxwNwxo/b31jhMuEq5LjaxPwKjZ9jOwRZszkrrZh9U HfU8faVv/hEyqacnwVSitjLE7YRwm4njDR2hEBcypLqKp3N56eZ0EImA+ jThtb9t6/E6v53SvkqGXlAJPbv19bjexzX/9DlB77WYCljQM5XZzsZwR4 5Y76HvbWOxMxF+WH8R/vVaKPcYlJfQXzs1c/q5JmqZPnwNEbn/AAJ7bLo A==; X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="350925005" X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="350925005" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:13 -0700 X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="658704103" Received: from rhweight-mobl.amr.corp.intel.com (HELO rhweight-mobl.ra.intel.com) ([10.209.56.239]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:12 -0700 From: Russ Weight To: mcgrof@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, linux-kernel@vger.kernel.org Cc: trix@redhat.com, marpagan@redhat.com, lgoncalv@redhat.com, yilun.xu@intel.com, hao.wu@intel.com, matthew.gerlach@linux.intel.com, basheer.ahmed.muddebihal@intel.com, tianfei.zhang@intel.com, Russ Weight Subject: [PATCH v5 7/8] test_firmware: Error injection for firmware upload Date: Thu, 21 Apr 2022 14:22:03 -0700 Message-Id: <20220421212204.36052-8-russell.h.weight@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220421212204.36052-1-russell.h.weight@intel.com> References: <20220421212204.36052-1-russell.h.weight@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add error injection capability to the test_firmware module specifically for firmware upload testing. Error injection instructions are transferred as the first part of the firmware payload. The format of an error injection string is similar to the error strings that may be read from the error sysfs node. To inject the error "programming:hw-error", one would use the error injection string "inject:programming:hw-error" as the firmware payload: $ echo 1 > loading $ echo inject:programming:hw-error > data $ echo 0 > loading $ cat status idle $ cat error programming:hw-error The first part of the error string is the progress state of the upload at the time of the error. The progress state would be one of the following: "preparing", "transferring", or "programming". The second part of the error string is one of the following: "hw-error", "timeout", "device-busy", "invalid-file-size", "read-write-error", "flash-wearout", and "user-abort". Note that all of the error strings except "user-abort" will fail without delay. The "user-abort" error will cause the firmware upload to stall at the requested progress state for up to 5 minutes to allow you to echo 1 to the cancel sysfs node. It is this cancellation that causes the 'user-abort" error. If the upload is not cancelled within the 5 minute time period, then the upload will complete without an error. Reviewed-by: Luis Chamberlain Reviewed-by: Tianfei zhang Tested-by: Matthew Gerlach Signed-off-by: Russ Weight --- v2: - No changes from v1 v3: - Added Reviewed-by tag v4: - Added Reviewed-by tag v5: - Added Tested-by tag --- lib/test_firmware.c | 127 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 122 insertions(+), 5 deletions(-) diff --git a/lib/test_firmware.c b/lib/test_firmware.c index 2b8c56d7bf37..76115c1a2629 100644 --- a/lib/test_firmware.c +++ b/lib/test_firmware.c @@ -117,12 +117,18 @@ struct test_config { struct device *device); }; =20 +struct upload_inject_err { + const char *prog; + enum fw_upload_err err_code; +}; + struct test_firmware_upload { char *name; struct list_head node; char *buf; size_t size; bool cancel_request; + struct upload_inject_err inject; struct fw_upload *fwl; }; =20 @@ -1067,20 +1073,105 @@ static void upload_release_all(void) test_fw_config->upload_name =3D NULL; } =20 +/* + * This table is replicated from .../firmware_loader/sysfs_upload.c + * and needs to be kept in sync. + */ +static const char * const fw_upload_err_str[] =3D { + [FW_UPLOAD_ERR_NONE] =3D "none", + [FW_UPLOAD_ERR_HW_ERROR] =3D "hw-error", + [FW_UPLOAD_ERR_TIMEOUT] =3D "timeout", + [FW_UPLOAD_ERR_CANCELED] =3D "user-abort", + [FW_UPLOAD_ERR_BUSY] =3D "device-busy", + [FW_UPLOAD_ERR_INVALID_SIZE] =3D "invalid-file-size", + [FW_UPLOAD_ERR_RW_ERROR] =3D "read-write-error", + [FW_UPLOAD_ERR_WEAROUT] =3D "flash-wearout", +}; + +static void upload_err_inject_error(struct test_firmware_upload *tst, + const u8 *p, const char *prog) +{ + enum fw_upload_err err; + + for (err =3D FW_UPLOAD_ERR_NONE + 1; err < FW_UPLOAD_ERR_MAX; err++) { + if (strncmp(p, fw_upload_err_str[err], + strlen(fw_upload_err_str[err])) =3D=3D 0) { + tst->inject.prog =3D prog; + tst->inject.err_code =3D err; + return; + } + } +} + +static void upload_err_inject_prog(struct test_firmware_upload *tst, + const u8 *p) +{ + static const char * const progs[] =3D { + "preparing:", "transferring:", "programming:" + }; + int i; + + for (i =3D 0; i < ARRAY_SIZE(progs); i++) { + if (strncmp(p, progs[i], strlen(progs[i])) =3D=3D 0) { + upload_err_inject_error(tst, p + strlen(progs[i]), + progs[i]); + return; + } + } +} + +#define FIVE_MINUTES_MS (5 * 60 * 1000) +static enum fw_upload_err +fw_upload_wait_on_cancel(struct test_firmware_upload *tst) +{ + int ms_delay; + + for (ms_delay =3D 0; ms_delay < FIVE_MINUTES_MS; ms_delay +=3D 100) { + msleep(100); + if (tst->cancel_request) + return FW_UPLOAD_ERR_CANCELED; + } + return FW_UPLOAD_ERR_NONE; +} + static enum fw_upload_err test_fw_upload_prepare(struct fw_upload *fwl, const u8 *data, u32 size) { struct test_firmware_upload *tst =3D fwl->dd_handle; + enum fw_upload_err ret =3D FW_UPLOAD_ERR_NONE; + const char *progress =3D "preparing:"; =20 tst->cancel_request =3D false; =20 - if (!size || size > TEST_UPLOAD_MAX_SIZE) - return FW_UPLOAD_ERR_INVALID_SIZE; + if (!size || size > TEST_UPLOAD_MAX_SIZE) { + ret =3D FW_UPLOAD_ERR_INVALID_SIZE; + goto err_out; + } + + if (strncmp(data, "inject:", strlen("inject:")) =3D=3D 0) + upload_err_inject_prog(tst, data + strlen("inject:")); =20 memset(tst->buf, 0, TEST_UPLOAD_MAX_SIZE); tst->size =3D size; =20 - return FW_UPLOAD_ERR_NONE; + if (tst->inject.err_code =3D=3D FW_UPLOAD_ERR_NONE || + strncmp(tst->inject.prog, progress, strlen(progress)) !=3D 0) + return FW_UPLOAD_ERR_NONE; + + if (tst->inject.err_code =3D=3D FW_UPLOAD_ERR_CANCELED) + ret =3D fw_upload_wait_on_cancel(tst); + else + ret =3D tst->inject.err_code; + +err_out: + /* + * The cleanup op only executes if the prepare op succeeds. + * If the prepare op fails, it must do it's own clean-up. + */ + tst->inject.err_code =3D FW_UPLOAD_ERR_NONE; + tst->inject.prog =3D NULL; + + return ret; } =20 static enum fw_upload_err test_fw_upload_write(struct fw_upload *fwl, @@ -1088,6 +1179,7 @@ static enum fw_upload_err test_fw_upload_write(struct= fw_upload *fwl, u32 size, u32 *written) { struct test_firmware_upload *tst =3D fwl->dd_handle; + const char *progress =3D "transferring:"; u32 blk_size; =20 if (tst->cancel_request) @@ -1097,17 +1189,33 @@ static enum fw_upload_err test_fw_upload_write(stru= ct fw_upload *fwl, memcpy(tst->buf + offset, data + offset, blk_size); =20 *written =3D blk_size; - return FW_UPLOAD_ERR_NONE; + + if (tst->inject.err_code =3D=3D FW_UPLOAD_ERR_NONE || + strncmp(tst->inject.prog, progress, strlen(progress)) !=3D 0) + return FW_UPLOAD_ERR_NONE; + + if (tst->inject.err_code =3D=3D FW_UPLOAD_ERR_CANCELED) + return fw_upload_wait_on_cancel(tst); + + return tst->inject.err_code; } =20 static enum fw_upload_err test_fw_upload_complete(struct fw_upload *fwl) { struct test_firmware_upload *tst =3D fwl->dd_handle; + const char *progress =3D "programming:"; =20 if (tst->cancel_request) return FW_UPLOAD_ERR_CANCELED; =20 - return FW_UPLOAD_ERR_NONE; + if (tst->inject.err_code =3D=3D FW_UPLOAD_ERR_NONE || + strncmp(tst->inject.prog, progress, strlen(progress)) !=3D 0) + return FW_UPLOAD_ERR_NONE; + + if (tst->inject.err_code =3D=3D FW_UPLOAD_ERR_CANCELED) + return fw_upload_wait_on_cancel(tst); + + return tst->inject.err_code; } =20 static void test_fw_upload_cancel(struct fw_upload *fwl) @@ -1117,11 +1225,20 @@ static void test_fw_upload_cancel(struct fw_upload = *fwl) tst->cancel_request =3D true; } =20 +static void test_fw_cleanup(struct fw_upload *fwl) +{ + struct test_firmware_upload *tst =3D fwl->dd_handle; + + tst->inject.err_code =3D FW_UPLOAD_ERR_NONE; + tst->inject.prog =3D NULL; +} + static const struct fw_upload_ops upload_test_ops =3D { .prepare =3D test_fw_upload_prepare, .write =3D test_fw_upload_write, .poll_complete =3D test_fw_upload_complete, .cancel =3D test_fw_upload_cancel, + .cleanup =3D test_fw_cleanup }; =20 static ssize_t upload_register_store(struct device *dev, --=20 2.25.1 From nobody Sun May 10 22:40:19 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00F48C433F5 for ; Thu, 21 Apr 2022 21:22:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441985AbiDUVZZ (ORCPT ); Thu, 21 Apr 2022 17:25:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1441950AbiDUVZI (ORCPT ); Thu, 21 Apr 2022 17:25:08 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D9B44D638 for ; Thu, 21 Apr 2022 14:22:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1650576136; x=1682112136; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EmmHXYijTppqdweAqHFO0Rj5EUmT7b5AnxEE6B8/lIs=; b=XCXungWQh4+prMXF1JdWQ+esCVeXi44Ic6eIQTXKtIrVeHImcXT8D6Gq xqDBYEc2vO/sMm4TjjjGPWgH0n4hEtbEz3ZfaFosJXxsSldIh732rL+53 QzvXFoOo+gQXQ87mMIpU029QBGahi5LRCBn1T61WdFOsbZrXYGFgNcciq ycw7RG6foH/hLhYVIwFsIgiLcQHAR/oeSlY0HIt76enMCwD2iRDsO1Zpx lmR/7O5DKtF2DClRqqVe2D0HwFoSEhYnSuZhp0WoUGeYiFYA51KbOHsV+ hy/8X+kvaNNeEu1JgThZlzqYJQpnRho8sJECGOq80aQd6cNhEgGfcYHo6 g==; X-IronPort-AV: E=McAfee;i="6400,9594,10324"; a="350925011" X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="350925011" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:14 -0700 X-IronPort-AV: E=Sophos;i="5.90,279,1643702400"; d="scan'208";a="658704114" Received: from rhweight-mobl.amr.corp.intel.com (HELO rhweight-mobl.ra.intel.com) ([10.209.56.239]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2022 14:22:13 -0700 From: Russ Weight To: mcgrof@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, linux-kernel@vger.kernel.org Cc: trix@redhat.com, marpagan@redhat.com, lgoncalv@redhat.com, yilun.xu@intel.com, hao.wu@intel.com, matthew.gerlach@linux.intel.com, basheer.ahmed.muddebihal@intel.com, tianfei.zhang@intel.com, Russ Weight Subject: [PATCH v5 8/8] selftests: firmware: Add firmware upload selftests Date: Thu, 21 Apr 2022 14:22:04 -0700 Message-Id: <20220421212204.36052-9-russell.h.weight@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220421212204.36052-1-russell.h.weight@intel.com> References: <20220421212204.36052-1-russell.h.weight@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add selftests to verify the firmware upload mechanism. These test include simple firmware uploads as well as upload cancellation and error injection. The test creates three firmware devices and verifies that they all work correctly and independently. Reviewed-by: Luis Chamberlain Reviewed-by: Tianfei zhang Tested-by: Matthew Gerlach Signed-off-by: Russ Weight --- v2: - No changes from v1 v3: - Added Reviewed-by tag v4: - Added Reviewed-by tag v5: - Added Tested-by tag --- tools/testing/selftests/firmware/Makefile | 2 +- tools/testing/selftests/firmware/config | 1 + tools/testing/selftests/firmware/fw_lib.sh | 7 + .../selftests/firmware/fw_run_tests.sh | 4 + tools/testing/selftests/firmware/fw_upload.sh | 214 ++++++++++++++++++ 5 files changed, 227 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/firmware/fw_upload.sh diff --git a/tools/testing/selftests/firmware/Makefile b/tools/testing/self= tests/firmware/Makefile index 40211cd8f0e6..7992969deaa2 100644 --- a/tools/testing/selftests/firmware/Makefile +++ b/tools/testing/selftests/firmware/Makefile @@ -4,7 +4,7 @@ CFLAGS =3D -Wall \ -O2 =20 TEST_PROGS :=3D fw_run_tests.sh -TEST_FILES :=3D fw_fallback.sh fw_filesystem.sh fw_lib.sh +TEST_FILES :=3D fw_fallback.sh fw_filesystem.sh fw_upload.sh fw_lib.sh TEST_GEN_FILES :=3D fw_namespace =20 include ../lib.mk diff --git a/tools/testing/selftests/firmware/config b/tools/testing/selfte= sts/firmware/config index bf634dda0720..6e402519b117 100644 --- a/tools/testing/selftests/firmware/config +++ b/tools/testing/selftests/firmware/config @@ -3,3 +3,4 @@ CONFIG_FW_LOADER=3Dy CONFIG_FW_LOADER_USER_HELPER=3Dy CONFIG_IKCONFIG=3Dy CONFIG_IKCONFIG_PROC=3Dy +CONFIG_FW_UPLOAD=3Dy diff --git a/tools/testing/selftests/firmware/fw_lib.sh b/tools/testing/sel= ftests/firmware/fw_lib.sh index 5b8c0fedee76..fe8d34dbe7ca 100755 --- a/tools/testing/selftests/firmware/fw_lib.sh +++ b/tools/testing/selftests/firmware/fw_lib.sh @@ -63,6 +63,7 @@ check_setup() HAS_FW_LOADER_USER_HELPER=3D"$(kconfig_has CONFIG_FW_LOADER_USER_HELPER= =3Dy)" HAS_FW_LOADER_USER_HELPER_FALLBACK=3D"$(kconfig_has CONFIG_FW_LOADER_USER= _HELPER_FALLBACK=3Dy)" HAS_FW_LOADER_COMPRESS=3D"$(kconfig_has CONFIG_FW_LOADER_COMPRESS=3Dy)" + HAS_FW_UPLOAD=3D"$(kconfig_has CONFIG_FW_UPLOAD=3Dy)" PROC_FW_IGNORE_SYSFS_FALLBACK=3D"0" PROC_FW_FORCE_SYSFS_FALLBACK=3D"0" =20 @@ -113,6 +114,12 @@ verify_reqs() exit 0 fi fi + if [ "$TEST_REQS_FW_UPLOAD" =3D "yes" ]; then + if [ ! "$HAS_FW_UPLOAD" =3D "yes" ]; then + echo "firmware upload disabled so ignoring test" + exit 0 + fi + fi } =20 setup_tmp_file() diff --git a/tools/testing/selftests/firmware/fw_run_tests.sh b/tools/testi= ng/selftests/firmware/fw_run_tests.sh index 777377078d5e..f6d95a2d5124 100755 --- a/tools/testing/selftests/firmware/fw_run_tests.sh +++ b/tools/testing/selftests/firmware/fw_run_tests.sh @@ -22,6 +22,10 @@ run_tests() proc_set_force_sysfs_fallback $1 proc_set_ignore_sysfs_fallback $2 $TEST_DIR/fw_fallback.sh + + proc_set_force_sysfs_fallback $1 + proc_set_ignore_sysfs_fallback $2 + $TEST_DIR/fw_upload.sh } =20 run_test_config_0001() diff --git a/tools/testing/selftests/firmware/fw_upload.sh b/tools/testing/= selftests/firmware/fw_upload.sh new file mode 100755 index 000000000000..c7a6f06c9adb --- /dev/null +++ b/tools/testing/selftests/firmware/fw_upload.sh @@ -0,0 +1,214 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# This validates the user-initiated fw upload mechanism of the firmware +# loader. It verifies that one or more firmware devices can be created +# for a device driver. It also verifies the data transfer, the +# cancellation support, and the error flows. +set -e + +TEST_REQS_FW_UPLOAD=3D"yes" +TEST_DIR=3D$(dirname $0) + +progress_states=3D"preparing transferring programming" +errors=3D"hw-error + timeout + device-busy + invalid-file-size + read-write-error + flash-wearout" +error_abort=3D"user-abort" +fwname1=3Dfw1 +fwname2=3Dfw2 +fwname3=3Dfw3 + +source $TEST_DIR/fw_lib.sh + +check_mods +check_setup +verify_reqs + +trap "upload_finish" EXIT + +upload_finish() { + local fwdevs=3D"$fwname1 $fwname2 $fwname3" + + for name in $fwdevs; do + if [ -e "$DIR/$name" ]; then + echo -n "$name" > "$DIR"/upload_unregister + fi + done +} + +upload_fw() { + local name=3D"$1" + local file=3D"$2" + + echo 1 > "$DIR"/"$name"/loading + cat "$file" > "$DIR"/"$name"/data + echo 0 > "$DIR"/"$name"/loading +} + +verify_fw() { + local name=3D"$1" + local file=3D"$2" + + echo -n "$name" > "$DIR"/config_upload_name + if ! cmp "$file" "$DIR"/upload_read > /dev/null 2>&1; then + echo "$0: firmware compare for $name did not match" >&2 + exit 1 + fi + + echo "$0: firmware upload for $name works" >&2 + return 0 +} + +inject_error() { + local name=3D"$1" + local status=3D"$2" + local error=3D"$3" + + echo 1 > "$DIR"/"$name"/loading + echo -n "inject":"$status":"$error" > "$DIR"/"$name"/data + echo 0 > "$DIR"/"$name"/loading +} + +await_status() { + local name=3D"$1" + local expected=3D"$2" + local status + local i + + let i=3D0 + while [ $i -lt 50 ]; do + status=3D$(cat "$DIR"/"$name"/status) + if [ "$status" =3D "$expected" ]; then + return 0; + fi + sleep 1e-03 + let i=3D$i+1 + done + + echo "$0: Invalid status: Expected $expected, Actual $status" >&2 + return 1; +} + +await_idle() { + local name=3D"$1" + + await_status "$name" "idle" + return $? +} + +expect_error() { + local name=3D"$1" + local expected=3D"$2" + local error=3D$(cat "$DIR"/"$name"/error) + + if [ "$error" !=3D "$expected" ]; then + echo "Invalid error: Expected $expected, Actual $error" >&2 + return 1 + fi + + return 0 +} + +random_firmware() { + local bs=3D"$1" + local count=3D"$2" + local file=3D$(mktemp -p /tmp uploadfwXXX.bin) + + dd if=3D/dev/urandom of=3D"$file" bs=3D"$bs" count=3D"$count" > /dev/null= 2>&1 + echo "$file" +} + +test_upload_cancel() { + local name=3D"$1" + local status + + for status in $progress_states; do + inject_error $name $status $error_abort + if ! await_status $name $status; then + exit 1 + fi + + echo 1 > "$DIR"/"$name"/cancel + + if ! await_idle $name; then + exit 1 + fi + + if ! expect_error $name "$status":"$error_abort"; then + exit 1 + fi + done + + echo "$0: firmware upload cancellation works" + return 0 +} + +test_error_handling() { + local name=3D$1 + local status + local error + + for status in $progress_states; do + for error in $errors; do + inject_error $name $status $error + + if ! await_idle $name; then + exit 1 + fi + + if ! expect_error $name "$status":"$error"; then + exit 1 + fi + + done + done + echo "$0: firmware upload error handling works" +} + +test_fw_too_big() { + local name=3D$1 + local fw_too_big=3D`random_firmware 512 5` + local expected=3D"preparing:invalid-file-size" + + upload_fw $name $fw_too_big + rm -f $fw_too_big + + if ! await_idle $name; then + exit 1 + fi + + if ! expect_error $name $expected; then + exit 1 + fi + + echo "$0: oversized firmware error handling works" +} + +echo -n "$fwname1" > "$DIR"/upload_register +echo -n "$fwname2" > "$DIR"/upload_register +echo -n "$fwname3" > "$DIR"/upload_register + +test_upload_cancel $fwname1 +test_error_handling $fwname1 +test_fw_too_big $fwname1 + +fw_file1=3D`random_firmware 512 4` +fw_file2=3D`random_firmware 512 3` +fw_file3=3D`random_firmware 512 2` + +upload_fw $fwname1 $fw_file1 +upload_fw $fwname2 $fw_file2 +upload_fw $fwname3 $fw_file3 + +verify_fw ${fwname1} ${fw_file1} +verify_fw ${fwname2} ${fw_file2} +verify_fw ${fwname3} ${fw_file3} + +echo -n "$fwname1" > "$DIR"/upload_unregister +echo -n "$fwname2" > "$DIR"/upload_unregister +echo -n "$fwname3" > "$DIR"/upload_unregister + +exit 0 --=20 2.25.1