• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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
26type avbAddHashFooter struct {
27	android.ModuleBase
28	android.DefaultableModuleBase
29
30	properties avbAddHashFooterProperties
31
32	output     android.Path
33	installDir android.InstallPath
34}
35
36type 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
47type avbAddHashFooterProperties struct {
48	// Source file of this image. Can reference a genrule type module with the ":module" syntax.
49	Src proptools.Configurable[string] `android:"path,arch_variant,replace_instead_of_append"`
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
68
69	// List of properties to add to the footer
70	Props []avbProp
71
72	// The index used to prevent rollback of the image on device.
73	Rollback_index proptools.Configurable[int64] `android:"replace_instead_of_append"`
74
75	// Include descriptors from images
76	Include_descriptors_from_images []string `android:"path,arch_variant"`
77}
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)
84	android.InitDefaultableModule(module)
85	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	src := a.properties.Src.GetOrDefault(ctx, "")
95
96	if src == "" {
97		ctx.PropertyErrorf("src", "missing source file")
98		return
99	}
100	input := android.PathForModuleSrc(ctx, src)
101	output := android.PathForModuleOut(ctx, a.installFileName())
102	builder.Command().Text("cp").Input(input).Output(output)
103
104	cmd := builder.Command().BuiltTool("avbtool").Text("add_hash_footer")
105
106	partition_name := proptools.StringDefault(a.properties.Partition_name, a.BaseModuleName())
107	cmd.FlagWithArg("--partition_name ", partition_name)
108
109	if a.properties.Partition_size == nil {
110		cmd.Flag("--dynamic_partition_size")
111	} else {
112		partition_size := proptools.Int(a.properties.Partition_size)
113		cmd.FlagWithArg("--partition_size ", strconv.Itoa(partition_size))
114	}
115
116	key := android.PathForModuleSrc(ctx, proptools.String(a.properties.Private_key))
117	cmd.FlagWithInput("--key ", key)
118
119	algorithm := proptools.StringDefault(a.properties.Algorithm, "SHA256_RSA4096")
120	cmd.FlagWithArg("--algorithm ", algorithm)
121
122	if a.properties.Salt == nil {
123		ctx.PropertyErrorf("salt", "missing salt value")
124		return
125	}
126	cmd.FlagWithArg("--salt ", proptools.String(a.properties.Salt))
127
128	imagePaths := android.PathsForModuleSrc(ctx, a.properties.Include_descriptors_from_images)
129	for _, imagePath := range imagePaths {
130		cmd.FlagWithInput("--include_descriptors_from_image ", imagePath)
131	}
132
133	for _, prop := range a.properties.Props {
134		addAvbProp(ctx, cmd, prop)
135	}
136
137	rollbackIndex := a.properties.Rollback_index.Get(ctx)
138	if rollbackIndex.IsPresent() {
139		rollbackIndex := rollbackIndex.Get()
140		if rollbackIndex < 0 {
141			ctx.PropertyErrorf("rollback_index", "Rollback index must be non-negative")
142		}
143		cmd.Flag(fmt.Sprintf(" --rollback_index %d", rollbackIndex))
144	}
145
146	cmd.FlagWithOutput("--image ", output)
147
148	builder.Build("avbAddHashFooter", fmt.Sprintf("avbAddHashFooter %s", ctx.ModuleName()))
149
150	a.installDir = android.PathForModuleInstall(ctx, "etc")
151	ctx.InstallFile(a.installDir, a.installFileName(), output)
152	a.output = output
153
154	setCommonFilesystemInfo(ctx, a)
155}
156
157func addAvbProp(ctx android.ModuleContext, cmd *android.RuleBuilderCommand, prop avbProp) {
158	name := proptools.String(prop.Name)
159	value := proptools.String(prop.Value)
160	file := proptools.String(prop.File)
161	if name == "" {
162		ctx.PropertyErrorf("name", "can't be empty")
163		return
164	}
165	if value == "" && file == "" {
166		ctx.PropertyErrorf("value", "either value or file should be set")
167		return
168	}
169	if value != "" && file != "" {
170		ctx.PropertyErrorf("value", "value and file can't be set at the same time")
171		return
172	}
173
174	if value != "" {
175		cmd.FlagWithArg("--prop ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, value)))
176	} else {
177		p := android.PathForModuleSrc(ctx, file)
178		cmd.Implicit(p)
179		cmd.FlagWithArg("--prop_from_file ", proptools.ShellEscape(fmt.Sprintf("%s:%s", name, cmd.PathForInput(p))))
180	}
181}
182
183var _ android.AndroidMkEntriesProvider = (*avbAddHashFooter)(nil)
184
185// Implements android.AndroidMkEntriesProvider
186func (a *avbAddHashFooter) AndroidMkEntries() []android.AndroidMkEntries {
187	return []android.AndroidMkEntries{android.AndroidMkEntries{
188		Class:      "ETC",
189		OutputFile: android.OptionalPathForPath(a.output),
190		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
191			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
192				entries.SetString("LOCAL_MODULE_PATH", a.installDir.String())
193				entries.SetString("LOCAL_INSTALLED_MODULE_STEM", a.installFileName())
194			},
195		},
196	}}
197}
198
199var _ Filesystem = (*avbAddHashFooter)(nil)
200
201func (a *avbAddHashFooter) OutputPath() android.Path {
202	return a.output
203}
204
205func (a *avbAddHashFooter) SignedOutputPath() android.Path {
206	return a.OutputPath() // always signed
207}
208
209// TODO(b/185115783): remove when not needed as input to a prebuilt_etc rule
210var _ android.SourceFileProducer = (*avbAddHashFooter)(nil)
211
212// Implements android.SourceFileProducer
213func (a *avbAddHashFooter) Srcs() android.Paths {
214	if a.output == nil {
215		return nil
216	}
217	return append(android.Paths{}, a.output)
218}
219
220type avbAddHashFooterDefaults struct {
221	android.ModuleBase
222	android.DefaultsModuleBase
223}
224
225// avb_add_hash_footer_defaults provides a set of properties that can be inherited by other
226// avb_add_hash_footer modules. A module can use the properties from an avb_add_hash_footer_defaults
227// using `defaults: ["<:default_module_name>"]`. Properties of both modules are erged (when
228// possible) by prepending the default module's values to the depending module's values.
229func avbAddHashFooterDefaultsFactory() android.Module {
230	module := &avbAddHashFooterDefaults{}
231	module.AddProperties(&avbAddHashFooterProperties{})
232	android.InitDefaultsModule(module)
233	return module
234}
235