From patchwork Mon Jun 3 08:55:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dominique Martinet X-Patchwork-Id: 1942764 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=MAV39d0f; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2607:f8b0:4864:20::c3e; helo=mail-oo1-xc3e.google.com; envelope-from=swupdate+bncbcwivbv7sugrbmek62zamgqemze4dqy@googlegroups.com; receiver=patchwork.ozlabs.org) Received: from mail-oo1-xc3e.google.com (mail-oo1-xc3e.google.com [IPv6:2607:f8b0:4864:20::c3e]) (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 4Vt6yr0zxFz20WQ for ; Mon, 3 Jun 2024 18:56:20 +1000 (AEST) Received: by mail-oo1-xc3e.google.com with SMTP id 006d021491bc7-5b976c32d9dsf3302103eaf.3 for ; Mon, 03 Jun 2024 01:56:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717404977; cv=pass; d=google.com; s=arc-20160816; b=UwAfd/WZWQUAovnDr+LwoyfNKY893SME3Pnt3rVp6G8y+H+ML+P86cesWN8XAHNHf0 jRdSTi3k1rXjeIj7EN3Ew+F9u9Kos0EbkqOsbMkxqC+9UjXUxuShcoDXdDn4X6agE7hU ctnTx7ZUXJYHtCUe3A13mpjVZW02plJfnx1wn/3Y6qWWu+P3PLceut8xBK2HrquMJuXn xUvwm5T3MNkfIqCnuvb4C1nflLDuaMR87gQDnN+e14LF9ctRJkLT+BEQDAFKbYaiB5Fb 9NSlxxaOxVgdj9r0210G0Pcb3Lp8V1k614bAVxctrO0Q94wIgu56vHPWXx8CJXmtSkae 052w== 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=g4Z5/vbW27sJgqKy4BXaZBXv1RcxNhe3pkluEX/x7jE=; fh=iwyXRyWYpWONuXhOLrsqVaZTfYqQIMIBY/FtvYl/rGY=; b=IUI0uCKp37yQ2dJzlGxyVM2eT5LCiqO6tqkr9Jkbi9pgmTDky3T+TOe2Xlu7GiCqLr wvjjcupV/tVzYBDPFdCh/8CgkB9f4/FsJuttimXebS1Vl5wDy+6wYWrkT3Fc8tuzTedy ZgU8+/PbjRE0cAZEN5TLe7Eeck+RQ59i/Ec/UTJGfUaB38QTPpHsrdVYyrTibWVqCFLE vsiwIJSBX7D9jckbiO54wYW5JVezPbEHoocYRNha8RQWzzvUVeBBfGS1jLOK+Bf1JsgK U1OCBI4auBDUrPbI7imGDDlOU4Ul0mRIXaDme6l/BO82UPcZNftAowwJhfrDCF1r6jHz kJSA==; 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=MtQ33SZ1; dkim=pass header.i=@atmark-techno.com header.s=google header.b=UAd4VJmG; 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=1717404977; x=1718009777; 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=g4Z5/vbW27sJgqKy4BXaZBXv1RcxNhe3pkluEX/x7jE=; b=MAV39d0fRyoBxrB3vAJ6WDHC6RgCXBjOFV5M/ongZ3ezuQDv9qaAFSwqFoUEFyUaM/ o7TiZYv30f/iMBT326hCjIS0VeP19pzi1oL3rFDfh6n6axefjsmnAAD6q3NRZcHDBqwB 8Cdw19OEE562lJ7ST9yO5U+FzLwLZB+8y380YpRUiJuuemFVWSc3tCIhSEYF217EWfMD xaZIs5/gEfNcoFHiNeBPx4P90fJXBY6pS9uQEKV8pw9Gm7GhWNv0pFBGFiGCYrhsciuG viJUIDVR1wDlD2SosxcuBKPr0MdWTLDATBAldxXV/20fmyaxOVCiT1zL8rt9PGuQ+l7W NGug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717404977; x=1718009777; 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=g4Z5/vbW27sJgqKy4BXaZBXv1RcxNhe3pkluEX/x7jE=; b=slTP4+JftHUSOkj7xfxChX8tGfD7xH3PZT0rwaOXbB3E/n8F7dHgBrnlRTdk0+qaUN 24a3TGkcJcECwCKf69uv2kgToCbekamS7iuOE9+ZIInnpaJv52A1E+Zbc96JqaA+2W7M NaYt+3NqE7wY83KzC1tvDHX8qH708VID9K6cFvrssl2qqkXLAZhODMFgXwYsZNmIHwqE jjD79thAOrdOYYr0J9dn3Y/oDoObh7MYHkFOCmwJd6vZ4tYJcf2T6TlzssB0ucKkYOqv UR6AAM6KTGcBcvDNa2Jsn7jEpToNYueJdJhjH7ROYlCX4+DGmbfyJvMJ9LryKyPIpQEd 65Sg== Sender: swupdate@googlegroups.com X-Forwarded-Encrypted: i=2; AJvYcCWWi8hS0s9Tu4t1faxNB17aGCfuuwe/4nYMBjeKwDaEIy+hpKOH2GB5XNWN9KjL14i8yoOIMC4qLjTmJ4JRTsFuQYpnQ1/XOV56mcLrCg== X-Gm-Message-State: AOJu0YyOLs8v2CtcfmdOQMMR/q+k/UQrXXwYNeVVpnMn6kxIxxVunaxd AQL94k/FbcDxkY5zqRIHVhLy3+hP5//yNS3VX1i8NMThvpznJus7 X-Google-Smtp-Source: AGHT+IGuo1PSdIyB2vpfjvx0OE5HmBm/3oceBskZZIoNzaQ4ooJtJf5MLo9GafWoojKrT6Vrcv2MMg== X-Received: by 2002:a05:6820:160c:b0:5b2:1095:a58b with SMTP id 006d021491bc7-5ba05d32b7cmr8466655eaf.8.1717404977274; Mon, 03 Jun 2024 01:56:17 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a4a:aa08:0:b0:5ae:1f6c:8983 with SMTP id 006d021491bc7-5b9feca8e7cls1021911eaf.1.-pod-prod-01-us; Mon, 03 Jun 2024 01:56:16 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVzpFWrAe14mY7rdXfZ25pAak4Ha7mYGFs4V7pPmC89iPPBxSk6Ox7S2ATd0nvO1Krc9jV5gg5OnPzrE/lVst8i+qomPPwpfADj X-Received: by 2002:a05:6830:1510:b0:6f0:7ca9:dd09 with SMTP id 46e09a7af769-6f911f26983mr8824465a34.7.1717404976017; Mon, 03 Jun 2024 01:56:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1717404976; cv=none; d=google.com; s=arc-20160816; b=ScyFnyTWUIFB8vvK5rP1ISeE1GskHl03soyhzk04uwPtPMLs3GhMflkMiDp/e0jn8A mHUkSCefRTuxrqjwdkgzPWP1TdHPvrl3jurDf22VdYE5OPL7yu3PkZqYURJjQwmJvx04 9OXEug0UqKSBpFDYognx22YaD5mb7clQoBkj7aFLHPGG7Y204+OwvrMMGkKeS+8SmxxL K1kfaxnIGS88ld4L13sUiUfy88z4Ht1f84XnRSiN6dZkZQPmCWl6jWRvqso3NoXI+4NQ Edy3SJgzIQpH6Sn20JOnoo4tDz0jbmXdKXol7IzaFBDvbMrwEQPSEnXqH//A3Our3XT5 tITw== 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=uUDMe6I2hpMs3Xhcp2x6yMa0I4iDwbfOEx6/NnPGeIw=; fh=2foHRTrtYPPJOLAf/lesImXdXMvdx4snb1yg7YzHah0=; b=R3IsCJ0fHND3EmfPQilWfhm9366cmQKvx/RtPmpz4SCZbuAyuWkFfNBCcLRwwW3mcI oQNpSmOV37gDDlziBvAr0D/pnRnRX2/MfOUj2ccKDNqFCYFLRgtp+L5Cin7SyNCo/wlh 7P6ZJFqZC7Z4HrqwaIzgqYsncNv4xw2h8VynauOmL3muj1GBoKcXniod+mD5bbBgvVap 3gZFDF7hScGFpz5s0Q5dCCtVli24zGI8yR/Mgofjo7viiN1K/lVFl526OAf2YXP6BVog AskJA8RXHmsRko7CouseW4jKWexsz0XQ4eJ1c7FxFslY3VuSYx6/W4IHiJamg1nvCbYV 6USQ==; 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=MtQ33SZ1; dkim=pass header.i=@atmark-techno.com header.s=google header.b=UAd4VJmG; 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 46e09a7af769-6f91049a907si390970a34.0.2024.06.03.01.56.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 01:56: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 B23F3A92 for ; Mon, 3 Jun 2024 17:56:13 +0900 (JST) Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) by gw2.atmark-techno.com (Postfix) with ESMTPS id 33602A4A for ; Mon, 3 Jun 2024 17:56:12 +0900 (JST) Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-2c19ebab03eso3042870a91.2 for ; Mon, 03 Jun 2024 01:56:12 -0700 (PDT) X-Forwarded-Encrypted: i=1; AJvYcCVx6vd2ukRAKht6DTt4K9tusFtV514deA+TyP8sG4wPiBDkZsGovTVtjI3vFsLNCD0fUn/TxydtSEdSrW1SJoMYWMyMzlguyFM9 X-Received: by 2002:a17:90a:ee8d:b0:2c0:1dd7:4221 with SMTP id 98e67ed59e1d1-2c1dc568f90mr6973068a91.10.1717404971060; Mon, 03 Jun 2024 01:56:11 -0700 (PDT) X-Received: by 2002:a17:90a:ee8d:b0:2c0:1dd7:4221 with SMTP id 98e67ed59e1d1-2c1dc568f90mr6973057a91.10.1717404970514; Mon, 03 Jun 2024 01:56:10 -0700 (PDT) Received: from pc-0182.atmarktech (76.125.194.35.bc.googleusercontent.com. [35.194.125.76]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c1a776e17esm7420489a91.15.2024.06.03.01.56.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Jun 2024 01:56:08 -0700 (PDT) Received: from [::1] (helo=pc-0182.atmark.tech) by pc-0182.atmarktech with esmtp (Exim 4.96) (envelope-from ) id 1sE3U6-009riT-0q; Mon, 03 Jun 2024 17:56:06 +0900 From: Dominique Martinet To: stefano.babic@swupdate.org, swupdate@googlegroups.com Cc: Dominique Martinet Subject: [swupdate] [PATCH RFC 4/9] copyfile refactor: replace the 13 arguments by a struct Date: Mon, 3 Jun 2024 17:55:57 +0900 Message-Id: <20240603085602.2351411-5-dominique.martinet@atmark-techno.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240603085602.2351411-1-dominique.martinet@atmark-techno.com> References: <20240603085602.2351411-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=MtQ33SZ1; dkim=pass header.i=@atmark-techno.com header.s=google header.b=UAd4VJmG; 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 | 143 +++++++++++++++--------------------- 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, 155 insertions(+), 140 deletions(-) diff --git a/core/cpio_utils.c b/core/cpio_utils.c index a05a32cd59b5..acb3f3b634f8 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; @@ -618,8 +617,8 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby } #if defined(CONFIG_GUNZIP) || defined(CONFIG_ZSTD) - if (compressed) { - if (encrypted) { + if (args->compressed) { + if (args->encrypted) { decrypt_state.upstream_step = &input_step; decrypt_state.upstream_state = &input_state; decompress_state.upstream_step = &decrypt_step; @@ -632,7 +631,7 @@ static int __swupdate_copy(int fdin, unsigned char *inbuf, void *out, size_t nby state = &decompress_state; } else { #endif - if (encrypted) { + if (args->encrypted) { decrypt_state.upstream_step = &input_step; decrypt_state.upstream_state = &input_state; step = &decrypt_step; @@ -653,7 +652,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; @@ -663,31 +662,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; @@ -713,57 +713,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) @@ -846,8 +812,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);