From patchwork Wed Feb 27 11:13:47 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: 159280 Delivered-To: patch@linaro.org Received: by 2002:a02:5cc1:0:0:0:0:0 with SMTP id w62csp4316910jad; Wed, 27 Feb 2019 03:36:37 -0800 (PST) X-Google-Smtp-Source: AHgI3IZzHIRs+J4LSjY7aSf19LJvFPYAz4vmlsc+e8md1aKCDwLDiY/xdHYissOM8EEgMRqZSwvB X-Received: by 2002:a25:9988:: with SMTP id p8mr1481529ybo.127.1551267397582; Wed, 27 Feb 2019 03:36:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551267397; cv=none; d=google.com; s=arc-20160816; b=Mt2i2t9nxASQI5XlNGflUUwPTrUQ0luDCIU9whyeosRhndK3VvroXf7sqPerZx9+y1 UyaNgzmlPl64lRFkVJByYUkBRcGyeg3UwtWAgRDioAIHEIEM4MFy0UD+I1tx74urjhyB bOUgg8ErpIUp4RbrstAbI+YK9EyhkNGviiwWP4fLznUWy0ZiUItmE/oHzYPiZe1G831R l/KsGuKgez44oB+9R128CGY2qWeyeWxmra9QuaDsCR1OA+FkwHf1yW8Nr/XrjbB7WbN7 7flWWeHBnPRMtzB3BwP5vviyOw7QdXb28c3OhL+yF/Pq2OZCiUMxAr0eW6sE29+nYscw wmQg== 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=ykOljB2vbGcyFYJUaA+gCQdraZEMWCskvExfV6LRvOY=; b=byu5bJfqW1spoRq+45YQ5qWkQrjqTN8zxOWHDhEv23t2IPbtMHe2e0ncXnJbWvKTXA zLEVl2O1UtaufVZED5IFRB2DPX3xmFVkHjXMQiyXCzbkyA9Tdo101c56tbnWYhYwa0Bi zBbRScNorl44vRaAbH3UlAgMGwkPrjyHsNZjG3vEtGJF4r7A7nYlyqjocnC6l91Q7rQd Wr+aMZ6e77a9jEaQ5Rp6wwzVQuh6M7QseKw91umX8/7ErmQ7Z63yGHe1NKhz5OCKR/V2 GcxQ9IGAhOcvQ9zUbp5cafmKy/SEHEftCezlUMCkZ6TWv7mltT8e0JhjO1f6wbAEAH2D xt5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=vkuTAXrP; 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" Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id z62si394038ybb.428.2019.02.27.03.36.37 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 27 Feb 2019 03:36:37 -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=vkuTAXrP; 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" Received: from localhost ([127.0.0.1]:42441 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyxVt-00060m-0j for patch@linaro.org; Wed, 27 Feb 2019 06:36:37 -0500 Received: from eggs.gnu.org ([209.51.188.92]:38797) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gyxP9-0000dN-7c for qemu-devel@nongnu.org; Wed, 27 Feb 2019 06:29:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gyx9z-00063s-VJ for qemu-devel@nongnu.org; Wed, 27 Feb 2019 06:14:01 -0500 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]:53201) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gyx9w-0005yy-2p for qemu-devel@nongnu.org; Wed, 27 Feb 2019 06:13:58 -0500 Received: by mail-wm1-x32e.google.com with SMTP id m1so5442871wml.2 for ; Wed, 27 Feb 2019 03:13:53 -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=ykOljB2vbGcyFYJUaA+gCQdraZEMWCskvExfV6LRvOY=; b=vkuTAXrP6u6T0V8QldDRsScLFXQcqOpsgjOxNx9rhYtX12K8761OUvHxPq+KIBWguh bNyaoaNzS4ddFaE1gUJDfUhAOmnw+pULxwnB9lKf2kfk1k+erIWJUs6pGl4oRPH6My/i cbJIQdxsURCUOyJeCs6h64UeHl0lsSeaPX96rlvBLGoFr8YzsusgS4neg/YlvueOFtmH ClNDM9pJ3m9AZryT3RAyAtrvHKrWeQYER2gdbG/lvJU3U7d79laeKGJ74L/FBAEWPyzZ /aoF8V5GhEqxZekuoLhu+e5NSaBvn6WVwBABYFi/J+GXACR+v2Q55A5rxP6gxnQ3e5XR c/gQ== 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=ykOljB2vbGcyFYJUaA+gCQdraZEMWCskvExfV6LRvOY=; b=HC0T/bg4qkUGWb7hf0JGw3oHOXodUALSiMv9Wmfc1zD+uhVNy53mEFSwPeENN/KP/q C/G/oBUYu33EjT8M4XKRfRlXRg6N03ebJj0vyWOzHLQz4qT7zhfZuUqHGf2IxkluZz31 8EmOzrfYlA+wADRI60C6UxEXUmp6Z8fQHZ8TS0CY6ZyjDYWDRxmM27eTkKMPN7bQCCmA VNYUqOY5Tl0xwt1jvReb+7pU8vgOADbdLNN7lisd/yWqGjayMZp0qGFeoQ0mW8381LDf nlqaGxnk6cP8MMFyZ/MdzUbHz61T+8cJiaubNdH0zyIXPzx5611CfrOplyS3TlRJ+O4N tN7w== X-Gm-Message-State: AHQUAubiGA/JmlUre9XntMPa+S/rO3gfWRIrdTFyjulBrl5BwVoxrZjj ceWlQSw/Pt4ZD6ycgvN6s96gBhdPV0M= X-Received: by 2002:a1c:dc8a:: with SMTP id t132mr2090726wmg.48.1551266032303; Wed, 27 Feb 2019 03:13:52 -0800 (PST) Received: from zen.linaroharston ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id o12sm37324747wre.0.2019.02.27.03.13.51 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 27 Feb 2019 03:13:51 -0800 (PST) Received: from zen.linaroharston. (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 57C101FF80; Wed, 27 Feb 2019 11:13:51 +0000 (UTC) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: armbru@redhat.com Date: Wed, 27 Feb 2019 11:13:47 +0000 Message-Id: <20190227111347.15063-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::32e Subject: [Qemu-devel] [PATCH v5] 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: Laszlo Ersek , =?utf-8?q?Alex_Benn=C3=A9e?= , qemu-devel@nongnu.org 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 and warn the user when we have performed magic to enable things to Just Work (tm). Signed-off-by: Alex Bennée 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 v5 - remove mention of null padding - use %zu for size_t fmt string - add Laszlo r-b --- hw/block/pflash_cfi01.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) -- 2.20.1 diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c index 9d1c356eb6..d8cfa4789a 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" @@ -730,13 +731,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, @@ -763,6 +757,38 @@ 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 pad the region first. If the user supplies a + * larger file we ignore the tail. + */ + int64_t backing_len = 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 = total_len - backing_len; + /* pad with NOR erase pattern */ + memset((uint8_t*)pfl->storage + backing_len, 0xff, pad_bytes); + warn_report("device needs %" PRIu64 + " bytes, padded with %zu 0xff bytes", + total_len, pad_bytes); + total_len = 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", total_len); + } + /* read the initial flash content */ ret = blk_pread(pfl->blk, 0, pfl->storage, total_len);