aboutsummaryrefslogtreecommitdiff
path: root/build/image/image.gni
blob: 8f5d26d242b7977357153c3db0f2356b150f7d34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# Copyright 2018 The Hafnium Authors.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/BSD-3-Clause.

import("//build/toolchain/embedded.gni")
import("//build/toolchain/platform.gni")

# Build image, link to an ELF file then convert to plain binary.
template("image_binary") {
  assert(defined(invoker.image_name),
         "image_binary() must specify an \"image_name\" value")

  output_root = ""
  if (defined(invoker.output_path)) {
    output_root += "${invoker.output_path}/"
  }
  output_root += invoker.image_name

  file_root = "${root_out_dir}/${output_root}"
  elf_file = "${file_root}.elf"
  bin_file = "${file_root}.bin"

  elf_target = "${target_name}__elf"
  checked_elf_target = "${target_name}__checked_elf"

  # Link objects together
  executable(elf_target) {
    forward_variables_from(invoker,
                           [
                             "cflags",
                             "cflags_c",
                             "defines",
                             "deps",
                             "include_dirs",
                             "public_configs",
                             "public_deps",
                             "sources",
                             "testonly",
                           ])
    output_name = "${output_root}.elf"
    inputs = [
      rebase_path("//build/image/image.ld"),
    ]
    ldflags = [
      "-T",
      rebase_path("//build/image/image.ld"),
    ]
    visibility = [
      ":${checked_elf_target}",
      ":${invoker.target_name}",
    ]
  }

  # Analyze the generated ELF file and check that assembly-level fixes, e.g.
  # for CPU errata, have been properly applied.
  action(checked_elf_target) {
    forward_variables_from(invoker, [ "testonly" ])
    stamp_file = elf_file + "_check.stamp"

    script = "//build/image/check_elf.py"
    deps = [
      ":${elf_target}",
    ]
    args = [
      rebase_path(elf_file),
      rebase_path(stamp_file),
      "--max-image-size",
      "$plat_max_image_size",
    ]
    outputs = [
      stamp_file,
    ]
    visibility = [ ":${invoker.target_name}" ]
  }

  action(target_name) {
    forward_variables_from(invoker, [ "testonly" ])

    script = "//build/image/convert_to_binary.py"

    if (defined(invoker.check_binary) && invoker.check_binary == true) {
      deps = [
        ":${checked_elf_target}",
      ]
    } else {
      deps = [
        ":${elf_target}",
      ]
    }
    args = [
      "--input",
      rebase_path(elf_file),
      "--output",
      rebase_path(bin_file),
    ]
    outputs = [
      bin_file,
    ]
  }
}

# Helper to build a hypervisor image
template("hypervisor") {
  image_binary(target_name) {
    forward_variables_from(invoker,
                           [
                             "cflags",
                             "cflags_c",
                             "defines",
                             "deps",
                             "public_deps",
                             "sources",
                             "testonly",
                           ])
    image_name = target_name

    # Perform checks on the generated binary to prevent regressing on some
    # classes of bugs, typically CPU erratas.
    check_binary = true
  }
}

# Helper to build a virtual machine kernel
template("vm_kernel") {
  image_binary(target_name) {
    forward_variables_from(invoker,
                           [
                             "cflags",
                             "cflags_c",
                             "defines",
                             "deps",
                             "include_dirs",
                             "public_configs",
                             "public_deps",
                             "sources",
                             "testonly",
                           ])
    output_path = rebase_path(".", root_out_dir, target_out_dir)
    image_name = target_name
  }
}

# Build the initial RAM disk for the Linux VM.
template("linux_initrd") {
  initrd_base = "${target_out_dir}/${target_name}/initrd"
  initrd_file = "${initrd_base}.img"
  initrd_staging = "${initrd_base}"

  copy_sources = []
  copy_deps = []

  # Add the files that need to be packaged into the RAM disk. The information
  # about these files is encoded in lists with the following elements:
  #
  # - Source file: the target will have the same name as the source file.
  # - Build target for the file (dependency)
  foreach(file, invoker.files) {
    copy_sources += [ file[0] ]
    copy_deps += [ file[1] ]
  }

  copy("${target_name}__staging") {
    forward_variables_from(invoker, [ "testonly" ])
    sources = copy_sources
    deps = copy_deps
    outputs = [
      "${initrd_staging}/{{source_file_part}}",
    ]
  }

  action(target_name) {
    forward_variables_from(invoker, [ "testonly" ])
    script = "//build/image/generate_linux_initrd.py"
    args = [
      "--staging",
      rebase_path(initrd_staging),
      "--output",
      rebase_path(initrd_file),
    ]
    deps = [
      ":${target_name}__staging",
    ]
    outputs = [
      initrd_file,
    ]
  }
}

template("device_tree") {
  action(target_name) {
    forward_variables_from(invoker,
                           [
                             "testonly",
                             "deps",
                           ])
    script = "//build/image/dtc.py"

    sources = [
      invoker.source,
    ]
    outputs = [
      invoker.output,
    ]
    args = [
      "compile",
      "-i",
      rebase_path(sources[0]),
      "-o",
      rebase_path(outputs[0]),
    ]
  }
}

template("fdt_overlay") {
  action(target_name) {
    forward_variables_from(invoker,
                           [
                             "testonly",
                             "deps",
                           ])
    script = "//build/image/dtc.py"

    sources = [
      invoker.base,
      invoker.overlay,
    ]
    outputs = [
      invoker.output,
    ]
    args = [
      "overlay",
      rebase_path(outputs[0]),
      rebase_path(sources[0]),
      rebase_path(sources[1]),
    ]
  }
}

template("incbin") {
  source_set(target_name) {
    forward_variables_from(invoker,
                           [
                             "testonly",
                             "deps",
                           ])

    sources = [
      "//build/image/incbin.S",
    ]
    inputs = invoker.sources
    defines = [
      "SECTION_NAME=" + invoker.section,
      "FILE_PATH=\"" + rebase_path(inputs[0]) + "\"",
    ]
  }
}

template("incbin_target") {
  incbin(target_name) {
    forward_variables_from(invoker,
                           [
                             "testonly",
                             "deps",
                             "section",
                           ])
    target_outputs = get_target_outputs(invoker.target)
    target_file = target_outputs[0]
    sources = [
      target_file,
    ]
    deps = [
      invoker.target,
    ]
  }
}

# Generate the manifest for the hypervisor and apply overlay if specified.
template("manifest") {
  # Check if the invoker specified an overlay for the manifest.
  if (defined(invoker.overlay) && invoker.overlay != "") {
    base_out_dir = invoker.base_out_dir

    base_target = "${target_name}__manifest_base"
    base_file = "${base_out_dir}/manifest_base.dtb"
    overlay_target = "${target_name}__manifest_overlay"
    overlay_file = "${base_out_dir}/manifest_overlay.dtbo"

    # Compile the base manifest.
    device_tree(base_target) {
      source = invoker.source
      output = base_file
    }

    # Compile the manifest overlay.
    device_tree(overlay_target) {
      source = invoker.overlay
      output = overlay_file
    }

    # Merge the base manifest and the overlay, producing the final manifest.
    fdt_overlay(target_name) {
      base = base_file
      overlay = overlay_file
      output = "${target_out_dir}" + "/" + invoker.output
      deps = [
        ":$base_target",
        ":$overlay_target",
      ]
    }
  } else {
    # No overlay specified. Compile the manifest to DTB.
    device_tree(target_name) {
      source = invoker.source
      output = "${target_out_dir}" + "/" + invoker.output
    }
  }
}

# Build the initial RAM disk for the hypervisor.
template("initrd") {
  base_out_dir = "${target_out_dir}/${target_name}"

  action(target_name) {
    forward_variables_from(invoker, [ "testonly" ])
    script = "//build/image/generate_initrd.py"

    initrd_file = "${base_out_dir}/initrd.img"
    initrd_staging = "${base_out_dir}/initrd"

    deps = []
    args = [
      "--staging",
      rebase_path(initrd_staging),
      "--output",
      rebase_path(initrd_file),
    ]

    # Add the files that need to be packaged into the RAM disk. The information
    # about these files is encoded in lists with the following elements:
    #
    # - Target file name in the RAM disk
    # - Build target for the file
    # - File name to use as source (generated as output of the build)
    foreach(file, invoker.files) {
      deps += [ file[1] ]
      args += [
        "--file",
        file[0],
        rebase_path(get_label_info(file[1], "target_out_dir") + "/" + file[2]),
      ]
    }

    outputs = [
      initrd_file,
    ]
  }
}