gen_arm_fs_files.py revision 12123
112123Spau.cabre@metempsy.com#!/usr/bin/env python2
212123Spau.cabre@metempsy.com
312123Spau.cabre@metempsy.com# Copyright (c) 2017 Metempsy Technology Consulting
412123Spau.cabre@metempsy.com# All rights reserved.
512123Spau.cabre@metempsy.com#
612123Spau.cabre@metempsy.com# Redistribution and use in source and binary forms, with or without
712123Spau.cabre@metempsy.com# modification, are permitted provided that the following conditions are
812123Spau.cabre@metempsy.com# met: redistributions of source code must retain the above copyright
912123Spau.cabre@metempsy.com# notice, this list of conditions and the following disclaimer;
1012123Spau.cabre@metempsy.com# redistributions in binary form must reproduce the above copyright
1112123Spau.cabre@metempsy.com# notice, this list of conditions and the following disclaimer in the
1212123Spau.cabre@metempsy.com# documentation and/or other materials provided with the distribution;
1312123Spau.cabre@metempsy.com# neither the name of the copyright holders nor the names of its
1412123Spau.cabre@metempsy.com# contributors may be used to endorse or promote products derived from
1512123Spau.cabre@metempsy.com# this software without specific prior written permission.
1612123Spau.cabre@metempsy.com#
1712123Spau.cabre@metempsy.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1812123Spau.cabre@metempsy.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1912123Spau.cabre@metempsy.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2012123Spau.cabre@metempsy.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2112123Spau.cabre@metempsy.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2212123Spau.cabre@metempsy.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2312123Spau.cabre@metempsy.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2412123Spau.cabre@metempsy.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2512123Spau.cabre@metempsy.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2612123Spau.cabre@metempsy.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2712123Spau.cabre@metempsy.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2812123Spau.cabre@metempsy.com#
2912123Spau.cabre@metempsy.com# Authors: Pau Cabre
3012123Spau.cabre@metempsy.com
3112123Spau.cabre@metempsy.comfrom optparse import OptionParser
3212123Spau.cabre@metempsy.comfrom subprocess import call
3312123Spau.cabre@metempsy.comfrom platform import machine
3412123Spau.cabre@metempsy.comfrom distutils import spawn
3512123Spau.cabre@metempsy.comfrom glob import glob
3612123Spau.cabre@metempsy.com
3712123Spau.cabre@metempsy.comimport sys
3812123Spau.cabre@metempsy.comimport os
3912123Spau.cabre@metempsy.com
4012123Spau.cabre@metempsy.comdef run_cmd(explanation, working_dir, cmd):
4112123Spau.cabre@metempsy.com    print "Running phase '%s'" % explanation
4212123Spau.cabre@metempsy.com    return_code = call(cmd, cwd = working_dir)
4312123Spau.cabre@metempsy.com    if return_code == 0:
4412123Spau.cabre@metempsy.com        return
4512123Spau.cabre@metempsy.com    print "Error running phase %s. Returncode: %d" % (explanation, return_code)
4612123Spau.cabre@metempsy.com    sys.exit(1)
4712123Spau.cabre@metempsy.com
4812123Spau.cabre@metempsy.comscript_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
4912123Spau.cabre@metempsy.comgem5_dir = os.path.dirname(script_dir)
5012123Spau.cabre@metempsy.com
5112123Spau.cabre@metempsy.comparser = OptionParser()
5212123Spau.cabre@metempsy.com
5312123Spau.cabre@metempsy.comparser.add_option("--gem5-dir", default = gem5_dir,
5412123Spau.cabre@metempsy.com    metavar = "GEM5_DIR",
5512123Spau.cabre@metempsy.com    help = "gem5 root directory to be used for bootloader and "
5612123Spau.cabre@metempsy.com           "VExpress_GEM5_V1 DTB sources. The default value is the gem5 root "
5712123Spau.cabre@metempsy.com           "directory of the executed script (%default)")
5812123Spau.cabre@metempsy.comparser.add_option("--dest-dir", default = "/tmp",
5912123Spau.cabre@metempsy.com    metavar = "DEST_DIR",
6012123Spau.cabre@metempsy.com    help = "Directory to use for checking out the different kernel "
6112123Spau.cabre@metempsy.com           "repositories. Generated files will be copied to "
6212123Spau.cabre@metempsy.com           "DEST_DIR/binaries (which must not exist). The default "
6312123Spau.cabre@metempsy.com           "value is %default")
6412123Spau.cabre@metempsy.com
6512123Spau.cabre@metempsy.com(options, args) = parser.parse_args()
6612123Spau.cabre@metempsy.com
6712123Spau.cabre@metempsy.comif args:
6812123Spau.cabre@metempsy.com    print "Unrecognized argument(s) %s." % args
6912123Spau.cabre@metempsy.com    sys.exit(1)
7012123Spau.cabre@metempsy.com
7112123Spau.cabre@metempsy.comif not os.path.isdir(options.dest_dir):
7212123Spau.cabre@metempsy.com    print "Error: %s is not a directory." % options.dest_dir
7312123Spau.cabre@metempsy.com    sys.exit(1)
7412123Spau.cabre@metempsy.com
7512123Spau.cabre@metempsy.combinaries_dir = options.dest_dir + "/binaries"
7612123Spau.cabre@metempsy.com
7712123Spau.cabre@metempsy.comif os.path.exists(binaries_dir):
7812123Spau.cabre@metempsy.com    print "Error: %s already exists." % binaries_dir
7912123Spau.cabre@metempsy.com    sys.exit(1)
8012123Spau.cabre@metempsy.com
8112123Spau.cabre@metempsy.comif machine() != "x86_64":
8212123Spau.cabre@metempsy.com    print "Error: This script should run in a x86_64 machine"
8312123Spau.cabre@metempsy.com    sys.exit(1)
8412123Spau.cabre@metempsy.com
8512123Spau.cabre@metempsy.com# Some basic dependency checking
8612123Spau.cabre@metempsy.comneeded_programs = [
8712123Spau.cabre@metempsy.com    "make",
8812123Spau.cabre@metempsy.com    "aarch64-linux-gnu-gcc",
8912123Spau.cabre@metempsy.com    "arm-linux-gnueabihf-gcc",
9012123Spau.cabre@metempsy.com    "aarch64-linux-gnu-gcc-4.8",
9112123Spau.cabre@metempsy.com    "arm-linux-gnueabihf-gcc-4.8",
9212123Spau.cabre@metempsy.com    "gcc",
9312123Spau.cabre@metempsy.com    "bc",
9412123Spau.cabre@metempsy.com    "dtc",
9512123Spau.cabre@metempsy.com    "arm-linux-gnueabi-gcc"
9612123Spau.cabre@metempsy.com]
9712123Spau.cabre@metempsy.com
9812123Spau.cabre@metempsy.comfor program in needed_programs:
9912123Spau.cabre@metempsy.com    if not spawn.find_executable(program):
10012123Spau.cabre@metempsy.com        print "Error: command %s not found in $PATH" % program
10112123Spau.cabre@metempsy.com        print ("If running on an Debian-based linux, please try the following "
10212123Spau.cabre@metempsy.com               "cmd to get all the necessary packages: ")
10312123Spau.cabre@metempsy.com        print ("sudo apt-get install -y make gcc bc gcc-aarch64-linux-gnu "
10412123Spau.cabre@metempsy.com              "gcc-4.8-aarch64-linux-gnu gcc-4.8-arm-linux-gnueabihf "
10512123Spau.cabre@metempsy.com              "gcc-arm-linux-gnueabihf device-tree-compiler "
10612123Spau.cabre@metempsy.com              "gcc-arm-linux-gnueabi")
10712123Spau.cabre@metempsy.com        sys.exit(1)
10812123Spau.cabre@metempsy.com
10912123Spau.cabre@metempsy.comos.mkdir(binaries_dir);
11012123Spau.cabre@metempsy.com
11112123Spau.cabre@metempsy.com# Checkout and build linux kernel for VExpress_GEM5_V1 (arm and arm64)
11212123Spau.cabre@metempsy.comkernel_vexpress_gem5_dir = options.dest_dir + "/linux-kernel-vexpress_gem5"
11312123Spau.cabre@metempsy.comrun_cmd("clone linux kernel for VExpress_GEM5_V1 platform",
11412123Spau.cabre@metempsy.com    options.dest_dir,
11512123Spau.cabre@metempsy.com    ["git", "clone", "https://github.com/gem5/linux-arm-gem5.git", "-b",
11612123Spau.cabre@metempsy.com     "gem5/v4.4", kernel_vexpress_gem5_dir])
11712123Spau.cabre@metempsy.comrun_cmd("configure kernel for arm64",
11812123Spau.cabre@metempsy.com    kernel_vexpress_gem5_dir,
11912123Spau.cabre@metempsy.com    ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
12012123Spau.cabre@metempsy.com     "gem5_defconfig"])
12112123Spau.cabre@metempsy.comrun_cmd("compile kernel for arm64",
12212123Spau.cabre@metempsy.com    kernel_vexpress_gem5_dir,
12312123Spau.cabre@metempsy.com    ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-"])
12412123Spau.cabre@metempsy.comrun_cmd("copy arm64 vmlinux",
12512123Spau.cabre@metempsy.com    kernel_vexpress_gem5_dir,
12612123Spau.cabre@metempsy.com    ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_gem5_v1_64"])
12712123Spau.cabre@metempsy.comrun_cmd("cleanup arm64 kernel compilation",
12812123Spau.cabre@metempsy.com    kernel_vexpress_gem5_dir,
12912123Spau.cabre@metempsy.com    ["make", "distclean"])
13012123Spau.cabre@metempsy.comrun_cmd("configure kernel for arm",
13112123Spau.cabre@metempsy.com    kernel_vexpress_gem5_dir,
13212123Spau.cabre@metempsy.com    ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
13312123Spau.cabre@metempsy.com     "gem5_defconfig"])
13412123Spau.cabre@metempsy.comrun_cmd("compile kernel for arm",
13512123Spau.cabre@metempsy.com    kernel_vexpress_gem5_dir,
13612123Spau.cabre@metempsy.com    ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-"])
13712123Spau.cabre@metempsy.comrun_cmd("copy arm vmlinux",
13812123Spau.cabre@metempsy.com    kernel_vexpress_gem5_dir,
13912123Spau.cabre@metempsy.com    ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_gem5_v1"])
14012123Spau.cabre@metempsy.com
14112123Spau.cabre@metempsy.com# Checkout and build linux kernel and DTB for VExpress_EMM64
14212123Spau.cabre@metempsy.comkernel_vexpress_emm64_dir = options.dest_dir + "/linux-kernel-vexpress_emm64"
14312123Spau.cabre@metempsy.comrun_cmd("clone linux kernel for VExpress_EMM64 platform",
14412123Spau.cabre@metempsy.com    options.dest_dir,
14512123Spau.cabre@metempsy.com    ["git", "clone", "https://github.com/gem5/linux-arm64-gem5.git",
14612123Spau.cabre@metempsy.com     kernel_vexpress_emm64_dir])
14712123Spau.cabre@metempsy.comrun_cmd("configure kernel",
14812123Spau.cabre@metempsy.com    kernel_vexpress_emm64_dir,
14912123Spau.cabre@metempsy.com    ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
15012123Spau.cabre@metempsy.com     "CC=aarch64-linux-gnu-gcc-4.8", "gem5_defconfig"])
15112123Spau.cabre@metempsy.comrun_cmd("compile kernel",
15212123Spau.cabre@metempsy.com    kernel_vexpress_emm64_dir,
15312123Spau.cabre@metempsy.com    ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
15412123Spau.cabre@metempsy.com     "CC=aarch64-linux-gnu-gcc-4.8"])
15512123Spau.cabre@metempsy.comrun_cmd("copy vmlinux",
15612123Spau.cabre@metempsy.com    kernel_vexpress_emm64_dir,
15712123Spau.cabre@metempsy.com    ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_emm64"])
15812123Spau.cabre@metempsy.comrun_cmd("copy DTB",
15912123Spau.cabre@metempsy.com    kernel_vexpress_emm64_dir,
16012123Spau.cabre@metempsy.com    ["cp", "arch/arm64/boot/dts/aarch64_gem5_server.dtb", binaries_dir])
16112123Spau.cabre@metempsy.com
16212123Spau.cabre@metempsy.com# Checkout and build linux kernel and DTBs for VExpress_EMM
16312123Spau.cabre@metempsy.comkernel_vexpress_emm_dir = options.dest_dir + "/linux-kernel-vexpress_emm"
16412123Spau.cabre@metempsy.comrun_cmd("clone linux kernel for VExpress_EMM platform",
16512123Spau.cabre@metempsy.com    options.dest_dir,
16612123Spau.cabre@metempsy.com    ["git", "clone", "https://github.com/gem5/linux-arm-gem5.git", "-b",
16712123Spau.cabre@metempsy.com     "gem5/linaro", kernel_vexpress_emm_dir])
16812123Spau.cabre@metempsy.comrun_cmd("configure kernel",
16912123Spau.cabre@metempsy.com    kernel_vexpress_emm_dir,
17012123Spau.cabre@metempsy.com    ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
17112123Spau.cabre@metempsy.com     "CC=arm-linux-gnueabihf-gcc-4.8", "vexpress_gem5_server_defconfig"])
17212123Spau.cabre@metempsy.comrun_cmd("compile kernel",
17312123Spau.cabre@metempsy.com    kernel_vexpress_emm_dir,
17412123Spau.cabre@metempsy.com    ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
17512123Spau.cabre@metempsy.com     "CC=arm-linux-gnueabihf-gcc-4.8"])
17612123Spau.cabre@metempsy.comrun_cmd("copy vmlinux",
17712123Spau.cabre@metempsy.com    kernel_vexpress_emm_dir,
17812123Spau.cabre@metempsy.com    ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_emm"])
17912123Spau.cabre@metempsy.comrun_cmd("copy DTB 1 CPU",
18012123Spau.cabre@metempsy.com    kernel_vexpress_emm_dir,
18112123Spau.cabre@metempsy.com    ["cp", "arch/arm/boot/dts/vexpress-v2p-ca15-tc1-gem5.dtb",
18212123Spau.cabre@metempsy.com     binaries_dir + "/vexpress-v2p-ca15-tc1-gem5_1cpus.dtb"])
18312123Spau.cabre@metempsy.comrun_cmd("copy DTB 2 CPUs",
18412123Spau.cabre@metempsy.com    kernel_vexpress_emm_dir,
18512123Spau.cabre@metempsy.com    ["cp", "arch/arm/boot/dts/vexpress-v2p-ca15-tc1-gem5_2cpus.dtb",
18612123Spau.cabre@metempsy.com     binaries_dir])
18712123Spau.cabre@metempsy.comrun_cmd("copy DTB 4 CPUs",
18812123Spau.cabre@metempsy.com    kernel_vexpress_emm_dir,
18912123Spau.cabre@metempsy.com    ["cp", "arch/arm/boot/dts/vexpress-v2p-ca15-tc1-gem5_4cpus.dtb",
19012123Spau.cabre@metempsy.com     binaries_dir])
19112123Spau.cabre@metempsy.com
19212123Spau.cabre@metempsy.com# Build DTBs for VExpress_GEM5_V1
19312123Spau.cabre@metempsy.comdt_dir = gem5_dir + "/system/arm/dt"
19412123Spau.cabre@metempsy.comrun_cmd("compile DTBs for VExpress_GEM5_V1 platform",
19512123Spau.cabre@metempsy.com    dt_dir,
19612123Spau.cabre@metempsy.com    ["make"])
19712123Spau.cabre@metempsy.comrun_cmd("copy DTBs",
19812123Spau.cabre@metempsy.com    dt_dir,
19912123Spau.cabre@metempsy.com    ["cp"] + glob(dt_dir + "/*dtb") + [binaries_dir])
20012123Spau.cabre@metempsy.com
20112123Spau.cabre@metempsy.com# Build bootloaders arm64
20212123Spau.cabre@metempsy.combootloader_arm64_dir = gem5_dir + "/system/arm/aarch64_bootloader"
20312123Spau.cabre@metempsy.comrun_cmd("compile arm64 bootloader",
20412123Spau.cabre@metempsy.com    bootloader_arm64_dir,
20512123Spau.cabre@metempsy.com    ["make"])
20612123Spau.cabre@metempsy.comrun_cmd("copy arm64 bootloader",
20712123Spau.cabre@metempsy.com    bootloader_arm64_dir,
20812123Spau.cabre@metempsy.com    ["cp", "boot_emm.arm64", binaries_dir])
20912123Spau.cabre@metempsy.com
21012123Spau.cabre@metempsy.com# Build bootloaders arm
21112123Spau.cabre@metempsy.combootloader_arm_dir = gem5_dir + "/system/arm/simple_bootloader"
21212123Spau.cabre@metempsy.comrun_cmd("compile arm bootloader",
21312123Spau.cabre@metempsy.com    bootloader_arm_dir,
21412123Spau.cabre@metempsy.com    ["make"])
21512123Spau.cabre@metempsy.comrun_cmd("copy arm bootloaders",
21612123Spau.cabre@metempsy.com    bootloader_arm_dir,
21712123Spau.cabre@metempsy.com    ["cp", "boot.arm", "boot_emm.arm", binaries_dir])
21812123Spau.cabre@metempsy.com
21912123Spau.cabre@metempsy.comprint "Done! All the generated files can be found in %s" % binaries_dir
22012123Spau.cabre@metempsy.com
221