1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0-only 3# 4# Copyright 2022 Google LLC 5# Author: ramjiyani@google.com (Ramji Jiyani) 6# 7 8# 9# Generates hearder file with list of unprotected symbols 10# 11# Called By: KERNEL_SRC/kernel/Makefile if CONFIG_MODULE_SIG_PROTECT=y 12# 13# gki_module_unprotected.h: Symbols allowed to _access_ by unsigned modules 14# 15# If valid symbol file doesn't exists then still generates valid C header files for 16# compilation to proceed with no symbols to protect 17# 18 19# Collect arguments from Makefile 20TARGET=$1 21SRCTREE=$2 22SYMBOL_LIST=$3 23 24set -e 25 26# 27# Common Definitions 28# 29# Use "make V=1" to debug this script. 30case "$KBUILD_VERBOSE" in 31*1*) 32 set -x 33 ;; 34esac 35 36# 37# generate_header(): 38# Args: $1 = Name of the header file 39# $2 = Input symbol list 40# $3 = Symbol type (protected/exported) 41# 42generate_header() { 43 local header_file=$1 44 local symbol_file=$2 45 local symbol_type=$3 46 47 if [ -f "${header_file}" ]; then 48 rm -f -- "${header_file}" 49 fi 50 51 # If symbol_file exist preprocess it and find maximum name length 52 if [ -s "${symbol_file}" ]; then 53 # Remove any trailing CR, leading / trailing whitespace, 54 # line comments, empty lines and symbol list markers. 55 sed -i ' 56 s/\r$// 57 s/^[[:space:]]*// 58 s/[[:space:]]*$// 59 /^#/d 60 /^$/d 61 /^\[abi_symbol_list\]$/d 62 ' "${symbol_file}" 63 64 # Sort in byte order for kernel binary search at runtime 65 LC_ALL=C sort -u -o "${symbol_file}" "${symbol_file}" 66 67 # Trim white spaces & +1 for null termination 68 local max_name_len=$(awk ' 69 { 70 $1=$1; 71 if ( length > L ) { 72 L=length 73 } 74 } END { print ++L }' "${symbol_file}") 75 else 76 # Set to 1 to generate valid C header file 77 local max_name_len=1 78 fi 79 80 # Header generation 81 cat > "${header_file}" <<- EOT 82 /* 83 * DO NOT EDIT 84 * 85 * Build generated header file with ${symbol_type} 86 */ 87 88 #define NR_$(printf ${symbol_type} | tr [:lower:] [:upper:])_SYMBOLS \\ 89 $(printf '\t')(ARRAY_SIZE(gki_${symbol_type}_symbols)) 90 #define MAX_$(printf ${symbol_type} | tr [:lower:] [:upper:])_NAME_LEN (${max_name_len}) 91 92 static const char gki_${symbol_type}_symbols[][MAX_$(printf ${symbol_type} | 93 tr [:lower:] [:upper:])_NAME_LEN] = { 94 EOT 95 96 # If a valid symbol_file present add symbols in an array except the 1st line 97 if [ -s "${symbol_file}" ]; then 98 sed -e 's/^[ \t]*/\t"/;s/[ \t]*$/",/' "${symbol_file}" >> "${header_file}" 99 fi 100 101 # Terminate the file 102 echo "};" >> "${header_file}" 103} 104 105if [ "$(basename "${TARGET}")" = "gki_module_unprotected.h" ]; then 106 # Union of vendor symbol lists 107 GKI_VENDOR_SYMBOLS="${SYMBOL_LIST}" 108 generate_header "${TARGET}" "${GKI_VENDOR_SYMBOLS}" "unprotected" 109else 110 # Sorted list of exported symbols 111 GKI_EXPORTED_SYMBOLS="include/config/abi_gki_protected_exports" 112 113 if [ -z "${SYMBOL_LIST}" ]; then 114 # Create empty list if ARCH doesn't have protected exports 115 touch "${GKI_EXPORTED_SYMBOLS}" 116 else 117 # Make a temp copy to avoid changing source during pre-processing 118 cp -f "${SYMBOL_LIST}" "${GKI_EXPORTED_SYMBOLS}" 119 fi 120 121 generate_header "${TARGET}" "${GKI_EXPORTED_SYMBOLS}" "protected_exports" 122fi 123 124