blob: 4bc18239661d0225751ea421b5b1c17211fbf5d3 [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 {
41 // Path to the linux kernel prebuilt file
42 Kernel_prebuilt *string `android:"arch_variant,path"`
43
44 // Filesystem module that is used as ramdisk
45 Ramdisk_module *string
46
47 // Path to the device tree blob (DTB) prebuilt file to add to this boot image
48 Dtb_prebuilt *string `android:"arch_variant,path"`
49
50 // Header version number. Must be set to one of the version numbers that are currently
51 // supported. Refer to
52 // https://source.android.com/devices/bootloader/boot-image-header
53 Header_version *string
54
55 // Determines if this image is for the vendor_boot partition. Default is false. Refer to
56 // https://source.android.com/devices/bootloader/partitions/vendor-boot-partitions
57 Vendor_boot *bool
58
59 // Optional kernel commandline
60 Cmdline *string
61
62 // When set to true, sign the image with avbtool. Default is false.
63 Use_avb *bool
64
65 // Name of the partition stored in vbmeta desc. Defaults to the name of this module.
66 Partition_name *string
67
68 // Path to the private key that avbtool will use to sign this filesystem image.
69 // TODO(jiyong): allow apex_key to be specified here
70 Avb_private_key *string `android:"path"`
71
72 // Hash and signing algorithm for avbtool. Default is SHA256_RSA4096.
73 Avb_algorithm *string
74}
75
76// bootimg is the image for the boot partition. It consists of header, kernel, ramdisk, and dtb.
77func bootimgFactory() android.Module {
78 module := &bootimg{}
79 module.AddProperties(&module.properties)
80 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
81 return module
82}
83
84type bootimgDep struct {
85 blueprint.BaseDependencyTag
86 kind string
87}
88
89var bootimgRamdiskDep = bootimgDep{kind: "ramdisk"}
90
91func (b *bootimg) DepsMutator(ctx android.BottomUpMutatorContext) {
92 ramdisk := proptools.String(b.properties.Ramdisk_module)
93 if ramdisk != "" {
94 ctx.AddDependency(ctx.Module(), bootimgRamdiskDep, ramdisk)
95 }
96}
97
98func (b *bootimg) installFileName() string {
99 return b.BaseModuleName() + ".img"
100}
101
102func (b *bootimg) partitionName() string {
103 return proptools.StringDefault(b.properties.Partition_name, b.BaseModuleName())
104}
105
106func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) {
107 var unsignedOutput android.OutputPath
108 if proptools.Bool(b.properties.Vendor_boot) {
109 unsignedOutput = b.buildVendorBootImage(ctx)
110 } else {
111 // TODO(jiyong): fix this
112 ctx.PropertyErrorf("vendor_boot", "only vendor_boot:true is supported")
113 }
114
115 if proptools.Bool(b.properties.Use_avb) {
116 b.output = b.signImage(ctx, unsignedOutput)
117 } else {
118 b.output = unsignedOutput
119 }
120
121 b.installDir = android.PathForModuleInstall(ctx, "etc")
122 ctx.InstallFile(b.installDir, b.installFileName(), b.output)
123}
124
125func (b *bootimg) buildVendorBootImage(ctx android.ModuleContext) android.OutputPath {
126 output := android.PathForModuleOut(ctx, "unsigned.img").OutputPath
127 builder := android.NewRuleBuilder(pctx, ctx)
128 cmd := builder.Command().BuiltTool("mkbootimg")
129
130 kernel := android.OptionalPathForModuleSrc(ctx, b.properties.Kernel_prebuilt)
131 if kernel.Valid() {
132 ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
133 return output
134 }
135
136 dtbName := proptools.String(b.properties.Dtb_prebuilt)
137 if dtbName == "" {
138 ctx.PropertyErrorf("dtb_prebuilt", "must be set")
139 return output
140 }
141 dtb := android.PathForModuleSrc(ctx, dtbName)
142 cmd.FlagWithInput("--dtb ", dtb)
143
144 cmdline := proptools.String(b.properties.Cmdline)
145 if cmdline != "" {
146 cmd.FlagWithArg("--vendor_cmdline ", "\""+cmdline+"\"")
147 }
148
149 headerVersion := proptools.String(b.properties.Header_version)
150 if headerVersion == "" {
151 ctx.PropertyErrorf("header_version", "must be set")
152 return output
153 }
154 verNum, err := strconv.Atoi(headerVersion)
155 if err != nil {
156 ctx.PropertyErrorf("header_version", "%q is not a number", headerVersion)
157 return output
158 }
159 if verNum < 3 {
160 ctx.PropertyErrorf("header_version", "must be 3 or higher for vendor_boot")
161 return output
162 }
163 cmd.FlagWithArg("--header_version ", headerVersion)
164
165 ramdiskName := proptools.String(b.properties.Ramdisk_module)
166 if ramdiskName == "" {
167 ctx.PropertyErrorf("ramdisk_module", "must be set")
168 return output
169 }
170 ramdisk := ctx.GetDirectDepWithTag(ramdiskName, bootimgRamdiskDep)
171 if filesystem, ok := ramdisk.(*filesystem); ok {
172 cmd.FlagWithInput("--vendor_ramdisk ", filesystem.OutputPath())
173 } else {
174 ctx.PropertyErrorf("ramdisk", "%q is not android_filesystem module", ramdisk.Name())
175 return output
176 }
177
178 cmd.FlagWithOutput("--vendor_boot ", output)
179
180 builder.Build("build_vendor_bootimg", fmt.Sprintf("Creating %s", b.BaseModuleName()))
181 return output
182}
183
184func (b *bootimg) signImage(ctx android.ModuleContext, unsignedImage android.OutputPath) android.OutputPath {
185 signedImage := android.PathForModuleOut(ctx, "signed.img").OutputPath
186 key := android.PathForModuleSrc(ctx, proptools.String(b.properties.Avb_private_key))
187
188 builder := android.NewRuleBuilder(pctx, ctx)
189 builder.Command().Text("cp").Input(unsignedImage).Output(signedImage)
190 builder.Command().
191 BuiltTool("avbtool").
192 Flag("add_hash_footer").
193 FlagWithArg("--partition_name ", b.partitionName()).
194 FlagWithInput("--key ", key).
195 FlagWithOutput("--image ", signedImage)
196
197 builder.Build("sign_bootimg", fmt.Sprintf("Signing %s", b.BaseModuleName()))
198
199 return signedImage
200}
201
202var _ android.AndroidMkEntriesProvider = (*bootimg)(nil)
203
204// Implements android.AndroidMkEntriesProvider
205func (b *bootimg) AndroidMkEntries() []android.AndroidMkEntries {
206 return []android.AndroidMkEntries{android.AndroidMkEntries{
207 Class: "ETC",
208 OutputFile: android.OptionalPathForPath(b.output),
209 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
210 func(entries *android.AndroidMkEntries) {
211 entries.SetString("LOCAL_MODULE_PATH", b.installDir.ToMakePath().String())
212 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", b.installFileName())
213 },
214 },
215 }}
216}
217
218var _ Filesystem = (*bootimg)(nil)
219
220func (b *bootimg) OutputPath() android.Path {
221 return b.output
222}