blob: b6f175a2dfc6bc2bc90bce655c89b46746c506b1 [file] [log] [blame]
Justin Yun635e7882024-07-04 14:50:57 +09001// 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.OutputPath
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.BaseModuleContext) {}
65
66func (m *otacertsZipModule) VendorVariantNeeded(ctx android.BaseModuleContext) bool {
67 return false
68}
69
70func (m *otacertsZipModule) ProductVariantNeeded(ctx android.BaseModuleContext) bool {
71 return false
72}
73
74func (m *otacertsZipModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
75 return !m.ModuleBase.InstallInRecovery()
76}
77
78func (m *otacertsZipModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
79 return false
80}
81
82func (m *otacertsZipModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
83 return false
84}
85
86func (m *otacertsZipModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
87 return false
88}
89
90func (m *otacertsZipModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
91 return proptools.Bool(m.properties.Recovery_available) || m.ModuleBase.InstallInRecovery()
92}
93
94func (m *otacertsZipModule) ExtraImageVariations(ctx android.BaseModuleContext) []string {
95 return nil
96}
97
98func (m *otacertsZipModule) SetImageVariation(ctx android.BaseModuleContext, 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 m.outputPath = android.PathForModuleOut(ctx, m.outputFileName()).OutputPath
121
122 rule := android.NewRuleBuilder(pctx, ctx)
123 cmd := rule.Command().BuiltTool("soong_zip").
124 FlagWithOutput("-o ", m.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(), m.outputPath)
134}
135
136func (m *otacertsZipModule) AndroidMkEntries() []android.AndroidMkEntries {
137 nameSuffix := ""
138 if m.InRecovery() {
139 nameSuffix = ".recovery"
140 }
141 return []android.AndroidMkEntries{android.AndroidMkEntries{
142 Class: "ETC",
143 SubName: nameSuffix,
144 OutputFile: android.OptionalPathForPath(m.outputPath),
145 }}
146}