Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0+
0003 #
0004 # Create an initrd directory if one does not already exist.
0005 #
0006 # Copyright (C) IBM Corporation, 2013
0007 #
0008 # Author: Connor Shu <Connor.Shu@ibm.com>
0009 
0010 D=tools/testing/selftests/rcutorture
0011 
0012 # Prerequisite checks
0013 [ -z "$D" ] && echo >&2 "No argument supplied" && exit 1
0014 if [ ! -d "$D" ]; then
0015     echo >&2 "$D does not exist: Malformed kernel source tree?"
0016     exit 1
0017 fi
0018 if [ -s "$D/initrd/init" ]; then
0019     echo "$D/initrd/init already exists, no need to create it"
0020     exit 0
0021 fi
0022 
0023 # Create a C-language initrd/init infinite-loop program and statically
0024 # link it.  This results in a very small initrd.
0025 echo "Creating a statically linked C-language initrd"
0026 cd $D
0027 mkdir -p initrd
0028 cd initrd
0029 cat > init.c << '___EOF___'
0030 #ifndef NOLIBC
0031 #include <unistd.h>
0032 #include <sys/time.h>
0033 #endif
0034 
0035 volatile unsigned long delaycount;
0036 
0037 int main(int argc, int argv[])
0038 {
0039         int i;
0040         struct timeval tv;
0041         struct timeval tvb;
0042 
0043         for (;;) {
0044                 sleep(1);
0045                 /* Need some userspace time. */
0046                 if (gettimeofday(&tvb, NULL))
0047                         continue;
0048                 do {
0049                         for (i = 0; i < 1000 * 100; i++)
0050                                 delaycount = i * i;
0051                         if (gettimeofday(&tv, NULL))
0052                                 break;
0053                         tv.tv_sec -= tvb.tv_sec;
0054                         if (tv.tv_sec > 1)
0055                                 break;
0056                         tv.tv_usec += tv.tv_sec * 1000 * 1000;
0057                         tv.tv_usec -= tvb.tv_usec;
0058                 } while (tv.tv_usec < 1000);
0059         }
0060         return 0;
0061 }
0062 ___EOF___
0063 
0064 # build using nolibc on supported archs (smaller executable) and fall
0065 # back to regular glibc on other ones.
0066 if echo -e "#if __x86_64__||__i386__||__i486__||__i586__||__i686__" \
0067            "||__ARM_EABI__||__aarch64__\nyes\n#endif" \
0068    | ${CROSS_COMPILE}gcc -E -nostdlib -xc - \
0069    | grep -q '^yes'; then
0070         # architecture supported by nolibc
0071         ${CROSS_COMPILE}gcc -fno-asynchronous-unwind-tables -fno-ident \
0072                 -nostdlib -include ../../../../include/nolibc/nolibc.h \
0073                 -s -static -Os -o init init.c -lgcc
0074 else
0075         ${CROSS_COMPILE}gcc -s -static -Os -o init init.c
0076 fi
0077 
0078 rm init.c
0079 echo "Done creating a statically linked C-language initrd"
0080 
0081 exit 0