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(); |
Andrew Duggan | 5597d1e | 2015-03-16 15:43:54 -0700 | [diff] [blame] | 127 | fprintf(stderr, "Firmware image (%ld) is not newer then the firmware on the device (%ld)\n", |
| 128 | m_firmwareImage.GetFirmwareID(), m_device.GetFirmwareID()); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 129 | return UPDATE_FAIL_FIRMWARE_IMAGE_IS_OLDER; |
| 130 | } |
| 131 | } |
| 132 | |
Satoshi Noguchi | a0b675a | 2014-09-29 02:38:14 -0700 | [diff] [blame] | 133 | if (performLockdown && m_unlocked) { |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 134 | if (m_firmwareImage.GetLockdownData()) { |
| 135 | fprintf(stdout, "Writing lockdown...\n"); |
| 136 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 137 | rc = WriteBlocks(m_firmwareImage.GetLockdownData(), |
| 138 | m_firmwareImage.GetLockdownSize() / 0x10, |
| 139 | RMI_F34_WRITE_LOCKDOWN_BLOCK); |
| 140 | if (rc != UPDATE_SUCCESS) { |
| 141 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 142 | return rc; |
| 143 | } |
| 144 | clock_gettime(CLOCK_MONOTONIC, &end); |
Andrew Duggan | 65e5553 | 2014-04-04 16:59:54 -0700 | [diff] [blame] | 145 | duration_us = diff_time(&start, &end); |
| 146 | fprintf(stdout, "Done writing lockdown, time: %lld us.\n", duration_us); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | rc = EnterFlashProgramming(); |
| 150 | if (rc != UPDATE_SUCCESS) { |
| 151 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 152 | return rc; |
| 153 | } |
| 154 | |
| 155 | } |
| 156 | |
| 157 | rc = WriteBootloaderID(); |
| 158 | if (rc != UPDATE_SUCCESS) { |
| 159 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 160 | return rc; |
| 161 | } |
| 162 | |
| 163 | fprintf(stdout, "Erasing FW...\n"); |
| 164 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 165 | rc = m_device.Write(m_f34StatusAddr, &eraseAll, 1); |
| 166 | if (rc < 0) { |
| 167 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(UPDATE_FAIL_ERASE_ALL)); |
| 168 | return UPDATE_FAIL_ERASE_ALL; |
| 169 | } |
| 170 | |
| 171 | rc = WaitForIdle(RMI_F34_ERASE_WAIT_MS); |
| 172 | if (rc != UPDATE_SUCCESS) { |
| 173 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 174 | return rc; |
| 175 | } |
| 176 | clock_gettime(CLOCK_MONOTONIC, &end); |
Andrew Duggan | 65e5553 | 2014-04-04 16:59:54 -0700 | [diff] [blame] | 177 | duration_us = diff_time(&start, &end); |
| 178 | fprintf(stdout, "Erase complete, time: %lld us.\n", duration_us); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 179 | |
| 180 | if (m_firmwareImage.GetFirmwareData()) { |
| 181 | fprintf(stdout, "Writing firmware...\n"); |
| 182 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 183 | rc = WriteBlocks(m_firmwareImage.GetFirmwareData(), m_fwBlockCount, |
| 184 | RMI_F34_WRITE_FW_BLOCK); |
| 185 | if (rc != UPDATE_SUCCESS) { |
| 186 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 187 | return rc; |
| 188 | } |
| 189 | clock_gettime(CLOCK_MONOTONIC, &end); |
Andrew Duggan | 65e5553 | 2014-04-04 16:59:54 -0700 | [diff] [blame] | 190 | duration_us = diff_time(&start, &end); |
| 191 | fprintf(stdout, "Done writing FW, time: %lld us.\n", duration_us); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | if (m_firmwareImage.GetConfigData()) { |
| 195 | fprintf(stdout, "Writing configuration...\n"); |
| 196 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 197 | rc = WriteBlocks(m_firmwareImage.GetConfigData(), m_configBlockCount, |
| 198 | RMI_F34_WRITE_CONFIG_BLOCK); |
| 199 | if (rc != UPDATE_SUCCESS) { |
| 200 | fprintf(stderr, "%s: %s\n", __func__, update_err_to_string(rc)); |
| 201 | return rc; |
| 202 | } |
| 203 | clock_gettime(CLOCK_MONOTONIC, &end); |
Andrew Duggan | 65e5553 | 2014-04-04 16:59:54 -0700 | [diff] [blame] | 204 | duration_us = diff_time(&start, &end); |
| 205 | fprintf(stdout, "Done writing config, time: %lld us.\n", duration_us); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 206 | } |
| 207 | m_device.Reset(); |
Andrew Duggan | f2e021f | 2015-05-07 10:35:45 -0700 | [diff] [blame^] | 208 | m_device.RebindDriver(); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 209 | |
| 210 | return UPDATE_SUCCESS; |
| 211 | |
| 212 | } |
| 213 | |
Andrew Duggan | dc83855 | 2014-11-11 07:58:09 -0800 | [diff] [blame] | 214 | int RMI4Update::DisableNonessentialInterupts() |
| 215 | { |
| 216 | int rc; |
| 217 | unsigned char interruptEnabeMask = m_f34.GetInterruptMask() | m_f01.GetInterruptMask(); |
| 218 | |
| 219 | rc = m_device.Write(m_f01.GetControlBase() + 1, &interruptEnabeMask, 1); |
| 220 | if (rc < 0) |
| 221 | return rc; |
| 222 | |
| 223 | return UPDATE_SUCCESS; |
| 224 | } |
| 225 | |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 226 | int RMI4Update::FindUpdateFunctions() |
| 227 | { |
| 228 | if (0 > m_device.ScanPDT()) |
| 229 | return UPDATE_FAIL_SCAN_PDT; |
| 230 | |
| 231 | if (!m_device.GetFunction(m_f01, 0x01)) |
| 232 | return UPDATE_FAIL_NO_FUNCTION_01; |
| 233 | |
| 234 | if (!m_device.GetFunction(m_f34, 0x34)) |
| 235 | return UPDATE_FAIL_NO_FUNCTION_34; |
| 236 | |
| 237 | return UPDATE_SUCCESS; |
| 238 | } |
| 239 | |
| 240 | int RMI4Update::ReadF34Queries() |
| 241 | { |
| 242 | int rc; |
| 243 | unsigned char idStr[3]; |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 244 | unsigned char buf[8]; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 245 | unsigned short queryAddr = m_f34.GetQueryBase(); |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 246 | unsigned short f34Version = m_f34.GetFunctionVersion(); |
| 247 | unsigned short querySize; |
| 248 | |
| 249 | if (f34Version == 0x1) |
| 250 | querySize = 8; |
| 251 | else |
| 252 | querySize = 2; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 253 | |
| 254 | rc = m_device.Read(queryAddr, m_bootloaderID, RMI_BOOTLOADER_ID_SIZE); |
| 255 | if (rc < 0) |
| 256 | return UPDATE_FAIL_READ_BOOTLOADER_ID; |
| 257 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 258 | if (f34Version == 0x1) |
| 259 | ++queryAddr; |
| 260 | else |
| 261 | queryAddr += querySize; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 262 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 263 | if (f34Version == 0x1) { |
| 264 | rc = m_device.Read(queryAddr, buf, 1); |
| 265 | if (rc < 0) |
| 266 | return UPDATE_FAIL_READ_F34_QUERIES; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 267 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 268 | m_hasNewRegmap = buf[0] & RMI_F34_HAS_NEW_REG_MAP; |
| 269 | m_unlocked = buf[0] & RMI_F34_IS_UNLOCKED;; |
| 270 | m_hasConfigID = buf[0] & RMI_F34_HAS_CONFIG_ID; |
| 271 | |
| 272 | ++queryAddr; |
| 273 | |
| 274 | rc = m_device.Read(queryAddr, buf, 2); |
| 275 | if (rc < 0) |
| 276 | return UPDATE_FAIL_READ_F34_QUERIES; |
| 277 | |
| 278 | m_blockSize = extract_short(buf + RMI_F34_BLOCK_SIZE_V1_OFFSET); |
| 279 | |
| 280 | ++queryAddr; |
| 281 | |
| 282 | rc = m_device.Read(queryAddr, buf, 8); |
| 283 | if (rc < 0) |
| 284 | return UPDATE_FAIL_READ_F34_QUERIES; |
| 285 | |
| 286 | m_fwBlockCount = extract_short(buf + RMI_F34_FW_BLOCKS_V1_OFFSET); |
| 287 | m_configBlockCount = extract_short(buf + RMI_F34_CONFIG_BLOCKS_V1_OFFSET); |
| 288 | } else { |
| 289 | rc = m_device.Read(queryAddr, buf, RMI_F34_QUERY_SIZE); |
| 290 | if (rc < 0) |
| 291 | return UPDATE_FAIL_READ_F34_QUERIES; |
| 292 | |
| 293 | m_hasNewRegmap = buf[0] & RMI_F34_HAS_NEW_REG_MAP; |
| 294 | m_unlocked = buf[0] & RMI_F34_IS_UNLOCKED;; |
| 295 | m_hasConfigID = buf[0] & RMI_F34_HAS_CONFIG_ID; |
| 296 | m_blockSize = extract_short(buf + RMI_F34_BLOCK_SIZE_OFFSET); |
| 297 | m_fwBlockCount = extract_short(buf + RMI_F34_FW_BLOCKS_OFFSET); |
| 298 | m_configBlockCount = extract_short(buf + RMI_F34_CONFIG_BLOCKS_OFFSET); |
| 299 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 300 | |
| 301 | idStr[0] = m_bootloaderID[0]; |
| 302 | idStr[1] = m_bootloaderID[1]; |
| 303 | idStr[2] = 0; |
| 304 | |
| 305 | fprintf(stdout, "F34 bootloader id: %s (%#04x %#04x)\n", idStr, m_bootloaderID[0], |
| 306 | m_bootloaderID[1]); |
| 307 | fprintf(stdout, "F34 has config id: %d\n", m_hasConfigID); |
| 308 | fprintf(stdout, "F34 unlocked: %d\n", m_unlocked); |
| 309 | fprintf(stdout, "F34 new reg map: %d\n", m_hasNewRegmap); |
| 310 | fprintf(stdout, "F34 block size: %d\n", m_blockSize); |
| 311 | fprintf(stdout, "F34 fw blocks: %d\n", m_fwBlockCount); |
| 312 | fprintf(stdout, "F34 config blocks: %d\n", m_configBlockCount); |
| 313 | fprintf(stdout, "\n"); |
| 314 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 315 | if (f34Version == 0x1) |
| 316 | m_f34StatusAddr = m_f34.GetDataBase() + 2; |
| 317 | else |
| 318 | m_f34StatusAddr = m_f34.GetDataBase() + RMI_F34_BLOCK_DATA_OFFSET + m_blockSize; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 319 | |
| 320 | return UPDATE_SUCCESS; |
| 321 | } |
| 322 | |
| 323 | int RMI4Update::ReadF34Controls() |
| 324 | { |
| 325 | int rc; |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 326 | unsigned char buf[2]; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 327 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 328 | if (m_f34.GetFunctionVersion() == 0x1) { |
| 329 | rc = m_device.Read(m_f34StatusAddr, buf, 2); |
| 330 | if (rc < 0) |
| 331 | return UPDATE_FAIL_READ_F34_CONTROLS; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 332 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 333 | m_f34Command = buf[0] & RMI_F34_COMMAND_V1_MASK; |
| 334 | m_f34Status = buf[1] & RMI_F34_STATUS_V1_MASK; |
| 335 | m_programEnabled = !!(buf[1] & RMI_F34_ENABLED_MASK); |
| 336 | |
| 337 | } else { |
| 338 | rc = m_device.Read(m_f34StatusAddr, buf, 1); |
| 339 | if (rc < 0) |
| 340 | return UPDATE_FAIL_READ_F34_CONTROLS; |
| 341 | |
| 342 | m_f34Command = buf[0] & RMI_F34_COMMAND_MASK; |
| 343 | m_f34Status = (buf[0] >> RMI_F34_STATUS_SHIFT) & RMI_F34_STATUS_MASK; |
| 344 | m_programEnabled = !!(buf[0] & RMI_F34_ENABLED_MASK); |
| 345 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 346 | |
| 347 | return UPDATE_SUCCESS; |
| 348 | } |
| 349 | |
| 350 | int RMI4Update::WriteBootloaderID() |
| 351 | { |
| 352 | int rc; |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 353 | int blockDataOffset = RMI_F34_BLOCK_DATA_OFFSET; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 354 | |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 355 | if (m_f34.GetFunctionVersion() == 0x1) |
| 356 | blockDataOffset = RMI_F34_BLOCK_DATA_V1_OFFSET; |
| 357 | |
| 358 | rc = m_device.Write(m_f34.GetDataBase() + blockDataOffset, |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 359 | m_bootloaderID, RMI_BOOTLOADER_ID_SIZE); |
| 360 | if (rc < 0) |
| 361 | return UPDATE_FAIL_WRITE_BOOTLOADER_ID; |
| 362 | |
| 363 | return UPDATE_SUCCESS; |
| 364 | } |
| 365 | |
| 366 | int RMI4Update::EnterFlashProgramming() |
| 367 | { |
| 368 | int rc; |
| 369 | unsigned char f01Control_0; |
| 370 | const unsigned char enableProg = RMI_F34_ENABLE_FLASH_PROG; |
| 371 | |
| 372 | rc = WriteBootloaderID(); |
| 373 | if (rc != UPDATE_SUCCESS) |
| 374 | return rc; |
| 375 | |
| 376 | fprintf(stdout, "Enabling flash programming.\n"); |
| 377 | rc = m_device.Write(m_f34StatusAddr, &enableProg, 1); |
| 378 | if (rc < 0) |
| 379 | return UPDATE_FAIL_ENABLE_FLASH_PROGRAMMING; |
| 380 | |
Andrew Duggan | f2e021f | 2015-05-07 10:35:45 -0700 | [diff] [blame^] | 381 | Sleep(RMI_F34_ENABLE_WAIT_MS); |
| 382 | m_device.RebindDriver(); |
| 383 | rc = WaitForIdle(0); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 384 | if (rc != UPDATE_SUCCESS) |
| 385 | return UPDATE_FAIL_NOT_IN_IDLE_STATE; |
| 386 | |
| 387 | if (!m_programEnabled) |
| 388 | return UPDATE_FAIL_PROGRAMMING_NOT_ENABLED; |
| 389 | |
Andrew Duggan | 6213791 | 2014-05-06 16:06:19 -0700 | [diff] [blame] | 390 | fprintf(stdout, "Programming is enabled.\n"); |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 391 | rc = FindUpdateFunctions(); |
| 392 | if (rc != UPDATE_SUCCESS) |
| 393 | return rc; |
| 394 | |
| 395 | rc = m_device.Read(m_f01.GetDataBase(), &m_deviceStatus, 1); |
| 396 | if (rc < 0) |
| 397 | return UPDATE_FAIL_READ_DEVICE_STATUS; |
| 398 | |
| 399 | if (!RMI_F01_STATUS_BOOTLOADER(m_deviceStatus)) |
| 400 | return UPDATE_FAIL_DEVICE_NOT_IN_BOOTLOADER; |
| 401 | |
| 402 | rc = ReadF34Queries(); |
| 403 | if (rc != UPDATE_SUCCESS) |
| 404 | return rc; |
| 405 | |
| 406 | rc = m_device.Read(m_f01.GetControlBase(), &f01Control_0, 1); |
| 407 | if (rc < 0) |
| 408 | return UPDATE_FAIL_READ_F01_CONTROL_0; |
| 409 | |
| 410 | f01Control_0 |= RMI_F01_CRTL0_NOSLEEP_BIT; |
| 411 | f01Control_0 = (f01Control_0 & ~RMI_F01_CTRL0_SLEEP_MODE_MASK) | RMI_SLEEP_MODE_NORMAL; |
| 412 | |
| 413 | rc = m_device.Write(m_f01.GetControlBase(), &f01Control_0, 1); |
| 414 | if (rc < 0) |
| 415 | return UPDATE_FAIL_WRITE_F01_CONTROL_0; |
| 416 | |
| 417 | return UPDATE_SUCCESS; |
| 418 | } |
| 419 | |
| 420 | int RMI4Update::WriteBlocks(unsigned char *block, unsigned short count, unsigned char cmd) |
| 421 | { |
| 422 | int blockNum; |
| 423 | unsigned char zeros[] = { 0, 0 }; |
| 424 | int rc; |
Andrew Duggan | a90829b | 2014-05-23 12:34:31 -0700 | [diff] [blame] | 425 | unsigned short addr; |
| 426 | |
| 427 | if (m_f34.GetFunctionVersion() == 0x1) |
| 428 | addr = m_f34.GetDataBase() + RMI_F34_BLOCK_DATA_V1_OFFSET; |
| 429 | else |
| 430 | addr = m_f34.GetDataBase() + RMI_F34_BLOCK_DATA_OFFSET; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 431 | |
| 432 | rc = m_device.Write(m_f34.GetDataBase(), zeros, 2); |
| 433 | if (rc < 0) |
| 434 | return UPDATE_FAIL_WRITE_INITIAL_ZEROS; |
| 435 | |
| 436 | for (blockNum = 0; blockNum < count; ++blockNum) { |
| 437 | rc = m_device.Write(addr, block, m_blockSize); |
| 438 | if (rc < 0) { |
| 439 | fprintf(stderr, "failed to write block %d\n", blockNum); |
| 440 | return UPDATE_FAIL_WRITE_BLOCK; |
| 441 | } |
| 442 | |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 443 | rc = m_device.Write(m_f34StatusAddr, &cmd, 1); |
| 444 | if (rc < 0) { |
| 445 | fprintf(stderr, "failed to write command for block %d\n", blockNum); |
| 446 | return UPDATE_FAIL_WRITE_FLASH_COMMAND; |
| 447 | } |
| 448 | |
| 449 | rc = WaitForIdle(RMI_F34_IDLE_WAIT_MS); |
| 450 | if (rc != UPDATE_SUCCESS) { |
| 451 | fprintf(stderr, "failed to go into idle after writing block %d\n", blockNum); |
| 452 | return UPDATE_FAIL_NOT_IN_IDLE_STATE; |
| 453 | } |
| 454 | |
| 455 | block += m_blockSize; |
| 456 | } |
| 457 | |
| 458 | return UPDATE_SUCCESS; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * This is a limited implementation of WaitForIdle which assumes WaitForAttention is supported |
| 463 | * this will be true for HID, but other protocols will need to revert polling. Polling |
| 464 | * is not implemented yet. |
| 465 | */ |
| 466 | int RMI4Update::WaitForIdle(int timeout_ms) |
| 467 | { |
| 468 | int rc; |
| 469 | struct timeval tv; |
| 470 | |
Andrew Duggan | f2e021f | 2015-05-07 10:35:45 -0700 | [diff] [blame^] | 471 | if (timeout_ms > 0) { |
| 472 | tv.tv_sec = timeout_ms / 1000; |
| 473 | tv.tv_usec = (timeout_ms % 1000) * 1000; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 474 | |
Andrew Duggan | f2e021f | 2015-05-07 10:35:45 -0700 | [diff] [blame^] | 475 | rc = m_device.WaitForAttention(&tv, m_f34.GetInterruptMask()); |
| 476 | if (rc == -ETIMEDOUT) |
| 477 | /* |
| 478 | * If for some reason we are not getting attention reports for HID devices |
| 479 | * then we can still continue after the timeout and read F34 status |
| 480 | * but if we have to wait for the timeout to ellapse everytime then this |
| 481 | * will be slow. If this message shows up a lot then something is wrong |
| 482 | * with receiving attention reports and that should be fixed. |
| 483 | */ |
| 484 | fprintf(stderr, "Timed out waiting for attn report\n"); |
| 485 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 486 | |
Andrew Duggan | 6213791 | 2014-05-06 16:06:19 -0700 | [diff] [blame] | 487 | rc = ReadF34Controls(); |
| 488 | if (rc != UPDATE_SUCCESS) |
| 489 | return rc; |
| 490 | |
| 491 | if (!m_f34Status && !m_f34Command) { |
| 492 | if (!m_programEnabled) { |
| 493 | fprintf(stderr, "Bootloader is idle but program_enabled bit isn't set.\n"); |
| 494 | return UPDATE_FAIL_PROGRAMMING_NOT_ENABLED; |
| 495 | } else { |
| 496 | return UPDATE_SUCCESS; |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 497 | } |
Andrew Duggan | 4e81125 | 2014-04-03 15:17:57 -0700 | [diff] [blame] | 498 | } |
Andrew Duggan | 6213791 | 2014-05-06 16:06:19 -0700 | [diff] [blame] | 499 | |
| 500 | fprintf(stderr, "ERROR: Waiting for idle status.\n"); |
| 501 | fprintf(stderr, "Command: %#04x\n", m_f34Command); |
| 502 | fprintf(stderr, "Status: %#04x\n", m_f34Status); |
| 503 | fprintf(stderr, "Enabled: %d\n", m_programEnabled); |
| 504 | fprintf(stderr, "Idle: %d\n", !m_f34Command && !m_f34Status); |
| 505 | |
| 506 | return UPDATE_FAIL_NOT_IN_IDLE_STATE; |
Andrew Duggan | bef9c2d | 2015-05-06 17:45:48 -0700 | [diff] [blame] | 507 | } |