From patchwork Thu Feb 21 18:48:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 158929 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp826409jaa; Thu, 21 Feb 2019 10:51:17 -0800 (PST) X-Google-Smtp-Source: AHgI3IZiq5ohcu2TI3TFLW38shcx6nKq9KgR1t5IUiCZg6dlFxv58DqMmJoU+znDTCoKAovPnW9y X-Received: by 2002:a0d:d595:: with SMTP id x143mr20141601ywd.406.1550775077109; Thu, 21 Feb 2019 10:51:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550775077; cv=none; d=google.com; s=arc-20160816; b=zUUIl2zcBZ6r0sz9dPHER7qCkvw251jKO4Zsi2QY+CK+G0rZ3ebnLtwJOXWR3xNNkD ods4SdbEOYATXodhT4BuggNbXYs9LqBIRndqhvcezcX27iSgas5bbeK4SwopdcO0ffvP VphFvQzVpXud7hv+1VcnBCSMpx+DzjWiPRycLvnR2Wazfym0D/fLDiyY3HSNM94i+6bg plht15HFrdBtjYKrQq6yzUY8YExOrxW30KA7hlN/Np1/sZYfTezKfrQufmiWuRa6Ml3X gIMqDhk31M6IDDpoD4MWVn/MDSi0NYwcunUifqg7Tsgrc+YW/H2Wa3T4/EE2w4t+yJqs sIsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:message-id:date:to:from :dkim-signature; bh=+RSB+PD024ov3tcn94kfmu7kejcaBvwIj3dxVbt/pVc=; b=a/sGJ7N54h6ydfGlN/HzTMJhTsDumKpNQPerNgLe5P0vg9NcAENF/J8qYzjzS8Ttch EYBu7WBxCkB2nCzimOJW2jUy11/ftr25NfRHNK53MWd4PDpkr1ElrzAseelASWNPqsgc Dit7J46L9Wnlbcwxc3OJ9uyfnuF9lKAK3orowZPjh2iUb9L+86qfqy6ErstH0jMnSrzM 0shUczuZvdiVDXbqR7M2nHd2fgzPq2M3odQcnDW3V2HPGrbPENYKTKiZHuFiz4fPOiuG /mPO6LMhDENdu8CTcWIsp9Scs8ThGPMnpu7bWhrDXcPH8It2TRIj4/w4AIiXR9u9f/26 oBbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=lOLWaDom; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id y77si13118546yby.305.2019.02.21.10.51.17 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 21 Feb 2019 10:51:17 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=lOLWaDom; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1]:36736 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gwtRE-0001Nt-LT for patch@linaro.org; Thu, 21 Feb 2019 13:51:16 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50041) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gwtP7-0000Bw-LA for qemu-devel@nongnu.org; Thu, 21 Feb 2019 13:49:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gwtP6-0002NE-Rk for qemu-devel@nongnu.org; Thu, 21 Feb 2019 13:49:05 -0500 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]:38796) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gwtP6-0002ML-Ev for qemu-devel@nongnu.org; Thu, 21 Feb 2019 13:49:04 -0500 Received: by mail-wr1-x444.google.com with SMTP id v13so31763886wrw.5 for ; Thu, 21 Feb 2019 10:49:04 -0800 (PST) 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=+RSB+PD024ov3tcn94kfmu7kejcaBvwIj3dxVbt/pVc=; b=lOLWaDomJG5manJBGrJH0BJ9FxKdFgU7xIGjWrlPOrLth72d272+qR2BidrprIHR6F j/mTYtBstqd0KW5KRw+ggBPorNg/7Y8M2pPS+O5OD91oE98CCANVGNgcjoUIYJhaPfJp MuN527k7PlxllFEael/cSmcMxjqbxeOo73nZAoewSzAhdE1WKDBo3NF6TJNiHZxBjfY3 z2Sn+8trSd7oCbLdr/X4BL0/oBZrb9efTMM/Nlzv+Z67jowVpYCPX9Ygq1aAgh3H/pMB lI/5WoZ9h75J3/lSoy8+UkVtYz5XwFK2rnsgIiAmbZ7/Nmw8M7Reu8ZUW8IIbjUEWtGo j8vQ== 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=+RSB+PD024ov3tcn94kfmu7kejcaBvwIj3dxVbt/pVc=; b=ZdgacxTvir0go/bdq4qwzo1PmpEocCqCJGvKfPZylgFpN3NmFAvqMPYT3K530u16Zl ri7eDem1zPhWmcCKR6KPLIFlnAMKTQLhkyd1h8jEpHOrQL/ACDi3NPBtX0xvQ4KcvgMu seq/igQU/p24W6DK8gTWFs4DJeszYT92Su/Jcb/xvgMUdvu4d0MeZ0pco1ufmPYKEyJL gzW73XFSllKdS4+EB1vqmXANBTlHXZhFqtrR4EJwy1nYjReNiaBXfYmkpcGi9M4DU7ZL OxA64SdmPTYexOYbw+a0PvqlsULE+PwWPFGrNWUv3dzolxo+1rz0R8edc1D04xbCZb8v XBWg== X-Gm-Message-State: AHQUAuZQUecS+TcVjZYoyAS1r702OWEWUYPVTpHNwB4Xd+gWagg9Ph60 6Mkr+8LCoY7psBMRHV5K1kXq9g== X-Received: by 2002:a5d:68c2:: with SMTP id p2mr27866437wrw.23.1550774943021; Thu, 21 Feb 2019 10:49:03 -0800 (PST) Received: from zen.linaroharston ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id n11sm17630034wrw.60.2019.02.21.10.49.02 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 21 Feb 2019 10:49:02 -0800 (PST) Received: from zen.linaroharston. (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 005561FF80; Thu, 21 Feb 2019 18:49:01 +0000 (UTC) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Date: Thu, 21 Feb 2019 18:48:57 +0000 Message-Id: <20190221184857.22434-1-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::444 Subject: [Qemu-devel] [PATCH v3] 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_Benn=C3=A9e?= , lersek@redhat.com, armbru@redhat.com Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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=pflash,file=blob,format=raw,readonly for loading your firmware code. To mitigate that we automatically pad in the read-only case. Signed-off-by: Alex Bennée --- 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?) --- hw/block/pflash_cfi01.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) -- 2.20.1 diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index 00c2efd0d7..37d7513c45 100644 --- a/hw/block/pflash_cfi01.c +++ b/hw/block/pflash_cfi01.c @@ -714,13 +714,6 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp) } device_len = sector_len_per_device * blocks_per_device; - /* XXX: to be fixed */ -#if 0 - if (total_len != (8 * 1024 * 1024) && total_len != (16 * 1024 * 1024) && - total_len != (32 * 1024 * 1024) && total_len != (64 * 1024 * 1024)) - return NULL; -#endif - memory_region_init_rom_device( &pfl->mem, OBJECT(dev), &pflash_cfi01_ops, @@ -747,6 +740,27 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp) } 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 silently accept it. + */ + uint64_t backing_len = blk_getlength(pfl->blk); + + if (backing_len < total_len) { + if (pfl->ro) { + memset(pfl->storage, 0, total_len); + total_len = backing_len; + } else { + error_setg(errp, "device(s) needs %" PRIu64 " bytes, " + "backing file provides only %" PRIu64 " bytes", + total_len, backing_len); + return; + } + } + /* read the initial flash content */ ret = blk_pread(pfl->blk, 0, pfl->storage, total_len);