18c2ecf20Sopenharmony_ci#!/bin/sh
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ciif [ $# -ne 2 ]
58c2ecf20Sopenharmony_cithen
68c2ecf20Sopenharmony_ci	echo "Usage: headers_install.sh INFILE OUTFILE"
78c2ecf20Sopenharmony_ci	echo
88c2ecf20Sopenharmony_ci	echo "Prepares kernel header files for use by user space, by removing"
98c2ecf20Sopenharmony_ci	echo "all compiler.h definitions and #includes, removing any"
108c2ecf20Sopenharmony_ci	echo "#ifdef __KERNEL__ sections, and putting __underscores__ around"
118c2ecf20Sopenharmony_ci	echo "asm/inline/volatile keywords."
128c2ecf20Sopenharmony_ci	echo
138c2ecf20Sopenharmony_ci	echo "INFILE: header file to operate on"
148c2ecf20Sopenharmony_ci	echo "OUTFILE: output file which the processed header is written to"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	exit 1
178c2ecf20Sopenharmony_cifi
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci# Grab arguments
208c2ecf20Sopenharmony_ciINFILE=$1
218c2ecf20Sopenharmony_ciOUTFILE=$2
228c2ecf20Sopenharmony_ciTMPFILE=$OUTFILE.tmp
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_citrap 'rm -f $OUTFILE $TMPFILE' EXIT
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci# SPDX-License-Identifier with GPL variants must have "WITH Linux-syscall-note"
278c2ecf20Sopenharmony_ciif [ -n "$(sed -n -e "/SPDX-License-Identifier:.*GPL-/{/WITH Linux-syscall-note/!p}" $INFILE)" ]; then
288c2ecf20Sopenharmony_ci	echo "error: $INFILE: missing \"WITH Linux-syscall-note\" for SPDX-License-Identifier" >&2
298c2ecf20Sopenharmony_ci	exit 1
308c2ecf20Sopenharmony_cifi
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cised -E -e '
338c2ecf20Sopenharmony_ci	s/([[:space:](])(__user|__force|__iomem)[[:space:]]/\1/g
348c2ecf20Sopenharmony_ci	s/__attribute_const__([[:space:]]|$)/\1/g
358c2ecf20Sopenharmony_ci	s@^#include <linux/compiler(|_types).h>@@
368c2ecf20Sopenharmony_ci	s/(^|[^a-zA-Z0-9])__packed([^a-zA-Z0-9_]|$)/\1__attribute__((packed))\2/g
378c2ecf20Sopenharmony_ci	s/(^|[[:space:](])(inline|asm|volatile)([[:space:](]|$)/\1__\2__\3/g
388c2ecf20Sopenharmony_ci	s@#(ifndef|define|endif[[:space:]]*/[*])[[:space:]]*_UAPI@#\1 @
398c2ecf20Sopenharmony_ci' $INFILE > $TMPFILE || exit 1
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciscripts/unifdef -U__KERNEL__ -D__EXPORTED_HEADERS__ $TMPFILE > $OUTFILE
428c2ecf20Sopenharmony_ci[ $? -gt 1 ] && exit 1
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci# Remove /* ... */ style comments, and find CONFIG_ references in code
458c2ecf20Sopenharmony_ciconfigs=$(sed -e '
468c2ecf20Sopenharmony_ci:comment
478c2ecf20Sopenharmony_ci	s:/\*[^*][^*]*:/*:
488c2ecf20Sopenharmony_ci	s:/\*\*\**\([^/]\):/*\1:
498c2ecf20Sopenharmony_ci	t comment
508c2ecf20Sopenharmony_ci	s:/\*\*/: :
518c2ecf20Sopenharmony_ci	t comment
528c2ecf20Sopenharmony_ci	/\/\*/! b check
538c2ecf20Sopenharmony_ci	N
548c2ecf20Sopenharmony_ci	b comment
558c2ecf20Sopenharmony_ci:print
568c2ecf20Sopenharmony_ci	P
578c2ecf20Sopenharmony_ci	D
588c2ecf20Sopenharmony_ci:check
598c2ecf20Sopenharmony_ci	s:^\(CONFIG_[[:alnum:]_]*\):\1\n:
608c2ecf20Sopenharmony_ci	t print
618c2ecf20Sopenharmony_ci	s:^[[:alnum:]_][[:alnum:]_]*::
628c2ecf20Sopenharmony_ci	s:^[^[:alnum:]_][^[:alnum:]_]*::
638c2ecf20Sopenharmony_ci	t check
648c2ecf20Sopenharmony_ci	d
658c2ecf20Sopenharmony_ci' $OUTFILE)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci# The entries in the following list do not result in an error.
688c2ecf20Sopenharmony_ci# Please do not add a new entry. This list is only for existing ones.
698c2ecf20Sopenharmony_ci# The list will be reduced gradually, and deleted eventually. (hopefully)
708c2ecf20Sopenharmony_ci#
718c2ecf20Sopenharmony_ci# The format is <file-name>:<CONFIG-option> in each line.
728c2ecf20Sopenharmony_ciconfig_leak_ignores="
738c2ecf20Sopenharmony_ciarch/alpha/include/uapi/asm/setup.h:CONFIG_ALPHA_LEGACY_START_ADDRESS
748c2ecf20Sopenharmony_ciarch/arc/include/uapi/asm/page.h:CONFIG_ARC_PAGE_SIZE_16K
758c2ecf20Sopenharmony_ciarch/arc/include/uapi/asm/page.h:CONFIG_ARC_PAGE_SIZE_4K
768c2ecf20Sopenharmony_ciarch/arc/include/uapi/asm/swab.h:CONFIG_ARC_HAS_SWAPE
778c2ecf20Sopenharmony_ciarch/arm/include/uapi/asm/ptrace.h:CONFIG_CPU_ENDIAN_BE8
788c2ecf20Sopenharmony_ciarch/hexagon/include/uapi/asm/ptrace.h:CONFIG_HEXAGON_ARCH_VERSION
798c2ecf20Sopenharmony_ciarch/hexagon/include/uapi/asm/user.h:CONFIG_HEXAGON_ARCH_VERSION
808c2ecf20Sopenharmony_ciarch/ia64/include/uapi/asm/cmpxchg.h:CONFIG_IA64_DEBUG_CMPXCHG
818c2ecf20Sopenharmony_ciarch/m68k/include/uapi/asm/ptrace.h:CONFIG_COLDFIRE
828c2ecf20Sopenharmony_ciarch/nios2/include/uapi/asm/swab.h:CONFIG_NIOS2_CI_SWAB_NO
838c2ecf20Sopenharmony_ciarch/nios2/include/uapi/asm/swab.h:CONFIG_NIOS2_CI_SWAB_SUPPORT
848c2ecf20Sopenharmony_ciarch/x86/include/uapi/asm/auxvec.h:CONFIG_IA32_EMULATION
858c2ecf20Sopenharmony_ciarch/x86/include/uapi/asm/auxvec.h:CONFIG_X86_64
868c2ecf20Sopenharmony_ciarch/x86/include/uapi/asm/mman.h:CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
878c2ecf20Sopenharmony_ciinclude/uapi/asm-generic/fcntl.h:CONFIG_64BIT
888c2ecf20Sopenharmony_ciinclude/uapi/linux/atmdev.h:CONFIG_COMPAT
898c2ecf20Sopenharmony_ciinclude/uapi/linux/eventpoll.h:CONFIG_PM_SLEEP
908c2ecf20Sopenharmony_ciinclude/uapi/linux/hw_breakpoint.h:CONFIG_HAVE_MIXED_BREAKPOINTS_REGS
918c2ecf20Sopenharmony_ciinclude/uapi/linux/pktcdvd.h:CONFIG_CDROM_PKTCDVD_WCACHE
928c2ecf20Sopenharmony_ci"
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cifor c in $configs
958c2ecf20Sopenharmony_cido
968c2ecf20Sopenharmony_ci	leak_error=1
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	for ignore in $config_leak_ignores
998c2ecf20Sopenharmony_ci	do
1008c2ecf20Sopenharmony_ci		if echo "$INFILE:$c" | grep -q "$ignore$"; then
1018c2ecf20Sopenharmony_ci			leak_error=
1028c2ecf20Sopenharmony_ci			break
1038c2ecf20Sopenharmony_ci		fi
1048c2ecf20Sopenharmony_ci	done
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	if [ "$leak_error" = 1 ]; then
1078c2ecf20Sopenharmony_ci		echo "error: $INFILE: leak $c to user-space" >&2
1088c2ecf20Sopenharmony_ci		exit 1
1098c2ecf20Sopenharmony_ci	fi
1108c2ecf20Sopenharmony_cidone
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cirm -f $TMPFILE
1138c2ecf20Sopenharmony_citrap - EXIT
114