From patchwork Thu Jun 16 12:40:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe REYNES X-Patchwork-Id: 1644309 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=bfw1r6+J; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4864:20::53a; helo=mail-ed1-x53a.google.com; envelope-from=swupdate+bncbds5hgup7qgbbxgjvskqmgqezeu3vwq@googlegroups.com; receiver=) Received: from mail-ed1-x53a.google.com (mail-ed1-x53a.google.com [IPv6:2a00:1450:4864:20::53a]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LP1xc6tdlz9sG2 for ; Thu, 16 Jun 2022 22:41:06 +1000 (AEST) Received: by mail-ed1-x53a.google.com with SMTP id co13-20020a0564020c0d00b0042dc9ef4f01sf1158692edb.16 for ; Thu, 16 Jun 2022 05:41:06 -0700 (PDT) ARC-Seal: i=3; a=rsa-sha256; t=1655383261; cv=pass; d=google.com; s=arc-20160816; b=SgJX+hedem8mHGlCDBu4uDsnBDqg1ctIBq1AIxPkmFUn6KCZ4H42AEGQIafLoftCfg TP9PSeNP3LCG81DsJU/jNGP51H2TmJaauXPGRMKl32F89cukXmaP3pzHQyBZsJMhctvb TqnUkAQmnKF9/UIcZsr82LBq/td4RqmUa/iYVv2yRsxFTo7CR08ORbQiPv5TkWPZBsXi fvDFBe556XjO9zgrtH8tYaZTC1XoQC5u5rDxJWrqgNH5v9jfM/WDkoTw5yu9Ym0ovp0J oBNrPOjCch/PpAoFwc/nDigGxHTTkreWTkuyhy3yqZtJ497DWwc3uZDPiXAdfFdpH7qS Y4nw== ARC-Message-Signature: i=3; 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:message-id:date :subject:cc:to:from:sender:dkim-signature; bh=9ioCR2zP+7r/pKXnLZ+8kdyTuarl62OL0E47ww7Upag=; b=MCbUX19ewjBhYvwAUAdbI9LyTLc3hRQ9NM2mAHRcRSXujIn+UumlvP4MhHgjnx8KAD OfbRL9g1KGeCFNTo4OZI4dCAKIINwQ4D7E/QOX6QL+hKneTIBpHPML47qjOeMFMU/nSG OK4d5s1f4IBDVwz3tFOseeMy8MKRt4SJfnPPExI6ebHAGujhgKzWB4jqD49y4PYZfA5F U71LWM5viLd0FJRopv6odBr7ekdpAAYGRbf3E/lLfGUVq1kLb8MSAZfFH+xomzvnkLHR j64urbQ6xcU1FrrAhOZdvP3HMKO7Y3qb1fYOoXOkJUMaObzBrTLbYJWjuQIagepltzjd YChA== ARC-Authentication-Results: i=3; gmr-mx.google.com; dkim=pass header.i=@softathome1.onmicrosoft.com header.s=selector1-softathome1-onmicrosoft-com header.b=Vsc6hGtQ; arc=pass (i=1 spf=pass spfdomain=softathome.com); spf=pass (google.com: domain of philippe.reynes@softathome.com designates 2a01:111:f400:7e19::629 as permitted sender) smtp.mailfrom=philippe.reynes@softathome.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:mime-version :x-original-sender:x-original-authentication-results:precedence :mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=9ioCR2zP+7r/pKXnLZ+8kdyTuarl62OL0E47ww7Upag=; b=bfw1r6+J8TJYGaMWW+aR1gNY5/LPFA54h+Qwpr1NgYC6axB9UX4shyHGj8OfWk4XPp OUUufbfh3yHErRMN6Ygx0a8OQY7Nif88F9+VV7YdQjWBFFbLGg8MCyHhv4PNwAxBJ0As xLHaBDVp+CUtsYkZdrbFrFy6BPXaV69HGv9kcwn5ugHzvJcR6tQXpalDu8Lpmliyc6WM LZr69NdYLvWHzhGWha5robyIJPM4xd0GE2r1jbffJhhTJgUGHAmxaxWgccSP9t3xtpgH pMpH1I8C9tPjxnX8RHjNFbwQuAOKmNrs126+Co5WXQuO2CVwIToq2dBPa3ATRQqHl4DR 5bXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=9ioCR2zP+7r/pKXnLZ+8kdyTuarl62OL0E47ww7Upag=; b=73MAzhvO9pCcHKRmjShRHF1JRt1Dq1CkDMUfHYieMlFZVhlZgB/YRgbKdy/ZywNRqs S9zz/tG6kEsZlFwd7BiCOD5oMIR4Taibe+QM2xHmW643NoIjkSFic9eVbZ1JyaBRoZtP en9comB5bGmB2Gr5cB4PzaNWuwaz0C8ws7iAENYo2pEVWpIKH7v/aPnMyeqoWBiUfJ3H J+gvYg3X1zIQIUl9tb5wOglYzYW5LAUozEYwyqLL06dm0kH1YZStjMmxmESdYv4TUjlx sH6QVGqFky8GEzpxJXXilxIUGG47McJntfukiZkYGObSDXE1x3ybG6MkEkpBdnVyM4qO UcIQ== Sender: swupdate@googlegroups.com X-Gm-Message-State: AJIora/QQM1nvkvZ7B0jczo9xniqORqkqSgU9sjiGmdgXoJJoOGysjQl OXGKwc9Br0S3rxMhzPhPv/I= X-Google-Smtp-Source: AGRyM1vyMDWxP1f0XvKhGap5rg8wABI94IuL52Txkpo2IJWxsm+ilZdCvWHFUQy54HRsjRK1572FfQ== X-Received: by 2002:aa7:c9c9:0:b0:431:962f:f61e with SMTP id i9-20020aa7c9c9000000b00431962ff61emr6233618edt.189.1655383260736; Thu, 16 Jun 2022 05:41:00 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a05:6402:709:b0:431:3a5e:75b3 with SMTP id w9-20020a056402070900b004313a5e75b3ls1849100edx.1.gmail; Thu, 16 Jun 2022 05:40:59 -0700 (PDT) X-Received: by 2002:a05:6402:11d2:b0:42d:e68a:eae0 with SMTP id j18-20020a05640211d200b0042de68aeae0mr6216703edw.111.1655383259706; Thu, 16 Jun 2022 05:40:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1655383259; cv=pass; d=google.com; s=arc-20160816; b=Ev4nQob1ZPrqkpnSZoFM3r1wCYZ6qF8jtBYr3X698vtC1TVXyFa8qtlPbY0Bo+c2Js sF68OzHMt49Z9sgWGPkS6ki5upG98B0Htqgd7I1xCTypJMXJ21dgkywB4lPXfS4jmaoK mFIZ3CknfhU+Xg3G2j+A5GOzlg6jQ6ykae3HoxxHJfAcaiYVcSQC/BOEIxBVzOZDKcr6 M8q50BIcsJGirNo3Hgzp5qXASntHyPdpRLiV5NRQK3nXPVQqSqZaS5hzrbh1KMtYRqO9 We/7tlZgTS64N9Mc1LtlDeVDEndgatc9kHnqUs7x9sPy67l6wGEe703In5YaKRCWaQuP jBMw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=JFa2PgCu5uuRT/40xMfqhaP8N5zMf6DRu99O3eTEcJo=; b=JgWyWhkufn/Y0r3NRtwTrPSqB7fB3KtkcPupPOG5tJtLR6Slqy38UfZR0blrYLgiIx tZaPcsmzy3kgWTaiDIJ/iGgwGnBpGDWj8QPULOHIzrhiyhdhoceuxgbPErr/k/wwgE2a yZiN8Kz7mQMAdW59kSOjal3ENLGfsmBLL8fDOpxwrBBtN/hv+bAZCvh7izeZjoXBZIoN kHbm//XtvbTdeGTM93QdM5IL2ceeLHVEQmJN3Ykr8eI6iItRQlgoRDUGQXVXvXjvOiGg noGorWh76oJhM0zCOykop/PMrh8a+MQjPjsV5PQmyXfXrC0qzWDXTtRhrL/d0VIEWcR3 FdFQ== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@softathome1.onmicrosoft.com header.s=selector1-softathome1-onmicrosoft-com header.b=Vsc6hGtQ; arc=pass (i=1 spf=pass spfdomain=softathome.com); spf=pass (google.com: domain of philippe.reynes@softathome.com designates 2a01:111:f400:7e19::629 as permitted sender) smtp.mailfrom=philippe.reynes@softathome.com Received: from FRA01-MR2-obe.outbound.protection.outlook.com (mail-mr2fra01on0629.outbound.protection.outlook.com. [2a01:111:f400:7e19::629]) by gmr-mx.google.com with ESMTPS id j8-20020aa7c0c8000000b0042e15342b19si96717edp.3.2022.06.16.05.40.59 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 Jun 2022 05:40:59 -0700 (PDT) Received-SPF: pass (google.com: domain of philippe.reynes@softathome.com designates 2a01:111:f400:7e19::629 as permitted sender) client-ip=2a01:111:f400:7e19::629; ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k611JAQJrdmHplieqCNp49laqx6N3c6IqyaHKDxJKqNLyqqySc6JmD1/Qt8LvN4UC/F4ejLfdCZjd4gmyWzt8zBYg/vHQoLe2c+qchFJfPlsq2h/Uph8uynM5S3TH7TTcxCD+kcOwXUg5NJ6nyTHxVMpsIWmvSWVh9JQX/UHmihnnjMMwny5ROxmvRMxNpVahFUzwSlqy3lSbcx0sK4mQzdBVPWSG/CBwA/WInL2n6nqF1vo7rkokjQAVI62rg6x4PvedCL8ifPkjXKPIahoP4+ry4DizOUCGKNn9EhVdxMI1346P+Lw9Q1vTJo6yyiei9Q6AL8e/lrurp2fC65uCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JFa2PgCu5uuRT/40xMfqhaP8N5zMf6DRu99O3eTEcJo=; b=avgJuz4I1EscoaorkWCAowGo0q9r8HmydcQtGjNE2XI5WyiEZBtkAuxtj2JD6cUf8knjkgCq5aD4o2Y/1FsM/p2O7WOE9WSy1BsLWKMbzJ26zlEh4BZo+/5wTaxOWNs1nuN4nl3hVyM829ZnSSaURJz8OKKFYn6v22Q3d1sn4RLJotLIA/cz7FZmLwtlo1S0YkOLO3DA9v7XaAOBvrth0NkwDdeofrl0SLPHp0CPfr7bJwaB6yOKPrc7WlmY7hehe9Pyf0cQ1NbzojG+OSXlu7LlOkzAqXCxlO8utbceQ8rzFeIDHOTTjp+5rMThY1Yw8eFTALtouDd21hS9vFQj1A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 149.6.166.170) smtp.rcpttodomain=googlegroups.com smtp.mailfrom=softathome.com; dmarc=bestguesspass action=none header.from=softathome.com; dkim=none (message not signed); arc=none Received: from MRXP264CA0010.FRAP264.PROD.OUTLOOK.COM (2603:10a6:500:15::22) by MR1P264MB3075.FRAP264.PROD.OUTLOOK.COM (2603:10a6:501:3c::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5353.13; Thu, 16 Jun 2022 12:40:57 +0000 Received: from MR2FRA01FT016.eop-fra01.prod.protection.outlook.com (2603:10a6:500:15:cafe::df) by MRXP264CA0010.outlook.office365.com (2603:10a6:500:15::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.12 via Frontend Transport; Thu, 16 Jun 2022 12:40:57 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 149.6.166.170) smtp.mailfrom=softathome.com; dkim=none (message not signed) header.d=none;dmarc=bestguesspass action=none header.from=softathome.com; Received-SPF: Pass (protection.outlook.com: domain of softathome.com designates 149.6.166.170 as permitted sender) receiver=protection.outlook.com; client-ip=149.6.166.170; helo=proxy.softathome.com; pr=C Received: from proxy.softathome.com (149.6.166.170) by MR2FRA01FT016.mail.protection.outlook.com (10.152.50.130) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5353.14 via Frontend Transport; Thu, 16 Jun 2022 12:40:57 +0000 Received: from sah1lpt571.softathome.com (unknown [192.168.75.67]) by proxy.softathome.com (Postfix) with ESMTPSA id 4EDF01FE77; Thu, 16 Jun 2022 14:40:57 +0200 (CEST) From: Philippe Reynes To: swupdate@googlegroups.com Cc: Philippe Reynes Subject: [swupdate] [PATCH v2 1/2] handlers: diskpart_handler.c: add handler for script gptswap Date: Thu, 16 Jun 2022 14:40:54 +0200 Message-Id: <20220616124055.2163867-1-philippe.reynes@softathome.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a059c645-8b01-453d-070a-08da4f957626 X-MS-TrafficTypeDiagnostic: MR1P264MB3075:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SyzL3BUYTi83QrQiIju4JedltCIaBic7bWwb1unufR6CFYHzl3LogvZdlAH8LikPd0FQD/oCOwb4+HgAInUmcAmefj9XDNQMmWm/b5yT5OAFJ1zDzABNzuYV3dDps5LTFPtsnykYmolXFzZQjVHX2O7ZHQY/zROqu7m7uj50wZaE2PaqprFT1N9sWnm8Y/IgXzXDrjtOEwOMkUC4kKYno70ZVc6Fstdi07Y1fHyvL2uYM6SfzjvrPFa2QUPhDiNzs5jsSb/FLCGV7EBOqDm7pt6xiOrBKP0CN6I3wxRWQfd4LjrI92dc0DD2P8WAGIR0KUM9e19CXtKMvbllok8UADHIcfYSH5obNSf4a6qG/ExvhFAu7CrMuackI2hFx8U/MVenekir5+uq1yoWTdQ7hw3HUAiSn9yOpM8hrjiO0s0LoO/VAUmiMGQJ5+ZZ1bLG7q/EAadxaPFSPlfBeVTwk8rpzwYTdigHW+JIvRiDRarTgNiguzSpgMRLaCajwaaWdj9QCQQu2vQNS4nSMtYdOoiR5b9ZVb+KUolYRUXDaCtWw4JyLRBft2X4l6AxdYcufHH5KXE7JppVXP+lGPxzrzkq2fHNqK0YxGMp7603DgLOxxkrRwyK7iv4aS7IMn74KXrCpIiHkalTcVe3ZjurqzdnYzzree3T56z5ONGY5OSOc3MXl5IBK4khZdgQ5C/x X-Forefront-Antispam-Report: CIP:149.6.166.170;CTRY:FR;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:proxy.softathome.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230016)(4636009)(46966006)(36840700001)(508600001)(6966003)(8676002)(86362001)(81166007)(6666004)(36756003)(6266002)(26005)(7696005)(186003)(316002)(107886003)(426003)(6916009)(1076003)(70206006)(70586007)(336012)(2616005)(47076005)(2906002)(5660300002)(44832011)(8936002)(356005)(83380400001)(82960400001)(36860700001)(4326008)(82310400005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: softathome.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Jun 2022 12:40:57.6334 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a059c645-8b01-453d-070a-08da4f957626 X-MS-Exchange-CrossTenant-Id: aa10e044-e405-4c10-8353-36b4d0cce511 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=aa10e044-e405-4c10-8353-36b4d0cce511;Ip=[149.6.166.170];Helo=[proxy.softathome.com] X-MS-Exchange-CrossTenant-AuthSource: MR2FRA01FT016.eop-fra01.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MR1P264MB3075 X-Original-Sender: philippe.reynes@softathome.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@softathome1.onmicrosoft.com header.s=selector1-softathome1-onmicrosoft-com header.b=Vsc6hGtQ; arc=pass (i=1 spf=pass spfdomain=softathome.com); spf=pass (google.com: domain of philippe.reynes@softathome.com designates 2a01:111:f400:7e19::629 as permitted sender) smtp.mailfrom=philippe.reynes@softathome.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: , Add a handler gptswap to swap gpt partition. This handler is a script that can be used to swap gpt partition names after all the image were flashed (only partition names are swapped). This script may be usefull for a dual bank strategy. For example: scripts: ( { type = "gptswap"; device = "/dev/vdb"; properties = { swap-0 = [ "u-boot-0" , "u-boot-1" ]; swap-1 = [ "kernel-0" , "kernel-1" ]; }; }, ); Signed-off-by: Philippe Reynes --- Changelog: v2: - rebase on master - add a constructor to register the handler gpt_swap_partition doc/source/handlers.rst | 23 ++++ handlers/diskpart_handler.c | 249 ++++++++++++++++++++++++++++++++++++ 2 files changed, 272 insertions(+) diff --git a/doc/source/handlers.rst b/doc/source/handlers.rst index fd8d785..01ec221 100644 --- a/doc/source/handlers.rst +++ b/doc/source/handlers.rst @@ -963,6 +963,29 @@ if the partition table is DOS. It reports an error if the table is GPT. } } +gpt partition swap +.................. + +There is a handler gptswap that allow to swap gpt partitions after all the images were flashed. +This handler only swap the name of the partition. It coud be usefull for a dual bank strategy. +This handler is a script for the point of view of swupdate, so the node that provide it should +be added in the section scripts. + +Simple example: + +:: + + scripts: ( + { + type = "gptswap"; + device = "/dev/vdb"; + properties = + { + swap-0 = [ "u-boot-0" , "u-boot-1" ]; + swap-1 = [ "kernel-0" , "kernel-1" ]; + }; + }, + ); Diskformat Handler ------------------ diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c index 6b0ccd9..925976b 100644 --- a/handlers/diskpart_handler.c +++ b/handlers/diskpart_handler.c @@ -14,8 +14,10 @@ #include #include #include +#include #include #include +#include #include #include #include "swupdate.h" @@ -301,6 +303,80 @@ static int diskpart_get_partitions(struct fdisk_context *cxt, struct diskpart_ta return ret; } +static struct fdisk_partition * +diskpart_fdisk_table_get_partition_by_name(struct fdisk_table *tb, char *name) +{ + struct fdisk_partition *pa = NULL; + struct fdisk_partition *ipa = NULL; + struct fdisk_iter *itr; + const char *iname; + + if (!tb || !name) + goto out; + + itr = fdisk_new_iter(FDISK_ITER_FORWARD); + + while (!fdisk_table_next_partition(tb, itr, &ipa)) { + iname = fdisk_partition_get_name(ipa); + if (iname && !strcmp(iname, name)) { + pa = ipa; + break; + } + } + + fdisk_free_iter(itr); + + out: + return pa; +} + +static struct fdisk_partition * +diskpart_get_partition_by_name(struct diskpart_table *tb, char *name) +{ + struct fdisk_partition *pa = NULL; + + if (!tb || !name) + goto out; + + if (tb->parent) + pa = diskpart_fdisk_table_get_partition_by_name(tb->parent, name); + + out: + return pa; +} + +static int diskpart_swap_partition(struct diskpart_table *tb, + struct create_table *createtable, + char *name1, char *name2) +{ + struct fdisk_partition *pa1 = NULL, *pa2 = NULL; + int ret = -1; + + pa1 = diskpart_get_partition_by_name(tb, name1); + if (!pa1) { + ERROR("Can't find partition %s", name1); + goto out; + } + + pa2 = diskpart_get_partition_by_name(tb, name2); + if (!pa2) { + ERROR("Can't find partition %s", name2); + goto out; + } + + ret = fdisk_partition_set_name(pa1, name2); + if (ret) + goto out; + ret = fdisk_partition_set_name(pa2, name1); + if (ret) + goto out; + + createtable->parent = true; + + out: + return ret; +} + static int diskpart_set_partition(struct fdisk_partition *pa, struct partition_data *part, unsigned long sector_size, @@ -808,6 +884,29 @@ static void diskpart_unref_context(struct fdisk_context *cxt) fdisk_unref_context(cxt); } +static int diskpart_reread_partition(char *device) +{ + int fd, ret = -1; + + fd = open(device, O_RDONLY); + if (fd < 0) { + ERROR("Device %s can't be opened", device); + goto out; + } + + ret = ioctl(fd, BLKRRPART, NULL); + if (ret < 0) { + ERROR("Scan cannot be done on device %s", device); + goto out_close; + } + + out_close: + close(fd); + + out: + return ret; +} + static int diskpart(struct img_type *img, void __attribute__ ((__unused__)) *data) { @@ -1210,6 +1309,149 @@ toggle_boot_exit: return ret; } +static int gpt_swap_partition(struct img_type *img, void *data) +{ + struct fdisk_context *cxt = NULL; + struct diskpart_table *tb = NULL; + int ret = 0; + unsigned long hybrid = 0; + struct hnd_priv priv = { + .labeltype = FDISK_DISKLABEL_DOS, + }; + struct create_table *createtable = NULL; + struct script_handler_data *script_data = data; + char prop[SWUPDATE_GENERAL_STRING_SIZE]; + struct dict_list *partitions; + struct dict_list_elem *partition; + int num, count = 0; + char *name[2]; + + if (!script_data) + return -EINVAL; + + /* + * Call only in case of postinstall + */ + if (script_data->scriptfn != POSTINSTALL) + return 0; + + LIST_INIT(&priv.listparts); + if (!strlen(img->device)) { + ERROR("Partition handler without setting the device"); + return -EINVAL; + } + + createtable = calloc(1, sizeof(*createtable)); + if (!createtable) { + ERROR("OOM allocating createtable !"); + return -ENOMEM; + } + + /* + * Reads flags: nolock and noinuse + */ + priv.nolock = strtobool(dict_get_value(&img->properties, "nolock")); + priv.noinuse = strtobool(dict_get_value(&img->properties, "noinuse")); + + /* + * Create context + */ + ret = diskpart_assign_context(&cxt, img, priv, hybrid, createtable); + if (ret == -EACCES) + goto handler_release; + else if (ret) + goto handler_exit; + + tb = calloc(1, sizeof(*tb)); + if (!tb) { + ERROR("OOM loading partitions !"); + ret = -ENOMEM; + goto handler_exit; + } + + /* + * Fill the in-memory partition table from the disk. + */ + ret = diskpart_get_partitions(cxt, tb, createtable); + if (ret) + goto handler_exit; + + while (1) { + snprintf(prop, sizeof(prop), "swap-%d", count); + partitions = dict_get_list(&img->properties, prop); + if (!partitions) + break; + + num = 0; + LIST_FOREACH(partition, partitions, next) { + if (num >= 2) { + ERROR("Too many partition (%s)", prop); + goto handler_exit; + } + + name[num] = partition->value; + num++; + } + + if (num != 2) { + ERROR("Invalid number (%d) of partition (%s)", num, prop); + goto handler_exit; + } + + TRACE("swap partition %s <-> %s", name[0], name[1]); + + ret = diskpart_swap_partition(tb, createtable, name[0], name[1]); + if (ret) { + ERROR("Can't swap %s and %s", name[0], name[1]); + break; + } + + count++; + } + + /* Reload table for parent */ + ret = diskpart_reload_table(PARENT(cxt), tb->parent); + if (ret) { + ERROR("Can't reload table for parent (err = %d)", ret); + goto handler_exit; + } + + /* Reload table for child */ + if (IS_HYBRID(cxt)) { + ret = diskpart_reload_table(cxt, tb->child); + if (ret) { + ERROR("Can't reload table for child (err = %d)", ret); + goto handler_exit; + } + } + + /* Write table */ + ret = diskpart_write_table(cxt, createtable, priv.nolock, priv.noinuse); + if (ret) { + ERROR("Can't write table (err = %d)", ret); + goto handler_exit; + } + +handler_exit: + if (tb) + diskpart_unref_table(tb); + if (cxt && fdisk_get_devfd(cxt) >= 0) + if (fdisk_deassign_device(cxt, 0)) + WARN("Error deassign device %s", img->device); + +handler_release: + if (cxt) + diskpart_unref_context(cxt); + + /* + * Re-read the partition table to be sure that SWupdate does not + * try to acces the partitions before the kernel is ready + */ + diskpart_reread_partition(img->device); + + return ret; +} + __attribute__((constructor)) void diskpart_handler(void) { @@ -1223,3 +1465,10 @@ void diskpart_toggle_boot(void) register_handler("toggleboot", toggle_boot, SCRIPT_HANDLER | NO_DATA_HANDLER, NULL); } + +__attribute__((constructor)) +void diskpart_gpt_swap_partition(void) +{ + register_handler("gptswap", gpt_swap_partition, + SCRIPT_HANDLER | NO_DATA_HANDLER, NULL); +} From patchwork Thu Jun 16 12:40:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe REYNES X-Patchwork-Id: 1644308 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=M1CDgU2f; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4864:20::13b; helo=mail-lf1-x13b.google.com; envelope-from=swupdate+bncbds5hgup7qgbbxojvskqmgqesowvcsq@googlegroups.com; receiver=) Received: from mail-lf1-x13b.google.com (mail-lf1-x13b.google.com [IPv6:2a00:1450:4864:20::13b]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LP1xd0LN0z9sGF for ; Thu, 16 Jun 2022 22:41:07 +1000 (AEST) Received: by mail-lf1-x13b.google.com with SMTP id u7-20020a05651206c700b00479723664a9sf741521lff.6 for ; Thu, 16 Jun 2022 05:41:06 -0700 (PDT) ARC-Seal: i=3; a=rsa-sha256; t=1655383262; cv=pass; d=google.com; s=arc-20160816; b=uBS4i9/EDJVMH+mMMvhuKgSdp0QuZTfc0G7rt4+sPyXi7OezvWWBA76LWB1SPqlXGQ PVvT0DbIfyLr2QZ6dVGQSHugMoZNuuHzW1faZTCUwlAkpi4MvgEN0jIk14YWj3L9dY2E oLYOR7mkWUIfSbSr3fRGV1Wmni10ZyggTZVwA5x0V/vro62Utn4arl+30AJWAxgujE2p roQsvDsw87guA7YjWFoHGOvFbvMCTIqgATQNqJryA0rC4D/vzXi/kFxzq5oNUJRZ/vV0 xSu/RpPsndsKKC6l7yYbAGTZxz90kUwzm6v1aE6fmPNxn9h+1aC07Zp5+VPMYnQAkPmu 8p3Q== ARC-Message-Signature: i=3; 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=NjFBHTBpx9LV5bvKn5obgAnSYDAg9XDO+WNwjMPeZZE=; b=VxHHnPioyikH2QkBSN+DYd++7MFHh0upmCz5nHFVLmXNTTPRmu7mcS8PCUK6if9LA0 ykRoCiF7vKVZ/2szL5ejLwrLdJSQ9eDhXtBXLJwy/NLcy481Hrga4CP9l6mWXcip8PTC /mBn/sgLoXZIH5ieeUNivLunEISld3Cx+NncOf32NovSVu1fgov4ImVQNVhyEjeZoT4B 0hN5cyXhT47TxsXBbGMsu9WaL8krZucFiInM8HcMYsWmXkQHdvw3VplUplWwPB0sPcXw wU/2vtzXQhWAZlczmCaOJOpkayTmQpReuhp3C188X5nMEpZt9lJqrXYFEzRQCYgrKg2K lvAw== ARC-Authentication-Results: i=3; gmr-mx.google.com; dkim=pass header.i=@softathome1.onmicrosoft.com header.s=selector1-softathome1-onmicrosoft-com header.b=OqORYTXI; arc=pass (i=1 spf=pass spfdomain=softathome.com); spf=pass (google.com: domain of philippe.reynes@softathome.com designates 2a01:111:f400:7e18::624 as permitted sender) smtp.mailfrom=philippe.reynes@softathome.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:x-original-sender:x-original-authentication-results :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=NjFBHTBpx9LV5bvKn5obgAnSYDAg9XDO+WNwjMPeZZE=; b=M1CDgU2fRwXojqKSQVfF0bX5SO8xQf61DY6LUjNK7THUw+FUJjyOuKjxd4j1S3eCxr qkOsHzmSPU5bdjDO6UcmQwMFGA2GuKMcttpq5neTtxJ5JBklXsTv4h7cXXHphpjJsTNV u8WRw2IDflwmEnwNhIW4HuZHX50k9DGkvz8NaUv9YFxcM70egDFfxbbjpPleDNoXMMhI M55Nwl+WqCWR66uZzV9o26riwkSIO2qlJF0Q38EdxAtTpyyTHyblE8uZysshcZ+3wf+s GjodDLQvCuzQSs5Jr1muZkaorEYB4BK5NgHKnSfz+vBo6A32r2R5o8R+Bko2mJDK8kIT xEsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=sender:x-gm-message-state:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=NjFBHTBpx9LV5bvKn5obgAnSYDAg9XDO+WNwjMPeZZE=; b=j+Um4wqruZ1hu/XXd0Mx3ku1LzottEvcEP5EOp1Q3O4YI9vGc7Spt1xRots19oDq05 NLddkKzPmlezhmTH+g1CA1k6DcQQdqGPNXohi9SSWcy3/Yd1KMk3jeR0gQUpH+n5nAtu Dlfsk69HPEryG4s0kfL3v4mn39BYrgbUVDMNVyqU+jT5VcrLriSTX+iY9GSmes9XaBJW 9e00+t1V8eYbF1raNCu9k2qMu6B9IoMHJNYxdT5NQSrgSXnq0NOKd3eYrCCCbzzJ1gZ6 wzCwKPEZMIpPWxhXm4LXtgWJZVzn3DKKpIbEQ5MCycCRZmhLVt2hq/t6cbcNc/hP6PH3 cnMg== Sender: swupdate@googlegroups.com X-Gm-Message-State: AJIora8dqw37/aUkDu5nt4fzpnNjmNia8A4yvXbnKQ+TwU5ms0V1U+D8 Cv3OuZNnRGKk41UiauDEnkw= X-Google-Smtp-Source: AGRyM1t2HtoPiTxHMr6QTRojzXawTfscorDnNeb2OWLD6tgFzgzxQ0//TjP5F+CZpd8LGmkA43SyoA== X-Received: by 2002:a19:f203:0:b0:479:50a4:c925 with SMTP id q3-20020a19f203000000b0047950a4c925mr2560304lfh.329.1655383261917; Thu, 16 Jun 2022 05:41:01 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:a05:6512:1693:b0:448:3742:2320 with SMTP id bu19-20020a056512169300b0044837422320ls552169lfb.1.gmail; Thu, 16 Jun 2022 05:41:00 -0700 (PDT) X-Received: by 2002:a19:384a:0:b0:479:17b1:2e49 with SMTP id d10-20020a19384a000000b0047917b12e49mr2433459lfj.629.1655383260584; Thu, 16 Jun 2022 05:41:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1655383260; cv=pass; d=google.com; s=arc-20160816; b=i3fYZFCD1EZlr6YCQ61VgPm20RzOQbkYJFZWVk6vfVXpULVm6Io1hZlwd2DhzhYdDl y899H16Wm3eGlNslKo+ysCcGuukAQ4oLjU7Kua4aWyp+9O7n2UgAc2+uF5RSM5RO670X 8NXoPYA8s8XmvF1RdSiLmWDcREq95jzdtYvCsdAsV31Pf7wsoeimCtXu4QUbo0yFtqgh UdJuSPHjI6uxrvkY3SyaomdBQo6mG7M8x4k2fglK6wyF3JwJnIhnko0qiWrPGOzt0c56 L+P4zmBbetGvYNXbj9Ls79dHU1ap1gacy1TBGp7N3E2HwEXH9O2P56C7Jh5/9TnZ8hBy URLQ== ARC-Message-Signature: i=2; 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; bh=VfnJTnmvjGSW3AiDP4d7m/3hql8/rIsHU8HLFzmphms=; b=pBak2YJAAcVl45GTnN/FbbFH+Gkrjcfy5NAr3y1esfKjhRn8SGueDDOb/mdOFrhg6F q+LgTSj06Uc9/P883cuzzANOX0dLYgefe0/w5OyNMvNFyS6ZYHa65ZRTZup3QjLd0AO2 7aT0D5EF5DyLY1TJ29XqKNNPbu/jOIIJAtR9KNEpAJHryDEH5b7c09eAdUvygpRjHRJy o0S5iL2OLp95uduUTHRLiX4Xfe+ZFkTVEzUQc6H31rXaaLgXUH+kGevwCQED8RYRj4KT Jqx13/28R7enuqucNNeiMT1VFlS0ss6JmA7IDx26OiM/3jdPJ9/8qzI3Inx9zxgKYNkl bq2A== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@softathome1.onmicrosoft.com header.s=selector1-softathome1-onmicrosoft-com header.b=OqORYTXI; arc=pass (i=1 spf=pass spfdomain=softathome.com); spf=pass (google.com: domain of philippe.reynes@softathome.com designates 2a01:111:f400:7e18::624 as permitted sender) smtp.mailfrom=philippe.reynes@softathome.com Received: from FRA01-PR2-obe.outbound.protection.outlook.com (mail-pr2fra01on0624.outbound.protection.outlook.com. [2a01:111:f400:7e18::624]) by gmr-mx.google.com with ESMTPS id a8-20020a05651c210800b0025594e68748si76112ljq.4.2022.06.16.05.41.00 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 Jun 2022 05:41:00 -0700 (PDT) Received-SPF: pass (google.com: domain of philippe.reynes@softathome.com designates 2a01:111:f400:7e18::624 as permitted sender) client-ip=2a01:111:f400:7e18::624; ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ehqloeEXt8dFKE409KfrZddfsBwr4p7iaHOQUgltN/I78EhU/RvoPL3UuRy2cBD2XOH2Mli5XV/VT00FEce1TmiR2jkpcsc+ODlmHnEnJWS6bH7aeuoSfc8LNvSef/cbdxQaobE5gjvMoAxWdt/S10FrNCP3byGneWCWEGKdRkYuooi/aRIAhX8SDaV/9pxvPUk9DSbU6jLkRDPg2S3bI9YUuqmpa9G2/uqd9+Nh8PqO6ctX0Ch51Kp68gU5Jxyb0qyASV/PSJ6Vyu/Eok6bekaAasGhWsixso4sHae7EK6CSwTg/+70qOs4RyG4NgC84Yz9lxdg/qUAYIgHzC87WQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=VfnJTnmvjGSW3AiDP4d7m/3hql8/rIsHU8HLFzmphms=; b=ZbERqprMQvuGv7RQQoHRYND1BGe9+rjnBYWCIG0UiJ1hK0+jAIR00bokgCOyCJ8Kv5f2d7GhIXZjvwDllhWi6+mxGJhZm5YoyWF9pivbdrAAQ3l7/v9RO+Qg/qs6TLmz8lljmzivQT4P/rUAdK85tCVzdugsnTwDw+4WbWeCeyQ0AUyut2v2jWid0HSvXNzIGiNTCB3px49wBgPNJjEKp4CtBt4JmIj9KFDBZLmdMMZfCI2hmZiLJ1B08ZXM4P5+YKBmHD+kjfACpA4OkXzI8RDdCEMnsBkTd++sHBS26H3G8WTmUPV6Hz3RtbphSadRh4jJocgvmDhVbuyRx+RqTA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 149.6.166.170) smtp.rcpttodomain=googlegroups.com smtp.mailfrom=softathome.com; dmarc=bestguesspass action=none header.from=softathome.com; dkim=none (message not signed); arc=none Received: from MR2P264CA0187.FRAP264.PROD.OUTLOOK.COM (2603:10a6:501::26) by MR1P264MB3907.FRAP264.PROD.OUTLOOK.COM (2603:10a6:501:20::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5353.13; Thu, 16 Jun 2022 12:40:57 +0000 Received: from MR2FRA01FT013.eop-fra01.prod.protection.outlook.com (2603:10a6:501:0:cafe::fc) by MR2P264CA0187.outlook.office365.com (2603:10a6:501::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5353.15 via Frontend Transport; Thu, 16 Jun 2022 12:40:57 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 149.6.166.170) smtp.mailfrom=softathome.com; dkim=none (message not signed) header.d=none;dmarc=bestguesspass action=none header.from=softathome.com; Received-SPF: Pass (protection.outlook.com: domain of softathome.com designates 149.6.166.170 as permitted sender) receiver=protection.outlook.com; client-ip=149.6.166.170; helo=proxy.softathome.com; pr=C Received: from proxy.softathome.com (149.6.166.170) by MR2FRA01FT013.mail.protection.outlook.com (10.152.50.82) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5353.14 via Frontend Transport; Thu, 16 Jun 2022 12:40:57 +0000 Received: from sah1lpt571.softathome.com (unknown [192.168.75.67]) by proxy.softathome.com (Postfix) with ESMTPSA id 618702003C; Thu, 16 Jun 2022 14:40:57 +0200 (CEST) From: Philippe Reynes To: swupdate@googlegroups.com Cc: Philippe Reynes Subject: [swupdate] [PATCH v2 2/2] handlers: diskpart_handler.c: add install_gpt_partition_image Date: Thu, 16 Jun 2022 14:40:55 +0200 Message-Id: <20220616124055.2163867-2-philippe.reynes@softathome.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220616124055.2163867-1-philippe.reynes@softathome.com> References: <20220616124055.2163867-1-philippe.reynes@softathome.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cb7eeca1-73e3-4aa3-c560-08da4f957623 X-MS-TrafficTypeDiagnostic: MR1P264MB3907:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +EY1l8AodqLnsVEDLrSH6NZfX1/upxMhLgDnnWhfdLfyFIyx7uhzIg+hgi5dScSpXQkxtcLmRT8WUaIwSuq7WTDkCplXIn1asf63Ns/VooxRw0nKDFSbdkvMvaVpQ4/NRRB2b3UPefn4cvv0JvYxzBNI+yS3SxjkpbbYfiDPTJM7fKzpwJpNKZYv8gEHvKDlJc8mq2UQcGNWSBT3Vso4HPXhZznE+7GKUtmWLDOhWEnpo1RIGDAgwjOK5DkR0SH8Is8eu3M4GwYL+wD0PB7XW2QAOEtuJ/U+7Yh1jtSUIzOXS12XpWalJMBU9tZCo3uW09bS1EtcMFRE8XZV0EgCeridWrWy8X1nJ94RoK4Eno/0bzH9wduGjBslxiQ5zOgFgzChzeLjdQ1jc8DJznZCDMNIyeOzakGnP1hqAhVaTwUb+ZXKFKCyW//EiqnHgPatjM5T5eS4g/xi3jjhT/dFEW9kkUh3VK/mblFS/K9bg+hmwHokgGWS5DWzdD3JRINf5vYST6cX/Fm8wAPsud+c87O5dihOgRDSCFix1EQeMTcIh5K7wOI9W4yyUt/juH+fNije7xPzxO6UB5ZWy2kieRVq+A1Bs1nX5X7RZt2DlO0fpNu5ACbqXL9yJWVu1YTZhbK8NMAJRdAnVsy8DN7LjXzT7JFj46inrbV2USdOUkoUeQyDvG7dRJ0647cYSuZ3 X-Forefront-Antispam-Report: CIP:149.6.166.170;CTRY:FR;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:proxy.softathome.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230016)(4636009)(36840700001)(46966006)(44832011)(316002)(356005)(2616005)(5660300002)(6966003)(82310400005)(7696005)(498600001)(8936002)(82960400001)(186003)(107886003)(1076003)(81166007)(83380400001)(70586007)(426003)(336012)(47076005)(6266002)(2906002)(26005)(36860700001)(36756003)(86362001)(6916009)(8676002)(4326008)(70206006)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: softathome.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Jun 2022 12:40:57.6342 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cb7eeca1-73e3-4aa3-c560-08da4f957623 X-MS-Exchange-CrossTenant-Id: aa10e044-e405-4c10-8353-36b4d0cce511 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=aa10e044-e405-4c10-8353-36b4d0cce511;Ip=[149.6.166.170];Helo=[proxy.softathome.com] X-MS-Exchange-CrossTenant-AuthSource: MR2FRA01FT013.eop-fra01.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MR1P264MB3907 X-Original-Sender: philippe.reynes@softathome.com X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@softathome1.onmicrosoft.com header.s=selector1-softathome1-onmicrosoft-com header.b=OqORYTXI; arc=pass (i=1 spf=pass spfdomain=softathome.com); spf=pass (google.com: domain of philippe.reynes@softathome.com designates 2a01:111:f400:7e18::624 as permitted sender) smtp.mailfrom=philippe.reynes@softathome.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: , Add a handler to write in a GPT partition. With the handler raw, the device of the parition is provided to the raw handler. This handler use a device on the disk that contains the GPT partition and the name of the GPT partition to found the device of the partition to write. For example: images: ( { filename = "u-boot.bin"; type = "gptpart"; device = "/dev/vdb"; volume = "u-boot-1"; }, { filename = "kernel.bin"; type = "gptpart"; device = "/dev/vdb"; volume = "kernel-1"; }, ); Signed-off-by: Philippe Reynes --- Changelog: v2: - rebase on master - add a constructor to register the handler install_gpt_partition_image doc/source/handlers.rst | 24 ++++++++ handlers/diskpart_handler.c | 115 ++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) diff --git a/doc/source/handlers.rst b/doc/source/handlers.rst index 01ec221..029a70c 100644 --- a/doc/source/handlers.rst +++ b/doc/source/handlers.rst @@ -963,6 +963,30 @@ if the partition table is DOS. It reports an error if the table is GPT. } } +gpt partition installer +....................... + +There is a handler gptpart that allows writing an image into a gpt partition selected by +the name. This handler do not modify the gpt partition (type, size, ...), it just writes +the image in the GPT partition. + +:: + + images: ( + { + filename = "u-boot.bin"; + type = "gptpart"; + device = "/dev/vdb"; + volume = "u-boot-1"; + }, + { + filename = "kernel.bin"; + type = "gptpart"; + device = "/dev/vdb"; + volume = "kernel-1"; + }, + ); + gpt partition swap .................. diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c index 925976b..7408ee6 100644 --- a/handlers/diskpart_handler.c +++ b/handlers/diskpart_handler.c @@ -907,6 +907,114 @@ static int diskpart_reread_partition(char *device) return ret; } +static int install_gpt_partition_image(struct img_type *img, + void __attribute__ ((__unused__)) *data) +{ + struct fdisk_context *cxt = NULL; + struct diskpart_table *tb = NULL; + int ret = 0; + unsigned long hybrid = 0; + struct hnd_priv priv = { + .labeltype = FDISK_DISKLABEL_DOS, + }; + struct create_table *createtable = NULL; + struct fdisk_partition *pa; + size_t partno; + char node[64]; + int fdout; + + LIST_INIT(&priv.listparts); + if (!strlen(img->device)) { + ERROR("Partition handler without setting the device"); + return -EINVAL; + } + + createtable = calloc(1, sizeof(*createtable)); + if (!createtable) { + ERROR("OOM allocating createtable !"); + return -ENOMEM; + } + + /* + * Reads flags: nolock and noinuse + */ + priv.nolock = strtobool(dict_get_value(&img->properties, "nolock")); + priv.noinuse = strtobool(dict_get_value(&img->properties, "noinuse")); + + /* + * Create context + */ + ret = diskpart_assign_context(&cxt, img, priv, hybrid, createtable); + if (ret == -EACCES) + goto handler_release; + else if (ret) + goto handler_exit; + + tb = calloc(1, sizeof(*tb)); + if (!tb) { + ERROR("OOM loading partitions !"); + ret = -ENOMEM; + goto handler_exit; + } + + /* + * Fill the in-memory partition table from the disk. + */ + ret = diskpart_get_partitions(cxt, tb, createtable); + if (ret) + goto handler_exit; + + /* + * Search partition to update + */ + pa = diskpart_get_partition_by_name(tb, img->volname); + if (!pa) { + ERROR("Can't find partition %s", img->volname); + ret = -1; + goto handler_exit; + } + + /* + * Open the node of the partition + */ + partno = fdisk_partition_get_partno(pa); + snprintf(node, sizeof(node), "%s%zu", img->device, partno + 1); + fdout = open(node, O_RDWR); + if (fdout < 0) { + ERROR("Node %s cannot be opened", node); + ret = -1; + goto handler_exit; + } + + /* + * Write image + */ + if (copyimage(&fdout, img, NULL) < 0) { + ERROR("Error copying extracted file"); + ret = -1; + goto handler_close; + } + +handler_close: + close(fdout); + +handler_exit: + if (tb) + diskpart_unref_table(tb); + if (cxt && fdisk_get_devfd(cxt) >= 0) + if (fdisk_deassign_device(cxt, 0)) + WARN("Error deassign device %s", img->device); + +handler_release: + if (cxt) + diskpart_unref_context(cxt); + + if (createtable) + free(createtable); + + return ret; +} + static int diskpart(struct img_type *img, void __attribute__ ((__unused__)) *data) { @@ -1472,3 +1580,10 @@ void diskpart_gpt_swap_partition(void) register_handler("gptswap", gpt_swap_partition, SCRIPT_HANDLER | NO_DATA_HANDLER, NULL); } + +__attribute__((constructor)) +void diskpart_install_gpt_partition_image(void) +{ + register_handler("gptpart", install_gpt_partition_image, + IMAGE_HANDLER, NULL); +}