blob: ff348a8449c40760320127ef5671cb1981d1c2f3 [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"
19 "io"
20
21 "android/soong/android"
22 "github.com/google/blueprint/proptools"
23)
24
25var String = proptools.String
26
27func init() {
28 android.RegisterModuleType("apex_key", apexKeyFactory)
29}
30
31type apexKey struct {
32 android.ModuleBase
33
34 properties apexKeyProperties
35
36 public_key_file android.Path
37 private_key_file android.Path
38
39 keyName string
40}
41
42type apexKeyProperties struct {
43 // Path to the public key file in avbpubkey format. Installed to the device.
44 // Base name of the file is used as the ID for the key.
45 Public_key *string
46 // Path to the private key file in pem format. Used to sign APEXs.
47 Private_key *string
48}
49
50func apexKeyFactory() android.Module {
51 module := &apexKey{}
52 module.AddProperties(&module.properties)
53 android.InitAndroidModule(module)
54 return module
55}
56
57func (m *apexKey) DepsMutator(ctx android.BottomUpMutatorContext) {
58}
59
60func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
61 m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
62 m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
63
64 pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())]
65 privKeyName := m.private_key_file.Base()[0 : len(m.private_key_file.Base())-len(m.private_key_file.Ext())]
66
67 if pubKeyName != privKeyName {
68 ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
69 m.public_key_file.String(), pubKeyName, m.private_key_file, privKeyName)
70 return
71 }
72 m.keyName = pubKeyName
73
74 ctx.InstallFile(android.PathForModuleInstall(ctx, "etc/security/apex"), m.keyName, m.public_key_file)
75}
76
77func (m *apexKey) AndroidMk() android.AndroidMkData {
78 return android.AndroidMkData{
79 Class: "ETC",
80 OutputFile: android.OptionalPathForPath(m.public_key_file),
81 Extra: []android.AndroidMkExtraFunc{
82 func(w io.Writer, outputFile android.Path) {
83 fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", "$(TARGET_OUT)/etc/security/apex")
84 fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.keyName)
85 },
86 },
87 }
88}