blob: 8e2c5548243e2158fb6f0052bf7e4acd3e5701d2 [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
65 // When set to true, sign the image with avbtool. Default is false.
66 Use_avb *bool
67
68 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
69 Partition_name *string
70
71 // Path to the private key that avbtool will use to sign this filesystem image.
72 // TODO(jiyong): allow apex_key to be specified here
73 Avb_private_key *string `android:"path"`
74
75 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
76 Avb_algorithm *string
77}
78
79// bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb.
80func bootimgFactory() android.Module {
81 module := &bootimg{}
82 module.AddProperties(&module.properties)
83 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
84 return module
85}
86
87type bootimgDep struct {
88 blueprint.BaseDependencyTag
89 kind string
90}
91
92var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"}
93
94func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) {
95 ramdisk := proptools.String(b.properties.Ramdisk_module)
96 if ramdisk != "" {
97 ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk)
98 }
99}
100
101func (b *bootimg) installFileName() string {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900102 return proptools.StringDefault(b.properties.Stem, b.BaseModuleName()+".img")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900103}
104
105func (b *bootimg) partitionName() string {
106 return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName())
107}
108
109func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900110 vendor := proptools.Bool(b.properties.Vendor_boot)
111 unsignedOutput := b.buildBootImage(ctx, vendor)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900112
113 if proptools.Bool(b.properties.Use_avb) {
114 b.output = b.signImage(ctx, unsignedOutput)
115 } else {
116 b.output = unsignedOutput
117 }
118
119 b.installDir = android.PathForModuleInstall(ctx, "etc")
120 ctx.InstallFile(b.installDir, b.installFileName(), b.output)
121}
122
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900123func (b *bootimg) buildBootImage(ctx android.ModuleContext, vendor bool) android.OutputPath {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900124 output := android.PathForModuleOut(ctx, "unsigned", b.installFileName()).OutputPath
125
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900126 builder := android.NewRuleBuilder(pctx, ctx)
127 cmd := builder.Command().BuiltTool("mkbootimg")
128
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900129 kernel := proptools.String(b.properties.Kernel_prebuilt)
130 if vendor && kernel != "" {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900131 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
132 return output
133 }
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900134 if !vendor && kernel == "" {
135 ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel")
136 return output
137 }
138 if kernel != "" {
139 cmd.FlagWithInput("--kernel ", android.PathForModuleSrc(ctx, kernel))
140 }
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900141
142 dtbName := proptools.String(b.properties.Dtb_prebuilt)
143 if dtbName == "" {
144 ctx.PropertyErrorf("dtb_prebuilt", "must be set")
145 return output
146 }
147 dtb := android.PathForModuleSrc(ctx, dtbName)
148 cmd.FlagWithInput("--dtb ", dtb)
149
150 cmdline := proptools.String(b.properties.Cmdline)
151 if cmdline != "" {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900152 flag := "--cmdline "
153 if vendor {
154 flag = "--vendor_cmdline "
155 }
156 cmd.FlagWithArg(flag, "\""+proptools.ShellEscape(cmdline)+"\"")
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900157 }
158
159 headerVersion := proptools.String(b.properties.Header_version)
160 if headerVersion == "" {
161 ctx.PropertyErrorf("header_version", "must be set")
162 return output
163 }
164 verNum, err := strconv.Atoi(headerVersion)
165 if err != nil {
166 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion)
167 return output
168 }
169 if verNum < 3 {
170 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot")
171 return output
172 }
173 cmd.FlagWithArg("--header_version ", headerVersion)
174
175 ramdiskName := proptools.String(b.properties.Ramdisk_module)
176 if ramdiskName == "" {
177 ctx.PropertyErrorf("ramdisk_module", "must be set")
178 return output
179 }
180 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep)
181 if filesystem, ok := ramdisk.(*filesystem); ok {
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900182 flag := "--ramdisk "
183 if vendor {
184 flag = "--vendor_ramdisk "
185 }
186 cmd.FlagWithInput(flag, filesystem.OutputPath())
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900187 } else {
188 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name())
189 return output
190 }
191
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900192 flag := "--output "
193 if vendor {
194 flag = "--vendor_boot "
195 }
196 cmd.FlagWithOutput(flag, output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900197
Jiyong Park4bbd6cf2021-02-18 22:28:31 +0900198 builder.Build("build_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName()))
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900199 return output
200}
201
202func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.OutputPath) android.OutputPath {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900203 output := android.PathForModuleOut(ctx, b.installFileName()).OutputPath
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900204 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key))
205
206 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900207 builder.Command().Text("cp").Input(unsignedImage).Output(output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900208 builder.Command().
209 BuiltTool("avbtool").
210 Flag("add_hash_footer").
211 FlagWithArg("--partition_name ", b.partitionName()).
212 FlagWithInput("--key ", key).
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900213 FlagWithOutput("--image ", output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900214
215 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName()))
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900216 return output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900217}
218
219var _ android.AndroidMkEntriesProvider = (*bootimg)(nil)
220
221// Implements android.AndroidMkEntriesProvider
222func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries {
223 return []android.AndroidMkEntries{android.AndroidMkEntries{
224 Class: "ETC",
225 OutputFile: android.OptionalPathForPath(b.output),
226 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700227 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900228 entries.SetString("LOCAL_MODULE_PATH", b.installDir.ToMakePath().String())
229 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName())
230 },
231 },
232 }}
233}
234
235var _ Filesystem = (*bootimg)(nil)
236
237func (b *bootimg) OutputPath() android.Path {
238 return b.output
239}
Jiyong Parkb0eb3192021-03-09 20:29:07 +0900240
241var _ android.OutputFileProducer = (*bootimg)(nil)
242
243// Implements android.OutputFileProducer
244func (b *bootimg) OutputFiles(tag string) (android.Paths, error) {
245 if tag == "" {
246 return []android.Path{b.output}, nil
247 }
248 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
249}