diff --git a/src/01_get_kernel.sh b/src/01_get_kernel.sh index 4c78d7861..3cc9a4c3e 100755 --- a/src/01_get_kernel.sh +++ b/src/01_get_kernel.sh @@ -2,43 +2,24 @@ set -e +# Load common properties and functions in the current script. +. ./common.sh + echo "*** GET KERNEL BEGIN ***" -SRC_DIR=$(pwd) - -# Grab everything after the '=' character. -DOWNLOAD_URL=$(grep -i ^KERNEL_SOURCE_URL .config | cut -f2 -d'=') +# Read the 'KERNEL_SOURCE_URL' property from '.config'. +DOWNLOAD_URL=`read_property KERNEL_SOURCE_URL` # Grab everything after the last '/' character. ARCHIVE_FILE=${DOWNLOAD_URL##*/} -# Read the 'USE_LOCAL_SOURCE' property from '.config' -USE_LOCAL_SOURCE="$(grep -i ^USE_LOCAL_SOURCE .config | cut -f2 -d'=')" +# Download kernel source archive in the 'source' directory. +download_source $DOWNLOAD_URL $SOURCE_DIR/$ARCHIVE_FILE -if [ "$USE_LOCAL_SOURCE" = "true" -a ! -f $SRC_DIR/source/$ARCHIVE_FILE ] ; then - echo "Source bundle $SRC_DIR/source/$ARCHIVE_FILE is missing and will be downloaded." - USE_LOCAL_SOURCE="false" -fi - -cd source - -if [ ! "$USE_LOCAL_SOURCE" = "true" ] ; then - # Downloading kernel source bundle file. The '-c' option allows the download to resume. - echo "Downloading kernel source bundle from $DOWNLOAD_URL" - wget -c $DOWNLOAD_URL -else - echo "Using local kernel source bundle $SRC_DIR/source/$ARCHIVE_FILE" -fi - -# Delete folder with previously extracted kernel. -echo "Removing kernel work area. This may take a while." -rm -rf ../work/kernel -mkdir ../work/kernel - -# Extract kernel to folder 'work/kernel'. -# Full path will be something like 'work/kernel/linux-4.4.6'. -tar -xvf $ARCHIVE_FILE -C ../work/kernel +# Extract the kernel sources in the 'work/kernel' directory. +extract_source $SOURCE_DIR/$ARCHIVE_FILE kernel +# Just in case we go back to the main MLL source folder. cd $SRC_DIR echo "*** GET KERNEL END ***" diff --git a/src/02_build_kernel.sh b/src/02_build_kernel.sh index 5aa78b917..b1e344210 100755 --- a/src/02_build_kernel.sh +++ b/src/02_build_kernel.sh @@ -2,22 +2,11 @@ set -e +# Load common properties and functions in the current script. +. ./common.sh + echo "*** BUILD KERNEL BEGIN ***" -SRC_DIR=$(pwd) - -# Read the 'JOB_FACTOR' property from '.config' -JOB_FACTOR="$(grep -i ^JOB_FACTOR .config | cut -f2 -d'=')" - -# Read the 'CFLAGS' property from '.config' -CFLAGS="$(grep -i ^CFLAGS .config | cut -f2 -d'=')" - -# Find the number of available CPU cores. -NUM_CORES=$(grep ^processor /proc/cpuinfo | wc -l) - -# Calculate the number of 'make' jobs to be used later. -NUM_JOBS=$((NUM_CORES * JOB_FACTOR)) - cd work/kernel # Prepare the kernel install area. @@ -32,16 +21,16 @@ echo "Preparing kernel work area." make mrproper -j $NUM_JOBS # Read the 'USE_PREDEFINED_KERNEL_CONFIG' property from '.config' -USE_PREDEFINED_KERNEL_CONFIG="$(grep -i ^USE_PREDEFINED_KERNEL_CONFIG $SRC_DIR/.config | cut -f2 -d'=')" +USE_PREDEFINED_KERNEL_CONFIG=`read_property USE_PREDEFINED_KERNEL_CONFIG` if [ "$USE_PREDEFINED_KERNEL_CONFIG" = "true" -a ! -f $SRC_DIR/minimal_config/kernel.config ] ; then - echo "Config file $SRC_DIR/minimal_config/kernel.config does not exist." - USE_PREDEFINED_KERNEL_CONFIG="false" + echo "Config file '$SRC_DIR/minimal_config/kernel.config' does not exist." + USE_PREDEFINED_KERNEL_CONFIG=false fi if [ "$USE_PREDEFINED_KERNEL_CONFIG" = "true" ] ; then # Use predefined configuration file for the kernel. - echo "Using config file $SRC_DIR/minimal_config/kernel.config" + echo "Using config file '$SRC_DIR/minimal_config/kernel.config'." cp -f $SRC_DIR/minimal_config/kernel.config .config else # Create default configuration file for the kernel. @@ -70,7 +59,7 @@ else sed -i "s/.*CONFIG_FB_VESA.*/CONFIG_FB_VESA=y/" .config # Read the 'USE_BOOT_LOGO' property from '.config' - USE_BOOT_LOGO="$(grep -i ^USE_BOOT_LOGO $SRC_DIR/.config | cut -f2 -d'=')" + USE_BOOT_LOGO=`read_property USE_BOOT_LOGO` if [ "$USE_BOOT_LOGO" = "true" ] ; then sed -i "s/.*CONFIG_LOGO_LINUX_CLUT224.*/CONFIG_LOGO_LINUX_CLUT224=y/" .config @@ -86,7 +75,7 @@ else # Enable the EFI stub sed -i "s/.*CONFIG_EFI_STUB.*/CONFIG_EFI_STUB=y/" .config - # Request that the firmware clear the contents of RAM after a reboot (4.14+). + # Request that the firmware clear the contents of RAM after reboot (4.14+). echo "CONFIG_RESET_ATTACK_MITIGATION=y" >> .config # Disable Apple Properties (Useful for Macs but useless in general) diff --git a/src/common.sh b/src/common.sh new file mode 100755 index 000000000..f5c59d065 --- /dev/null +++ b/src/common.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +set -e + +SRC_DIR=`realpath --no-symlinks $PWD` +SOURCE_DIR=$SRC_DIR/source +WORK_DIR=$SRC_DIR/work +CONFIG=$SRC_DIR/.config + +# This function reads property from the main '.config' file. +# +# Using () instead of {} for the function body is a POSIX +# compliant way to execute subshell and as consequence all +# variables in the function will become effectively in local +# scope. Note that the 'local' keyword is supported by most +# shells but it is not POSIX compliant. +read_property() ( + # The property we are looking for. + prop_name=$1 + + # The value of the property set initially to empty string. + prop_value= + + if [ ! "$prop_name" = "" ] ; then + # Search in the main '.config' file. + prop_value=`grep -i ^${prop_name}= $CONFIG | cut -f2- -d'=' | xargs` + fi + + echo $prop_value +) + +# Read commonly used properties from the main '.config' file. +JOB_FACTOR=`read_property JOB_FACTOR` +CFLAGS=`read_property CFLAGS` +NUM_CORES=$(grep ^processor /proc/cpuinfo | wc -l) + +# Calculate the number of 'make' jobs to be used later. +NUM_JOBS=$((NUM_CORES * JOB_FACTOR)) + +download_source() ( + url=$1 # Download from this URL. + file=$2 # Save the resource in this file. + + local=`read_property USE_LOCAL_SOURCE` + + if [ "$local" = "true" -a ! -f $file ] ; then + echo "Source file '$file' is missing and will be downloaded." + local=false + fi + + if [ ! "$local" = "true" ] ; then + echo "Downloading source file from '$url'." + echo "Saving source file in '$file'". + wget -O $file -c $url + else + echo "Using local source file '$file'." + fi +) + +extract_source() ( + file=$1 + name=$2 + + # Delete folder with previously extracted source. + echo "Removing '$name' work area. This may take a while." + rm -rf $WORK_DIR/$name + mkdir $WORK_DIR/$name + + # Extract source to folder 'work/$source'. + tar -xvf $file -C $WORK_DIR/$name +)