Skip to content

Commit 91f5f42

Browse files
committed
feat(lib_video): robust module resolution & insmod (KO_DIRS files/dirs, compressed .ko, altroot)
- Improve video_find_module_file() and video_insmod_with_deps() to reliably locate and load downstream modules across diverse layouts. - Accept KO_DIRS entries as files or directories; try fast direct matches before bounded find (depth-limited). - Support hyphen/underscore name variants and compressed modules (.ko.xz, .ko.gz, .ko.zst, .ko.bz2) in all search paths. - Preserve preference order: modinfo -n → KO_DIRS (if KO_PREFER_CUSTOM=1) → KO_TREE/lib/modules/$KVER → /lib/modules/$KVER/updates → system tree → KO_DIRS (fallback when not preferred first). Log with video_log_resolve. - Enhance insmod path: - Parse deps via modinfo -F depends, try modprobe -d "$KO_TREE" then regular modprobe, then direct insmod using resolved paths. - On-the-fly decompress compressed modules to /run/iris_mods/$KVER/ and insmod the decompressed artifact. Log with video_log_load_success. Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent 83fbf03 commit 91f5f42

File tree

1 file changed

+105
-18
lines changed

1 file changed

+105
-18
lines changed

Runner/utils/lib_video.sh

Lines changed: 105 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,58 +105,144 @@ video_log_fw_hint() {
105105
fi
106106
}
107107

108-
# -----------------------------------------------------------------------------
109-
# Shared module introspection helpers (kept; used by dump)
110-
# -----------------------------------------------------------------------------
108+
# Install a module file into /lib/modules/$(uname -r)/updates and run depmod.
109+
# Usage: video_ensure_moddir_install <module_path> <logical_name>
110+
video_ensure_moddir_install() {
111+
mp="$1" # source path to .ko
112+
mname="$2" # logical module name for logging
113+
kr="$(uname -r 2>/dev/null)"
114+
[ -z "$kr" ] && kr="$(find /lib/modules -mindepth 1 -maxdepth 1 -type d -printf '%f\n' 2>/dev/null | head -n 1)"
115+
116+
if [ -z "$mp" ] || [ ! -f "$mp" ]; then
117+
log_warn "install-ko: invalid module path: $mp"
118+
return 1
119+
fi
120+
121+
updates="/lib/modules/$kr/updates"
122+
if [ ! -d "$updates" ]; then
123+
if ! mkdir -p "$updates" 2>/dev/null; then
124+
log_warn "install-ko: cannot create $updates (read-only FS?)"
125+
return 1
126+
fi
127+
fi
128+
129+
base="$(basename "$mp")"
130+
dst="$updates/$base"
131+
132+
do_copy=1
133+
if [ -f "$dst" ]; then
134+
if command -v md5sum >/dev/null 2>&1; then
135+
src_md5="$(md5sum "$mp" 2>/dev/null | awk '{print $1}')"
136+
dst_md5="$(md5sum "$dst" 2>/dev/null | awk '{print $1}')"
137+
[ -n "$src_md5" ] && [ "$src_md5" = "$dst_md5" ] && do_copy=0
138+
else
139+
if cmp -s "$mp" "$dst" 2>/dev/null; then
140+
do_copy=0
141+
fi
142+
fi
143+
fi
144+
145+
if [ "$do_copy" -eq 1 ]; then
146+
if ! cp -f "$mp" "$dst" 2>/dev/null; then
147+
log_warn "install-ko: failed to copy $mp -> $dst"
148+
return 1
149+
fi
150+
chmod 0644 "$dst" 2>/dev/null || true
151+
sync 2>/dev/null || true
152+
log_info "install-ko: copied $(basename "$mp") to $dst"
153+
else
154+
log_info "install-ko: up-to-date at $dst"
155+
fi
156+
157+
if command -v depmod >/dev/null 2>&1; then
158+
if depmod -a "$kr" >/dev/null 2>&1; then
159+
log_info "install-ko: depmod -a $kr done"
160+
else
161+
log_warn "install-ko: depmod -a $kr failed (continuing)"
162+
fi
163+
else
164+
log_warn "install-ko: depmod not found; modprobe may fail to resolve deps"
165+
fi
166+
167+
[ -n "$mname" ] && video_log_resolve "$mname" updates-tree "$dst"
168+
printf '%s\n' "$dst"
169+
return 0
170+
}
171+
172+
# Takes a logical module name (e.g. iris_vpu), resolves/copies into updates/, depmods, then loads deps+module.
111173
video_insmod_with_deps() {
112-
# Takes a logical module name (e.g. qcom_iris), resolves the path, then insmods deps+module
113174
m="$1"
175+
[ -z "$m" ] && { log_warn "insmod-with-deps: empty module name"; return 1; }
176+
177+
[ -z "$MODPROBE" ] && MODPROBE="modprobe"
114178

115179
if video_has_module_loaded "$m"; then
116-
log_info "module already loaded (insmod path skipped): $m"
180+
log_info "module already loaded (skip): $m"
117181
return 0
118182
fi
119183

120-
path="$(video_find_module_file "$m")" || path=""
121-
if [ -z "$path" ] || [ ! -f "$path" ]; then
184+
mp="$(video_find_module_file "$m")" || mp=""
185+
if [ -z "$mp" ] || [ ! -f "$mp" ]; then
122186
log_warn "insmod fallback: could not locate module file for $m"
123187
return 1
124188
fi
189+
log_info "resolve: $m -> $mp"
190+
191+
case "$mp" in
192+
/lib/modules/*) staged="$mp" ;;
193+
*)
194+
staged="$(video_ensure_moddir_install "$mp" "$m")" || staged=""
195+
if [ -z "$staged" ]; then
196+
log_warn "insmod-with-deps: staging into updates/ failed for $m"
197+
return 1
198+
fi
199+
;;
200+
esac
201+
202+
if "$MODPROBE" -q "$m" 2>/dev/null; then
203+
video_log_load_success "$m" modprobe "$staged"
204+
return 0
205+
fi
206+
log_warn "modprobe failed: $m (attempting direct insmod)"
125207

126208
deps=""
127209
if video_exist_cmd modinfo; then
128-
deps="$(modinfo -F depends "$path" 2>/dev/null | tr ',' ' ' | tr -s ' ')"
210+
deps="$(modinfo -F depends "$staged" 2>/dev/null | tr ',' ' ' | tr -s ' ')"
129211
fi
130212

131213
for d in $deps; do
132-
if [ -z "$d" ]; then
133-
continue
134-
fi
214+
[ -z "$d" ] && continue
135215
if video_has_module_loaded "$d"; then
136216
continue
137217
fi
138218
if ! "$MODPROBE" -q "$d" 2>/dev/null; then
139219
dpath="$(video_find_module_file "$d")" || dpath=""
220+
if [ -z "$dpath" ] || [ ! -f "$dpath" ]; then
221+
kr="$(uname -r 2>/dev/null)"
222+
[ -z "$kr" ] && kr="$(find /lib/modules -mindepth 1 -maxdepth 1 -type d -printf '%f\n' 2>/dev/null | head -n 1)"
223+
cand="/lib/modules/$kr/updates/${d}.ko"
224+
[ -f "$cand" ] && dpath="$cand"
225+
fi
140226
if [ -n "$dpath" ] && [ -f "$dpath" ]; then
141227
if insmod "$dpath" 2>/dev/null; then
142228
video_log_load_success "$d" dep-insmod "$dpath"
143229
else
144-
log_warn "dep insmod failed for $dpath"
230+
log_warn "dep insmod failed: $d ($dpath)"
145231
fi
146232
else
147-
log_warn "dep resolve failed for $d"
233+
log_warn "dep resolve failed: $d"
148234
fi
149235
else
150236
video_log_load_success "$d" dep-modprobe
151237
fi
152238
done
153239

154-
if insmod "$path" 2>/dev/null; then
155-
video_log_load_success "$m" insmod "$path"
240+
if insmod "$staged" 2>/dev/null; then
241+
video_log_load_success "$m" insmod "$staged"
156242
return 0
157243
fi
158244

159-
log_warn "insmod failed for $path"
245+
log_warn "insmod failed for $staged"
160246
return 1
161247
}
162248

@@ -256,12 +342,12 @@ video_log_load_success() {
256342

257343
video_find_module_file() {
258344
# Resolve a module file path for a logical mod name (handles _ vs -).
259-
# Prefers: modinfo -n, then .../updates/, then general search.
345+
# Prefers: modinfo -n, then .../updates/, then general search (and KO_DIRS/KO_TREE if provided).
260346
m="$1"
261347
kr="$(uname -r 2>/dev/null)"
262348

263349
if [ -z "$kr" ]; then
264-
kr="$(find /lib/modules -mindepth 1 -maxdepth 1 -type d -printf '%f\n' 2>/dev/null | head -n1)"
350+
kr="$(find /lib/modules -mindepth 1 -maxdepth 1 -type d -printf '%f\n' 2>/dev/null | head -n 1)"
265351
fi
266352

267353
if video_exist_cmd modinfo; then
@@ -287,6 +373,7 @@ video_find_module_file() {
287373
IFS=':'
288374
for d in $KO_DIRS; do
289375
IFS="$OLD_IFS"
376+
# SC2015 fix: avoid `[ -n "$d" ] && [ -d "$d" ] || continue`
290377
if [ -z "$d" ] || [ ! -d "$d" ]; then
291378
continue
292379
fi

0 commit comments

Comments
 (0)