From 5b1f6f300408ba47b2d936e3d890e9f8b282fc84 Mon Sep 17 00:00:00 2001 From: Jakub Michalski Date: Mon, 22 Jul 2024 15:33:20 +0200 Subject: [PATCH] samples: add bootargs sample Add bootargs sample Signed-off-by: Jakub Michalski Signed-off-by: Filip Kokosinski --- samples/kernel/bootargs/CMakeLists.txt | 7 + samples/kernel/bootargs/README.rst | 122 ++++++++++++++++++ samples/kernel/bootargs/prj.conf | 1 + samples/kernel/bootargs/prj_efi.conf | 4 + samples/kernel/bootargs/prj_multiboot.conf | 3 + .../kernel/bootargs/prj_static_bootargs.conf | 1 + samples/kernel/bootargs/sample.yaml | 30 +++++ samples/kernel/bootargs/src/main.c | 15 +++ 8 files changed, 183 insertions(+) create mode 100644 samples/kernel/bootargs/CMakeLists.txt create mode 100644 samples/kernel/bootargs/README.rst create mode 100644 samples/kernel/bootargs/prj.conf create mode 100644 samples/kernel/bootargs/prj_efi.conf create mode 100644 samples/kernel/bootargs/prj_multiboot.conf create mode 100644 samples/kernel/bootargs/prj_static_bootargs.conf create mode 100644 samples/kernel/bootargs/sample.yaml create mode 100644 samples/kernel/bootargs/src/main.c diff --git a/samples/kernel/bootargs/CMakeLists.txt b/samples/kernel/bootargs/CMakeLists.txt new file mode 100644 index 00000000000..87c71a249b7 --- /dev/null +++ b/samples/kernel/bootargs/CMakeLists.txt @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(bootargs) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/kernel/bootargs/README.rst b/samples/kernel/bootargs/README.rst new file mode 100644 index 00000000000..3fa45e034d5 --- /dev/null +++ b/samples/kernel/bootargs/README.rst @@ -0,0 +1,122 @@ +.. zephyr:code-sample:: bootargs + :name: Bootargs + + Print received bootargs to the console. + +Overview +******** + +This sample demonstrates use of bootargs passed to Zephyr by printing each main argument to the console. +Zephyr support both dynamic bootargs, received from supported bootloader, and static bootargs embedded in the binary. + +Requirements +************ + +Static bootargs don't have special requirements. +Dynamic bootargs work on platforms where Zephyr is booted by multiboot or efi. + +Building and Running +******************** + +Static bootargs +=============== + +Static bootargs can be configured using ``CONFIG_BOOTARGS_STRING``. + +.. zephyr-app-commands:: + :zephyr-app: samples/kernel/bootargs + :board: qemu_x86 + :conf: prj_static_bootargs.conf + :goals: build run + :compact: + +Output: + +.. code-block:: console + + *** Booting Zephyr OS build v3.7.0-514-gd4490bc739d1 *** + argv[0] = appname + argv[1] = This + argv[2] = is + argv[3] = a list of + argv[4] = arguments + +Multiboot +========= + +.. zephyr-app-commands:: + :zephyr-app: samples/kernel/bootargs + :board: qemu_x86 + :conf: prj_multiboot.conf + :goals: build run + :compact: + +Output: + +.. code-block:: console + + *** Booting Zephyr OS build v3.7.0-rc2-421-g3cf718e6dabc *** + argv[0] = /home/user/zephyr/samples/kernel/bootargs/build/zephyr/zephyr.elf + +To pass your own arguments you can manually invoke qemu with ``-append "your args"``, for example: + +.. code-block:: console + + qemu-system-x86_64 -kernel ./build/zephyr/zephyr.elf -nographic -append "This is 'a list of' arguments" + +Which will result in the following output: + +.. code-block:: console + + *** Booting Zephyr OS build v3.7.0-rc2-421-g3cf718e6dabc *** + argv[0] = ./build/zephyr/zephyr.elf + argv[1] = This + argv[2] = is + argv[3] = a list of + argv[4] = arguments + +Efi +========= + +.. zephyr-app-commands:: + :zephyr-app: samples/kernel/bootargs + :board: qemu_x86_64 + :conf: prj_efi.conf + :goals: build run + :compact: + +Output: + +.. code-block:: console + + *** Zephyr EFI Loader *** + RSDP found at 0xbf7e014 + Zeroing 501792 bytes of memory at 0x163000 + Copying 16384 data bytes to 0x1000 from image offset + Copying 405504 data bytes to 0x100000 from image offset 16384 + Copying 30688 data bytes to 0x1dd820 from image offset 421888 + Jumping to Entry Point: 0x1137 (48 31 c0 48 31 d2 48) + + *** Booting Zephyr OS build v3.7.0-rc2-421-g3cf718e6dabc *** + argv[0] = run.efi + +To pass your own arguments you can press ESC and write your arguments after name of the Zephyr efi binary, for example: + +.. code-block:: console + + Press ESC in 5 seconds to skip startup.nsh or any other key to continue. + Shell> run.efi This is 'a list of' arguments + *** Zephyr EFI Loader *** + RSDP found at 0xbf7e014 + Zeroing 501792 bytes of memory at 0x163000 + Copying 16384 data bytes to 0x1000 from image offset + Copying 405504 data bytes to 0x100000 from image offset 16384 + Copying 30688 data bytes to 0x1dd820 from image offset 421888 + Jumping to Entry Point: 0x1137 (48 31 c0 48 31 d2 48) + + *** Booting Zephyr OS build v3.7.0-rc2-421-g3cf718e6dabc *** + argv[0] = run.efi + argv[1] = This + argv[2] = is + argv[3] = a list of + argv[4] = arguments diff --git a/samples/kernel/bootargs/prj.conf b/samples/kernel/bootargs/prj.conf new file mode 100644 index 00000000000..897bddd6bc7 --- /dev/null +++ b/samples/kernel/bootargs/prj.conf @@ -0,0 +1 @@ +CONFIG_BOOTARGS=y diff --git a/samples/kernel/bootargs/prj_efi.conf b/samples/kernel/bootargs/prj_efi.conf new file mode 100644 index 00000000000..75a72363301 --- /dev/null +++ b/samples/kernel/bootargs/prj_efi.conf @@ -0,0 +1,4 @@ +CONFIG_DYNAMIC_BOOTARGS=y +CONFIG_QEMU_UEFI_BOOT=y +CONFIG_BUILD_OUTPUT_EFI=y +CONFIG_SRAM_SIZE=204800 diff --git a/samples/kernel/bootargs/prj_multiboot.conf b/samples/kernel/bootargs/prj_multiboot.conf new file mode 100644 index 00000000000..b3de58d4cff --- /dev/null +++ b/samples/kernel/bootargs/prj_multiboot.conf @@ -0,0 +1,3 @@ +CONFIG_DYNAMIC_BOOTARGS=y +CONFIG_MULTIBOOT_INFO=y +CONFIG_X86_MEMMAP=y diff --git a/samples/kernel/bootargs/prj_static_bootargs.conf b/samples/kernel/bootargs/prj_static_bootargs.conf new file mode 100644 index 00000000000..871b79f7fd6 --- /dev/null +++ b/samples/kernel/bootargs/prj_static_bootargs.conf @@ -0,0 +1 @@ +CONFIG_BOOTARGS_STRING="appname This is 'a list of' arguments" diff --git a/samples/kernel/bootargs/sample.yaml b/samples/kernel/bootargs/sample.yaml new file mode 100644 index 00000000000..b1d175f75f6 --- /dev/null +++ b/samples/kernel/bootargs/sample.yaml @@ -0,0 +1,30 @@ +sample: + name: Bootargs Sample +tests: + sample.kernel.bootargs.multiboot: + extra_args: EXTRA_CONF_FILE=prj_multiboot.conf + platform_allow: qemu_x86 qemu_x86_64 + harness: console + harness_config: + type: one_line + regex: + - "argv\\[0\\] = .*/zephyr(-qemu-locore)?.elf" + sample.kernel.bootargs.efi: + extra_args: EXTRA_CONF_FILE=prj_efi.conf + platform_allow: qemu_x86_64 + harness: console + harness_config: + type: one_line + regex: + - "argv\\[0\\] = run.efi" + sample.kernel.bootargs.tokenization: + extra_configs: + - CONFIG_BOOTARGS_STRING="appname arg1 'arg2.1 arg2.2' arg3" + harness: console + harness_config: + type: multi_line + regex: + - "argv\\[0\\] = appname" + - "argv\\[1\\] = arg1" + - "argv\\[2\\] = arg2.1 arg2.2" + - "argv\\[3\\] = arg3" diff --git a/samples/kernel/bootargs/src/main.c b/samples/kernel/bootargs/src/main.c new file mode 100644 index 00000000000..89c71551d72 --- /dev/null +++ b/samples/kernel/bootargs/src/main.c @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2024 Antmicro + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +int main(int argc, char **argv) +{ + for (int i = 0; i < argc; i++) { + printf("argv[%d] = %s\n", i, argv[i]); + } + return 0; +}