summary refs log tree commit diff
path: root/host/rootfs/scripts/modprobe/gen_modalias.sh.awk
blob: 65852c16823516dc01d5b03e13cccdcaa06cfba1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/awk -f
# SPDX-License-Identifier: EUPL-1.2+
# SPDX-FileCopyrightText: 2021 Alyssa Ross <hi@alyssa.is>
#
# This program generates a shell script expected to be run with a
# kernel uevent in its environment, for example from mdev.conf.  The
# shell script will match on the modalias from the event, and choose a
# command to run based on what driver would be loaded by the kernel's
# modules.alias file.  By default, it will run modprobe -q "$MODALIAS".
#
# Matching on default drivers instead of modalias patterns directly is
# a much friendlier and more maintainable way to configure a system.
# The mappings from default drivers to commands is configured in a
# file, the path to which is passed to this script in the modmap
# variable.  Each line of the file should contain a driver name, then
# some blanks, and then a command to run.  Empty lines, as well as
# lines starting with optional blanks followed by a # character, are
# ignored.

function normalize_driver_name(name) {
	return gsub("-", "_", name)
}

BEGIN {
	while (getline line < modmap) {
		if (line == "" || match(line, "^[[:blank:]]*#"))
			continue

		if (!match(line, "[[:blank:]]+"))
			exit 1

		driver = substr(line, 1, RSTART - 1)
		command = substr(line, RSTART + RLENGTH)

		normalize_driver_name(driver)

		remap[driver] = command
	}

	print "#!/bin/sh -x"
	# Break this up to avoid confusing reuse lint.
	print "# SPDX-License-Identifier" ": CC0-1.0"
	print "#"
	print "# Generated by gen_modalias.sh.awk."
	print "# Rules file: " modmap
}

FNR == 1 {
	print "# Input file: " FILENAME
}

$1 == "alias" && $3 in remap {
	normalize_driver_name($3)
	command = remap[$3]
	patterns[command][patterns_lengths[command]++] = $2
}

END {
	for (_ in patterns) {
		print ""
		print "case \"$MODALIAS\" in"
		break
	}

	for (exec in patterns) {
		printf "\t"
		for (n in patterns[exec]) {
			if (n > 0)
				printf "|"
			printf "%s", patterns[exec][n]
		}
		printf ")\n\t\texec %s\n\t\t;;\n", exec
	}

	for (_ in patterns) {
		printf "\t*)\n\t\texec modprobe -q \"$MODALIAS\"\n\t\t;;\nesac"
		break
	}
}