xref: /aosp_15_r20/build/soong/etc/otacerts_zip.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2024 Google Inc. All rights reserved.
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 etc
16
17import (
18	"android/soong/android"
19
20	"github.com/google/blueprint/proptools"
21)
22
23func init() {
24	RegisterOtacertsZipBuildComponents(android.InitRegistrationContext)
25}
26
27func RegisterOtacertsZipBuildComponents(ctx android.RegistrationContext) {
28	ctx.RegisterModuleType("otacerts_zip", otacertsZipFactory)
29}
30
31type otacertsZipProperties struct {
32	// Make this module available when building for recovery.
33	// Only the recovery partition is available.
34	Recovery_available *bool
35
36	// Optional subdirectory under which the zip file is installed into.
37	Relative_install_path *string
38
39	// Optional name for the installed file. If unspecified, otacerts.zip is used.
40	Filename *string
41}
42
43type otacertsZipModule struct {
44	android.ModuleBase
45
46	properties otacertsZipProperties
47	outputPath android.Path
48}
49
50// otacerts_zip collects key files defined in PRODUCT_DEFAULT_DEV_CERTIFICATE
51// and PRODUCT_EXTRA_OTA_KEYS for system or PRODUCT_EXTRA_RECOVERY_KEYS for
52// recovery image. The output file (otacerts.zip by default) is installed into
53// the relative_install_path directory under the etc directory of the target
54// partition.
55func otacertsZipFactory() android.Module {
56	module := &otacertsZipModule{}
57	module.AddProperties(&module.properties)
58	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
59	return module
60}
61
62var _ android.ImageInterface = (*otacertsZipModule)(nil)
63
64func (m *otacertsZipModule) ImageMutatorBegin(ctx android.ImageInterfaceContext) {}
65
66func (m *otacertsZipModule) VendorVariantNeeded(ctx android.ImageInterfaceContext) bool {
67	return false
68}
69
70func (m *otacertsZipModule) ProductVariantNeeded(ctx android.ImageInterfaceContext) bool {
71	return false
72}
73
74func (m *otacertsZipModule) CoreVariantNeeded(ctx android.ImageInterfaceContext) bool {
75	return !m.ModuleBase.InstallInRecovery()
76}
77
78func (m *otacertsZipModule) RamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
79	return false
80}
81
82func (m *otacertsZipModule) VendorRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
83	return false
84}
85
86func (m *otacertsZipModule) DebugRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
87	return false
88}
89
90func (m *otacertsZipModule) RecoveryVariantNeeded(ctx android.ImageInterfaceContext) bool {
91	return proptools.Bool(m.properties.Recovery_available) || m.ModuleBase.InstallInRecovery()
92}
93
94func (m *otacertsZipModule) ExtraImageVariations(ctx android.ImageInterfaceContext) []string {
95	return nil
96}
97
98func (m *otacertsZipModule) SetImageVariation(ctx android.ImageInterfaceContext, variation string) {
99}
100
101func (m *otacertsZipModule) InRecovery() bool {
102	return m.ModuleBase.InRecovery() || m.ModuleBase.InstallInRecovery()
103}
104
105func (m *otacertsZipModule) InstallInRecovery() bool {
106	return m.InRecovery()
107}
108
109func (m *otacertsZipModule) outputFileName() string {
110	// Use otacerts.zip if not specified.
111	return proptools.StringDefault(m.properties.Filename, "otacerts.zip")
112}
113
114func (m *otacertsZipModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
115	// Read .x509.pem file defined in PRODUCT_DEFAULT_DEV_CERTIFICATE or the default test key.
116	pem, _ := ctx.Config().DefaultAppCertificate(ctx)
117	// Read .x509.pem files listed  in PRODUCT_EXTRA_OTA_KEYS or PRODUCT_EXTRA_RECOVERY_KEYS.
118	extras := ctx.Config().ExtraOtaKeys(ctx, m.InRecovery())
119	srcPaths := append([]android.SourcePath{pem}, extras...)
120	outputPath := android.PathForModuleOut(ctx, m.outputFileName())
121
122	rule := android.NewRuleBuilder(pctx, ctx)
123	cmd := rule.Command().BuiltTool("soong_zip").
124		FlagWithOutput("-o ", outputPath).
125		Flag("-j ").
126		Flag("-symlinks=false ")
127	for _, src := range srcPaths {
128		cmd.FlagWithInput("-f ", src)
129	}
130	rule.Build(ctx.ModuleName(), "Generating the otacerts zip file")
131
132	installPath := android.PathForModuleInstall(ctx, "etc", proptools.String(m.properties.Relative_install_path))
133	ctx.InstallFile(installPath, m.outputFileName(), outputPath)
134	m.outputPath = outputPath
135}
136
137func (m *otacertsZipModule) AndroidMkEntries() []android.AndroidMkEntries {
138	nameSuffix := ""
139	if m.InRecovery() {
140		nameSuffix = ".recovery"
141	}
142	return []android.AndroidMkEntries{android.AndroidMkEntries{
143		Class:      "ETC",
144		SubName:    nameSuffix,
145		OutputFile: android.OptionalPathForPath(m.outputPath),
146	}}
147}
148