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);