2023-01-29 11:47:40 +00:00
function kernel_drivers_create_patches_hash_only( ) {
hash_only = "yes" kernel_drivers_create_patches " ${ @ } "
2022-12-24 14:33:59 +00:00
}
function kernel_drivers_create_patches( ) {
2023-01-29 11:47:40 +00:00
kernel_drivers_patch_hash = "undetermined" # outer scope
kernel_drivers_patch_file = "undetermined" # outer scope
2022-12-24 14:33:59 +00:00
2023-01-23 02:29:49 +00:00
declare hash_files # any changes in these files will trigger a cache miss; also any changes in misc .patch with "wireless" at start or "wifi" anywhere in the name
calculate_hash_for_files " ${ SRC } /lib/functions/compilation/patch/drivers_network.sh " " ${ SRC } /lib/functions/compilation/patch/drivers-harness.sh " " ${ SRC } " /patch/misc/wireless*.patch " ${ SRC } " /patch/misc/*wifi*.patch
2022-12-24 14:33:59 +00:00
declare cache_key_base = " ${ KERNEL_MAJOR_MINOR } _ ${ LINUXFAMILY } "
declare cache_key = " ${ cache_key_base } _ ${ hash_files } "
display_alert "Cache key base:" " $cache_key_base " "debug"
display_alert "Cache key:" " $cache_key " "debug"
declare cache_dir_base = " ${ SRC } /cache/patch/kernel-drivers "
mkdir -p " ${ cache_dir_base } "
declare cache_target_file = " ${ cache_dir_base } / ${ cache_key } .patch "
# outer scope variables:
2023-01-29 11:47:40 +00:00
kernel_drivers_patch_file = " ${ cache_target_file } " # outer scope
kernel_drivers_patch_hash = " ${ hash_files } " # outer scope
if [ [ " ${ hash_only :- "no" } " = = "yes" ] ] ; then
2023-02-03 14:36:28 +00:00
display_alert "Hash-only kernel driver requested" " $kernel_drivers_patch_hash - returning " "debug"
2023-01-29 11:47:40 +00:00
return 0
fi
declare kernel_work_dir = " ${ 1 } "
declare kernel_git_revision = " ${ 2 } "
2022-12-24 14:33:59 +00:00
# If the target file exists, we can skip the patch creation.
if [ [ -f " ${ cache_target_file } " ] ] ; then
2023-01-05 14:15:18 +00:00
# Make sure the file is larger than 512 bytes. Old versions of this code left small/empty files on failure.
if [ [ $( stat -c%s " ${ cache_target_file } " ) -gt 512 ] ] ; then
display_alert " Using cached drivers patch file for ${ LINUXFAMILY } - ${ KERNEL_MAJOR_MINOR } " " ${ cache_key } " "cachehit"
return
else
display_alert " Removing invalid/small cached drivers patch file for ${ LINUXFAMILY } - ${ KERNEL_MAJOR_MINOR } " " ${ cache_key } " "warn"
run_host_command_logged rm -fv " ${ cache_target_file } "
fi
2022-12-24 14:33:59 +00:00
fi
2023-01-04 14:05:20 +00:00
2023-01-03 18:20:34 +00:00
display_alert "Creating patches for kernel drivers" " version: ' ${ KERNEL_MAJOR_MINOR } ' family: ' ${ LINUXFAMILY } ' " "info"
2022-12-24 14:33:59 +00:00
# if it does _not_ exist, fist clear the base, so no old patches are left over
run_host_command_logged rm -fv " ${ cache_dir_base } / ${ cache_key_base } * "
# since it does not exist, go create it. this requires working tree.
declare target_patch_file = " ${ cache_target_file } "
2023-01-03 18:20:34 +00:00
display_alert "Preparing patch for drivers" " version: ${ KERNEL_MAJOR_MINOR } kernel_work_dir: ${ kernel_work_dir } " "debug"
2022-12-24 14:33:59 +00:00
kernel_drivers_prepare_harness " ${ kernel_work_dir } " " ${ kernel_git_revision } "
}
function kernel_drivers_prepare_harness( ) {
declare kernel_work_dir = " ${ 1 } "
declare kernel_git_revision = " ${ 2 } "
2023-01-05 14:11:22 +00:00
# outer scope variable: target_patch_file
2022-12-24 14:33:59 +00:00
declare -a drivers = (
2023-01-04 14:34:10 +00:00
driver_generic_bring_back_ipx
2022-12-24 14:33:59 +00:00
driver_rtl8152_rtl8153
driver_rtl8189ES
driver_rtl8189FS
driver_rtl8192EU
driver_rtl8811_rtl8812_rtl8814_rtl8821
driver_xradio_xr819
driver_rtl8811CU_rtl8821C
driver_rtl8188EU_rtl8188ETV
driver_rtl88x2bu
driver_rtl88x2cs
driver_rtl8822cs_bt
driver_rtl8723DS
driver_rtl8723DU
driver_rtl8822BS
2023-01-23 02:08:12 +00:00
driver_uwe5622_allwinner
2022-12-24 14:33:59 +00:00
)
# change cwd to the kernel working dir
cd " ${ kernel_work_dir } " || exit_with_error " Failed to change directory to ${ kernel_work_dir } "
#run_host_command_logged git status
run_host_command_logged git reset --hard " ${ kernel_git_revision } "
# git: remove tracked files, but not those in .gitignore
run_host_command_logged git clean -fd # no -x here
for driver in " ${ drivers [@] } " ; do
display_alert "Preparing driver" " ${ driver } " "info"
# reset variables used by each driver
declare version = " ${ KERNEL_MAJOR_MINOR } "
declare kernel_work_dir = " ${ 1 } "
declare kernel_git_revision = " ${ 2 } "
# for compatibility with `master`-based code
declare kerneldir = " ${ kernel_work_dir } "
declare EXTRAWIFI = "yes" # forced! @TODO not really?
# change cwd to the kernel working dir
cd " ${ kernel_work_dir } " || exit_with_error " Failed to change directory to ${ kernel_work_dir } "
# invoke the driver; non-armbian-next code.
" ${ driver } "
# recover from possible cwd changes in the driver code
cd " ${ kernel_work_dir } " || exit_with_error " Failed to change directory to ${ kernel_work_dir } "
done
# git: check if there are modifications
if [ [ -n " $( git status --porcelain) " ] ] ; then
display_alert "Drivers have modifications" " exporting patch into ${ target_patch_file } " "info"
export_changes_as_patch_via_git_format_patch
else
exit_with_error "Applying drivers didn't produce changes."
fi
}
function export_changes_as_patch_via_git_format_patch( ) {
# git: add all modifications
2023-01-04 14:05:20 +00:00
run_host_command_logged git add .
2022-12-24 14:33:59 +00:00
2023-01-05 09:59:25 +00:00
declare -a common_envs = (
" HOME= ${ HOME } "
" PATH= ${ PATH } "
)
2022-12-24 14:33:59 +00:00
# git: commit the changes
2023-01-09 16:39:36 +00:00
declare -a git_params = (
"-c" "commit.gpgsign=false" # force gpgsign off; the user might have it enabled and it will fail.
)
2022-12-24 14:33:59 +00:00
declare -a commit_params = (
2023-01-05 14:15:18 +00:00
"--quiet" # otherwise too much output
2022-12-24 14:33:59 +00:00
-m " drivers for ${ LINUXFAMILY } version ${ KERNEL_MAJOR_MINOR } "
--author= " ${ MAINTAINER } < ${ MAINTAINERMAIL } > "
)
2023-01-04 14:05:20 +00:00
declare -a commit_envs = (
" GIT_COMMITTER_NAME= ${ MAINTAINER } "
" GIT_COMMITTER_EMAIL= ${ MAINTAINERMAIL } "
)
2023-01-09 16:39:36 +00:00
run_host_command_logged env -i " ${ common_envs [@]@Q } " " ${ commit_envs [@]@Q } " git " ${ git_params [@]@Q } " commit " ${ commit_params [@]@Q } "
2022-12-24 14:33:59 +00:00
2023-01-04 14:05:20 +00:00
# export the commit as a patch
2022-12-24 14:33:59 +00:00
declare formatpatch_params = (
"-1" "--stdout"
2023-01-05 14:17:18 +00:00
"--unified=3" # force 3 lines of diff context
"--keep-subject" # do not add a prefix to the subject "[PATCH] "
# "--no-encode-email-headers" # do not encode email headers - @TODO does not exist under focal, disable
2022-12-24 14:33:59 +00:00
'--signature' " Armbian generated patch from drivers for kernel ${ version } and family ${ LINUXFAMILY } "
'--stat=120' # 'wider' stat output; default is 80
'--stat-graph-width=10' # shorten the diffgraph graph part, it's too long
"--zero-commit" # Output an all-zero hash in each patch’ s From header instead of the hash of the commit.
)
2023-01-05 14:15:18 +00:00
declare target_patch_file_tmp = " ${ target_patch_file } .tmp "
# The redirect ">" is escaped here, so it's run inside the subshell, not in the current shell.
run_host_command_logged env -i " ${ common_envs [@]@Q } " git format-patch " ${ formatpatch_params [@]@Q } " ">" " ${ target_patch_file_tmp } "
# move the tmp to final, if it worked.
run_host_command_logged mv -v " ${ target_patch_file_tmp } " " ${ target_patch_file } "
2022-12-24 14:33:59 +00:00
}