Skip to content

Enable bootscript templating #8315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

djurny
Copy link
Contributor

@djurny djurny commented Jun 22, 2025

  • Enable bootscript templating using envsubst for bootscripts with the filename extension .template.
  • Add generic bootscript. Plan is to have boards use this generic bootscript one by one.

Description

In order to prepare for a generic bootscript, introduce simple templating for bootscripts using envsubst. The templating is needed as some things in the generic bootscript do require customization, where templating offers easy customization using already-existing shell variables. For example, load_addr, serial consoles, memory alignment settings and family/vendor names to enable proper name resolving for DT and DT overlay files.

Documentation summary for feature / change

If bootscript name ends with the .template extension (could not find a more suitable extension), envsubst will be invoked to render the bootscript [template]. Rendering will consist of replacing any occurrance of a variable named $BOOTSCRIPT_TEMPLATE__xxx with the value in the environment of the build system at the time of rendering, in the bootscript template.

Some things to take into consideration, BOOTSCRIPT in the boards/config/xxx files can be overridden by sources/families/xxx or sources/families/includes/xxx. For example, for boards/config/orangepizero.csc, the setting for BOOTSCRIPT will be overridden by sources/families/includes/sunxi-common.inc. This can be solved with the following construct in sources/families/includes/sunxi-common.inc:

declare -g BOOTSCRIPT="${BOOTSCRIPT:-boot-sunxi.cmd}:boot.cmd"

For every board, this has to be inspected to make sure the bootscript selection is done as expected.

How Has This Been Tested?

  • [x ] Orangepi Zero
    • Change BOOTSCRIPT declaration in sources/families/includes/sunxi-common.inc.
    • Add the following to 'boards/config/orangepizero.csc`:
      DISPLAYCON=''
      SERIALCON="ttyS0:115200,ttyGS0"
      
      BOOTSCRIPT='boot-generic.cmd.template:boot.cmd'
      BOOTSCRIPT_TEMPLATE__ALIGN_TO='0x00001000'
      BOOTSCRIPT_TEMPLATE__BOARD_FAMILY="${BOARDFAMILY:-sun8i}"
      BOOTSCRIPT_TEMPLATE__BOARD_VENDOR='allwinner'
      BOOTSCRIPT_TEMPLATE__DISPLAY_CONSOLE='' # determine later on
      BOOTSCRIPT_TEMPLATE__LOAD_ADDR='0x45000000'
      BOOTSCRIPT_TEMPLATE__ROOTFS_TYPE="${ROOTFS_TYPE:-ext4}"
      BOOTSCRIPT_TEMPLATE__SERIAL_CONSOLE='' # determine later on
      
    • Compile for Orangepizero board and inspect generated (overridden and modified) boot/boot.cmd:
      [🐳|🌱] Rendering boot script template [ /armbian/config/bootscripts/boot-generic.cmd.template -> /armbian/.tmp/work-83d4164d-920e-4928-8d37-e7fa9aab818e/deb-bsp-cli-dUPmB/usr/share/armbian/boot.cmd ]
      [🐳|🔨]   '/armbian/config/bootenv/sunxi.txt' -> '/armbian/.tmp/work-83d4164d-920e-4928-8d37-e7fa9aab818e/deb-bsp-cli-dUPmB/usr/share/armbian/armbianEnv.txt'
      ...
      [🐳|🌱] Rendering boot script template [ /armbian/config/bootscripts/boot-generic.cmd.template ]
      [🐳|🔨]   '/armbian/config/bootenv/sunxi.txt' -> '/armbian/.tmp/rootfs-83d4164d-920e-4928-8d37-e7fa9aab818e/boot/armbianEnv.txt'
      ...
      
    • Resulting bootscript:
      # DO NOT EDIT THIS FILE
      #
      # Please edit /boot/armbianEnv.txt to set supported parameters
      #
      
      # This bootscript was generated on Sat, 21 Jun 2025 17:31:29 +0000
      
      # NOTE
      #       This file is the result of rendering a template using 'envsubst'.
      #       Any variable starting with 'BOOTSCRIPT_TEMPLATE__' is expected to be rendered.
      #       The build system will check if any variable is left after rendering and will
      #       exit with error if any unrendered variables are found.
      #       Check the following files for more details:
      #               lib/functions/bsp/armbian-bsp-cli-deb.sh
      #               lib/functions/rootfs/distro-agnostic.sh
      
      # NOTE
      #       If you intend to use 'outside' of 'global' variables from U-Boot, make sure that you do not change them!
      #       The boot logic will attempt a list of 'boot_targets' that all might rely on (environment) variables that
      #       have been set by U-Boot, either compile-time or as part of U-Boot's default 'bootcmd'.
      #       Any variable that this bootscript uses needs to be set explicitly and not conflict with any pre-set var>
      #       Variables that we might change will be saved in preset_x and variables we use will be copied into l_x.
      
      # default environment variables
      setenv align_overlap_oboe_avoidance "on"
      setenv align_to "0x00001000"
      setenv consoleargs_display ""
      setenv consoleargs_serial "console=ttyS0,115200 console=ttyGS0"
      setenv console "both"
      setenv docker_optimizations "on"
      setenv earlycon "off"
      setenv exit_on_critical_errors "on"
      setenv family "sun8i"
      setenv fdt_extrasize "0x00010000"
      setenv kver
      setenv load_addr_calc
      setenv overlay_error "false"
      setenv preset_fdtdir "${fdtdir}"
      setenv preset_fdtfile "${fdtfile}"
      setenv preset_kernel_comp_addr_r "${kernel_comp_addr_r}"
      setenv preset_kernel_comp_size "${kernel_comp_size}"
      setenv rootdev "/dev/mmcblk${devnum}p${distro_bootpart}"
      setenv rootfstype "ext4"
      setenv vendor "allwinner"
      setenv verbosity "1"
      
      # load addresses
      setenv load_addr "0x45000000"
      
      # NOTE
      #       No 'BOOTSCRIPT_TEMPLATE__' references beyond this point.
      

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

@djurny djurny requested review from a team and igorpecovnik as code owners June 22, 2025 00:24
Copy link
Contributor

coderabbitai bot commented Jun 22, 2025

Walkthrough

A new U-Boot boot script template, boot-generic.cmd.template, has been introduced, providing comprehensive and dynamic boot logic for generic ARM boards. The build framework now supports rendering boot script templates by substituting environment variables, with validation to ensure all template variables are resolved. Functions for generating console argument strings and rendering templates have been added. The logic for deploying boot scripts is updated to distinguish between template and non-template scripts, rendering and validating templates as needed. Additionally, the gettext package is added to host dependencies, with no changes to exported function signatures except for new internal helpers.

Possibly related PRs

Suggested labels

ready to merge

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in Comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added 08 Milestone: Third quarter release size/large PR with 250 lines or more Needs review Seeking for review Hardware Hardware related like kernel, U-Boot, ... Framework Framework components labels Jun 22, 2025
@coderabbitai coderabbitai bot added the Ready to merge Reviewed, tested and ready for merge label Jun 22, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
lib/functions/rootfs/distro-agnostic.sh (1)

54-68: Template rendering function: consider shellcheck annotations.
render_bootscript_template correctly exports BOOTSCRIPT_TEMPLATE__CREATE_DATE and console vars, then invokes envsubst. To silence SC2034 (unused var) and SC2046 (word splitting on export $(…)), add above the function:

# shellcheck disable=SC2034,SC2046
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3844ef9 and 5f93a68.

📒 Files selected for processing (4)
  • config/bootscripts/boot-generic.cmd.template (1 hunks)
  • lib/functions/bsp/armbian-bsp-cli-deb.sh (1 hunks)
  • lib/functions/host/prepare-host.sh (1 hunks)
  • lib/functions/rootfs/distro-agnostic.sh (3 hunks)
🧰 Additional context used
🪛 Shellcheck (0.10.0)
lib/functions/rootfs/distro-agnostic.sh

[warning] 58-58: BOOTSCRIPT_TEMPLATE__CREATE_DATE appears unused. Verify use (or export if used externally).

(SC2034)


[warning] 66-66: Quote this to prevent word splitting.

(SC2046)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Shell script analysis
  • GitHub Check: Check kernel security options
🔇 Additional comments (11)
lib/functions/host/prepare-host.sh (1)

189-189: Add gettext to host dependencies: Looks correct.
Including gettext ensures that envsubst is available for template rendering. Verify that the package name matches the target distributions (e.g., Debian’s gettext-base vs. gettext).

lib/functions/bsp/armbian-bsp-cli-deb.sh (2)

145-153: Implement template rendering for .template bootscripts.
Good use of render_bootscript_template and validation via proof_rendered_bootscript_template to fail fast on unrendered placeholders.


155-159: Retain direct copy path for non-template bootscripts.
The fallback to cp -pv preserves existing behavior for plain bootscripts.

lib/functions/rootfs/distro-agnostic.sh (4)

10-30: New console export helpers look solid.
bootscript_export_display_console and bootscript_export_serial_console correctly assemble console arguments and export them under BOOTSCRIPT_TEMPLATE__DISPLAY_CONSOLE and BOOTSCRIPT_TEMPLATE__SERIAL_CONSOLE.


70-72: Proof-rendering verification is concise and accurate.
The grep against ${BOOTSCRIPT_TEMPLATE__ ensures no placeholders remain.


182-191: Resolve bootscript source path with patch override support.
The bootscript_src_path selection between USERPATCHES_PATH and default SRC/config/bootscripts is correct.


226-235: Enable templating in rootfs installation.
Differentiates .template suffix, renders via render_bootscript_template, and enforces completeness with proof_rendered_bootscript_template.

config/bootscripts/boot-generic.cmd.template (4)

1-16: Template header and documentation are clear.
The introductory comments outline templating expectations and references to implementing scripts.


24-29: Default environment variable placeholders.
ALIGN_TO, DISPLAY_CONSOLE, and SERIAL_CONSOLE placeholders are correctly positioned for substitution.


33-46: Board, rootfs, load address, and vendor placeholders.
Use of ${BOOTSCRIPT_TEMPLATE__BOARD_FAMILY}, ${BOOTSCRIPT_TEMPLATE__ROOTFS_TYPE}, ${BOOTSCRIPT_TEMPLATE__LOAD_ADDR}, and ${BOOTSCRIPT_TEMPLATE__BOARD_VENDOR} is appropriate and aligns with export functions.


49-52: Templating termination marker.
The # NOTE block correctly signals that no further BOOTSCRIPT_TEMPLATE__ variables should appear beyond this point.

@djurny
Copy link
Contributor Author

djurny commented Jun 22, 2025

Hi @The-going,
Would be great if you can have a look here as well.
Thx,
Groetjes,

Enable bootscript templating using `envsubst`.
Add generic bootscript.
@djurny djurny force-pushed the feat/bootscript-templating branch from 5f93a68 to 7d42abc Compare June 22, 2025 00:28
@github-actions github-actions bot removed the Ready to merge Reviewed, tested and ready for merge label Jun 22, 2025
@coderabbitai coderabbitai bot added the Ready to merge Reviewed, tested and ready for merge label Jun 22, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
lib/functions/rootfs/distro-agnostic.sh (1)

32-52: Duplicate logic: see previous comment

The bootscript_export_serial_console function mirrors the display-console one. Apply the same refactoring and quoting recommendations above to consolidate and secure the code.

🧹 Nitpick comments (4)
lib/functions/rootfs/distro-agnostic.sh (4)

10-30: Refactor duplicated console-export logic and harden variable handling

The two functions for display- and serial-console exports share nearly identical loops and use unquoted expansions (${DISPLAYCON//,/ }, $SRC_CMDLINE) that can lead to word‐splitting or globbing. Consider extracting a generic helper (e.g. bootscript_export_console VAR_NAME INPUT_LIST FILTER_REGEX) to DRY up both functions. Also quote parameter expansions to prevent unintended splitting.


70-72: Suppress grep output and use exit codes

proof_rendered_bootscript_template should use grep -q to avoid printing matches and make its intent clearer. For example:

-function proof_rendered_bootscript_template() {
-   ! egrep '\$\{?BOOTSCRIPT_TEMPLATE__' "${1:?}"
-}
+function proof_rendered_bootscript_template() {
+   if egrep -q '\$\{?BOOTSCRIPT_TEMPLATE__' "${1:?}"; then
+     return 1
+   fi
+   return 0
+}

182-190: Combine local declarations for clarity

Group related locals into a single declaration to improve readability and reduce boilerplate. Example:

- local bootscript_src_path
- local bootscript_src=${BOOTSCRIPT%%:*}
- local bootscript_dst=${BOOTSCRIPT##*:}
+ local bootscript_src_path bootscript_src bootscript_dst
+ bootscript_src=${BOOTSCRIPT%%:*}
+ bootscript_dst=${BOOTSCRIPT##*:}

227-240: Avoid useless use of cat

You can pipe the template directly into render_bootscript_template without cat:

- run_host_command_logged cat "${bootscript_src_path}/${bootscript_src}" \
-   | render_bootscript_template > "${SDCARD}/boot/${bootscript_dst}"
+ run_host_command_logged render_bootscript_template \
+   < "${bootscript_src_path}/${bootscript_src}" \
+   > "${SDCARD}/boot/${bootscript_dst}"
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5f93a68 and 7d42abc.

📒 Files selected for processing (4)
  • config/bootscripts/boot-generic.cmd.template (1 hunks)
  • lib/functions/bsp/armbian-bsp-cli-deb.sh (1 hunks)
  • lib/functions/host/prepare-host.sh (1 hunks)
  • lib/functions/rootfs/distro-agnostic.sh (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • lib/functions/host/prepare-host.sh
🚧 Files skipped from review as they are similar to previous changes (2)
  • lib/functions/bsp/armbian-bsp-cli-deb.sh
  • config/bootscripts/boot-generic.cmd.template
🧰 Additional context used
🪛 Shellcheck (0.10.0)
lib/functions/rootfs/distro-agnostic.sh

[warning] 58-58: BOOTSCRIPT_TEMPLATE__CREATE_DATE appears unused. Verify use (or export if used externally).

(SC2034)


[warning] 66-66: Quote this to prevent word splitting.

(SC2046)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Shell script analysis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
08 Milestone: Third quarter release Framework Framework components Hardware Hardware related like kernel, U-Boot, ... Needs review Seeking for review Ready to merge Reviewed, tested and ready for merge size/large PR with 250 lines or more
Development

Successfully merging this pull request may close these issues.

1 participant