From patchwork Fri Jun 28 02:41:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominique Martinet X-Patchwork-Id: 1953666 X-Patchwork-Delegate: sbabic@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20230601 header.b=m+KS97VU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2001:4860:4864:20::3f; helo=mail-oa1-x3f.google.com; envelope-from=swupdate+bncbcwivbv7sugrbbwg7czqmgqepzcijti@googlegroups.com; receiver=patchwork.ozlabs.org) Received: from mail-oa1-x3f.google.com (mail-oa1-x3f.google.com [IPv6:2001:4860:4864:20::3f]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W9KTh5nLtz20Xf for ; Fri, 28 Jun 2024 12:42:16 +1000 (AEST) Received: by mail-oa1-x3f.google.com with SMTP id 586e51a60fabf-2547e18cb07sf274742fac.2 for ; Thu, 27 Jun 2024 19:42:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1719542535; cv=pass; d=google.com; s=arc-20160816; b=NsGR3qkKw8w0BgBw9/j/DkDnBndJE061Ko1JZtZCjavizHN7MeoUafoA6Y6kgvlQAi VV97WgTXkme1SD1wGQpM781VMEOvj8bXhAxhh7ADbZ4eIIPgOzSMQjVlm1HRDCkMXqXN gKayAF/XDxsLGbz+dzEwNm54LSq08ri/guWh20i3huQzn8mE8erLmkfFV2rif9z/iX4e X38Hhkf7M2k5aI7NUxHLqhWidyuhx+NGiVGEA+aFXFLozvQnanfEdV6aYu6Btqo4ZHKz YaSMFUYm5kP3y8FDMoIYC+RUR8opjVtJn9XryfXyitdAvS7lUKEn+3CMad+ZtYHj3qrm Ehtg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=dPqTCtFwhA2bEGWrloXnl7kzh9LIXH65SiXCWY/Dh+E=; fh=kbG+l+MdYy3lN1qEvPTqUJ3f6c8i8I9JbCA1v4imbno=; b=K8SAhrCygkL2ZNrxR+hP8zDcgHEHgrObn3wivojUkYIAiWu1DtwcaxWWmQYUxYvOSW XkYB6OuN5Y1iKI9GQO6KtJN508dJC3TnpVp3i5bfJ2vWfSp1FTBSgo/LwBvU04GfvCGR huxY/kPSa8WhXvVjxQ9JbYEjmQs7puRta5ZECyOvblrWHk2yCGdAnGMLlB9Dal7+Mi/L n5RSv7YkyHPP+cGPon/vazA4i1T1zFkiQiR8ibJsTZONEmJXOc4L7jjlQWfYKvVuh9ME 39jLLTeqsnCUYlq9EJa//A6VAexEBSYR6ebYgNoClciKx21b7Py0wvdeRCHHUYVSWluG s4Zw==; darn=patchwork.ozlabs.org ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@atmark-techno.com header.s=gw2_bookworm header.b="Iv72n/9t"; dkim=pass header.i=@atmark-techno.com header.s=google header.b=jQaJpdu1; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=atmark-techno.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20230601; t=1719542535; x=1720147335; darn=patchwork.ozlabs.org; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:sender:from:to:cc:subject:date:message-id :reply-to; bh=dPqTCtFwhA2bEGWrloXnl7kzh9LIXH65SiXCWY/Dh+E=; b=m+KS97VUa9LfCgTgOVqhNAtJsXDEXQqea4/6dte4j5ElwnGTTWDrbS329Jt1QqLTXH JNlFTOCONMz/YJPnlMlSSmTrmvFgJ2iC6hEq7yxQsbZqrIfaJdmavaAA1xJE/hQmmP70 vuRjqDRThwPnlpke4Jtzl+zJdlYdz7dTtXk4C2t+q053YOTgjO6IFML2kwgrKDs6483/ 0rIainBxCWmDUrkbKN/cE7oy/HIOwdTOVxQu6oNXIt340WLh1Er6284lcCPrAQF39029 irCopovc7sQAuo7r79b1RJbw3XNxK3WcDjktbY0BbNizf3dzWaiMfiwFNMOK2DtgcDq6 /DtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719542535; x=1720147335; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :x-beenthere:x-gm-message-state:sender:from:to:cc:subject:date :message-id:reply-to; bh=dPqTCtFwhA2bEGWrloXnl7kzh9LIXH65SiXCWY/Dh+E=; b=Y8GUz16pMHRhDTIXjJgFPMlsuuJj2bJd+J8XZYuRqSUW1SCru9h5dyWPGNB7YgWjz+ LvTASC1VWwU3or+hAjysZUFUkkbA1vDEf4D5MNYyEGdtI80tJtkf+pePiWOWuQBmMDoY jCGDyNwDzGxOL3MriG4p0luijD/djKLCUhDjLMFBSVeRGtiIa8Dtgnt78ZDoMWN/gZUn XaZOYP/5m3b5LCayrp9TEj/ujJ3VznsnCSv3mKeG0WvJGNl6ksXnn/tcMW8tBm+6iGhB s8hBMm5YTalaQCMHckyKeJMPhWt1pTnKvWZwf/Nvumm0YcSJBu7/MwU9WmTZUoyLroYk 9EtA== Sender: swupdate@googlegroups.com X-Forwarded-Encrypted: i=2; AJvYcCUvBvsKh94+3ejcMyJjx8g31pWaf53T7pxxBEE27KE1AxGJzHbDE5Z7fciC6LK4LhznhkFBhxz8i0gl11EIVX3cZi6RX6cigzrnljUjVw== X-Gm-Message-State: AOJu0YxAe6avpo/cjpvleAru6XpMblTObxfeFUiw8Bdkjv4BnDi459m7 nO21kn57FSEEgiklF6J9VDfwWhod97wSEyxJfjcgaWDfJZs5zRho X-Google-Smtp-Source: AGHT+IE3OnK3E4QWHepOfmoNUCGZpEDm3++06MAVpRMMbuZ39x/Mn6eAidtm/NH3E3PBEwOP9U/ong== X-Received: by 2002:a05:6870:ecaa:b0:25c:ad1f:b334 with SMTP id 586e51a60fabf-25d016f3502mr16481449fac.21.1719542535121; Thu, 27 Jun 2024 19:42:15 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a05:6870:b8a:b0:259:89be:cd7e with SMTP id 586e51a60fabf-25d92caebefls260812fac.2.-pod-prod-02-us; Thu, 27 Jun 2024 19:42:14 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVXZFyge76gKcZ7Uuo/sZFDsGQKwkS5PY+SnBQRDgKFnhPUr6BQ9hYQxfAYLQ7i7NXcDz8nBk5DlLgJ7z2PE1ZfobYzy0HXugRU X-Received: by 2002:a05:6808:10d1:b0:3d5:63f0:ee9e with SMTP id 5614622812f47-3d563f0f047mr7017917b6e.20.1719542534017; Thu, 27 Jun 2024 19:42:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1719542534; cv=none; d=google.com; s=arc-20160816; b=jrQpRE0dSkR2bWUtuNXfNG5uDkKxrHgtDKF+F0n0oUtRqpRf1zOnkpkZy5JaN+VC+Z RRJjcpXqCnhntBAAMr1zkPdeaUpfYt/glCgXLLFdQnzSmZXNSx9DBu6yZaBNwf8QqTon aaug9uHv3g7+vvbriFUuR9rgCflwC7TKquZ5nBTmhkEeAp6WlnGrl3yVw+pw5+I02sIl wZbqZUzEcqCMlzJq5KjZpxGjOrlY3u/EyByrSyLHdzjzhE8kbTpq6NRTzs4V7NDxiX69 NGwYBxjQEOjgqcnbCOnIcWDrdjzgeh7gvxhNYBmJGcYl/1z0GxCK4Dcb9q9Kvqmwlbos Z+kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dkim-signature; bh=DKuBrzIyTyx1vBMR5ehy5ixaO/AKf8M9Hku4D/EwGoc=; fh=igJvc4PZKVhDEuXhGrn4n6CchvGhOSkNg4OURqn4Rlo=; b=hbs9VC0zQjLs+eX1pG1gHi0WDSWb4PDMqtxL73ysyov7L6Poap2b3DRrA/cWidAAeD 9qKOk/H0/5brsYy1M/K8Bb/71ZKpj0IE8a+barG8xpg1jHRwKAtXlYaRODmfI0haC2K8 nnV0YJcS0WHm83xVlaiuaiq/6y1pFddA8Z7RRsweg6WdvwXH5OyAJu/F+LjohNRZI29c /0yBzmmY3+7xvO8WbEx+oVPGmBIATrCHTNAv3nqho2bHc4fM1ynJywLmkZMZzXZc2GH8 m95mBLZVOxt+l9T4bq8GJCsIOIDrN4v51ADGEN5vnpwuRrunVhZDxs3a8OLWjH14/TyI N9Dg==; dara=google.com ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@atmark-techno.com header.s=gw2_bookworm header.b="Iv72n/9t"; dkim=pass header.i=@atmark-techno.com header.s=google header.b=jQaJpdu1; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=atmark-techno.com Received: from gw2.atmark-techno.com (gw2.atmark-techno.com. [35.74.137.57]) by gmr-mx.google.com with ESMTPS id 5614622812f47-3d62fa43be7si41235b6e.3.2024.06.27.19.42.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 19:42:13 -0700 (PDT) Received-SPF: pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) client-ip=35.74.137.57; Received: from gw2.atmark-techno.com (localhost [127.0.0.1]) by gw2.atmark-techno.com (Postfix) with ESMTP id C7D9D9AA for ; Fri, 28 Jun 2024 11:42:11 +0900 (JST) Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by gw2.atmark-techno.com (Postfix) with ESMTPS id 5A7DAA6A for ; Fri, 28 Jun 2024 11:42:11 +0900 (JST) Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-7086fe615afso76476b3a.1 for ; Thu, 27 Jun 2024 19:42:11 -0700 (PDT) X-Forwarded-Encrypted: i=1; AJvYcCXz9W8ByOxSwZcLSqRo0QYOD6ze/ci/SaQyvcvV/FzKXJe1Gd8O//7fkWddS1ByNw5fYgpO+JYMS/fY7buwHCKP4B79iDsGtTzi X-Received: by 2002:aa7:92d4:0:b0:705:bc32:534e with SMTP id d2e1a72fcca58-70670e9ed1bmr15401579b3a.9.1719542530252; Thu, 27 Jun 2024 19:42:10 -0700 (PDT) X-Received: by 2002:aa7:92d4:0:b0:705:bc32:534e with SMTP id d2e1a72fcca58-70670e9ed1bmr15401563b3a.9.1719542529792; Thu, 27 Jun 2024 19:42:09 -0700 (PDT) Received: from pc-0182.atmarktech (178.101.200.35.bc.googleusercontent.com. [35.200.101.178]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7080246ee80sm436387b3a.69.2024.06.27.19.42.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Jun 2024 19:42:09 -0700 (PDT) Received: from [::1] (helo=pc-0182.atmark.tech) by pc-0182.atmarktech with esmtp (Exim 4.96) (envelope-from ) id 1sN1Yt-007bPN-0d; Fri, 28 Jun 2024 11:42:07 +0900 From: Dominique Martinet To: stefano.babic@swupdate.org, swupdate@googlegroups.com Cc: Dominique Martinet Subject: [swupdate] [PATCH 1/2] cpio_utils: move out hash comparison in a helper Date: Fri, 28 Jun 2024 11:41:59 +0900 Message-Id: <20240628024201.1812065-2-dominique.martinet@atmark-techno.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240628024201.1812065-1-dominique.martinet@atmark-techno.com> References: <20240628024201.1812065-1-dominique.martinet@atmark-techno.com> MIME-Version: 1.0 X-Original-Sender: dominique.martinet@atmark-techno.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@atmark-techno.com header.s=gw2_bookworm header.b="Iv72n/9t"; dkim=pass header.i=@atmark-techno.com header.s=google header.b=jQaJpdu1; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=atmark-techno.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , While here, also a couple of minor improvements: - reword comment about 64 bytes for md_value - only convert to ascii if we're going to use it for logging Signed-off-by: Dominique Martinet --- core/cpio_utils.c | 65 +++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/core/cpio_utils.c b/core/cpio_utils.c index 65f301909f0c..cc6e32619751 100644 --- a/core/cpio_utils.c +++ b/core/cpio_utils.c @@ -434,6 +434,38 @@ static int zstd_step(void* state, void* buffer, size_t size) #endif +static int hash_compare(struct swupdate_digest *dgst, unsigned char *hash) +{ + /* + * SHA256_HASH_LENGTH should be enough but openssl might write + * up to EVP_MAX_MD_SIZE = 64 bytes (sha512 size) + */ + unsigned char md_value[64]; + unsigned int md_len = 0; + + if (swupdate_HASH_final(dgst, md_value, &md_len) < 0) { + return -EFAULT; + } + /* + * Now check if the computed hash is equal + * to the value retrieved from sw-descritpion + */ + if (md_len != SHA256_HASH_LENGTH || swupdate_HASH_compare(hash, md_value)) { +#ifndef CONFIG_ENCRYPTED_IMAGES_HARDEN_LOGGING + char hashstring[2 * SHA256_HASH_LENGTH + 1]; + char newhashstring[2 * SHA256_HASH_LENGTH + 1]; + + hash_to_ascii(hash, hashstring); + hash_to_ascii(md_value, newhashstring); + + ERROR("HASH mismatch : %s <--> %s", + hashstring, newhashstring); +#endif + return -EFAULT; + } + return 0; +} + static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nbytes, unsigned long *offs, unsigned long long seek, int skip_file, int __attribute__ ((__unused__)) compressed, uint32_t *checksum, unsigned char *hash, bool encrypted, const char *imgivt, writeimage callback) @@ -441,11 +473,6 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby unsigned int percent, prevpercent = 0; int ret = 0; int len; - unsigned char md_value[64]; /* - * Maximum hash is 64 bytes for SHA512 - * and we use sha256 in swupdate - */ - unsigned int md_len = 0; unsigned char *aes_key = NULL; unsigned char *ivt = NULL; unsigned char ivtbuf[AES_BLK_SIZE]; @@ -639,31 +666,9 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby } } - if (IsValidHash(hash)) { - if (swupdate_HASH_final(input_state.dgst, md_value, &md_len) < 0) { - ret = -EFAULT; - goto copyfile_exit; - } - - - /* - * Now check if the computed hash is equal - * to the value retrieved from sw-descritpion - */ - if (md_len != SHA256_HASH_LENGTH || swupdate_HASH_compare(hash, md_value)) { - char hashstring[2 * SHA256_HASH_LENGTH + 1]; - char newhashstring[2 * SHA256_HASH_LENGTH + 1]; - - hash_to_ascii(hash, hashstring); - hash_to_ascii(md_value, newhashstring); - -#ifndef CONFIG_ENCRYPTED_IMAGES_HARDEN_LOGGING - ERROR("HASH mismatch : %s <--> %s", - hashstring, newhashstring); -#endif - ret = -EFAULT; - goto copyfile_exit; - } + if (IsValidHash(hash) && hash_compare(input_state.dgst, hash) < 0) { + ret = -EFAULT; + goto copyfile_exit; } if (!inbuf) { From patchwork Fri Jun 28 02:42:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominique Martinet X-Patchwork-Id: 1953669 X-Patchwork-Delegate: sbabic@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.a=rsa-sha256 header.s=20230601 header.b=vAncwgm+; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::83e; helo=mail-qt1-x83e.google.com; envelope-from=swupdate+bncbcwivbv7sugrbcgg7czqmgqeol3cl5i@googlegroups.com; receiver=patchwork.ozlabs.org) Received: from mail-qt1-x83e.google.com (mail-qt1-x83e.google.com [IPv6:2607:f8b0:4864:20::83e]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W9KTl4RjCz20Xf for ; Fri, 28 Jun 2024 12:42:19 +1000 (AEST) Received: by mail-qt1-x83e.google.com with SMTP id d75a77b69052e-44505dd2221sf3063671cf.0 for ; Thu, 27 Jun 2024 19:42:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1719542537; cv=pass; d=google.com; s=arc-20160816; b=ttFyYlXoFA3teD1d0uK9i/7ONLbRYyqf2en62Xv42zwNPGpHbNOiTSV0OqxFvi+Dhv yyU/nGRWQAKGbXPihOrQKcX9bkc+HPTDCBVdS97jkw6WmY/M4n6wL5FOdkeo+tbKpR+D Fwh214unuRTb2C3FVkDVbfrBZOXJ6ZbyDN4QVDNVW+ydbzQkXM7rZm0/pJ2RRW2uOsU2 rYwlGEbd13d7sm7Qs9M3guEEXE5iLT55+81NEQveJ/eMBASysazWKR7DPrkMa3XyRp8D U0Ipd7/RHDGva+e1NZZvI0KlVkKdCIm2NN/OcXDsjGXa/335jAmuK+dpuqxeh9of0scM A0jw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:dkim-signature; bh=Ma4Bdog5WOtYgT8dh+Gnhmg+PydCBa50AbV9OskoBMQ=; fh=H/MLlxuzNyJBdln/b5Tt15P20G7H+h/UO7Wse/Z13YE=; b=GLwVD3Ifg3GHNIWkcJEmlmsgnHTQaq9jbyyRLui6zuDNLF/N+/PxJm021YqvUbhMn2 IHmqVMYXMUz8alZrjAFK4eISER+v0yCmPuC4DLS8zi5yeK2lFwIzm4fZ9kIFxkXMorSS IMQXxSNtEpwp5pfv3fqOp9iHM5yq2tYHyiioYtVP5Xfupb9Vq3UHLr1fnlixLtmVaghb bKe6J3AzrXheMDc0n3hAMVRiRpY1mU5MGZyWNHr592w6ypCpsOiZNhYdFAyFP6rzRc2L AXYaotRWjsELo60Zd0z4i9cyNP1FrDX66yWei60ZSjdMxu93dZT593V5OyU/HjmXUF3u 7blQ==; darn=patchwork.ozlabs.org ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@atmark-techno.com header.s=gw2_bookworm header.b=O60Rt5KI; dkim=pass header.i=@atmark-techno.com header.s=google header.b=cAO1m5FU; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=atmark-techno.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20230601; t=1719542537; x=1720147337; darn=patchwork.ozlabs.org; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:x-original-authentication-results :x-original-sender:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:sender:from:to:cc:subject:date:message-id :reply-to; bh=Ma4Bdog5WOtYgT8dh+Gnhmg+PydCBa50AbV9OskoBMQ=; b=vAncwgm+U+q3J5sHV3MJsnIoB1mAjShcooIgp6LztEhnFfrAJbxdWf9jOkNHcSq6ty dVEj6VZYSYMvbKJPoW+LZ5oaOJ3mPlFUWQ5w0/jRVuOrkAOYlObMKeFmSxjng6ig8ygm QDYMyQ4Tu+0u0WUfVpcY9mP1KfkpA4MyPT76q5k4uh9gSIpcaE4jY9aaUl4t4+iSVw3p Rz/NxmtvWXk5ANYBLFEcM277aLwxiGpXniSGeUGDX2bdIheSOEwWWRjhjBnZYlVA8Duf PugDg6ZYX1BQG11pzZI2D0hGe6wEiL0/IlmhqMc44O8YbKu1pROMRRdST7QAKPizl817 R0RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719542537; x=1720147337; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence :x-original-authentication-results:x-original-sender:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :x-beenthere:x-gm-message-state:sender:from:to:cc:subject:date :message-id:reply-to; bh=Ma4Bdog5WOtYgT8dh+Gnhmg+PydCBa50AbV9OskoBMQ=; b=RJIQQE9lBslX//0S4j1gHQdamN6ar3LDIn43hSVq/LkrgZA/A2Eslx0CqvdNDQHMRK 5uhKn2F+1K/Lcyr6Y86mCAzXxOE63ARpOoyaaj/yNYvqWKnnNZ/gwapkavbubcUIPgDM JET+u5fisQsLdh2IabB6Lk8nx25eDMRpWeMc3481/17mxyFhia5Omn5n28EEjzTjmVWk 5JQnA/IZ+KEgDvuVLWzhLcIssAz5edcHZ6JENHMGZNS29qf3/7ypYDDfGRz1Y+7AM6Tg szhLLw+cOvaZFjrLgkR0zf/XTWufw/yZHlFDinlQUVELMIe0PfYWYSFfrOr+wNvuG4Zw Zd7A== Sender: swupdate@googlegroups.com X-Forwarded-Encrypted: i=2; AJvYcCUa2Lui9aecOElNmj5gpL0yy96ZMsDaNLLVJGy6qI6YELOAlrcZUXnh+7EB2cPvb8Dao76XauuS+pdzDXMDzZy8njZYKD77YGpQUqfFhg== X-Gm-Message-State: AOJu0YwqpGyOix6lhnY+8M8NKu6AfjJ0/C9cO6LcpYC/HBMDx5ipFtGw OqhD6KhWaWlD6w+88Jng9pM9lVbXj5Kj4ph4ehOfrVkK0d0zFfL6 X-Google-Smtp-Source: AGHT+IF4GVm7jeutG1zharyqSDANOqt8IjvD9Cp5ZOIEMCVyTN9SZAJIzM70r0w4/DwbXoCCxsrPQw== X-Received: by 2002:ac8:5a44:0:b0:439:8bae:6ab2 with SMTP id d75a77b69052e-444d91a18e1mr176627731cf.17.1719542536563; Thu, 27 Jun 2024 19:42:16 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:ac8:5a0e:0:b0:446:4642:4f5e with SMTP id d75a77b69052e-446549fed4cls2130551cf.2.-pod-prod-08-us; Thu, 27 Jun 2024 19:42:15 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVXc+8T+Lr8ayeEhz/2fFz+fjpNQa2JaC2p2fbEYCQNhvvngr6vsZJKccX0pGE1seBmVAIl3LPt+9wq9ArzlhrpfoAS32tSLSaq X-Received: by 2002:ac8:5a85:0:b0:441:5a4b:60f4 with SMTP id d75a77b69052e-444d921305dmr174110511cf.45.1719542535216; Thu, 27 Jun 2024 19:42:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1719542535; cv=none; d=google.com; s=arc-20160816; b=tA7q8AU965FUTDITFGgMH5JAI8VRtbYbPYw6CWwYBt1CYRlpTZPYwTGLkUK2eYUC5a 74QPZf2e32vi1FicwM8ErXuR3ddkICqPblZvK64F2rL5vdupz72UYm1mdNdZ5ugTT9WS cBqhrGjTHla7lZs8a4NSNZThGRzEooDdZqwjTk0GytUWFLS0DDcHlvpAUcIX9RlNrimf 3KTAWGEWvc5/Y0wCcEKikpKklR9BeerIl98ywpooZ/yCAwmekgcfLnQ62Q+A3FFB2EF2 r8NBt92OXC0qQYfXz2WOsjpFYfHsTF2FWFkxkLWZg3ZqWIhHFchA2B8KRcxWtqb8wHDR eo6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dkim-signature; bh=+k/pGw83FNaRc3BvhGFGlfsBf4COOpUexAX1iYOwHqA=; fh=mY5vGqSMEPuFggrwotv5ckwh71r5k3mVVs6JEUzO368=; b=ycm3E7Clol0qi1SgDpH1EDAqcJxb7H7h2qzN9uHJiYM7DIlbs8bw3QZGIrJ5fzbWEO rBdSSoezlsjblY5BesiGDSXWTsLELGk5u8HbNseo1FeGC1J+7MrIkXf3usACjAA/JgNy 002m2l4krsb02+Gt+hA/oAI+zEp9FfHS/T9w66XYwxPR+AgFbnjZK6/tHNAIRbAXBrNK 9H9FjxbXcAR2waeRcTsOL1JOlphJ+zuTnkH3veTdra2TqQKuQbzY023KKcdTJycZq3BH XUiYfjIoO1wTvMuGkHIdvdsW3Cxtm88JWwSe4juPwQzEFewGbIMVmZRWHq1n9aSJg/4k 0PDw==; dara=google.com ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@atmark-techno.com header.s=gw2_bookworm header.b=O60Rt5KI; dkim=pass header.i=@atmark-techno.com header.s=google header.b=cAO1m5FU; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=atmark-techno.com Received: from gw2.atmark-techno.com (gw2.atmark-techno.com. [35.74.137.57]) by gmr-mx.google.com with ESMTPS id d75a77b69052e-446514e8282si476571cf.5.2024.06.27.19.42.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 19:42:15 -0700 (PDT) Received-SPF: pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) client-ip=35.74.137.57; Received: from gw2.atmark-techno.com (localhost [127.0.0.1]) by gw2.atmark-techno.com (Postfix) with ESMTP id BA1BA2AF for ; Fri, 28 Jun 2024 11:42:12 +0900 (JST) Received: from mail-yw1-f197.google.com (mail-yw1-f197.google.com [209.85.128.197]) by gw2.atmark-techno.com (Postfix) with ESMTPS id 3F44AA0C for ; Fri, 28 Jun 2024 11:42:12 +0900 (JST) Received: by mail-yw1-f197.google.com with SMTP id 00721157ae682-64a7d2dc2a9so2871027b3.1 for ; Thu, 27 Jun 2024 19:42:12 -0700 (PDT) X-Forwarded-Encrypted: i=1; AJvYcCXVhz+NDuOO4I1PUbCpJmnBegZj/D/NB923gzxIj65wHpFlyNqfzSXqYgoCvInYla0X6HVtkvypeIGMH/9Lh3RA+yaGTBg9gKSJ X-Received: by 2002:a25:bfc3:0:b0:dfb:420:1e91 with SMTP id 3f1490d57ef6-e03454e2b2emr4400444276.27.1719542530690; Thu, 27 Jun 2024 19:42:10 -0700 (PDT) X-Received: by 2002:a25:bfc3:0:b0:dfb:420:1e91 with SMTP id 3f1490d57ef6-e03454e2b2emr4400424276.27.1719542530187; Thu, 27 Jun 2024 19:42:10 -0700 (PDT) Received: from pc-0182.atmarktech (145.82.198.104.bc.googleusercontent.com. [104.198.82.145]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-72c6afb8ef1sm378880a12.40.2024.06.27.19.42.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Jun 2024 19:42:09 -0700 (PDT) Received: from [::1] (helo=pc-0182.atmark.tech) by pc-0182.atmarktech with esmtp (Exim 4.96) (envelope-from ) id 1sN1Yt-007bPN-10; Fri, 28 Jun 2024 11:42:07 +0900 From: Dominique Martinet To: stefano.babic@swupdate.org, swupdate@googlegroups.com Cc: Dominique Martinet Subject: [swupdate] [PATCH 2/2] copyfile refactor: replace the 13 arguments by a struct Date: Fri, 28 Jun 2024 11:42:01 +0900 Message-Id: <20240628024201.1812065-4-dominique.martinet@atmark-techno.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240628024201.1812065-1-dominique.martinet@atmark-techno.com> References: <20240628024201.1812065-1-dominique.martinet@atmark-techno.com> MIME-Version: 1.0 X-Original-Sender: dominique.martinet@atmark-techno.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@atmark-techno.com header.s=gw2_bookworm header.b=O60Rt5KI; dkim=pass header.i=@atmark-techno.com header.s=google header.b=cAO1m5FU; spf=pass (google.com: domain of dominique.martinet@atmark-techno.com designates 35.74.137.57 as permitted sender) smtp.mailfrom=dominique.martinet@atmark-techno.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=atmark-techno.com Precedence: list Mailing-list: list swupdate@googlegroups.com; contact swupdate+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: swupdate@googlegroups.com X-Google-Group-Id: 605343134186 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Having too many arguments make it difficult to see at a glance what is passed where, and makes it hard to extend further. Make copyfile() take a struct that describes all the current arguments and fix all callers. There is no intended functional change. Signed-off-by: Dominique Martinet --- core/cpio_utils.c | 141 +++++++++++++++--------------------- core/installer.c | 19 +++-- core/stream_interface.c | 24 +++++- handlers/copy_handler.c | 20 ++--- handlers/delta_handler.c | 29 +++++--- handlers/readback_handler.c | 20 ++--- include/util.h | 40 +++++++--- 7 files changed, 154 insertions(+), 139 deletions(-) diff --git a/core/cpio_utils.c b/core/cpio_utils.c index cc6e32619751..382b22c36434 100644 --- a/core/cpio_utils.c +++ b/core/cpio_utils.c @@ -466,9 +466,7 @@ static int hash_compare(struct swupdate_digest *dgst, unsigned char *hash) return 0; } -static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nbytes, unsigned long *offs, unsigned long long seek, - int skip_file, int __attribute__ ((__unused__)) compressed, - uint32_t *checksum, unsigned char *hash, bool encrypted, const char *imgivt, writeimage callback) +int copyfile(struct swupdate_copy *args) { unsigned int percent, prevpercent = 0; int ret = 0; @@ -478,12 +476,12 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby unsigned char ivtbuf[AES_BLK_SIZE]; struct InputState input_state = { - .fdin = fdin, + .fdin = args->fdin, .source = INPUT_FROM_FD, .inbuf = NULL, .pos = 0, - .nbytes = nbytes, - .offs = offs, + .nbytes = args->nbytes, + .offs = args->offs, .dgst = NULL, .checksum = 0 }; @@ -522,32 +520,33 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby /* * If inbuf is set, read from buffer instead of from file */ - if (inbuf) { - input_state.inbuf = inbuf; + if (args->inbuf) { + input_state.inbuf = args->inbuf; input_state.source = INPUT_FROM_MEMORY; } PipelineStep step = NULL; void *state = NULL; uint8_t buffer[BUFF_SIZE]; + writeimage callback = args->callback; if (!callback) { callback = copy_write; } - if (checksum) - *checksum = 0; + if (args->checksum) + *args->checksum = 0; - if (IsValidHash(hash)) { + if (IsValidHash(args->hash)) { input_state.dgst = swupdate_HASH_init(SHA_DEFAULT); if (!input_state.dgst) return -EFAULT; } - if (encrypted) { + if (args->encrypted) { aes_key = get_aes_key(); - if (imgivt && strlen(imgivt)) { - if (!is_hex_str(imgivt) || ascii_to_bin(ivtbuf, sizeof(ivtbuf), imgivt)) { + if (args->imgivt && strlen(args->imgivt)) { + if (!is_hex_str(args->imgivt) || ascii_to_bin(ivtbuf, sizeof(ivtbuf), args->imgivt)) { ERROR("Invalid image ivt"); return -EINVAL; } @@ -562,12 +561,12 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby } } - if (compressed) { - if (compressed == COMPRESSED_TRUE) { + if (args->compressed) { + if (args->compressed == COMPRESSED_TRUE) { WARN("compressed argument: boolean form is deprecated, use compressed = \"zlib\";"); } #ifdef CONFIG_GUNZIP - if (compressed == COMPRESSED_ZLIB || compressed == COMPRESSED_TRUE) { + if (args->compressed == COMPRESSED_ZLIB || args->compressed == COMPRESSED_TRUE) { /* * 16 + MAX_WBITS means that Zlib should expect and decode a * gzip header. @@ -583,7 +582,7 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby } else #endif #ifdef CONFIG_ZSTD - if (compressed == COMPRESSED_ZSTD) { + if (args->compressed == COMPRESSED_ZSTD) { if ((zstd_state.dctx = ZSTD_createDStream()) == NULL) { ERROR("ZSTD_createDStream failed"); ret = -EFAULT; @@ -595,22 +594,22 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby } else #endif { - TRACE("Requested decompression method (%d) is not configured!", compressed); + TRACE("Requested decompression method (%d) is not configured!", args->compressed); ret = -EINVAL; goto copyfile_exit; } } - if (seek) { - int fdout = (out != NULL) ? *(int *)out : -1; + if (args->seek) { + int fdout = (args->out != NULL) ? *(int *)args->out : -1; if (fdout < 0) { ERROR("out argument: invalid fd or pointer"); ret = -EFAULT; goto copyfile_exit; } - TRACE("offset has been defined: %llu bytes", seek); - if (lseek(fdout, seek, SEEK_SET) < 0) { + TRACE("offset has been defined: %llu bytes", args->seek); + if (lseek(fdout, args->seek, SEEK_SET) < 0) { ERROR("offset argument: seek failed"); ret = -EFAULT; goto copyfile_exit; @@ -620,7 +619,7 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby step = &input_step; state = &input_state; - if (encrypted) { + if (args->encrypted) { decrypt_state.upstream_step = step; decrypt_state.upstream_state = state; step = &decrypt_step; @@ -628,7 +627,7 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby } #if defined(CONFIG_GUNZIP) || defined(CONFIG_ZSTD) - if (compressed) { + if (args->compressed) { decompress_state.upstream_step = step; decompress_state.upstream_state = state; step = decompress_step; @@ -644,7 +643,7 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby if (ret == 0) { break; } - if (skip_file) { + if (args->skip_file) { continue; } len = ret; @@ -654,31 +653,32 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby * results corrupted. This lets the cleanup routine * to remove it */ - if (callback(out, buffer, len) < 0) { + if (callback(args->out, buffer, len) < 0) { ret = -ENOSPC; goto copyfile_exit; } - percent = (unsigned)(100ULL * (nbytes - input_state.nbytes) / nbytes); + percent = (unsigned)(100ULL * (args->nbytes - input_state.nbytes) / args->nbytes); if (percent != prevpercent) { prevpercent = percent; swupdate_progress_update(percent); } } - if (IsValidHash(hash) && hash_compare(input_state.dgst, hash) < 0) { + if (IsValidHash(args->hash) && hash_compare(input_state.dgst, args->hash) < 0) { ret = -EFAULT; goto copyfile_exit; } - if (!inbuf) { - ret = _fill_buffer(fdin, buffer, NPAD_BYTES(*offs), offs, checksum, NULL); + if (!args->inbuf) { + ret = _fill_buffer(args->fdin, buffer, NPAD_BYTES(*args->offs), + args->offs, args->checksum, NULL); if (ret < 0) DEBUG("Padding bytes are not read, ignoring"); } - if (checksum != NULL) { - *checksum = input_state.checksum; + if (args->checksum != NULL) { + *args->checksum = input_state.checksum; } ret = 0; @@ -704,57 +704,23 @@ copyfile_exit: return ret; } -int copyfile(int fdin, void *out, size_t nbytes, unsigned long *offs, unsigned long long seek, - int skip_file, int __attribute__ ((__unused__)) compressed, - uint32_t *checksum, unsigned char *hash, bool encrypted, const char *imgivt, writeimage callback) -{ - return __swupdate_copy(fdin, - NULL, - out, - nbytes, - offs, - seek, - skip_file, - compressed, - checksum, - hash, - encrypted, - imgivt, - callback); -} - -int copybuffer(unsigned char *inbuf, void *out, size_t nbytes, int __attribute__ ((__unused__)) compressed, - unsigned char *hash, bool encrypted, const char *imgivt, writeimage callback) -{ - return __swupdate_copy(-1, - inbuf, - out, - nbytes, - NULL, - 0, - 0, - compressed, - NULL, - hash, - encrypted, - imgivt, - callback); -} - int copyimage(void *out, struct img_type *img, writeimage callback) { - return copyfile(img->fdin, - out, - img->size, - (unsigned long *)&img->offset, - img->seek, - 0, /* no skip */ - img->compressed, - &img->checksum, - img->sha256, - img->is_encrypted, - img->ivt_ascii, - callback); + struct swupdate_copy copy = { + .fdin = img->fdin, + .out = out, + .callback = callback, + .nbytes = img->size, + .offs = (unsigned long*)&img->offset, + .seek = img->seek, + .skip_file = 0, + .compressed = img->compressed, + .checksum = &img->checksum, + .hash = img->sha256, + .encrypted = img->is_encrypted, + .imgivt = img->ivt_ascii, + }; + return copyfile(©); } int extract_cpio_header(int fd, struct filehdr *fhdr, unsigned long *offset) @@ -837,8 +803,15 @@ int cpio_scan(int fd, struct swupdate_cfg *cfg, off_t start) * use copyfile for checksum and hash verification, as we skip file * we do not have to provide fdout */ - if (copyfile(fd, NULL, fdh.size, &offset, 0, 1, 0, &checksum, img ? img->sha256 : NULL, - false, NULL, NULL) != 0) { + struct swupdate_copy copy = { + .fdin = fd, + .nbytes = fdh.size, + .offs = &offset, + .skip_file = 1, + .checksum = &checksum, + .hash = img ? img->sha256 : NULL, + }; + if (copyfile(©) != 0) { ERROR("invalid archive"); return -1; } diff --git a/core/installer.c b/core/installer.c index cc5ca34b013d..0cb06b2ca419 100644 --- a/core/installer.c +++ b/core/installer.c @@ -141,13 +141,18 @@ static int extract_scripts(struct imglist *head) return -ENOENT; } - ret = copyfile(fdin, &fdout, script->size, &offset, 0, 0, - script->compressed, - &checksum, - script->sha256, - script->is_encrypted, - script->ivt_ascii, - NULL); + struct swupdate_copy copy = { + .fdin = fdin, + .out = &fdout, + .nbytes = script->size, + .offs = &offset, + .compressed = script->compressed, + .checksum = &checksum, + .hash = script->sha256, + .encrypted = script->is_encrypted, + .imgivt = script->ivt_ascii, + }; + ret = copyfile(©); close(fdin); close(fdout); diff --git a/core/stream_interface.c b/core/stream_interface.c index 5f3ad2e3fd93..14a483b19648 100644 --- a/core/stream_interface.c +++ b/core/stream_interface.c @@ -103,8 +103,15 @@ static int extract_file_to_tmp(int fd, const char *fname, unsigned long *poffs, if (fdout < 0) return -1; - if (copyfile(fd, &fdout, fdh.size, poffs, 0, 0, 0, &checksum, NULL, - encrypted, NULL, NULL) < 0) { + struct swupdate_copy copy = { + .fdin = fd, + .out = &fdout, + .nbytes = fdh.size, + .offs = poffs, + .checksum = &checksum, + .encrypted = encrypted, + }; + if (copyfile(©) < 0) { close(fdout); return -1; } @@ -230,6 +237,13 @@ static int extract_files(int fd, struct swupdate_cfg *software) fdout = -1; offset = 0; + struct swupdate_copy copy = { + .fdin = fd, + .out = &fdout, + .nbytes = fdh.size, + .offs = &offset, + .checksum = &checksum, + }; /* * If images are not streamed directly into the target * copy them into TMPDIR to check if it is all ok @@ -243,7 +257,8 @@ static int extract_files(int fd, struct swupdate_cfg *software) close(fdout); return -1; } - if (copyfile(fd, &fdout, fdh.size, &offset, 0, 0, 0, &checksum, img->sha256, false, NULL, NULL) < 0) { + copy.hash = img->sha256; + if (copyfile(©) < 0) { close(fdout); return -1; } @@ -255,7 +270,8 @@ static int extract_files(int fd, struct swupdate_cfg *software) break; case SKIP_FILE: - if (copyfile(fd, &fdout, fdh.size, &offset, 0, skip, 0, &checksum, NULL, false, NULL, NULL) < 0) { + copy.skip_file = 1; + if (copyfile(©) < 0) { return -1; } if (!swupdate_verify_chksum(checksum, &fdh)) { diff --git a/handlers/copy_handler.c b/handlers/copy_handler.c index 3e2f1f40f9e5..0dcb540a119e 100644 --- a/handlers/copy_handler.c +++ b/handlers/copy_handler.c @@ -121,18 +121,14 @@ static int copy_single_file(const char *path, ssize_t size, struct img_type *img * Copying from device itself, * no encryption or compression */ - ret = copyfile(fdin, - &fdout, - size, - &offset, - 0, - 0, /* no skip */ - 0, /* no compressed */ - &checksum, - 0, /* no sha256 */ - false, /* no encrypted */ - NULL, /* no IVT */ - NULL); + struct swupdate_copy copy = { + .fdin = fdin, + .out = &fdout, + .nbytes = size, + .offs = &offset, + .checksum = &checksum, + }; + ret = copyfile(©); close(fdout); void *status; diff --git a/handlers/delta_handler.c b/handlers/delta_handler.c index 5ec243ee01d9..a14181f11d49 100644 --- a/handlers/delta_handler.c +++ b/handlers/delta_handler.c @@ -161,14 +161,14 @@ static int network_process_data(multipart_parser* p, const char *at, size_t leng zck_get_chunk_number(priv->chunk), priv->current.chunksize); if (priv->current.chunksize != 0) { - ret = copybuffer(priv->current.buf, - &priv->fdout, - priv->current.chunksize, - COMPRESSED_ZSTD, - hash, - 0, - NULL, - NULL); + struct swupdate_copy copy = { + .inbuf = priv->current.buf, + .out = &priv->fdout, + .nbytes = priv->current.chunksize, + .compressed = COMPRESSED_ZSTD, + .hash = hash, + }; + ret = copyfile(©); } else ret = 0; /* skipping, nothing to be copied */ /* Buffer can be discarged */ @@ -178,7 +178,7 @@ static int network_process_data(multipart_parser* p, const char *at, size_t leng * if an error occurred, stops */ if (ret) { - ERROR("copybuffer failed !"); + ERROR("copyfile failed !"); priv->error_in_parser = true; return -EFAULT; } @@ -845,8 +845,15 @@ static bool copy_existing_chunks(zckChunk **dstChunk, struct hnd_priv *priv) zck_get_chunk_number(chunk), start, len); - ret = copyfile(priv->fdsrc, &priv->fdout, len, &offset, 0, 0, COMPRESSED_FALSE, - &checksum, hash, false, NULL, NULL); + struct swupdate_copy copy = { + .fdin = priv->fdsrc, + .out = &priv->fdout, + .nbytes = len, + .offs = &offset, + .checksum = &checksum, + .hash = hash, + }; + ret = copyfile(©); free(sha); if (ret) diff --git a/handlers/readback_handler.c b/handlers/readback_handler.c index 70a82c1a893c..9e462eabe8e5 100644 --- a/handlers/readback_handler.c +++ b/handlers/readback_handler.c @@ -102,18 +102,14 @@ static int readback_postinst(struct img_type *img) * the input device. */ unsigned long offset_out = 0; - int status = copyfile(fdin, - NULL, /* no output */ - size, - &offset_out, - 0, /* no output seek */ - 1, /* skip file, do not write to the output */ - 0, /* no compressed */ - NULL, /* no checksum */ - hash, - false, /* no encrypted */ - NULL, /* no IVT */ - NULL); /* no callback */ + struct swupdate_copy copy = { + .fdin = fdin, + .nbytes = size, + .offs = &offset_out, + .skip_file = 1, + .hash = hash, + }; + int status = copyfile(©); if (status == 0) { INFO("Readback verification success"); } else { diff --git a/include/util.h b/include/util.h index 77da1b17ce15..1703514269f1 100644 --- a/include/util.h +++ b/include/util.h @@ -56,13 +56,42 @@ typedef enum { SERVER_ID_REQUESTED, } server_op_res_t; -enum { +enum compression_type { COMPRESSED_FALSE, COMPRESSED_TRUE, COMPRESSED_ZLIB, COMPRESSED_ZSTD, }; +typedef int (*writeimage) (void *out, const void *buf, size_t len); + +struct swupdate_copy { + /* input: either fdin is set or fdin < 0 and inbuf */ + int fdin; + unsigned char *inbuf; + /* data handler callback and output argument. + * out must point to a fd if seeking */ + writeimage callback; + void *out; + /* amount of data to copy */ + size_t nbytes; + /* pointer to offset within source, must be set for fd */ + unsigned long *offs; + /* absolute offset to seek in output (*out) if non-zero */ + unsigned long long seek; + /* skip callback: only verify input */ + int skip_file; + /* decompression to use */ + enum compression_type compressed; + /* cpio crc checksum */ + uint32_t *checksum; + /* sw-description sha256 checksum */ + unsigned char *hash; + /* encryption */ + bool encrypted; + const char *imgivt; +}; + /* * loglevel is used into TRACE / ERROR * for values > LASTLOGLEVEL, it is an encoded field @@ -188,8 +217,6 @@ bool strtobool(const char *s); /* * Function to extract / copy images */ -typedef int (*writeimage) (void *out, const void *buf, size_t len); - void *saferealloc(void *ptr, size_t size); int copy_write(void *out, const void *buf, size_t len); #if defined(__FreeBSD__) @@ -200,13 +227,8 @@ int copy_write_padded(void *out, const void *buf, size_t len); size_t strlcpy(char *dst, const char * src, size_t size); #endif -int copyfile(int fdin, void *out, size_t nbytes, unsigned long *offs, - unsigned long long seek, - int skip_file, int compressed, uint32_t *checksum, - unsigned char *hash, bool encrypted, const char *imgivt, writeimage callback); +int copyfile(struct swupdate_copy *copy); int copyimage(void *out, struct img_type *img, writeimage callback); -int copybuffer(unsigned char *inbuf, void *out, size_t nbytes, int compressed, - unsigned char *hash, bool encrypted, const char *imgivt, writeimage callback); int openfileoutput(const char *filename); int mkpath(char *dir, mode_t mode); int swupdate_file_setnonblock(int fd, bool block);