blob: 5aa60d2ae78ac0f3aff06dda4399cf24a33c20df [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
Cole Faust26bdac52024-11-19 13:37:53 -080070 // The size of the partition on the device. It will be a build error if this built partition
71 // image exceeds this size.
72 Partition_size *int64
73
Jiyong Park1f7b93e2021-02-01 21:38:11 +090074 // When set to true, sign the image with avbtool. Default is false.
75 Use_avb *bool
76
77 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
78 Partition_name *string
79
80 // Path to the private key that avbtool will use to sign this filesystem image.
81 // TODO(jiyong): allow apex_key to be specified here
Cole Faust65cb40a2024-10-21 15:41:42 -070082 Avb_private_key *string `android:"path_device_first"`
Jiyong Park1f7b93e2021-02-01 21:38:11 +090083
84 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
85 Avb_algorithm *string
86}
87
88// bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb.
Cole Faustf2a6e8b2024-11-14 10:54:48 -080089func BootimgFactory() android.Module {
Jiyong Park1f7b93e2021-02-01 21:38:11 +090090 module := &bootimg{}
91 module.AddProperties(&module.properties)
92 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
93 return module
94}
95
96type bootimgDep struct {
97 blueprint.BaseDependencyTag
98 kind string
99}
100
101var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"}
102
103func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) {
104 ramdisk := proptools.String(b.properties.Ramdisk_module)
105 if ramdisk != "" {
106 ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk)
107 }
108}
109
110func (b *bootimg) installFileName() string {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900111 return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900112}
113
114func (b *bootimg) partitionName() string {
115 return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName())
116}
117
118func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900119 vendor := proptools.Bool(b.properties.Vendor_boot)
120 unsignedOutput := b.buildBootImage(ctx, vendor)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900121
Cole Faust4e9f5922024-11-13 16:09:23 -0800122 output := unsignedOutput
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900123 if proptools.Bool(b.properties.Use_avb) {
Cole Faust4e9f5922024-11-13 16:09:23 -0800124 output = b.signImage(ctx, unsignedOutput)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900125 }
126
127 b.installDir = android.PathForModuleInstall(ctx, "etc")
Cole Faust4e9f5922024-11-13 16:09:23 -0800128 ctx.InstallFile(b.installDir, b.installFileName(), output)
mrziwang555d1332024-06-07 11:15:33 -0700129
Cole Faust4e9f5922024-11-13 16:09:23 -0800130 ctx.SetOutputFiles([]android.Path{output}, "")
131 b.output = output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900132}
133
Cole Faust4e9f5922024-11-13 16:09:23 -0800134func (b *bootimg) buildBootImage(ctx android.ModuleContext, vendor bool) android.Path {
135 output := android.PathForModuleOut(ctx, "unsigned", b.installFileName())
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900136
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900137 builder := android.NewRuleBuilder(pctx, ctx)
138 cmd := builder.Command().BuiltTool("mkbootimg")
139
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900140 kernel := proptools.String(b.properties.Kernel_prebuilt)
141 if vendor && kernel != "" {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900142 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
143 return output
144 }
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900145 if !vendor && kernel == "" {
146 ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel")
147 return output
148 }
149 if kernel != "" {
150 cmd.FlagWithInput("--kernel ", android.PathForModuleSrc(ctx, kernel))
151 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900152
Cole Faust1c9c3352024-11-19 11:34:44 -0800153 if !vendor {
154 cmd.FlagWithArg("--os_version ", ctx.Config().PlatformVersionLastStable())
155 cmd.FlagWithArg("--os_patch_level ", ctx.Config().PlatformSecurityPatch())
156 }
157
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900158 dtbName := proptools.String(b.properties.Dtb_prebuilt)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900159 if dtbName != "" {
160 dtb := android.PathForModuleSrc(ctx, dtbName)
161 cmd.FlagWithInput("--dtb ", dtb)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900162 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900163
Jiyong Park16e77a92021-08-30 18:43:19 +0900164 cmdline := strings.Join(b.properties.Cmdline, " ")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900165 if cmdline != "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900166 flag := "--cmdline "
167 if vendor {
168 flag = "--vendor_cmdline "
169 }
Jooyung Han32cddd02021-03-08 20:54:16 +0900170 cmd.FlagWithArg(flag, proptools.ShellEscapeIncludingSpaces(cmdline))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900171 }
172
173 headerVersion := proptools.String(b.properties.Header_version)
174 if headerVersion == "" {
175 ctx.PropertyErrorf("header_version", "must be set")
176 return output
177 }
178 verNum, err := strconv.Atoi(headerVersion)
179 if err != nil {
180 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion)
181 return output
182 }
183 if verNum < 3 {
184 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot")
185 return output
186 }
187 cmd.FlagWithArg("--header_version ", headerVersion)
188
189 ramdiskName := proptools.String(b.properties.Ramdisk_module)
Jiyong Park393ebfc2022-01-06 14:28:53 +0900190 if ramdiskName != "" {
191 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep)
192 if filesystem, ok := ramdisk.(*filesystem); ok {
193 flag := "--ramdisk "
194 if vendor {
195 flag = "--vendor_ramdisk "
196 }
197 cmd.FlagWithInput(flag, filesystem.OutputPath())
198 } else {
199 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name())
200 return output
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900201 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900202 }
203
Jiyong Park81aea9a2021-03-05 18:58:29 +0900204 bootconfig := proptools.String(b.properties.Bootconfig)
205 if bootconfig != "" {
206 if !vendor {
207 ctx.PropertyErrorf("bootconfig", "requires vendor_boot: true")
208 return output
209 }
210 if verNum < 4 {
211 ctx.PropertyErrorf("bootconfig", "requires header_version: 4 or later")
212 return output
213 }
214 cmd.FlagWithInput("--vendor_bootconfig ", android.PathForModuleSrc(ctx, bootconfig))
215 }
216
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900217 flag := "--output "
218 if vendor {
219 flag = "--vendor_boot "
220 }
221 cmd.FlagWithOutput(flag, output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900222
Cole Faust26bdac52024-11-19 13:37:53 -0800223 if b.properties.Partition_size != nil {
224 assertMaxImageSize(builder, output, *b.properties.Partition_size, proptools.Bool(b.properties.Use_avb))
225 }
226
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900227 builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName()))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900228 return output
229}
230
Cole Faust4e9f5922024-11-13 16:09:23 -0800231func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.Path) android.Path {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900232 propFile, toolDeps := b.buildPropFile(ctx)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900233
Cole Faust4e9f5922024-11-13 16:09:23 -0800234 output := android.PathForModuleOut(ctx, b.installFileName())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900235 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900236 builder.Command().Text("cp").Input(unsignedImage).Output(output)
Jiyong Parkac4076d2021-03-15 23:21:30 +0900237 builder.Command().BuiltTool("verity_utils").
238 Input(propFile).
239 Implicits(toolDeps).
240 Output(output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900241
242 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName()))
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900243 return output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900244}
245
Jooyung Han65f402b2022-04-21 14:24:04 +0900246// Calculates avb_salt from some input for deterministic output.
247func (b *bootimg) salt() string {
248 var input []string
249 input = append(input, b.properties.Cmdline...)
250 input = append(input, proptools.StringDefault(b.properties.Partition_name, b.Name()))
251 input = append(input, proptools.String(b.properties.Header_version))
252 return sha1sum(input)
253}
254
Cole Faust4e9f5922024-11-13 16:09:23 -0800255func (b *bootimg) buildPropFile(ctx android.ModuleContext) (android.Path, android.Paths) {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900256 var sb strings.Builder
257 var deps android.Paths
258 addStr := func(name string, value string) {
259 fmt.Fprintf(&sb, "%s=%s\n", name, value)
260 }
261 addPath := func(name string, path android.Path) {
262 addStr(name, path.String())
263 deps = append(deps, path)
264 }
265
266 addStr("avb_hash_enable", "true")
267 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
268 algorithm := proptools.StringDefault(b.properties.Avb_algorithm, "SHA256_RSA4096")
269 addStr("avb_algorithm", algorithm)
270 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key))
271 addPath("avb_key_path", key)
272 addStr("avb_add_hash_footer_args", "") // TODO(jiyong): add --rollback_index
273 partitionName := proptools.StringDefault(b.properties.Partition_name, b.Name())
274 addStr("partition_name", partitionName)
Jooyung Han65f402b2022-04-21 14:24:04 +0900275 addStr("avb_salt", b.salt())
Jiyong Parkac4076d2021-03-15 23:21:30 +0900276
Cole Faust4e9f5922024-11-13 16:09:23 -0800277 propFile := android.PathForModuleOut(ctx, "prop")
Jiyong Parkac4076d2021-03-15 23:21:30 +0900278 android.WriteFileRule(ctx, propFile, sb.String())
279 return propFile, deps
280}
281
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900282var _ android.AndroidMkEntriesProvider = (*bootimg)(nil)
283
284// Implements android.AndroidMkEntriesProvider
285func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries {
286 return []android.AndroidMkEntries{android.AndroidMkEntries{
287 Class: "ETC",
288 OutputFile: android.OptionalPathForPath(b.output),
289 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700290 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800291 entries.SetString("LOCAL_MODULE_PATH", b.installDir.String())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900292 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName())
293 },
294 },
295 }}
296}
297
298var _ Filesystem = (*bootimg)(nil)
299
300func (b *bootimg) OutputPath() android.Path {
301 return b.output
302}
Jiyong Parkb0eb3192021-03-09 20:29:07 +0900303
Jiyong Park972e06c2021-03-15 23:32:49 +0900304func (b *bootimg) SignedOutputPath() android.Path {
305 if proptools.Bool(b.properties.Use_avb) {
306 return b.OutputPath()
307 }
308 return nil
309}