18c2ecf20Sopenharmony_ci#!/bin/bash
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only
38c2ecf20Sopenharmony_ci# Translate the bits making up a GFP mask
48c2ecf20Sopenharmony_ci# (c) 2009, Mel Gorman <mel@csn.ul.ie>
58c2ecf20Sopenharmony_ciSOURCE=
68c2ecf20Sopenharmony_ciGFPMASK=none
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci# Helper function to report failures and exit
98c2ecf20Sopenharmony_cidie() {
108c2ecf20Sopenharmony_ci	echo ERROR: $@
118c2ecf20Sopenharmony_ci	if [ "$TMPFILE" != "" ]; then
128c2ecf20Sopenharmony_ci		rm -f $TMPFILE
138c2ecf20Sopenharmony_ci	fi
148c2ecf20Sopenharmony_ci	exit -1
158c2ecf20Sopenharmony_ci}
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ciusage() {
188c2ecf20Sopenharmony_ci	echo "usage: gfp-translate [-h] [ --source DIRECTORY ] gfpmask"
198c2ecf20Sopenharmony_ci	exit 0
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci# Parse command-line arguments
238c2ecf20Sopenharmony_ciwhile [ $# -gt 0 ]; do
248c2ecf20Sopenharmony_ci	case $1 in
258c2ecf20Sopenharmony_ci		--source)
268c2ecf20Sopenharmony_ci			SOURCE=$2
278c2ecf20Sopenharmony_ci			shift 2
288c2ecf20Sopenharmony_ci			;;
298c2ecf20Sopenharmony_ci		-h)
308c2ecf20Sopenharmony_ci			usage
318c2ecf20Sopenharmony_ci			;;
328c2ecf20Sopenharmony_ci		--help)
338c2ecf20Sopenharmony_ci			usage
348c2ecf20Sopenharmony_ci			;;
358c2ecf20Sopenharmony_ci		*)
368c2ecf20Sopenharmony_ci			GFPMASK=$1
378c2ecf20Sopenharmony_ci			shift
388c2ecf20Sopenharmony_ci			;;
398c2ecf20Sopenharmony_ci	esac
408c2ecf20Sopenharmony_cidone
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci# Guess the kernel source directory if it's not set. Preference is in order of
438c2ecf20Sopenharmony_ci# o current directory
448c2ecf20Sopenharmony_ci# o /usr/src/linux
458c2ecf20Sopenharmony_ciif [ "$SOURCE" = "" ]; then
468c2ecf20Sopenharmony_ci	if [ -r "/usr/src/linux/Makefile" ]; then
478c2ecf20Sopenharmony_ci		SOURCE=/usr/src/linux
488c2ecf20Sopenharmony_ci	fi
498c2ecf20Sopenharmony_ci	if [ -r "`pwd`/Makefile" ]; then
508c2ecf20Sopenharmony_ci		SOURCE=`pwd`
518c2ecf20Sopenharmony_ci	fi
528c2ecf20Sopenharmony_cifi
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci# Confirm that a source directory exists
558c2ecf20Sopenharmony_ciif [ ! -r "$SOURCE/Makefile" ]; then
568c2ecf20Sopenharmony_ci	die "Could not locate kernel source directory or it is invalid"
578c2ecf20Sopenharmony_cifi
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci# Confirm that a GFP mask has been specified
608c2ecf20Sopenharmony_ciif [ "$GFPMASK" = "none" ]; then
618c2ecf20Sopenharmony_ci	usage
628c2ecf20Sopenharmony_cifi
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci# Extract GFP flags from the kernel source
658c2ecf20Sopenharmony_ciTMPFILE=`mktemp -t gfptranslate-XXXXXX` || exit 1
668c2ecf20Sopenharmony_cigrep -q ___GFP $SOURCE/include/linux/gfp.h
678c2ecf20Sopenharmony_ciif [ $? -eq 0 ]; then
688c2ecf20Sopenharmony_ci	grep "^#define ___GFP" $SOURCE/include/linux/gfp.h | sed -e 's/u$//' | grep -v GFP_BITS > $TMPFILE
698c2ecf20Sopenharmony_cielse
708c2ecf20Sopenharmony_ci	grep "^#define __GFP" $SOURCE/include/linux/gfp.h | sed -e 's/(__force gfp_t)//' | sed -e 's/u)/)/' | grep -v GFP_BITS | sed -e 's/)\//) \//' > $TMPFILE
718c2ecf20Sopenharmony_cifi
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci# Parse the flags
748c2ecf20Sopenharmony_ciIFS="
758c2ecf20Sopenharmony_ci"
768c2ecf20Sopenharmony_ciecho Source: $SOURCE
778c2ecf20Sopenharmony_ciecho Parsing: $GFPMASK
788c2ecf20Sopenharmony_cifor LINE in `cat $TMPFILE`; do
798c2ecf20Sopenharmony_ci	MASK=`echo $LINE | awk '{print $3}'`
808c2ecf20Sopenharmony_ci	if [ $(($GFPMASK&$MASK)) -ne 0 ]; then
818c2ecf20Sopenharmony_ci		echo $LINE
828c2ecf20Sopenharmony_ci	fi
838c2ecf20Sopenharmony_cidone
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cirm -f $TMPFILE
868c2ecf20Sopenharmony_ciexit 0
87