Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1569078pxb; Wed, 10 Feb 2021 11:20:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJyFwXXKM4o9n9IBjsF5YDOi8wB4TN3onZS8AoKu4iWUxnGA+7YeiqYiq+ZqhUE/3MkmwRC1 X-Received: by 2002:a17:906:3105:: with SMTP id 5mr4610730ejx.168.1612984851346; Wed, 10 Feb 2021 11:20:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612984851; cv=none; d=google.com; s=arc-20160816; b=g0iFE76njZwX8lcbsrHMUtOOe3OU5Kqhfo6wdmiw0VevIF+0i2lW/Eidu2ws1BIci9 BnO3wUWdQYo4Rms7rch3EbcUYF5/6OwLIPOFj2L0R1x+ZEbL+X0NYwcxQ7nSls+bkrHR Xrq0s7lAZDdZ1Zoq6526a2bxMRCswXnuBS23YK46Foy5Erooaw57wXN55GcqF7OjHlPE awqp/g7aOq/4drwF8+/o+3JF51WfTVYACQJ+JRpIhzdW6xc3fPjtdglnCu61mxw5Ml01 mf26VWunYu/yZ8p7GWOyWAjgCSVY9+Kw+zFObQlOXJwUxknsmZqmAuZtajWEV1XeYojD CLMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:to:from:dkim-signature; bh=EU3dNuG0S8VA647wcuwwXTHc4mfh7zoiNOfTHPKD8M8=; b=a0jJQEX0Xl3l3J/sdyHfre9s4oS9dBoOhpmNnyEJRvS1qhNajR9FRdozfmRx0B/HEO dvficFfNYq9Wr6mK0HmC+E5BXYESDoUrjr4XZZN6EnNYIEx2tjQGHKdUV2cFY27Ru47g mEe2z2KDhGGpnHMV7NKjJ/6Sf33qU/9wkpeWUr5drD8YWbEUHvxXO3lSqAjod9pWxwt/ 415WIAkY3piPcsIDUNkZjXghB71T34bnUQIOr5kNIr1C+8AVXtCIShm+kdkE86ERNIs8 6bi95111pvVYN6Is5ssMaHGant6P9SKScND54JKUvmwQvfth+ubsE17DA75GecaDa92B 622g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=BbLGsDnQ; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w26si2059163edt.21.2021.02.10.11.20.24; Wed, 10 Feb 2021 11:20:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=BbLGsDnQ; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234621AbhBJTS4 (ORCPT + 99 others); Wed, 10 Feb 2021 14:18:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233224AbhBJTSw (ORCPT ); Wed, 10 Feb 2021 14:18:52 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46C4BC061574 for ; Wed, 10 Feb 2021 11:18:12 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id my11so3385821pjb.1 for ; Wed, 10 Feb 2021 11:18:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=EU3dNuG0S8VA647wcuwwXTHc4mfh7zoiNOfTHPKD8M8=; b=BbLGsDnQ6n5nPt8LJ8X37JmY+ie8jZ22jAyDbuE3Lj0/JHrxjIHBrzNwTgZC7pAAyd mICd8qmWkd7mjnt3DMPDJEdpW47h7iE2xhOwF9pxEAtPtHtNAvMbJgBTscr5X+Ey2ttW k8LQf8NiRDLsyeDJek+eqESAXzVsvOI8dCjRUbljFvGhKSj7xXUfu8rIrTjlOeG7xzXH R/JkqXFabs6zisHNL6sWF/6pMnStBUPoyvf9pl6+4zvgCQoJZMl0rljZ48un31nZpg/T OjTes9h1U/ELiowt55wHa3Cb//HpuR4pUhpz9+728LaAe2QVkIZUNFCtZkk36rYCl6zo jyhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=EU3dNuG0S8VA647wcuwwXTHc4mfh7zoiNOfTHPKD8M8=; b=WsDpa0LoKghBgfGBU51BbPDXR3gdihjfaOFDJfVJCHxfp6JV9dtkoxgSl9QXEAyllf TWsfLN7vJRb3pJSbH9Dq5UCWl3y/TkR24vn/ineQpUULtFl2nH/q8emH/pH7GH8Qef/t XkkiElyIKFmuVit6BGHJ8zmOOyGb5Qa6BJcKLWpp/lR6mlc8us3sFCMhb8+hoHEqmAaL s4Kn+lJxMvzjOtH+FmZKmUbiSzQ3I55sPTAWocs0PYDSRcH6U90C0uGdQhqcbs+kLz78 70IejWs9GHKuPrq12xql9S/ifFw8ieJ0NXRntUNhBaVqr4y/ffbDSoL8VVgRiJLKYuQl 6IHQ== X-Gm-Message-State: AOAM5326U/aHkzgnjZEWUuIsRCypZmYUGh08vv9DUtp6cCvi4CUCS0S0 GVZD/hQWEWXseJZqEtu90BlcrRBYLBMiyg== X-Received: by 2002:a17:90a:5d0d:: with SMTP id s13mr395070pji.156.1612984691175; Wed, 10 Feb 2021 11:18:11 -0800 (PST) Received: from localhost.localdomain (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id 125sm2988822pfu.7.2021.02.10.11.18.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Feb 2021 11:18:10 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v5 1/6] Bluetooth: btintel: Check firmware version before download Date: Wed, 10 Feb 2021 11:18:04 -0800 Message-Id: <20210210191809.2181630-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This checks the firmware build number, week and year matches with repective version loaded and then skip the download process. Signed-off-by: Luiz Augusto von Dentz --- v2: Add patch that mover checks for operational mode after the version checking. v3: Fix not checking for operation mode before using btintel_read_boot_params since some models depend on that to contruct the fw filename. Also attempt to cleanup duplicated code. v4: Fix forwarding -EALREADY when firmware has already been loaded. v5: Fix not advancing fw_ptr. drivers/bluetooth/btintel.c | 94 +++++++++++++++++++++++++++-------- drivers/bluetooth/btintel.h | 5 +- drivers/bluetooth/btusb.c | 18 ++++++- drivers/bluetooth/hci_intel.c | 7 ++- 4 files changed, 98 insertions(+), 26 deletions(-) diff --git a/drivers/bluetooth/btintel.c b/drivers/bluetooth/btintel.c index 88ce5f0ffc4b..96bca89d1b99 100644 --- a/drivers/bluetooth/btintel.c +++ b/drivers/bluetooth/btintel.c @@ -24,6 +24,14 @@ #define ECDSA_OFFSET 644 #define ECDSA_HEADER_LEN 320 +#define CMD_WRITE_BOOT_PARAMS 0xfc0e +struct cmd_write_boot_params { + u32 boot_addr; + u8 fw_build_num; + u8 fw_build_ww; + u8 fw_build_yy; +} __packed; + int btintel_check_bdaddr(struct hci_dev *hdev) { struct hci_rp_read_bd_addr *bda; @@ -841,7 +849,7 @@ static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev, static int btintel_download_firmware_payload(struct hci_dev *hdev, const struct firmware *fw, - u32 *boot_param, size_t offset) + size_t offset) { int err; const u8 *fw_ptr; @@ -854,20 +862,6 @@ static int btintel_download_firmware_payload(struct hci_dev *hdev, while (fw_ptr - fw->data < fw->size) { struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len); - /* Each SKU has a different reset parameter to use in the - * HCI_Intel_Reset command and it is embedded in the firmware - * data. So, instead of using static value per SKU, check - * the firmware data and save it for later use. - */ - if (le16_to_cpu(cmd->opcode) == 0xfc0e) { - /* The boot parameter is the first 32-bit value - * and rest of 3 octets are reserved. - */ - *boot_param = get_unaligned_le32(fw_ptr + sizeof(*cmd)); - - bt_dev_dbg(hdev, "boot_param=0x%x", *boot_param); - } - frag_len += sizeof(*cmd) + cmd->plen; /* The parameter length of the secure send command requires @@ -896,28 +890,90 @@ static int btintel_download_firmware_payload(struct hci_dev *hdev, return err; } +static bool btintel_firmware_version(struct hci_dev *hdev, + u8 num, u8 ww, u8 yy, + const struct firmware *fw, + u32 *boot_addr) +{ + const u8 *fw_ptr; + + fw_ptr = fw->data; + + while (fw_ptr - fw->data < fw->size) { + struct hci_command_hdr *cmd = (void *)(fw_ptr); + + /* Each SKU has a different reset parameter to use in the + * HCI_Intel_Reset command and it is embedded in the firmware + * data. So, instead of using static value per SKU, check + * the firmware data and save it for later use. + */ + if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) { + struct cmd_write_boot_params *params; + + params = (void *)(fw_ptr + sizeof(*cmd)); + + bt_dev_info(hdev, "Boot Address: 0x%x", + le32_to_cpu(params->boot_addr)); + + bt_dev_info(hdev, "Firmware Version: %u-%u.%u", + params->fw_build_num, params->fw_build_ww, + params->fw_build_yy); + + return (num == params->fw_build_num && + ww == params->fw_build_ww && + yy == params->fw_build_yy); + } + + fw_ptr += sizeof(*cmd) + cmd->plen; + } + + return false; +} + int btintel_download_firmware(struct hci_dev *hdev, + struct intel_version *ver, const struct firmware *fw, u32 *boot_param) { int err; + /* Skip download if firmware has the same version */ + if (btintel_firmware_version(hdev, ver->fw_build_num, ver->fw_build_ww, + ver->fw_build_yy, fw, boot_param)) { + bt_dev_info(hdev, "Firmware already loaded"); + /* Return -EALREADY to indicate that the firmware has already + * been loaded. + */ + return -EALREADY; + } + err = btintel_sfi_rsa_header_secure_send(hdev, fw); if (err) return err; - return btintel_download_firmware_payload(hdev, fw, boot_param, - RSA_HEADER_LEN); + return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN); } EXPORT_SYMBOL_GPL(btintel_download_firmware); int btintel_download_firmware_newgen(struct hci_dev *hdev, + struct intel_version_tlv *ver, const struct firmware *fw, u32 *boot_param, u8 hw_variant, u8 sbe_type) { int err; u32 css_header_ver; + /* Skip download if firmware has the same version */ + if (btintel_firmware_version(hdev, ver->min_fw_build_nn, + ver->min_fw_build_cw, ver->min_fw_build_yy, + fw, boot_param)) { + bt_dev_info(hdev, "Firmware already loaded"); + /* Return -EALREADY to indicate that firmware has already been + * loaded. + */ + return -EALREADY; + } + /* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support * only RSA secure boot engine. Hence, the corresponding sfi file will * have RSA header of 644 bytes followed by Command Buffer. @@ -947,7 +1003,7 @@ int btintel_download_firmware_newgen(struct hci_dev *hdev, if (err) return err; - err = btintel_download_firmware_payload(hdev, fw, boot_param, RSA_HEADER_LEN); + err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN); if (err) return err; } else if (hw_variant >= 0x17) { @@ -968,7 +1024,6 @@ int btintel_download_firmware_newgen(struct hci_dev *hdev, return err; err = btintel_download_firmware_payload(hdev, fw, - boot_param, RSA_HEADER_LEN + ECDSA_HEADER_LEN); if (err) return err; @@ -978,7 +1033,6 @@ int btintel_download_firmware_newgen(struct hci_dev *hdev, return err; err = btintel_download_firmware_payload(hdev, fw, - boot_param, RSA_HEADER_LEN + ECDSA_HEADER_LEN); if (err) return err; diff --git a/drivers/bluetooth/btintel.h b/drivers/bluetooth/btintel.h index 6511b091caf5..51f1f2c883b4 100644 --- a/drivers/bluetooth/btintel.h +++ b/drivers/bluetooth/btintel.h @@ -163,9 +163,10 @@ struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read, int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param); int btintel_read_boot_params(struct hci_dev *hdev, struct intel_boot_params *params); -int btintel_download_firmware(struct hci_dev *dev, const struct firmware *fw, - u32 *boot_param); +int btintel_download_firmware(struct hci_dev *dev, struct intel_version *ver, + const struct firmware *fw, u32 *boot_param); int btintel_download_firmware_newgen(struct hci_dev *hdev, + struct intel_version_tlv *ver, const struct firmware *fw, u32 *boot_param, u8 hw_variant, u8 sbe_type); diff --git a/drivers/bluetooth/btusb.c b/drivers/bluetooth/btusb.c index 66ada8217797..c92060e7472c 100644 --- a/drivers/bluetooth/btusb.c +++ b/drivers/bluetooth/btusb.c @@ -2623,10 +2623,17 @@ static int btusb_intel_download_firmware_newgen(struct hci_dev *hdev, set_bit(BTUSB_DOWNLOADING, &data->flags); /* Start firmware downloading and get boot parameter */ - err = btintel_download_firmware_newgen(hdev, fw, boot_param, + err = btintel_download_firmware_newgen(hdev, ver, fw, boot_param, INTEL_HW_VARIANT(ver->cnvi_bt), ver->sbe_type); if (err < 0) { + if (err == -EALREADY) { + /* Firmware has already been loaded */ + set_bit(BTUSB_FIRMWARE_LOADED, &data->flags); + err = 0; + goto done; + } + /* When FW download fails, send Intel Reset to retry * FW download. */ @@ -2817,8 +2824,15 @@ static int btusb_intel_download_firmware(struct hci_dev *hdev, set_bit(BTUSB_DOWNLOADING, &data->flags); /* Start firmware downloading and get boot parameter */ - err = btintel_download_firmware(hdev, fw, boot_param); + err = btintel_download_firmware(hdev, ver, fw, boot_param); if (err < 0) { + if (err == -EALREADY) { + /* Firmware has already been loaded */ + set_bit(BTUSB_FIRMWARE_LOADED, &data->flags); + err = 0; + goto done; + } + /* When FW download fails, send Intel Reset to retry * FW download. */ diff --git a/drivers/bluetooth/hci_intel.c b/drivers/bluetooth/hci_intel.c index b20a40fab83e..7249b91d9b91 100644 --- a/drivers/bluetooth/hci_intel.c +++ b/drivers/bluetooth/hci_intel.c @@ -735,7 +735,7 @@ static int intel_setup(struct hci_uart *hu) set_bit(STATE_DOWNLOADING, &intel->flags); /* Start firmware downloading and get boot parameter */ - err = btintel_download_firmware(hdev, fw, &boot_param); + err = btintel_download_firmware(hdev, &ver, fw, &boot_param); if (err < 0) goto done; @@ -784,7 +784,10 @@ static int intel_setup(struct hci_uart *hu) done: release_firmware(fw); - if (err < 0) + /* Check if there was an error and if is not -EALREADY which means the + * firmware has already been loaded. + */ + if (err < 0 && err != -EALREADY) return err; /* We need to restore the default speed before Intel reset */ -- 2.26.2