blob: 90409b318dbf960b99e1a945fe1f83282828b2f0 [file] [log] [blame]
Jiyong Park1f7b93e2021-02-01 21:38:11 +09001// Copyright (C) 2021 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package filesystem
16
17import (
18 "fmt"
19 "strconv"
Jiyong Parkac4076d2021-03-15 23:21:30 +090020 "strings"
Jiyong Park1f7b93e2021-02-01 21:38:11 +090021
22 "github.com/google/blueprint"
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26)
27
28func init() {
Cole Faustf2a6e8b2024-11-14 10:54:48 -080029 android.RegisterModuleType("bootimg", BootimgFactory)
Jiyong Park1f7b93e2021-02-01 21:38:11 +090030}
31
32type bootimg struct {
33 android.ModuleBase
34
Cole Faustf2a6e8b2024-11-14 10:54:48 -080035 properties BootimgProperties
Jiyong Park1f7b93e2021-02-01 21:38:11 +090036
Cole Faust4e9f5922024-11-13 16:09:23 -080037 output android.Path
Jiyong Park1f7b93e2021-02-01 21:38:11 +090038 installDir android.InstallPath
Jihoon Kang96fdba92024-11-19 22:25:36 +000039
40 bootImageType bootImageType
Jiyong Park1f7b93e2021-02-01 21:38:11 +090041}
42
Cole Faustf2a6e8b2024-11-14 10:54:48 -080043type BootimgProperties struct {
Jiyong Park1f55dbd2021-02-15 17:57:35 +090044 // Set the name of the output. Defaults to <module_name>.img.
45 Stem *string
46
Jiyong Park1f7b93e2021-02-01 21:38:11 +090047 // Path to the linux kernel prebuilt file
48 Kernel_prebuilt *string `android:"arch_variant,path"`
49
50 // Filesystem module that is used as ramdisk
51 Ramdisk_module *string
52
53 // Path to the device tree blob (DTB) prebuilt file to add to this boot image
54 Dtb_prebuilt *string `android:"arch_variant,path"`
55
56 // Header version number. Must be set to one of the version numbers that are currently
57 // supported. Refer to
58 // https://source.android.com/devices/bootloader/boot-image-header
59 Header_version *string
60
Jihoon Kang96fdba92024-11-19 22:25:36 +000061 // Determines the specific type of boot image this module is building. Can be boot,
62 // vendor_boot or init_boot. Defaults to boot.
63 // Refer to https://source.android.com/devices/bootloader/partitions/vendor-boot-partitions
64 // for vendor_boot.
65 // Refer to https://source.android.com/docs/core/architecture/partitions/generic-boot for
66 // init_boot.
67 Boot_image_type *string
Jihoon Kang95eb1da2024-11-19 20:55:20 +000068
Jiyong Park16e77a92021-08-30 18:43:19 +090069 // Optional kernel commandline arguments
70 Cmdline []string `android:"arch_variant"`
Jiyong Park1f7b93e2021-02-01 21:38:11 +090071
Jiyong Park81aea9a2021-03-05 18:58:29 +090072 // File that contains bootconfig parameters. This can be set only when `vendor_boot` is true
73 // and `header_version` is greater than or equal to 4.
74 Bootconfig *string `android:"arch_variant,path"`
75
Cole Faust26bdac52024-11-19 13:37:53 -080076 // The size of the partition on the device. It will be a build error if this built partition
77 // image exceeds this size.
78 Partition_size *int64
79
Jiyong Park1f7b93e2021-02-01 21:38:11 +090080 // When set to true, sign the image with avbtool. Default is false.
81 Use_avb *bool
82
83 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
84 Partition_name *string
85
86 // Path to the private key that avbtool will use to sign this filesystem image.
87 // TODO(jiyong): allow apex_key to be specified here
Cole Faust65cb40a2024-10-21 15:41:42 -070088 Avb_private_key *string `android:"path_device_first"`
Jiyong Park1f7b93e2021-02-01 21:38:11 +090089
90 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
91 Avb_algorithm *string
Cole Faust336b3ba2024-11-19 16:34:29 -080092
93 // The security patch passed to as the com.android.build.<type>.security_patch avb property.
94 // Replacement for the make variables BOOT_SECURITY_PATCH / INIT_BOOT_SECURITY_PATCH.
95 Security_patch *string
Jiyong Park1f7b93e2021-02-01 21:38:11 +090096}
97
Jihoon Kang96fdba92024-11-19 22:25:36 +000098type bootImageType int
99
100const (
101 unsupported bootImageType = iota
102 boot
103 vendorBoot
104 initBoot
105)
106
107func toBootImageType(ctx android.ModuleContext, bootImageType string) bootImageType {
108 switch bootImageType {
109 case "boot":
110 return boot
111 case "vendor_boot":
112 return vendorBoot
113 case "init_boot":
114 return initBoot
115 default:
116 ctx.ModuleErrorf("Unknown boot_image_type %s. Must be one of \"boot\", \"vendor_boot\", or \"init_boot\"", bootImageType)
117 }
118 return unsupported
119}
120
Cole Faust336b3ba2024-11-19 16:34:29 -0800121func (b bootImageType) String() string {
122 switch b {
123 case boot:
124 return "boot"
125 case vendorBoot:
126 return "vendor_boot"
127 case initBoot:
128 return "init_boot"
129 default:
130 panic("unknown boot image type")
131 }
132}
133
Jihoon Kang96fdba92024-11-19 22:25:36 +0000134func (b bootImageType) isBoot() bool {
135 return b == boot
136}
137
138func (b bootImageType) isVendorBoot() bool {
139 return b == vendorBoot
140}
141
142func (b bootImageType) isInitBoot() bool {
143 return b == initBoot
144}
145
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900146// bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb.
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800147func BootimgFactory() android.Module {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900148 module := &bootimg{}
149 module.AddProperties(&module.properties)
150 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
151 return module
152}
153
154type bootimgDep struct {
155 blueprint.BaseDependencyTag
156 kind string
157}
158
159var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"}
160
161func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) {
162 ramdisk := proptools.String(b.properties.Ramdisk_module)
163 if ramdisk != "" {
164 ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk)
165 }
166}
167
168func (b *bootimg) installFileName() string {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900169 return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900170}
171
172func (b *bootimg) partitionName() string {
173 return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName())
174}
175
176func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jihoon Kang96fdba92024-11-19 22:25:36 +0000177 b.bootImageType = toBootImageType(ctx, proptools.StringDefault(b.properties.Boot_image_type, "boot"))
Cole Faust336b3ba2024-11-19 16:34:29 -0800178 if b.bootImageType == unsupported {
179 return
180 }
181
182 kernelProp := proptools.String(b.properties.Kernel_prebuilt)
183 if b.bootImageType.isVendorBoot() && kernelProp != "" {
184 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
185 return
186 }
187 if b.bootImageType.isBoot() && kernelProp == "" {
188 ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel")
189 return
190 }
191 var kernel android.Path
192 if kernelProp != "" {
193 kernel = android.PathForModuleSrc(ctx, kernelProp)
194 }
195
196 unsignedOutput := b.buildBootImage(ctx, kernel)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900197
Cole Faust4e9f5922024-11-13 16:09:23 -0800198 output := unsignedOutput
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900199 if proptools.Bool(b.properties.Use_avb) {
Cole Faust336b3ba2024-11-19 16:34:29 -0800200 // This bootimg module supports 2 modes of avb signing, it picks between them based on
201 // if the private key is specified or not. If there is a key, it does a signing process
202 // similar to how the regular partitions (system, product, vendor, etc) are signed.
203 // If the key is not provided, it will just add an avb footer to the image. The avb
204 // footer only signing is how the make-built init_boot, boot, and vendor_boot images are
205 // built.
206 if proptools.String(b.properties.Avb_private_key) != "" {
207 output = b.signImage(ctx, unsignedOutput)
208 } else {
209 output = b.addAvbFooter(ctx, unsignedOutput, kernel)
210 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900211 }
212
213 b.installDir = android.PathForModuleInstall(ctx, "etc")
Cole Faust4e9f5922024-11-13 16:09:23 -0800214 ctx.InstallFile(b.installDir, b.installFileName(), output)
mrziwang555d1332024-06-07 11:15:33 -0700215
Cole Faust4e9f5922024-11-13 16:09:23 -0800216 ctx.SetOutputFiles([]android.Path{output}, "")
217 b.output = output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900218}
219
Cole Faust336b3ba2024-11-19 16:34:29 -0800220func (b *bootimg) buildBootImage(ctx android.ModuleContext, kernel android.Path) android.Path {
Cole Faust4e9f5922024-11-13 16:09:23 -0800221 output := android.PathForModuleOut(ctx, "unsigned", b.installFileName())
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900222
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900223 builder := android.NewRuleBuilder(pctx, ctx)
224 cmd := builder.Command().BuiltTool("mkbootimg")
225
Cole Faust336b3ba2024-11-19 16:34:29 -0800226 if kernel != nil {
227 cmd.FlagWithInput("--kernel ", kernel)
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900228 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900229
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000230 // These arguments are passed for boot.img and init_boot.img generation
Jihoon Kang96fdba92024-11-19 22:25:36 +0000231 if b.bootImageType.isBoot() || b.bootImageType.isInitBoot() {
Cole Faust1c9c3352024-11-19 11:34:44 -0800232 cmd.FlagWithArg("--os_version ", ctx.Config().PlatformVersionLastStable())
233 cmd.FlagWithArg("--os_patch_level ", ctx.Config().PlatformSecurityPatch())
234 }
235
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900236 dtbName := proptools.String(b.properties.Dtb_prebuilt)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900237 if dtbName != "" {
238 dtb := android.PathForModuleSrc(ctx, dtbName)
239 cmd.FlagWithInput("--dtb ", dtb)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900240 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900241
Jiyong Park16e77a92021-08-30 18:43:19 +0900242 cmdline := strings.Join(b.properties.Cmdline, " ")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900243 if cmdline != "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900244 flag := "--cmdline "
Jihoon Kang96fdba92024-11-19 22:25:36 +0000245 if b.bootImageType.isVendorBoot() {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900246 flag = "--vendor_cmdline "
247 }
Jooyung Han32cddd02021-03-08 20:54:16 +0900248 cmd.FlagWithArg(flag, proptools.ShellEscapeIncludingSpaces(cmdline))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900249 }
250
251 headerVersion := proptools.String(b.properties.Header_version)
252 if headerVersion == "" {
253 ctx.PropertyErrorf("header_version", "must be set")
254 return output
255 }
256 verNum, err := strconv.Atoi(headerVersion)
257 if err != nil {
258 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion)
259 return output
260 }
261 if verNum < 3 {
262 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot")
263 return output
264 }
265 cmd.FlagWithArg("--header_version ", headerVersion)
266
267 ramdiskName := proptools.String(b.properties.Ramdisk_module)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900268 if ramdiskName != "" {
269 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep)
270 if filesystem, ok := ramdisk.(*filesystem); ok {
271 flag := "--ramdisk "
Jihoon Kang96fdba92024-11-19 22:25:36 +0000272 if b.bootImageType.isVendorBoot() {
Jiyong Park393ebfc2022-01-06 14:28:53 +0900273 flag = "--vendor_ramdisk "
274 }
275 cmd.FlagWithInput(flag, filesystem.OutputPath())
276 } else {
277 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name())
278 return output
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900279 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900280 }
281
Jiyong Park81aea9a2021-03-05 18:58:29 +0900282 bootconfig := proptools.String(b.properties.Bootconfig)
283 if bootconfig != "" {
Jihoon Kang96fdba92024-11-19 22:25:36 +0000284 if !b.bootImageType.isVendorBoot() {
Jiyong Park81aea9a2021-03-05 18:58:29 +0900285 ctx.PropertyErrorf("bootconfig", "requires vendor_boot: true")
286 return output
287 }
288 if verNum < 4 {
289 ctx.PropertyErrorf("bootconfig", "requires header_version: 4 or later")
290 return output
291 }
292 cmd.FlagWithInput("--vendor_bootconfig ", android.PathForModuleSrc(ctx, bootconfig))
293 }
294
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000295 // Output flag for boot.img and init_boot.img
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900296 flag := "--output "
Jihoon Kang96fdba92024-11-19 22:25:36 +0000297 if b.bootImageType.isVendorBoot() {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900298 flag = "--vendor_boot "
299 }
300 cmd.FlagWithOutput(flag, output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900301
Cole Faust26bdac52024-11-19 13:37:53 -0800302 if b.properties.Partition_size != nil {
303 assertMaxImageSize(builder, output, *b.properties.Partition_size, proptools.Bool(b.properties.Use_avb))
304 }
305
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900306 builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName()))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900307 return output
308}
309
Cole Faust336b3ba2024-11-19 16:34:29 -0800310func (b *bootimg) addAvbFooter(ctx android.ModuleContext, unsignedImage android.Path, kernel android.Path) android.Path {
311 output := android.PathForModuleOut(ctx, b.installFileName())
312 builder := android.NewRuleBuilder(pctx, ctx)
313 builder.Command().Text("cp").Input(unsignedImage).Output(output)
314 cmd := builder.Command().BuiltTool("avbtool").
315 Text("add_hash_footer").
316 FlagWithInput("--image ", output)
317
318 if b.properties.Partition_size != nil {
319 cmd.FlagWithArg("--partition_size ", strconv.FormatInt(*b.properties.Partition_size, 10))
320 } else {
321 cmd.Flag("--dynamic_partition_size")
322 }
323
324 if kernel != nil {
325 cmd.Textf(`--salt $(sha256sum "%s" | cut -d " " -f 1)`, kernel.String())
326 cmd.Implicit(kernel)
327 }
328
329 cmd.FlagWithArg("--partition_name ", b.bootImageType.String())
330
331 if !b.bootImageType.isVendorBoot() {
332 cmd.FlagWithArg("--prop ", proptools.NinjaAndShellEscape(fmt.Sprintf(
333 "com.android.build.%s.os_version:%s", b.bootImageType.String(), ctx.Config().PlatformVersionLastStable())))
334 }
335
336 fingerprintFile := ctx.Config().BuildFingerprintFile(ctx)
337 cmd.FlagWithArg("--prop ", fmt.Sprintf("com.android.build.%s.fingerprint:%s", b.bootImageType.String(), fingerprintFile.String()))
338 cmd.OrderOnly(fingerprintFile)
339
340 if b.properties.Security_patch != nil {
341 cmd.FlagWithArg("--prop ", proptools.NinjaAndShellEscape(fmt.Sprintf(
342 "com.android.build.%s.security_patch:%s", b.bootImageType.String(), *b.properties.Security_patch)))
343 }
344
345 builder.Build("add_avb_footer", fmt.Sprintf("Adding avb footer to %s", b.BaseModuleName()))
346 return output
347}
348
Cole Faust4e9f5922024-11-13 16:09:23 -0800349func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.Path) android.Path {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900350 propFile, toolDeps := b.buildPropFile(ctx)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900351
Cole Faust4e9f5922024-11-13 16:09:23 -0800352 output := android.PathForModuleOut(ctx, b.installFileName())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900353 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900354 builder.Command().Text("cp").Input(unsignedImage).Output(output)
Jiyong Parkac4076d2021-03-15 23:21:30 +0900355 builder.Command().BuiltTool("verity_utils").
356 Input(propFile).
357 Implicits(toolDeps).
358 Output(output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900359
360 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName()))
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900361 return output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900362}
363
Jooyung Han65f402b2022-04-21 14:24:04 +0900364// Calculates avb_salt from some input for deterministic output.
365func (b *bootimg) salt() string {
366 var input []string
367 input = append(input, b.properties.Cmdline...)
368 input = append(input, proptools.StringDefault(b.properties.Partition_name, b.Name()))
369 input = append(input, proptools.String(b.properties.Header_version))
370 return sha1sum(input)
371}
372
Cole Faust4e9f5922024-11-13 16:09:23 -0800373func (b *bootimg) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900374 var sb strings.Builder
375 var deps android.Paths
376 addStr := func(name string, value string) {
377 fmt.Fprintf(&sb, "%s=%s\n", name, value)
378 }
379 addPath := func(name string, path android.Path) {
380 addStr(name, path.String())
381 deps = append(deps, path)
382 }
383
384 addStr("avb_hash_enable", "true")
385 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
386 algorithm := proptools.StringDefault(b.properties.Avb_algorithm, "SHA256_RSA4096")
387 addStr("avb_algorithm", algorithm)
388 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key))
389 addPath("avb_key_path", key)
390 addStr("avb_add_hash_footer_args", "") // TODO(jiyong): add --rollback_index
391 partitionName := proptools.StringDefault(b.properties.Partition_name, b.Name())
392 addStr("partition_name", partitionName)
Jooyung Han65f402b2022-04-21 14:24:04 +0900393 addStr("avb_salt", b.salt())
Jiyong Parkac4076d2021-03-15 23:21:30 +0900394
Cole Faust4e9f5922024-11-13 16:09:23 -0800395 propFile := android.PathForModuleOut(ctx, "prop")
Jiyong Parkac4076d2021-03-15 23:21:30 +0900396 android.WriteFileRule(ctx, propFile, sb.String())
397 return propFile, deps
398}
399
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900400var _ android.AndroidMkEntriesProvider = (*bootimg)(nil)
401
402// Implements android.AndroidMkEntriesProvider
403func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries {
404 return []android.AndroidMkEntries{android.AndroidMkEntries{
405 Class: "ETC",
406 OutputFile: android.OptionalPathForPath(b.output),
407 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700408 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800409 entries.SetString("LOCAL_MODULE_PATH", b.installDir.String())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900410 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName())
411 },
412 },
413 }}
414}
415
416var _ Filesystem = (*bootimg)(nil)
417
418func (b *bootimg) OutputPath() android.Path {
419 return b.output
420}
Jiyong Parkb0eb3192021-03-09 20:29:07 +0900421
Jiyong Park972e06c2021-03-15 23:32:49 +0900422func (b *bootimg) SignedOutputPath() android.Path {
423 if proptools.Bool(b.properties.Use_avb) {
424 return b.OutputPath()
425 }
426 return nil
427}