From patchwork Tue Oct 11 11:17:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 1688601 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4864:20::137; helo=mail-lf1-x137.google.com; envelope-from=swupdate+bncbcxploxj6ikrbpfbswnamgqebdgynhi@googlegroups.com; receiver=) 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=20210112 header.b=Txv8/Otb; dkim-atps=neutral Received: from mail-lf1-x137.google.com (mail-lf1-x137.google.com [IPv6:2a00:1450:4864:20::137]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MmtXw58R7z23jy for ; Tue, 11 Oct 2022 22:17:19 +1100 (AEDT) Received: by mail-lf1-x137.google.com with SMTP id b34-20020a0565120ba200b004a2542bd072sf3628147lfv.2 for ; Tue, 11 Oct 2022 04:17:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1665487037; cv=pass; d=google.com; s=arc-20160816; b=aM+6+p4mfc3J2c3W8kNmb0+vecHvdkS9k2pFt0LtF+pGc8Hn7QYdyoL4CUBulam4bC R0Jmc5qWYQ+Q9ucKBpe+u7942b8YU7UaHc1Izw530Y0Am+ZGpcRg0GONbZpJ1g3NfP8M HgtRdrmByD8ZM/bROP7hSwsM+Ex2qWFj94WBQN5EC9xhPpHAuTbOU7eqsjfILDKLL9AT whmfpBBnMkncRV5i7ZF8s5a0dUu0nHqSTzVb0TgVtaNmMDzomegPY4fNHiXoKuljMdl8 ZboJnEWI7QOmlMQSTHaUd1rjSUJyvJSTe9j1+u8G1ZWEYJSfuiiVKiG1uDy/g6SF0i1s hkLQ== 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=znbpffLwUHtFhaA7iUQCbqIxaf7w/e5GCsoGGAO7Lxw=; b=0Hj2fYj4uO64I2HuITTyD+r1XyiXpHiXeRaqRWW3EU9LcI7eaxjX1W+e5AoBuHcbnX vF9U1BOpcs3Vh6TzFfvTcnBz/LqiTZqYrKYTozePFf1KixIgK+L23a+voxHve7Lr9Z4S q8ERIk9vxpmDqR05/4l1xpetcZQHNblGG44oSIzW685/z7qcRtat8dd8YGjIfuxsUvew QXmr/yLrMFdyF3W2YAaZ9BdC3t65oJjxoMYvV6opazBjezsxKWm50Z3Q9fepJA4Up0ir /pgpc4cG1sQDRaBWiRF4jluszRStzakewC2iWuA7U6W5ST35Y07o0ImBfkx6ftoG7nYi 9SAQ== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 2001:a60:0:28:0:1:25:1 is neither permitted nor denied by domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; 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=znbpffLwUHtFhaA7iUQCbqIxaf7w/e5GCsoGGAO7Lxw=; b=Txv8/Otb/+XJgLskq93gvkHvN5OHElxn4mrdoyzVdEbwNf16+xVTawKitsq3biyEcO dJ/lhoNRF6oBLoU2Y5etR/8KlUDe+eMpNpt/b5sXPbk1pa+/oWla83NrarOf538Tp6gF krKj/mcffGFna5Nmlq42FJhKnse6vBhU24uOvQo4+ljskkeWU4dyHftyCDyF7uc+OZPv xrv1yGUEw7pgaCZAblSROpa6CvxNQFboPNorQYClsljzgs1AiKcAF6Fov2430ekkC1Qk EtwvSWgolH5EaUuabM9k9j2UxJQNgcC7UrT8hCvOXRzxS0vwN4YQ68TiU6I5MtFqOtvs vRPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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-gm-message-state:sender:from:to:cc:subject:date:message-id :reply-to; bh=znbpffLwUHtFhaA7iUQCbqIxaf7w/e5GCsoGGAO7Lxw=; b=2EMk9hOXhXPAP9y/O0v2hK7YbrSSQjijQcZb7qntHNIZzGhcG8Fio/2WL7FlAvk8aT CfF0gvXEjhXLZ4CovbLPJuieEjoullgnyw4a4ZpHyVEY+G5M3giEWhdG3mi918EUbRCg asMTQLhJXUL9PmbQjjXuE/YbQBR8qH58SjrW+8g2OV85+ORSvF7Wcb1K/0fWiCsUn5TN Fwii31ihn39s7HBVpxSBMMmq5JJyOw0YSM5XtdGx1uAlk7UhF9EIPNOg8aNBrf4Oe4Du +Zs94RKUvWT1RKFINYAHHh5yL2P7QfSdtG1Tl5Tmqwifx46faIqL/Inwpq0PopzpArbM AX1w== Sender: swupdate@googlegroups.com X-Gm-Message-State: ACrzQf24Zq24kCfrkgzs/DH9rr4wJ6EZ/MkdN2N90v29QH57m0rGuo3G 2bfPTnKWhfQsTCylIUF+mFE= X-Google-Smtp-Source: AMsMyM57DaCD1oRsrM0lXBZ1nlBBk7Tat6fN/d3cwIJif0UO9HDuIQXt3zdk+5/z9tA0jT5b8EC9CA== X-Received: by 2002:a2e:83c6:0:b0:26d:e24c:ec01 with SMTP id s6-20020a2e83c6000000b0026de24cec01mr8217427ljh.340.1665487037036; Tue, 11 Oct 2022 04:17:17 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:ac2:4ec6:0:b0:4a2:3951:eac8 with SMTP id p6-20020ac24ec6000000b004a23951eac8ls2531536lfr.0.-pod-prod-gmail; Tue, 11 Oct 2022 04:17:15 -0700 (PDT) X-Received: by 2002:a05:6512:3404:b0:4a2:c77d:9212 with SMTP id i4-20020a056512340400b004a2c77d9212mr6010542lfr.489.1665487035560; Tue, 11 Oct 2022 04:17:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665487035; cv=none; d=google.com; s=arc-20160816; b=BueSjz9jy7Ni/29HHgbeBDzJNVqCbcKHNSgjdqHZYT+KeDXGjT1X0xCLLXFJOdBUdH vJffVA1G/YcYpigiGT5pmeEZjct6inwSgskX2v5FlxYf0rFhLilucWGT21et3x34bf77 tn1MvVGb2o2TrF686GYrMJDaBYhkqE90jPAmcY9DnaBFIdS6Foc25rGhAHQhYQB2rN5g as2d5QxOe5pYWlf2RSq8djUvp4dfskpy+P1dNRS6RM5I/WzXOOZDg6Xy14IeqxtgNRmF PpeU16AbzrlgL1ze/dsG9BsCxTSVRMOtIrGIWvia1E1zDQBtfjOS1eOAHCvZqk12CrQ0 IoHg== 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; bh=y+wo7OfB4eTrJnRHA/l0icGwNPiPeOzW8m160yeFtAY=; b=ZTkDZYAaBWIStIeE5bnHVtyDLe+o3L+B2J3vYW6bjW/XVLQSpvLab05ArQrKtwZYsU 2jw3A/1PoKassVA/Kkn0EiQabGfsUGF/iN7GvnM8y31pWUvF9axc3ivuA/KeFltm1zI6 UKbnoRvFzH8aNMNjwQMXvTWBNZ7zap1mUKgyjQMeRSVi+ib/UhjuFK0XBg1ylSZUOt/0 sKyIK9tb7pnWYtFhL09ybFh0oKn68EeOqfvw7q6GqyzNkWYZH3vPdGK6ZtxeCtAEyXmw C5764fk/8dZfEJsu/sfwLxoJc/5O9gayKiz6Qog8NbTY3c9Jxbaq9njPxZfPZkWN79Wj Ub7Q== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 2001:a60:0:28:0:1:25:1 is neither permitted nor denied by domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de Received: from mail-out.m-online.net (mail-out.m-online.net. [2001:a60:0:28:0:1:25:1]) by gmr-mx.google.com with ESMTPS id bj40-20020a2eaaa8000000b0026dee3f71aesi317201ljb.5.2022.10.11.04.17.15 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Oct 2022 04:17:15 -0700 (PDT) Received-SPF: neutral (google.com: 2001:a60:0:28:0:1:25:1 is neither permitted nor denied by domain of sbabic@denx.de) client-ip=2001:a60:0:28:0:1:25:1; Received: from frontend03.mail.m-online.net (unknown [192.168.6.182]) by mail-out.m-online.net (Postfix) with ESMTP id 4MmtXq04yjz1s946; Tue, 11 Oct 2022 13:17:15 +0200 (CEST) Received: from localhost (dynscan3.mnet-online.de [192.168.6.84]) by mail.m-online.net (Postfix) with ESMTP id 4MmtXp6xvpz1qqlR; Tue, 11 Oct 2022 13:17:14 +0200 (CEST) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan3.mail.m-online.net [192.168.6.84]) (amavisd-new, port 10024) with ESMTP id 834yX39wulge; Tue, 11 Oct 2022 13:17:13 +0200 (CEST) Received: from babic.homelinux.org (host-88-217-136-221.customer.m-online.net [88.217.136.221]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPS; Tue, 11 Oct 2022 13:17:13 +0200 (CEST) Received: from localhost (mail.babic.homelinux.org [127.0.0.1]) by babic.homelinux.org (Postfix) with ESMTP id 77E464540D5A; Tue, 11 Oct 2022 13:17:13 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at babic.homelinux.org Received: from babic.homelinux.org ([IPv6:::1]) by localhost (mail.babic.homelinux.org [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id aRbftFeXGOJu; Tue, 11 Oct 2022 13:17:09 +0200 (CEST) Received: from paperino.fritz.box (paperino.fritz.box [192.168.178.48]) by babic.homelinux.org (Postfix) with ESMTP id 5146C4541362; Tue, 11 Oct 2022 13:17:09 +0200 (CEST) From: Stefano Babic To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH 2/4] Introduce generic copy handler Date: Tue, 11 Oct 2022 13:17:04 +0200 Message-Id: <20221011111706.455706-2-sbabic@denx.de> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221011111706.455706-1-sbabic@denx.de> References: <20221011111706.455706-1-sbabic@denx.de> MIME-Version: 1.0 X-Original-Sender: sbabic@denx.de X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 2001:a60:0:28:0:1:25:1 is neither permitted nor denied by domain of sbabic@denx.de) smtp.mailfrom=sbabic@denx.de 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: , The current "rawcopy" handler is quite limited. It just works as "raw" handler. One common use case is to orvide a copy from a MTD device, and then it is useless. Add a copy handler that runs in a chain another handler, like "flash", so that any handler can be reused. Signed-off-by: Stefano Babic --- handlers/Config.in | 8 ++ handlers/Makefile | 3 +- handlers/chain_handler.c | 48 +++++++++ handlers/copy_handler.c | 204 ++++++++++++++++++++++++++++++++++++++ include/chained_handler.h | 13 +++ 5 files changed, 275 insertions(+), 1 deletion(-) create mode 100644 handlers/chain_handler.c create mode 100644 handlers/copy_handler.c create mode 100644 include/chained_handler.h diff --git a/handlers/Config.in b/handlers/Config.in index 11c0b2d..744968e 100644 --- a/handlers/Config.in +++ b/handlers/Config.in @@ -60,6 +60,14 @@ config CFIHAMMING1 You do not need this if you do not have an OMAP SoC. +config COPY + bool "copy" + default n + help + This works as script and copies files / images to a + different destination. It requires a chained handler + so that it works even with flash or special devices. + config DELTA bool "delta" depends on HAVE_LIBCURL diff --git a/handlers/Makefile b/handlers/Makefile index 2b6faee..847d59f 100644 --- a/handlers/Makefile +++ b/handlers/Makefile @@ -7,9 +7,10 @@ # not drop the handler if it is not called. # Handler can be called dynamically based # on the received image type. -obj-y += dummy_handler.o +obj-y += dummy_handler.o chain_handler.o obj-$(CONFIG_ARCHIVE) += archive_handler.o obj-$(CONFIG_BOOTLOADERHANDLER) += boot_handler.o +obj-$(CONFIG_COPY) += copy_handler.o obj-$(CONFIG_CFI) += flash_handler.o obj-$(CONFIG_DELTA) += delta_handler.o delta_downloader.o zchunk_range.o obj-$(CONFIG_DISKFORMAT_HANDLER) += diskformat_handler.o diff --git a/handlers/chain_handler.c b/handlers/chain_handler.c new file mode 100644 index 0000000..75e79ee --- /dev/null +++ b/handlers/chain_handler.c @@ -0,0 +1,48 @@ +/* + * (C) Copyright 2022 + * Stefano Babic, DENX Software Engineering, sbabic@denx.de. + * + * SPDX-License-Identifier: GPL-2.0-only + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "chained_handler.h" +#include "installer.h" +#include "pctl.h" +#include "util.h" + +/* + * Thread to start the chained handler. + * This received from FIFO the reassembled stream with + * the artifact and can pass it to the handler responsible for the install. + */ +void *chain_handler_thread(void *data) +{ + struct chain_handler_data *priv = (struct chain_handler_data *)data; + struct img_type *img = &priv->img; + unsigned long ret; + + thread_ready(); + if (img->fdin < 0) { + return (void *)1; + } + + img->install_directly = true; + ret = install_single_image(img, false); + + if (ret) { + ERROR("Chain handler return with Error"); + close(img->fdin); + } + + return (void *)ret; +} + + diff --git a/handlers/copy_handler.c b/handlers/copy_handler.c new file mode 100644 index 0000000..6d1c577 --- /dev/null +++ b/handlers/copy_handler.c @@ -0,0 +1,204 @@ +/* + * (C) Copyright 2022 + * Stefano Babic, DENX Software Engineering, sbabic@denx.de. + * + * SPDX-License-Identifier: GPL-2.0-only + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __FreeBSD__ +#include +// the ioctls are almost identical except for the name, just alias it +#define BLKGETSIZE64 DIOCGMEDIASIZE +#else +#include +#endif + +#include +#include "swupdate.h" +#include "handler.h" +#include "util.h" +#include "chained_handler.h" +#include "installer.h" + +#define PIPE_READ 0 +#define PIPE_WRITE 1 + +static void copy_handler(void); + +static int copy_image_file(struct img_type *img, void *data) +{ + int ret; + int fdout, fdin; + struct dict_list *proplist; + struct dict_list_elem *entry; + uint32_t checksum; + unsigned long offset = 0; + size_t size; + struct stat statbuf; + struct script_handler_data *script_data; + struct chain_handler_data priv; + pthread_t chain_handler_thread_id; + int pipes[2]; + char *path = NULL; + struct mtd_info_user mtdinfo; + + if (!data) + return -1; + + script_data = data; + + proplist = dict_get_list(&img->properties, "type"); + if (proplist) { + entry = LIST_FIRST(proplist); + /* check if this should just run as pre or post install */ + if (entry) { + if (strcmp(entry->value, "preinstall") && strcmp(entry->value, "postinstall")) { + ERROR("Type can be just preinstall or postinstall"); + return -EINVAL; + } + script_fn type = !strcmp(entry->value, "preinstall") ? PREINSTALL : POSTINSTALL; + if (type != script_data->scriptfn) { + TRACE("Script set to %s, skipping", entry->value); + return 0; + } + } + } + + proplist = dict_get_list(&img->properties, "copyfrom"); + if (!proplist || !(entry = LIST_FIRST(proplist))) { + ERROR("Missing source device, no copyfrom property"); + return -EINVAL; + } + + path = realpath(entry->value, NULL); + if (!path) { + ERROR("%s cannot be resolved", entry->value); + return -EINVAL; + } + + /* + * Get information about source (size) + */ + fdin = open(path, O_RDONLY); + if (fdin < 0) { + ERROR("%s cannot be opened: %s", path, strerror(errno)); + free(path); + return -EINVAL; + } + + ret = fstat(fdin, &statbuf); + if (ret < 0) { + ERROR("Cannot be retrieved information on %s", path); + free(path); + close(fdin); + return -ENODEV; + } + + /* + * Detect the size if not set in sw-descriptiont + */ + size = dict_get_value(&img->properties, "size") ? ustrtoull(dict_get_value(&img->properties, "size"), NULL, 0) : 0; + if (!size) { + if (S_ISREG(statbuf.st_mode)) + size = statbuf.st_size; + else if (S_ISBLK(statbuf.st_mode) && (ioctl(fdin, BLKGETSIZE64, &size) < 0)) { + ERROR("Cannot get size of Block Device %s", path); + } else if (S_ISCHR(statbuf.st_mode)) { + /* it is maybe a MTD device, just try */ + ret = ioctl(fdin, MEMGETINFO, &mtdinfo); + if (!ret) + size = mtdinfo.size; + } + } + + if (!size) { + ERROR("Size cannot be detected for %s", path); + free(path); + close(fdin); + return -ENODEV; + } + + TRACE("Copying %lu from %s to %s", size, path, img->device); + + free(path); + + if (!size) { + ERROR("Size cannot be detected, please set it, exiting..."); + close(fdin); + return -EFAULT; + } + + if (pipe(pipes) < 0) { + ERROR("Could not create pipes for chained handler, existing..."); + close(fdin); + return -EFAULT; + } + + /* Overwrite some parameters for chained handler */ + memcpy(&priv.img, img, sizeof(*img)); + priv.img.compressed = COMPRESSED_FALSE; + memset(priv.img.sha256, 0, SHA256_HASH_LENGTH); + priv.img.fdin = pipes[PIPE_READ]; + priv.img.size = size; + + /* + * No chain set, fallback to rawcopy + */ + proplist = dict_get_list(&img->properties, "chain"); + if (!proplist || !(entry = LIST_FIRST(proplist))) { + WARN("No chained handler set, fallback to rawcopy"); + strncpy(priv.img.type, "raw", sizeof(priv.img.type)); + } else { + strlcpy(priv.img.type, entry->value, sizeof(priv.img.type)); + TRACE("Set %s handler in the chain", priv.img.type); + } + + fdout = pipes[PIPE_WRITE]; + signal(SIGPIPE, SIG_IGN); + + chain_handler_thread_id = start_thread(chain_handler_thread, &priv); + wait_threads_ready(); + + ret = copyfile(fdin, + &fdout, + size, + &offset, + 0, + 0, /* no skip */ + 0, /* no compressed */ + &checksum, + 0, /* no sha256 */ + false, /* no encrypted */ + NULL, /* no IVT */ + NULL); + + close(fdout); + void *status; + ret = pthread_join(chain_handler_thread_id, &status); + if (ret) { + ERROR("return code from pthread_join() is %d", ret); + } else + ret = (unsigned long)status; + + DEBUG("Chained handler returned %d", ret); + + close(fdin); + return ret; +} + +__attribute__((constructor)) +void copy_handler(void) +{ + register_handler("copy", copy_image_file, + SCRIPT_HANDLER | NO_DATA_HANDLER, NULL); +} diff --git a/include/chained_handler.h b/include/chained_handler.h new file mode 100644 index 0000000..0c74025 --- /dev/null +++ b/include/chained_handler.h @@ -0,0 +1,13 @@ +/* + * (C) Copyright 2022 + * Stefano Babic, DENX Software Engineering, sbabic@denx.de. + * + * SPDX-License-Identifier: GPL-2.0-only + */ + +#include "swupdate.h" +struct chain_handler_data { + struct img_type img; +}; + +extern void *chain_handler_thread(void *data);