18c2ecf20Sopenharmony_ci#!/bin/bash
28c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0+
38c2ecf20Sopenharmony_ci#
48c2ecf20Sopenharmony_ci# Create an initrd directory if one does not already exist.
58c2ecf20Sopenharmony_ci#
68c2ecf20Sopenharmony_ci# Copyright (C) IBM Corporation, 2013
78c2ecf20Sopenharmony_ci#
88c2ecf20Sopenharmony_ci# Author: Connor Shu <Connor.Shu@ibm.com>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciD=tools/testing/selftests/rcutorture
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci# Prerequisite checks
138c2ecf20Sopenharmony_ci[ -z "$D" ] && echo >&2 "No argument supplied" && exit 1
148c2ecf20Sopenharmony_ciif [ ! -d "$D" ]; then
158c2ecf20Sopenharmony_ci    echo >&2 "$D does not exist: Malformed kernel source tree?"
168c2ecf20Sopenharmony_ci    exit 1
178c2ecf20Sopenharmony_cifi
188c2ecf20Sopenharmony_ciif [ -s "$D/initrd/init" ]; then
198c2ecf20Sopenharmony_ci    echo "$D/initrd/init already exists, no need to create it"
208c2ecf20Sopenharmony_ci    exit 0
218c2ecf20Sopenharmony_cifi
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci# Create a C-language initrd/init infinite-loop program and statically
248c2ecf20Sopenharmony_ci# link it.  This results in a very small initrd.
258c2ecf20Sopenharmony_ciecho "Creating a statically linked C-language initrd"
268c2ecf20Sopenharmony_cicd $D
278c2ecf20Sopenharmony_cimkdir -p initrd
288c2ecf20Sopenharmony_cicd initrd
298c2ecf20Sopenharmony_cicat > init.c << '___EOF___'
308c2ecf20Sopenharmony_ci#ifndef NOLIBC
318c2ecf20Sopenharmony_ci#include <unistd.h>
328c2ecf20Sopenharmony_ci#include <sys/time.h>
338c2ecf20Sopenharmony_ci#endif
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_civolatile unsigned long delaycount;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ciint main(int argc, int argv[])
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	int i;
408c2ecf20Sopenharmony_ci	struct timeval tv;
418c2ecf20Sopenharmony_ci	struct timeval tvb;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	for (;;) {
448c2ecf20Sopenharmony_ci		sleep(1);
458c2ecf20Sopenharmony_ci		/* Need some userspace time. */
468c2ecf20Sopenharmony_ci		if (gettimeofday(&tvb, NULL))
478c2ecf20Sopenharmony_ci			continue;
488c2ecf20Sopenharmony_ci		do {
498c2ecf20Sopenharmony_ci			for (i = 0; i < 1000 * 100; i++)
508c2ecf20Sopenharmony_ci				delaycount = i * i;
518c2ecf20Sopenharmony_ci			if (gettimeofday(&tv, NULL))
528c2ecf20Sopenharmony_ci				break;
538c2ecf20Sopenharmony_ci			tv.tv_sec -= tvb.tv_sec;
548c2ecf20Sopenharmony_ci			if (tv.tv_sec > 1)
558c2ecf20Sopenharmony_ci				break;
568c2ecf20Sopenharmony_ci			tv.tv_usec += tv.tv_sec * 1000 * 1000;
578c2ecf20Sopenharmony_ci			tv.tv_usec -= tvb.tv_usec;
588c2ecf20Sopenharmony_ci		} while (tv.tv_usec < 1000);
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci	return 0;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci___EOF___
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci# build using nolibc on supported archs (smaller executable) and fall
658c2ecf20Sopenharmony_ci# back to regular glibc on other ones.
668c2ecf20Sopenharmony_ciif echo -e "#if __x86_64__||__i386__||__i486__||__i586__||__i686__" \
678c2ecf20Sopenharmony_ci           "||__ARM_EABI__||__aarch64__\nyes\n#endif" \
688c2ecf20Sopenharmony_ci   | ${CROSS_COMPILE}gcc -E -nostdlib -xc - \
698c2ecf20Sopenharmony_ci   | grep -q '^yes'; then
708c2ecf20Sopenharmony_ci	# architecture supported by nolibc
718c2ecf20Sopenharmony_ci        ${CROSS_COMPILE}gcc -fno-asynchronous-unwind-tables -fno-ident \
728c2ecf20Sopenharmony_ci		-nostdlib -include ../../../../include/nolibc/nolibc.h \
738c2ecf20Sopenharmony_ci		-lgcc -s -static -Os -o init init.c
748c2ecf20Sopenharmony_cielse
758c2ecf20Sopenharmony_ci	${CROSS_COMPILE}gcc -s -static -Os -o init init.c
768c2ecf20Sopenharmony_cifi
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cirm init.c
798c2ecf20Sopenharmony_ciecho "Done creating a statically linked C-language initrd"
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ciexit 0
82