From nobody Fri Oct 3 07:49:45 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550851142437912.364203303223; Fri, 22 Feb 2019 07:59:02 -0800 (PST) Received: from localhost ([127.0.0.1]:53333 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gxDE2-0002s7-75 for importer@patchew.org; Fri, 22 Feb 2019 10:58:58 -0500 Received: from eggs.gnu.org ([209.51.188.92]:59457) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gxD0X-0007jU-OU for qemu-devel@nongnu.org; Fri, 22 Feb 2019 10:45:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gxD0W-0003rp-Nn for qemu-devel@nongnu.org; Fri, 22 Feb 2019 10:45:01 -0500 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]:51189) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gxD0W-0003pp-F6 for qemu-devel@nongnu.org; Fri, 22 Feb 2019 10:45:00 -0500 Received: by mail-wm1-x342.google.com with SMTP id x7so2380124wmj.0 for ; Fri, 22 Feb 2019 07:45:00 -0800 (PST) Received: from zen.linaroharston ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id o30sm1938577wro.57.2019.02.22.07.44.58 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Feb 2019 07:44:58 -0800 (PST) Received: from zen.linaroharston. (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id CFDD31FF80; Fri, 22 Feb 2019 15:44:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3TzO2ZOlm3C1u2ya0SGIOxLyr124VZ0Y+amZluTXP+A=; b=HlrJIJaT66ZwdqRZjdAEpnv53uxPMutTugRjuu4ARlpPAIX88YrUv2doMD38VNgatr 8B1u+Nm6VkSZWE6f0OkJ+LWdWiuvLx9x/WpMmcnHTx/aOQnke6XnkLSQHpn2bDUAjQG3 tN0ynHblgWlSqyn5U7t47FGf1CqgOrfTu3fezRH0rgfm4KGTUh5KzjUonkeAJNJCSbUk bX5lqZji0NHY0RoLb546rD4OjnbMEaDvx1k9c3wrHDd76D7HJj2TXZhdwC9RYWd1IGjB dlsi/gTsLhCEcKp/NdXg8470MeoaMtP7Pb0Lfe/8mARj++zy5P44CYl7VAUssszoFgQ7 MtLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=3TzO2ZOlm3C1u2ya0SGIOxLyr124VZ0Y+amZluTXP+A=; b=SazR+GNg3t5rKxXjZn/2E38PhrZNRSgBL0lvRY9+9/uJJSK8/BfG7OaH0aWZ91GSEm 4OkcGMJAOJxZb7zeTRElJCDTmjy73gs9NlJzt5gM9ksIWOFVn053s9+0vvkhuFyXCCYk +nSKzQligBthY/7cDxuC+m71D1VCDR/+iTiHi+OiNnh4dVAqOja4o5833vb1K9h/w1k+ Qf2He7Dr6dg6GEtjc+q2De6VTb/vflwIR+q2pbeXeMQcnmO+CC5YopGbmYI1GUcBUiO7 JU6K53RRvtyXDuqsKfBT9ntBIDOjPGK/ALZ4f+KP25/ty+5vU5VPX1aDOVPIGUmRIGMa PhPg== X-Gm-Message-State: AHQUAuY6U7+FLVcDJuxeMv1Zp2mC1hJ5v2moSDCLcCxQ2adrIqWUs4GV 0sx4qeSmc4E1CUyeVqzdOKZ1MA== X-Google-Smtp-Source: AHgI3IaCpvq1CMULOwgT7R/pw5V9n5HCCtpQlCFwA+Da5r6y/9OsUgvZTK2P1PnzjEkebcOVWXQ+6g== X-Received: by 2002:a1c:44d7:: with SMTP id r206mr2989653wma.40.1550850298871; Fri, 22 Feb 2019 07:44:58 -0800 (PST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Date: Fri, 22 Feb 2019 15:44:54 +0000 Message-Id: <20190222154454.30289-1-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::342 Subject: [Qemu-devel] [PATCH v4] hw/block: better reporting on pflash backing file mismatch X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: stappers@stappers.nl, =?UTF-8?q?Alex=20Benn=C3=A9e?= , lersek@redhat.com, armbru@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) It looks like there was going to be code to check we had some sort of alignment so lets replace it with an actual check. This is a bit more useful than the enigmatic "failed to read the initial flash content" when we attempt to read the number of bytes the device should have. This is a potential confusing stumbling block when you move from using -bios to using -drive if=3Dpflash,file=3Dblob,format=3Draw,readonly for loading your firmware code. To mitigate that we automatically pad in the read-only case and warn the user when we have performed magic to enable things to Just Work (tm). Signed-off-by: Alex Benn=C3=A9e Reviewed-by: Laszlo Ersek --- v3 - tweak commit title/commentary - use total_len instead of device_len for checks - if the device is read-only do the padding for them - accept baking_len > total_len (how to warn_report with NULL *errp?) v4 - error check blk_getlength - optimise memset and use NOR erase pattern - restore singular device (overly confusing) - add warn_report for when we do magic --- hw/block/pflash_cfi01.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index 00c2efd0d7..c69ecc20a0 100644 --- a/hw/block/pflash_cfi01.c +++ b/hw/block/pflash_cfi01.c @@ -45,6 +45,7 @@ #include "qemu/bitops.h" #include "qemu/host-utils.h" #include "qemu/log.h" +#include "qemu/error-report.h" #include "hw/sysbus.h" #include "sysemu/sysemu.h" #include "trace.h" @@ -714,13 +715,6 @@ static void pflash_cfi01_realize(DeviceState *dev, Err= or **errp) } device_len =3D sector_len_per_device * blocks_per_device; =20 - /* XXX: to be fixed */ -#if 0 - if (total_len !=3D (8 * 1024 * 1024) && total_len !=3D (16 * 1024 * 10= 24) && - total_len !=3D (32 * 1024 * 1024) && total_len !=3D (64 * 1024 * 1= 024)) - return NULL; -#endif - memory_region_init_rom_device( &pfl->mem, OBJECT(dev), &pflash_cfi01_ops, @@ -747,6 +741,38 @@ static void pflash_cfi01_realize(DeviceState *dev, Err= or **errp) } =20 if (pfl->blk) { + /* + * Validate the backing store is the right size for pflash + * devices. It should be padded to a multiple of the flash + * block size. If the device is read-only we can elide the + * check and just null pad the region first. If the user + * supplies a larger file we ignore the tail. + */ + int64_t backing_len =3D blk_getlength(pfl->blk); + if (backing_len < 0) { + error_setg(errp, "unable to check size of backing file"); + return; + } + + if (backing_len < total_len) { + if (pfl->ro) { + size_t pad_bytes =3D total_len - backing_len; + /* pad with NOR erase pattern */ + memset((uint8_t*)pfl->storage + backing_len, 0xff, pad_byt= es); + warn_report("device needs %" PRIu64 + " bytes, padded with %zd 0xff bytes", + total_len, pad_bytes); + total_len =3D backing_len; + } else { + error_setg(errp, "device needs %" PRIu64 " bytes, " + "backing file provides only %" PRIu64 " bytes", + total_len, backing_len); + return; + } + } else if (backing_len > total_len) { + warn_report("device needs %" PRIu64 " bytes, rest ignored", to= tal_len); + } + /* read the initial flash content */ ret =3D blk_pread(pfl->blk, 0, pfl->storage, total_len); =20 --=20 2.20.1