blob: 6ee3dca3f7731b66107634bc52194f188137adf4 [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 Parka41f12a2019-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() {
30 android.RegisterModuleType("apex_key", apexKeyFactory)
Jiyong Park0ca3ce82019-02-18 15:25:04 +090031 android.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
Jiyong Parkff1458f2018-10-12 21:49:38 +090032}
33
34type apexKey struct {
35 android.ModuleBase
36
37 properties apexKeyProperties
38
39 public_key_file android.Path
40 private_key_file android.Path
41
42 keyName string
43}
44
45type apexKeyProperties struct {
Jiyong Park67882562019-03-21 01:11:21 +090046 // Path or module to the public key file in avbpubkey format. Installed to the device.
Jiyong Parkff1458f2018-10-12 21:49:38 +090047 // Base name of the file is used as the ID for the key.
Jiyong Park67882562019-03-21 01:11:21 +090048 Public_key *string `android:"path"`
49 // Path or module to the private key file in pem format. Used to sign APEXs.
50 Private_key *string `android:"path"`
Jiyong Park50d99202018-12-27 13:32:34 +090051
52 // Whether this key is installable to one of the partitions. Defualt: true.
53 Installable *bool
Jiyong Parkff1458f2018-10-12 21:49:38 +090054}
55
56func apexKeyFactory() android.Module {
57 module := &apexKey{}
58 module.AddProperties(&module.properties)
Jiyong Parkd1e293d2019-03-15 02:13:21 +090059 // This module is device-only
60 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jiyong Parkff1458f2018-10-12 21:49:38 +090061 return module
62}
63
Jiyong Park50d99202018-12-27 13:32:34 +090064func (m *apexKey) installable() bool {
Jiyong Park42cca6c2019-04-01 11:15:50 +090065 return false
Jiyong Park50d99202018-12-27 13:32:34 +090066}
67
Jiyong Parkff1458f2018-10-12 21:49:38 +090068func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park67882562019-03-21 01:11:21 +090069 // If the keys are from other modules (i.e. :module syntax) respect it.
70 // Otherwise, try to locate the key files in the default cert dir or
71 // in the local module dir
72 if android.SrcIsModule(String(m.properties.Public_key)) != "" {
Jiyong Park9335a262018-12-24 11:31:58 +090073 m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
Jiyong Park67882562019-03-21 01:11:21 +090074 } else {
75 m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
76 // If not found, fall back to the local key pairs
77 if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
78 m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
79 }
Jiyong Park9335a262018-12-24 11:31:58 +090080 }
Jiyong Park67882562019-03-21 01:11:21 +090081
82 if android.SrcIsModule(String(m.properties.Private_key)) != "" {
Jiyong Park9335a262018-12-24 11:31:58 +090083 m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
Jiyong Park67882562019-03-21 01:11:21 +090084 } else {
85 m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
86 if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
87 m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
88 }
Jiyong Park9335a262018-12-24 11:31:58 +090089 }
Jiyong Parkff1458f2018-10-12 21:49:38 +090090
91 pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())]
92 privKeyName := m.private_key_file.Base()[0 : len(m.private_key_file.Base())-len(m.private_key_file.Ext())]
93
Jaewoong Jung939ebd52019-03-26 15:07:36 -070094 if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName {
Jiyong Parkff1458f2018-10-12 21:49:38 +090095 ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
96 m.public_key_file.String(), pubKeyName, m.private_key_file, privKeyName)
97 return
98 }
99 m.keyName = pubKeyName
Jiyong Parkff1458f2018-10-12 21:49:38 +0900100}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900101
102////////////////////////////////////////////////////////////////////////
103// apex_keys_text
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900104type apexKeysText struct {
105 output android.OutputPath
106}
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900107
108func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) {
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900109 s.output = android.PathForOutput(ctx, "apexkeys.txt")
Jiyong Parka41f12a2019-04-23 18:00:10 +0900110 apexModulesMap := make(map[string]android.Module)
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900111 ctx.VisitAllModules(func(module android.Module) {
Jiyong Parka41f12a2019-04-23 18:00:10 +0900112 if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() {
113 apexModulesMap[m.Name()] = m
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900114 }
Jiyong Parka41f12a2019-04-23 18:00:10 +0900115 })
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900116
Jiyong Parka41f12a2019-04-23 18:00:10 +0900117 // Find prebuilts and let them override apexBundle if they are preferred
118 ctx.VisitAllModules(func(module android.Module) {
119 if m, ok := module.(*Prebuilt); ok && m.Enabled() && m.installable() &&
120 m.Prebuilt().UsePrebuilt() {
121 apexModulesMap[m.BaseModuleName()] = m
122 }
123 })
124
125 // iterating over map does not give consistent ordering in golang
126 var moduleNames []string
127 for key, _ := range apexModulesMap {
128 moduleNames = append(moduleNames, key)
129 }
130 sort.Strings(moduleNames)
131
132 var filecontent strings.Builder
133 for _, key := range moduleNames {
134 module := apexModulesMap[key]
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900135 if m, ok := module.(*apexBundle); ok {
136 fmt.Fprintf(&filecontent,
137 "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q\\n",
138 m.Name()+".apex",
139 m.public_key_file.String(),
140 m.private_key_file.String(),
141 m.container_certificate_file.String(),
142 m.container_private_key_file.String())
Jiyong Parka41f12a2019-04-23 18:00:10 +0900143 } else if m, ok := module.(*Prebuilt); ok {
144 fmt.Fprintf(&filecontent,
145 "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q\\n",
146 m.InstallFilename(),
147 "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED")
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900148 }
Jiyong Parka41f12a2019-04-23 18:00:10 +0900149 }
150
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900151 ctx.Build(pctx, android.BuildParams{
152 Rule: android.WriteFile,
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900153 Description: "apexkeys.txt",
154 Output: s.output,
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900155 Args: map[string]string{
156 "content": filecontent.String(),
157 },
158 })
159}
160
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900161func apexKeysTextFactory() android.Singleton {
162 return &apexKeysText{}
163}
164
Jiyong Park37eb8bb2019-02-20 22:23:29 +0900165func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) {
166 ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String())
Jiyong Park0ca3ce82019-02-18 15:25:04 +0900167}