Commit Graph

31 Commits

Author SHA1 Message Date
Juerg Haefliger 554c4761e9 copy-firmware.sh: Run check_whence.py only if in a git repo
check_whence.py requires the current tree to be a git repo so run it only
if that is indeed the case.

Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
2024-11-08 16:42:31 +01:00
Emil Velikov a89349c33c copy-firmware.sh: rename variables in symlink hanlding
Currently we use f(ile) and d(irectory), over the more common ones t(arget) and
l(ink). Rename things appropriately.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2024-10-10 14:33:32 +00:00
Emil Velikov 9ce7dac098 copy-firmware.sh: remove no longer reachable test -L
The check_whence.py script ensures that links defined in WHENCE are not in-tree.
Since we're calling the script, we no longer need the convoluted path and
associated --prune tag.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2024-10-10 14:33:32 +00:00
Emil Velikov 25d5e6352b copy-firmware.sh: remove no longer reachable test -f
With previous commit we call check_whence.py, which ensures that all files
listed are available. Drop the now dead code.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2024-10-10 14:33:32 +00:00
Emil Velikov 6edd0fd36a copy-firmware.sh: call ./check_whence.py before parsing the file
Currently ./check_whence.py is used when submitting new firmware, while
copy-firmware.sh when the firmware is to be consumed.

Since the latter does (very little) validation, having a malformed WHENCE file
can lead to all sorted of problems. From the obvious, where it errors out, to
more serious one where it overwrites or executes something it should not have.

Just call check_whence.py and error out. It takes 0.2s on my 5 year old
mid-range laptop, so the overhead is negligible.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2024-10-10 14:33:32 +00:00
Emil Velikov 32f71d6d45 copy-firmware.sh: warn if the destination folder is not empty
If the user provides an existing non-empty folder (their /usr/lib/firmware/ or otherwise)
there is a high chance we'll silently overwrite existing files. That may or may not be what
they wanted, so throw a warning so highlight that.

v2:
 - use printf instead of echo

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2024-10-10 14:33:32 +00:00
Emil Velikov 97d200d75b copy-firmware.sh: add err() helper
v2:
 - use printf instead of echo -e

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2024-10-10 14:33:32 +00:00
Emil Velikov 6360b4a1d7 copy-firmware.sh: fix indentation
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2024-10-10 14:33:32 +00:00
Emil Velikov a3446bc1a7 copy-firmware.sh: reset and consistently handle destdir
Currently we don't reset/override the destdir variable, so we end up inheriting
whatever the caller's environment has for it. While it may work, it's not
particularly consistent (be that within the script or other tools) nor is it
obvious.

While in here, ensure we handle the variable with test -z/-n instead of varying
other constructs.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2024-10-10 14:33:32 +00:00
Emil Velikov e8f8537768 Revert "copy-firmware: Support additional compressor options"
This reverts commit 2bad80e7ed.

The commit effectively added accidental command injection, while it was aiming
to control the compression flags.

In practise you'd want to use ZSTD_CLEVEL and ZSTD_NBTHREADS for zstd. As
documented in zstd(1) it allows for up-to level 19, which is fine since the
kernel does not support higher levels.

Arch, Alpine and likely other distributions have been using this approach
since day one.

The other compressors like xz have equivalent.

Cc: Juerg Haefliger <juerg.haefliger@canonical.com>
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2024-10-10 14:33:32 +00:00
Emil Velikov ee8c336ab3 copy-firmware.sh: flesh out and fix dedup-firmware.sh
Flesh out the de-duplication logic in separate script. The copy-firmware.sh is
already complex enough and de-duplication doesn't really fit in there.

In the process we migrate away from the open-coded `ln --relative`. We also
avoid touching symlinks, which are not created by rdfind. Otherwise we end up
"fixing" the folder to folder symlinks (created earlier in the process) and
things explode.

As result we also get a few bonuses:
 - the COPYOPTS shell injection is gone - the variable was never used
 - people can dedup as separate step if/when they choose to do so

Aside: based on the noise in git log and around distros ... I'm wondering if
having the de-duplication as opt-in, would have been better. Is it too late to
change or the ship has sailed?

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
2024-10-10 14:33:32 +00:00
Juerg Haefliger 121e467fde copy-firmware: Handle links to uncompressed files
If copy-firwmware is told to compress files, it blindly assumes that
the target is indeed compressed (unless it's a directory) and creates
any symlinks accordingly.

This leads to broken symlinks for links that point at RawFiles:
adspr.jsn.zst -> ../qcm6490/adspr.jsn.zst
adsps.jsn.zst -> ../qcm6490/adsps.jsn.zst

That should be:
adspr.jsn -> ../qcm6490/adspr.jsn
adsps.jsn -> ../qcm6490/adsps.jsn

Fix that by checking if the target (directory or file) exists. And while
at it, add a check for broken symlinks at the end.

Fixes: 541f96c0fa ("qcom: qcm6490: add ADSP and CDSP firmware")
Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
2024-09-13 08:38:36 +02:00
Josh Boyer 4124f8f928
Make rdfind optional
A number of distributions don't include rdfind by default.  This is an
optimization, so make it optional via a --ignore-duplicates option and a
new make target.

Distributions that include rdfind should add it as a dependency for
their linux-firmware package.  Those that don't should use the new
target or option.

Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2023-11-27 14:12:07 -05:00
Eli Schwartz c6823ce2e5 Makefile, copy-firmware: Use portable "command -v" to detect installed programs
The "which" utility is not guaranteed to be installed either, and if it
is, its behavior is not portable either. This means that when rdfind /
pre-commit are installed, the `which` check will report a fatal error
because the which tool did not exist and the shell returned a nonzero
status when attempting to fork+exec. If it did exist, it might not be an
implementation of `which` that returns nonzero when commands do not
exist.

Conversely, the "command -v" shell builtin is required to exist in all
POSIX 2008 compliant shells, and is thus guaranteed to work everywhere.

For some in-depth discussions on the topic, see:
- https://mywiki.wooledge.org/BashFAQ/081
- https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then/85250#85250

Examples of open-source shells likely to be installed as /bin/sh on
Linux, which implement the 15-year-old standard: ash, bash, busybox,
dash, ksh, mksh and zsh.

A side benefit of using the POSIX portable option is that it requires
neither an external disk executable, nor (because unlike "which", the
exit code is reliable) a subshell fork. This therefore represents a mild
speedup.

Signed-off-by: Eli Schwartz <eschwartz93@gmail.com>
2023-11-26 11:09:32 -06:00
Mario Limonciello cf8315ded9 Ensure rdfind is installed
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
2023-11-08 17:14:52 -06:00
Mario Limonciello 195eae5962 Add checks for destination directory being specified
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
2023-11-08 17:13:51 -06:00
Mario Limonciello 4c55675d7a Fix symlink creation for some files
rdfind was trying to duplicate symlinks created by WHENCE causing
problems.  Move the call before WHENCE creates symlinks.

Reported-by: Timur Tabi <ttabi@nvidia.com>
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
2023-11-08 17:03:14 -06:00
Mario Limonciello 3e79f6b83b Create symlinks for all firmware that is duplicate using rdfind
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
2023-08-31 10:15:45 -05:00
Juerg Haefliger db99828b24
copy-firmware: Introduce 'RawFile' keyword
Currently, 'Raw: <filename>' needs to be added to WHENCE for files that
must not be compressed. This means that such files need to be listed
twice ('File: <filename>' and 'Raw: <filename>') which is not pretty and
error prone. Also, the current implementation is broken for filenames
that contain quotes (") and it also requires grep'ing of WHENCE for every
processed line which slows things down.

Fix all of the above by introducing a new keyword 'RawFile' so that such
files are listed only once, quotes are handled correctly and grep is no
longer needed.

Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2023-08-28 07:18:15 -04:00
Juerg Haefliger 2bad80e7ed
copy-firmware: Support additional compressor options
Any options following the compressor option --zstd or --xz are passed on
to the compressor as additional options so that - for example - the
compression level can be specified:

  $ ./copy-firmware.sh --zst -19 <destdir>

Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2023-08-28 07:17:16 -04:00
Benjamin Drung b6ea35ff6b
copy-firmware: Fix linking directories when using compression
When `copy-firmware` is called with `--xz` or `--zstd` it will create
broken symlinks for directories:

```
$ ./copy-firmware -v --zstd $dir
[...]
creating link qcom/LENOVO/21BX.zst -> ../sc8280xp/LENOVO/21BX.zst
```

The original target `../sc8280xp/LENOVO/21BX` is a directory. Adding the
compression extension to the directory name breaks the link. The
directory `qcom/sc8280xp/LENOVO/21BX` exists but
`qcom/sc8280xp/LENOVO/21BX.zst` does not exist.

The relative symlink needs to be resolved. If it points to a directory,
create the symlink without the compression extension.

Signed-off-by: Benjamin Drung <benjamin.drung@canonical.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2023-07-25 06:53:30 -04:00
Benjamin Drung 0a51959c6f
copy-firmware: Fix test: unexpected operator
dash is the default /bin/sh on Debian based system.

```
dash ./copy-firmware.sh --zstd example
./copy-firmware.sh: 37: test: unexpected operator
```

`=` should be used with the test command for POSIX conformance.

Signed-off-by: Benjamin Drung <benjamin.drung@canonical.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2023-07-25 06:53:21 -04:00
Emil Velikov ee91452dac
Makefile, copy-firmware: support xz/zstd compressed firmware
The kernel has supported compressed firmware for quite some time. So
let's add a couple of targets to produce that. In practical terms this
means it we'll use ~5x times less space on disk.

Reportedly the amd ucode, needs to be uncompressed _within_ the
initrd in order to work. Using compressed ucode in late load just works.
Ideally this will be addressed by the initrd generators, but considering
the files are tiny in size let's skip the compression.

v2
 - commit message, skip compression for files annotated as Raw

v3
 - rebase

[Drop extra verbose statement in zstd case, Josh Boyer
<jwboyer@kernel.org>]

Cc: David Woodhouse <dwmw2@infradead.org>
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2023-06-25 12:18:57 -04:00
Emil Velikov ad2ce8beee
copy-firmware: silence the last shellcheck warnings
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2023-06-25 11:59:31 -04:00
Emil Velikov 67bf50e72c
copy-firmware: drop obsolete backticks, quote
As mentioned by shellcheck backticks are considered legacy. Plus we
should ensure the output is quoted, otherwise we'll get word splitting.

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2023-06-25 11:58:52 -04:00
Emil Velikov 77f31a8000
copy-firmware: tweak sed invocation
Add space between the arguments and pattern and combine patterns where
possible.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2023-06-25 11:58:33 -04:00
Emil Velikov 40fa2b2096
copy-firmware: quote deskdir and dirname
Properly quote, otherwise we'll get word splitting. In other words:
Files might end up installed to /some/foo/, where /some path/foo bar/ is
expected.

v2:
 - rebase/split && also quote the $() output

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2023-06-25 11:58:09 -04:00
Konrad Weihmann eaee2dacc2
Makefile: replace mkdir by install
mkdir -p creates paths that are bound to user's settings and therefore
can lead to different file mode bits of the base paths accross different
machines.
Use install instead, as this tool is not prone to such behavior.

Signed-off-by: Konrad Weihmann <kweihmann@outlook.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2022-05-31 12:57:56 -04:00
Tony Nguyen c1bef9e010
copy-firmware: Always write Link: entries
File: entries in the WHENCE file overwrite existing files in the target
directory, however, Link: entries are skipped if the file exists in the
target directory. This can cause issues if the Link: entry is updated, but
the target directory contains an old symlink. Do not skip writing Link:
entries if the file exists, always create the symlink. This matches the
behavior of File: entries and ensures symlinks will contain values from
the WHENCE file.

Signed-off-by: Tony Nguyen <anthony.l.nguyen@intel.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2020-10-05 08:06:02 -04:00
Thierry Reding 2de7abd480
copy-firmware: Create symlinks from WHENCE file
Rather than require symlinks to be created in the filesystem and have
duplicate Link: entries in the WHENCE file, make copy-firmware.sh create
the symlinks on the fly.

Signed-off-by: Thierry Reding <treding@nvidia.com>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2019-10-04 18:34:02 -04:00
Takashi Iwai 07b925b450
Install only listed firmware files
The current make-install procedure leaves lots of garbage files that
aren't really firmware files in /lib/firmware.

Instead of copy-all-and-prune approach, copy only the listed files and
links in WHENCE by make-install for assuring only the proper firmware
files.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Josh Boyer <jwboyer@kernel.org>
2019-08-15 07:46:53 -04:00