From patchwork Sun Apr 10 09:35:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Babic X-Patchwork-Id: 1615406 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=IkblJi6m; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=googlegroups.com (client-ip=2a00:1450:4864:20::33f; helo=mail-wm1-x33f.google.com; envelope-from=swupdate+bncbcxploxj6ikrbxwjzkjamgqeynjok2y@googlegroups.com; receiver=) Received: from mail-wm1-x33f.google.com (mail-wm1-x33f.google.com [IPv6:2a00:1450:4864:20::33f]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Kbn0R4TFlz9sFt for ; Sun, 10 Apr 2022 19:35:33 +1000 (AEST) Received: by mail-wm1-x33f.google.com with SMTP id h65-20020a1c2144000000b0038e9ce3b29csf3771202wmh.2 for ; Sun, 10 Apr 2022 02:35:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1649583327; cv=pass; d=google.com; s=arc-20160816; b=wfn/gmumggcgtK81Rrcord6/nBiwGe53sVgJrXhDmVOHwwp/kDYPX94dP1q8R49c++ 6VYrjgFCqgVP10Fx1R8CcGSXZjovB2MzLqG7z2gBYpqiD/y5eUNUtLdoC+EmDORseVZp KJRiI/ZVm12cVsDNLchFXW2wAoGyvWOnQX6sq9t64TXNIWr8BdDTxoQeT6pYLttvNRPU u/DpHTsKVboK5vY/734nHHMXlTDRfIH53YOBDonCA0Uj8LZ0UlA10+SNO/H4nh+6w7cv L/RFZSBiDXgThjjv/nhefQuI9di8aNO8jMpBd5KhzVe9XnALcOs1P802P/Ws3MNXHMMZ Zr0Q== 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=nLFGKjA9Iz+aqib+5Eim3D7Y8XUdOKaOI/smX87CeMQ=; b=UHd/IFwgs/0gbNc1ekykdRe/49+liDHKtF0YuoEz4KG9tdGrZ0+XXLkgOvNKwuqHjo i5ZokYMd/Sjn2Un3yih5/eqaehe/GoTvTtjAXC75zXzyMn3BKrne47TSz6+v+VBZ0Gin ThtU2gvj+v9hkNe6TiJ0S/s7t1BLkBWHGyZTf0OEPSqYuXViJFEpBvopA19+eBO1ZBPf wf5BOf5Tgac9OTYUViEEgG+5/Du1zSOdw+b0Dk6NUpoR/UUcXU0r6xmIxnvOBog9ljgB Z7QCRneAhOFU91lA3KYzcU3gZqYFDJci5k+aI3+o+EPoRZxT4TzUV2qZ7WXbfOgh7Cfe sC+w== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.10 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=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=nLFGKjA9Iz+aqib+5Eim3D7Y8XUdOKaOI/smX87CeMQ=; b=IkblJi6mhsLQEjRnTKO6x9Ce4myoqGr28yiOY5ImXh67G9VeoMcqI/flUlyEEbtiwk 5mEBDUyhMiD5NVKoZ7DO3C3ljEyaBMHi4W+mZ+C/hGLPwzcZXXN0KiM7N9FMsejnecr8 lu45hNFzPzbDd9lmNN2Qr98NY0YOwire0VwTZX3N78gD+KDo4KvDbRSPaNLyoJT8oD1O M6hOA+coYwU/ep+HVd/A0+IJdzFvJVw42knSajbKn1yUsZI95KlGIOl4W/nOqwi5qohn 0+pLw9Vl4VHbJFRdHEFg6Uzfr3Wpj5zZFim2a9LkCbc7bKRFuJLS4Qt5asbPM7u6fk7m beLQ== 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=nLFGKjA9Iz+aqib+5Eim3D7Y8XUdOKaOI/smX87CeMQ=; b=papYV4b/R69cFs032PY8Wsl8Q8HjjOvCWrO+OMSqSdv+HsFQf8K+6KpommGnBZ3TtK YIeZt89S1ONBSa22K8yvv5gNt2awidg9nism2LlcZwUjDRoGYX8pfqn8KI6pbV+frrcT cFjOUOyiuyTlYk/MyWbgNiaetLF7LdCZMeaPVcliij/bXRMxwP1tkcNX/PpKxvu/pcNK DmTygPx+ItmmGUfyjM4luFjfkMdrIlWM7Vv3Pjux187b2t0YIEnDX2cTE7bxgEmxGdVF bAnsqlyTpmk3QGgi98bo8y63tsQEb0u6k7uLL9OtoEtAo6WMKCsAlUPtNFefBwVE+lFu suWg== Sender: swupdate@googlegroups.com X-Gm-Message-State: AOAM532JDnUZsLN8ar9wt992nlIy9d4NrG7RJLuyZXNbHeO+bN/bUwkI /69DTpmB9xMaHrWffUtPhPQ= X-Google-Smtp-Source: ABdhPJyIgI3Zj3+bNioM0isdhuty9OSS1g0Aj35/yNWk1B9LajWg5TyhU1fsBwavuMzlED6D8EMHzw== X-Received: by 2002:adf:e908:0:b0:207:a28e:b904 with SMTP id f8-20020adfe908000000b00207a28eb904mr2932343wrm.542.1649583326940; Sun, 10 Apr 2022 02:35:26 -0700 (PDT) X-BeenThere: swupdate@googlegroups.com Received: by 2002:adf:f5cf:0:b0:205:e1d9:2a6b with SMTP id k15-20020adff5cf000000b00205e1d92a6bls3426100wrp.1.gmail; Sun, 10 Apr 2022 02:35:26 -0700 (PDT) X-Received: by 2002:a5d:5705:0:b0:207:a299:27e2 with SMTP id a5-20020a5d5705000000b00207a29927e2mr2829197wrv.59.1649583325988; Sun, 10 Apr 2022 02:35:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649583325; cv=none; d=google.com; s=arc-20160816; b=MC0GhUiDc5vsm/VLlB2n99A7MxChFr5vRe9WljxDq9Fr+xem+s+VT9zxXO/+UNVY5h w8Sf3Sc9p55utEYhvzyexGOkwL75Lc/QmcQ/hw2HE+YoGlCQ50Pv5CdyUNadZOwRahlV uMdcQaLLyMu2RPEwW8TQ8OPofhvhgUrTd7Fa/nVBcaDAunAjglDff9lcJpSvghcFy85D 2h4A8ekDxmgN0QJuQq3ZAgVJaOI9xBJAeck9I9naZYE1Sd18SdVVOGQR6j4RPfZy9Xy7 5oUUTBD0TjMRTikuTWfp1b2HZE56skmnpwzcpKt67NWNusnd4hFsUDwawMgkF6my4OKl /uCA== 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=NmCZvnB7KCXElEPb86G3RFAkwvf9J1+WsJYaDdsMzV4=; b=FjuXLhWnnQFqAAU3/T0+G4x6+EtcypfsX0UDq9eDEQKxge1ZId+T+r4jZZ5oYKaIls LiUGvcDhI0TOmqpXN6TAaZ2uBhznGg2f0Ro3/iDmiDwR0giop35zY3UC5/OYb1K73OiA 13/TlKfvVtu2UIf/y8zvQPOKfGnuEKnFt2vRqCrEwMdD9l6Z6YIm/oCQ4+VzKn2RvFkX flQq6qzz0ZYRGrWkQfLu/eI87ykb0Q9LnBjJ5JxuRNyQL1tw9BKjIfljq7A7UdnFAiEk pIGHw/lug1DiocqkEf3VcMrXRFSi2+23JpqIVSud67gthiKkm7XhaI49TfVKfrgnw+FM 1Rfg== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=neutral (google.com: 212.18.0.10 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. [212.18.0.10]) by gmr-mx.google.com with ESMTPS id n3-20020a5d4843000000b0020619efb241si236290wrs.4.2022.04.10.02.35.25 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Apr 2022 02:35:25 -0700 (PDT) Received-SPF: neutral (google.com: 212.18.0.10 is neither permitted nor denied by domain of sbabic@denx.de) client-ip=212.18.0.10; Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 4Kbn0F5Qczz1s75T; Sun, 10 Apr 2022 11:35:25 +0200 (CEST) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 4Kbn0F5CkQz1qqkH; Sun, 10 Apr 2022 11:35:25 +0200 (CEST) X-Virus-Scanned: amavisd-new at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id S2tB2p4dA_xf; Sun, 10 Apr 2022 11:35:24 +0200 (CEST) Received: from babic.homelinux.org (host-88-217-136-221.customer.m-online.net [88.217.136.221]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPS; Sun, 10 Apr 2022 11:35:24 +0200 (CEST) Received: from localhost (mail.babic.homelinux.org [127.0.0.1]) by babic.homelinux.org (Postfix) with ESMTP id 8DB874540B9A; Sun, 10 Apr 2022 11:35:24 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at babic.homelinux.org Received: from babic.homelinux.org ([127.0.0.1]) by localhost (mail.babic.homelinux.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Nun8KqHT1_MW; Sun, 10 Apr 2022 11:35:22 +0200 (CEST) Received: from paperino.fritz.box (paperino.fritz.box [192.168.178.48]) by babic.homelinux.org (Postfix) with ESMTP id 7184B4541A98; Sun, 10 Apr 2022 11:35:19 +0200 (CEST) From: Stefano Babic To: swupdate@googlegroups.com Cc: Stefano Babic Subject: [swupdate] [PATCH 3/3] diskpart: add handler to toggle boot flag Date: Sun, 10 Apr 2022 11:35:18 +0200 Message-Id: <20220410093518.308218-3-sbabic@denx.de> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220410093518.308218-1-sbabic@denx.de> References: <20220410093518.308218-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: 212.18.0.10 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: , type = "toggleboot"; properties { partition = ; } Signed-off-by: Stefano Babic --- doc/source/handlers.rst | 18 ++++++ handlers/diskpart_handler.c | 106 ++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) diff --git a/doc/source/handlers.rst b/doc/source/handlers.rst index 95fb02d..8bf1f43 100644 --- a/doc/source/handlers.rst +++ b/doc/source/handlers.rst @@ -946,6 +946,24 @@ MBR Example: } } +Toggleboot Handler +------------------ + +This handler is a script handler. It turns on the bootflag for one of a disk partition +if the partition table is DOS. It reports an error if the table is GPT. + +:: + + script: ( + { + type = "toggleboot"; + device = "/dev/sde"; + properties: { + partition = "1"; + } + } + + Diskformat Handler ------------------ diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c index 3cf7f6d..a6fe369 100644 --- a/handlers/diskpart_handler.c +++ b/handlers/diskpart_handler.c @@ -23,6 +23,7 @@ #include "util.h" void diskpart_handler(void); +void diskpart_toggle_boot(void); /* * This is taken from libfdisk to declare if a field is not set @@ -1111,9 +1112,114 @@ handler_release: return ret; } +static int toggle_boot(struct img_type *img, void *data) +{ + + struct fdisk_context *cxt; + char *path; + int ret; + unsigned long partno; + + struct script_handler_data *script_data; + + if (!data) + return -1; + + script_data = data; + + if (script_data->scriptfn != POSTINSTALL) + return 0; + + /* + * Parse properties + */ + partno = strtoul(dict_get_value(&img->properties, "partition"), NULL, 10); + + /* + * Set is possible only for primary partitions + */ + if (partno > 4 || partno < 1) { + ERROR("Wrong partition number: %ld", partno); + return -EINVAL; + } + + partno--; + + cxt = fdisk_new_context(); + if (!cxt) { + ERROR("Failed to allocate libfdisk context"); + return -ENOMEM; + } + + /* + * Resolve device path symlink. + */ + path = realpath(img->device, NULL); + if (!path) + path = strdup(img->device); + + /* + * fdisk_new_nested_context requires the device to be assigned. + */ + ret = fdisk_assign_device(cxt, path, 0); + free(path); + if (ret < 0) { + ERROR("Device %s cannot be opened: %s", img->device, strerror(-ret)); + return ret; + } + + if (!fdisk_is_label(cxt, DOS)) { + ERROR("Setting boot flag supported for DOS table only"); + ret = -EINVAL; + goto toggle_boot_exit; + } + + int nparts = fdisk_get_npartitions(cxt); + + TRACE("Toggling Boot Flag for partition %ld", partno); + + struct fdisk_partition *pa = NULL; + for (int i = 0; i < nparts; i++) { + + if (fdisk_get_partition(cxt, i, &pa) != 0) + continue; + + if (i != partno) { + if (fdisk_partition_is_bootable(pa)) + fdisk_toggle_partition_flag(cxt, i, DOS_FLAG_ACTIVE); + } else { + if (!fdisk_partition_is_bootable(pa)) { + ret = fdisk_toggle_partition_flag(cxt, i, DOS_FLAG_ACTIVE); + if (ret) + ERROR("Setting boot flag for partition %d on %s FAILED", i, img->device); + } + } + } + fdisk_unref_partition(pa); + + ret = fdisk_write_disklabel(cxt); + +toggle_boot_exit: + if (cxt && fdisk_get_devfd(cxt) >= 0) { + if (fdisk_deassign_device(cxt, 0)) + WARN("Error deassign device %s", img->device); + } + if (cxt) + diskpart_unref_context(cxt); + + return ret; +} + __attribute__((constructor)) void diskpart_handler(void) { register_handler("diskpart", diskpart, PARTITION_HANDLER | NO_DATA_HANDLER, NULL); } + +__attribute__((constructor)) +void diskpart_toggle_boot(void) +{ + register_handler("toggleboot", toggle_boot, + SCRIPT_HANDLER | NO_DATA_HANDLER, NULL); +}