summaryrefslogtreecommitdiff
path: root/autoprobe.zsh
blob: 344f1fe3f003710977db4c43273acb8f42e5a950 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env zsh
#
# Copyright (C) 2026 Maria Lisina
# SPDX-License-Identifier: Apache-2.0
#
# A script for probing basic hardware from /sys/bus on modern laptops

set -e

dir="${0%/*}"
lkddb="${dir}/lkddb.list"
defconfig="${dir}/autoprobe.defconfig"

if ! [[ -f "${lkddb}" ]]
then
    echo "lkddb.list is not found!" \
        "\nPlease download it from \`https://cateee.net/sources/lkddb\` for your kernel version," \
        "\nand rename it to lkddb.list in the same directory as this script."
    exit 1
fi

rm -f "${defconfig}"

printf "#\n# Automatically generated by ${0##*/}\n#\n" >> "${defconfig}"
printf "# This is not a real defconfig file, but a minimal list of kernel\n" >> "${defconfig}"
printf "# configurations for a better understanding of your hardware.\n#\n" >> "${defconfig}"
printf "# Expect false positives, use with caution!\n#\n" >> "${defconfig}"

add_section()
{
    printf '\n# %s\n' "${1}" >> "${defconfig}"
}

extract_configs_from_lkddb()
{
    if [[ -n "${1}" ]]
    then
        grep "${1}" "${lkddb}" | grep -o 'CONFIG_.*:' | sed 's/ .$//'
    fi
}

add_config_if_unique()
{
    if [[ -n "${1}" ]] && ! grep -q "${1}" "${defconfig}"
    then
        printf '%s\n' "${1}" >> "${defconfig}"
    fi
}

extract_driver_from_uevent()
{
    if [[ -f "${1}" ]]
    then
        < "${1}" | grep '^DRIVER=' | cut -d '=' -f 2 | sed 's/[-_]/\[-_\]/g'
    fi
}

search_and_add_configs()
{
    local configs=($(extract_configs_from_lkddb "${1}"))

    for config in ${configs[@]}
    do
        add_config_if_unique "${config}"
    done
}

search_with_module_fallback()
{
    local configs=($(extract_configs_from_lkddb "${1}"))

    if [[ -n "${2}" && -z "${configs}" ]]
    then
        configs=($(extract_configs_from_lkddb "^module ${2} "))
    fi

    for config in ${configs[@]}
    do
        add_config_if_unique "${config}"
    done
}

echo "Adding ACPI devices to defconfig..."
add_section "ACPI"

for dev in $(ls -1 /sys/bus/acpi/devices)
do
    if ! [[ -f "/sys/bus/acpi/devices/${dev}/hid" ]]
    then
        continue
    fi

    search_and_add_configs "^acpi \"$(< "/sys/bus/acpi/devices/${dev}/hid")\""
done

echo "Adding PCI devices to defconfig..."
add_section "PCI"

for dev in $(ls -1 /sys/bus/pci/devices)
do
    if ! [[ -f "/sys/bus/pci/devices/${dev}/vendor" ]]
    then
        continue
    fi

    pci_id=$(printf '%s %s' \
        "$(< "/sys/bus/pci/devices/${dev}/vendor")" \
        "$(< "/sys/bus/pci/devices/${dev}/device")" |
        sed 's/0x//g'
    )
    driver=$(extract_driver_from_uevent "/sys/bus/pci/devices/${dev}/uevent")

    search_with_module_fallback "^pci ${pci_id} " "${driver}"
done

echo "Adding HD-audio devices to defconfig..."
add_section "HD-audio"

for dev in $(ls -1 /sys/bus/hdaudio/devices)
do
    if ! [[ -f "/sys/bus/hdaudio/devices/${dev}/vendor_id" ]]
    then
        continue
    fi

    vendor_id="$(< "/sys/bus/hdaudio/devices/${dev}/vendor_id" | sed 's/0x//g')"
    driver=$(extract_driver_from_uevent "/sys/bus/hdaudio/devices/${dev}/uevent")

    search_with_module_fallback "^hda ${vendor_id} " "${driver}"
done

echo "Adding HID devices to defconfig..."
add_section "HID"

for dev in $(ls -1 /sys/bus/hid/devices)
do
    if ! [[ -f "/sys/bus/hid/devices/${dev}/uevent" ]]
    then
        continue
    fi

    hid_id="$(< "/sys/bus/hid/devices/${dev}/uevent" |
        grep '^HID_ID=' |
        cut -d '=' -f 2 |
        tr ':' ' ' |
        tr '[:upper:]' '[:lower:]'
    )"
    driver=$(extract_driver_from_uevent "/sys/bus/hid/devices/${dev}/uevent")

    search_with_module_fallback "^hid ${hid_id} " "${driver}"
done

echo "Adding USB devices to defconfig..."
add_section "USB"

for dev in $(ls -1 /sys/bus/usb/devices)
do
    if ! [[ -f "/sys/bus/usb/devices/${dev}/uevent" ]]
    then
        continue
    fi

    driver=$(extract_driver_from_uevent "/sys/bus/usb/devices/${dev}/uevent")

    search_and_add_configs "^module ${driver} "
done

echo "Adding I2C devices to defconfig..."
add_section "I2C"

for dev in $(ls -1 /sys/bus/i2c/devices)
do
    if ! [[ -f "/sys/bus/i2c/devices/${dev}/name" ]]
    then
        continue
    fi

    modname="$(< "/sys/bus/i2c/devices/${dev}/name")"

    if [[ -z "${modname}" ]]
    then
        continue
    fi

    search_and_add_configs "^module.*\"${modname}\""
done

echo "Adding PNP devices to defconfig..."
add_section "PNP"

for dev in $(ls -1 /sys/bus/pnp/devices)
do
    if ! [[ -f "/sys/bus/pnp/devices/${dev}/id" ]]
    then
        continue
    fi

    search_and_add_configs "^pnp.*\"$(< "/sys/bus/pnp/devices/${dev}/id")\""
done

echo "Adding Platform devices to defconfig..."
add_section "Platform"

for dev in $(ls -1 /sys/bus/platform/devices)
do
    if ! [[ -f "/sys/bus/platform/devices/${dev}/uevent" ]]
    then
        continue
    fi

    driver=$(extract_driver_from_uevent "/sys/bus/platform/devices/${dev}/uevent")

    search_and_add_configs "^module ${driver} "
done

echo "Adding Serial devices to defconfig..."
add_section "Serial"

for dev in $(ls -1 /sys/bus/serio/devices)
do
    if ! [[ -f "/sys/bus/serio/devices/${dev}/id/type" ]]
    then
        continue
    fi

    serio_type="$(< "/sys/bus/serio/devices/${dev}/id/type")"
    serio_proto="$(< "/sys/bus/serio/devices/${dev}/id/proto")"

    if [[ "${serio_proto}" == "00" ]]
    then
        serio_proto=".."
    fi

    serio_id=$(printf '%s %s' "${serio_type}" "${serio_proto}")

    search_and_add_configs "^serio ${serio_id}"
done

echo "Adding Filesystems to defconfig..."
add_section "Filesystems"

for fs in $(mount | cut -d ' ' -f 5 | sort)
do
    search_and_add_configs "^fs \"${fs}\""
done

echo "Adding CPU features to defconfig..."
add_section "CPU Features"

if [[ -f "/sys/bus/cpu/devices/cpu0/uevent" ]]
then
    modalias="$(< "/sys/bus/cpu/devices/cpu0/uevent" |
        grep '^MODALIAS=' |
        cut -d '=' -f 2 |
        sed 's/[-_]/\[-_\]/g'
    )"

    for modname in $(modprobe -R "${modalias}" | sed 's/[-_]/\[-_\]/g')
    do
        search_and_add_configs "^module ${modname} "
    done
fi

echo "Adding Modules to defconfig..."
add_section "Modules"

for module in $(lsmod | sed -e '1d' -e 's/ .*$//g' -e 's/[-_]/\[-_\]/g' | sort)
do
    search_and_add_configs "^module ${module} "
done

sed -i '/^CONFIG__UNKNOWN__.*$/d' "${defconfig}"