From mboxrd@z Thu Jan 1 00:00:00 1970 Delivery-date: Mon, 02 Jun 2025 15:51:40 +0200 Received: from metis.whiteo.stw.pengutronix.de ([2a0a:edc0:2:b01:1d::104]) by lore.white.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uM5Zk-002ypF-0V for lore@lore.pengutronix.de; Mon, 02 Jun 2025 15:51:40 +0200 Received: from bombadil.infradead.org ([2607:7c80:54:3::133]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1uM5Zj-0007Nx-3c for lore@pengutronix.de; Mon, 02 Jun 2025 15:51:40 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From :Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=9de+ex5ktJmuuoOanbKg7SdB+qLkqagjrB0MWHQbipU=; b=pzvqrIoUZIlYcpPV20icNCPOjT J/38ML8cgmf72zlLqzD8cLsYkKgaDM97tcbWAXS0dlGMRL81nWUXChW9QV/soqSLC90nY6EFXRKKO AhJmSl93ThW4/7ckPpW1ju6lOm3O4mSV/qigxj6cqKm4nXLw5TO2OkSE5iBWBBkQw6+Jhz3xuFpjG Vp37HWmPC6Bl2Zd4GcUnZ5qXezzbwfPevaZuftgnLyu6IBHONkSZilB69dkw6HK3paD2ouWgCxGv/ b3hHXD+R/9PlD8y9YiChwwiUOnYY5hon3KEY7ro0TIsVvpDREIl+Uf7VjLgHW3aRPhX29XlpSvOFF j/pKiyOQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1uM5ZF-00000007W6y-0rWG; Mon, 02 Jun 2025 13:51:09 +0000 Received: from metis.whiteo.stw.pengutronix.de ([2a0a:edc0:2:b01:1d::104]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1uM5Wd-00000007VpJ-3UDv for barebox@lists.infradead.org; Mon, 02 Jun 2025 13:48:29 +0000 Received: from ptz.office.stw.pengutronix.de ([2a0a:edc0:0:900:1d::77] helo=[127.0.0.1]) by metis.whiteo.stw.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1uM5Wc-0006DG-IE; Mon, 02 Jun 2025 15:48:26 +0200 Message-ID: <264954b4-d6d3-4ea1-a962-b2bc8a298bb9@pengutronix.de> Date: Mon, 2 Jun 2025 15:48:26 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: Sascha Hauer , BAREBOX References: <20250602-createnv-v1-0-c3239ff875d5@pengutronix.de> <20250602-createnv-v1-4-c3239ff875d5@pengutronix.de> Content-Language: en-US From: Ahmad Fatoum In-Reply-To: <20250602-createnv-v1-4-c3239ff875d5@pengutronix.de> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250602_064828_030505_56B18C60 X-CRM114-Status: GOOD ( 33.03 ) X-BeenThere: barebox@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "barebox" X-SA-Exim-Connect-IP: 2607:7c80:54:3::133 X-SA-Exim-Mail-From: barebox-bounces+lore=pengutronix.de@lists.infradead.org X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on metis.whiteo.stw.pengutronix.de X-Spam-Level: X-Spam-Status: No, score=-5.3 required=4.0 tests=AWL,BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.2 Subject: Re: [PATCH 4/5] commands: create createnv command X-SA-Exim-Version: 4.2.1 (built Wed, 08 May 2019 21:11:16 +0000) X-SA-Exim-Scanned: Yes (on metis.whiteo.stw.pengutronix.de) Hi, On 02.06.25 15:28, Sascha Hauer wrote: > We want to move away from describing the barebox environment explicitly > in the device tree and instead motivate usage of GPT partitions for the > envrionment. This patch creates a createnv command to facilitate this. > It creates an environment partition on the specified device and if > necessary also a GPT partition table. In the simplest case a "createnv" > without arguments will create a partition on the device barebox itself > booted from. Both the device and the size of the partition can be > specified on the command line. > > We can't create environment partitions on a device containing a MBR > partition table, so a MBR partitioned device won't be touched. > > As an additional safety net the command will ask the user if altering > the partition table is really desired. This behaviour can be overwritten > by specifying -f on the command line. Could we have saveenv/loadenv nudge the user towards createnv if CMD_CREATENV is enabled? Something along the lines of: saveenv: no environment was registered saveenv: run createnv to create an ad-hoc environment Cheers, Ahmad > > Signed-off-by: Sascha Hauer > --- > commands/Kconfig | 18 ++++++ > commands/Makefile | 1 + > commands/createnv.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 198 insertions(+) > > diff --git a/commands/Kconfig b/commands/Kconfig > index 0f3155d123ab6eebd6a8b4585ff8c496318c9efe..27a935d4d2e55f0384a28ab1d8718c0cf90fb746 100644 > --- a/commands/Kconfig > +++ b/commands/Kconfig > @@ -771,6 +771,24 @@ endmenu > > menu "Environment" > > +config CMD_CREATENV > + tristate > + depends on PARTITION_DISK_EFI > + select PARTITION_MANIPULATION > + prompt "createnv" > + help > + createnv - Create a barebox environment partition > + > + Usage: createnv [-sf] [device] > + > + Create a barebox environment partition. > + This creates a barebox environment partition and eventually > + a GPT partition table when it does not exist. > + > + Options: > + -s specify partition size (default 1MiB) > + -f force. Do not ask > + > config CMD_NV > depends on NVVAR > tristate > diff --git a/commands/Makefile b/commands/Makefile > index 8c2749b5ebddb5ef22691d2007f556af195b432b..2780a26dc2daf86ebdc11011fed0849bafaa982a 100644 > --- a/commands/Makefile > +++ b/commands/Makefile > @@ -157,6 +157,7 @@ obj-$(CONFIG_CMD_BTHREAD) += bthread.o > obj-$(CONFIG_CMD_UBSAN) += ubsan.o > obj-$(CONFIG_CMD_SELFTEST) += selftest.o > obj-$(CONFIG_CMD_TUTORIAL) += tutorial.o > +obj-$(CONFIG_CMD_CREATENV) += createnv.o > obj-$(CONFIG_CMD_STACKSMASH) += stacksmash.o > obj-$(CONFIG_CMD_PARTED) += parted.o > obj-$(CONFIG_CMD_EFI_HANDLE_DUMP) += efi_handle_dump.o > diff --git a/commands/createnv.c b/commands/createnv.c > new file mode 100644 > index 0000000000000000000000000000000000000000..df03664be11251165287bd2291d09bee7408f553 > --- /dev/null > +++ b/commands/createnv.c > @@ -0,0 +1,179 @@ > +// SPDX-License-Identifier: GPL-2.0-only > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +static int do_createnv(int argc, char *argv[]) > +{ > + uint64_t start, size = SZ_1M; > + struct cdev *cdev; > + struct block_device *blk; > + struct partition *part; > + struct partition_desc *pdesc = NULL; > + enum filetype filetype; > + guid_t partition_barebox_env_guid = PARTITION_BAREBOX_ENVIRONMENT_GUID; > + void *buf = NULL; > + bool force = false; > + int opt, ret; > + > + while ((opt = getopt(argc, argv, "s:f")) > 0) { > + switch (opt) { > + case 's': > + size = strtoull_suffix(optarg, NULL, 0); > + break; > + case 'f': > + force = true; > + break; > + default: > + return COMMAND_ERROR_USAGE; > + } > + } > + > + argc -= optind; > + argv += optind; > + > + if (argc < 1) { > + cdev = bootsource_of_cdev_find(); > + if (!cdev) { > + printf("Cannot find bootsource cdev\n"); > + return COMMAND_ERROR; > + } > + ret = cdev_open(cdev, O_RDWR); > + if (ret) { > + printf("Cannot open %s: %pe\n", cdev->name, ERR_PTR(ret)); > + return COMMAND_ERROR; > + } > + } else { > + cdev = cdev_open_by_path_name(argv[0], O_RDWR); > + if (!cdev) { > + printf("Cannot open %s\n", argv[0]); > + return COMMAND_ERROR; > + } > + } > + > + blk = cdev_get_block_device(cdev); > + if (!blk) { > + ret = -ENOENT; > + goto err; > + } > + > + buf = xzalloc(2 * SECTOR_SIZE); > + > + ret = cdev_read(cdev, buf, 2 * SECTOR_SIZE, 0, 0); > + if (ret < 0) { > + printf("Cannot read from %s: %pe\n", cdev->name, ERR_PTR(ret)); > + goto err; > + } > + > + filetype = file_detect_partition_table(buf, 2 * SECTOR_SIZE); > + > + switch (filetype) { > + case filetype_gpt: > + pdesc = partition_table_read(blk); > + if (!pdesc) { > + printf("Cannot read partition table\n"); > + ret = -EINVAL; > + goto err; > + } > + break; > + case filetype_mbr: > + printf("%s contains a DOS partition table. Cannot create a barebox environment here\n", > + cdev->name); > + ret = -EINVAL; > + goto err; > + default: > + printf("Will create a GPT on %s\n", cdev->name); > + pdesc = partition_table_new(blk, "gpt"); > + break; > + } > + > + list_for_each_entry(part, &pdesc->partitions, list) { > + if (guid_equal(&part->typeuuid, &partition_barebox_env_guid)) { > + printf("%s already contains a barebox environment partition\n", > + cdev->name); > + ret = -EINVAL; > + goto err; > + } > + } > + > + size >>= SECTOR_SHIFT; > + > + ret = partition_find_free_space(pdesc, size, &start); > + if (ret) { > + printf("No free space available\n"); > + goto err; > + } > + > + ret = partition_create(pdesc, "barebox-environment", "bbenv", start, start + size - 1); > + if (ret) { > + printf("Creating partition failed: %pe\n", ERR_PTR(ret)); > + goto err; > + } > + > + printf("Will create a barebox environment partition of size %llu bytes on %s\n", > + size << SECTOR_SHIFT, cdev->name); > + > + if (!force) { > + char c; > + > + printf("Do you wish to continue? (y/n)\n"); > + > + c = getchar(); > + if (c != 'y') { > + ret = -EINTR; > + goto err; > + } > + } > + > + ret = partition_table_write(pdesc); > + if (ret) { > + printf("Cannot write partition table: %pe\n", ERR_PTR(ret)); > + goto err; > + } > + > + if (!default_environment_path_get()) { > + char *path = xasprintf("/dev/%s.barebox-environment", cdev->name); > + default_environment_path_set(path); > + free(path); > + } > + > + printf("Created barebox environment partition on %s\n", cdev->name); > + > + ret = 0; > +err: > + if (pdesc) > + partition_table_free(pdesc); > + > + free(buf); > + cdev_close(cdev); > + > + return ret ? COMMAND_ERROR : 0; > +} > + > +BAREBOX_CMD_HELP_START(createnv) > +BAREBOX_CMD_HELP_TEXT("Create a barebox environment partition.") > +BAREBOX_CMD_HELP_TEXT("This creates a barebox environment partition and eventually") > +BAREBOX_CMD_HELP_TEXT("a GPT partition table when it does not exist.") > +BAREBOX_CMD_HELP_OPT("-s ", "specify partition size (default 1MiB)") > +BAREBOX_CMD_HELP_OPT("-f\t", "force. Do not ask") > +BAREBOX_CMD_HELP_END > + > +BAREBOX_CMD_START(createnv) > + .cmd = do_createnv, > + BAREBOX_CMD_DESC("Create a barebox environment partition") > + BAREBOX_CMD_OPTS("[-sf] [device]") > + BAREBOX_CMD_GROUP(CMD_GRP_CONSOLE) > + BAREBOX_CMD_HELP(cmd_createnv_help) > +BAREBOX_CMD_END > -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |