blob: ead579fc445624b8c91c4bbe6aff4458bb07c2e6 [file] [log] [blame]
Andrew Scullebd61e92022-06-09 15:53:36 +00001// Copyright (C) 2022 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/proptools"
22
23 "android/soong/android"
24)
25
Andrew Scullebd61e92022-06-09 15:53:36 +000026type avbAddHashFooter struct {
27 android.ModuleBase
Inseob Kim87230e62023-11-22 18:55:07 +090028 android.DefaultableModuleBase
Andrew Scullebd61e92022-06-09 15:53:36 +000029
30 properties avbAddHashFooterProperties
31
32 output android.OutputPath
33 installDir android.InstallPath
34}
35
Jiyong Parkbc485482022-11-15 22:31:49 +090036type avbProp struct {
37 // Name of a property
38 Name *string
39
40 // Value of a property. Can't be used together with `file`.
41 Value *string
42
43 // File from which the value of the prop is read from. Can't be used together with `value`.
44 File *string `android:"path,arch_variant"`
45}
46
Andrew Scullebd61e92022-06-09 15:53:36 +000047type avbAddHashFooterProperties struct {
48 // Source file of this image. Can reference a genrule type module with the ":module" syntax.
49 Src *string `android:"path,arch_variant"`
50
51 // Set the name of the output. Defaults to <module_name>.img.
52 Filename *string
53
54 // Name of the image partition. Defaults to the name of this module.
55 Partition_name *string
56
57 // Size of the partition. Defaults to dynamically calculating the size.
58 Partition_size *int64
59
60 // Path to the private key that avbtool will use to sign this image.
61 Private_key *string `android:"path"`
62
63 // Algorithm that avbtool will use to sign this image. Default is SHA256_RSA4096.
64 Algorithm *string
65
66 // The salt in hex. Required for reproducible builds.
67 Salt *string
Jiyong Parkbc485482022-11-15 22:31:49 +090068
69 // List of properties to add to the footer
70 Props []avbProp
Alice Wang000e3a32023-01-03 16:11:20 +000071
Shikha Panware56c9012023-09-11 13:51:47 +000072 // The index used to prevent rollback of the image on device.
73 Rollback_index *int64
74
Alice Wang000e3a32023-01-03 16:11:20 +000075 // Include descriptors from images
76 Include_descriptors_from_images []string `android:"path,arch_variant"`
Andrew Scullebd61e92022-06-09 15:53:36 +000077}
78
79// The AVB footer adds verification information to the image.
80func avbAddHashFooterFactory() android.Module {
81 module := &avbAddHashFooter{}
82 module.AddProperties(&module.properties)
83 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
Inseob Kim87230e62023-11-22 18:55:07 +090084 android.InitDefaultableModule(module)
Andrew Scullebd61e92022-06-09 15:53:36 +000085 return module
86}
87
88func (a *avbAddHashFooter) installFileName() string {
89 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".img")
90}
91
92func (a *avbAddHashFooter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
93 builder := android.NewRuleBuilder(pctx, ctx)
94
95 if a.properties.Src == nil {
96 ctx.PropertyErrorf("src", "missing source file")
97 return
98 }
99 input := android.PathForModuleSrc(ctx, proptools.String(a.properties.Src))
100 a.output = android.PathForModuleOut(ctx, a.installFileName()).OutputPath
101 builder.Command().Text("cp").Input(input).Output(a.output)
102
103 cmd := builder.Command().BuiltTool("avbtool").Text("add_hash_footer")
104
105 partition_name := proptools.StringDefault(a.properties.Partition_name, a.BaseModuleName())
106 cmd.FlagWithArg("--partition_name ", partition_name)
107
108 if a.properties.Partition_size == nil {
109 cmd.Flag("--dynamic_partition_size")
110 } else {
111 partition_size := proptools.Int(a.properties.Partition_size)
112 cmd.FlagWithArg("--partition_size ", strconv.Itoa(partition_size))
113 }
114
115 key := android.PathForModuleSrc(ctx, proptools.String(a.properties.Private_key))
116 cmd.FlagWithInput("--key ", key)
117
118 algorithm := proptools.StringDefault(a.properties.Algorithm, "SHA256_RSA4096")
119 cmd.FlagWithArg("--algorithm ", algorithm)
120
121 if a.properties.Salt == nil {
122 ctx.PropertyErrorf("salt", "missing salt value")
123 return
124 }
125 cmd.FlagWithArg("--salt ", proptools.String(a.properties.Salt))
126
Alice Wang000e3a32023-01-03 16:11:20 +0000127 imagePaths := android.PathsForModuleSrc(ctx, a.properties.Include_descriptors_from_images)
128 for _, imagePath := range imagePaths {
129 cmd.FlagWithInput("--include_descriptors_from_image ", imagePath)
130 }
131
Jiyong Parkbc485482022-11-15 22:31:49 +0900132 for _, prop := range a.properties.Props {
133 addAvbProp(ctx, cmd, prop)
134 }
135
Shikha Panware56c9012023-09-11 13:51:47 +0000136 if a.properties.Rollback_index != nil {
137 rollbackIndex := proptools.Int(a.properties.Rollback_index)
138 if rollbackIndex < 0 {
139 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
140 }
141 cmd.Flag(fmt.Sprintf(" --rollback_index %x", rollbackIndex))
142 }
143
Andrew Scullebd61e92022-06-09 15:53:36 +0000144 cmd.FlagWithOutput("--image ", a.output)
145
146 builder.Build("avbAddHashFooter", fmt.Sprintf("avbAddHashFooter %s", ctx.ModuleName()))
147
148 a.installDir = android.PathForModuleInstall(ctx, "etc")
149 ctx.InstallFile(a.installDir, a.installFileName(), a.output)
150}
151
Jiyong Parkbc485482022-11-15 22:31:49 +0900152func addAvbProp(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, prop avbProp) {
153 name := proptools.String(prop.Name)
154 value := proptools.String(prop.Value)
155 file := proptools.String(prop.File)
156 if name == "" {
157 ctx.PropertyErrorf("name", "can't be empty")
158 return
159 }
160 if value == "" && file == "" {
161 ctx.PropertyErrorf("value", "either value or file should be set")
162 return
163 }
164 if value != "" && file != "" {
165 ctx.PropertyErrorf("value", "value and file can't be set at the same time")
166 return
167 }
168
169 if value != "" {
170 cmd.FlagWithArg("--prop ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, value)))
171 } else {
172 p := android.PathForModuleSrc(ctx, file)
Jiyong Parkb0fda8f2022-12-05 17:09:55 +0900173 cmd.Implicit(p)
Jiyong Parkbc485482022-11-15 22:31:49 +0900174 cmd.FlagWithArg("--prop_from_file ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, cmd.PathForInput(p))))
175 }
176}
177
Andrew Scullebd61e92022-06-09 15:53:36 +0000178var _ android.AndroidMkEntriesProvider = (*avbAddHashFooter)(nil)
179
180// Implements android.AndroidMkEntriesProvider
181func (a *avbAddHashFooter) AndroidMkEntries() []android.AndroidMkEntries {
182 return []android.AndroidMkEntries{android.AndroidMkEntries{
183 Class: "ETC",
184 OutputFile: android.OptionalPathForPath(a.output),
185 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
186 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
187 entries.SetString("LOCAL_MODULE_PATH", a.installDir.String())
188 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", a.installFileName())
189 },
190 },
191 }}
192}
193
194var _ Filesystem = (*avbAddHashFooter)(nil)
195
196func (a *avbAddHashFooter) OutputPath() android.Path {
197 return a.output
198}
199
200func (a *avbAddHashFooter) SignedOutputPath() android.Path {
201 return a.OutputPath() // always signed
202}
203
204// TODO(b/185115783): remove when not needed as input to a prebuilt_etc rule
205var _ android.SourceFileProducer = (*avbAddHashFooter)(nil)
206
207// Implements android.SourceFileProducer
208func (a *avbAddHashFooter) Srcs() android.Paths {
209 return append(android.Paths{}, a.output)
210}
Inseob Kim87230e62023-11-22 18:55:07 +0900211
212type avbAddHashFooterDefaults struct {
213 android.ModuleBase
214 android.DefaultsModuleBase
215}
216
217// avb_add_hash_footer_defaults provides a set of properties that can be inherited by other
218// avb_add_hash_footer modules. A module can use the properties from an avb_add_hash_footer_defaults
219// using `defaults: ["<:default_module_name>"]`. Properties of both modules are erged (when
220// possible) by prepending the default module's values to the depending module's values.
221func avbAddHashFooterDefaultsFactory() android.Module {
222 module := &avbAddHashFooterDefaults{}
223 module.AddProperties(&avbAddHashFooterProperties{})
224 android.InitDefaultsModule(module)
225 return module
226}