From patchwork Tue Jul 16 18:09:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 812775 Delivered-To: patch@linaro.org Received: by 2002:a5d:42c4:0:b0:367:895a:4699 with SMTP id t4csp435191wrr; Tue, 16 Jul 2024 11:10:28 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUiw4wFsIFbYc8PKn+qgrvjkFiQgZ59Kx0KmEBTMbrmvK6vCmPksdmfwrUsSgqgwyaJOGXN8BisHTYhQ+krsp2S X-Google-Smtp-Source: AGHT+IG4sbzlwbgCHZ9RZMVeT65a6WF/Z1FIoA9XiQShHc8No9NZt/gTfJ8Rh6oBEJ+t8mR0Z5tA X-Received: by 2002:a05:6808:2124:b0:3d8:a93a:33bd with SMTP id 5614622812f47-3dac7bf4f77mr3829154b6e.34.1721153428225; Tue, 16 Jul 2024 11:10:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1721153428; cv=none; d=google.com; s=arc-20160816; b=txac8D5XKkM8aLRh7b0uTjrgEQdMGFiUJdnT4rQRJNwVmyqpjGQqMBtVaPgKKUwqe9 XHmU2zFCgRdpkxkV+OV5+/9jk61v8T2hMsS74RE/eVlBqLIMY5LxIBYh8an/T2d3SBfH bzEd8PO6ZjBZcGYs+q+DTZ5d00cOCFPA/lPZh2UGetF7S5ADMosYC4dX3cxHFqGhqCzt /kIHjxAttntlc2wsnH49f0ym7Kq+p2Qmo3FhZlUIDCnDWQ4psHCpBL+3LcSuKpDRsb4Y DABbtKo3VlIyRlF0/t4ncQVZfyStN0mSU6USM+xW0AVBQpC83uRDIA7ig4PF+7Qhau2K SrvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=+SuZjNq7VfYR+yUP5NB/Weekc2q9mEwEfGGbbLYskCs=; fh=1ieggTHFECSIMInboqlh7X75OhpJtso/ZV12TJvwK60=; b=Z3JfK//0H2ABCVsJl53KjafvbzRtjY85IeMz5opw+TELUhpPVAw9AA9tX9LPNQZqq/ dLh2nzFIfvpoRkrXMOv7507qqL4bSyfaeYUDNDM7XzLoK1hAHIJ9IcC0JQw8Wd7ZbuDo axQmLTXIKcIXAD7Ql8E/nVX2BW8AQLSYMoXMZ3wh+vErJ+aaNyAeF5zK2TmZR3aSWTap elP+YTV32ngAHUpqTDVjAcHvHI5pPJsKGvfmbladNW+fcczv5A2GvaZpWT6bhaVWTvQy G/p2MaPrkdU/47Cu1sn0fKPT2c692K4tXaevStDCN37fslkRFsTF6GyF/jxuaCrxVxIq ASMg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=W68+pyle; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7a160c84654si748143685a.687.2024.07.16.11.10.28 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Jul 2024 11:10:28 -0700 (PDT) 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=pass header.i=@linaro.org header.s=google header.b=W68+pyle; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sTmcd-00066s-V7; Tue, 16 Jul 2024 14:09:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sTmcb-0005xF-FR for qemu-devel@nongnu.org; Tue, 16 Jul 2024 14:09:53 -0400 Received: from mail-wr1-x432.google.com ([2a00:1450:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sTmcZ-0000tk-Ga for qemu-devel@nongnu.org; Tue, 16 Jul 2024 14:09:53 -0400 Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-367990aaef3so3475527f8f.0 for ; Tue, 16 Jul 2024 11:09:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1721153389; x=1721758189; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+SuZjNq7VfYR+yUP5NB/Weekc2q9mEwEfGGbbLYskCs=; b=W68+pylefKOECTzT1CgRHZKODUyNXt3jPtJpGUG6pySLu+m+K5C+Bsuge3vNS2o5hK 2iJ07klONyCnmYKnCf0wgGiee99h4j6pqwxkY9PUM2Vq1yqoAoORhZ8mX+ifu0WYGAfo Wc+73quMWAOTyjqyD0cwOuZmTiNpXxq0KQwdL1CDBgIKrc2oJoaId+uUMweVBvEpgdNe LR2wdiDOBr9N7jX/UN/IRY762swzHGypvei6SPsCFRJ8yxaA/qVvYh+RHcJYiH+O69SP W9oTGzHP/KOLU4kIQFAYYyHIHeK9o6yLYP2JGBD8NvwemXJ8gfsnNTG6wcnHTkt8E8W6 LsGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721153389; x=1721758189; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+SuZjNq7VfYR+yUP5NB/Weekc2q9mEwEfGGbbLYskCs=; b=NiUfW/HqyOr4KN94j3QStXcc02w41oEebqQGby5l/E+AlUH4yHVkxSmjPySVs2OuV/ mF8f2/dqyvWvSpt6Nu8CcqC4BxJDDIz6kt6NBYHr5e6UIEpydGej1l57HhMJ1zBun1lx MhkWcmv3NrE+fyzejLyWfXnVGORT4r7S8w50/aJCyNniuvQvIbsAT6LsS3aNah0bcXl1 yu0dIftrZwtcKKVaGqHh2VW3ZN7Q40AjIEzNsK+4uu5OlLjxh0EJ1Q+tXluCYPdQ4P+g Dsd4qZS7aQPC75vhTK5KhgVuj3lTOFlfEewGaBUjSFRdZ/ySWqhl2LzXrIS+FDK+uxZE nVeA== X-Gm-Message-State: AOJu0YzB3Qnq6TwCCNjsteu/ftYAu6FpLXJYTYEoxEfpuHs/JwXGML0k KlRxRzOeEg/jZsSBLUeBsV3fGgjGcBsoxqiPUUB1dsYvulV5flqQhnd1RVJsXXfLV2y6DulW7hd M1t5RmA== X-Received: by 2002:adf:dd83:0:b0:364:3ba5:b629 with SMTP id ffacd0b85a97d-3682610f680mr1966923f8f.20.1721153389591; Tue, 16 Jul 2024 11:09:49 -0700 (PDT) Received: from localhost.localdomain ([176.187.209.82]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-427ba20aaf8sm29584465e9.0.2024.07.16.11.09.48 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 16 Jul 2024 11:09:49 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Gregor Haas , Xingtao Yao , =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , =?utf-8?q?Phi?= =?utf-8?q?lippe_Mathieu-Daud=C3=A9?= Subject: [PULL 01/13] hw/core/loader: allow loading larger ROMs Date: Tue, 16 Jul 2024 20:09:28 +0200 Message-ID: <20240716180941.40211-2-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240716180941.40211-1-philmd@linaro.org> References: <20240716180941.40211-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::432; envelope-from=philmd@linaro.org; helo=mail-wr1-x432.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Gregor Haas The read() syscall is not guaranteed to return all data from a file. The default ROM loader implementation currently does not take this into account, instead failing if all bytes are not read at once. This change loads the ROM using g_file_get_contents() instead, which correctly reads all data using multiple calls to read() while also returning the loaded ROM size. Signed-off-by: Gregor Haas Reviewed-by: Xingtao Yao Reviewed-by: Daniel P. Berrangé Reviewed-by: Philippe Mathieu-Daudé Message-ID: <20240628182706.99525-1-gregorhaas1997@gmail.com> [PMD: Use gsize with g_file_get_contents()] Signed-off-by: Philippe Mathieu-Daudé --- hw/core/loader.c | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/hw/core/loader.c b/hw/core/loader.c index a3bea1e718..39bd8f9e4d 100644 --- a/hw/core/loader.c +++ b/hw/core/loader.c @@ -1076,8 +1076,8 @@ ssize_t rom_add_file(const char *file, const char *fw_dir, { MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine()); Rom *rom; - ssize_t rc; - int fd = -1; + gsize size; + g_autoptr(GError) gerr = NULL; char devpath[100]; if (as && mr) { @@ -1095,10 +1095,10 @@ ssize_t rom_add_file(const char *file, const char *fw_dir, rom->path = g_strdup(file); } - fd = open(rom->path, O_RDONLY | O_BINARY); - if (fd == -1) { - fprintf(stderr, "Could not open option rom '%s': %s\n", - rom->path, strerror(errno)); + if (!g_file_get_contents(rom->path, (gchar **) &rom->data, + &size, &gerr)) { + fprintf(stderr, "rom: file %-20s: error %s\n", + rom->name, gerr->message); goto err; } @@ -1107,23 +1107,8 @@ ssize_t rom_add_file(const char *file, const char *fw_dir, rom->fw_file = g_strdup(file); } rom->addr = addr; - rom->romsize = lseek(fd, 0, SEEK_END); - if (rom->romsize == -1) { - fprintf(stderr, "rom: file %-20s: get size error: %s\n", - rom->name, strerror(errno)); - goto err; - } - + rom->romsize = size; rom->datasize = rom->romsize; - rom->data = g_malloc0(rom->datasize); - lseek(fd, 0, SEEK_SET); - rc = read(fd, rom->data, rom->datasize); - if (rc != rom->datasize) { - fprintf(stderr, "rom: file %-20s: read error: rc=%zd (expected %zd)\n", - rom->name, rc, rom->datasize); - goto err; - } - close(fd); rom_insert(rom); if (rom->fw_file && fw_cfg) { const char *basename; @@ -1160,9 +1145,6 @@ ssize_t rom_add_file(const char *file, const char *fw_dir, return 0; err: - if (fd != -1) - close(fd); - rom_free(rom); return -1; }