gen_arm_fs_files.py revision 12123
1#!/usr/bin/env python2
2
3# Copyright (c) 2017 Metempsy Technology Consulting
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met: redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer;
10# 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# neither the name of the copyright holders nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29# Authors: Pau Cabre
30
31from optparse import OptionParser
32from subprocess import call
33from platform import machine
34from distutils import spawn
35from glob import glob
36
37import sys
38import os
39
40def run_cmd(explanation, working_dir, cmd):
41    print "Running phase '%s'" % explanation
42    return_code = call(cmd, cwd = working_dir)
43    if return_code == 0:
44        return
45    print "Error running phase %s. Returncode: %d" % (explanation, return_code)
46    sys.exit(1)
47
48script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
49gem5_dir = os.path.dirname(script_dir)
50
51parser = OptionParser()
52
53parser.add_option("--gem5-dir", default = gem5_dir,
54    metavar = "GEM5_DIR",
55    help = "gem5 root directory to be used for bootloader and "
56           "VExpress_GEM5_V1 DTB sources. The default value is the gem5 root "
57           "directory of the executed script (%default)")
58parser.add_option("--dest-dir", default = "/tmp",
59    metavar = "DEST_DIR",
60    help = "Directory to use for checking out the different kernel "
61           "repositories. Generated files will be copied to "
62           "DEST_DIR/binaries (which must not exist). The default "
63           "value is %default")
64
65(options, args) = parser.parse_args()
66
67if args:
68    print "Unrecognized argument(s) %s." % args
69    sys.exit(1)
70
71if not os.path.isdir(options.dest_dir):
72    print "Error: %s is not a directory." % options.dest_dir
73    sys.exit(1)
74
75binaries_dir = options.dest_dir + "/binaries"
76
77if os.path.exists(binaries_dir):
78    print "Error: %s already exists." % binaries_dir
79    sys.exit(1)
80
81if machine() != "x86_64":
82    print "Error: This script should run in a x86_64 machine"
83    sys.exit(1)
84
85# Some basic dependency checking
86needed_programs = [
87    "make",
88    "aarch64-linux-gnu-gcc",
89    "arm-linux-gnueabihf-gcc",
90    "aarch64-linux-gnu-gcc-4.8",
91    "arm-linux-gnueabihf-gcc-4.8",
92    "gcc",
93    "bc",
94    "dtc",
95    "arm-linux-gnueabi-gcc"
96]
97
98for program in needed_programs:
99    if not spawn.find_executable(program):
100        print "Error: command %s not found in $PATH" % program
101        print ("If running on an Debian-based linux, please try the following "
102               "cmd to get all the necessary packages: ")
103        print ("sudo apt-get install -y make gcc bc gcc-aarch64-linux-gnu "
104              "gcc-4.8-aarch64-linux-gnu gcc-4.8-arm-linux-gnueabihf "
105              "gcc-arm-linux-gnueabihf device-tree-compiler "
106              "gcc-arm-linux-gnueabi")
107        sys.exit(1)
108
109os.mkdir(binaries_dir);
110
111# Checkout and build linux kernel for VExpress_GEM5_V1 (arm and arm64)
112kernel_vexpress_gem5_dir = options.dest_dir + "/linux-kernel-vexpress_gem5"
113run_cmd("clone linux kernel for VExpress_GEM5_V1 platform",
114    options.dest_dir,
115    ["git", "clone", "https://github.com/gem5/linux-arm-gem5.git", "-b",
116     "gem5/v4.4", kernel_vexpress_gem5_dir])
117run_cmd("configure kernel for arm64",
118    kernel_vexpress_gem5_dir,
119    ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
120     "gem5_defconfig"])
121run_cmd("compile kernel for arm64",
122    kernel_vexpress_gem5_dir,
123    ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-"])
124run_cmd("copy arm64 vmlinux",
125    kernel_vexpress_gem5_dir,
126    ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_gem5_v1_64"])
127run_cmd("cleanup arm64 kernel compilation",
128    kernel_vexpress_gem5_dir,
129    ["make", "distclean"])
130run_cmd("configure kernel for arm",
131    kernel_vexpress_gem5_dir,
132    ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
133     "gem5_defconfig"])
134run_cmd("compile kernel for arm",
135    kernel_vexpress_gem5_dir,
136    ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-"])
137run_cmd("copy arm vmlinux",
138    kernel_vexpress_gem5_dir,
139    ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_gem5_v1"])
140
141# Checkout and build linux kernel and DTB for VExpress_EMM64
142kernel_vexpress_emm64_dir = options.dest_dir + "/linux-kernel-vexpress_emm64"
143run_cmd("clone linux kernel for VExpress_EMM64 platform",
144    options.dest_dir,
145    ["git", "clone", "https://github.com/gem5/linux-arm64-gem5.git",
146     kernel_vexpress_emm64_dir])
147run_cmd("configure kernel",
148    kernel_vexpress_emm64_dir,
149    ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
150     "CC=aarch64-linux-gnu-gcc-4.8", "gem5_defconfig"])
151run_cmd("compile kernel",
152    kernel_vexpress_emm64_dir,
153    ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
154     "CC=aarch64-linux-gnu-gcc-4.8"])
155run_cmd("copy vmlinux",
156    kernel_vexpress_emm64_dir,
157    ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_emm64"])
158run_cmd("copy DTB",
159    kernel_vexpress_emm64_dir,
160    ["cp", "arch/arm64/boot/dts/aarch64_gem5_server.dtb", binaries_dir])
161
162# Checkout and build linux kernel and DTBs for VExpress_EMM
163kernel_vexpress_emm_dir = options.dest_dir + "/linux-kernel-vexpress_emm"
164run_cmd("clone linux kernel for VExpress_EMM platform",
165    options.dest_dir,
166    ["git", "clone", "https://github.com/gem5/linux-arm-gem5.git", "-b",
167     "gem5/linaro", kernel_vexpress_emm_dir])
168run_cmd("configure kernel",
169    kernel_vexpress_emm_dir,
170    ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
171     "CC=arm-linux-gnueabihf-gcc-4.8", "vexpress_gem5_server_defconfig"])
172run_cmd("compile kernel",
173    kernel_vexpress_emm_dir,
174    ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
175     "CC=arm-linux-gnueabihf-gcc-4.8"])
176run_cmd("copy vmlinux",
177    kernel_vexpress_emm_dir,
178    ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_emm"])
179run_cmd("copy DTB 1 CPU",
180    kernel_vexpress_emm_dir,
181    ["cp", "arch/arm/boot/dts/vexpress-v2p-ca15-tc1-gem5.dtb",
182     binaries_dir + "/vexpress-v2p-ca15-tc1-gem5_1cpus.dtb"])
183run_cmd("copy DTB 2 CPUs",
184    kernel_vexpress_emm_dir,
185    ["cp", "arch/arm/boot/dts/vexpress-v2p-ca15-tc1-gem5_2cpus.dtb",
186     binaries_dir])
187run_cmd("copy DTB 4 CPUs",
188    kernel_vexpress_emm_dir,
189    ["cp", "arch/arm/boot/dts/vexpress-v2p-ca15-tc1-gem5_4cpus.dtb",
190     binaries_dir])
191
192# Build DTBs for VExpress_GEM5_V1
193dt_dir = gem5_dir + "/system/arm/dt"
194run_cmd("compile DTBs for VExpress_GEM5_V1 platform",
195    dt_dir,
196    ["make"])
197run_cmd("copy DTBs",
198    dt_dir,
199    ["cp"] + glob(dt_dir + "/*dtb") + [binaries_dir])
200
201# Build bootloaders arm64
202bootloader_arm64_dir = gem5_dir + "/system/arm/aarch64_bootloader"
203run_cmd("compile arm64 bootloader",
204    bootloader_arm64_dir,
205    ["make"])
206run_cmd("copy arm64 bootloader",
207    bootloader_arm64_dir,
208    ["cp", "boot_emm.arm64", binaries_dir])
209
210# Build bootloaders arm
211bootloader_arm_dir = gem5_dir + "/system/arm/simple_bootloader"
212run_cmd("compile arm bootloader",
213    bootloader_arm_dir,
214    ["make"])
215run_cmd("copy arm bootloaders",
216    bootloader_arm_dir,
217    ["cp", "boot.arm", "boot_emm.arm", binaries_dir])
218
219print "Done! All the generated files can be found in %s" % binaries_dir
220
221