^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #!/bin/sh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) # SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) if [ $# -ne 2 ]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) echo "Usage: headers_install.sh INFILE OUTFILE"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) echo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) echo "Prepares kernel header files for use by user space, by removing"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) echo "all compiler.h definitions and #includes, removing any"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) echo "#ifdef __KERNEL__ sections, and putting __underscores__ around"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) echo "asm/inline/volatile keywords."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) echo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) echo "INFILE: header file to operate on"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) echo "OUTFILE: output file which the processed header is written to"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) # Grab arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) INFILE=$1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) OUTFILE=$2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) TMPFILE=$OUTFILE.tmp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) trap 'rm -f $OUTFILE $TMPFILE' EXIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) # SPDX-License-Identifier with GPL variants must have "WITH Linux-syscall-note"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if [ -n "$(sed -n -e "/SPDX-License-Identifier:.*GPL-/{/WITH Linux-syscall-note/!p}" $INFILE)" ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) echo "error: $INFILE: missing \"WITH Linux-syscall-note\" for SPDX-License-Identifier" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) sed -E -e '
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) s/([[:space:](])(__user|__force|__iomem)[[:space:]]/\1/g
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) s/__attribute_const__([[:space:]]|$)/\1/g
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) s@^#include <linux/compiler(|_types).h>@@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) s/(^|[^a-zA-Z0-9])__packed([^a-zA-Z0-9_]|$)/\1__attribute__((packed))\2/g
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) s/(^|[[:space:](])(inline|asm|volatile)([[:space:](]|$)/\1__\2__\3/g
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) s@#(ifndef|define|endif[[:space:]]*/[*])[[:space:]]*_UAPI@#\1 @
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) ' $INFILE > $TMPFILE || exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) scripts/unifdef -U__KERNEL__ -D__EXPORTED_HEADERS__ $TMPFILE > $OUTFILE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) [ $? -gt 1 ] && exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) # Remove /* ... */ style comments, and find CONFIG_ references in code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) configs=$(sed -e '
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) :comment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) s:/\*[^*][^*]*:/*:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) s:/\*\*\**\([^/]\):/*\1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) t comment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) s:/\*\*/: :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) t comment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /\/\*/! b check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) N
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) b comment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) :print
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) P
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) :check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) s:^\(CONFIG_[[:alnum:]_]*\):\1\n:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) t print
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) s:^[[:alnum:]_][[:alnum:]_]*::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) s:^[^[:alnum:]_][^[:alnum:]_]*::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) t check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) ' $OUTFILE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) # The entries in the following list do not result in an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) # Please do not add a new entry. This list is only for existing ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) # The list will be reduced gradually, and deleted eventually. (hopefully)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) # The format is <file-name>:<CONFIG-option> in each line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) config_leak_ignores="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) include/uapi/linux/iio/types.h:CONFIG_NO_GKI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) arch/alpha/include/uapi/asm/setup.h:CONFIG_ALPHA_LEGACY_START_ADDRESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) arch/arc/include/uapi/asm/page.h:CONFIG_ARC_PAGE_SIZE_16K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) arch/arc/include/uapi/asm/page.h:CONFIG_ARC_PAGE_SIZE_4K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) arch/arc/include/uapi/asm/swab.h:CONFIG_ARC_HAS_SWAPE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) arch/arm/include/uapi/asm/ptrace.h:CONFIG_CPU_ENDIAN_BE8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) arch/hexagon/include/uapi/asm/ptrace.h:CONFIG_HEXAGON_ARCH_VERSION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) arch/hexagon/include/uapi/asm/user.h:CONFIG_HEXAGON_ARCH_VERSION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) arch/ia64/include/uapi/asm/cmpxchg.h:CONFIG_IA64_DEBUG_CMPXCHG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) arch/m68k/include/uapi/asm/ptrace.h:CONFIG_COLDFIRE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) arch/nios2/include/uapi/asm/swab.h:CONFIG_NIOS2_CI_SWAB_NO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) arch/nios2/include/uapi/asm/swab.h:CONFIG_NIOS2_CI_SWAB_SUPPORT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) arch/x86/include/uapi/asm/auxvec.h:CONFIG_IA32_EMULATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) arch/x86/include/uapi/asm/auxvec.h:CONFIG_X86_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) arch/x86/include/uapi/asm/mman.h:CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) include/uapi/asm-generic/fcntl.h:CONFIG_64BIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) include/uapi/linux/atmdev.h:CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) include/uapi/linux/eventpoll.h:CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) include/uapi/linux/hw_breakpoint.h:CONFIG_HAVE_MIXED_BREAKPOINTS_REGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) include/uapi/linux/pktcdvd.h:CONFIG_CDROM_PKTCDVD_WCACHE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) for c in $configs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) leak_error=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) for ignore in $config_leak_ignores
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if echo "$INFILE:$c" | grep -q "$ignore$"; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) leak_error=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if [ "$leak_error" = 1 ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) echo "error: $INFILE: leak $c to user-space" >&2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) exit 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) rm -f $TMPFILE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) trap - EXIT