1 /******************************************************************************
2 *
3 * Copyright (C) 1999-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 * Construct a buffer that contains multiple Type-Length-Value contents
21 * that is used by the HAL in a CORE_SET_CONFIG NCI command.
22 ******************************************************************************/
23
24 #define LOG_TAG "NfcNciHal"
25 #include "StartupConfig.h"
26 #include "_OverrideLog.h"
27
28 const uint8_t StartupConfig::mMaxLength = 255;
29
30 /*******************************************************************************
31 **
32 ** Function: initialize
33 **
34 ** Description: Initialize all member variables.
35 **
36 ** Returns: None
37 **
38 *******************************************************************************/
StartupConfig()39 StartupConfig::StartupConfig() {
40 // set first byte to 0, which is length of payload
41 mBuffer.append((uint8_string::size_type)1, (uint8_string::value_type)0);
42 }
43
44 /*******************************************************************************
45 **
46 ** Function: initialize
47 **
48 ** Description: Reset all member variables.
49 **
50 ** Returns: None
51 **
52 *******************************************************************************/
initialize()53 void StartupConfig::initialize() {
54 mBuffer.clear();
55 // set first byte to 0, which is length of payload
56 mBuffer.append((uint8_string::size_type)1, (uint8_string::value_type)0);
57 }
58
59 /*******************************************************************************
60 **
61 ** Function: getInternalBuffer
62 **
63 ** Description: Get the pointer to buffer that contains multiple
64 ** Type-Length-Value contents.
65 **
66 ** Returns: Pointer to buffer.
67 **
68 *******************************************************************************/
getInternalBuffer()69 const uint8_t* StartupConfig::getInternalBuffer() { return mBuffer.data(); }
70
71 /*******************************************************************************
72 **
73 ** Function: append
74 **
75 ** Description: Append new config data to internal buffer.
76 ** newContent: buffer containing new content; newContent[0] is
77 ** payload length; newContent[1..end] is payload.
78 ** newContentLen: total length of newContent.
79 **
80 ** Returns: True if ok.
81 **
82 *******************************************************************************/
append(const uint8_t * newContent,uint8_t newContentLen)83 bool StartupConfig::append(const uint8_t* newContent, uint8_t newContentLen) {
84 static const char fn[] = "StartupConfig::append";
85 if ((newContentLen + mBuffer.size()) > mMaxLength) {
86 ALOGE("%s: exceed max length", fn);
87 return false;
88 }
89
90 ALOGD("%s: try append %u bytes", fn,
91 (uint8_string::size_type)(newContentLen));
92 // append new payload into private buffer
93 mBuffer.append(newContent + 1, (uint8_string::size_type)(newContentLen - 1));
94 // increase size counter of payload in private buffer
95 mBuffer[0] = mBuffer[0] + newContentLen - 1;
96 ALOGD("%s: new size %u bytes", fn, mBuffer[0]);
97 return true;
98 };
99
100 /*******************************************************************************
101 **
102 ** Function: disableSecureElement
103 **
104 ** Description: Adjust a TLV to disable secure element(s). The TLV's type
105 *is 0xC2.
106 ** bitmask: 0xC0 = do not detect any secure element.
107 ** 0x40 = do not detect secure element in slot 0.
108 ** 0x80 = do not detect secure element in slot 1.
109 **
110 ** Returns: True if ok.
111 **
112 *******************************************************************************/
disableSecureElement(uint8_t bitmask)113 bool StartupConfig::disableSecureElement(uint8_t bitmask) {
114 const uint8_t maxLen = mBuffer[0];
115 uint8_t index = 1, tlvType = 0, tlvLen = 0;
116 bool found0xC2 = false;
117
118 while (true) {
119 if (index > maxLen) break;
120 tlvType = mBuffer[index];
121 index++;
122 tlvLen = mBuffer[index];
123 index++;
124 if (tlvType == 0xC2) // this TLV controls secure elements
125 {
126 index++; // index of second byte in TLV's value
127 mBuffer[index] = mBuffer[index] | bitmask; // turn on certain bits
128 found0xC2 = true;
129 } else
130 index += tlvLen;
131 }
132
133 if (found0xC2 == false) {
134 uint8_t tlv[] = {0x04, 0xC2, 0x02, 0x61, 0x00};
135 tlv[4] = tlv[4] | bitmask;
136 found0xC2 = append(tlv, 5);
137 }
138 return found0xC2;
139 }
140