Skip to content

sunxi: bootscript load address calculation a.o. #8287

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

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

Conversation

djurny
Copy link
Contributor

@djurny djurny commented Jun 9, 2025

Description

Attempt to work towards one U-Boot bootscript for (at least) mvebu, sunxi and rockchip64.
This adds:

  • (Aligned) load address calculations
    This will remove the need to update any kernel_load_addr_r or ramdisk_addr_r in case kernel image increases.
    Calculation is based on either
    • Flat kernel image image_size + text_offset as specified in the vmlinux(/Image) header info
    • Compressed kernel image filesize (vmlinuz/zImage)
  • Clear warnings to user in case files are not found, not able to load or application failed.
  • Merge of armbianEnv.txt kernel options was attempted for sunxi, mvebu and rockchip64.
  • DT folder determination based on sunxi approach.
  • DT file determination based on sunxi approach.
  • Compat with /boot/dtb/fdtfile.dtb and /boot/dtb/vendor/fdtfile.dtb.
  • Simplified some constructs by assuming U-Boot has successfully sourced us with a set of pre-set variables, like ${prefix} ${devtype} etc.
  • Actively set the ${kernel_comp_*} parameters based on calculations of load addresses.
  • Ability to select different kernel/initrd by setting kver in armbianEnv.txt

Also:

  • Any warning or error includes a 10 second delay to make sure the user is able to see and read them.
  • Any "informative" message added by the bootscript can be silenced by setting verbosity to 0
  • Attempted to make the bootscript 'reentrant' in away: all variables required for proper (re)execution are set, which should allow for the entire bootscript to be re-run on a next boot_target.
  • All variables used in for-loops are actively cleared from the environment to ensure for loops work as expected.
  • Any pre-set variable that might be used in next boot_target will be reset whenever necessary.

Documentation summary for feature / change

  • short description (copy / paste of PR title)
  • summary (description relevant for end users)
    Load address calculation can be disabled by adding load_addr_calc=off to armbianEnv.txt
    Load address calculation OBOE avoidance can be disabled by adding align_overlap_oboe_avoidance=off to armbianEnv.txt
    User can set custom fdtdir and fdtfile in armbianEnv.txt, but make sure to only specify DT filename in fdtfile. fdtdir will be used to both load DT, DT overlays and fixup scripts
  • example of usage (how to see this in function)

How Has This Been Tested?

  • Orangepi Zero (first gen):
    [12:39:52] U-Boot SPL 2024.01-armbian-2024.01-S866c-P6b16-Ha5c2-V367a-Bb703-R448a (Apr 29 2025 - 02:50:09 +0000)
    [12:39:52] DRAM: 512 MiB
    [12:39:52] Trying to boot from MMC1
    [12:39:53] ns16550_serial serial@1c28000: pinctrl_select_state_full: uclass_get_device_by_phandle_id: err=-19
    
    
    [12:39:53] U-Boot 2024.01-armbian-2024.01-S866c-P6b16-Ha5c2-V367a-Bb703-R448a (Apr 29 2025 - 02:50:09 +0000) Allwinner Technology
    
    [12:39:53] CPU:   Allwinner H3 (SUN8I 1680)
    [12:39:53] Model: Xunlong Orange Pi Zero
    [12:39:53] DRAM:  512 MiB
    [12:39:53] Core:  70 devices, 19 uclasses, devicetree: separate
    [12:39:53] WDT:   Not starting watchdog@1c20ca0
    [12:39:53] MMC:   mmc@1c0f000: 0, mmc@1c10000: 1
    [12:39:53] Loading Environment from FAT... Unable to use mmc 0:1...
    [12:39:53] In:    serial,usbkbd
    [12:39:53] Out:   serial
    [12:39:53] Err:   serial
    [12:39:53] Starting SCP...
    [12:39:53] Net:   SCP/INF: Crust v0.6.10000
    [12:39:53] eth0: ethernet@1c30000
    [12:39:53] starting USB...
    [12:39:53] Bus usb@1c1a000: sun4i_usb_phy phy@1c19400: External vbus detected, not enabling our own vbus
    [12:39:53] USB EHCI 1.00
    [12:39:53] Bus usb@1c1a400: USB OHCI 1.0
    [12:39:53] Bus usb@1c1b000: USB EHCI 1.00
    [12:39:53] Bus usb@1c1b400: USB OHCI 1.0
    [12:39:53] Bus usb@1c1c000: USB EHCI 1.00
    [12:39:53] Bus usb@1c1c400: USB OHCI 1.0
    [12:39:53] scanning bus usb@1c1a000 for devices... 1 USB Device(s) found
    [12:39:54] scanning bus usb@1c1a400 for devices... 1 USB Device(s) found
    [12:39:55] scanning bus usb@1c1b000 for devices... 1 USB Device(s) found
    [12:39:56] scanning bus usb@1c1b400 for devices... 1 USB Device(s) found
    [12:39:57] scanning bus usb@1c1c000 for devices... 1 USB Device(s) found
    [12:39:59] scanning bus usb@1c1c400 for devices... 1 USB Device(s) found
    [12:40:00]        scanning usb for storage devices... 0 Storage Device(s) found
    [12:40:00] Autoboot in 1 seconds, press <Space> to stop
    [12:40:01] switch to partitions #0, OK
    [12:40:01] mmc0 is current device
    [12:40:01] Scanning mmc 0:1...
    [12:40:01] Found U-Boot script /boot/boot.scr
    [12:40:01] 18117 bytes read in 4 ms (4.3 MiB/s)
    [12:40:01] ## Executing script at 43100000
    [12:40:01] Boot script loaded from mmc 0:1.
    [12:40:01] 687 bytes read in 3 ms (223.6 KiB/s)
    [12:40:01] Loaded/imported environment /boot/armbianEnv.txt to/from 0x45000000.
    [12:40:01] Found mainline kernel configuration.
    [12:40:01] 35473 bytes read in 11 ms (3.1 MiB/s)
    [12:40:01] Loaded DT /boot/dtb/sun8i-h2-plus-orangepi-zero.dtb to 0x43000000.
    [12:40:01] Working FDT set to 43000000
    [12:40:01] Loading kernel provided DT overlay(s) from /boot/dtb/overlay to 0x45000000 ..
    [12:40:01] 504 bytes read in 10 ms (48.8 KiB/s)
    [12:40:01] Applied DT overlay usbhost2 (/boot/dtb/overlay/sun8i-h3-usbhost2.dtbo).
    [12:40:01] 504 bytes read in 10 ms (48.8 KiB/s)
    [12:40:01] Applied DT overlay usbhost3 (/boot/dtb/overlay/sun8i-h3-usbhost3.dtbo).
    [12:40:01] 617 bytes read in 10 ms (59.6 KiB/s)
    [12:40:01] Applied DT overlay tve (/boot/dtb/overlay/sun8i-h3-tve.dtbo).
    [12:40:01] 374 bytes read in 9 ms (40 KiB/s)
    [12:40:01] Applied DT overlay i2c0 (/boot/dtb/overlay/sun8i-h3-i2c0.dtbo).
    [12:40:01] Loading user provided DT overlay(s) from /boot/overlay-user to 0x45000000 ..
    [12:40:01] 835 bytes read in 3 ms (271.5 KiB/s)
    [12:40:01] Applied user DT overlay rtc0-i2c-ds3231-rtc1-soc (/boot/overlay-user/rtc0-i2c-ds3231-rtc1-soc.dtbo).
    [12:40:01] 4185 bytes read in 10 ms (408.2 KiB/s)
    [12:40:01] ## Executing script at 45000000
    [12:40:01] Loaded/sourced fixup script /boot/dtb/overlay/sun8i-h3-fixup.scr to/at 0x45000000.
    [12:40:02] 10554192 bytes read in 441 ms (22.8 MiB/s)
    [12:40:02] Loaded compressed kernel image /boot/zImage to 4300a000.
    [12:40:02] Using compressed kernel image filesize 0xa10b50 bytes to calculate initial ramdisk load address.
    [12:40:02] 11621101 bytes read in 484 ms (22.9 MiB/s)
    [12:40:02] Loaded initial ramdisk /boot/uInitrd to 43a1b000.
    [12:40:02] Unknown command 'kaslrseed' - try 'help'
    [12:40:02] Not able to prepare for KASLR.
    [12:40:02] Kernel commandline arguments:
    [12:40:02]   root=/dev/nfs
    [12:40:02]   rootfstype=nfs
    [12:40:02]   rootwait
    [12:40:02]   splash=verbose
    [12:40:02]   earlycon
    [12:40:02]   console=ttyS0,115200
    [12:40:02]   consoleblank=0
    [12:40:02]   loglevel=8
    [12:40:02]   ubootpart=81f6566a-01
    [12:40:02]   usb-storage.quirks=0x2537:0x1066:u,0x2537:0x1068:u,0x174c:0x55aa:u
    [12:40:02]   dwc_otg.fiq_enable=0
    [12:40:02]   net.ifnames=0
    [12:40:02]   ipv6.disable=1
    [12:40:02]   rw
    [12:40:02]   nfsroot=192.168.2.34:/export/rootfs/blippi
    [12:40:02]   ip=dhcp
    [12:40:02]   nfsrootdebug
    [12:40:02]   nfs.enable_ino64=0
    [12:40:02]   cgroup_enable=cpuset
    [12:40:02]   cgroup_memory=1
    [12:40:02]   cgroup_enable=memory
    [12:40:02] Kernel image @ 0x4300a000 [ 0x000000 - 0xa10b50 ]
    [12:40:02] ## Loading init Ramdisk from Legacy Image at 43a1b000 ...
    [12:40:02]    Image Name:   uInitrd
    [12:40:02]    Image Type:   ARM Linux RAMDisk Image (gzip compressed)
    [12:40:02]    Data Size:    11621037 Bytes = 11.1 MiB
    [12:40:02]    Load Address: 00000000
    [12:40:02]    Entry Point:  00000000
    [12:40:02]    Verifying Checksum ... OK
    [12:40:02] ## Flattened Device Tree blob at 43000000
    [12:40:02]    Booting using the fdt blob at 0x43000000
    [12:40:02] Working FDT set to 43000000
    [12:40:02]    Loading Ramdisk to 494ea000, end 49fff2ad ... OK
    [12:40:02]    Loading Device Tree to 494de000, end 494e9fff ... OK
    [12:40:02] Working FDT set to 494de000
    
    [12:40:02] Starting kernel ...
    
    [12:40:03] [    0.000000] Booting Linux on physical CPU 0x0
    
  • Orangepi zero (first gen): OK
    U-Boot SPL 2024.01-armbian (Feb 23 2024 - 10:47:39 +0000)
    DRAM: 256 MiB
    Trying to boot from MMC1
    ns16550_serial serial@1c28000: pinctrl_select_state_full: uclass_get_device_by_phandle_id: err=-19
    
    
    U-Boot 2024.01-armbian (Feb 23 2024 - 10:47:39 +0000) Allwinner Technology
    
    CPU:   Allwinner H3 (SUN8I 1680)
    Model: Xunlong Orange Pi Zero
    DRAM:  256 MiB
    Core:  70 devices, 19 uclasses, devicetree: separate
    WDT:   Not starting watchdog@1c20ca0
    MMC:   mmc@1c0f000: 0, mmc@1c10000: 1
    Loading Environment from FAT... Unable to use mmc 0:1...
    In:    serial,usbkbd
    Out:   serial
    Err:   serial
    Starting SCP...
    Net:   SCP/INF: Crust v0.6.10000
    eth0: ethernet@1c30000
    starting USB...
    Bus usb@1c1a000: sun4i_usb_phy phy@1c19400: External vbus detected, not enabling our own vbus
    USB EHCI 1.00
    Bus usb@1c1a400: USB OHCI 1.0
    Bus usb@1c1b000: USB EHCI 1.00
    Bus usb@1c1b400: USB OHCI 1.0
    Bus usb@1c1c000: USB EHCI 1.00
    Bus usb@1c1c400: USB OHCI 1.0
    scanning bus usb@1c1a000 for devices... 1 USB Device(s) found
    scanning bus usb@1c1a400 for devices... 1 USB Device(s) found
    scanning bus usb@1c1b000 for devices... 1 USB Device(s) found
    scanning bus usb@1c1b400 for devices... 1 USB Device(s) found
    scanning bus usb@1c1c000 for devices... 1 USB Device(s) found
    scanning bus usb@1c1c400 for devices... 1 USB Device(s) found
           scanning usb for storage devices... 0 Storage Device(s) found
    Autoboot in 1 seconds, press <Space> to stop
    switch to partitions #0, OK
    mmc0 is current device
    Scanning mmc 0:1...
    Found U-Boot script /boot/boot.scr
    18202 bytes read in 5 ms (3.5 MiB/s)
    ## Executing script at 43100000
    Boot script loaded from mmc 0:1.
    265 bytes read in 4 ms (64.5 KiB/s)
    Loaded/imported environment /boot/armbianEnv.txt to/from 0x45000000.
    Found mainline kernel configuration.
    35473 bytes read in 9 ms (3.8 MiB/s)
    Loaded DT /boot/dtb/sun8i-h2-plus-orangepi-zero.dtb to 0x43000000.
    Working FDT set to 43000000
    Loading kernel provided DT overlay(s) from /boot/dtb/overlay to 0x45000000 ..
    504 bytes read in 11 ms (43.9 KiB/s)
    Applied DT overlay usbhost2 (/boot/dtb/overlay/sun8i-h3-usbhost2.dtbo).
    504 bytes read in 12 ms (41 KiB/s)
    Applied DT overlay usbhost3 (/boot/dtb/overlay/sun8i-h3-usbhost3.dtbo).
    4185 bytes read in 11 ms (371.1 KiB/s)
    ## Executing script at 45000000
    Loaded/sourced fixup script /boot/dtb/overlay/sun8i-h3-fixup.scr to/at 0x45000000.
    10554192 bytes read in 442 ms (22.8 MiB/s)
    Loaded compressed kernel image /boot/zImage to 4300a000.
    Using compressed kernel image filesize 0xa10b50 bytes to calculate initial ramdisk load address.
    11867098 bytes read in 497 ms (22.8 MiB/s)
    Loaded initial ramdisk /boot/uInitrd to 43a1b000.
    Unknown command 'kaslrseed' - try 'help'
    Not able to prepare for KASLR.
    Kernel commandline arguments:
      root=PARTUUID=1c01f668-02
      rootfstype=f2fs
      rootwait
      splash=verbose
      console=ttyS0,115200
      consoleblank=0
      loglevel=8
      ubootpart=1c01f668-01
      usb-storage.quirks=0x2537:0x1066:u,0x2537:0x1068:u,0x174c:0x55aa:u
      net.ifnames=0
      ipv6.disable=1
      cgroup_enable=cpuset
      cgroup_memory=1
      cgroup_enable=memory
    Kernel image @ 0x4300a000 [ 0x000000 - 0xa10b50 ]
    ## Loading init Ramdisk from Legacy Image at 43a1b000 ...
       Image Name:   uInitrd
       Image Type:   ARM Linux RAMDisk Image (gzip compressed)
       Data Size:    11867034 Bytes = 11.3 MiB
       Load Address: 00000000
       Entry Point:  00000000
       Verifying Checksum ... OK
    ## Flattened Device Tree blob at 43000000
       Booting using the fdt blob at 0x43000000
    Working FDT set to 43000000
       Loading Ramdisk to 494ae000, end 49fff39a ... OK
       Loading Device Tree to 494a2000, end 494adfff ... OK
    Working FDT set to 494a2000
    
    Starting kernel ...
    

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
    New warnings introduced:
    • Environment load failed
    • Environment import failed
    • DT load/application failed
    • DT (user) overlay load/application failed
    • Initial ramdisk load failed
    • Kernel image load failed
    • Boot failed
  • Any dependent changes have been merged and published in downstream modules
    Prequisite U-Boot setexpr command already merged via rockchip64/rk3328: U-Boot v2022.04/07 add setexpr #8260.

@djurny djurny requested a review from igorpecovnik as a code owner June 9, 2025 19:45
Copy link
Contributor

coderabbitai bot commented Jun 9, 2025

## Walkthrough

The boot-sunxi.cmd script was extensively rewritten to enhance the handling of environment variables, device tree blob (DTB) loading, overlay application, kernel and ramdisk image loading, and boot command execution. The changes introduce explicit default and preset environment variables, helper functions for error handling and messaging, improved disk-based environment loading, and more robust kernel command line construction. The script now searches for DTB files across multiple paths, applies overlays and fixups with error checking, dynamically calculates load addresses for kernel and ramdisk images, and selects the appropriate boot command based on kernel image type, with detailed logging and fallback mechanisms.

## Possibly related PRs

- armbian/build#8166: Both PRs focus on improving U-Boot boot scripts by enhancing device tree handling, dynamic load address calculation, and kernel/ramdisk loading with alignment and error handling, indicating a strong relation in their boot script refactoring approaches.  
- armbian/build#8272: The main PR and the retrieved PR both refactor and enhance the U-Boot bootscript for different platforms (sunxi vs mvebu) with closely matching improvements in environment variable handling, device tree and overlay loading, dynamic load address calculation, kernel image type detection, and boot command execution, indicating they are strongly related in purpose and implementation.  
- armbian/build#8189: The main PR’s extensive refactoring and enhancement of the boot-sunxi.cmd script, including improved environment handling, DTB and overlay loading, error reporting, and dynamic load address calculation, directly relates to the retrieved PR’s similar bootscript fixes and enhancements focused on error handling, address alignment, DT resizing, and overlay application in boot-mvebu.cmd.  

## Suggested labels

`ready to merge`

## Suggested reviewers

- igorpecovnik

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 65f2167 and 05daf37.

📒 Files selected for processing (1)
  • config/bootscripts/boot-sunxi.cmd (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • config/bootscripts/boot-sunxi.cmd
✨ Finishing Touches
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch feat/sunxi-boot-cmd-calc-load-addr-ao
  • 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 size/large PR with 250 lines or more 08 Milestone: Third quarter release Needs review Seeking for review Hardware Hardware related like kernel, U-Boot, ... labels Jun 9, 2025
@coderabbitai coderabbitai bot added the Ready to merge Reviewed, tested and ready for merge label Jun 9, 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 (4)
config/bootscripts/boot-sunxi.cmd (4)

14-14: Consider renaming align_overlap_oboe_avoidance to a clearer name.

The variable name align_overlap_oboe_avoidance is cryptic. Consider renaming it to something more descriptive like align_overlap_avoidance or prevent_address_overlap. The term "oboe" doesn't clarify the purpose.

-setenv align_overlap_oboe_avoidance "on"
+setenv align_overlap_avoidance "on"

38-38: Update variable reference if renamed.

If you rename align_overlap_oboe_avoidance as suggested, remember to update this reference as well.

-	test "${align_overlap_oboe_avoidance}" = "on" && setexpr l_addr_next ${l_addr_next} + 1 ;
+	test "${align_overlap_avoidance}" = "on" && setexpr l_addr_next ${l_addr_next} + 1 ;

183-211: Consider refactoring the deeply nested DTB search logic.

The 6-level deep nesting makes this code hard to follow. Consider refactoring using a loop that iterates through a list of paths to try, which would improve readability and maintainability.

Example approach:

# Define search paths in order of preference
setenv dtb_search_paths "${l_fdtdir}/${l_fdtfile}|${prefix}dtb${kver}/${vendor}/${l_fdtfile_basename}|${prefix}dtb${kver}/${l_fdtfile_basename}|${fdtdir}/${fdtfile}|${preset_fdtdir}/${preset_fdtfile}"

# Loop through paths
setenv dtb_found "false"
for dtb_path in ${dtb_search_paths}; do
    # Split path into dir and file
    if test -e ${devtype} ${devnum}:${distro_bootpart} "${dtb_path}"; then
        setenv l_fdtdir "..."  # extract dir
        setenv l_fdtfile "..." # extract file
        setenv dtb_found "true"
        break
    fi
done

if test "${dtb_found}" != "true"; then
    setenv l_message "Cannot find DT!"
    run func_critical_error || exit
fi

503-504: Remove redundant comment.

The comment "default booting command" on line 504 is redundant since this code is already within the compressed kernel type conditional block.

-			# default booting command
 			bootz ${l_kernel_addr_r} ${l_ramdisk_addr_r} ${fdt_addr_r}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0a001ec and fd8a12c.

📒 Files selected for processing (1)
  • config/bootscripts/boot-sunxi.cmd (1 hunks)
🔇 Additional comments (4)
config/bootscripts/boot-sunxi.cmd (4)

6-12: Good documentation for variable management strategy.

The explanation about preserving U-Boot's global variables and the naming convention (preset_x for saved values, l_x for local copies) helps prevent conflicts and makes the script more maintainable.


415-426: Well-implemented kernel type detection.

The magic number check for 'ARMd' (0x644d5241) at the correct offset is a reliable way to differentiate between flat (uncompressed) and compressed kernel images. Good implementation!


488-495: Good addition for debugging boot issues.

Displaying the kernel command line arguments helps users troubleshoot boot problems. This is a valuable debugging feature.


47-53: Well-designed error handling strategy.

The configurable error handling through exit_on_critical_errors provides good flexibility. Users can choose between fail-fast behavior for production or continue-on-error for debugging. The consistent use of run func_critical_error || exit pattern is good.

Also applies to: 214-215, 228-229, 331-332, 410-411, 478-479, 517-517

@djurny djurny force-pushed the feat/sunxi-boot-cmd-calc-load-addr-ao branch from fd8a12c to b5c66c8 Compare June 10, 2025 22:54
@github-actions github-actions bot removed the Ready to merge Reviewed, tested and ready for merge label Jun 10, 2025
@igorpecovnik
Copy link
Member

one U-Boot bootscript for (at least) mvebu, sunxi and rockchip64.

Great idea. Also when ready, remove deprecated scripts and perhaps name this common differently. Like generic, common or similar?

@djurny
Copy link
Contributor Author

djurny commented Jun 12, 2025

Hi @igorpecovnik,
For now it's generic but still copy/modify for some specifics. Would be nice to have a real common base and have the specials add-in with extra scripts, like how the DT fixup scripts are done at the moment.
PS Would be great to have some template action going on with this, so the boot.cmd can be templated during build time based on the board that's being built.
Groetjes,

@djurny djurny force-pushed the feat/sunxi-boot-cmd-calc-load-addr-ao branch from 3f68eb6 to 65f2167 Compare June 12, 2025 20:58
@coderabbitai coderabbitai bot added the Ready to merge Reviewed, tested and ready for merge label Jun 12, 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: 5

🧹 Nitpick comments (4)
config/bootscripts/boot-sunxi.cmd (4)

98-110: Undefined ${usbstoragequirks} may pollute bootargs

bootargs concatenation blindly appends usb-storage.quirks=${usbstoragequirks} even when the variable is empty, leaving a dangling = on the kernel cmdline.

Quick fix:

-if test "${usbstoragequirks}" != "" ; then
-	setenv bootargs "${bootargs} usb-storage.quirks=${usbstoragequirks}"
-fi

231-287: Overlay prefix guessing loop creates three test -e branches per overlay

This yields 3 × N I/O look-ups. Since overlays are located in a small fixed set of directories, compute the candidate list once, then iterate.

This improves cold-boot time on slow SD cards noticeably (>150 ms for 10 overlays).


497-506: Boot fallback loops indefinitely on persistent failure

After a failed booti/bootz, the script restores presets and prints “Could not boot kernel!”, but immediately executes func_critical_error. With exit_on_critical_errors=off it will just continue and eventually re-enter itself via the board’s bootcmd, looping forever.

Consider adding sleep 10; reset after the error to force manual intervention or next boot source.


522-524: Legacy boot path lacks error checking

load ... script.bin and subsequent bootz invocations ignore return codes. If script.bin is missing the board will hang silently. Wrap them in the same helper-based error handling used for the mainline path.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between b5c66c8 and 65f2167.

📒 Files selected for processing (1)
  • config/bootscripts/boot-sunxi.cmd (1 hunks)
🔇 Additional comments (2)
config/bootscripts/boot-sunxi.cmd (2)

118-141: setexpr 1 + 1 test for availability is brittle

Using maths on integer literals to probe for the existence of setexpr relies on silent fallback to decimal evaluation and may explode on some older U-Boot builds. Prefer version or help setexpr:

-if setexpr load_addr_calc 1 + 1 ; then
+if help setexpr 2>/dev/null ; then

364-366: fdt resize without size argument may fail on very small DTBs

Some downstream U-Boots require an explicit size when shrinking. Use fdt resize only after verifying the command succeeds, or recalc the minimal size with fdt addr & fdt header.

Comment on lines +422 to +427
if test "${l_magic_msw}${l_magic_lsw}" != "" && itest "${l_magic_msw}${l_magic_lsw}" -eq 0x644d5241 ; then
setenv l_kernel_image_type "flat"
else
setenv l_kernel_image_type "compressed"
fi

Copy link
Contributor

@coderabbitai coderabbitai bot Jun 12, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue

Kernel-type detection concatenates hex strings before numeric compare

${l_magic_msw}${l_magic_lsw} forms a string, e.g. 0xd4 + 0x410xd40x41, which itest treats as decimal and fails, falsely flagging flat images as compressed.

Proposed minimal fix:

-	if test "${l_magic_msw}${l_magic_lsw}" != "" && itest "${l_magic_msw}${l_magic_lsw}" -eq 0x644d5241 ; then
+	setexpr l_magic 0x${l_magic_msw}${l_magic_lsw}
+	if itest ${l_magic} -eq 0x644d5241 ; then
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if test "${l_magic_msw}${l_magic_lsw}" != "" && itest "${l_magic_msw}${l_magic_lsw}" -eq 0x644d5241 ; then
setenv l_kernel_image_type "flat"
else
setenv l_kernel_image_type "compressed"
fi
# build a single numeric magic value from the two halves
setexpr l_magic 0x${l_magic_msw}${l_magic_lsw}
if itest ${l_magic} -eq 0x644d5241 ; then
setenv l_kernel_image_type "flat"
else
setenv l_kernel_image_type "compressed"
fi
🤖 Prompt for AI Agents
In config/bootscripts/boot-sunxi.cmd around lines 422 to 427, the kernel-type
detection concatenates two hex string variables into one string before comparing
numerically, causing incorrect evaluation. To fix this, convert each hex string
to a number separately, then combine them using bitwise operations to form a
single numeric value for the comparison. Replace the string concatenation and
itest numeric comparison with this numeric combination and comparison to
correctly detect the kernel image type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi bot,
Nice try, but alas - miss. This has been tested to work on aarch64 boards. I will check the U-Boot sources again to make sure if you do not have a valid point or not.
Groetjes,

Copy link
Contributor

Choose a reason for hiding this comment

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

@djurny Thanks for double-checking!
My concern was purely about the parsing rules of U-Boot’s itest/setexpr:

${l_magic_msw} and ${l_magic_lsw} are produced with setexpr.w … *${ptr} – they already include the 0x prefix, so the two variables will look like 0x644d and 0x5241.
• When they’re concatenated (${l_magic_msw}${l_magic_lsw}) the result becomes the literal string 0x644d0x5241. In recent U-Boot (hush) that string is not recognised as hex, so itest … -eq treats it as a decimal value and the comparison with 0x644d5241 will always fail (unless by coincidence the decimals match).

On boards that boot fine today you’ll still get the correct path because the script later retries with bootz, so it’s easy to miss. The edge case shows up when:

  1. a flat Image (vmlinux) is really required (aarch64) and
  2. the compressed fallback is absent or fails to load.

If after your re-read of the U-Boot sources you still see no risk, feel free to keep it as-is; I just wanted to flag the string-vs-number ambiguity so it doesn’t bite later—especially on shells that tighten parsing in future versions.

Groetjes!

@igorpecovnik
Copy link
Member

so the boot.cmd can be templated during build time based on the board that's being built.

Aha, it still need changes for board / family specifics. This can probably be integrated right where cp is executing conditioned on this generic boot script and leaving legacy as is.

@The-going
Copy link
Contributor

@djurny
I strongly recommend that you do not modify existing files.
Instead, copy the underlying file and save it as boot-common.cmd or boot-generic.cmd.
This is necessary so that the device maintainers can verify the correct operation for their device and the u-boot version.

There are also related changes for here

@djurny
Copy link
Contributor Author

djurny commented Jun 13, 2025

Hi @The-going,
Thanks for your remarks!
Let me paraphrase to make sure I understand your concern and proposal.

  • Keep current boot-sunxi.cmd as-is.
  • Add boot-generic.cmd (or any name indicating this bootscript is for generic use) with the boot-sunxi.cmd as in this PR.

Not sure how to switch between the generic and current bootscripts though. Any ideas on that?

Do let me know if I got the point (or not).

Groetjes,

@djurny djurny force-pushed the feat/sunxi-boot-cmd-calc-load-addr-ao branch from 65f2167 to 05daf37 Compare June 13, 2025 23:18
@github-actions github-actions bot removed the Ready to merge Reviewed, tested and ready for merge label Jun 13, 2025
@coderabbitai coderabbitai bot added the Ready to merge Reviewed, tested and ready for merge label Jun 13, 2025
@djurny djurny marked this pull request as draft June 13, 2025 23:34
@The-going
Copy link
Contributor

Hi @The-going, Thanks for your remarks! Let me paraphrase to make sure I understand your concern and proposal.

* Keep current `boot-sunxi.cmd` as-is.

* Add `boot-generic.cmd` (or any name indicating this bootscript is for generic use) with the `boot-sunxi.cmd` as in this PR.

Yes, you got it right.

Not sure how to switch between the generic and current bootscripts though. Any ideas on that?

The maintainer of this device will decide which script to use, old or new.
It explicitly states which script to use for this subfamily.
config/sources/families/sun50iw9-bpi.conf
or directly in the device configuration file.

armbian/build> grep -nr 'BOOTSCRIPT=' ./config/*
./config/boards/rock-s0.conf:30:        declare -g BOOTSCRIPT="boot-rockchip64-ttyS0.cmd:boot.cmd"
./config/boards/aml-s805-mxq.tvb:8:BOOTSCRIPT="boot-aml-s805-mxq.cmd:boot.cmd"
./config/boards/qemu-uboot-arm64.csc:20:        declare -g BOOTSCRIPT="boot-qemu-arm64.cmd:boot.cmd"
./config/boards/onecloud.csc:8:BOOTSCRIPT="boot-onecloud.cmd:boot.cmd"
./config/boards/qemu-uboot-x86.csc:23:  declare -g BOOTSCRIPT="boot-qemu-x86.cmd:boot.cmd"
./config/boards/rockpi-s.conf:30:       declare -g BOOTSCRIPT="boot-rockchip64-ttyS0.cmd:boot.cmd"
./config/boards/odroidc1.conf:15:BOOTSCRIPT="boot-odroid-c1.ini:boot.ini"
........

Before switching to a new script for the devices that I support, I would like to test this thoroughly.
You have published tests for two devices.
You can specify which script to use directly in the configuration file for these two devices.
This leaves the option of choice (maneuver) for other escorts.

Do let me know if I got the point (or not).

Groetjes,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
08 Milestone: Third quarter release 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.

3 participants