add createnv command to create environment partition
v2 cover add createnv command to create environment partition
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 series 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.
As the first GPT partition as well as the GPT partition entries might conflict with a barebox written on the device on certain SoCs this series also includes patches to move the first partition up to the 8MiB boundary leaving space for a barebox binary written onto the raw device. On Some SoCs (i.MX7 and earlier) the GPT partition entries also collide with the barebox binary, so these are moved up to just below the 8MiB boundary.
---
Changes in v2:
- Move MCI_STARTUP_NONE to first entry to make it the default (Ahmad)
- make 8MiB offset for first usable LBA configurable (Marco)
- Link to v1: https://lore.barebox.org/[email protected]
---
Sascha Hauer (5):
partitions: efi: calculate instead of hardcode gpt header fields
partitions: Start partitions at 8MiB offset
cdev: fix cdev_open_by_name() misuse
commands: create createnv command
mci: add option to detect non-removable cards during startup
commands/Kconfig | 18 +++++ commands/Makefile | 1 + commands/createnv.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++ commands/devlookup.c | 4 +- commands/findmnt.c | 2 +- commands/parted.c | 2 +- common/partitions.c | 45 ++++++++++++ common/partitions/efi.c | 21 ++++-- drivers/mci/Kconfig | 21 +++++- drivers/mci/mci-core.c | 6 +- fs/devfs-core.c | 17 ++++- fs/fs.c | 6 +- include/driver.h | 5 ++ include/mci.h | 1 + include/partitions.h | 2 + 15 files changed, 311 insertions(+), 19 deletions(-)
---
base-commit: b7a40f473de4de2307eabfa6a7a25be895ea1365
change-id: 20250602-createnv-11c4279116d4
Best regards,
Signature · 1 line
Sascha Hauer <[email protected]>
PATCH v2 1/5 partitions: efi: calculate instead of hardcode gpt header fields
Calculate the lba values of the GPT header instead of hardcoding them. This will allow us to modify some fields while preserving a consistent partition table in the next step.
--- common/partitions/efi.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
common/partitions/efi.c +9 -6
@@ -583,10 +583,13 @@ static __maybe_unused struct partition_desc *efi_partition_create_table(struct b {struct efi_partition_desc *epd =xzalloc (sizeof (*epd));gpt_header *gpt; +unsigned int num_partition_entries =128 ; +unsigned int gpt_size = (sizeof (gpt_entry) * num_partition_entries) /SECTOR_SIZE ; +unsigned int first_usable_lba = gpt_size +2 ;partition_desc_init (&epd->pd, blk); - epd->gpt =xzalloc (512 ); + epd->gpt =xzalloc (SECTOR_SIZE ); gpt = epd->gpt; gpt->signature =cpu_to_le64 (GPT_HEADER_SIGNATURE );
@@ -594,18 +597,18 @@ static __maybe_unused struct partition_desc *efi_partition_create_table(struct b gpt->header_size =cpu_to_le32 (sizeof (*gpt)); gpt->my_lba =cpu_to_le64 (1 ); gpt->alternate_lba =cpu_to_le64 (last_lba (blk)); - gpt->first_usable_lba =cpu_to_le64 (34 ); - gpt->last_usable_lba =cpu_to_le64 (last_lba (blk) -34 );; + gpt->first_usable_lba =cpu_to_le64 (first_usable_lba); + gpt->last_usable_lba =cpu_to_le64 (last_lba (blk) - (gpt_size +2 ));;generate_random_guid ((unsigned char *)&gpt->disk_guid); - gpt->partition_entry_lba =cpu_to_le64 (2 ); - gpt->num_partition_entries =cpu_to_le32 (128 ); + gpt->partition_entry_lba =cpu_to_le64 (first_usable_lba - gpt_size); + gpt->num_partition_entries =cpu_to_le32 (num_partition_entries); gpt->sizeof_partition_entry =cpu_to_le32 (sizeof (gpt_entry));add_gpt_diskuuid_param (epd, blk);pr_info ("Created new disk label with GUID %pU\n" , &gpt->disk_guid); - epd->ptes =xzalloc (128 *sizeof (gpt_entry)); + epd->ptes =xzalloc (num_partition_entries *sizeof (gpt_entry));return &epd->pd; }
Signature · 1 line
2.39.5
PATCH v2 2/5 partitions: Start partitions at 8MiB offset
In reply to Sascha HauerThere are many SoCs that store the bootloader on the raw eMMC/SD device outside of partitions. Examples are all i.MX SoCs and Rockchip SoCs. Until now the first partition starts at offset 1MiB and with this we risk conflicting with the bootloader.
It's cumbersome to sort out all SoCs that have this problem. With nowadays eMMC/SD card sizes it doesn't really matter anymore, so change the 1MiB offset to 8MiB which is still only a small fraction of the card size, but leaves enough space for putting the bootloader into.
Note the change in the GPT partition handling introduced with this change. The layout previously was:
LBA0: MBR
LBA1: GPT header
LBA2-33: GPT partition entries
which now becomes:
LBA0: MBR
LBA1: GPT header
LBA16352-16383: GPT partition entries
This means the GPT partition entries moved from LBA2 to just below the 8MiB boundary and LBA2-16351 are now free for the bootloader. This helps with with older SoCs on which the bootloader overlaps with the normal location of the GPT partition entries, like i.MX7 and earlier.
For nowadays disk sizes a 8MiB offset is negligible. However, for very size constraint environments the first usable LBA is configurable with the globalvar global.partitions.first_usable_lba which defaults to 16384 aka 8MiB.
--- common/partitions.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ common/partitions/efi.c | 8 ++++++-- include/partitions.h | 2 ++ 3 files changed, 53 insertions(+), 2 deletions(-)
common/partitions.c +45 -0
@@ -18,6 +18,8 @@#include <partitions.h> #include <range.h> #include <fuzz.h> +#include <globalvar.h> +#include <magicvar.h> static LIST_HEAD (partition_parser_list);
@@ -202,6 +204,9 @@ int partition_find_free_space(struct partition_desc *pdesc, uint64_t sectors, uistruct partition *p;uint64_t min_sec =PARTITION_ALIGN_SECTORS ; +if (min_sec <partition_first_usable_lba ()) + min_sec =partition_first_usable_lba (); + min_sec =ALIGN (min_sec,PARTITION_ALIGN_SECTORS );if (partition_is_free (pdesc, min_sec, sectors)) {
@@ -239,6 +244,12 @@ int partition_create(struct partition_desc *pdesc, const char *name,return -EINVAL ; } +if (lba_start <partition_first_usable_lba ()) { +pr_err ("partition starts before first usable lba: %llu < %llu\n" , + lba_start,partition_first_usable_lba ()); +return -EINVAL ; + } +list_for_each_entry (part, &pdesc->partitions, list) { if (region_overlap_end(part->first_sec, part->first_sec + part->size - 1,
@@ -411,3 +422,37 @@ loff_t cdev_unallocated_space(struct cdev *cdev)return start; } + +static uint64_t first_usable_dma =SZ_8M /SECTOR_SIZE ; + +uint64_t partition_first_usable_lba (void ) +{ +return first_usable_dma; +} + +static int set_first_usable_lba (struct param_d *p,void *priv) +{ +if (first_usable_dma <1 ) { +pr_err ("Minimum is 1\n" ); +return -EINVAL ; + } + +if (first_usable_dma % (SZ_1M /SECTOR_SIZE )) +pr_warn ("recommended to align to 1MiB\n" ); + +return 0 ; +} + +static int partitions_init (void ) +{ +struct param_d *p; + + p =dev_add_param_uint64 (&global_device,"partitions.first_usable_lba" , + set_first_usable_lba,NULL , + &first_usable_dma,"%llu" ,NULL ); + +return PTR_ERR_OR_ZERO (p); +} +core_initcall (partitions_init); + +BAREBOX_MAGICVAR (global.partitions.first_usable_lba,"first usable LBA used for creating partitions" );
common/partitions/efi.c +6 -2
@@ -585,7 +585,7 @@ static __maybe_unused struct partition_desc *efi_partition_create_table(struct bgpt_header *gpt;unsigned int num_partition_entries =128 ;unsigned int gpt_size = (sizeof (gpt_entry) * num_partition_entries) /SECTOR_SIZE ; -unsigned int first_usable_lba = gpt_size +2 ; +unsigned int first_usable_lba =partition_first_usable_lba ();partition_desc_init (&epd->pd, blk);
@@ -719,7 +719,11 @@ static int efi_protective_mbr(struct block_device *blk)return PTR_ERR (pdesc); } - ret =partition_create (pdesc,"primary" ,"0xee" ,1 ,last_lba (blk)); +/* +* For the protective MBR call directly into the mkpart hook. partition_create() +* does not allow us to create a partition starting at LBA1 +*/ + ret = pdesc->parser->mkpart (pdesc,"primary" ,"0xee" ,1 ,last_lba (blk));if (ret) {pr_err ("Cannot create partition: %pe\n" ,ERR_PTR (ret)); goto out;
include/partitions.h +2 -0
@@ -8,6 +8,7 @@#define __PARTITIONS_PARSER_H__#include <block.h> +#include <disks.h> #include <filetype.h> #include <linux/uuid.h> #include <linux/list.h>
@@ -70,5 +71,6 @@ int partition_remove(struct partition_desc *pdesc, int num);void partition_table_free (struct partition_desc *pdesc);bool partition_is_free (struct partition_desc *pdesc,uint64_t start,uint64_t size);int partition_find_free_space (struct partition_desc *pdesc,uint64_t sectors,uint64_t *start); +uint64_t partition_first_usable_lba (void );#endif /* __PARTITIONS_PARSER_H__ */
Signature · 1 line
2.39.5
PATCH v2 3/5 cdev: fix cdev_open_by_name() misuse
In reply to Sascha Hauercdev_open_by_name() opens a cdev by its name or path. The open by path is implemented with a simple skip the "/dev/" part of the string and use the remaining part as name.
The effect is that several commands in barebox work with "/dev/mmc0" and "mmc0" as argument, but not with "dev/mmc0" or even "../dev/mmc0".
Create a new cdev_open_by_path_name() function which uses canonicalize_path() to resolve the full path and skip the "/dev/" part afterwards.
Convert the places where a path or a name is expected to use the new function. With this we can drop the "/dev/" skipping from cdev_open_by_name() so that this function does what the name suggests.
--- commands/devlookup.c | 4 +--- commands/findmnt.c | 2 +- commands/parted.c | 2 +- fs/devfs-core.c | 17 ++++++++++++++++- fs/fs.c | 6 +++--- include/driver.h | 5 +++++ 6 files changed, 27 insertions(+), 9 deletions(-)
commands/devlookup.c +1 -3
@@ -71,9 +71,7 @@ static int do_devlookup(int argc, char *argv[]) devicefile = aliasbuf; } - devicefile =devpath_to_name (devicefile); - - cdev =cdev_open_by_name (devicefile,O_RDONLY ); + cdev =cdev_open_by_path_name (devicefile,O_RDONLY );if (!cdev) {printf ("devlookup: cdev %s not found\n" , devicefile); ret = -ENOENT ;
commands/findmnt.c +1 -1
@@ -87,7 +87,7 @@ static int do_findmnt(int argc, char *argv[])const char *backingstore;struct cdev *cdev; - cdev =cdev_open_by_name (devpath_to_name (device),O_RDONLY ); + cdev =cdev_open_by_path_name (device,O_RDONLY );if (!cdev)continue ;
commands/parted.c +1 -1
@@ -366,7 +366,7 @@ static int do_parted(int argc, char *argv[])if (argc <3 )return COMMAND_ERROR_USAGE ; - cdev =cdev_open_by_name (argv[1 ],O_RDWR ); + cdev =cdev_open_by_path_name (argv[1 ],O_RDWR );if (!cdev) {printf ("Cannot open %s\n" , argv[1 ]);return COMMAND_ERROR ;
fs/devfs-core.c +16 -1
@@ -271,7 +271,7 @@ struct cdev *cdev_open_by_name(const char *name, unsigned long flags)struct cdev *cdev;int ret; - cdev =cdev_by_name (devpath_to_name (name)); + cdev =cdev_by_name (name);if (!cdev)return NULL ;
@@ -282,6 +282,21 @@ struct cdev *cdev_open_by_name(const char *name, unsigned long flags)return cdev; } +struct cdev *cdev_open_by_path_name (const char *name,unsigned long flags) +{ +char *canon =canonicalize_path (AT_FDCWD , name); +struct cdev *cdev; + +if (!canon) +return cdev_open_by_name (name, flags); + + cdev =cdev_open_by_name (devpath_to_name (canon), flags); + +free (canon); + +return cdev; +} +int cdev_close (struct cdev *cdev) {struct cdev *master =cdev_get_master (cdev);
fs/fs.c +3 -3
@@ -912,7 +912,7 @@ const char *fs_detect(const char *filename, const char *fsoptions) ret =file_name_detect_type_offset (filename, offset, &type, file_detect_fs_type); }else { -struct cdev *cdev =cdev_open_by_name (filename,O_RDONLY ); +struct cdev *cdev =cdev_open_by_path_name (filename,O_RDONLY );if (cdev) { ret =cdev_detect_type (cdev, &type);cdev_close (cdev);
@@ -957,7 +957,7 @@ int fsdev_open_cdev(struct fs_device *fsdev) } } }else { - fsdev->cdev =cdev_open_by_name (fsdev->backingstore,O_RDWR ); + fsdev->cdev =cdev_open_by_path_name (fsdev->backingstore,O_RDWR ); }if (!fsdev->cdev) {path_put (&path);
@@ -3217,7 +3217,7 @@ int umount(const char *pathname)path_put (&path);if (!fsdev) { -struct cdev *cdev =cdev_open_by_name (pathname,O_RDWR ); +struct cdev *cdev =cdev_open_by_path_name (pathname,O_RDWR );if (cdev) {cdev_close (cdev);
include/driver.h +5 -0
@@ -551,6 +551,7 @@ ssize_t cdev_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulossize_t cdev_write (struct cdev *cdev,const void *buf,size_t count,loff_t offset,ulong flags);struct cdev *cdev_by_name (const char *filename);struct cdev *cdev_open_by_name (const char *name,unsigned long flags); +struct cdev *cdev_open_by_path_name (const char *name,unsigned long flags);#else static inline ssize_t cdev_read (struct cdev *cdev,void *buf,size_t count,loff_t offset,ulong flags) {
@@ -568,6 +569,10 @@ static inline struct cdev *cdev_open_by_name(const char *name, unsigned long fla {return NULL ; } +static inline struct cdev *cdev_open_by_path_name (const char *name,unsigned long flags) +{ +return NULL ; +}#endif int cdev_ioctl (struct cdev *cdev,unsigned int cmd,void *buf);int cdev_erase (struct cdev *cdev,loff_t count,loff_t offset);
Signature · 1 line
2.39.5
PATCH v2 4/5 commands: create createnv command
In reply to Sascha HauerWe 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.
--- commands/Kconfig | 18 ++++++ commands/Makefile | 1 + commands/createnv.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+)
commands/Kconfig +18 -0
@@ -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 <size> specify partition size (default 1MiB) + -f force. Do not ask + config CMD_NV depends on NVVAR tristate
commands/Makefile +1 -0
@@ -159,6 +159,7 @@ obj-$(CONFIG_CMD_UBSAN) += ubsan.o obj-$(CONFIG_CMD_SELFTEST) += selftest.o obj-$(CONFIG_CMD_FUZZ) += fuzz.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
commands/createnv.c new file +179 -0
179 lines not on this page · raw message
Signature · 1 line
2.39.5
PATCH v2 5/5 mci: add option to detect non-removable cards during startup
In reply to Sascha HauerWe can optionally detect all MMC/SD cards during startup which might cost some boot time, so this is best disabled. This adds another option to detect only cards that are marked non-removable in the device tree during startup. The rationale is that we will likely boot from that device anyway, so detecting it during startup does not add to the boot time.
We are aiming to find a good place for the barebox environment without having to explicitly add it to the device tree. Putting the environment into a regular partition is a good start, but the device that has the environment needs to be available when we are searching for it. Enabling this new option is one way to it.
--- drivers/mci/Kconfig | 21 +++++++++++++++++++-- drivers/mci/mci-core.c | 6 +++++- include/mci.h | 1 + 3 files changed, 25 insertions(+), 3 deletions(-)
drivers/mci/Kconfig +19 -2
@@ -19,14 +19,31 @@ config MCI_TUNING higher clock speeds than 52 MHz SDR. MMC only; SD-Card max frequency is 50MHz SDR at present. +choice + prompt "MMC detection on startup" + +config MCI_STARTUP_NONE + bool "do not detect cards during system start" + help + Choose this if the MCI framework should never detect cards during system + start + +config MCI_STARTUP_NONREMOVABLE + bool "detect non removable cards on system start" + help + Choose this if the MCI framework should detect non removable cards (i.e. + devices which have the non-removable device tree property). + config MCI_STARTUP - bool "Force probe on system start" + bool "detect all cards on system start" help - Say 'y' here if the MCI framework should always probe for all attached + Choose this if the MCI framework should always probe for all attached MCI cards on system start up. This may required for some legacy boards. When this is 'n', probing happens on demand either with "mci*.probe=1" or with driver/board code calling device_detect. +endchoice + config MCI_INFO bool "MCI Info" depends on CMD_DEVINFO
drivers/mci/mci-core.c +5 -1
@@ -3101,7 +3101,9 @@ int mci_register(struct mci_host *host)devinfo_add (&mci->dev, mci_info);/* if enabled, probe the attached card immediately */ -if (IS_ENABLED (CONFIG_MCI_STARTUP )) +if (IS_ENABLED (CONFIG_MCI_STARTUP ) || + (IS_ENABLED (CONFIG_MCI_STARTUP_NONREMOVABLE ) && + (host->host_caps &MMC_CAP_NONREMOVABLE )))mci_card_probe (mci);if (!(host->caps2 &MMC_CAP2_NO_SD ) &&dev_of_node (host->hw_dev)) {
@@ -3195,6 +3197,8 @@ void mci_of_parse_node(struct mci_host *host, host->caps2 |=MMC_CAP2_NO_SD ;if (of_property_read_bool (np,"no-mmc" )) host->caps2 |=MMC_CAP2_NO_MMC ; +if (of_property_read_bool (np,"non-removable" )) + host->host_caps |=MMC_CAP_NONREMOVABLE ;if (IS_ENABLED (CONFIG_MCI_TUNING )) {u32 drv_type;
include/mci.h +1 -0
@@ -58,6 +58,7 @@#define MMC_CAP_1_2V_DDR (1 << 9)/* Host supports eMMC DDR 1.2V */ #define MMC_CAP_DDR (MMC_CAP_3_3V_DDR | MMC_CAP_1_8V_DDR | \ MMC_CAP_1_2V_DDR) +#define MMC_CAP_NONREMOVABLE (1 << 8)/* Nonremovable e.g. eMMC */ #define MMC_CAP_UHS_SDR12 (1 << 16)/* Host supports UHS SDR12 mode */ #define MMC_CAP_UHS_SDR25 (1 << 17)/* Host supports UHS SDR25 mode */ #define MMC_CAP_UHS_SDR50 (1 << 18)/* Host supports UHS SDR50 mode */
Signature · 1 line
2.39.5
Sascha Hauer
AppliedQuoted 10 lines · Sascha Hauer
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 series 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.
[...]
Applied, thanks!
[1/5] partitions: efi: calculate instead of hardcode gpt header fields
https://git.pengutronix.de/cgit/barebox/commit/?id=ea57365a6a74 (link may not be stable)
[2/5] partitions: Start partitions at 8MiB offset
https://git.pengutronix.de/cgit/barebox/commit/?id=0d2433bfc08c (link may not be stable)
[3/5] cdev: fix cdev_open_by_name() misuse
https://git.pengutronix.de/cgit/barebox/commit/?id=f76032dd5a31 (link may not be stable)
[4/5] commands: create createnv command
https://git.pengutronix.de/cgit/barebox/commit/?id=c8a30874d7c8 (link may not be stable)
[5/5] mci: add option to detect non-removable cards during startup
https://git.pengutronix.de/cgit/barebox/commit/?id=ac16c162ea73 (link may not be stable)
Best regards,
Signature · 1 line
Sascha Hauer <[email protected]>