| 553 | } |
| 554 | |
| 555 | void doRemoteCommand(txMachine *the, uint8_t *cmd, uint32_t cmdLen) |
| 556 | { |
| 557 | uint16_t resultID = 0; |
| 558 | int16_t resultCode = 0; |
| 559 | uint8_t cmdID; |
| 560 | int baud = 0; |
| 561 | |
| 562 | if (!cmdLen) |
| 563 | return; |
| 564 | |
| 565 | cmdID = *cmd++; |
| 566 | cmdLen -= 1; |
| 567 | //printf("do Remote command %d\n", cmdID); |
| 568 | |
| 569 | if (cmdLen >= 2) { |
| 570 | resultID = (cmd[0] << 8) | cmd[1]; |
| 571 | cmd += 2, cmdLen -= 2; |
| 572 | |
| 573 | the->echoBuffer[0] = 5; |
| 574 | the->echoBuffer[1] = resultID >> 8; |
| 575 | the->echoBuffer[2] = resultID & 0xff; |
| 576 | the->echoBuffer[3] = 0; |
| 577 | the->echoBuffer[4] = 0; |
| 578 | the->echoOffset = 5; |
| 579 | } |
| 580 | |
| 581 | switch (cmdID) { |
| 582 | case 1: // restart |
| 583 | break; |
| 584 | |
| 585 | #if MODDEF_XS_MODS |
| 586 | case 2: { // uninstall |
| 587 | uint8_t erase[16] = {0}; |
| 588 | uint32_t offset = (uintptr_t)kModulesStart - (uintptr_t)kFlashStart; |
| 589 | if (!modSPIWrite(offset, sizeof(erase), erase)) |
| 590 | resultCode = -1; |
| 591 | } break; |
| 592 | |
| 593 | case 3: { // install some |
| 594 | uint32_t offset = c_read32be(cmd); |
| 595 | cmd += 4, cmdLen -= 4; |
| 596 | if ((offset + cmdLen) > kModulesByteLength) { |
| 597 | resultCode = -2; |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | offset += (uintptr_t)kModulesStart - (uintptr_t)kFlashStart; |
| 602 | |
| 603 | int firstSector = offset / kFlashSectorSize, lastSector = (offset + cmdLen) / kFlashSectorSize; |
| 604 | if (!(offset % kFlashSectorSize)) // starts on sector boundary |
| 605 | modSPIErase(offset, kFlashSectorSize * ((lastSector - firstSector) + 1)); |
| 606 | else if (firstSector != lastSector) |
| 607 | modSPIErase((firstSector + 1) * kFlashSectorSize, kFlashSectorSize * (lastSector - firstSector)); // crosses into a new sector |
| 608 | |
| 609 | if (!modSPIWrite(offset, cmdLen, cmd)) |
| 610 | resultCode = -1; |
| 611 | } |
| 612 | break; |
no test coverage detected