Andrew Duggan | 052556f | 2014-04-16 11:32:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Andrew Duggan |
| 3 | * Copyright (C) 2014 Synaptics Inc |
| 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 | |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 18 | #include <alloca.h> |
| 19 | #include <time.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <unistd.h> |
| 23 | #include <string.h> |
| 24 | #include <stdlib.h> |
Andrew Duggan | 6213791 | 2014-05-06 16:06:19 -0700 | [diff] [blame] | 25 | #include <errno.h> |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 26 | |
| 27 | #include "rmi4update.h" |
| 28 | |
| 29 | #define RMI_F34_QUERY_SIZE 7 |
| 30 | #define RMI_F34_HAS_NEW_REG_MAP (1 << 0) |
| 31 | #define RMI_F34_IS_UNLOCKED (1 << 1) |
| 32 | #define RMI_F34_HAS_CONFIG_ID (1 << 2) |
| 33 | #define RMI_F34_BLOCK_SIZE_OFFSET 1 |
| 34 | #define RMI_F34_FW_BLOCKS_OFFSET 3 |
| 35 | #define RMI_F34_CONFIG_BLOCKS_OFFSET 5 |
| 36 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 37 | #define RMI_F34_BLOCK_SIZE_V1_OFFSET 0 |
| 38 | #define RMI_F34_FW_BLOCKS_V1_OFFSET 0 |
| 39 | #define RMI_F34_CONFIG_BLOCKS_V1_OFFSET 2 |
| 40 | |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 41 | #define RMI_F34_BLOCK_DATA_OFFSET 2 |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 42 | #define RMI_F34_BLOCK_DATA_V1_OFFSET 1 |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 43 | |
| 44 | #define RMI_F34_COMMAND_MASK 0x0F |
| 45 | #define RMI_F34_STATUS_MASK 0x07 |
| 46 | #define RMI_F34_STATUS_SHIFT 4 |
| 47 | #define RMI_F34_ENABLED_MASK 0x80 |
| 48 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 49 | #define RMI_F34_COMMAND_V1_MASK 0x3F |
| 50 | #define RMI_F34_STATUS_V1_MASK 0x3F |
| 51 | #define RMI_F34_ENABLED_V1_MASK 0x80 |
| 52 | |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 53 | #define RMI_F34_WRITE_FW_BLOCK 0x02 |
| 54 | #define RMI_F34_ERASE_ALL 0x03 |
| 55 | #define RMI_F34_WRITE_LOCKDOWN_BLOCK 0x04 |
| 56 | #define RMI_F34_WRITE_CONFIG_BLOCK 0x06 |
| 57 | #define RMI_F34_ENABLE_FLASH_PROG 0x0f |
| 58 | |
| 59 | #define RMI_F34_ENABLE_WAIT_MS 300 |
| 60 | #define RMI_F34_ERASE_WAIT_MS (5 * 1000) |
| 61 | #define RMI_F34_IDLE_WAIT_MS 500 |
| 62 | |
| 63 | /* Most recent device status event */ |
| 64 | #define RMI_F01_STATUS_CODE(status) ((status) & 0x0f) |
| 65 | /* Indicates that flash programming is enabled (bootloader mode). */ |
| 66 | #define RMI_F01_STATUS_BOOTLOADER(status) (!!((status) & 0x40)) |
| 67 | /* The device has lost its configuration for some reason. */ |
| 68 | #define RMI_F01_STATUS_UNCONFIGURED(status) (!!((status) & 0x80)) |
| 69 | |
| 70 | /* |
| 71 | * Sleep mode controls power management on the device and affects all |
| 72 | * functions of the device. |
| 73 | */ |
| 74 | #define RMI_F01_CTRL0_SLEEP_MODE_MASK 0x03 |
| 75 | |
| 76 | #define RMI_SLEEP_MODE_NORMAL 0x00 |
| 77 | #define RMI_SLEEP_MODE_SENSOR_SLEEP 0x01 |
| 78 | #define RMI_SLEEP_MODE_RESERVED0 0x02 |
| 79 | #define RMI_SLEEP_MODE_RESERVED1 0x03 |
| 80 | |
| 81 | /* |
| 82 | * This bit disables whatever sleep mode may be selected by the sleep_mode |
| 83 | * field and forces the device to run at full power without sleeping. |
| 84 | */ |
| 85 | #define RMI_F01_CRTL0_NOSLEEP_BIT (1 << 2) |
| 86 | |
Satoshi Noguchi | a0b675a | 2014-09-29 02:38:14 -0700 | [diff] [blame] | 87 | int RMI4Update::UpdateFirmware(bool force, bool performLockdown) |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 88 | { |
| 89 | struct timespec start; |
| 90 | struct timespec end; |
Andrew Duggan | 65e5553 | 2014-04-04 16:59:54 -0700 | [diff] [blame] | 91 | long long int duration_us = 0; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 92 | int rc; |
| 93 | const unsigned char eraseAll = RMI_F34_ERASE_ALL; |
| 94 | |
| 95 | rc = FindUpdateFunctions(); |
| 96 | if (rc != UPDATE_SUCCESS) |
| 97 | return rc; |
| 98 | |
| 99 | rc = m_device.QueryBasicProperties(); |
| 100 | if (rc < 0) |
| 101 | return UPDATE_FAIL_QUERY_BASIC_PROPERTIES; |
| 102 | |
| 103 | fprintf(stdout, "Device Properties:\n"); |
| 104 | m_device.PrintProperties(); |
| 105 | |
Andrew Duggan | dc83855 | 2014-11-11 07:58:09 -0800 | [diff] [blame^] | 106 | rc = DisableNonessentialInterupts(); |
| 107 | if (rc != UPDATE_SUCCESS) |
| 108 | return rc; |
| 109 | |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 110 | rc = ReadF34Queries(); |
| 111 | if (rc != UPDATE_SUCCESS) |
| 112 | return rc; |
| 113 | |
Andrew Duggan | 050db0e | 2014-07-29 12:42:49 -0700 | [diff] [blame] | 114 | rc = m_firmwareImage.VerifyImageMatchesDevice(GetFirmwareSize(), GetConfigSize()); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 115 | if (rc != UPDATE_SUCCESS) |
| 116 | return rc; |
| 117 | |
| 118 | rc = EnterFlashProgramming(); |
| 119 | if (rc != UPDATE_SUCCESS) { |
| 120 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 121 | return rc; |
| 122 | } |
| 123 | |
| 124 | if (!force && m_firmwareImage.HasIO()) { |
| 125 | if (m_firmwareImage.GetFirmwareID() <= m_device.GetFirmwareID()) { |
| 126 | m_device.Reset(); |
| 127 | return UPDATE_FAIL_FIRMWARE_IMAGE_IS_OLDER; |
| 128 | } |
| 129 | } |
| 130 | |
Satoshi Noguchi | a0b675a | 2014-09-29 02:38:14 -0700 | [diff] [blame] | 131 | if (performLockdown && m_unlocked) { |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 132 | if (m_firmwareImage.GetLockdownData()) { |
| 133 | fprintf(stdout, "Writing lockdown...\n"); |
| 134 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 135 | rc = WriteBlocks(m_firmwareImage.GetLockdownData(), |
| 136 | m_firmwareImage.GetLockdownSize() / 0x10, |
| 137 | RMI_F34_WRITE_LOCKDOWN_BLOCK); |
| 138 | if (rc != UPDATE_SUCCESS) { |
| 139 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 140 | return rc; |
| 141 | } |
| 142 | clock_gettime(CLOCK_MONOTONIC, &end); |
Andrew Duggan | 65e5553 | 2014-04-04 16:59:54 -0700 | [diff] [blame] | 143 | duration_us = diff_time(&start, &end); |
| 144 | fprintf(stdout, "Done writing lockdown, time: %lld us.\n", duration_us); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | rc = EnterFlashProgramming(); |
| 148 | if (rc != UPDATE_SUCCESS) { |
| 149 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 150 | return rc; |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | rc = WriteBootloaderID(); |
| 156 | if (rc != UPDATE_SUCCESS) { |
| 157 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 158 | return rc; |
| 159 | } |
| 160 | |
| 161 | fprintf(stdout, "Erasing FW...\n"); |
| 162 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 163 | rc = m_device.Write(m_f34StatusAddr, &eraseAll, 1); |
| 164 | if (rc < 0) { |
| 165 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(UPDATE_FAIL_ERASE_ALL)); |
| 166 | return UPDATE_FAIL_ERASE_ALL; |
| 167 | } |
| 168 | |
| 169 | rc = WaitForIdle(RMI_F34_ERASE_WAIT_MS); |
| 170 | if (rc != UPDATE_SUCCESS) { |
| 171 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 172 | return rc; |
| 173 | } |
| 174 | clock_gettime(CLOCK_MONOTONIC, &end); |
Andrew Duggan | 65e5553 | 2014-04-04 16:59:54 -0700 | [diff] [blame] | 175 | duration_us = diff_time(&start, &end); |
| 176 | fprintf(stdout, "Erase complete, time: %lld us.\n", duration_us); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 177 | |
| 178 | if (m_firmwareImage.GetFirmwareData()) { |
| 179 | fprintf(stdout, "Writing firmware...\n"); |
| 180 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 181 | rc = WriteBlocks(m_firmwareImage.GetFirmwareData(), m_fwBlockCount, |
| 182 | RMI_F34_WRITE_FW_BLOCK); |
| 183 | if (rc != UPDATE_SUCCESS) { |
| 184 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 185 | return rc; |
| 186 | } |
| 187 | clock_gettime(CLOCK_MONOTONIC, &end); |
Andrew Duggan | 65e5553 | 2014-04-04 16:59:54 -0700 | [diff] [blame] | 188 | duration_us = diff_time(&start, &end); |
| 189 | fprintf(stdout, "Done writing FW, time: %lld us.\n", duration_us); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | if (m_firmwareImage.GetConfigData()) { |
| 193 | fprintf(stdout, "Writing configuration...\n"); |
| 194 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 195 | rc = WriteBlocks(m_firmwareImage.GetConfigData(), m_configBlockCount, |
| 196 | RMI_F34_WRITE_CONFIG_BLOCK); |
| 197 | if (rc != UPDATE_SUCCESS) { |
| 198 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 199 | return rc; |
| 200 | } |
| 201 | clock_gettime(CLOCK_MONOTONIC, &end); |
Andrew Duggan | 65e5553 | 2014-04-04 16:59:54 -0700 | [diff] [blame] | 202 | duration_us = diff_time(&start, &end); |
| 203 | fprintf(stdout, "Done writing config, time: %lld us.\n", duration_us); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 204 | } |
| 205 | m_device.Reset(); |
| 206 | |
| 207 | return UPDATE_SUCCESS; |
| 208 | |
| 209 | } |
| 210 | |
Andrew Duggan | dc83855 | 2014-11-11 07:58:09 -0800 | [diff] [blame^] | 211 | int RMI4Update::DisableNonessentialInterupts() |
| 212 | { |
| 213 | int rc; |
| 214 | unsigned char interruptEnabeMask = m_f34.GetInterruptMask() | m_f01.GetInterruptMask(); |
| 215 | |
| 216 | rc = m_device.Write(m_f01.GetControlBase() + 1, &interruptEnabeMask, 1); |
| 217 | if (rc < 0) |
| 218 | return rc; |
| 219 | |
| 220 | return UPDATE_SUCCESS; |
| 221 | } |
| 222 | |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 223 | int RMI4Update::FindUpdateFunctions() |
| 224 | { |
| 225 | if (0 > m_device.ScanPDT()) |
| 226 | return UPDATE_FAIL_SCAN_PDT; |
| 227 | |
| 228 | if (!m_device.GetFunction(m_f01, 0x01)) |
| 229 | return UPDATE_FAIL_NO_FUNCTION_01; |
| 230 | |
| 231 | if (!m_device.GetFunction(m_f34, 0x34)) |
| 232 | return UPDATE_FAIL_NO_FUNCTION_34; |
| 233 | |
| 234 | return UPDATE_SUCCESS; |
| 235 | } |
| 236 | |
| 237 | int RMI4Update::ReadF34Queries() |
| 238 | { |
| 239 | int rc; |
| 240 | unsigned char idStr[3]; |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 241 | unsigned char buf[8]; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 242 | unsigned short queryAddr = m_f34.GetQueryBase(); |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 243 | unsigned short f34Version = m_f34.GetFunctionVersion(); |
| 244 | unsigned short querySize; |
| 245 | |
| 246 | if (f34Version == 0x1) |
| 247 | querySize = 8; |
| 248 | else |
| 249 | querySize = 2; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 250 | |
| 251 | rc = m_device.Read(queryAddr, m_bootloaderID, RMI_BOOTLOADER_ID_SIZE); |
| 252 | if (rc < 0) |
| 253 | return UPDATE_FAIL_READ_BOOTLOADER_ID; |
| 254 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 255 | if (f34Version == 0x1) |
| 256 | ++queryAddr; |
| 257 | else |
| 258 | queryAddr += querySize; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 259 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 260 | if (f34Version == 0x1) { |
| 261 | rc = m_device.Read(queryAddr, buf, 1); |
| 262 | if (rc < 0) |
| 263 | return UPDATE_FAIL_READ_F34_QUERIES; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 264 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 265 | m_hasNewRegmap = buf[0] & RMI_F34_HAS_NEW_REG_MAP; |
| 266 | m_unlocked = buf[0] & RMI_F34_IS_UNLOCKED;; |
| 267 | m_hasConfigID = buf[0] & RMI_F34_HAS_CONFIG_ID; |
| 268 | |
| 269 | ++queryAddr; |
| 270 | |
| 271 | rc = m_device.Read(queryAddr, buf, 2); |
| 272 | if (rc < 0) |
| 273 | return UPDATE_FAIL_READ_F34_QUERIES; |
| 274 | |
| 275 | m_blockSize = extract_short(buf + RMI_F34_BLOCK_SIZE_V1_OFFSET); |
| 276 | |
| 277 | ++queryAddr; |
| 278 | |
| 279 | rc = m_device.Read(queryAddr, buf, 8); |
| 280 | if (rc < 0) |
| 281 | return UPDATE_FAIL_READ_F34_QUERIES; |
| 282 | |
| 283 | m_fwBlockCount = extract_short(buf + RMI_F34_FW_BLOCKS_V1_OFFSET); |
| 284 | m_configBlockCount = extract_short(buf + RMI_F34_CONFIG_BLOCKS_V1_OFFSET); |
| 285 | } else { |
| 286 | rc = m_device.Read(queryAddr, buf, RMI_F34_QUERY_SIZE); |
| 287 | if (rc < 0) |
| 288 | return UPDATE_FAIL_READ_F34_QUERIES; |
| 289 | |
| 290 | m_hasNewRegmap = buf[0] & RMI_F34_HAS_NEW_REG_MAP; |
| 291 | m_unlocked = buf[0] & RMI_F34_IS_UNLOCKED;; |
| 292 | m_hasConfigID = buf[0] & RMI_F34_HAS_CONFIG_ID; |
| 293 | m_blockSize = extract_short(buf + RMI_F34_BLOCK_SIZE_OFFSET); |
| 294 | m_fwBlockCount = extract_short(buf + RMI_F34_FW_BLOCKS_OFFSET); |
| 295 | m_configBlockCount = extract_short(buf + RMI_F34_CONFIG_BLOCKS_OFFSET); |
| 296 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 297 | |
| 298 | idStr[0] = m_bootloaderID[0]; |
| 299 | idStr[1] = m_bootloaderID[1]; |
| 300 | idStr[2] = 0; |
| 301 | |
| 302 | fprintf(stdout, "F34 bootloader id: %s (%#04x %#04x)\n", idStr, m_bootloaderID[0], |
| 303 | m_bootloaderID[1]); |
| 304 | fprintf(stdout, "F34 has config id: %d\n", m_hasConfigID); |
| 305 | fprintf(stdout, "F34 unlocked: %d\n", m_unlocked); |
| 306 | fprintf(stdout, "F34 new reg map: %d\n", m_hasNewRegmap); |
| 307 | fprintf(stdout, "F34 block size: %d\n", m_blockSize); |
| 308 | fprintf(stdout, "F34 fw blocks: %d\n", m_fwBlockCount); |
| 309 | fprintf(stdout, "F34 config blocks: %d\n", m_configBlockCount); |
| 310 | fprintf(stdout, "\n"); |
| 311 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 312 | if (f34Version == 0x1) |
| 313 | m_f34StatusAddr = m_f34.GetDataBase() + 2; |
| 314 | else |
| 315 | m_f34StatusAddr = m_f34.GetDataBase() + RMI_F34_BLOCK_DATA_OFFSET + m_blockSize; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 316 | |
| 317 | return UPDATE_SUCCESS; |
| 318 | } |
| 319 | |
| 320 | int RMI4Update::ReadF34Controls() |
| 321 | { |
| 322 | int rc; |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 323 | unsigned char buf[2]; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 324 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 325 | if (m_f34.GetFunctionVersion() == 0x1) { |
| 326 | rc = m_device.Read(m_f34StatusAddr, buf, 2); |
| 327 | if (rc < 0) |
| 328 | return UPDATE_FAIL_READ_F34_CONTROLS; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 329 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 330 | m_f34Command = buf[0] & RMI_F34_COMMAND_V1_MASK; |
| 331 | m_f34Status = buf[1] & RMI_F34_STATUS_V1_MASK; |
| 332 | m_programEnabled = !!(buf[1] & RMI_F34_ENABLED_MASK); |
| 333 | |
| 334 | } else { |
| 335 | rc = m_device.Read(m_f34StatusAddr, buf, 1); |
| 336 | if (rc < 0) |
| 337 | return UPDATE_FAIL_READ_F34_CONTROLS; |
| 338 | |
| 339 | m_f34Command = buf[0] & RMI_F34_COMMAND_MASK; |
| 340 | m_f34Status = (buf[0] >> RMI_F34_STATUS_SHIFT) & RMI_F34_STATUS_MASK; |
| 341 | m_programEnabled = !!(buf[0] & RMI_F34_ENABLED_MASK); |
| 342 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 343 | |
| 344 | return UPDATE_SUCCESS; |
| 345 | } |
| 346 | |
| 347 | int RMI4Update::WriteBootloaderID() |
| 348 | { |
| 349 | int rc; |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 350 | int blockDataOffset = RMI_F34_BLOCK_DATA_OFFSET; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 351 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 352 | if (m_f34.GetFunctionVersion() == 0x1) |
| 353 | blockDataOffset = RMI_F34_BLOCK_DATA_V1_OFFSET; |
| 354 | |
| 355 | rc = m_device.Write(m_f34.GetDataBase() + blockDataOffset, |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 356 | m_bootloaderID, RMI_BOOTLOADER_ID_SIZE); |
| 357 | if (rc < 0) |
| 358 | return UPDATE_FAIL_WRITE_BOOTLOADER_ID; |
| 359 | |
| 360 | return UPDATE_SUCCESS; |
| 361 | } |
| 362 | |
| 363 | int RMI4Update::EnterFlashProgramming() |
| 364 | { |
| 365 | int rc; |
| 366 | unsigned char f01Control_0; |
| 367 | const unsigned char enableProg = RMI_F34_ENABLE_FLASH_PROG; |
| 368 | |
| 369 | rc = WriteBootloaderID(); |
| 370 | if (rc != UPDATE_SUCCESS) |
| 371 | return rc; |
| 372 | |
| 373 | fprintf(stdout, "Enabling flash programming.\n"); |
| 374 | rc = m_device.Write(m_f34StatusAddr, &enableProg, 1); |
| 375 | if (rc < 0) |
| 376 | return UPDATE_FAIL_ENABLE_FLASH_PROGRAMMING; |
| 377 | |
| 378 | rc = WaitForIdle(RMI_F34_ENABLE_WAIT_MS); |
| 379 | if (rc != UPDATE_SUCCESS) |
| 380 | return UPDATE_FAIL_NOT_IN_IDLE_STATE; |
| 381 | |
| 382 | if (!m_programEnabled) |
| 383 | return UPDATE_FAIL_PROGRAMMING_NOT_ENABLED; |
| 384 | |
Andrew Duggan | 6213791 | 2014-05-06 16:06:19 -0700 | [diff] [blame] | 385 | fprintf(stdout, "Programming is enabled.\n"); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 386 | rc = FindUpdateFunctions(); |
| 387 | if (rc != UPDATE_SUCCESS) |
| 388 | return rc; |
| 389 | |
| 390 | rc = m_device.Read(m_f01.GetDataBase(), &m_deviceStatus, 1); |
| 391 | if (rc < 0) |
| 392 | return UPDATE_FAIL_READ_DEVICE_STATUS; |
| 393 | |
| 394 | if (!RMI_F01_STATUS_BOOTLOADER(m_deviceStatus)) |
| 395 | return UPDATE_FAIL_DEVICE_NOT_IN_BOOTLOADER; |
| 396 | |
| 397 | rc = ReadF34Queries(); |
| 398 | if (rc != UPDATE_SUCCESS) |
| 399 | return rc; |
| 400 | |
| 401 | rc = m_device.Read(m_f01.GetControlBase(), &f01Control_0, 1); |
| 402 | if (rc < 0) |
| 403 | return UPDATE_FAIL_READ_F01_CONTROL_0; |
| 404 | |
| 405 | f01Control_0 |= RMI_F01_CRTL0_NOSLEEP_BIT; |
| 406 | f01Control_0 = (f01Control_0 & ~RMI_F01_CTRL0_SLEEP_MODE_MASK) | RMI_SLEEP_MODE_NORMAL; |
| 407 | |
| 408 | rc = m_device.Write(m_f01.GetControlBase(), &f01Control_0, 1); |
| 409 | if (rc < 0) |
| 410 | return UPDATE_FAIL_WRITE_F01_CONTROL_0; |
| 411 | |
| 412 | return UPDATE_SUCCESS; |
| 413 | } |
| 414 | |
| 415 | int RMI4Update::WriteBlocks(unsigned char *block, unsigned short count, unsigned char cmd) |
| 416 | { |
| 417 | int blockNum; |
| 418 | unsigned char zeros[] = { 0, 0 }; |
| 419 | int rc; |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 420 | unsigned short addr; |
| 421 | |
| 422 | if (m_f34.GetFunctionVersion() == 0x1) |
| 423 | addr = m_f34.GetDataBase() + RMI_F34_BLOCK_DATA_V1_OFFSET; |
| 424 | else |
| 425 | addr = m_f34.GetDataBase() + RMI_F34_BLOCK_DATA_OFFSET; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 426 | |
| 427 | rc = m_device.Write(m_f34.GetDataBase(), zeros, 2); |
| 428 | if (rc < 0) |
| 429 | return UPDATE_FAIL_WRITE_INITIAL_ZEROS; |
| 430 | |
| 431 | for (blockNum = 0; blockNum < count; ++blockNum) { |
| 432 | rc = m_device.Write(addr, block, m_blockSize); |
| 433 | if (rc < 0) { |
| 434 | fprintf(stderr, "failed to write block %d\n", blockNum); |
| 435 | return UPDATE_FAIL_WRITE_BLOCK; |
| 436 | } |
| 437 | |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 438 | rc = m_device.Write(m_f34StatusAddr, &cmd, 1); |
| 439 | if (rc < 0) { |
| 440 | fprintf(stderr, "failed to write command for block %d\n", blockNum); |
| 441 | return UPDATE_FAIL_WRITE_FLASH_COMMAND; |
| 442 | } |
| 443 | |
| 444 | rc = WaitForIdle(RMI_F34_IDLE_WAIT_MS); |
| 445 | if (rc != UPDATE_SUCCESS) { |
| 446 | fprintf(stderr, "failed to go into idle after writing block %d\n", blockNum); |
| 447 | return UPDATE_FAIL_NOT_IN_IDLE_STATE; |
| 448 | } |
| 449 | |
| 450 | block += m_blockSize; |
| 451 | } |
| 452 | |
| 453 | return UPDATE_SUCCESS; |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * This is a limited implementation of WaitForIdle which assumes WaitForAttention is supported |
| 458 | * this will be true for HID, but other protocols will need to revert polling. Polling |
| 459 | * is not implemented yet. |
| 460 | */ |
| 461 | int RMI4Update::WaitForIdle(int timeout_ms) |
| 462 | { |
| 463 | int rc; |
| 464 | struct timeval tv; |
| 465 | |
| 466 | tv.tv_sec = timeout_ms / 1000; |
| 467 | tv.tv_usec = (timeout_ms % 1000) * 1000; |
| 468 | |
Andrew Duggan | dca0efc | 2014-11-10 09:28:12 -0800 | [diff] [blame] | 469 | rc = m_device.WaitForAttention(&tv, m_f34.GetInterruptMask()); |
Andrew Duggan | 6213791 | 2014-05-06 16:06:19 -0700 | [diff] [blame] | 470 | if (rc == -ETIMEDOUT) |
| 471 | /* |
| 472 | * If for some reason we are not getting attention reports for HID devices |
| 473 | * then we can still continue after the timeout and read F34 status |
| 474 | * but if we have to wait for the timeout to ellapse everytime then this |
| 475 | * will be slow. If this message shows up a lot then something is wrong |
| 476 | * with receiving attention reports and that should be fixed. |
| 477 | */ |
| 478 | fprintf(stderr, "Timed out waiting for attn report\n"); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 479 | |
Andrew Duggan | 6213791 | 2014-05-06 16:06:19 -0700 | [diff] [blame] | 480 | rc = ReadF34Controls(); |
| 481 | if (rc != UPDATE_SUCCESS) |
| 482 | return rc; |
| 483 | |
| 484 | if (!m_f34Status && !m_f34Command) { |
| 485 | if (!m_programEnabled) { |
| 486 | fprintf(stderr, "Bootloader is idle but program_enabled bit isn't set.\n"); |
| 487 | return UPDATE_FAIL_PROGRAMMING_NOT_ENABLED; |
| 488 | } else { |
| 489 | return UPDATE_SUCCESS; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 490 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 491 | } |
Andrew Duggan | 6213791 | 2014-05-06 16:06:19 -0700 | [diff] [blame] | 492 | |
| 493 | fprintf(stderr, "ERROR: Waiting for idle status.\n"); |
| 494 | fprintf(stderr, "Command: %#04x\n", m_f34Command); |
| 495 | fprintf(stderr, "Status: %#04x\n", m_f34Status); |
| 496 | fprintf(stderr, "Enabled: %d\n", m_programEnabled); |
| 497 | fprintf(stderr, "Idle: %d\n", !m_f34Command && !m_f34Status); |
| 498 | |
| 499 | return UPDATE_FAIL_NOT_IN_IDLE_STATE; |
Andrew Duggan | 050db0e | 2014-07-29 12:42:49 -0700 | [diff] [blame] | 500 | } |