blob: dabbc461bc1005be03610d38758ce69751b12644 [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
28
29 properties avbAddHashFooterProperties
30
31 output android.OutputPath
32 installDir android.InstallPath
33}
34
Jiyong Parkbc485482022-11-15 22:31:49 +090035type avbProp struct {
36 // Name of a property
37 Name *string
38
39 // Value of a property. Can't be used together with `file`.
40 Value *string
41
42 // File from which the value of the prop is read from. Can't be used together with `value`.
43 File *string `android:"path,arch_variant"`
44}
45
Andrew Scullebd61e92022-06-09 15:53:36 +000046type avbAddHashFooterProperties struct {
47 // Source file of this image. Can reference a genrule type module with the ":module" syntax.
48 Src *string `android:"path,arch_variant"`
49
50 // Set the name of the output. Defaults to <module_name>.img.
51 Filename *string
52
53 // Name of the image partition. Defaults to the name of this module.
54 Partition_name *string
55
56 // Size of the partition. Defaults to dynamically calculating the size.
57 Partition_size *int64
58
59 // Path to the private key that avbtool will use to sign this image.
60 Private_key *string `android:"path"`
61
62 // Algorithm that avbtool will use to sign this image. Default is SHA256_RSA4096.
63 Algorithm *string
64
65 // The salt in hex. Required for reproducible builds.
66 Salt *string
Jiyong Parkbc485482022-11-15 22:31:49 +090067
68 // List of properties to add to the footer
69 Props []avbProp
Alice Wang000e3a32023-01-03 16:11:20 +000070
Shikha Panware56c9012023-09-11 13:51:47 +000071 // The index used to prevent rollback of the image on device.
72 Rollback_index *int64
73
Alice Wang000e3a32023-01-03 16:11:20 +000074 // Include descriptors from images
75 Include_descriptors_from_images []string `android:"path,arch_variant"`
Andrew Scullebd61e92022-06-09 15:53:36 +000076}
77
78// The AVB footer adds verification information to the image.
79func avbAddHashFooterFactory() android.Module {
80 module := &avbAddHashFooter{}
81 module.AddProperties(&module.properties)
82 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
83 return module
84}
85
86func (a *avbAddHashFooter) installFileName() string {
87 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".img")
88}
89
90func (a *avbAddHashFooter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
91 builder := android.NewRuleBuilder(pctx, ctx)
92
93 if a.properties.Src == nil {
94 ctx.PropertyErrorf("src", "missing source file")
95 return
96 }
97 input := android.PathForModuleSrc(ctx, proptools.String(a.properties.Src))
98 a.output = android.PathForModuleOut(ctx, a.installFileName()).OutputPath
99 builder.Command().Text("cp").Input(input).Output(a.output)
100
101 cmd := builder.Command().BuiltTool("avbtool").Text("add_hash_footer")
102
103 partition_name := proptools.StringDefault(a.properties.Partition_name, a.BaseModuleName())
104 cmd.FlagWithArg("--partition_name ", partition_name)
105
106 if a.properties.Partition_size == nil {
107 cmd.Flag("--dynamic_partition_size")
108 } else {
109 partition_size := proptools.Int(a.properties.Partition_size)
110 cmd.FlagWithArg("--partition_size ", strconv.Itoa(partition_size))
111 }
112
113 key := android.PathForModuleSrc(ctx, proptools.String(a.properties.Private_key))
114 cmd.FlagWithInput("--key ", key)
115
116 algorithm := proptools.StringDefault(a.properties.Algorithm, "SHA256_RSA4096")
117 cmd.FlagWithArg("--algorithm ", algorithm)
118
119 if a.properties.Salt == nil {
120 ctx.PropertyErrorf("salt", "missing salt value")
121 return
122 }
123 cmd.FlagWithArg("--salt ", proptools.String(a.properties.Salt))
124
Alice Wang000e3a32023-01-03 16:11:20 +0000125 imagePaths := android.PathsForModuleSrc(ctx, a.properties.Include_descriptors_from_images)
126 for _, imagePath := range imagePaths {
127 cmd.FlagWithInput("--include_descriptors_from_image ", imagePath)
128 }
129
Jiyong Parkbc485482022-11-15 22:31:49 +0900130 for _, prop := range a.properties.Props {
131 addAvbProp(ctx, cmd, prop)
132 }
133
Shikha Panware56c9012023-09-11 13:51:47 +0000134 if a.properties.Rollback_index != nil {
135 rollbackIndex := proptools.Int(a.properties.Rollback_index)
136 if rollbackIndex < 0 {
137 ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
138 }
139 cmd.Flag(fmt.Sprintf(" --rollback_index %x", rollbackIndex))
140 }
141
Andrew Scullebd61e92022-06-09 15:53:36 +0000142 cmd.FlagWithOutput("--image ", a.output)
143
144 builder.Build("avbAddHashFooter", fmt.Sprintf("avbAddHashFooter %s", ctx.ModuleName()))
145
146 a.installDir = android.PathForModuleInstall(ctx, "etc")
147 ctx.InstallFile(a.installDir, a.installFileName(), a.output)
148}
149
Jiyong Parkbc485482022-11-15 22:31:49 +0900150func addAvbProp(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, prop avbProp) {
151 name := proptools.String(prop.Name)
152 value := proptools.String(prop.Value)
153 file := proptools.String(prop.File)
154 if name == "" {
155 ctx.PropertyErrorf("name", "can't be empty")
156 return
157 }
158 if value == "" && file == "" {
159 ctx.PropertyErrorf("value", "either value or file should be set")
160 return
161 }
162 if value != "" && file != "" {
163 ctx.PropertyErrorf("value", "value and file can't be set at the same time")
164 return
165 }
166
167 if value != "" {
168 cmd.FlagWithArg("--prop ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, value)))
169 } else {
170 p := android.PathForModuleSrc(ctx, file)
Jiyong Parkb0fda8f2022-12-05 17:09:55 +0900171 cmd.Implicit(p)
Jiyong Parkbc485482022-11-15 22:31:49 +0900172 cmd.FlagWithArg("--prop_from_file ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, cmd.PathForInput(p))))
173 }
174}
175
Andrew Scullebd61e92022-06-09 15:53:36 +0000176var _ android.AndroidMkEntriesProvider = (*avbAddHashFooter)(nil)
177
178// Implements android.AndroidMkEntriesProvider
179func (a *avbAddHashFooter) AndroidMkEntries() []android.AndroidMkEntries {
180 return []android.AndroidMkEntries{android.AndroidMkEntries{
181 Class: "ETC",
182 OutputFile: android.OptionalPathForPath(a.output),
183 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
184 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
185 entries.SetString("LOCAL_MODULE_PATH", a.installDir.String())
186 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", a.installFileName())
187 },
188 },
189 }}
190}
191
192var _ Filesystem = (*avbAddHashFooter)(nil)
193
194func (a *avbAddHashFooter) OutputPath() android.Path {
195 return a.output
196}
197
198func (a *avbAddHashFooter) SignedOutputPath() android.Path {
199 return a.OutputPath() // always signed
200}
201
202// TODO(b/185115783): remove when not needed as input to a prebuilt_etc rule
203var _ android.SourceFileProducer = (*avbAddHashFooter)(nil)
204
205// Implements android.SourceFileProducer
206func (a *avbAddHashFooter) Srcs() android.Paths {
207 return append(android.Paths{}, a.output)
208}