User:Trickygnome/Adding driver to kernel

From Gentoo Wiki
Jump to:navigation Jump to:search

To add driver you should add lines to parent Kconfing and Makefile and folder with driver.

Those bash functions may help with that.

FILE functions.sh
backup_or_restore() {
    local file="$1"
    local backup="${file}.back"

    if [ -e "$backup" ]; then
        cp "$backup" "$file"
    else
        cp "$file" "$backup"
    fi
}


restore() {
    local file="$1"
    local backup="${file}.back"
    cp "$backup" "$file"
}


incremental_backup() {
    local file="$1"
    local base_name="${file}.back"
    local i=0

    while [ -f "${base_name}.${i}" ]; do
        ((i++))
    done

    cp "$file" "${base_name}.${i}"
    echo "Backup created as ${base_name}.${i}"
}

insert_lines_to_config() {
    if [ -z "$1" ] ; then echo no \$1; return 1 ; fi
    if [ -z "$2" ] ; then echo no \$2; return 1 ; fi
    if [ -z "$3" ] ; then echo no \$3; return 1 ; fi
    file=$1
    after_what=$2 # '^source' for Kconfig, '^obj-$(CONFIG' for Makefile.
    insert_lines=$3

    # Get the first line of insert_lines
    first_insert_line=$(echo "$insert_lines" | head -n 1)


    # Check if the first line of insert_lines exists in the file
    if grep -q "^$first_insert_line$" "$file"; then
        echo "First line of insert_lines already exists in $file, skipping modification."
        return 0
    fi

    # Find the line number of the last line matching the pattern
    last_match_line=$(grep -n "$after_what" "$file" | tail -n 1 | cut -d ':' -f 1)

    if [ -n "$last_match_line" ]; then
        # Insert lines after this point
        tmp_file=$(mktemp --tmpdir=/tmp)
        head -n $((last_match_line)) "$file" > "$tmp_file"
        echo "$insert_lines" >> "$tmp_file"
        tail -n +$((last_match_line + 1)) "$file" >> "$tmp_file"
        mv "$tmp_file" "$file"
        echo "Successfully inserted lines into $file."
    else
        echo "No lines matching '$after_what' found in $file."
    fi
}

# --- example
parentKconfig="/usr/src/linux/drivers/net/wireless/realtek/Kconfig"
insert_after="^source"
lines_to_insert="source \"drivers/net/wireless/realtek/rtl22211/Kconfig\"
"
incremental_backup "$parentKconfig"
insert_lines_to_config "$parentKconfig" "$insert_after" "$lines_to_insert"