2 # Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # 3. The name of the author may not be used to endorse or promote products
14 # derived from this software without specific prior written permission.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # This script processes header files containing ioctl command definitions in
30 # symbolic form, assuming that these definitions match the following regular
33 r_define='^[[:space:]]*#[[:space:]]*define[[:space:]]\+'
34 r_cmd_name='[A-Z][A-Z0-9_]*'
35 r_io='\([A-Z]\+\)\?_S\?\(IO\|IOW\|IOR\|IOWR\|IOC\)'
36 r_value='[[:space:]]\+'"$r_io"'[[:space:]]*([^)]'
37 regexp="${r_define}${r_cmd_name}${r_value}"
43 printf >&2 '%s\n' "$me: $*"
48 1) inc_dir="$1"; shift
50 2) inc_dir="$1"; shift
53 *) echo >&2 "usage: $me include-directory [prefix]"
59 prefix="${prefix%%/}/"
70 trap 'cleanup $?' EXIT
71 trap 'cleanup 1' HUP PIPE INT QUIT TERM
72 tmpdir="$(mktemp -dt "$me.XXXXXX")"
74 # list interesting files in $inc_dir.
77 find . -type f -name '*.h' -print0 |
78 xargs -r0 grep -l "$r_value" -- > "$tmpdir"/headers1.list ||
81 sed 's|^\./\(uapi/\)\?||' < "$tmpdir"/headers1.list > "$tmpdir"/headers.list
82 LC_COLLATE=C sort -u -o "$tmpdir"/headers.list "$tmpdir"/headers.list
84 msg "processing $(wc -l < "$tmpdir"/headers.list) header files from $inc_dir"
89 CPPFLAGS="${CPPFLAGS-} -D__EXPORTED_HEADERS__"
90 CFLAGS="${CFLAGS:--Wall -O2} -D__EXPORTED_HEADERS__"
92 INCLUDES="-I$inc_dir/uapi -I$inc_dir ${INCLUDES-}"
94 $CC $INCLUDES $CFLAGS -c -o "$tmpdir"/print_ioctlent.o "${0%/*}"/print_ioctlent.c
96 # Hook onto <asm-generic/ioctl.h> and <asm/ioctl.h>
97 for d in asm-generic asm; do
99 cat > "$tmpdir/$d"/ioctl.h <<__EOF__
100 #include_next <$d/ioctl.h>
102 #define _IOC(dir,type,nr,size) dir, type, nr, size
106 INCLUDES="-I$tmpdir $INCLUDES"
112 # Common code for every processed file.
113 cat > "$tmpdir"/printents.c <<__EOF__
114 #include <asm/termbits.h>
115 #include <asm/ioctl.h>
116 #include <linux/types.h>
117 #include <linux/limits.h>
118 #include <linux/major.h>
120 #include <sys/types.h>
121 #include <sys/socket.h>
126 # define NULL ((void*)0)
135 # define __noreturn __attribute__((noreturn))
138 # define __packed __attribute__((packed))
141 typedef signed char s8;
142 typedef unsigned char u8;
143 typedef signed short s16;
144 typedef unsigned short u16;
145 typedef signed int s32;
146 typedef unsigned int u32;
147 typedef signed long long s64;
148 typedef unsigned long long u64;
152 #include <asm/bitsperlong.h>
153 #ifndef BITS_PER_LONG
154 # define BITS_PER_LONG __BITS_PER_LONG
159 void print_ioctlent(const char *, const char *, unsigned short, unsigned short, unsigned short, unsigned short);
170 # Soft pre-include workarounds for some processed files. Fragile.
173 echo '#include <asm/dasd.h>'
177 #include <asm/termios.h>
178 #include <linux/serial.h>
182 echo '#include <drm/drm.h>'
185 echo '#include <drm/drm.h>' > "$tmpdir/drm.h"
189 #include <linux/fb.h>
196 #include <linux/atm.h>
197 #ifndef _LINUX_TIME_H
198 # define _LINUX_TIME_H
200 #ifndef _UAPI_LINUX_TIME_H
201 # define _UAPI_LINUX_TIME_H
206 echo '#include <linux/atm.h>'
209 echo 'typedef u32 compat_ulong_t;'
211 *linux/coda.h|*android_alarm.h)
213 #ifndef _LINUX_TIME_H
214 # define _LINUX_TIME_H
216 #ifndef _UAPI_LINUX_TIME_H
217 # define _UAPI_LINUX_TIME_H
221 *linux/fs.h|*linux/ncp_fs.h)
223 #include <linux/blktrace_api.h>
224 #include <linux/fiemap.h>
229 #include <linux/if.h>
230 #include <linux/in.h>
231 #include <linux/in6.h>
234 *linux/if_tun.h|*linux/ppp-ioctl.h)
235 echo '#include <linux/filter.h>'
237 *linux/isdn_ppp.h|*linux/gsmmux.h)
238 echo '#include <linux/if.h>'
241 echo 'typedef struct poll_table_struct poll_table;'
243 *linux/ivtvfb.h|*linux/meye.h|*media/*.h)
244 echo '#include <linux/videodev2.h>'
248 i?86|x86_64|arm*|ppc*|s390*) ;;
249 *) return 0 ;; # not applicable
253 echo '#include <linux/atmioc.h>'
255 *linux/usbdevice_fs.h)
257 struct usbdevfs_ctrltransfer32 { __u32 unused[4]; };
258 struct usbdevfs_bulktransfer32 { __u32 unused[4]; };
259 struct usbdevfs_disconnectsignal32 { __u32 unused[2]; };
260 struct usbdevfs_urb32 { __u8 unused[42]; };
261 struct usbdevfs_ioctl32 { __u32 unused[3]; };
265 echo 'typedef __u32 kuid_t;'
269 #include <sound/asound.h>
270 struct snd_seq_queue_owner { __u32 unused[0]; };
275 #include <sound/asound.h>
276 #ifndef DECLARE_BITMAP
277 # define DIV_ROUND_UP(x,y) (((x) + ((y) - 1)) / (y))
278 # define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, 8 * sizeof(long))
279 # define DECLARE_BITMAP(name,bits) unsigned long name[BITS_TO_LONGS(bits)]
284 echo 'struct fb_info;'
286 *xen/evtchn.h|*xen/gntdev.h)
288 typedef uint32_t grant_ref_t;
289 typedef uint16_t domid_t;
293 return 0 # false positives
296 return 0 # too much work to make it compileable
298 esac > "$tmpdir"/fixes.h
300 cat > "$tmpdir"/header.in <<__EOF__
301 #include <asm/bitsperlong.h>
302 #ifndef BITS_PER_LONG
303 # define BITS_PER_LONG __BITS_PER_LONG
308 if [ -f "$inc_dir/uapi/$f" ]; then
310 elif [ -f "$inc_dir/$f" ]; then
313 msg "$f: file not found"
317 [ -n "${f##*/*}" ] ||
318 mkdir -p "$tmpdir/${f%/*}"
319 # Hard workarounds for some processed files. Very fragile.
321 *acpi/*|*linux/i2o.h|*media*/exynos-fimc.h|*media/v4l2-subdev.h|*net/bluetooth/*|net/nfc/nci_core.h)
323 grep "${r_define}${r_cmd_name}" < "$s" > "$tmpdir/$f"
326 # Convert enums to macros.
327 sed '/^enum binder/,/^};/d' < "$s" > "$tmpdir/$f"
328 sed -n '/^enum binder/,/^};/ s/^[[:space:]].*/&/p' < "$s" |
330 s/^[[:space:]]*\([A-Z][A-Z_0-9]*\)[[:space:]]*=[[:space:]]*_\(IO\|IOW\|IOR\|IOWR\|IOC\)[[:space:]]*(/#define \1 _\2(/
331 s/^\(#define .*)\),$/\1/
332 s/^\(#define .*,\)$/\1 \\/
333 s/^\([[:space:]]\+[^),]\+)\),$/\1/' >> "$tmpdir/$f"
336 # Filter out the code that references unknown types.
337 sed '/drm_r128_clear2_t/d' < "$s" > "$tmpdir/$f"
340 # Filter out the code that references unknown types.
341 sed '/^struct sis_file_private/,/^}/d' < "$s" > "$tmpdir/$f"
344 # Create the file it attempts to include.
345 touch "$tmpdir/via_drmclient.h"
346 # Filter out the code that references unknown types.
347 sed '/^struct via_file_private/,/^}/d' < "$s" > "$tmpdir/$f"
350 # Create the file it attempts to include.
351 touch "$tmpdir/asm/bug.h"
353 *linux/vmw_vmci_defs.h)
354 # Fetch ioctl macros only.
355 grep "${r_define}I" < "$s" > "$tmpdir/$f"
357 *media/v4l2-common.h)
358 # Fetch one piece of code containing ioctls definitions.
359 sed -n '/ remaining ioctls/,/ ---/p' < "$s" > "$tmpdir/$f"
361 openpromio.h|*/openpromio.h|fbio.h|*/fbio.h)
362 # Create the file it attempts to include.
363 mkdir -p "$tmpdir/linux"
364 touch "$tmpdir/linux/compiler.h"
366 if [ -f "$tmpdir/$f" ]; then
370 # This may fail if the file includes unavailable headers.
371 # In case of success it outputs both the #define directives
372 # and the result of preprocessing.
373 $CPP $CPPFLAGS -dD $INCLUDES < "$tmpdir"/header.in > "$tmpdir"/header.out
375 # Soft post-preprocess workarounds. Fragile.
378 arm_list='KVM_ARM_PREFERRED_TARGET|KVM_ARM_VCPU_INIT'
379 ppc_list='KVM_ALLOCATE_RMA|KVM_CREATE_SPAPR_TCE|KVM_CREATE_SPAPR_TCE_64|KVM_PPC_GET_HTAB_FD|KVM_PPC_RTAS_DEFINE_TOKEN'
380 x86_list='KVM_GET_CPUID2|KVM_GET_DEBUGREGS|KVM_GET_EMULATED_CPUID|KVM_GET_LAPIC|KVM_GET_MSRS|KVM_GET_MSR_INDEX_LIST|KVM_GET_PIT|KVM_GET_PIT2|KVM_GET_SUPPORTED_CPUID|KVM_GET_VCPU_EVENTS|KVM_GET_XCRS|KVM_GET_XSAVE|KVM_SET_CPUID|KVM_SET_CPUID2|KVM_SET_DEBUGREGS|KVM_SET_LAPIC|KVM_SET_MEMORY_ALIAS|KVM_SET_MSRS|KVM_SET_PIT|KVM_SET_PIT2|KVM_SET_VCPU_EVENTS|KVM_SET_XCRS|KVM_SET_XSAVE|KVM_X86_SET_MCE|KVM_XEN_HVM_CONFIG'
382 arm*) list="$ppc_list|$x86_list" ;;
383 ppc*) list="$arm_list|$x86_list" ;;
384 i?86|x86_64*) list="$arm_list|$ppc_list" ;;
385 *) list="$arm_list|$ppc_list|$x86_list" ;;
387 sed -r -i "/[[:space:]]($list)[[:space:]]/d" "$tmpdir"/header.out
391 # Need to exclude ioctl commands defined elsewhere.
392 local_defines='^[[:space:]]*#[[:space:]]*define[[:space:]]\+\('"$r_cmd_name"'\)[[:space:]]'
393 sed -n 's/'"$local_defines"'.*/\1\\/p' "$s" > "$tmpdir"/local_names
394 r_local_names="$(tr '\n' '|' < "$tmpdir"/local_names)"
395 r_local_names="${r_local_names%%|}"
396 r_local_names="${r_local_names%%\\}"
398 # Keep this in sync with $regexp by replacing $r_cmd_name with $r_local_names.
399 defs_regexp="${r_define}\($r_local_names\)${r_value}"
401 qf="$(echo "$prefix$f" | sed 's/[&\/]/\\&/g')"
402 # This outputs lines in the following format:
403 # print_ioctlent("filename.h", "IOCTL_CMD_NAME", IOCTL_CMD_NAME);
404 sed -n 's/'"$defs_regexp"'.*/print_ioctlent("'"$qf"'", "\1", \1);/p' \
405 < "$tmpdir"/header.out > "$tmpdir"/defs.h
407 # If something is wrong with the file, this will fail.
408 $CC $INCLUDES $CFLAGS -c -o "$tmpdir"/printents.o "$tmpdir"/printents.c
409 $CC $LDFLAGS -o "$tmpdir"/print_ioctlents \
410 "$tmpdir"/printents.o "$tmpdir"/print_ioctlent.o
411 "$tmpdir"/print_ioctlents > "$tmpdir"/ioctlents
412 cat "$tmpdir"/ioctlents
413 msg "$f: fetched $(grep -c '^{' "$tmpdir"/ioctlents) ioctl entries"
417 (process_file "$f" < /dev/null)
419 msg "$f: failed to process"
420 failed=$((1 + $failed))
422 done < "$tmpdir"/headers.list
425 msg "failed to process $failed file(s)"