blob: ce1c855d475865e9fcd72a090b2d6ac0876739b7 [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() {
29 android.RegisterModuleType("bootimg", bootimgFactory)
30}
31
32type bootimg struct {
33 android.ModuleBase
34
35 properties bootimgProperties
36
37 output android.OutputPath
38 installDir android.InstallPath
39}
40
41type 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
63 // Optional kernel commandline
64 Cmdline *string
65
66 // When set to true, sign the image with avbtool. Default is false.
67 Use_avb *bool
68
69 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
70 Partition_name *string
71
72 // Path to the private key that avbtool will use to sign this filesystem image.
73 // TODO(jiyong): allow apex_key to be specified here
74 Avb_private_key *string `android:"path"`
75
76 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
77 Avb_algorithm *string
78}
79
80// bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb.
81func bootimgFactory() android.Module {
82 module := &bootimg{}
83 module.AddProperties(&module.properties)
84 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
85 return module
86}
87
88type bootimgDep struct {
89 blueprint.BaseDependencyTag
90 kind string
91}
92
93var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"}
94
95func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) {
96 ramdisk := proptools.String(b.properties.Ramdisk_module)
97 if ramdisk != "" {
98 ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk)
99 }
100}
101
102func (b *bootimg) installFileName() string {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900103 return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900104}
105
106func (b *bootimg) partitionName() string {
107 return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName())
108}
109
110func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900111 vendor := proptools.Bool(b.properties.Vendor_boot)
112 unsignedOutput := b.buildBootImage(ctx, vendor)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900113
114 if proptools.Bool(b.properties.Use_avb) {
115 b.output = b.signImage(ctx, unsignedOutput)
116 } else {
117 b.output = unsignedOutput
118 }
119
120 b.installDir = android.PathForModuleInstall(ctx, "etc")
121 ctx.InstallFile(b.installDir, b.installFileName(), b.output)
122}
123
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900124func (b *bootimg) buildBootImage(ctx android.ModuleContext, vendor bool) android.OutputPath {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900125 output := android.PathForModuleOut(ctx, "unsigned", b.installFileName()).OutputPath
126
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900127 builder := android.NewRuleBuilder(pctx, ctx)
128 cmd := builder.Command().BuiltTool("mkbootimg")
129
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900130 kernel := proptools.String(b.properties.Kernel_prebuilt)
131 if vendor && kernel != "" {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900132 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
133 return output
134 }
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900135 if !vendor && kernel == "" {
136 ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel")
137 return output
138 }
139 if kernel != "" {
140 cmd.FlagWithInput("--kernel ", android.PathForModuleSrc(ctx, kernel))
141 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900142
143 dtbName := proptools.String(b.properties.Dtb_prebuilt)
144 if dtbName == "" {
145 ctx.PropertyErrorf("dtb_prebuilt", "must be set")
146 return output
147 }
148 dtb := android.PathForModuleSrc(ctx, dtbName)
149 cmd.FlagWithInput("--dtb ", dtb)
150
151 cmdline := proptools.String(b.properties.Cmdline)
152 if cmdline != "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900153 flag := "--cmdline "
154 if vendor {
155 flag = "--vendor_cmdline "
156 }
Jooyung Han32cddd02021-03-08 20:54:16 +0900157 cmd.FlagWithArg(flag, proptools.ShellEscapeIncludingSpaces(cmdline))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900158 }
159
160 headerVersion := proptools.String(b.properties.Header_version)
161 if headerVersion == "" {
162 ctx.PropertyErrorf("header_version", "must be set")
163 return output
164 }
165 verNum, err := strconv.Atoi(headerVersion)
166 if err != nil {
167 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion)
168 return output
169 }
170 if verNum < 3 {
171 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot")
172 return output
173 }
174 cmd.FlagWithArg("--header_version ", headerVersion)
175
176 ramdiskName := proptools.String(b.properties.Ramdisk_module)
177 if ramdiskName == "" {
178 ctx.PropertyErrorf("ramdisk_module", "must be set")
179 return output
180 }
181 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep)
182 if filesystem, ok := ramdisk.(*filesystem); ok {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900183 flag := "--ramdisk "
184 if vendor {
185 flag = "--vendor_ramdisk "
186 }
187 cmd.FlagWithInput(flag, filesystem.OutputPath())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900188 } else {
189 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name())
190 return output
191 }
192
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900193 flag := "--output "
194 if vendor {
195 flag = "--vendor_boot "
196 }
197 cmd.FlagWithOutput(flag, output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900198
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900199 builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName()))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900200 return output
201}
202
203func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.OutputPath) android.OutputPath {
Jiyong Parkac4076d2021-03-15 23:21:30 +0900204 propFile, toolDeps := b.buildPropFile(ctx)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900205
Jiyong Parkac4076d2021-03-15 23:21:30 +0900206 output := android.PathForModuleOut(ctx, b.installFileName()).OutputPath
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900207 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900208 builder.Command().Text("cp").Input(unsignedImage).Output(output)
Jiyong Parkac4076d2021-03-15 23:21:30 +0900209 builder.Command().BuiltTool("verity_utils").
210 Input(propFile).
211 Implicits(toolDeps).
212 Output(output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900213
214 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName()))
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900215 return output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900216}
217
Jiyong Parkac4076d2021-03-15 23:21:30 +0900218func (b *bootimg) buildPropFile(ctx android.ModuleContext) (propFile android.OutputPath, toolDeps android.Paths) {
219 var sb strings.Builder
220 var deps android.Paths
221 addStr := func(name string, value string) {
222 fmt.Fprintf(&sb, "%s=%s\n", name, value)
223 }
224 addPath := func(name string, path android.Path) {
225 addStr(name, path.String())
226 deps = append(deps, path)
227 }
228
229 addStr("avb_hash_enable", "true")
230 addPath("avb_avbtool", ctx.Config().HostToolPath(ctx, "avbtool"))
231 algorithm := proptools.StringDefault(b.properties.Avb_algorithm, "SHA256_RSA4096")
232 addStr("avb_algorithm", algorithm)
233 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key))
234 addPath("avb_key_path", key)
235 addStr("avb_add_hash_footer_args", "") // TODO(jiyong): add --rollback_index
236 partitionName := proptools.StringDefault(b.properties.Partition_name, b.Name())
237 addStr("partition_name", partitionName)
238
239 propFile = android.PathForModuleOut(ctx, "prop").OutputPath
240 android.WriteFileRule(ctx, propFile, sb.String())
241 return propFile, deps
242}
243
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900244var _ android.AndroidMkEntriesProvider = (*bootimg)(nil)
245
246// Implements android.AndroidMkEntriesProvider
247func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries {
248 return []android.AndroidMkEntries{android.AndroidMkEntries{
249 Class: "ETC",
250 OutputFile: android.OptionalPathForPath(b.output),
251 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700252 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900253 entries.SetString("LOCAL_MODULE_PATH", b.installDir.ToMakePath().String())
254 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName())
255 },
256 },
257 }}
258}
259
260var _ Filesystem = (*bootimg)(nil)
261
262func (b *bootimg) OutputPath() android.Path {
263 return b.output
264}
Jiyong Parkb0eb3192021-03-09 20:29:07 +0900265
266var _ android.OutputFileProducer = (*bootimg)(nil)
267
268// Implements android.OutputFileProducer
269func (b *bootimg) OutputFiles(tag string) (android.Paths, error) {
270 if tag == "" {
271 return []android.Path{b.output}, nil
272 }
273 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
274}