blob: 8b33b593f2fd7c34de61154c1fa823c58ffb8b46 [file] [log] [blame]
Jiyong Parkff1458f2018-10-12 21:49:38 +09001// Copyright (C) 2018 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 apex
16
17import (
18 "fmt"
Jiyong Park4d277042019-04-23 18:00:10 +090019 "sort"
Jiyong Park0ca3ce82019-02-18 15:25:04 +090020 "strings"
Jiyong Parkff1458f2018-10-12 21:49:38 +090021
22 "android/soong/android"
Colin Cross5f692ec2019-02-01 16:53:07 -080023
Jiyong Parkff1458f2018-10-12 21:49:38 +090024 "github.com/google/blueprint/proptools"
25)
26
27var String = proptools.String
28
29func init() {
Paul Duffin667893c2021-03-09 22:34:13 +000030 registerApexKeyBuildComponents(android.InitRegistrationContext)
31}
32
33func registerApexKeyBuildComponents(ctx android.RegistrationContext) {
34 ctx.RegisterModuleType("apex_key", ApexKeyFactory)
35 ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
Jiyong Parkff1458f2018-10-12 21:49:38 +090036}
37
38type apexKey struct {
39 android.ModuleBase
40
41 properties apexKeyProperties
42
Jaewoong Jung18aefc12020-12-21 09:11:10 -080043 publicKeyFile android.Path
44 privateKeyFile android.Path
Jiyong Parkff1458f2018-10-12 21:49:38 +090045
46 keyName string
47}
48
49type apexKeyProperties struct {
Jiyong Park67882562019-03-21 01:11:21 +090050 // Path or module to the public key file in avbpubkey format. Installed to the device.
Jiyong Parkff1458f2018-10-12 21:49:38 +090051 // Base name of the file is used as the ID for the key.
Jiyong Park67882562019-03-21 01:11:21 +090052 Public_key *string `android:"path"`
53 // Path or module to the private key file in pem format. Used to sign APEXs.
54 Private_key *string `android:"path"`
Jiyong Park50d99202018-12-27 13:32:34 +090055
56 // Whether this key is installable to one of the partitions. Defualt: true.
57 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090058}
59
Jiyong Parkd1063c12019-07-17 20:08:41 +090060func ApexKeyFactory() android.Module {
Jiyong Parkff1458f2018-10-12 21:49:38 +090061 module := &apexKey{}
62 module.AddProperties(&module.properties)
dimitryf8071672019-04-11 17:27:11 +020063 android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon)
Jiyong Parkff1458f2018-10-12 21:49:38 +090064 return module
65}
66
Jiyong Park50d99202018-12-27 13:32:34 +090067func (m *apexKey) installable() bool {
Jiyong Park42cca6c2019-04-01 11:15:50 +090068 return false
Jiyong Park50d99202018-12-27 13:32:34 +090069}
70
Jiyong Parkff1458f2018-10-12 21:49:38 +090071func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park67882562019-03-21 01:11:21 +090072 // If the keys are from other modules (i.e. :module syntax) respect it.
73 // Otherwise, try to locate the key files in the default cert dir or
74 // in the local module dir
75 if android.SrcIsModule(String(m.properties.Public_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080076 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090077 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080078 m.publicKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090079 // If not found, fall back to the local key pairs
Jaewoong Jung18aefc12020-12-21 09:11:10 -080080 if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() {
81 m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090082 }
Jiyong Park9335a262018-12-24 11:31:58 +090083 }
Jiyong Park67882562019-03-21 01:11:21 +090084
85 if android.SrcIsModule(String(m.properties.Private_key)) != "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080086 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090087 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -080088 m.privateKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
89 if !android.ExistentPathForSource(ctx, m.privateKeyFile.String()).Valid() {
90 m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090091 }
Jiyong Park9335a262018-12-24 11:31:58 +090092 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090093
Jaewoong Jung18aefc12020-12-21 09:11:10 -080094 pubKeyName := m.publicKeyFile.Base()[0 : len(m.publicKeyFile.Base())-len(m.publicKeyFile.Ext())]
95 privKeyName := m.privateKeyFile.Base()[0 : len(m.privateKeyFile.Base())-len(m.privateKeyFile.Ext())]
Jiyong Parkff1458f2018-10-12 21:49:38 +090096
Jaewoong Jung939ebd52019-03-26 15:07:36 -070097 if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName {
Jiyong Parkff1458f2018-10-12 21:49:38 +090098 ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
Jaewoong Jung18aefc12020-12-21 09:11:10 -080099 m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName)
Jiyong Parkff1458f2018-10-12 21:49:38 +0900100 return
101 }
102 m.keyName = pubKeyName
Jiyong Parkff1458f2018-10-12 21:49:38 +0900103}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900104
105////////////////////////////////////////////////////////////////////////
106// apex_keys_text
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900107type apexKeysText struct {
108 output android.OutputPath
109}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900110
111func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) {
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900112 s.output = android.PathForOutput(ctx, "apexkeys.txt")
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900113 type apexKeyEntry struct {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800114 name string
115 presigned bool
116 publicKey string
117 privateKey string
118 containerCertificate string
119 containerPrivateKey string
120 partition string
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900121 }
122 toString := func(e apexKeyEntry) string {
Colin Crosscf371cc2020-11-13 11:48:42 -0800123 format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q\n"
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900124 if e.presigned {
125 return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition)
126 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800127 return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900128 }
129 }
130
131 apexKeyMap := make(map[string]apexKeyEntry)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900132 ctx.VisitAllModules(func(module android.Module) {
Jiyong Park4d277042019-04-23 18:00:10 +0900133 if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() {
Jiyong Parkb81b9902020-11-24 19:51:18 +0900134 pem, key := m.getCertificateAndPrivateKey(ctx)
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900135 apexKeyMap[m.Name()] = apexKeyEntry{
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800136 name: m.Name() + ".apex",
137 presigned: false,
138 publicKey: m.publicKeyFile.String(),
139 privateKey: m.privateKeyFile.String(),
140 containerCertificate: pem.String(),
141 containerPrivateKey: key.String(),
142 partition: m.PartitionTag(ctx.DeviceConfig()),
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900143 }
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900144 }
Jiyong Park4d277042019-04-23 18:00:10 +0900145 })
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900146
Jiyong Park4d277042019-04-23 18:00:10 +0900147 // Find prebuilts and let them override apexBundle if they are preferred
148 ctx.VisitAllModules(func(module android.Module) {
149 if m, ok := module.(*Prebuilt); ok && m.Enabled() && m.installable() &&
150 m.Prebuilt().UsePrebuilt() {
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900151 apexKeyMap[m.BaseModuleName()] = apexKeyEntry{
152 name: m.InstallFilename(),
153 presigned: true,
154 partition: m.PartitionTag(ctx.DeviceConfig()),
155 }
156 }
157 })
158
159 // Find apex_set and let them override apexBundle or prebuilts. This is done in a separate pass
160 // so that apex_set are not overridden by prebuilts.
161 ctx.VisitAllModules(func(module android.Module) {
162 if m, ok := module.(*ApexSet); ok && m.Enabled() {
163 entry := apexKeyEntry{
164 name: m.InstallFilename(),
165 presigned: true,
166 partition: m.PartitionTag(ctx.DeviceConfig()),
167 }
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900168 apexKeyMap[m.BaseModuleName()] = entry
Jiyong Park4d277042019-04-23 18:00:10 +0900169 }
170 })
171
172 // iterating over map does not give consistent ordering in golang
173 var moduleNames []string
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900174 for key, _ := range apexKeyMap {
Jiyong Park4d277042019-04-23 18:00:10 +0900175 moduleNames = append(moduleNames, key)
176 }
177 sort.Strings(moduleNames)
178
179 var filecontent strings.Builder
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900180 for _, name := range moduleNames {
Colin Crosscf371cc2020-11-13 11:48:42 -0800181 filecontent.WriteString(toString(apexKeyMap[name]))
Jiyong Park4d277042019-04-23 18:00:10 +0900182 }
Colin Crosscf371cc2020-11-13 11:48:42 -0800183 android.WriteFileRule(ctx, s.output, filecontent.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900184}
185
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900186func apexKeysTextFactory() android.Singleton {
187 return &apexKeysText{}
188}
189
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900190func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) {
191 ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900192}