blob: ab91eb4fa1e645a78361e398bcdd647a010a14cf [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
Jiyong Park16e77a92021-08-30 18:43:19 +090063 // Optional kernel commandline arguments
64 Cmdline []string `android:"arch_variant"`
Jiyong Park1f7b93e2021-02-01 21:38:11 +090065
Jiyong Park81aea9a2021-03-05 18:58:29 +090066 // File that contains bootconfig parameters. This can be set only when `vendor_boot` is true
67 // and `header_version` is greater than or equal to 4.
68 Bootconfig *string `android:"arch_variant,path"`
69
Jiyong Park1f7b93e2021-02-01 21:38:11 +090070 // When set to true, sign the image with avbtool. Default is false.
71 Use_avb *bool
72
73 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
74 Partition_name *string
75
76 // Path to the private key that avbtool will use to sign this filesystem image.
77 // TODO(jiyong): allow apex_key to be specified here
Cole Faust65cb40a2024-10-21 15:41:42 -070078 Avb_private_key *string `android:"path_device_first"`
Jiyong Park1f7b93e2021-02-01 21:38:11 +090079
80 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
81 Avb_algorithm *string
82}
83
84// bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb.
Cole Faustf2a6e8b2024-11-14 10:54:48 -080085func BootimgFactory() android.Module {
Jiyong Park1f7b93e2021-02-01 21:38:11 +090086 module := &bootimg{}
87 module.AddProperties(&module.properties)
88 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
89 return module
90}
91
92type bootimgDep struct {
93 blueprint.BaseDependencyTag
94 kind string
95}
96
97var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"}
98
99func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) {
100 ramdisk := proptools.String(b.properties.Ramdisk_module)
101 if ramdisk != "" {
102 ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk)
103 }
104}
105
106func (b *bootimg) installFileName() string {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900107 return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900108}
109
110func (b *bootimg) partitionName() string {
111 return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName())
112}
113
114func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900115 vendor := proptools.Bool(b.properties.Vendor_boot)
116 unsignedOutput := b.buildBootImage(ctx, vendor)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900117
Cole Faust4e9f5922024-11-13 16:09:23 -0800118 output := unsignedOutput
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900119 if proptools.Bool(b.properties.Use_avb) {
Cole Faust4e9f5922024-11-13 16:09:23 -0800120 output = b.signImage(ctx, unsignedOutput)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900121 }
122
123 b.installDir = android.PathForModuleInstall(ctx, "etc")
Cole Faust4e9f5922024-11-13 16:09:23 -0800124 ctx.InstallFile(b.installDir, b.installFileName(), output)
mrziwang555d1332024-06-07 11:15:33 -0700125
Cole Faust4e9f5922024-11-13 16:09:23 -0800126 ctx.SetOutputFiles([]android.Path{output}, "")
127 b.output = output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900128}
129
Cole Faust4e9f5922024-11-13 16:09:23 -0800130func (b *bootimg) buildBootImage(ctx android.ModuleContext, vendor bool) android.Path {
131 output := android.PathForModuleOut(ctx, "unsigned", b.installFileName())
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900132
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900133 builder := android.NewRuleBuilder(pctx, ctx)
134 cmd := builder.Command().BuiltTool("mkbootimg")
135
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900136 kernel := proptools.String(b.properties.Kernel_prebuilt)
137 if vendor && kernel != "" {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900138 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
139 return output
140 }
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900141 if !vendor && kernel == "" {
142 ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel")
143 return output
144 }
145 if kernel != "" {
146 cmd.FlagWithInput("--kernel ", android.PathForModuleSrc(ctx, kernel))
147 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900148
Cole Faust1c9c3352024-11-19 11:34:44 -0800149 if !vendor {
150 cmd.FlagWithArg("--os_version ", ctx.Config().PlatformVersionLastStable())
151 cmd.FlagWithArg("--os_patch_level ", ctx.Config().PlatformSecurityPatch())
152 }
153
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900154 dtbName := proptools.String(b.properties.Dtb_prebuilt)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900155 if dtbName != "" {
156 dtb := android.PathForModuleSrc(ctx, dtbName)
157 cmd.FlagWithInput("--dtb ", dtb)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900158 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900159
Jiyong Park16e77a92021-08-30 18:43:19 +0900160 cmdline := strings.Join(b.properties.Cmdline, " ")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900161 if cmdline != "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900162 flag := "--cmdline "
163 if vendor {
164 flag = "--vendor_cmdline "
165 }
Jooyung Han32cddd02021-03-08 20:54:16 +0900166 cmd.FlagWithArg(flag, proptools.ShellEscapeIncludingSpaces(cmdline))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900167 }
168
169 headerVersion := proptools.String(b.properties.Header_version)
170 if headerVersion == "" {
171 ctx.PropertyErrorf("header_version", "must be set")
172 return output
173 }
174 verNum, err := strconv.Atoi(headerVersion)
175 if err != nil {
176 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion)
177 return output
178 }
179 if verNum < 3 {
180 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot")
181 return output
182 }
183 cmd.FlagWithArg("--header_version ", headerVersion)
184
185 ramdiskName := proptools.String(b.properties.Ramdisk_module)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900186 if ramdiskName != "" {
187 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep)
188 if filesystem, ok := ramdisk.(*filesystem); ok {
189 flag := "--ramdisk "
190 if vendor {
191 flag = "--vendor_ramdisk "
192 }
193 cmd.FlagWithInput(flag, filesystem.OutputPath())
194 } else {
195 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name())
196 return output
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900197 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900198 }
199
Jiyong Park81aea9a2021-03-05 18:58:29 +0900200 bootconfig := proptools.String(b.properties.Bootconfig)
201 if bootconfig != "" {
202 if !vendor {
203 ctx.PropertyErrorf("bootconfig", "requires vendor_boot: true")
204 return output
205 }
206 if verNum < 4 {
207 ctx.PropertyErrorf("bootconfig", "requires header_version: 4 or later")
208 return output
209 }
210 cmd.FlagWithInput("--vendor_bootconfig ", android.PathForModuleSrc(ctx, bootconfig))
211 }
212
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900213 flag := "--output "
214 if vendor {
215 flag = "--vendor_boot "
216 }
217 cmd.FlagWithOutput(flag, output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900218
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900219 builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName()))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900220 return output
221}
222
Cole Faust4e9f5922024-11-13 16:09:23 -0800223func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.Path) android.Path {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900224 propFile, toolDeps := b.buildPropFile(ctx)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900225
Cole Faust4e9f5922024-11-13 16:09:23 -0800226 output := android.PathForModuleOut(ctx, b.installFileName())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900227 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900228 builder.Command().Text("cp").Input(unsignedImage).Output(output)
Jiyong Parkac4076d2021-03-15 23:21:30 +0900229 builder.Command().BuiltTool("verity_utils").
230 Input(propFile).
231 Implicits(toolDeps).
232 Output(output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900233
234 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName()))
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900235 return output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900236}
237
Jooyung Han65f402b2022-04-21 14:24:04 +0900238// Calculates avb_salt from some input for deterministic output.
239func (b *bootimg) salt() string {
240 var input []string
241 input = append(input, b.properties.Cmdline...)
242 input = append(input, proptools.StringDefault(b.properties.Partition_name, b.Name()))
243 input = append(input, proptools.String(b.properties.Header_version))
244 return sha1sum(input)
245}
246
Cole Faust4e9f5922024-11-13 16:09:23 -0800247func (b *bootimg) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900248 var sb strings.Builder
249 var deps android.Paths
250 addStr := func(name string, value string) {
251 fmt.Fprintf(&sb, "%s=%s\n", name, value)
252 }
253 addPath := func(name string, path android.Path) {
254 addStr(name, path.String())
255 deps = append(deps, path)
256 }
257
258 addStr("avb_hash_enable", "true")
259 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
260 algorithm := proptools.StringDefault(b.properties.Avb_algorithm, "SHA256_RSA4096")
261 addStr("avb_algorithm", algorithm)
262 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key))
263 addPath("avb_key_path", key)
264 addStr("avb_add_hash_footer_args", "") // TODO(jiyong): add --rollback_index
265 partitionName := proptools.StringDefault(b.properties.Partition_name, b.Name())
266 addStr("partition_name", partitionName)
Jooyung Han65f402b2022-04-21 14:24:04 +0900267 addStr("avb_salt", b.salt())
Jiyong Parkac4076d2021-03-15 23:21:30 +0900268
Cole Faust4e9f5922024-11-13 16:09:23 -0800269 propFile := android.PathForModuleOut(ctx, "prop")
Jiyong Parkac4076d2021-03-15 23:21:30 +0900270 android.WriteFileRule(ctx, propFile, sb.String())
271 return propFile, deps
272}
273
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900274var _ android.AndroidMkEntriesProvider = (*bootimg)(nil)
275
276// Implements android.AndroidMkEntriesProvider
277func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries {
278 return []android.AndroidMkEntries{android.AndroidMkEntries{
279 Class: "ETC",
280 OutputFile: android.OptionalPathForPath(b.output),
281 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700282 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800283 entries.SetString("LOCAL_MODULE_PATH", b.installDir.String())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900284 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName())
285 },
286 },
287 }}
288}
289
290var _ Filesystem = (*bootimg)(nil)
291
292func (b *bootimg) OutputPath() android.Path {
293 return b.output
294}
Jiyong Parkb0eb3192021-03-09 20:29:07 +0900295
Jiyong Park972e06c2021-03-15 23:32:49 +0900296func (b *bootimg) SignedOutputPath() android.Path {
297 if proptools.Bool(b.properties.Use_avb) {
298 return b.OutputPath()
299 }
300 return nil
301}