Skip to content

scripts: west_commands: runners: fix and removes the '--dt-flash' option. #85395

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 3 commits into
base: main
Choose a base branch
from

Conversation

miggazElquez
Copy link
Contributor

The '--dt-flash' parameter accepts a string like "y", "yes", "no", etc, and is supposed to be converted into a boolean value. This is only the theory as in practice, the default value is set to 'n' and is never converted to False afterwards.

Some runners are depending on this behavior, so this pull request fixes these runners, and corrects the value of the '--dt-flash' option.

Once everything is fixed, I noticed that the --dt-flash option was not really useful, as it was set to True almost everywhere. So the fourth commit removes the option, except for the spi_burn runner, which seems to be using it. I didn't really understand what was spi_burn trying to do, so I just moved the handling of the option into spi_burn.py. Maybe it isn't useful and should also be removed ?

If --dt-flash is useful and I just didn't saw why, I can remove the fourth commit and only keep the first three.

@mbolivar
Copy link
Contributor

For the historical record, I added --dt-flash a long time ago when DT was new and experimental in zephyr; I could see a justification for getting rid of it as long we respect the default code partition when it is enabled and the appropriate kconfig is set directing the build system to use it

@miggazElquez
Copy link
Contributor Author

miggazElquez commented Feb 21, 2025

This PR doesn't change the behavior of any board, on all the concerned boards the flash address comes from CONFIG_FLASH_BASE_ADDRESS which in most cases comes from the devicetree. The --dt-flash option, in the current state, only allow to have a default flash adress of 0 when --dt-flash is not set (apart from the spi_burn runner), and no boards used this (the only one I found was the ch32v003, where it just happened that 0 was the correct value)

pdgendt
pdgendt previously approved these changes Feb 26, 2025
Copy link
Collaborator

@pdgendt pdgendt left a comment

Choose a reason for hiding this comment

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

I'm inclined to approve this PR as it removes an artifact that has become obsolete, but I'll assign @mbolivar as his judgement trumps mine.

@pdgendt pdgendt assigned mbolivar and unassigned pdgendt Feb 26, 2025
Due to a bug in scripts/west_commands/runners/core.py, the value of
--dt-flash can currently be True, False, or the default value, 'n'. The
script silabs_commander.py only check the truth value of --dt-flash,
 so the default value is interpreted as a truthy. That means that the
default case was effectively the same as if dt-flash was set to True.

Set this flag to True by default explicitly, in the same
way as done for pyocd or jlink.

Signed-off-by: Miguel Gazquez <[email protected]>
The '--dt-flash' parameter accepts a string like "y", "yes", "no", etc,
and is supposed to be converted into a boolean value. This is only the
theory as in practice, the default value is set to 'n' and is never
converted to False afterwards.

Set the default value to False.

Signed-off-by: Miguel Gazquez <[email protected]>
In minichlink and spi_burn, the script checks if dt_flash is True by
checking if the value is "y". But dt_flash is a boolean.
Fix these checks.

Also, when dt-flash is True, spi_burn calculate the address in a
convoluted way, by substrating CONFIG_FLASH_LOAD_OFFSET to
itself.
Simplify this computation.

Signed-off-by: Miguel Gazquez <[email protected]>
@miggazElquez
Copy link
Contributor Author

Rebased on main, and removed flash_addr=False from two runners that I had missed.

@martinjaeger martinjaeger removed their request for review March 10, 2025 09:36
@miggazElquez miggazElquez requested a review from pdgendt March 11, 2025 16:20
@pdgendt
Copy link
Collaborator

pdgendt commented Mar 12, 2025

@mbolivar PTAL

VynDragon
VynDragon previously approved these changes Mar 27, 2025
import os
import re
import subprocess
import time

from runners.core import BuildConfiguration, RunnerCaps, ZephyrBinaryRunner

_YN_CHOICES = ['Y', 'y', 'N', 'n', 'yes', 'no', 'YES', 'NO']
Copy link
Collaborator

Choose a reason for hiding this comment

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

It could be possible to set dt_flash to always on unless a addr is set (which can be 0)

@@ -558,7 +558,7 @@ def add_parser(cls, parser):
parser.add_argument('-i', '--dev-id', help=argparse.SUPPRESS)

if caps.flash_addr:
parser.add_argument('--dt-flash', default='n', choices=_YN_CHOICES,
parser.add_argument('--dt-flash', default=False, choices=_YN_CHOICES,
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't make sense to me. False is not in _YN_CHOICES. The point of action=_DTFlashAction was to do the conversion to bool based on the value. If that's not working, the solution is to fix the action, not change the default to a value that isn't in choices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The action is not run if there is no value passed to the argument, only if the value is passed through the command line. So without argument the final value is 'n', but if we pass --dt-flash n, then _DTFlashAction is called, and we correctly get False.

@@ -557,15 +535,6 @@ def add_parser(cls, parser):
else:
parser.add_argument('-i', '--dev-id', help=argparse.SUPPRESS)

if caps.flash_addr:
parser.add_argument('--dt-flash', default=False, choices=_YN_CHOICES,
Copy link
Contributor

Choose a reason for hiding this comment

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

OK, I just got to this commit while reviewing and see the earlier patch changing default=False has no effect in the PR at all, so I'm now I'm just confused why it's needed in the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The situation currently is that dt-flash is broken in some cases. The idea was to first have commits fixing it (the first three). Then, as in the fixed situation every user of the option set it to True, delete it.

I did it like that because I thought it would be easier to understand why I wanted to delete the option, and I can easily either squash everything into one commit to just remove dt-flash, or just drop the last one to just fix the handling of the option but keeping it. Sorry if instead of helping it just made everything more confusing.

@@ -309,7 +305,6 @@ class RunnerCaps:
commands: set[str] = field(default_factory=lambda: set(_RUNNERCAPS_COMMANDS))
dev_id: bool = False
mult_dev_ids: bool = False
flash_addr: bool = False
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a backwards incompatible API change to a stable API:

https://docs.zephyrproject.org/latest/develop/west/build-flash-debug.html#out-of-tree-runners

I don't mind if you want to take it through the stable API change process, but it seems like it would be easier to just keep it around for compatibility, no?

Copy link
Contributor Author

@miggazElquez miggazElquez Mar 28, 2025

Choose a reason for hiding this comment

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

Oh I didn't know it was part of the stable API. I guess it would be easier to keep it then.
But it means that a lot of out of tree runners are probably mishandling the option as well, because if they test the option using something like that :

#In the constructor
                dt_flash=args.dt_flash
#In the handling of the option
        if self.dt_flash:
            flash_addr = self.flash_address_from_build_conf(self.build_conf)
        else:
            flash_addr = 0

then they treat the default case as True instead of False (case of the runner for Silabs commander, see first commit). So by fixing the problem we will cause these runners to just go from an effective default of True to False.

If they used this:

     dt_flash=(args.dt_flash == "y"),

then the option can never be set to True (case of minichlink and spi_burn, see commit 3), but at least the fix will not break anything.

If some runners noticed the bug and were coded accordingly (by testing for 'n' and the boolean values) then this should not break anything.

I don't know what it the way to handle this situation, where fixing a bug in a stable API will probably lead to breaks in the code of the user of the API ?

@miggazElquez
Copy link
Contributor Author

miggazElquez commented Mar 28, 2025

I removed the commit removing dt-flash, now there is just the fix left.

@kartben
Copy link
Collaborator

kartben commented Apr 19, 2025

@mbolivar please revisit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants