Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package apex |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 19 | "sort" |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 20 | "strings" |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 21 | |
| 22 | "android/soong/android" |
Colin Cross | 5f692ec | 2019-02-01 16:53:07 -0800 | [diff] [blame] | 23 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
| 27 | var String = proptools.String |
| 28 | |
| 29 | func init() { |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame^] | 30 | android.RegisterModuleType("apex_key", ApexKeyFactory) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 31 | android.RegisterSingletonType("apex_keys_text", apexKeysTextFactory) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type 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 | |
| 45 | type apexKeyProperties struct { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 46 | // Path or module to the public key file in avbpubkey format. Installed to the device. |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 47 | // Base name of the file is used as the ID for the key. |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 48 | 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 Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 51 | |
| 52 | // Whether this key is installable to one of the partitions. Defualt: true. |
| 53 | Installable *bool |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 54 | } |
| 55 | |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame^] | 56 | func ApexKeyFactory() android.Module { |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 57 | module := &apexKey{} |
| 58 | module.AddProperties(&module.properties) |
dimitry | f807167 | 2019-04-11 17:27:11 +0200 | [diff] [blame] | 59 | android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon) |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 60 | return module |
| 61 | } |
| 62 | |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 63 | func (m *apexKey) installable() bool { |
Jiyong Park | 42cca6c | 2019-04-01 11:15:50 +0900 | [diff] [blame] | 64 | return false |
Jiyong Park | 50d9920 | 2018-12-27 13:32:34 +0900 | [diff] [blame] | 65 | } |
| 66 | |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 67 | func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 68 | // If the keys are from other modules (i.e. :module syntax) respect it. |
| 69 | // Otherwise, try to locate the key files in the default cert dir or |
| 70 | // in the local module dir |
| 71 | if android.SrcIsModule(String(m.properties.Public_key)) != "" { |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 72 | m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 73 | } else { |
| 74 | m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key)) |
| 75 | // If not found, fall back to the local key pairs |
| 76 | if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() { |
| 77 | m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key)) |
| 78 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 79 | } |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 80 | |
| 81 | if android.SrcIsModule(String(m.properties.Private_key)) != "" { |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 82 | m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
Jiyong Park | 6788256 | 2019-03-21 01:11:21 +0900 | [diff] [blame] | 83 | } else { |
| 84 | m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key)) |
| 85 | if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() { |
| 86 | m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key)) |
| 87 | } |
Jiyong Park | 9335a26 | 2018-12-24 11:31:58 +0900 | [diff] [blame] | 88 | } |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 89 | |
| 90 | pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())] |
| 91 | privKeyName := m.private_key_file.Base()[0 : len(m.private_key_file.Base())-len(m.private_key_file.Ext())] |
| 92 | |
Jaewoong Jung | 939ebd5 | 2019-03-26 15:07:36 -0700 | [diff] [blame] | 93 | if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName { |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 94 | ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname", |
| 95 | m.public_key_file.String(), pubKeyName, m.private_key_file, privKeyName) |
| 96 | return |
| 97 | } |
| 98 | m.keyName = pubKeyName |
Jiyong Park | ff1458f | 2018-10-12 21:49:38 +0900 | [diff] [blame] | 99 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 100 | |
| 101 | //////////////////////////////////////////////////////////////////////// |
| 102 | // apex_keys_text |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 103 | type apexKeysText struct { |
| 104 | output android.OutputPath |
| 105 | } |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 106 | |
| 107 | func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) { |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 108 | s.output = android.PathForOutput(ctx, "apexkeys.txt") |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 109 | apexModulesMap := make(map[string]android.Module) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 110 | ctx.VisitAllModules(func(module android.Module) { |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 111 | if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() { |
| 112 | apexModulesMap[m.Name()] = m |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 113 | } |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 114 | }) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 115 | |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 116 | // Find prebuilts and let them override apexBundle if they are preferred |
| 117 | ctx.VisitAllModules(func(module android.Module) { |
| 118 | if m, ok := module.(*Prebuilt); ok && m.Enabled() && m.installable() && |
| 119 | m.Prebuilt().UsePrebuilt() { |
| 120 | apexModulesMap[m.BaseModuleName()] = m |
| 121 | } |
| 122 | }) |
| 123 | |
| 124 | // iterating over map does not give consistent ordering in golang |
| 125 | var moduleNames []string |
| 126 | for key, _ := range apexModulesMap { |
| 127 | moduleNames = append(moduleNames, key) |
| 128 | } |
| 129 | sort.Strings(moduleNames) |
| 130 | |
| 131 | var filecontent strings.Builder |
| 132 | for _, key := range moduleNames { |
| 133 | module := apexModulesMap[key] |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 134 | if m, ok := module.(*apexBundle); ok { |
| 135 | fmt.Fprintf(&filecontent, |
| 136 | "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q\\n", |
| 137 | m.Name()+".apex", |
| 138 | m.public_key_file.String(), |
| 139 | m.private_key_file.String(), |
| 140 | m.container_certificate_file.String(), |
| 141 | m.container_private_key_file.String()) |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 142 | } else if m, ok := module.(*Prebuilt); ok { |
| 143 | fmt.Fprintf(&filecontent, |
| 144 | "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q\\n", |
| 145 | m.InstallFilename(), |
| 146 | "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED") |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 147 | } |
Jiyong Park | 4d27704 | 2019-04-23 18:00:10 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 150 | ctx.Build(pctx, android.BuildParams{ |
| 151 | Rule: android.WriteFile, |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 152 | Description: "apexkeys.txt", |
| 153 | Output: s.output, |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 154 | Args: map[string]string{ |
| 155 | "content": filecontent.String(), |
| 156 | }, |
| 157 | }) |
| 158 | } |
| 159 | |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 160 | func apexKeysTextFactory() android.Singleton { |
| 161 | return &apexKeysText{} |
| 162 | } |
| 163 | |
Jiyong Park | 37eb8bb | 2019-02-20 22:23:29 +0900 | [diff] [blame] | 164 | func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) { |
| 165 | ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String()) |
Jiyong Park | 0ca3ce8 | 2019-02-18 15:25:04 +0900 | [diff] [blame] | 166 | } |