blob: 13c6a46fe0211d2366459f618cd7dca2abd589a0 [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
Jiyong Park1f7b93e2021-02-01 21:38:11 +090076 // When set to true, sign the image with avbtool. Default is false.
77 Use_avb *bool
78
79 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
80 Partition_name *string
81
82 // Path to the private key that avbtool will use to sign this filesystem image.
83 // TODO(jiyong): allow apex_key to be specified here
Cole Faust65cb40a2024-10-21 15:41:42 -070084 Avb_private_key *string `android:"path_device_first"`
Jiyong Park1f7b93e2021-02-01 21:38:11 +090085
86 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
87 Avb_algorithm *string
88}
89
Jihoon Kang96fdba92024-11-19 22:25:36 +000090type bootImageType int
91
92const (
93 unsupported bootImageType = iota
94 boot
95 vendorBoot
96 initBoot
97)
98
99func toBootImageType(ctx android.ModuleContext, bootImageType string) bootImageType {
100 switch bootImageType {
101 case "boot":
102 return boot
103 case "vendor_boot":
104 return vendorBoot
105 case "init_boot":
106 return initBoot
107 default:
108 ctx.ModuleErrorf("Unknown boot_image_type %s. Must be one of \"boot\", \"vendor_boot\", or \"init_boot\"", bootImageType)
109 }
110 return unsupported
111}
112
113func (b bootImageType) isBoot() bool {
114 return b == boot
115}
116
117func (b bootImageType) isVendorBoot() bool {
118 return b == vendorBoot
119}
120
121func (b bootImageType) isInitBoot() bool {
122 return b == initBoot
123}
124
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900125// bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb.
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800126func BootimgFactory() android.Module {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900127 module := &bootimg{}
128 module.AddProperties(&module.properties)
129 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
130 return module
131}
132
133type bootimgDep struct {
134 blueprint.BaseDependencyTag
135 kind string
136}
137
138var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"}
139
140func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) {
141 ramdisk := proptools.String(b.properties.Ramdisk_module)
142 if ramdisk != "" {
143 ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk)
144 }
145}
146
147func (b *bootimg) installFileName() string {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900148 return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900149}
150
151func (b *bootimg) partitionName() string {
152 return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName())
153}
154
155func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jihoon Kang96fdba92024-11-19 22:25:36 +0000156 b.bootImageType = toBootImageType(ctx, proptools.StringDefault(b.properties.Boot_image_type, "boot"))
157 unsignedOutput := b.buildBootImage(ctx)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900158
Cole Faust4e9f5922024-11-13 16:09:23 -0800159 output := unsignedOutput
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900160 if proptools.Bool(b.properties.Use_avb) {
Cole Faust4e9f5922024-11-13 16:09:23 -0800161 output = b.signImage(ctx, unsignedOutput)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900162 }
163
164 b.installDir = android.PathForModuleInstall(ctx, "etc")
Cole Faust4e9f5922024-11-13 16:09:23 -0800165 ctx.InstallFile(b.installDir, b.installFileName(), output)
mrziwang555d1332024-06-07 11:15:33 -0700166
Cole Faust4e9f5922024-11-13 16:09:23 -0800167 ctx.SetOutputFiles([]android.Path{output}, "")
168 b.output = output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900169}
170
Jihoon Kang96fdba92024-11-19 22:25:36 +0000171func (b *bootimg) buildBootImage(ctx android.ModuleContext) android.Path {
Cole Faust4e9f5922024-11-13 16:09:23 -0800172 output := android.PathForModuleOut(ctx, "unsigned", b.installFileName())
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900173
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900174 builder := android.NewRuleBuilder(pctx, ctx)
175 cmd := builder.Command().BuiltTool("mkbootimg")
176
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900177 kernel := proptools.String(b.properties.Kernel_prebuilt)
Jihoon Kang96fdba92024-11-19 22:25:36 +0000178 if b.bootImageType.isVendorBoot() && kernel != "" {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900179 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
180 return output
181 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000182
Jihoon Kang96fdba92024-11-19 22:25:36 +0000183 if b.bootImageType.isBoot() && kernel == "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900184 ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel")
185 return output
186 }
187 if kernel != "" {
188 cmd.FlagWithInput("--kernel ", android.PathForModuleSrc(ctx, kernel))
189 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900190
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000191 // These arguments are passed for boot.img and init_boot.img generation
Jihoon Kang96fdba92024-11-19 22:25:36 +0000192 if b.bootImageType.isBoot() || b.bootImageType.isInitBoot() {
Cole Faust1c9c3352024-11-19 11:34:44 -0800193 cmd.FlagWithArg("--os_version ", ctx.Config().PlatformVersionLastStable())
194 cmd.FlagWithArg("--os_patch_level ", ctx.Config().PlatformSecurityPatch())
195 }
196
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900197 dtbName := proptools.String(b.properties.Dtb_prebuilt)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900198 if dtbName != "" {
199 dtb := android.PathForModuleSrc(ctx, dtbName)
200 cmd.FlagWithInput("--dtb ", dtb)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900201 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900202
Jiyong Park16e77a92021-08-30 18:43:19 +0900203 cmdline := strings.Join(b.properties.Cmdline, " ")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900204 if cmdline != "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900205 flag := "--cmdline "
Jihoon Kang96fdba92024-11-19 22:25:36 +0000206 if b.bootImageType.isVendorBoot() {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900207 flag = "--vendor_cmdline "
208 }
Jooyung Han32cddd02021-03-08 20:54:16 +0900209 cmd.FlagWithArg(flag, proptools.ShellEscapeIncludingSpaces(cmdline))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900210 }
211
212 headerVersion := proptools.String(b.properties.Header_version)
213 if headerVersion == "" {
214 ctx.PropertyErrorf("header_version", "must be set")
215 return output
216 }
217 verNum, err := strconv.Atoi(headerVersion)
218 if err != nil {
219 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion)
220 return output
221 }
222 if verNum < 3 {
223 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot")
224 return output
225 }
226 cmd.FlagWithArg("--header_version ", headerVersion)
227
228 ramdiskName := proptools.String(b.properties.Ramdisk_module)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900229 if ramdiskName != "" {
230 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep)
231 if filesystem, ok := ramdisk.(*filesystem); ok {
232 flag := "--ramdisk "
Jihoon Kang96fdba92024-11-19 22:25:36 +0000233 if b.bootImageType.isVendorBoot() {
Jiyong Park393ebfc2022-01-06 14:28:53 +0900234 flag = "--vendor_ramdisk "
235 }
236 cmd.FlagWithInput(flag, filesystem.OutputPath())
237 } else {
238 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name())
239 return output
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900240 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900241 }
242
Jiyong Park81aea9a2021-03-05 18:58:29 +0900243 bootconfig := proptools.String(b.properties.Bootconfig)
244 if bootconfig != "" {
Jihoon Kang96fdba92024-11-19 22:25:36 +0000245 if !b.bootImageType.isVendorBoot() {
Jiyong Park81aea9a2021-03-05 18:58:29 +0900246 ctx.PropertyErrorf("bootconfig", "requires vendor_boot: true")
247 return output
248 }
249 if verNum < 4 {
250 ctx.PropertyErrorf("bootconfig", "requires header_version: 4 or later")
251 return output
252 }
253 cmd.FlagWithInput("--vendor_bootconfig ", android.PathForModuleSrc(ctx, bootconfig))
254 }
255
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000256 // Output flag for boot.img and init_boot.img
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900257 flag := "--output "
Jihoon Kang96fdba92024-11-19 22:25:36 +0000258 if b.bootImageType.isVendorBoot() {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900259 flag = "--vendor_boot "
260 }
261 cmd.FlagWithOutput(flag, output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900262
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900263 builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName()))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900264 return output
265}
266
Cole Faust4e9f5922024-11-13 16:09:23 -0800267func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.Path) android.Path {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900268 propFile, toolDeps := b.buildPropFile(ctx)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900269
Cole Faust4e9f5922024-11-13 16:09:23 -0800270 output := android.PathForModuleOut(ctx, b.installFileName())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900271 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900272 builder.Command().Text("cp").Input(unsignedImage).Output(output)
Jiyong Parkac4076d2021-03-15 23:21:30 +0900273 builder.Command().BuiltTool("verity_utils").
274 Input(propFile).
275 Implicits(toolDeps).
276 Output(output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900277
278 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName()))
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900279 return output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900280}
281
Jooyung Han65f402b2022-04-21 14:24:04 +0900282// Calculates avb_salt from some input for deterministic output.
283func (b *bootimg) salt() string {
284 var input []string
285 input = append(input, b.properties.Cmdline...)
286 input = append(input, proptools.StringDefault(b.properties.Partition_name, b.Name()))
287 input = append(input, proptools.String(b.properties.Header_version))
288 return sha1sum(input)
289}
290
Cole Faust4e9f5922024-11-13 16:09:23 -0800291func (b *bootimg) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900292 var sb strings.Builder
293 var deps android.Paths
294 addStr := func(name string, value string) {
295 fmt.Fprintf(&sb, "%s=%s\n", name, value)
296 }
297 addPath := func(name string, path android.Path) {
298 addStr(name, path.String())
299 deps = append(deps, path)
300 }
301
302 addStr("avb_hash_enable", "true")
303 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
304 algorithm := proptools.StringDefault(b.properties.Avb_algorithm, "SHA256_RSA4096")
305 addStr("avb_algorithm", algorithm)
306 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key))
307 addPath("avb_key_path", key)
308 addStr("avb_add_hash_footer_args", "") // TODO(jiyong): add --rollback_index
309 partitionName := proptools.StringDefault(b.properties.Partition_name, b.Name())
310 addStr("partition_name", partitionName)
Jooyung Han65f402b2022-04-21 14:24:04 +0900311 addStr("avb_salt", b.salt())
Jiyong Parkac4076d2021-03-15 23:21:30 +0900312
Cole Faust4e9f5922024-11-13 16:09:23 -0800313 propFile := android.PathForModuleOut(ctx, "prop")
Jiyong Parkac4076d2021-03-15 23:21:30 +0900314 android.WriteFileRule(ctx, propFile, sb.String())
315 return propFile, deps
316}
317
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900318var _ android.AndroidMkEntriesProvider = (*bootimg)(nil)
319
320// Implements android.AndroidMkEntriesProvider
321func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries {
322 return []android.AndroidMkEntries{android.AndroidMkEntries{
323 Class: "ETC",
324 OutputFile: android.OptionalPathForPath(b.output),
325 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700326 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800327 entries.SetString("LOCAL_MODULE_PATH", b.installDir.String())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900328 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName())
329 },
330 },
331 }}
332}
333
334var _ Filesystem = (*bootimg)(nil)
335
336func (b *bootimg) OutputPath() android.Path {
337 return b.output
338}
Jiyong Parkb0eb3192021-03-09 20:29:07 +0900339
Jiyong Park972e06c2021-03-15 23:32:49 +0900340func (b *bootimg) SignedOutputPath() android.Path {
341 if proptools.Bool(b.properties.Use_avb) {
342 return b.OutputPath()
343 }
344 return nil
345}