Skip to content

Commit 3000726

Browse files
authored
Merge pull request #70 from Salamandar/relative_paths
Allow a 'paths-relative-to' key in the firmware config
2 parents 0ddb515 + 03865c2 commit 3000726

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

docs/fw_binaries.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ explains what firmware and configuration options are needed for each SoC family.
1010
A firmware configuration file for snagrecover has the following structure:
1111

1212
```
13+
paths-relative-to: THIS_FILE
1314
fw_1:
1415
path: /path/to/fw_1
1516
option1: value1
@@ -28,6 +29,20 @@ type of SoC you are using, you will probably want to configure your U-Boot build
2829
so that it can interact with snagflash correctly after recovery (e.g. use DFU,
2930
UMS or fastboot).
3031

32+
The key `paths-relative-to` is optional and indicates that paths to images are
33+
relative to a specific path, either:
34+
35+
- `CWD`: the current working directory, e.g the default behaviour
36+
- `THIS_FILE`: the directory containing the current configuration file
37+
- a path to a directory.
38+
39+
If `THIS_FILE` or a path is specified, snagrecover will effectively join this
40+
path and the images paths. `CWD` is actually a no-op and will keep the default
41+
behaviour.
42+
Absolute paths to images will not be modified by this option.
43+
44+
This is useful when distributing the configuration file alongside images.
45+
3146
## General tips on configuring U-Boot
3247

3348
In many cases, in can be necessary to build the recovery U-Boot yourself e.g.

src/snagrecover/config.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with this program; if not, write to the Free Software
1818
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919

20+
import os
2021
import yaml
2122
from snagrecover.utils import (
2223
cli_error,
@@ -93,6 +94,22 @@ def check_soc_model(soc_model: str):
9394
return None
9495

9596

97+
def complete_fw_paths(fw_config: dict, this_file_path: str) -> None:
98+
paths_relative_to_conf = fw_config.pop("paths-relative-to", "CWD")
99+
if paths_relative_to_conf == "CWD":
100+
return
101+
elif paths_relative_to_conf == "THIS_FILE":
102+
path_relative_to = os.path.dirname(this_file_path)
103+
else:
104+
path_relative_to = path_relative_to_conf
105+
106+
for binary in fw_config.keys():
107+
if "path" in fw_config[binary]:
108+
fw_config[binary]["path"] = os.path.join(
109+
path_relative_to, fw_config[binary]["path"]
110+
)
111+
112+
96113
def init_config(args: list):
97114
# this is the only time that config.recovery_config should be modified!
98115
# get soc model
@@ -138,11 +155,13 @@ def init_config(args: list):
138155
# get firmware configs
139156
for path in args.firmware_file:
140157
with open(path, "r") as file:
141-
fw_configs = {**fw_configs, **yaml.safe_load(file)}
142-
if not isinstance(fw_configs, dict):
143-
cli_error(
144-
f"firmware config passed to CLI did not evaluate to dict: {fw_configs}"
145-
)
158+
fw_config_file = yaml.safe_load(file)
159+
if not isinstance(fw_config_file, dict):
160+
cli_error(
161+
f"firmware config passed to CLI did not evaluate to dict: {fw_config_file}"
162+
)
163+
complete_fw_paths(fw_config_file, path)
164+
fw_configs = {**fw_configs, **fw_config_file}
146165
recovery_config["firmware"] = fw_configs
147166

148167
# store input arguments in config

0 commit comments

Comments
 (0)