blob: c90929ae99da80676cc6ea80df22867233cfa001 [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) {
110 var unsignedOutput android.OutputPath
111 if proptools.Bool(b.properties.Vendor_boot) {
112 unsignedOutput = b.buildVendorBootImage(ctx)
113 } else {
114 // TODO(jiyong): fix this
115 ctx.PropertyErrorf("vendor_boot", "only vendor_boot:true is supported")
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900116 return
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900117 }
118
119 if proptools.Bool(b.properties.Use_avb) {
120 b.output = b.signImage(ctx, unsignedOutput)
121 } else {
122 b.output = unsignedOutput
123 }
124
125 b.installDir = android.PathForModuleInstall(ctx, "etc")
126 ctx.InstallFile(b.installDir, b.installFileName(), b.output)
127}
128
129func (b *bootimg) buildVendorBootImage(ctx android.ModuleContext) android.OutputPath {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900130 output := android.PathForModuleOut(ctx, "unsigned", b.installFileName()).OutputPath
131
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900132 builder := android.NewRuleBuilder(pctx, ctx)
133 cmd := builder.Command().BuiltTool("mkbootimg")
134
135 kernel := android.OptionalPathForModuleSrc(ctx, b.properties.Kernel_prebuilt)
136 if kernel.Valid() {
137 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
138 return output
139 }
140
141 dtbName := proptools.String(b.properties.Dtb_prebuilt)
142 if dtbName == "" {
143 ctx.PropertyErrorf("dtb_prebuilt", "must be set")
144 return output
145 }
146 dtb := android.PathForModuleSrc(ctx, dtbName)
147 cmd.FlagWithInput("--dtb ", dtb)
148
149 cmdline := proptools.String(b.properties.Cmdline)
150 if cmdline != "" {
151 cmd.FlagWithArg("--vendor_cmdline ", "\""+cmdline+"\"")
152 }
153
154 headerVersion := proptools.String(b.properties.Header_version)
155 if headerVersion == "" {
156 ctx.PropertyErrorf("header_version", "must be set")
157 return output
158 }
159 verNum, err := strconv.Atoi(headerVersion)
160 if err != nil {
161 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion)
162 return output
163 }
164 if verNum < 3 {
165 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot")
166 return output
167 }
168 cmd.FlagWithArg("--header_version ", headerVersion)
169
170 ramdiskName := proptools.String(b.properties.Ramdisk_module)
171 if ramdiskName == "" {
172 ctx.PropertyErrorf("ramdisk_module", "must be set")
173 return output
174 }
175 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep)
176 if filesystem, ok := ramdisk.(*filesystem); ok {
177 cmd.FlagWithInput("--vendor_ramdisk ", filesystem.OutputPath())
178 } else {
179 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name())
180 return output
181 }
182
183 cmd.FlagWithOutput("--vendor_boot ", output)
184
185 builder.Build("build_vendor_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName()))
186 return output
187}
188
189func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.OutputPath) android.OutputPath {
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900190 output := android.PathForModuleOut(ctx, b.installFileName()).OutputPath
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900191 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key))
192
193 builder := android.NewRuleBuilder(pctx, ctx)
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900194 builder.Command().Text("cp").Input(unsignedImage).Output(output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900195 builder.Command().
196 BuiltTool("avbtool").
197 Flag("add_hash_footer").
198 FlagWithArg("--partition_name ", b.partitionName()).
199 FlagWithInput("--key ", key).
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900200 FlagWithOutput("--image ", output)
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900201
202 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName()))
Jiyong Park1f55dbd2021-02-15 17:57:35 +0900203 return output
Jiyong Park1f7b93e2021-02-01 21:38:11 +0900204}
205
206var _ android.AndroidMkEntriesProvider = (*bootimg)(nil)
207
208// Implements android.AndroidMkEntriesProvider
209func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries {
210 return []android.AndroidMkEntries{android.AndroidMkEntries{
211 Class: "ETC",
212 OutputFile: android.OptionalPathForPath(b.output),
213 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
214 func(entries *android.AndroidMkEntries) {
215 entries.SetString("LOCAL_MODULE_PATH", b.installDir.ToMakePath().String())
216 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName())
217 },
218 },
219 }}
220}
221
222var _ Filesystem = (*bootimg)(nil)
223
224func (b *bootimg) OutputPath() android.Path {
225 return b.output
226}