Skip to content

Commit da08589

Browse files
committed
west: sign/rimage: use rimage config to find SOF directory
After removal of SOF from manifest, if rimage is in path, the build would fail because the gen-uuid-reg.py cannot be found anymore. To fix this, use the rimage config path to find the SOF directory since the rimage config path is a directory under SOF. This would allow rimage to again sign images to run on hardware. Signed-off-by: Daniel Leung <[email protected]>
1 parent 9f0ce75 commit da08589

File tree

2 files changed

+95
-20
lines changed

2 files changed

+95
-20
lines changed

boards/intel/adsp/doc/intel_adsp_generic.rst

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,26 @@ Signing Tool
6060
As firmware binary signing is mandatory on Intel products from Skylake onwards,
6161
you will also need to set up the SOF rimage signing tool and key.
6262

63-
.. code-block:: shell
64-
65-
cd zephyrproject
66-
west config manifest.project-filter -- +sof
67-
west update
68-
cd modules/audio/sof/tools/rimage
63+
Clone the SOF repository and follow the instructions in the rimage :file:`README.md`
64+
to build the tool on your system.
6965

70-
Follow the instructions in the rimage :file:`README.md` to build the tool on
71-
your system. You can either copy the executable to a directory in your PATH or
66+
You can either copy the executable to a directory in your PATH or
7267
use ``west config rimage.path /path/to/rimage-build/rimage``; see more details
7368
in the output of ``west sign -h``. Running directly from the build directory
7469
makes you less likely to use an obsolete rimage version by mistake.
7570

76-
Platform-specific configuration files are located in the ``rimage/config/``
77-
subdirectory. For a different configuration directory you can use:
78-
``west config build.cmake-args -- -DRIMAGE_CONFIG_PATH=/path/to/my/rimage/config``.
71+
The west signing script will also need to know the path to the SOF directory.
72+
Depending on whether you need to use twister:
73+
74+
* If you only need to build with ``west build``:
75+
76+
* Platform-specific configuration files are located in the ``rimage/config/``
77+
subdirectory. For a different configuration directory you can use:
78+
``west config build.cmake-args -- -DRIMAGE_CONFIG_PATH=/path/to/my/rimage/config``.
79+
80+
* If you also need to use twister to build and run on hardware:
81+
82+
* Environment variable `SOF_SRC_DIR` must be set to point to the SOF directory.
7983

8084

8185
Xtensa Toolchain (Optional)

scripts/west_commands/sign.py

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,75 @@ def edt_flash_params(cmd, flash):
423423

424424
class RimageSigner(Signer):
425425

426+
def check_if_sof_dir(self, maybe_sof_dir):
427+
'''
428+
Check if a pathlib.Path() object is the root of the SOF tree.
429+
430+
Returns a Path object if it is indeed the SOF tree, or None if not.
431+
432+
This checks if the file "versions.json" exists under the path, as
433+
this file contains the SOF versioning information.
434+
'''
435+
versions_json_file = maybe_sof_dir / 'versions.json'
436+
if versions_json_file.exists():
437+
return maybe_sof_dir
438+
439+
return None
440+
441+
def get_sof_src_dir(self):
442+
'''
443+
Find the SOF tree.
444+
445+
Returns a Path object if SOF tree is found, or None if not.
446+
'''
447+
sof_src_dir = None
448+
449+
# Try to find SOF directory via path to rimage config files pointed to
450+
# via CMake cached variable "RIMAGE_CONFIG_PATH" if it exists.
451+
# The config files are inside <sof top>/tools/tools/rimage/config in
452+
# the SOF tree.
453+
conf_dir = self.cmake_cache.get('RIMAGE_CONFIG_PATH')
454+
if conf_dir:
455+
# Config path is <sof top>/tools/rimage/config/
456+
# So need to go up 3 levels to get SOF directory.
457+
maybe_sof_dir = pathlib.Path(conf_dir).parent.parent.parent
458+
459+
# Make sure this is the actual SOF directory as
460+
# RIMAGE_CONFIG_PATH may point to somewhere else.
461+
sof_src_dir = self.check_if_sof_dir(maybe_sof_dir)
462+
463+
# Try to find SOF if it is included via manifest as a project
464+
# under the main manifest (e.g. included via a sub-manifest).
465+
if not sof_src_dir:
466+
try:
467+
sof_proj = self.command.manifest.get_projects(['sof'], allow_paths=False)
468+
sof_src_dir = pathlib.Path(sof_proj[0].abspath)
469+
470+
# Since SOF is pulled in as a project, we assume it is
471+
# the correct one as the manifest has to be modified
472+
# manually.
473+
except ValueError:
474+
pass
475+
476+
# Try to find SOF as SOF is the top level manifest via west init.
477+
if not sof_src_dir:
478+
maybe_sof_dir = pathlib.Path(manifest.manifest_path()).parent
479+
480+
# Make sure this is the actual SOF directory
481+
# as the top level manifest may not be SOF.
482+
sof_src_dir = self.check_if_sof_dir(maybe_sof_dir)
483+
484+
# If the above all failed to find the SOF tree, see if the path is specified
485+
# via environment variable "SOF_SRC_DIR" as last resort.
486+
if not sof_src_dir and os.getenv("SOF_SRC_DIR"):
487+
maybe_sof_dir = pathlib.Path(os.getenv("SOF_SRC_DIR"))
488+
489+
# Make sure this is the actual SOF directory
490+
# as the top level manifest may not be SOF.
491+
sof_src_dir = self.check_if_sof_dir(maybe_sof_dir)
492+
493+
return sof_src_dir
494+
426495
def rimage_config_dir(self):
427496
'Returns the rimage/config/ directory with the highest precedence'
428497
args = self.command.args
@@ -438,7 +507,11 @@ def rimage_config_dir(self):
438507
def generate_uuid_registry(self):
439508
'Runs the uuid-registry.h generator script'
440509

441-
generate_cmd = [sys.executable, str(self.sof_src_dir / 'scripts' / 'gen-uuid-reg.py'),
510+
uuid_script_path = self.sof_src_dir / 'scripts' / 'gen-uuid-reg.py'
511+
if not uuid_script_path.exists():
512+
self.command.die(f"{uuid_script_path} does not exists.")
513+
514+
generate_cmd = [sys.executable, str(uuid_script_path),
442515
str(self.sof_src_dir / 'uuid-registry.txt'),
443516
str(pathlib.Path('zephyr') / 'include' / 'generated' / 'uuid-registry.h')
444517
]
@@ -554,13 +627,11 @@ def sign(self, command, build_dir, build_conf, formats):
554627
if not args.quiet:
555628
command.inf('Signing with tool {}'.format(tool_path))
556629

557-
try:
558-
sof_proj = command.manifest.get_projects(['sof'], allow_paths=False)
559-
sof_src_dir = pathlib.Path(sof_proj[0].abspath)
560-
except ValueError: # sof is the manifest
561-
sof_src_dir = pathlib.Path(manifest.manifest_path()).parent
562-
563-
self.sof_src_dir = sof_src_dir
630+
self.sof_src_dir = self.get_sof_src_dir()
631+
if self.sof_src_dir:
632+
command.inf(f"SOF directory: {self.sof_src_dir}")
633+
else:
634+
command.die("Cannot find SOF directory.")
564635

565636

566637
command.inf('Signing for SOC target ' + target)
@@ -605,7 +676,7 @@ def sign(self, command, build_dir, build_conf, formats):
605676
if '-k' not in sign_config_extra_args + args.tool_args:
606677
# rimage requires a key argument even when it does not sign
607678
cmake_default_key = cache.get('RIMAGE_SIGN_KEY', 'key placeholder from sign.py')
608-
extra_ri_args += [ '-k', str(sof_src_dir / 'keys' / cmake_default_key) ]
679+
extra_ri_args += [ '-k', str(self.sof_src_dir / 'keys' / cmake_default_key) ]
609680

610681
if args.tool_data and '-c' in args.tool_args:
611682
command.wrn('--tool-data ' + args.tool_data + ' ignored! Overridden by: -- -c ... ')

0 commit comments

Comments
 (0)