slip.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
  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. */
  15. #include "slip.h"
  16. #include "esp_loader_io.h"
  17. static const uint8_t DELIMITER = 0xC0;
  18. static const uint8_t C0_REPLACEMENT[2] = {0xDB, 0xDC};
  19. static const uint8_t DB_REPLACEMENT[2] = {0xDB, 0xDD};
  20. static inline esp_loader_error_t peripheral_read(uint8_t *buff, const size_t size)
  21. {
  22. return loader_port_read(buff, size, loader_port_remaining_time());
  23. }
  24. static inline esp_loader_error_t peripheral_write(const uint8_t *buff, const size_t size)
  25. {
  26. return loader_port_write(buff, size, loader_port_remaining_time());
  27. }
  28. esp_loader_error_t SLIP_receive_data(uint8_t *buff, const size_t size)
  29. {
  30. uint8_t ch;
  31. for (uint32_t i = 0; i < size; i++) {
  32. RETURN_ON_ERROR( peripheral_read(&ch, 1) );
  33. if (ch == 0xDB) {
  34. RETURN_ON_ERROR( peripheral_read(&ch, 1) );
  35. if (ch == 0xDC) {
  36. buff[i] = 0xC0;
  37. } else if (ch == 0xDD) {
  38. buff[i] = 0xDB;
  39. } else {
  40. return ESP_LOADER_ERROR_INVALID_RESPONSE;
  41. }
  42. } else {
  43. buff[i] = ch;
  44. }
  45. }
  46. return ESP_LOADER_SUCCESS;
  47. }
  48. esp_loader_error_t SLIP_receive_packet(uint8_t *buff, const size_t size)
  49. {
  50. uint8_t ch;
  51. // Wait for delimiter
  52. do {
  53. RETURN_ON_ERROR( peripheral_read(&ch, 1) );
  54. } while (ch != DELIMITER);
  55. // Workaround: bootloader sends two dummy(0xC0) bytes after response when baud rate is changed.
  56. do {
  57. RETURN_ON_ERROR( peripheral_read(&ch, 1) );
  58. } while (ch == DELIMITER);
  59. buff[0] = ch;
  60. RETURN_ON_ERROR( SLIP_receive_data(&buff[1], size - 1) );
  61. // Wait for delimiter
  62. do {
  63. RETURN_ON_ERROR( peripheral_read(&ch, 1) );
  64. } while (ch != DELIMITER);
  65. return ESP_LOADER_SUCCESS;
  66. }
  67. esp_loader_error_t SLIP_send(const uint8_t *data, const size_t size)
  68. {
  69. uint32_t to_write = 0; // Bytes ready to write as they are
  70. uint32_t written = 0; // Bytes already written
  71. for (uint32_t i = 0; i < size; i++) {
  72. if (data[i] != 0xC0 && data[i] != 0xDB) {
  73. to_write++; // Queue this byte for writing
  74. continue;
  75. }
  76. // We have a byte that needs encoding, write the queue first
  77. if (to_write > 0) {
  78. RETURN_ON_ERROR( peripheral_write(&data[written], to_write) );
  79. }
  80. // Write the encoded byte
  81. if (data[i] == 0xC0) {
  82. RETURN_ON_ERROR( peripheral_write(C0_REPLACEMENT, 2) );
  83. } else {
  84. RETURN_ON_ERROR( peripheral_write(DB_REPLACEMENT, 2) );
  85. }
  86. // Update to start again after the encoded byte
  87. written = i + 1;
  88. to_write = 0;
  89. }
  90. // Write the rest of the bytes that didn't need encoding
  91. if (to_write > 0) {
  92. RETURN_ON_ERROR( peripheral_write(&data[written], to_write) );
  93. }
  94. return ESP_LOADER_SUCCESS;
  95. }
  96. esp_loader_error_t SLIP_send_delimiter(void)
  97. {
  98. return peripheral_write(&DELIMITER, 1);
  99. }