From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C63C8C83F10 for ; Thu, 31 Aug 2023 16:52:44 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0DB7986802; Thu, 31 Aug 2023 18:52:13 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="VE7suPQk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0EAFF86852; Thu, 31 Aug 2023 18:52:10 +0200 (CEST) Received: from mail-ot1-x336.google.com (mail-ot1-x336.google.com [IPv6:2607:f8b0:4864:20::336]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 30D8C86802 for ; Thu, 31 Aug 2023 18:52:07 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jpewhacker@gmail.com Received: by mail-ot1-x336.google.com with SMTP id 46e09a7af769-6bcb15aa074so541800a34.0 for ; Thu, 31 Aug 2023 09:52:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693500725; x=1694105525; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FMbr+W+whz36YVe9LPqM/9Dbsw5gNKIx8MyLb6BFa0I=; b=VE7suPQkos60MHv7MMJHhqU8vJT6t0Xk+B3XlD34S5+9TVG2P+V6f3K8g5CUboI99q yguvvBWuMdrANeCFZt0iojNZjIwYEwmuTxzk/wtIJiUo+gRtgd2p8RKJ4Jj+aod1J1OH R0BdSvY+mAbwrxhIplR2oJwUIxksOGYHOvFXzXMGXPMr1bVI/2pLM8kY4Nfb3xmXG6cq HAyTUIYkxHEtOAVFm1D5YUtG/1oSHyfIt0Nt2bQcjretM8aE/N/OSsDvThTVxxBFWztt e3UqEuFcn7V0mM8o8mETmrE4IwZUcwnfVlaoNeDO72ptvoUEG6UDJhJyX7pNCNCtKVwP Ubow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693500725; x=1694105525; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FMbr+W+whz36YVe9LPqM/9Dbsw5gNKIx8MyLb6BFa0I=; b=gQM8uTQYpvKv5QTbhNU3LzpsZ9OSkoc13jBOwjfjxt2CRYXgFrBVNAmoLnH9SAMVfd Z/sPxCzvdPYQax4UCnmzT1eI1KvInGJ4ApovW/GzX1g7qE+fd+y1KNTEQhIySaUgdpB+ untQEJbPkV2AtPy89Dwc3PIux2WDU3f0+/slOiwXi3sgAtS23awvwWQl8HKZasBL2suP 4LFc56kSNxJav5WrmN64BEWyUZ4ACPj+wxqPL2HReu15bvT/dgGXeSXK7Kj5dLiC/UcR Crw7P3fPcCXLYPZ+0AC8aVJoSn0yzeQ4r5wgicorttUPlqTEwc/Q7MtR6luB8l/nj171 HHJw== X-Gm-Message-State: AOJu0YwUNrR7hzvErY0U3zPenWm+iLlYCJPgmY7UZWo3fmI1dJlJdMKR Br6N9jEQKOj9AEtnqNWUwvFyNR0qSLM= X-Google-Smtp-Source: AGHT+IEiMa+yg6Qf2ukbqA5+2648t+AG2M2puhyHeB7pRi+xR64TzdpCUIzZjijI6rRMVMOlP2IT8w== X-Received: by 2002:a05:6830:134c:b0:6b9:cfc3:65d1 with SMTP id r12-20020a056830134c00b006b9cfc365d1mr176550otq.17.1693500725221; Thu, 31 Aug 2023 09:52:05 -0700 (PDT) Received: from localhost.localdomain ([2601:282:4300:19e0::b66b]) by smtp.gmail.com with ESMTPSA id b15-20020a9d6b8f000000b006bb334880d8sm867020otq.81.2023.08.31.09.52.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 09:52:04 -0700 (PDT) From: Joshua Watt X-Google-Original-From: Joshua Watt To: u-boot@lists.denx.de Cc: Joshua Watt , Simon Glass , Heinrich Schuchardt , Enric Balletbo i Serra Subject: [PATCH v5 5/8] cmd: gpt: Add command to set bootable flags Date: Thu, 31 Aug 2023 10:51:38 -0600 Message-Id: <20230831165144.592820-6-JPEWhacker@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230831165144.592820-1-JPEWhacker@gmail.com> References: <20230828215623.3727536-1-JPEWhacker@gmail.com> <20230831165144.592820-1-JPEWhacker@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Adds a command that can be used to modify the GPT partition table to indicate which partitions should have the bootable flag set Signed-off-by: Joshua Watt --- cmd/gpt.c | 80 +++++++++++++++++++++++++++++++++++++++ doc/usage/cmd/gpt.rst | 12 ++++++ test/py/tests/test_gpt.py | 22 +++++++++++ 3 files changed, 114 insertions(+) diff --git a/cmd/gpt.c b/cmd/gpt.c index c6c8282ac9..45fbe07404 100644 --- a/cmd/gpt.c +++ b/cmd/gpt.c @@ -972,6 +972,81 @@ static int do_rename_gpt_parts(struct blk_desc *dev_desc, char *subcomm, free(partitions_list); return ret; } + +/** + * gpt_set_bootable() - Set bootable flags for partitions + * + * Sets the bootable flag for any partition names in the comma separated list of + * partition names. Any partitions not in the list have their bootable flag + * cleared + * + * @desc: block device descriptor + * @name: Comma separated list of partition names + * + * @Return: '0' on success and -ve error on failure + */ +static int gpt_set_bootable(struct blk_desc *blk_dev_desc, char *const part_list) +{ + char *name; + char disk_guid[UUID_STR_LEN + 1]; + struct list_head *pos; + struct disk_part *curr; + struct disk_partition *partitions = NULL; + int part_count = 0; + int ret = get_disk_guid(blk_dev_desc, disk_guid); + + if (ret < 0) + return ret; + + ret = get_gpt_info(blk_dev_desc); + if (ret <= 0) + goto out; + + part_count = ret; + partitions = malloc(sizeof(*partitions) * part_count); + if (!partitions) { + ret = -ENOMEM; + goto out; + } + + /* Copy partitions and clear bootable flag */ + part_count = 0; + list_for_each(pos, &disk_partitions) { + curr = list_entry(pos, struct disk_part, list); + partitions[part_count] = curr->gpt_part_info; + partitions[part_count].bootable &= ~PART_BOOTABLE; + part_count++; + } + + name = strtok(part_list, ","); + while (name) { + bool found = false; + + for (int i = 0; i < part_count; i++) { + if (strcmp((char *)partitions[i].name, name) == 0) { + partitions[i].bootable |= PART_BOOTABLE; + found = true; + } + } + + if (!found) { + printf("Warning: No partition matching '%s' found\n", + name); + } + + name = strtok(NULL, ","); + } + + ret = gpt_restore(blk_dev_desc, disk_guid, partitions, part_count); + +out: + del_gpt_info(); + + if (partitions) + free(partitions); + + return ret; +} #endif /** @@ -1031,6 +1106,8 @@ static int do_gpt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) } else if ((strcmp(argv[1], "swap") == 0) || (strcmp(argv[1], "rename") == 0)) { ret = do_rename_gpt_parts(blk_dev_desc, argv[1], argv[4], argv[5]); + } else if ((strcmp(argv[1], "set-bootable") == 0)) { + ret = gpt_set_bootable(blk_dev_desc, argv[4]); #endif } else { return CMD_RET_USAGE; @@ -1082,8 +1159,11 @@ U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt, " and vice-versa\n" " gpt rename \n" " - rename the specified partition\n" + " gpt set-bootable \n" + " - make partition names in list bootable\n" " Example usage:\n" " gpt swap mmc 0 foo bar\n" " gpt rename mmc 0 3 foo\n" + " gpt set-bootable mmc 0 boot_a,boot_b\n" #endif ); diff --git a/doc/usage/cmd/gpt.rst b/doc/usage/cmd/gpt.rst index b505b159d0..288dd365c0 100644 --- a/doc/usage/cmd/gpt.rst +++ b/doc/usage/cmd/gpt.rst @@ -13,6 +13,7 @@ Synopsis gpt read [] gpt rename gpt repair + gpt set-bootable gpt setenv gpt swap gpt verify [] @@ -90,6 +91,13 @@ gpt repair Repairs the GPT partition tables if it they become corrupted. +gpt set-bootable +~~~~~~~~~~~~~~~~ + +Sets the bootable flag for all partitions in the table. If the partition name +is in 'partition list' (separated by ','), the bootable flag is set, otherwise +it is cleared. CONFIG_CMD_GPT_RENAME=y is required. + gpt setenv ~~~~~~~~~~ @@ -187,3 +195,7 @@ Get the GUID for a disk:: => gpt guid mmc gpt_disk_uuid => echo ${gpt_disk_uuid} bec9fc2a-86c1-483d-8a0e-0109732277d7 + +Set the bootable flag for the 'boot' partition and clear it for all others:: + + => gpt set-bootable mmc 0 boot diff --git a/test/py/tests/test_gpt.py b/test/py/tests/test_gpt.py index 946858800d..5d23f9b292 100644 --- a/test/py/tests/test_gpt.py +++ b/test/py/tests/test_gpt.py @@ -222,6 +222,28 @@ def test_gpt_swap_partitions(state_disk_image, u_boot_console): assert '0x00000800 0x00000fff "part2"' in output assert '0x00001000 0x00001bff "part1"' in output +@pytest.mark.buildconfigspec('cmd_gpt') +@pytest.mark.buildconfigspec('cmd_gpt_rename') +@pytest.mark.buildconfigspec('cmd_part') +@pytest.mark.requiredtool('sgdisk') +def test_gpt_set_bootable(state_disk_image, u_boot_console): + """Test the gpt set-bootable command.""" + + u_boot_console.run_command('host bind 0 ' + state_disk_image.path) + parts = ('part2', 'part1') + for bootable in parts: + output = u_boot_console.run_command(f'gpt set-bootable host 0 {bootable}') + assert 'success!' in output + + for p in parts: + output = u_boot_console.run_command(f'gpt setenv host 0 {p}') + assert 'success!' in output + output = u_boot_console.run_command('echo ${gpt_partition_bootable}') + if p == bootable: + assert output.rstrip() == '1' + else: + assert output.rstrip() == '0' + @pytest.mark.boardspec('sandbox') @pytest.mark.buildconfigspec('cmd_gpt') @pytest.mark.buildconfigspec('cmd_part') -- 2.34.1