summaryrefslogtreecommitdiff
path: root/filesystem/avb_add_hash_footer.go
blob: c7760120d4ab5817dc29147dfffe0084a7b705c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (C) 2022 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filesystem

import (
	"fmt"
	"strconv"

	"github.com/google/blueprint/proptools"

	"android/soong/android"
)

type avbAddHashFooter struct {
	android.ModuleBase
	android.DefaultableModuleBase

	properties avbAddHashFooterProperties

	output     android.Path
	installDir android.InstallPath
}

type avbProp struct {
	// Name of a property
	Name *string

	// Value of a property. Can't be used together with `file`.
	Value *string

	// File from which the value of the prop is read from. Can't be used together with `value`.
	File *string `android:"path,arch_variant"`
}

type avbAddHashFooterProperties struct {
	// Source file of this image. Can reference a genrule type module with the ":module" syntax.
	Src proptools.Configurable[string] `android:"path,arch_variant,replace_instead_of_append"`

	// Set the name of the output. Defaults to <module_name>.img.
	Filename *string

	// Name of the image partition. Defaults to the name of this module.
	Partition_name *string

	// Size of the partition. Defaults to dynamically calculating the size.
	Partition_size *int64

	// Path to the private key that avbtool will use to sign this image.
	Private_key *string `android:"path"`

	// Algorithm that avbtool will use to sign this image. Default is SHA256_RSA4096.
	Algorithm *string

	// The salt in hex. Required for reproducible builds.
	Salt *string

	// List of properties to add to the footer
	Props []avbProp

	// The index used to prevent rollback of the image on device.
	Rollback_index proptools.Configurable[int64] `android:"replace_instead_of_append"`

	// Include descriptors from images
	Include_descriptors_from_images []string `android:"path,arch_variant"`
}

// The AVB footer adds verification information to the image.
func avbAddHashFooterFactory() android.Module {
	module := &avbAddHashFooter{}
	module.AddProperties(&module.properties)
	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
	android.InitDefaultableModule(module)
	return module
}

func (a *avbAddHashFooter) installFileName() string {
	return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".img")
}

func (a *avbAddHashFooter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	builder := android.NewRuleBuilder(pctx, ctx)
	src := a.properties.Src.GetOrDefault(ctx, "")

	if src == "" {
		ctx.PropertyErrorf("src", "missing source file")
		return
	}
	input := android.PathForModuleSrc(ctx, src)
	output := android.PathForModuleOut(ctx, a.installFileName())
	builder.Command().Text("cp").Input(input).Output(output)

	cmd := builder.Command().BuiltTool("avbtool").Text("add_hash_footer")

	partition_name := proptools.StringDefault(a.properties.Partition_name, a.BaseModuleName())
	cmd.FlagWithArg("--partition_name ", partition_name)

	if a.properties.Partition_size == nil {
		cmd.Flag("--dynamic_partition_size")
	} else {
		partition_size := proptools.Int(a.properties.Partition_size)
		cmd.FlagWithArg("--partition_size ", strconv.Itoa(partition_size))
	}

	key := android.PathForModuleSrc(ctx, proptools.String(a.properties.Private_key))
	cmd.FlagWithInput("--key ", key)

	algorithm := proptools.StringDefault(a.properties.Algorithm, "SHA256_RSA4096")
	cmd.FlagWithArg("--algorithm ", algorithm)

	if a.properties.Salt == nil {
		ctx.PropertyErrorf("salt", "missing salt value")
		return
	}
	cmd.FlagWithArg("--salt ", proptools.String(a.properties.Salt))

	imagePaths := android.PathsForModuleSrc(ctx, a.properties.Include_descriptors_from_images)
	for _, imagePath := range imagePaths {
		cmd.FlagWithInput("--include_descriptors_from_image ", imagePath)
	}

	for _, prop := range a.properties.Props {
		addAvbProp(ctx, cmd, prop)
	}

	rollbackIndex := a.properties.Rollback_index.Get(ctx)
	if rollbackIndex.IsPresent() {
		rollbackIndex := rollbackIndex.Get()
		if rollbackIndex < 0 {
			ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
		}
		cmd.Flag(fmt.Sprintf(" --rollback_index %d", rollbackIndex))
	}

	cmd.FlagWithOutput("--image ", output)

	builder.Build("avbAddHashFooter", fmt.Sprintf("avbAddHashFooter %s", ctx.ModuleName()))

	a.installDir = android.PathForModuleInstall(ctx, "etc")
	ctx.InstallFile(a.installDir, a.installFileName(), output)
	a.output = output

	setCommonFilesystemInfo(ctx, a)
}

func addAvbProp(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, prop avbProp) {
	name := proptools.String(prop.Name)
	value := proptools.String(prop.Value)
	file := proptools.String(prop.File)
	if name == "" {
		ctx.PropertyErrorf("name", "can't be empty")
		return
	}
	if value == "" && file == "" {
		ctx.PropertyErrorf("value", "either value or file should be set")
		return
	}
	if value != "" && file != "" {
		ctx.PropertyErrorf("value", "value and file can't be set at the same time")
		return
	}

	if value != "" {
		cmd.FlagWithArg("--prop ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, value)))
	} else {
		p := android.PathForModuleSrc(ctx, file)
		cmd.Implicit(p)
		cmd.FlagWithArg("--prop_from_file ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, cmd.PathForInput(p))))
	}
}

var _ android.AndroidMkEntriesProvider = (*avbAddHashFooter)(nil)

// Implements android.AndroidMkEntriesProvider
func (a *avbAddHashFooter) AndroidMkEntries() []android.AndroidMkEntries {
	return []android.AndroidMkEntries{android.AndroidMkEntries{
		Class:      "ETC",
		OutputFile: android.OptionalPathForPath(a.output),
		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
				entries.SetString("LOCAL_MODULE_PATH", a.installDir.String())
				entries.SetString("LOCAL_INSTALLED_MODULE_STEM", a.installFileName())
			},
		},
	}}
}

var _ Filesystem = (*avbAddHashFooter)(nil)

func (a *avbAddHashFooter) OutputPath() android.Path {
	return a.output
}

func (a *avbAddHashFooter) SignedOutputPath() android.Path {
	return a.OutputPath() // always signed
}

// TODO(b/185115783): remove when not needed as input to a prebuilt_etc rule
var _ android.SourceFileProducer = (*avbAddHashFooter)(nil)

// Implements android.SourceFileProducer
func (a *avbAddHashFooter) Srcs() android.Paths {
	if a.output == nil {
		return nil
	}
	return append(android.Paths{}, a.output)
}

type avbAddHashFooterDefaults struct {
	android.ModuleBase
	android.DefaultsModuleBase
}

// avb_add_hash_footer_defaults provides a set of properties that can be inherited by other
// avb_add_hash_footer modules. A module can use the properties from an avb_add_hash_footer_defaults
// using `defaults: ["<:default_module_name>"]`. Properties of both modules are erged (when
// possible) by prepending the default module's values to the depending module's values.
func avbAddHashFooterDefaultsFactory() android.Module {
	module := &avbAddHashFooterDefaults{}
	module.AddProperties(&avbAddHashFooterProperties{})
	android.InitDefaultsModule(module)
	return module
}