• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) onlyInRecovery() bool {
115	return m.ModuleBase.InstallInRecovery()
116}
117
118func (m *otacertsZipModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
119	// Read .x509.pem file defined in PRODUCT_DEFAULT_DEV_CERTIFICATE or the default test key.
120	pem, _ := ctx.Config().DefaultAppCertificate(ctx)
121	// Read .x509.pem files listed  in PRODUCT_EXTRA_OTA_KEYS or PRODUCT_EXTRA_RECOVERY_KEYS.
122	extras := ctx.Config().ExtraOtaKeys(ctx, m.InRecovery())
123	srcPaths := append([]android.SourcePath{pem}, extras...)
124	outputPath := android.PathForModuleOut(ctx, m.outputFileName())
125
126	rule := android.NewRuleBuilder(pctx, ctx)
127	cmd := rule.Command().BuiltTool("soong_zip").
128		FlagWithOutput("-o ", outputPath).
129		Flag("-j ").
130		Flag("-symlinks=false ")
131	for _, src := range srcPaths {
132		cmd.FlagWithInput("-f ", src)
133	}
134	rule.Build(ctx.ModuleName(), "Generating the otacerts zip file")
135
136	installPath := android.PathForModuleInstall(ctx, "etc", proptools.String(m.properties.Relative_install_path))
137	ctx.InstallFile(installPath, m.outputFileName(), outputPath)
138	m.outputPath = outputPath
139}
140
141func (m *otacertsZipModule) AndroidMkEntries() []android.AndroidMkEntries {
142	nameSuffix := ""
143	if m.InRecovery() && !m.onlyInRecovery() {
144		nameSuffix = ".recovery"
145	}
146	return []android.AndroidMkEntries{android.AndroidMkEntries{
147		Class:      "ETC",
148		SubName:    nameSuffix,
149		OutputFile: android.OptionalPathForPath(m.outputPath),
150	}}
151}
152