blob: 40b9ce6ce6d3e70cc0bd10b878cf61559c8fe4b3 [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
39}
40
Cole Faustf2a6e8b2024-11-14 10:54:48 -080041type BootimgProperties struct {
Jiyong Park1f55dbd2021-02-15 17:57:35 +090042 // Set the name of the output. Defaults to <module_name>.img.
43 Stem *string
44
Jiyong Park1f7b93e2021-02-01 21:38:11 +090045 // Path to the linux kernel prebuilt file
46 Kernel_prebuilt *string `android:"arch_variant,path"`
47
48 // Filesystem module that is used as ramdisk
49 Ramdisk_module *string
50
51 // Path to the device tree blob (DTB) prebuilt file to add to this boot image
52 Dtb_prebuilt *string `android:"arch_variant,path"`
53
54 // Header version number. Must be set to one of the version numbers that are currently
55 // supported. Refer to
56 // https://source.android.com/devices/bootloader/boot-image-header
57 Header_version *string
58
59 // Determines if this image is for the vendor_boot partition. Default is false. Refer to
60 // https://source.android.com/devices/bootloader/partitions/vendor-boot-partitions
61 Vendor_boot *bool
62
Jihoon Kang95eb1da2024-11-19 20:55:20 +000063 // Determines if this image is for the init_boot partition. Default is false.
64 Init_boot *bool
65
Jiyong Park16e77a92021-08-30 18:43:19 +090066 // Optional kernel commandline arguments
67 Cmdline []string `android:"arch_variant"`
Jiyong Park1f7b93e2021-02-01 21:38:11 +090068
Jiyong Park81aea9a2021-03-05 18:58:29 +090069 // File that contains bootconfig parameters. This can be set only when `vendor_boot` is true
70 // and `header_version` is greater than or equal to 4.
71 Bootconfig *string `android:"arch_variant,path"`
72
Jiyong Park1f7b93e2021-02-01 21:38:11 +090073 // When set to true, sign the image with avbtool. Default is false.
74 Use_avb *bool
75
76 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
77 Partition_name *string
78
79 // Path to the private key that avbtool will use to sign this filesystem image.
80 // TODO(jiyong): allow apex_key to be specified here
Cole Faust65cb40a2024-10-21 15:41:42 -070081 Avb_private_key *string `android:"path_device_first"`
Jiyong Park1f7b93e2021-02-01 21:38:11 +090082
83 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
84 Avb_algorithm *string
85}
86
87// bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb.
Cole Faustf2a6e8b2024-11-14 10:54:48 -080088func BootimgFactory() android.Module {
Jiyong Park1f7b93e2021-02-01 21:38:11 +090089 module := &bootimg{}
90 module.AddProperties(&module.properties)
91 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
92 return module
93}
94
95type bootimgDep struct {
96 blueprint.BaseDependencyTag
97 kind string
98}
99
100var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"}
101
102func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) {
103 ramdisk := proptools.String(b.properties.Ramdisk_module)
104 if ramdisk != "" {
105 ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk)
106 }
107}
108
109func (b *bootimg) installFileName() string {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900110 return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900111}
112
113func (b *bootimg) partitionName() string {
114 return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName())
115}
116
117func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900118 vendor := proptools.Bool(b.properties.Vendor_boot)
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000119 init := proptools.Bool(b.properties.Init_boot)
120
121 if vendor && init {
122 ctx.ModuleErrorf("vendor_boot and init_boot cannot be both set to true")
123 }
124
125 unsignedOutput := b.buildBootImage(ctx, vendor, init)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900126
Cole Faust4e9f5922024-11-13 16:09:23 -0800127 output := unsignedOutput
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900128 if proptools.Bool(b.properties.Use_avb) {
Cole Faust4e9f5922024-11-13 16:09:23 -0800129 output = b.signImage(ctx, unsignedOutput)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900130 }
131
132 b.installDir = android.PathForModuleInstall(ctx, "etc")
Cole Faust4e9f5922024-11-13 16:09:23 -0800133 ctx.InstallFile(b.installDir, b.installFileName(), output)
mrziwang555d1332024-06-07 11:15:33 -0700134
Cole Faust4e9f5922024-11-13 16:09:23 -0800135 ctx.SetOutputFiles([]android.Path{output}, "")
136 b.output = output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900137}
138
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000139func (b *bootimg) buildBootImage(ctx android.ModuleContext, vendor bool, init bool) android.Path {
Cole Faust4e9f5922024-11-13 16:09:23 -0800140 output := android.PathForModuleOut(ctx, "unsigned", b.installFileName())
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900141
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900142 builder := android.NewRuleBuilder(pctx, ctx)
143 cmd := builder.Command().BuiltTool("mkbootimg")
144
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900145 kernel := proptools.String(b.properties.Kernel_prebuilt)
146 if vendor && kernel != "" {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900147 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
148 return output
149 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000150
151 buildingBoot := !vendor && !init
152 if buildingBoot && kernel == "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900153 ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel")
154 return output
155 }
156 if kernel != "" {
157 cmd.FlagWithInput("--kernel ", android.PathForModuleSrc(ctx, kernel))
158 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900159
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000160 // These arguments are passed for boot.img and init_boot.img generation
Cole Faust1c9c3352024-11-19 11:34:44 -0800161 if !vendor {
162 cmd.FlagWithArg("--os_version ", ctx.Config().PlatformVersionLastStable())
163 cmd.FlagWithArg("--os_patch_level ", ctx.Config().PlatformSecurityPatch())
164 }
165
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900166 dtbName := proptools.String(b.properties.Dtb_prebuilt)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900167 if dtbName != "" {
168 dtb := android.PathForModuleSrc(ctx, dtbName)
169 cmd.FlagWithInput("--dtb ", dtb)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900170 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900171
Jiyong Park16e77a92021-08-30 18:43:19 +0900172 cmdline := strings.Join(b.properties.Cmdline, " ")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900173 if cmdline != "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900174 flag := "--cmdline "
175 if vendor {
176 flag = "--vendor_cmdline "
177 }
Jooyung Han32cddd02021-03-08 20:54:16 +0900178 cmd.FlagWithArg(flag, proptools.ShellEscapeIncludingSpaces(cmdline))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900179 }
180
181 headerVersion := proptools.String(b.properties.Header_version)
182 if headerVersion == "" {
183 ctx.PropertyErrorf("header_version", "must be set")
184 return output
185 }
186 verNum, err := strconv.Atoi(headerVersion)
187 if err != nil {
188 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion)
189 return output
190 }
191 if verNum < 3 {
192 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot")
193 return output
194 }
195 cmd.FlagWithArg("--header_version ", headerVersion)
196
197 ramdiskName := proptools.String(b.properties.Ramdisk_module)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900198 if ramdiskName != "" {
199 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep)
200 if filesystem, ok := ramdisk.(*filesystem); ok {
201 flag := "--ramdisk "
202 if vendor {
203 flag = "--vendor_ramdisk "
204 }
205 cmd.FlagWithInput(flag, filesystem.OutputPath())
206 } else {
207 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name())
208 return output
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900209 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900210 }
211
Jiyong Park81aea9a2021-03-05 18:58:29 +0900212 bootconfig := proptools.String(b.properties.Bootconfig)
213 if bootconfig != "" {
214 if !vendor {
215 ctx.PropertyErrorf("bootconfig", "requires vendor_boot: true")
216 return output
217 }
218 if verNum < 4 {
219 ctx.PropertyErrorf("bootconfig", "requires header_version: 4 or later")
220 return output
221 }
222 cmd.FlagWithInput("--vendor_bootconfig ", android.PathForModuleSrc(ctx, bootconfig))
223 }
224
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000225 // Output flag for boot.img and init_boot.img
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900226 flag := "--output "
227 if vendor {
228 flag = "--vendor_boot "
229 }
230 cmd.FlagWithOutput(flag, output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900231
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900232 builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName()))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900233 return output
234}
235
Cole Faust4e9f5922024-11-13 16:09:23 -0800236func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.Path) android.Path {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900237 propFile, toolDeps := b.buildPropFile(ctx)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900238
Cole Faust4e9f5922024-11-13 16:09:23 -0800239 output := android.PathForModuleOut(ctx, b.installFileName())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900240 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900241 builder.Command().Text("cp").Input(unsignedImage).Output(output)
Jiyong Parkac4076d2021-03-15 23:21:30 +0900242 builder.Command().BuiltTool("verity_utils").
243 Input(propFile).
244 Implicits(toolDeps).
245 Output(output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900246
247 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName()))
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900248 return output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900249}
250
Jooyung Han65f402b2022-04-21 14:24:04 +0900251// Calculates avb_salt from some input for deterministic output.
252func (b *bootimg) salt() string {
253 var input []string
254 input = append(input, b.properties.Cmdline...)
255 input = append(input, proptools.StringDefault(b.properties.Partition_name, b.Name()))
256 input = append(input, proptools.String(b.properties.Header_version))
257 return sha1sum(input)
258}
259
Cole Faust4e9f5922024-11-13 16:09:23 -0800260func (b *bootimg) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900261 var sb strings.Builder
262 var deps android.Paths
263 addStr := func(name string, value string) {
264 fmt.Fprintf(&sb, "%s=%s\n", name, value)
265 }
266 addPath := func(name string, path android.Path) {
267 addStr(name, path.String())
268 deps = append(deps, path)
269 }
270
271 addStr("avb_hash_enable", "true")
272 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
273 algorithm := proptools.StringDefault(b.properties.Avb_algorithm, "SHA256_RSA4096")
274 addStr("avb_algorithm", algorithm)
275 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key))
276 addPath("avb_key_path", key)
277 addStr("avb_add_hash_footer_args", "") // TODO(jiyong): add --rollback_index
278 partitionName := proptools.StringDefault(b.properties.Partition_name, b.Name())
279 addStr("partition_name", partitionName)
Jooyung Han65f402b2022-04-21 14:24:04 +0900280 addStr("avb_salt", b.salt())
Jiyong Parkac4076d2021-03-15 23:21:30 +0900281
Cole Faust4e9f5922024-11-13 16:09:23 -0800282 propFile := android.PathForModuleOut(ctx, "prop")
Jiyong Parkac4076d2021-03-15 23:21:30 +0900283 android.WriteFileRule(ctx, propFile, sb.String())
284 return propFile, deps
285}
286
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900287var _ android.AndroidMkEntriesProvider = (*bootimg)(nil)
288
289// Implements android.AndroidMkEntriesProvider
290func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries {
291 return []android.AndroidMkEntries{android.AndroidMkEntries{
292 Class: "ETC",
293 OutputFile: android.OptionalPathForPath(b.output),
294 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700295 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800296 entries.SetString("LOCAL_MODULE_PATH", b.installDir.String())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900297 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName())
298 },
299 },
300 }}
301}
302
303var _ Filesystem = (*bootimg)(nil)
304
305func (b *bootimg) OutputPath() android.Path {
306 return b.output
307}
Jiyong Parkb0eb3192021-03-09 20:29:07 +0900308
Jiyong Park972e06c2021-03-15 23:32:49 +0900309func (b *bootimg) SignedOutputPath() android.Path {
310 if proptools.Bool(b.properties.Use_avb) {
311 return b.OutputPath()
312 }
313 return nil
314}