b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 1) #!/bin/sh
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 2) # SPDX-License-Identifier: GPL-2.0-only
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 3) #
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 4) # Generate a syscall number header.
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 5) #
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 6) # Each line of the syscall table should have the following format:
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 7) #
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 8) # NR ABI NAME [NATIVE] [COMPAT]
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 9) #
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 10) # NR syscall number
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 11) # ABI ABI name
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 12) # NAME syscall name
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 13) # NATIVE native entry point (optional)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 14) # COMPAT compat entry point (optional)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 15)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 16) set -e
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 17)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 18) usage() {
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 19) echo >&2 "usage: $0 [--abis ABIS] [--emit-nr] [--offset OFFSET] [--prefix PREFIX] INFILE OUTFILE" >&2
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 20) echo >&2
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 21) echo >&2 " INFILE input syscall table"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 22) echo >&2 " OUTFILE output header file"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 23) echo >&2
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 24) echo >&2 "options:"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 25) echo >&2 " --abis ABIS ABI(s) to handle (By default, all lines are handled)"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 26) echo >&2 " --emit-nr Emit the macro of the number of syscalls (__NR_syscalls)"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 27) echo >&2 " --offset OFFSET The offset of syscall numbers"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 28) echo >&2 " --prefix PREFIX The prefix to the macro like __NR_<PREFIX><NAME>"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 29) exit 1
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 30) }
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 31)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 32) # default unless specified by options
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 33) abis=
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 34) emit_nr=
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 35) offset=
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 36) prefix=
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 37)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 38) while [ $# -gt 0 ]
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 39) do
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 40) case $1 in
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 41) --abis)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 42) abis=$(echo "($2)" | tr ',' '|')
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 43) shift 2;;
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 44) --emit-nr)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 45) emit_nr=1
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 46) shift 1;;
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 47) --offset)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 48) offset=$2
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 49) shift 2;;
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 50) --prefix)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 51) prefix=$2
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 52) shift 2;;
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 53) -*)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 54) echo "$1: unknown option" >&2
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 55) usage;;
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 56) *)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 57) break;;
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 58) esac
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 59) done
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 60)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 61) if [ $# -ne 2 ]; then
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 62) usage
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 63) fi
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 64)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 65) infile="$1"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 66) outfile="$2"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 67)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 68) guard=_UAPI_ASM_$(basename "$outfile" |
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 69) sed -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' \
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 70) -e 's/[^A-Z0-9_]/_/g' -e 's/__/_/g')
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 71)
c6a3a81d19b83 (Masahiro Yamada 2021-07-05 15:06:54 +0900 72) grep -E "^[0-9A-Fa-fXx]+[[:space:]]+$abis" "$infile" | {
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 73) echo "#ifndef $guard"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 74) echo "#define $guard"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 75) echo
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 76)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 77) max=0
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 78) while read nr abi name native compat ; do
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 79)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 80) max=$nr
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 81)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 82) if [ -n "$offset" ]; then
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 83) nr="($offset + $nr)"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 84) fi
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 85)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 86) echo "#define __NR_$prefix$name $nr"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 87) done
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 88)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 89) if [ -n "$emit_nr" ]; then
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 90) echo
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 91) echo "#ifdef __KERNEL__"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 92) echo "#define __NR_${prefix}syscalls $(($max + 1))"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 93) echo "#endif"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 94) fi
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 95)
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 96) echo
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 97) echo "#endif /* $guard */"
b9da928abf45c (Masahiro Yamada 2021-02-16 11:04:12 +0900 98) } > "$outfile"