xref: /aosp_15_r20/build/soong/filesystem/avb_gen_vbmeta_image.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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
20	"github.com/google/blueprint/proptools"
21
22	"android/soong/android"
23)
24
25type avbGenVbmetaImage struct {
26	android.ModuleBase
27	android.DefaultableModuleBase
28
29	properties avbGenVbmetaImageProperties
30
31	output     android.Path
32	installDir android.InstallPath
33}
34
35type avbGenVbmetaImageProperties struct {
36	// Source file of this image. Can reference a genrule type module with the ":module" syntax.
37	Src *string `android:"path,arch_variant"`
38
39	// Name of the image partition. Defaults to the name of this module.
40	Partition_name *string
41
42	// The salt in hex. Required for reproducible builds.
43	Salt *string
44}
45
46// The avbGenVbmetaImage generates an unsigned VBMeta image output for the given image.
47func avbGenVbmetaImageFactory() android.Module {
48	module := &avbGenVbmetaImage{}
49	module.AddProperties(&module.properties)
50	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
51	android.InitDefaultableModule(module)
52	return module
53}
54
55func (a *avbGenVbmetaImage) installFileName() string {
56	return a.Name() + ".img"
57}
58
59func (a *avbGenVbmetaImage) GenerateAndroidBuildActions(ctx android.ModuleContext) {
60	builder := android.NewRuleBuilder(pctx, ctx)
61	cmd := builder.Command().BuiltTool("avbtool").Text("add_hash_footer")
62	cmd.Flag("--dynamic_partition_size")
63	cmd.Flag("--do_not_append_vbmeta_image")
64
65	partition_name := proptools.StringDefault(a.properties.Partition_name, a.Name())
66	cmd.FlagWithArg("--partition_name ", partition_name)
67
68	if a.properties.Src == nil {
69		ctx.PropertyErrorf("src", "missing source file")
70		return
71	}
72	input := android.PathForModuleSrc(ctx, proptools.String(a.properties.Src))
73	cmd.FlagWithInput("--image ", input)
74
75	if a.properties.Salt == nil {
76		ctx.PropertyErrorf("salt", "missing salt value")
77		return
78	}
79	cmd.FlagWithArg("--salt ", proptools.String(a.properties.Salt))
80
81	output := android.PathForModuleOut(ctx, a.installFileName())
82	cmd.FlagWithOutput("--output_vbmeta_image ", output)
83	builder.Build("avbGenVbmetaImage", fmt.Sprintf("avbGenVbmetaImage %s", ctx.ModuleName()))
84
85	ctx.SetOutputFiles([]android.Path{output}, "")
86	a.output = output
87}
88
89var _ android.AndroidMkEntriesProvider = (*avbGenVbmetaImage)(nil)
90
91// Implements android.AndroidMkEntriesProvider
92func (a *avbGenVbmetaImage) AndroidMkEntries() []android.AndroidMkEntries {
93	return []android.AndroidMkEntries{android.AndroidMkEntries{
94		Class:      "ETC",
95		OutputFile: android.OptionalPathForPath(a.output),
96		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
97			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
98				entries.SetString("LOCAL_MODULE_PATH", a.installDir.String())
99				entries.SetString("LOCAL_INSTALLED_MODULE_STEM", a.installFileName())
100			},
101		},
102	}}
103}
104
105type avbGenVbmetaImageDefaults struct {
106	android.ModuleBase
107	android.DefaultsModuleBase
108}
109
110// avb_gen_vbmeta_image_defaults provides a set of properties that can be inherited by other
111// avb_gen_vbmeta_image modules. A module can use the properties from an
112// avb_gen_vbmeta_image_defaults using `defaults: ["<:default_module_name>"]`. Properties of both
113// modules are erged (when possible) by prepending the default module's values to the depending
114// module's values.
115func avbGenVbmetaImageDefaultsFactory() android.Module {
116	module := &avbGenVbmetaImageDefaults{}
117	module.AddProperties(&avbGenVbmetaImageProperties{})
118	android.InitDefaultsModule(module)
119	return module
120}
121