blob: 9d4ba3e956f82794aea2fdd74b5b093a20346437 [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
Cole Faust4e9f5922024-11-13 16:09:23 -080032 output android.Path
Andrew Scullebd61e92022-06-09 15:53:36 +000033 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))
Cole Faust4e9f5922024-11-13 16:09:23 -0800100 output := android.PathForModuleOut(ctx, a.installFileName())
101 builder.Command().Text("cp").Input(input).Output(output)
Andrew Scullebd61e92022-06-09 15:53:36 +0000102
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 }
David Pursell0ebba612024-01-19 12:49:22 -0800141 cmd.Flag(fmt.Sprintf(" --rollback_index %d", rollbackIndex))
Shikha Panware56c9012023-09-11 13:51:47 +0000142 }
143
Cole Faust4e9f5922024-11-13 16:09:23 -0800144 cmd.FlagWithOutput("--image ", output)
Andrew Scullebd61e92022-06-09 15:53:36 +0000145
146 builder.Build("avbAddHashFooter", fmt.Sprintf("avbAddHashFooter %s", ctx.ModuleName()))
147
148 a.installDir = android.PathForModuleInstall(ctx, "etc")
Cole Faust4e9f5922024-11-13 16:09:23 -0800149 ctx.InstallFile(a.installDir, a.installFileName(), output)
150 a.output = output
Andrew Scullebd61e92022-06-09 15:53:36 +0000151}
152
Jiyong Parkbc485482022-11-15 22:31:49 +0900153func addAvbProp(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, prop avbProp) {
154 name := proptools.String(prop.Name)
155 value := proptools.String(prop.Value)
156 file := proptools.String(prop.File)
157 if name == "" {
158 ctx.PropertyErrorf("name", "can't be empty")
159 return
160 }
161 if value == "" && file == "" {
162 ctx.PropertyErrorf("value", "either value or file should be set")
163 return
164 }
165 if value != "" && file != "" {
166 ctx.PropertyErrorf("value", "value and file can't be set at the same time")
167 return
168 }
169
170 if value != "" {
171 cmd.FlagWithArg("--prop ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, value)))
172 } else {
173 p := android.PathForModuleSrc(ctx, file)
Jiyong Parkb0fda8f2022-12-05 17:09:55 +0900174 cmd.Implicit(p)
Jiyong Parkbc485482022-11-15 22:31:49 +0900175 cmd.FlagWithArg("--prop_from_file ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, cmd.PathForInput(p))))
176 }
177}
178
Andrew Scullebd61e92022-06-09 15:53:36 +0000179var _ android.AndroidMkEntriesProvider = (*avbAddHashFooter)(nil)
180
181// Implements android.AndroidMkEntriesProvider
182func (a *avbAddHashFooter) AndroidMkEntries() []android.AndroidMkEntries {
183 return []android.AndroidMkEntries{android.AndroidMkEntries{
184 Class: "ETC",
185 OutputFile: android.OptionalPathForPath(a.output),
186 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
187 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
188 entries.SetString("LOCAL_MODULE_PATH", a.installDir.String())
189 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", a.installFileName())
190 },
191 },
192 }}
193}
194
195var _ Filesystem = (*avbAddHashFooter)(nil)
196
197func (a *avbAddHashFooter) OutputPath() android.Path {
198 return a.output
199}
200
201func (a *avbAddHashFooter) SignedOutputPath() android.Path {
202 return a.OutputPath() // always signed
203}
204
205// TODO(b/185115783): remove when not needed as input to a prebuilt_etc rule
206var _ android.SourceFileProducer = (*avbAddHashFooter)(nil)
207
208// Implements android.SourceFileProducer
209func (a *avbAddHashFooter) Srcs() android.Paths {
210 return append(android.Paths{}, a.output)
211}
Inseob Kim87230e62023-11-22 18:55:07 +0900212
213type avbAddHashFooterDefaults struct {
214 android.ModuleBase
215 android.DefaultsModuleBase
216}
217
218// avb_add_hash_footer_defaults provides a set of properties that can be inherited by other
219// avb_add_hash_footer modules. A module can use the properties from an avb_add_hash_footer_defaults
220// using `defaults: ["<:default_module_name>"]`. Properties of both modules are erged (when
221// possible) by prepending the default module's values to the depending module's values.
222func avbAddHashFooterDefaultsFactory() android.Module {
223 module := &avbAddHashFooterDefaults{}
224 module.AddProperties(&avbAddHashFooterProperties{})
225 android.InitDefaultsModule(module)
226 return module
227}