summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsaac J. Manjarres <isaacmanjarres@google.com>2023-01-25 14:45:03 -0800
committerIsaac J. Manjarres <isaacmanjarres@google.com>2023-05-02 10:55:50 -0700
commitb9ba4b67e90b85d01eb2f9cebf62830a73574cd9 (patch)
tree56b32f51ce1eedf483a30b22a65d8f20430eca34
parent76ca8d970ce7741813d93d3e0e1f1ec1f4290d6a (diff)
downloadbuild-b9ba4b67e90b85d01eb2f9cebf62830a73574cd9.tar.gz
Currently, build.sh makes it so that the same modules.load file is used for normal boot and booting into recovery/fastbootd and charger modes during first-stage init. This means that during normal boot, the modules that are needed for recovery and charger modes are loaded during first-stage init, which is not required. Add support for specifying a list of modules that are required for booting into recovery or charger mode, in addition to the set of modules loaded during first-stage init. This introduces two new environment variables called MODULES_RECOVERY_LIST and MODULES_CHARGER_LIST, which point to files that contain lists the modules that are needed for booting into recovery or charger mode respectively, in addition to the modules required for first-stage init (defined by MODULES_LIST). Defining MODULES_[RECOVERY/CHARGER]_LIST also produces a file named modules.load.[recovery/charger] in the initramfs, which init uses instead of modules.load when booting into recovery or charger mode respectively to load the first-stage init and recovery and charger modules. In case of normal boot, init will use the modules.load file, which will only contain the modules needed for first stage-init, and the recovery and charger modules will be loaded from from the vendor_dlkm partition during second-stage init. Bug: 266752750 Change-Id: I11503b11683ef64cf0933b8641959ea5acd63ab1 Signed-off-by: Isaac J. Manjarres <isaacmanjarres@google.com>
-rwxr-xr-xbuild.sh23
-rw-r--r--build_utils.sh98
-rw-r--r--kleaf/impl/image/initramfs.bzl3
3 files changed, 113 insertions, 11 deletions
diff --git a/build.sh b/build.sh
index 5b7b2e4..f67cbc8 100755
--- a/build.sh
+++ b/build.sh
@@ -230,6 +230,14 @@
# - MODULES_LIST=<file to list of modules> list of modules to use for
# vendor_boot.modules.load. If this property is not set, then the default
# modules.load is used.
+# - MODULES_RECOVERY_LIST=<file to list of modules> list of modules to load
+# in addition to the modules defined in MODULES_LIST during first stage
+# init when booting into recovery. If MODULES_LIST is not specified, this
+# variable is ignored. Defining this variable is optional.
+# - MODULES_CHARGER_LIST=<file to list of modules> list of modules to load
+# in addition to the modules defined in MODULES_LIST during first stage
+# init when booting into charger mode. If MODULES_LIST is not specified, this
+# variable is ignored. Defining this variable is optional.
# - TRIM_UNUSED_MODULES. If set, then modules not mentioned in
# modules.load are removed from initramfs. If MODULES_LIST is unset, then
# having this variable set effectively becomes a no-op.
@@ -1013,15 +1021,20 @@ if [ -n "${MODULES}" ]; then
echo " Creating initramfs"
rm -rf ${INITRAMFS_STAGING_DIR}
create_modules_staging "${MODULES_LIST}" ${MODULES_STAGING_DIR} \
- ${INITRAMFS_STAGING_DIR} "${MODULES_BLOCKLIST}" "-e"
-
+ ${INITRAMFS_STAGING_DIR} "${MODULES_BLOCKLIST}" "${MODULES_RECOVERY_LIST:-""}" \
+ "${MODULES_CHARGER_LIST:-""}" "-e"
MODULES_ROOT_DIR=$(echo ${INITRAMFS_STAGING_DIR}/lib/modules/*)
- cp ${MODULES_ROOT_DIR}/modules.load ${DIST_DIR}/modules.load
if [ -n "${BUILD_VENDOR_BOOT_IMG}" ]; then
- cp ${MODULES_ROOT_DIR}/modules.load ${DIST_DIR}/vendor_boot.modules.load
+ VENDOR_BOOT_NAME="vendor_boot"
elif [ -n "${BUILD_VENDOR_KERNEL_BOOT}" ]; then
- cp ${MODULES_ROOT_DIR}/modules.load ${DIST_DIR}/vendor_kernel_boot.modules.load
+ VENDOR_BOOT_NAME="vendor_kernel_boot"
fi
+ MODULES_LOAD_FILES=( "modules.load" "modules.load.recovery" "modules.load.charger" )
+ for file in "${MODULES_LOAD_FILES[@]}"; do
+ [ -f ${MODULES_ROOT_DIR}/${file} ] && cp ${MODULES_ROOT_DIR}/${file} ${DIST_DIR}/${file}
+ [ -f ${MODULES_ROOT_DIR}/${file} -a -n "${VENDOR_BOOT_NAME}" ] && \
+ cp ${MODULES_ROOT_DIR}/${file} ${DIST_DIR}/${VENDOR_BOOT_NAME}.${file}
+ done
echo "${MODULES_OPTIONS}" > ${MODULES_ROOT_DIR}/modules.options
mkbootfs "${INITRAMFS_STAGING_DIR}" >"${MODULES_STAGING_DIR}/initramfs.cpio"
diff --git a/build_utils.sh b/build_utils.sh
index aac2cb7..f8d2aa0 100644
--- a/build_utils.sh
+++ b/build_utils.sh
@@ -53,14 +53,27 @@ function rel_path2() {
# $1 directory of kernel modules ($1/lib/modules/x.y)
# $2 flags to pass to depmod
# $3 kernel version
+# $4 Optional: File with list of modules to run depmod on.
+# If left empty, depmod will run on all modules
+# under $1/lib/modules/x.y
function run_depmod() {
(
local ramdisk_dir=$1
local depmod_stdout
local depmod_stderr=$(mktemp)
+ local version=$3
+ local modules_list_file=$4
+ local modules_list=""
+
+ if [[ -n "${modules_list_file}" ]]; then
+ while read -r line; do
+ # depmod expects absolute paths for module files
+ modules_list+="${ramdisk_dir}/lib/modules/${version}/${line} "
+ done <${modules_list_file}
+ fi
cd ${ramdisk_dir}
- if ! depmod_stdout="$(depmod $2 -F ${DIST_DIR}/System.map -b . $3 \
+ if ! depmod_stdout="$(depmod $2 -F ${DIST_DIR}/System.map -b . ${version} ${modules_list} \
2>${depmod_stderr})"; then
echo "$depmod_stdout"
cat ${depmod_stderr} >&2
@@ -78,12 +91,60 @@ function run_depmod() {
)
}
+# $1 MODULES_LIST <File that contains the list of modules that need to be
+# loaded during first-stage init.>
+# $2 ADDITIONAL_MODULES_LIST <File that contains the list of additional modules
+# that need to be loaded from the initramfs.>
+# $3 MODULES_ORDER <File that contains the list of all modules in the order in which
+# they appear in Makefiles.>
+# $4 MODULES_OUTPUT_FILENAME <The name of the output modules.order file>
+function create_additional_modules_order() {
+ local modules_list_file=$1
+ local additional_modules_file=$2
+ local modules_order_file=$3
+ local modules_out_filename=$4
+ local tmp_all_modules_list_file
+
+ if [[ -f "${ROOT_DIR}/${additional_modules_file}" ]]; then
+ additional_modules_file="${ROOT_DIR}/${additional_modules_file}"
+ elif [[ "${additional_modules_file}" != /* ]]; then
+ echo "modules recovery list must be an absolute path or relative to ${ROOT_DIR}: ${additional_modules_file}" >&2
+ exit 1
+ elif [[ ! -f "${additional_modules_file}" ]]; then
+ echo "Failed to find modules list: ${additional_modules_file}" >&2
+ exit 1
+ fi
+
+ tmp_all_modules_list_file=$(mktemp)
+
+ cp ${modules_list_file} ${tmp_all_modules_list_file}
+ # Append to the first stage init modules
+ grep -v "^\#" ${additional_modules_file} >> ${tmp_all_modules_list_file}
+ grep -w -f ${tmp_all_modules_list_file} ${modules_order_file} > ${modules_out_filename}
+
+ rm ${tmp_all_modules_list_file}
+}
+
# $1 MODULES_LIST, <File contains the list of modules that should go in the ramdisk>
# $2 MODULES_STAGING_DIR <The directory to look for all the compiled modules>
# $3 IMAGE_STAGING_DIR <The destination directory in which MODULES_LIST is
# expected, and it's corresponding modules.* files>
# $4 MODULES_BLOCKLIST, <File contains the list of modules to prevent from loading>
-# $5 flags to pass to depmod
+# $5 MODULES_RECOVERY_LIST <File contains the list of modules that should go in
+# the ramdisk but should only be loaded when booting
+# into recovery.
+#
+# This parameter is optional, and if not used, should
+# be passed as an empty string to ensure that the depmod
+# flags are assigned correctly.>
+# $6 MODULES_CHARGER_LIST <File contains the list of modules that should go in
+# the ramdisk but should only be loaded when booting
+# into charger mode.
+#
+# This parameter is optional, and if not used, should
+# be passed as an empty string to ensure that the
+# depmod flags are assigned correctly.>
+# $7 flags to pass to depmod
function create_modules_staging() {
local modules_list_file=$1
local src_dir=$(echo $2/lib/modules/*)
@@ -91,7 +152,9 @@ function create_modules_staging() {
local dest_dir=$3/lib/modules/${version}
local dest_stage=$3
local modules_blocklist_file=$4
- local depmod_flags=$5
+ local modules_recoverylist_file=$5
+ local modules_chargerlist_file=$6
+ local depmod_flags=$7
rm -rf ${dest_dir}
mkdir -p ${dest_dir}/kernel
@@ -166,6 +229,14 @@ function create_modules_staging() {
# grep the modules.order for any KOs in the modules list
cp ${dest_dir}/modules.order ${old_modules_list}
! grep -w -f ${modules_list_filter} ${old_modules_list} > ${dest_dir}/modules.order
+ if [[ -n "${modules_recoverylist_file}" ]]; then
+ create_additional_modules_order "${modules_list_filter}" "${modules_recoverylist_file}" \
+ "${old_modules_list}" "${dest_dir}/modules.order.recovery"
+ fi
+ if [[ -n "${modules_chargerlist_file}" ]]; then
+ create_additional_modules_order "${modules_list_filter}" "${modules_chargerlist_file}" \
+ "${old_modules_list}" "${dest_dir}/modules.order.charger"
+ fi
rm -f ${modules_list_filter} ${old_modules_list}
fi
@@ -196,11 +267,27 @@ function create_modules_staging() {
# Trim modules from tree that aren't mentioned in modules.order
(
cd ${dest_dir}
- find * -type f -name "*.ko" | (grep -v -w -f modules.order -f $used_blocklist_modules - || true) | xargs -r rm
+ local grep_flags="-v -w -f modules.order -f ${used_blocklist_modules} "
+ [[ -f modules.order.recovery ]] && grep_flags+="-f modules.order.recovery "
+ [[ -f modules.order.charger ]] && grep_flags+="-f modules.order.charger "
+ find * -type f -name "*.ko" | (grep ${grep_flags} - || true) | xargs -r rm
)
rm $used_blocklist_modules
fi
+ # Run depmod to ensure that dependencies between all modules loaded during
+ # first stage init when booting into any other mode besides normal boot can be
+ # satisfied.
+ if [[ -f ${dest_dir}/modules.order.recovery ]]; then
+ run_depmod ${dest_stage} "${depmod_flags}" "${version}" "${dest_dir}/modules.order.recovery"
+ cp ${dest_dir}/modules.order.recovery ${dest_dir}/modules.load.recovery
+ fi
+
+ if [[ -f ${dest_dir}/modules.order.charger ]]; then
+ run_depmod ${dest_stage} "${depmod_flags}" "${version}" "${dest_dir}/modules.order.charger"
+ cp ${dest_dir}/modules.order.charger ${dest_dir}/modules.load.charger
+ fi
+
# Re-run depmod to detect any dependencies between in-kernel and external
# modules. Then, create modules.order based on all the modules compiled.
run_depmod ${dest_stage} "${depmod_flags}" "${version}"
@@ -213,7 +300,8 @@ function build_system_dlkm() {
rm -rf ${SYSTEM_DLKM_STAGING_DIR}
create_modules_staging "${SYSTEM_DLKM_MODULES_LIST:-${MODULES_LIST}}" "${MODULES_STAGING_DIR}" \
- ${SYSTEM_DLKM_STAGING_DIR} "${SYSTEM_DLKM_MODULES_BLOCKLIST:-${MODULES_BLOCKLIST}}" "-e"
+ ${SYSTEM_DLKM_STAGING_DIR} "${SYSTEM_DLKM_MODULES_BLOCKLIST:-${MODULES_BLOCKLIST}}" \
+ "${MODULES_RECOVERY_LIST:-""}" "${MODULES_CHARGER_LIST:-""}" "-e"
local system_dlkm_root_dir=$(echo ${SYSTEM_DLKM_STAGING_DIR}/lib/modules/*)
cp ${system_dlkm_root_dir}/modules.load ${DIST_DIR}/system_dlkm.modules.load
diff --git a/kleaf/impl/image/initramfs.bzl b/kleaf/impl/image/initramfs.bzl
index 1b7c6d4..a63759c 100644
--- a/kleaf/impl/image/initramfs.bzl
+++ b/kleaf/impl/image/initramfs.bzl
@@ -66,7 +66,8 @@ def _initramfs_impl(ctx):
mkdir -p {initramfs_staging_dir}
# Build initramfs
create_modules_staging "${{MODULES_LIST}}" {modules_staging_dir} \
- {initramfs_staging_dir} "${{MODULES_BLOCKLIST}}" "-e"
+ {initramfs_staging_dir} "${{MODULES_BLOCKLIST}}" \
+ "${{MODULES_RECOVERY_LIST:-""}}" "${{MODULES_CHARGER_LIST:-""}}" "-e"
modules_root_dir=$(readlink -e {initramfs_staging_dir}/lib/modules/*) || exit 1
cp ${{modules_root_dir}}/modules.load {modules_load}
{cp_vendor_boot_modules_load_cmd}