Skip to contentSkip to conversation

Add support for extlinux.conf

PATCH Superseded Alexander Shiyan 2 participants 6 messages May 29 – Jun 4 Documentation

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 Add support for extlinux.conf

This adds support for the extlinux.conf configuration format, commonly used by Syslinux and many Linux distributions. The configuration file is typically located at /boot/extlinux/extlinux.conf or /extlinux/extlinux.conf and defines boot entries with kernel, initrd, device tree, and command line options.

The implementation integrates with the existing boot entry framework:
- The extlinux scanner discovers entries on mounted filesystems.
- The default LABEL is turned into a boot entry.
- Bootm is used to load and start the kernel.
---
barebox@Diasom DS-RK3568-SOM-EVB:/ global.bootm.appendroot=true
barebox@Diasom DS-RK3568-SOM-EVB:/ global.boot.default=mmc1.2
barebox@Diasom DS-RK3568-SOM-EVB:/ boot
ext4 ext40: EXT2 rev 1, inode_size 256, descriptor size 64
Booting entry 'extlinux: linux'
extlinux: Booting extlinux label 'linux'
Adding "root=/dev/mmcblk1p3" to Kernel commandline
Loading ARM aarch64 Linux/EFI image '/mnt/mmc1.2/boot/extlinux/../vmlinuz'
commandline: root=/dev/mmcblk1p3 console=ttyS2,1500000n8 ro systemd.unit=setup.target quiet splash systemd.machine_id=181af2816b4c6b0aef77068e0ccc69ad Loaded kernel to 0x0a400000, devicetree at 0x000000000fb49000

---
 common/Kconfig    |  19 ++++
 common/Makefile   |   1 +
 common/extlinux.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 263 insertions(+)
 create mode 100644 common/extlinux.c
common/Kconfig +19 -0
index 047dd5390b..6f1eabdf8b 100644
@@ -776,6 +776,25 @@ config BLSPEC
 	  on a device and it allows the Operating System to install / update
 	  kernels.
 
+config EXTLINUX
+	bool
+	prompt "Support extlinux.conf"
+	depends on FLEXIBLE_BOOTARGS
+	depends on !SHELL_NONE
+	select BOOT
+	select BOOTM
+	select MMCBLKDEV_ROOTARG if MCI
+	help
+	  Enable this to let barebox parse extlinux.conf configuration files,
+	  commonly used by the Syslinux bootloader and many Linux distributions
+	  (e.g., on SD cards or USB drives).
+	  extlinux.conf is typically located at /boot/extlinux/extlinux.conf or
+	  /extlinux/extlinux.conf. It defines boot entries with kernel, initrd,
+	  device tree, and command line options.
+	  This option allows barebox to discover and boot operating systems
+	  that follow the extlinux configuration format, providing a simple
+	  and portable way to manage multiple boot options.
+
 config FLEXIBLE_BOOTARGS
 	bool
 	prompt "flexible Linux bootargs generation"
common/Makefile +1 -0
index 21b6cecb3b..6b97edc2c3 100644
@@ -9,6 +9,7 @@ obj-y				+= clock.o
 pbl-$(CONFIG_PBL_CLOCKSOURCE)	+= clock.o
 obj-y				+= console_common.o
 obj-$(CONFIG_OFDEVICE)		+= deep-probe.o
+obj-$(CONFIG_EXTLINUX)		+= extlinux.o
 obj-y				+= startup.o
 obj-y				+= misc.o
 obj-pbl-y			+= memsize.o
common/extlinux.c new file +243 -0

243 lines not on this page · raw message

Signature · 1 line
2.52.0
#1 lore raw

PATCH v3 2/2 Documentation: add extlinux.conf support description

---
 Documentation/user/booting-linux.rst | 42 ++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
Documentation/user/booting-linux.rst +42 -0
index 7bbb79ecdf..d74cc934aa 100644
@@ -304,6 +304,48 @@ Additional notes about keys in the bootloader spec entries:
    different devices without having to specify a different ``root=`` option each
    time.
 
+.. _extlinux_conf:
+
+extlinux.conf Support
+^^^^^^^^^^^^^^^^^^^^^
+
+In addition to the Boot Loader Specification, barebox also supports the
+extlinux configuration format, commonly used by the Syslinux bootloader and
+many Linux distributions. This format is often found on SD cards, USB drives,
+or disk partitions prepared with tools like ``extlinux --install``.
+
+The configuration file is named ``extlinux.conf`` and can be located at:
+* ``/boot/extlinux/extlinux.conf``
+* ``/extlinux/extlinux.conf``
+
+The file uses a simple key-value syntax with sections labeled by ``LABEL``.
+A typical example looks like:
+
+.. code-block:: none
+
+  DEFAULT linux
+  LABEL linux
+    KERNEL /boot/vmlinuz
+    INITRD /boot/initrd.img
+    FDT /boot/board.dtb
+    APPEND console=ttyS0,115200 root=PARTUUID=deadbeef-01
+
+When booting an extlinux entry, barebox strips any ``root=`` parameter from the
+``APPEND`` line **only if** the global variable ``global.bootm.appendroot`` is
+set to true. After stripping, barebox injects the correct ``root=`` based on
+the boot device (similar to the ``linux-appendroot`` feature in blspec). This
+makes the same root filesystem image usable across different storage media.
+
+To use extlinux support, enable ``CONFIG_EXTLINUX`` in your barebox
+configuration. Entries are automatically discovered by the :ref:`command_boot`
+command when scanning a device or mount point. For example:
+
+.. code-block:: sh
+
+  global.bootm.appendroot=true
+  global.boot.default=mmc1.2
+  boot
+
 .. _booting_linux_net:
 
 Network boot
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH v3 2/2] Documentation: add extlinux.conf support description

Hello All.

Quoted 4 lines
Documentation/user/booting-linux.rst +0 -0
index 7bbb79ecdf..d74cc934aa 100644

...

Documentation/user/booting-linux.rst Additional notes about keys in the bootloader spec entries: · line 337

Quoted 5 lines
Documentation/user/booting-linux.rst +5 -0
+When booting an extlinux entry, barebox strips any ``root=`` parameter from the
+``APPEND`` line **only if** the global variable ``global.bootm.appendroot`` is
+set to true. After stripping, barebox injects the correct ``root=`` based on
+the boot device (similar to the ``linux-appendroot`` feature in blspec). This
+makes the same root filesystem image usable across different storage media.

...

This block needs to be removed if the patch is applied. I copied and pasted it by mistake.

#3 lore raw
In reply to Alexander Shiyan

Hi Alexander,

Quoted 194 lines · Alexander Shiyan

This adds support for the extlinux.conf configuration format, commonly used by Syslinux and many Linux distributions. The configuration file is typically located at /boot/extlinux/extlinux.conf or /extlinux/extlinux.conf and defines boot entries with kernel, initrd, device tree, and command line options.

The implementation integrates with the existing boot entry framework:
- The extlinux scanner discovers entries on mounted filesystems.
- The default LABEL is turned into a boot entry.
- Bootm is used to load and start the kernel.
---
barebox@Diasom DS-RK3568-SOM-EVB:/ global.bootm.appendroot=true
barebox@Diasom DS-RK3568-SOM-EVB:/ global.boot.default=mmc1.2
barebox@Diasom DS-RK3568-SOM-EVB:/ boot
ext4 ext40: EXT2 rev 1, inode_size 256, descriptor size 64
Booting entry 'extlinux: linux'
extlinux: Booting extlinux label 'linux'
Adding "root=/dev/mmcblk1p3" to Kernel commandline
Loading ARM aarch64 Linux/EFI image '/mnt/mmc1.2/boot/extlinux/../vmlinuz'
commandline: root=/dev/mmcblk1p3 console=ttyS2,1500000n8 ro systemd.unit=setup.target quiet splash systemd.machine_id=181af2816b4c6b0aef77068e0ccc69ad Loaded kernel to 0x0a400000, devicetree at 0x000000000fb49000

---
 common/Kconfig    |  19 ++++
 common/Makefile   |   1 +
 common/extlinux.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 263 insertions(+)
 create mode 100644 common/extlinux.c
common/Kconfig +19 -0
index 047dd5390b..6f1eabdf8b 100644
@@ -776,6 +776,25 @@ config BLSPEC
 	  on a device and it allows the Operating System to install / update
 	  kernels.
 
+config EXTLINUX
+	bool
+	prompt "Support extlinux.conf"
+	depends on FLEXIBLE_BOOTARGS
+	depends on !SHELL_NONE
+	select BOOT
+	select BOOTM
+	select MMCBLKDEV_ROOTARG if MCI
+	help
+	  Enable this to let barebox parse extlinux.conf configuration files,
+	  commonly used by the Syslinux bootloader and many Linux distributions
+	  (e.g., on SD cards or USB drives).
+	  extlinux.conf is typically located at /boot/extlinux/extlinux.conf or
+	  /extlinux/extlinux.conf. It defines boot entries with kernel, initrd,
+	  device tree, and command line options.
+	  This option allows barebox to discover and boot operating systems
+	  that follow the extlinux configuration format, providing a simple
+	  and portable way to manage multiple boot options.
+
 config FLEXIBLE_BOOTARGS
 	bool
 	prompt "flexible Linux bootargs generation"
common/Makefile +1 -0
index 21b6cecb3b..6b97edc2c3 100644
@@ -9,6 +9,7 @@ obj-y				+= clock.o
 pbl-$(CONFIG_PBL_CLOCKSOURCE)	+= clock.o
 obj-y				+= console_common.o
 obj-$(CONFIG_OFDEVICE)		+= deep-probe.o
+obj-$(CONFIG_EXTLINUX)		+= extlinux.o
 obj-y				+= startup.o
 obj-y				+= misc.o
 obj-pbl-y			+= memsize.o
common/extlinux.c new file +116 -0
new file mode 100644
index 0000000000..6ad01cc652
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* SPDX-FileCopyrightText: Alexander Shiyan <[email protected]> */
+
+#define pr_fmt(fmt)     "extlinux: " fmt
+
+#include <boot.h>
+#include <bootm.h>
+#include <bootscan.h>
+#include <common.h>
+#include <environment.h>
+#include <fs.h>
+#include <globalvar.h>
+#include <libfile.h>
+#include <libgen.h>
+#include <string.h>
+
+struct extlinux_entry {
+	struct bootentry entry;
+	char *rootpath;
+	char *label;
+	char *kernel;
+	char *initrd;
+	char *fdtdir;
+	char *fdt;
+	char *append;
+};
+
+static int extlinux_boot(struct bootentry *entry, int verbose, int dryrun)
+{
+	struct extlinux_entry *e =
+		container_of(entry, struct extlinux_entry, entry);
+	char *kernel_abs, *initrd_abs = NULL, *fdt_abs = NULL;
+	struct bootm_data data = {};
+	int ret;
+
+	bootm_data_init_defaults(&data);
+
+	data.dryrun = max_t(int, dryrun, data.dryrun);
+	data.verbose = max(verbose, data.verbose);
+
+	kernel_abs = basprintf("%s/%s", e->rootpath, e->kernel);
+	data.os_file = kernel_abs;
+
+	if (e->initrd) {
+		initrd_abs = basprintf("%s/%s", e->rootpath, e->initrd);
+		data.initrd_file = initrd_abs;
+	}
+
+	if (e->fdt) {
+		char *fdtdir = e->fdtdir ? : e->rootpath;
+
+		fdt_abs = basprintf("%s/%s", fdtdir, e->fdt);
+		data.oftree_file = fdt_abs;
+	}
+
+	if (e->append)
+		globalvar_add_simple("linux.bootargs.dyn.bootentries",
+				     e->append);
+
+	pr_info("Booting extlinux label '%s'\n", e->label);
+
+	ret = bootm_entry(entry, &data);
+	if (ret)
+		pr_err("bootm failed: %pe\n", ERR_PTR(ret));
+
+	free(kernel_abs);
+	free(initrd_abs);
+	free(fdt_abs);
+
+	return ret;
+}
+
+static void extlinux_entry_free(struct bootentry *entry)
+{
+	struct extlinux_entry *e =
+		container_of(entry, struct extlinux_entry, entry);
+
+	free(e->rootpath);
+	free(e->label);
+	free(e->kernel);
+	free(e->initrd);
+	free(e->fdtdir);
+	free(e->fdt);
+	free(e->append);
+	free(e);
+}
+
+static struct extlinux_entry *parse_extlinux_conf(const char *abspath,
+						  const char *rootpath)
+{
+	char *buf, *bufptr, *line, *default_label = NULL;
+	struct extlinux_entry *entry = NULL;
+
+	bufptr = read_file(abspath, NULL);
+	if (!bufptr)
+		return ERR_PTR(-errno);
+
+	buf = bufptr;
+	while ((line = strsep(&buf, "\n\r")) != NULL) {
+		char *key, *val;
+
+		line = skip_spaces(line);
+
+		if (*line == '#' || *line == '\0')
+			continue;
+
+		key = strsep(&line, " \t");
+		val = isempty(line) ? NULL : skip_spaces(line);
+		if (!key || !val)
+			continue;
+
+		if (!default_label) {
+			if (!strcasecmp(key, "DEFAULT"))
+				default_label = xstrdup(val);
+
+			continue;

common/extlinux.c line 126

Quoted 10 lines
common/extlinux.c new file +10 -0
+		}
+
+		if (!strcasecmp(key, "LABEL")) {
+			if (!strcmp(val, default_label)) {
+				entry = xzalloc(sizeof(*entry));
+				entry->label = xstrdup(val);
+				entry->rootpath = dirname(xstrdup(abspath));
+			} else if (entry) {
+				break;
+			}

The extlinux format supports multiple entries, but you return here after the first entry parsed. We should either document that we only support a single entry or implement multiple entries.

Unless I am overlooking something it should be quite straight forward to implement multiple entries, just move the call to bootentries_add_entry() into this loop.

Sascha

--

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 |
#4 lore raw

Hello Sascha.

This change is possible, but then the question arises of how to specify the default boot entry.
I'd probably prefer to simply specify in the description that we only use the DEFAULT entry.

Thanks!

чт, 4 июн. 2026 г. в 09:25, Sascha Hauer <[email protected]>:

Quoted 224 lines

Hi Alexander,

Quoted 194 lines · Alexander Shiyan

This adds support for the extlinux.conf configuration format, commonly used by Syslinux and many Linux distributions. The configuration file is typically located at /boot/extlinux/extlinux.conf or /extlinux/extlinux.conf and defines boot entries with kernel, initrd, device tree, and command line options.

The implementation integrates with the existing boot entry framework:
- The extlinux scanner discovers entries on mounted filesystems.
- The default LABEL is turned into a boot entry.
- Bootm is used to load and start the kernel.
---
barebox@Diasom DS-RK3568-SOM-EVB:/ global.bootm.appendroot=true
barebox@Diasom DS-RK3568-SOM-EVB:/ global.boot.default=mmc1.2
barebox@Diasom DS-RK3568-SOM-EVB:/ boot
ext4 ext40: EXT2 rev 1, inode_size 256, descriptor size 64
Booting entry 'extlinux: linux'
extlinux: Booting extlinux label 'linux'
Adding "root=/dev/mmcblk1p3" to Kernel commandline
Loading ARM aarch64 Linux/EFI image '/mnt/mmc1.2/boot/extlinux/../vmlinuz'
commandline: root=/dev/mmcblk1p3 console=ttyS2,1500000n8 ro systemd.unit=setup.target quiet splash systemd.machine_id=181af2816b4c6b0aef77068e0ccc69ad Loaded kernel to 0x0a400000, devicetree at 0x000000000fb49000

---
 common/Kconfig    |  19 ++++
 common/Makefile   |   1 +
 common/extlinux.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 263 insertions(+)
 create mode 100644 common/extlinux.c
common/Kconfig +19 -0
index 047dd5390b..6f1eabdf8b 100644
@@ -776,6 +776,25 @@ config BLSPEC
        on a device and it allows the Operating System to install / update
        kernels.
 
+config EXTLINUX
+     bool
+     prompt "Support extlinux.conf"
+     depends on FLEXIBLE_BOOTARGS
+     depends on !SHELL_NONE
+     select BOOT
+     select BOOTM
+     select MMCBLKDEV_ROOTARG if MCI
+     help
+       Enable this to let barebox parse extlinux.conf configuration files,
+       commonly used by the Syslinux bootloader and many Linux distributions
+       (e.g., on SD cards or USB drives).
+       extlinux.conf is typically located at /boot/extlinux/extlinux.conf or
+       /extlinux/extlinux.conf. It defines boot entries with kernel, initrd,
+       device tree, and command line options.
+       This option allows barebox to discover and boot operating systems
+       that follow the extlinux configuration format, providing a simple
+       and portable way to manage multiple boot options.
+
 config FLEXIBLE_BOOTARGS
      bool
      prompt "flexible Linux bootargs generation"
common/Makefile +1 -0
index 21b6cecb3b..6b97edc2c3 100644
@@ -9,6 +9,7 @@ obj-y                         += clock.o
 pbl-$(CONFIG_PBL_CLOCKSOURCE)        += clock.o
 obj-y                                += console_common.o
 obj-$(CONFIG_OFDEVICE)               += deep-probe.o
+obj-$(CONFIG_EXTLINUX)               += extlinux.o
 obj-y                                += startup.o
 obj-y                                += misc.o
 obj-pbl-y                    += memsize.o
common/extlinux.c new file +116 -0
new file mode 100644
index 0000000000..6ad01cc652
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* SPDX-FileCopyrightText: Alexander Shiyan <[email protected]> */
+
+#define pr_fmt(fmt)     "extlinux: " fmt
+
+#include <boot.h>
+#include <bootm.h>
+#include <bootscan.h>
+#include <common.h>
+#include <environment.h>
+#include <fs.h>
+#include <globalvar.h>
+#include <libfile.h>
+#include <libgen.h>
+#include <string.h>
+
+struct extlinux_entry {
+     struct bootentry entry;
+     char *rootpath;
+     char *label;
+     char *kernel;
+     char *initrd;
+     char *fdtdir;
+     char *fdt;
+     char *append;
+};
+
+static int extlinux_boot(struct bootentry *entry, int verbose, int dryrun)
+{
+     struct extlinux_entry *e =
+             container_of(entry, struct extlinux_entry, entry);
+     char *kernel_abs, *initrd_abs = NULL, *fdt_abs = NULL;
+     struct bootm_data data = {};
+     int ret;
+
+     bootm_data_init_defaults(&data);
+
+     data.dryrun = max_t(int, dryrun, data.dryrun);
+     data.verbose = max(verbose, data.verbose);
+
+     kernel_abs = basprintf("%s/%s", e->rootpath, e->kernel);
+     data.os_file = kernel_abs;
+
+     if (e->initrd) {
+             initrd_abs = basprintf("%s/%s", e->rootpath, e->initrd);
+             data.initrd_file = initrd_abs;
+     }
+
+     if (e->fdt) {
+             char *fdtdir = e->fdtdir ? : e->rootpath;
+
+             fdt_abs = basprintf("%s/%s", fdtdir, e->fdt);
+             data.oftree_file = fdt_abs;
+     }
+
+     if (e->append)
+             globalvar_add_simple("linux.bootargs.dyn.bootentries",
+                                  e->append);
+
+     pr_info("Booting extlinux label '%s'\n", e->label);
+
+     ret = bootm_entry(entry, &data);
+     if (ret)
+             pr_err("bootm failed: %pe\n", ERR_PTR(ret));
+
+     free(kernel_abs);
+     free(initrd_abs);
+     free(fdt_abs);
+
+     return ret;
+}
+
+static void extlinux_entry_free(struct bootentry *entry)
+{
+     struct extlinux_entry *e =
+             container_of(entry, struct extlinux_entry, entry);
+
+     free(e->rootpath);
+     free(e->label);
+     free(e->kernel);
+     free(e->initrd);
+     free(e->fdtdir);
+     free(e->fdt);
+     free(e->append);
+     free(e);
+}
+
+static struct extlinux_entry *parse_extlinux_conf(const char *abspath,
+                                               const char *rootpath)
+{
+     char *buf, *bufptr, *line, *default_label = NULL;
+     struct extlinux_entry *entry = NULL;
+
+     bufptr = read_file(abspath, NULL);
+     if (!bufptr)
+             return ERR_PTR(-errno);
+
+     buf = bufptr;
+     while ((line = strsep(&buf, "\n\r")) != NULL) {
+             char *key, *val;
+
+             line = skip_spaces(line);
+
+             if (*line == '#' || *line == '\0')
+                     continue;
+
+             key = strsep(&line, " \t");
+             val = isempty(line) ? NULL : skip_spaces(line);
+             if (!key || !val)
+                     continue;
+
+             if (!default_label) {
+                     if (!strcasecmp(key, "DEFAULT"))
+                             default_label = xstrdup(val);
+
+                     continue;

common/extlinux.c line 126

Quoted 10 lines
common/extlinux.c new file +10 -0
+             }
+
+             if (!strcasecmp(key, "LABEL")) {
+                     if (!strcmp(val, default_label)) {
+                             entry = xzalloc(sizeof(*entry));
+                             entry->label = xstrdup(val);
+                             entry->rootpath = dirname(xstrdup(abspath));
+                     } else if (entry) {
+                             break;
+                     }

The extlinux format supports multiple entries, but you return here after the first entry parsed. We should either document that we only support a single entry or implement multiple entries.

Unless I am overlooking something it should be quite straight forward to implement multiple entries, just move the call to bootentries_add_entry() into this loop.

Sascha

--

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 |
#5 lore raw
Quoted 4 lines · Alexander Shiyan

Hello Sascha.

This change is possible, but then the question arises of how to specify the default boot entry.

That's still a blind spot in barebox. Currently there is no way to specify which entry is booted other than showing up the boot menu.

When I wrote the code I didn't have an idea how to specify an entry in a reproducible way. Simply specifying the nth bootentry would break whenever the numbering changes for whatever reason.

For now barebox will boot the entries in list order which means you would have to make sure that the default entry is added to the front of the list. You could use bootentries_add_entry_sorted() with the compare function preferring the default entry for this.

Anyway, ...

Quoted 2 lines

I'd probably prefer to simply specify in the description that we only use the DEFAULT entry.

That's fine for now as well.

Sascha

Quoted 229 lines

Thanks!

чт, 4 июн. 2026 г. в 09:25, Sascha Hauer <[email protected]>:

Quoted 224 lines

Hi Alexander,

Quoted 194 lines · Alexander Shiyan

This adds support for the extlinux.conf configuration format, commonly used by Syslinux and many Linux distributions. The configuration file is typically located at /boot/extlinux/extlinux.conf or /extlinux/extlinux.conf and defines boot entries with kernel, initrd, device tree, and command line options.

The implementation integrates with the existing boot entry framework:
- The extlinux scanner discovers entries on mounted filesystems.
- The default LABEL is turned into a boot entry.
- Bootm is used to load and start the kernel.
---
barebox@Diasom DS-RK3568-SOM-EVB:/ global.bootm.appendroot=true
barebox@Diasom DS-RK3568-SOM-EVB:/ global.boot.default=mmc1.2
barebox@Diasom DS-RK3568-SOM-EVB:/ boot
ext4 ext40: EXT2 rev 1, inode_size 256, descriptor size 64
Booting entry 'extlinux: linux'
extlinux: Booting extlinux label 'linux'
Adding "root=/dev/mmcblk1p3" to Kernel commandline
Loading ARM aarch64 Linux/EFI image '/mnt/mmc1.2/boot/extlinux/../vmlinuz'
commandline: root=/dev/mmcblk1p3 console=ttyS2,1500000n8 ro systemd.unit=setup.target quiet splash systemd.machine_id=181af2816b4c6b0aef77068e0ccc69ad Loaded kernel to 0x0a400000, devicetree at 0x000000000fb49000

---
 common/Kconfig    |  19 ++++
 common/Makefile   |   1 +
 common/extlinux.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 263 insertions(+)
 create mode 100644 common/extlinux.c
common/Kconfig +19 -0
index 047dd5390b..6f1eabdf8b 100644
@@ -776,6 +776,25 @@ config BLSPEC
        on a device and it allows the Operating System to install / update
        kernels.
 
+config EXTLINUX
+     bool
+     prompt "Support extlinux.conf"
+     depends on FLEXIBLE_BOOTARGS
+     depends on !SHELL_NONE
+     select BOOT
+     select BOOTM
+     select MMCBLKDEV_ROOTARG if MCI
+     help
+       Enable this to let barebox parse extlinux.conf configuration files,
+       commonly used by the Syslinux bootloader and many Linux distributions
+       (e.g., on SD cards or USB drives).
+       extlinux.conf is typically located at /boot/extlinux/extlinux.conf or
+       /extlinux/extlinux.conf. It defines boot entries with kernel, initrd,
+       device tree, and command line options.
+       This option allows barebox to discover and boot operating systems
+       that follow the extlinux configuration format, providing a simple
+       and portable way to manage multiple boot options.
+
 config FLEXIBLE_BOOTARGS
      bool
      prompt "flexible Linux bootargs generation"
common/Makefile +1 -0
index 21b6cecb3b..6b97edc2c3 100644
@@ -9,6 +9,7 @@ obj-y                         += clock.o
 pbl-$(CONFIG_PBL_CLOCKSOURCE)        += clock.o
 obj-y                                += console_common.o
 obj-$(CONFIG_OFDEVICE)               += deep-probe.o
+obj-$(CONFIG_EXTLINUX)               += extlinux.o
 obj-y                                += startup.o
 obj-y                                += misc.o
 obj-pbl-y                    += memsize.o
common/extlinux.c new file +116 -0
new file mode 100644
index 0000000000..6ad01cc652
@@ -0,0 +1,243 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* SPDX-FileCopyrightText: Alexander Shiyan <[email protected]> */
+
+#define pr_fmt(fmt)     "extlinux: " fmt
+
+#include <boot.h>
+#include <bootm.h>
+#include <bootscan.h>
+#include <common.h>
+#include <environment.h>
+#include <fs.h>
+#include <globalvar.h>
+#include <libfile.h>
+#include <libgen.h>
+#include <string.h>
+
+struct extlinux_entry {
+     struct bootentry entry;
+     char *rootpath;
+     char *label;
+     char *kernel;
+     char *initrd;
+     char *fdtdir;
+     char *fdt;
+     char *append;
+};
+
+static int extlinux_boot(struct bootentry *entry, int verbose, int dryrun)
+{
+     struct extlinux_entry *e =
+             container_of(entry, struct extlinux_entry, entry);
+     char *kernel_abs, *initrd_abs = NULL, *fdt_abs = NULL;
+     struct bootm_data data = {};
+     int ret;
+
+     bootm_data_init_defaults(&data);
+
+     data.dryrun = max_t(int, dryrun, data.dryrun);
+     data.verbose = max(verbose, data.verbose);
+
+     kernel_abs = basprintf("%s/%s", e->rootpath, e->kernel);
+     data.os_file = kernel_abs;
+
+     if (e->initrd) {
+             initrd_abs = basprintf("%s/%s", e->rootpath, e->initrd);
+             data.initrd_file = initrd_abs;
+     }
+
+     if (e->fdt) {
+             char *fdtdir = e->fdtdir ? : e->rootpath;
+
+             fdt_abs = basprintf("%s/%s", fdtdir, e->fdt);
+             data.oftree_file = fdt_abs;
+     }
+
+     if (e->append)
+             globalvar_add_simple("linux.bootargs.dyn.bootentries",
+                                  e->append);
+
+     pr_info("Booting extlinux label '%s'\n", e->label);
+
+     ret = bootm_entry(entry, &data);
+     if (ret)
+             pr_err("bootm failed: %pe\n", ERR_PTR(ret));
+
+     free(kernel_abs);
+     free(initrd_abs);
+     free(fdt_abs);
+
+     return ret;
+}
+
+static void extlinux_entry_free(struct bootentry *entry)
+{
+     struct extlinux_entry *e =
+             container_of(entry, struct extlinux_entry, entry);
+
+     free(e->rootpath);
+     free(e->label);
+     free(e->kernel);
+     free(e->initrd);
+     free(e->fdtdir);
+     free(e->fdt);
+     free(e->append);
+     free(e);
+}
+
+static struct extlinux_entry *parse_extlinux_conf(const char *abspath,
+                                               const char *rootpath)
+{
+     char *buf, *bufptr, *line, *default_label = NULL;
+     struct extlinux_entry *entry = NULL;
+
+     bufptr = read_file(abspath, NULL);
+     if (!bufptr)
+             return ERR_PTR(-errno);
+
+     buf = bufptr;
+     while ((line = strsep(&buf, "\n\r")) != NULL) {
+             char *key, *val;
+
+             line = skip_spaces(line);
+
+             if (*line == '#' || *line == '\0')
+                     continue;
+
+             key = strsep(&line, " \t");
+             val = isempty(line) ? NULL : skip_spaces(line);
+             if (!key || !val)
+                     continue;
+
+             if (!default_label) {
+                     if (!strcasecmp(key, "DEFAULT"))
+                             default_label = xstrdup(val);
+
+                     continue;

common/extlinux.c line 126

Quoted 10 lines
common/extlinux.c new file +10 -0
+             }
+
+             if (!strcasecmp(key, "LABEL")) {
+                     if (!strcmp(val, default_label)) {
+                             entry = xzalloc(sizeof(*entry));
+                             entry->label = xstrdup(val);
+                             entry->rootpath = dirname(xstrdup(abspath));
+                     } else if (entry) {
+                             break;
+                     }

The extlinux format supports multiple entries, but you return here after the first entry parsed. We should either document that we only support a single entry or implement multiple entries.

Unless I am overlooking something it should be quite straight forward to implement multiple entries, just move the call to bootentries_add_entry() into this loop.

Sascha

--

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 |
Signature · 4 lines
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 |