blob: 22495064db6647d8f1fdb546b432ee568684d07a [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
92}
93
Jihoon Kang96fdba92024-11-19 22:25:36 +000094type bootImageType int
95
96const (
97 unsupported bootImageType = iota
98 boot
99 vendorBoot
100 initBoot
101)
102
103func toBootImageType(ctx android.ModuleContext, bootImageType string) bootImageType {
104 switch bootImageType {
105 case "boot":
106 return boot
107 case "vendor_boot":
108 return vendorBoot
109 case "init_boot":
110 return initBoot
111 default:
112 ctx.ModuleErrorf("Unknown boot_image_type %s. Must be one of \"boot\", \"vendor_boot\", or \"init_boot\"", bootImageType)
113 }
114 return unsupported
115}
116
117func (b bootImageType) isBoot() bool {
118 return b == boot
119}
120
121func (b bootImageType) isVendorBoot() bool {
122 return b == vendorBoot
123}
124
125func (b bootImageType) isInitBoot() bool {
126 return b == initBoot
127}
128
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900129// bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb.
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800130func BootimgFactory() android.Module {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900131 module := &bootimg{}
132 module.AddProperties(&module.properties)
133 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
134 return module
135}
136
137type bootimgDep struct {
138 blueprint.BaseDependencyTag
139 kind string
140}
141
142var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"}
143
144func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) {
145 ramdisk := proptools.String(b.properties.Ramdisk_module)
146 if ramdisk != "" {
147 ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk)
148 }
149}
150
151func (b *bootimg) installFileName() string {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900152 return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900153}
154
155func (b *bootimg) partitionName() string {
156 return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName())
157}
158
159func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jihoon Kang96fdba92024-11-19 22:25:36 +0000160 b.bootImageType = toBootImageType(ctx, proptools.StringDefault(b.properties.Boot_image_type, "boot"))
161 unsignedOutput := b.buildBootImage(ctx)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900162
Cole Faust4e9f5922024-11-13 16:09:23 -0800163 output := unsignedOutput
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900164 if proptools.Bool(b.properties.Use_avb) {
Cole Faust4e9f5922024-11-13 16:09:23 -0800165 output = b.signImage(ctx, unsignedOutput)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900166 }
167
168 b.installDir = android.PathForModuleInstall(ctx, "etc")
Cole Faust4e9f5922024-11-13 16:09:23 -0800169 ctx.InstallFile(b.installDir, b.installFileName(), output)
mrziwang555d1332024-06-07 11:15:33 -0700170
Cole Faust4e9f5922024-11-13 16:09:23 -0800171 ctx.SetOutputFiles([]android.Path{output}, "")
172 b.output = output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900173}
174
Jihoon Kang96fdba92024-11-19 22:25:36 +0000175func (b *bootimg) buildBootImage(ctx android.ModuleContext) android.Path {
Cole Faust4e9f5922024-11-13 16:09:23 -0800176 output := android.PathForModuleOut(ctx, "unsigned", b.installFileName())
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900177
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900178 builder := android.NewRuleBuilder(pctx, ctx)
179 cmd := builder.Command().BuiltTool("mkbootimg")
180
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900181 kernel := proptools.String(b.properties.Kernel_prebuilt)
Jihoon Kang96fdba92024-11-19 22:25:36 +0000182 if b.bootImageType.isVendorBoot() && kernel != "" {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900183 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
184 return output
185 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000186
Jihoon Kang96fdba92024-11-19 22:25:36 +0000187 if b.bootImageType.isBoot() && kernel == "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900188 ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel")
189 return output
190 }
191 if kernel != "" {
192 cmd.FlagWithInput("--kernel ", android.PathForModuleSrc(ctx, kernel))
193 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900194
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000195 // These arguments are passed for boot.img and init_boot.img generation
Jihoon Kang96fdba92024-11-19 22:25:36 +0000196 if b.bootImageType.isBoot() || b.bootImageType.isInitBoot() {
Cole Faust1c9c3352024-11-19 11:34:44 -0800197 cmd.FlagWithArg("--os_version ", ctx.Config().PlatformVersionLastStable())
198 cmd.FlagWithArg("--os_patch_level ", ctx.Config().PlatformSecurityPatch())
199 }
200
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900201 dtbName := proptools.String(b.properties.Dtb_prebuilt)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900202 if dtbName != "" {
203 dtb := android.PathForModuleSrc(ctx, dtbName)
204 cmd.FlagWithInput("--dtb ", dtb)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900205 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900206
Jiyong Park16e77a92021-08-30 18:43:19 +0900207 cmdline := strings.Join(b.properties.Cmdline, " ")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900208 if cmdline != "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900209 flag := "--cmdline "
Jihoon Kang96fdba92024-11-19 22:25:36 +0000210 if b.bootImageType.isVendorBoot() {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900211 flag = "--vendor_cmdline "
212 }
Jooyung Han32cddd02021-03-08 20:54:16 +0900213 cmd.FlagWithArg(flag, proptools.ShellEscapeIncludingSpaces(cmdline))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900214 }
215
216 headerVersion := proptools.String(b.properties.Header_version)
217 if headerVersion == "" {
218 ctx.PropertyErrorf("header_version", "must be set")
219 return output
220 }
221 verNum, err := strconv.Atoi(headerVersion)
222 if err != nil {
223 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion)
224 return output
225 }
226 if verNum < 3 {
227 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot")
228 return output
229 }
230 cmd.FlagWithArg("--header_version ", headerVersion)
231
232 ramdiskName := proptools.String(b.properties.Ramdisk_module)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900233 if ramdiskName != "" {
234 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep)
235 if filesystem, ok := ramdisk.(*filesystem); ok {
236 flag := "--ramdisk "
Jihoon Kang96fdba92024-11-19 22:25:36 +0000237 if b.bootImageType.isVendorBoot() {
Jiyong Park393ebfc2022-01-06 14:28:53 +0900238 flag = "--vendor_ramdisk "
239 }
240 cmd.FlagWithInput(flag, filesystem.OutputPath())
241 } else {
242 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name())
243 return output
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900244 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900245 }
246
Jiyong Park81aea9a2021-03-05 18:58:29 +0900247 bootconfig := proptools.String(b.properties.Bootconfig)
248 if bootconfig != "" {
Jihoon Kang96fdba92024-11-19 22:25:36 +0000249 if !b.bootImageType.isVendorBoot() {
Jiyong Park81aea9a2021-03-05 18:58:29 +0900250 ctx.PropertyErrorf("bootconfig", "requires vendor_boot: true")
251 return output
252 }
253 if verNum < 4 {
254 ctx.PropertyErrorf("bootconfig", "requires header_version: 4 or later")
255 return output
256 }
257 cmd.FlagWithInput("--vendor_bootconfig ", android.PathForModuleSrc(ctx, bootconfig))
258 }
259
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000260 // Output flag for boot.img and init_boot.img
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900261 flag := "--output "
Jihoon Kang96fdba92024-11-19 22:25:36 +0000262 if b.bootImageType.isVendorBoot() {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900263 flag = "--vendor_boot "
264 }
265 cmd.FlagWithOutput(flag, output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900266
Cole Faust26bdac52024-11-19 13:37:53 -0800267 if b.properties.Partition_size != nil {
268 assertMaxImageSize(builder, output, *b.properties.Partition_size, proptools.Bool(b.properties.Use_avb))
269 }
270
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900271 builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName()))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900272 return output
273}
274
Cole Faust4e9f5922024-11-13 16:09:23 -0800275func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.Path) android.Path {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900276 propFile, toolDeps := b.buildPropFile(ctx)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900277
Cole Faust4e9f5922024-11-13 16:09:23 -0800278 output := android.PathForModuleOut(ctx, b.installFileName())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900279 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900280 builder.Command().Text("cp").Input(unsignedImage).Output(output)
Jiyong Parkac4076d2021-03-15 23:21:30 +0900281 builder.Command().BuiltTool("verity_utils").
282 Input(propFile).
283 Implicits(toolDeps).
284 Output(output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900285
286 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName()))
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900287 return output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900288}
289
Jooyung Han65f402b2022-04-21 14:24:04 +0900290// Calculates avb_salt from some input for deterministic output.
291func (b *bootimg) salt() string {
292 var input []string
293 input = append(input, b.properties.Cmdline...)
294 input = append(input, proptools.StringDefault(b.properties.Partition_name, b.Name()))
295 input = append(input, proptools.String(b.properties.Header_version))
296 return sha1sum(input)
297}
298
Cole Faust4e9f5922024-11-13 16:09:23 -0800299func (b *bootimg) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900300 var sb strings.Builder
301 var deps android.Paths
302 addStr := func(name string, value string) {
303 fmt.Fprintf(&sb, "%s=%s\n", name, value)
304 }
305 addPath := func(name string, path android.Path) {
306 addStr(name, path.String())
307 deps = append(deps, path)
308 }
309
310 addStr("avb_hash_enable", "true")
311 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
312 algorithm := proptools.StringDefault(b.properties.Avb_algorithm, "SHA256_RSA4096")
313 addStr("avb_algorithm", algorithm)
314 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key))
315 addPath("avb_key_path", key)
316 addStr("avb_add_hash_footer_args", "") // TODO(jiyong): add --rollback_index
317 partitionName := proptools.StringDefault(b.properties.Partition_name, b.Name())
318 addStr("partition_name", partitionName)
Jooyung Han65f402b2022-04-21 14:24:04 +0900319 addStr("avb_salt", b.salt())
Jiyong Parkac4076d2021-03-15 23:21:30 +0900320
Cole Faust4e9f5922024-11-13 16:09:23 -0800321 propFile := android.PathForModuleOut(ctx, "prop")
Jiyong Parkac4076d2021-03-15 23:21:30 +0900322 android.WriteFileRule(ctx, propFile, sb.String())
323 return propFile, deps
324}
325
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900326var _ android.AndroidMkEntriesProvider = (*bootimg)(nil)
327
328// Implements android.AndroidMkEntriesProvider
329func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries {
330 return []android.AndroidMkEntries{android.AndroidMkEntries{
331 Class: "ETC",
332 OutputFile: android.OptionalPathForPath(b.output),
333 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700334 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800335 entries.SetString("LOCAL_MODULE_PATH", b.installDir.String())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900336 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName())
337 },
338 },
339 }}
340}
341
342var _ Filesystem = (*bootimg)(nil)
343
344func (b *bootimg) OutputPath() android.Path {
345 return b.output
346}
Jiyong Parkb0eb3192021-03-09 20:29:07 +0900347
Jiyong Park972e06c2021-03-15 23:32:49 +0900348func (b *bootimg) SignedOutputPath() android.Path {
349 if proptools.Bool(b.properties.Use_avb) {
350 return b.OutputPath()
351 }
352 return nil
353}