blob: 4e587377e7f06b53240543358bf5da780c22b961 [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"
20
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
25)
26
27func init() {
28 android.RegisterModuleType("bootimg", bootimgFactory)
29}
30
31type bootimg struct {
32 android.ModuleBase
33
34 properties bootimgProperties
35
36 output android.OutputPath
37 installDir android.InstallPath
38}
39
40type bootimgProperties struct {
Jiyong Park1f55dbd2021-02-15 17:57:35 +090041 // Set the name of the output. Defaults to <module_name>.img.
42 Stem *string
43
Jiyong Park1f7b93e2021-02-01 21:38:11 +090044 // Path to the linux kernel prebuilt file
45 Kernel_prebuilt *string `android:"arch_variant,path"`
46
47 // Filesystem module that is used as ramdisk
48 Ramdisk_module *string
49
50 // Path to the device tree blob (DTB) prebuilt file to add to this boot image
51 Dtb_prebuilt *string `android:"arch_variant,path"`
52
53 // Header version number. Must be set to one of the version numbers that are currently
54 // supported. Refer to
55 // https://source.android.com/devices/bootloader/boot-image-header
56 Header_version *string
57
58 // Determines if this image is for the vendor_boot partition. Default is false. Refer to
59 // https://source.android.com/devices/bootloader/partitions/vendor-boot-partitions
60 Vendor_boot *bool
61
62 // Optional kernel commandline
63 Cmdline *string
64
Jiyong Park81aea9a2021-03-05 18:58:29 +090065 // File that contains bootconfig parameters. This can be set only when `vendor_boot` is true
66 // and `header_version` is greater than or equal to 4.
67 Bootconfig *string `android:"arch_variant,path"`
68
Jiyong Park1f7b93e2021-02-01 21:38:11 +090069 // When set to true, sign the image with avbtool. Default is false.
70 Use_avb *bool
71
72 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
73 Partition_name *string
74
75 // Path to the private key that avbtool will use to sign this filesystem image.
76 // TODO(jiyong): allow apex_key to be specified here
77 Avb_private_key *string `android:"path"`
78
79 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
80 Avb_algorithm *string
81}
82
83// bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb.
84func bootimgFactory() android.Module {
85 module := &bootimg{}
86 module.AddProperties(&module.properties)
87 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
88 return module
89}
90
91type bootimgDep struct {
92 blueprint.BaseDependencyTag
93 kind string
94}
95
96var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"}
97
98func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) {
99 ramdisk := proptools.String(b.properties.Ramdisk_module)
100 if ramdisk != "" {
101 ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk)
102 }
103}
104
105func (b *bootimg) installFileName() string {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900106 return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900107}
108
109func (b *bootimg) partitionName() string {
110 return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName())
111}
112
113func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900114 vendor := proptools.Bool(b.properties.Vendor_boot)
115 unsignedOutput := b.buildBootImage(ctx, vendor)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900116
117 if proptools.Bool(b.properties.Use_avb) {
118 b.output = b.signImage(ctx, unsignedOutput)
119 } else {
120 b.output = unsignedOutput
121 }
122
123 b.installDir = android.PathForModuleInstall(ctx, "etc")
124 ctx.InstallFile(b.installDir, b.installFileName(), b.output)
125}
126
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900127func (b *bootimg) buildBootImage(ctx android.ModuleContext, vendor bool) android.OutputPath {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900128 output := android.PathForModuleOut(ctx, "unsigned", b.installFileName()).OutputPath
129
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900130 builder := android.NewRuleBuilder(pctx, ctx)
131 cmd := builder.Command().BuiltTool("mkbootimg")
132
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900133 kernel := proptools.String(b.properties.Kernel_prebuilt)
134 if vendor && kernel != "" {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900135 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
136 return output
137 }
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900138 if !vendor && kernel == "" {
139 ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel")
140 return output
141 }
142 if kernel != "" {
143 cmd.FlagWithInput("--kernel ", android.PathForModuleSrc(ctx, kernel))
144 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900145
146 dtbName := proptools.String(b.properties.Dtb_prebuilt)
147 if dtbName == "" {
148 ctx.PropertyErrorf("dtb_prebuilt", "must be set")
149 return output
150 }
151 dtb := android.PathForModuleSrc(ctx, dtbName)
152 cmd.FlagWithInput("--dtb ", dtb)
153
154 cmdline := proptools.String(b.properties.Cmdline)
155 if cmdline != "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900156 flag := "--cmdline "
157 if vendor {
158 flag = "--vendor_cmdline "
159 }
160 cmd.FlagWithArg(flag, "\""+proptools.ShellEscape(cmdline)+"\"")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900161 }
162
163 headerVersion := proptools.String(b.properties.Header_version)
164 if headerVersion == "" {
165 ctx.PropertyErrorf("header_version", "must be set")
166 return output
167 }
168 verNum, err := strconv.Atoi(headerVersion)
169 if err != nil {
170 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion)
171 return output
172 }
173 if verNum < 3 {
174 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot")
175 return output
176 }
177 cmd.FlagWithArg("--header_version ", headerVersion)
178
179 ramdiskName := proptools.String(b.properties.Ramdisk_module)
180 if ramdiskName == "" {
181 ctx.PropertyErrorf("ramdisk_module", "must be set")
182 return output
183 }
184 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep)
185 if filesystem, ok := ramdisk.(*filesystem); ok {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900186 flag := "--ramdisk "
187 if vendor {
188 flag = "--vendor_ramdisk "
189 }
190 cmd.FlagWithInput(flag, filesystem.OutputPath())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900191 } else {
192 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name())
193 return output
194 }
195
Jiyong Park81aea9a2021-03-05 18:58:29 +0900196 bootconfig := proptools.String(b.properties.Bootconfig)
197 if bootconfig != "" {
198 if !vendor {
199 ctx.PropertyErrorf("bootconfig", "requires vendor_boot: true")
200 return output
201 }
202 if verNum < 4 {
203 ctx.PropertyErrorf("bootconfig", "requires header_version: 4 or later")
204 return output
205 }
206 cmd.FlagWithInput("--vendor_bootconfig ", android.PathForModuleSrc(ctx, bootconfig))
207 }
208
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900209 flag := "--output "
210 if vendor {
211 flag = "--vendor_boot "
212 }
213 cmd.FlagWithOutput(flag, output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900214
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900215 builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName()))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900216 return output
217}
218
219func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.OutputPath) android.OutputPath {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900220 output := android.PathForModuleOut(ctx, b.installFileName()).OutputPath
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900221 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key))
222
223 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900224 builder.Command().Text("cp").Input(unsignedImage).Output(output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900225 builder.Command().
226 BuiltTool("avbtool").
227 Flag("add_hash_footer").
228 FlagWithArg("--partition_name ", b.partitionName()).
229 FlagWithInput("--key ", key).
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900230 FlagWithOutput("--image ", output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900231
232 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName()))
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900233 return output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900234}
235
236var _ android.AndroidMkEntriesProvider = (*bootimg)(nil)
237
238// Implements android.AndroidMkEntriesProvider
239func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries {
240 return []android.AndroidMkEntries{android.AndroidMkEntries{
241 Class: "ETC",
242 OutputFile: android.OptionalPathForPath(b.output),
243 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700244 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900245 entries.SetString("LOCAL_MODULE_PATH", b.installDir.ToMakePath().String())
246 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName())
247 },
248 },
249 }}
250}
251
252var _ Filesystem = (*bootimg)(nil)
253
254func (b *bootimg) OutputPath() android.Path {
255 return b.output
256}