blob: 2ee420cdbf585452615c9f9f723983459474108b [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
Andrew Scullebd61e92022-06-09 15:53:36 +000070}
71
72// The AVB footer adds verification information to the image.
73func avbAddHashFooterFactory() android.Module {
74 module := &avbAddHashFooter{}
75 module.AddProperties(&module.properties)
76 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
77 return module
78}
79
80func (a *avbAddHashFooter) installFileName() string {
81 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".img")
82}
83
84func (a *avbAddHashFooter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
85 builder := android.NewRuleBuilder(pctx, ctx)
86
87 if a.properties.Src == nil {
88 ctx.PropertyErrorf("src", "missing source file")
89 return
90 }
91 input := android.PathForModuleSrc(ctx, proptools.String(a.properties.Src))
92 a.output = android.PathForModuleOut(ctx, a.installFileName()).OutputPath
93 builder.Command().Text("cp").Input(input).Output(a.output)
94
95 cmd := builder.Command().BuiltTool("avbtool").Text("add_hash_footer")
96
97 partition_name := proptools.StringDefault(a.properties.Partition_name, a.BaseModuleName())
98 cmd.FlagWithArg("--partition_name ", partition_name)
99
100 if a.properties.Partition_size == nil {
101 cmd.Flag("--dynamic_partition_size")
102 } else {
103 partition_size := proptools.Int(a.properties.Partition_size)
104 cmd.FlagWithArg("--partition_size ", strconv.Itoa(partition_size))
105 }
106
107 key := android.PathForModuleSrc(ctx, proptools.String(a.properties.Private_key))
108 cmd.FlagWithInput("--key ", key)
109
110 algorithm := proptools.StringDefault(a.properties.Algorithm, "SHA256_RSA4096")
111 cmd.FlagWithArg("--algorithm ", algorithm)
112
113 if a.properties.Salt == nil {
114 ctx.PropertyErrorf("salt", "missing salt value")
115 return
116 }
117 cmd.FlagWithArg("--salt ", proptools.String(a.properties.Salt))
118
Jiyong Parkbc485482022-11-15 22:31:49 +0900119 for _, prop := range a.properties.Props {
120 addAvbProp(ctx, cmd, prop)
121 }
122
Andrew Scullebd61e92022-06-09 15:53:36 +0000123 cmd.FlagWithOutput("--image ", a.output)
124
125 builder.Build("avbAddHashFooter", fmt.Sprintf("avbAddHashFooter %s", ctx.ModuleName()))
126
127 a.installDir = android.PathForModuleInstall(ctx, "etc")
128 ctx.InstallFile(a.installDir, a.installFileName(), a.output)
129}
130
Jiyong Parkbc485482022-11-15 22:31:49 +0900131func addAvbProp(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, prop avbProp) {
132 name := proptools.String(prop.Name)
133 value := proptools.String(prop.Value)
134 file := proptools.String(prop.File)
135 if name == "" {
136 ctx.PropertyErrorf("name", "can't be empty")
137 return
138 }
139 if value == "" && file == "" {
140 ctx.PropertyErrorf("value", "either value or file should be set")
141 return
142 }
143 if value != "" && file != "" {
144 ctx.PropertyErrorf("value", "value and file can't be set at the same time")
145 return
146 }
147
148 if value != "" {
149 cmd.FlagWithArg("--prop ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, value)))
150 } else {
151 p := android.PathForModuleSrc(ctx, file)
Jiyong Parkb0fda8f2022-12-05 17:09:55 +0900152 cmd.Implicit(p)
Jiyong Parkbc485482022-11-15 22:31:49 +0900153 cmd.FlagWithArg("--prop_from_file ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, cmd.PathForInput(p))))
154 }
155}
156
Andrew Scullebd61e92022-06-09 15:53:36 +0000157var _ android.AndroidMkEntriesProvider = (*avbAddHashFooter)(nil)
158
159// Implements android.AndroidMkEntriesProvider
160func (a *avbAddHashFooter) AndroidMkEntries() []android.AndroidMkEntries {
161 return []android.AndroidMkEntries{android.AndroidMkEntries{
162 Class: "ETC",
163 OutputFile: android.OptionalPathForPath(a.output),
164 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
165 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
166 entries.SetString("LOCAL_MODULE_PATH", a.installDir.String())
167 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", a.installFileName())
168 },
169 },
170 }}
171}
172
173var _ Filesystem = (*avbAddHashFooter)(nil)
174
175func (a *avbAddHashFooter) OutputPath() android.Path {
176 return a.output
177}
178
179func (a *avbAddHashFooter) SignedOutputPath() android.Path {
180 return a.OutputPath() // always signed
181}
182
183// TODO(b/185115783): remove when not needed as input to a prebuilt_etc rule
184var _ android.SourceFileProducer = (*avbAddHashFooter)(nil)
185
186// Implements android.SourceFileProducer
187func (a *avbAddHashFooter) Srcs() android.Paths {
188 return append(android.Paths{}, a.output)
189}