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