flash.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #include <stdio.h>
  5. #include <aos/kernel.h>
  6. #include <yoc/netio.h>
  7. #define TAG "fota"
  8. #ifndef __linux__
  9. #include <aos/aos.h>
  10. #include <yoc/partition.h>
  11. static int flash_open(netio_t *io, const char *path)
  12. {
  13. partition_t handle = partition_open(path + sizeof("flash://") - 1);
  14. if (handle >= 0) {
  15. partition_info_t *lp = hal_flash_get_info(handle);
  16. aos_assert(lp);
  17. io->size = lp->length;
  18. io->block_size = lp->sector_size;
  19. io->private = (void *)handle;
  20. return 0;
  21. }
  22. return -1;
  23. }
  24. static int flash_close(netio_t *io)
  25. {
  26. partition_t handle = (partition_t)io->private;
  27. partition_close(handle);
  28. return 0;
  29. }
  30. static int flash_read(netio_t *io, uint8_t *buffer, int length, int timeoutms)
  31. {
  32. partition_t handle = (partition_t)io->private;
  33. if (io->size - io->offset < length)
  34. length = io->size - io->offset;
  35. if (partition_read(handle, io->offset, buffer, length) >= 0) {
  36. io->offset += length;
  37. return length;
  38. }
  39. return -1;
  40. }
  41. static int fota_flash_erase(partition_t partition, off_t off_set, int block_size, uint32_t block_count)
  42. {
  43. if (off_set % block_size == 0) {
  44. if (partition_erase(partition, off_set, block_count) < 0) {
  45. LOGD(TAG, "0 erase addr:%x length:%x\n", off_set, block_count);
  46. return -1;
  47. }
  48. }
  49. return 0;
  50. }
  51. static int flash_write(netio_t *io, uint8_t *buffer, int length, int timeoutms)
  52. {
  53. partition_t handle = (partition_t)io->private;
  54. // LOGD(TAG, "%d %d %d\n", io->size, io->offset, length);
  55. if (io->size - io->offset < length)
  56. length = io->size - io->offset;
  57. // LOGD(TAG, "length %d\n", length);
  58. if (fota_flash_erase(handle, io->offset + (io->block_size << 1), io->block_size, (length + io->block_size - 1) / io->block_size) < 0) {
  59. LOGE(TAG, "erase addr:%x length:%x\n", io->offset + (io->block_size << 1), (length + io->block_size - 1) / io->block_size);
  60. return -1;
  61. }
  62. if (partition_write(handle, io->offset + (io->block_size << 1), buffer, length) >= 0) {
  63. // LOGD(TAG, "write addr:%x length:%x\n", io->offset + (io->block_size << 1), length);
  64. io->offset += length;
  65. return length;
  66. }
  67. LOGD(TAG, "write fail addr:0x%x length:0x%x\n", io->offset + (io->block_size << 1), length);
  68. return -1;
  69. }
  70. static int flash_seek(netio_t *io, size_t offset, int whence)
  71. {
  72. // partition_t handle = (partition_t)io->private;
  73. switch (whence) {
  74. case SEEK_SET:
  75. io->offset = offset;
  76. return 0;
  77. case SEEK_CUR:
  78. io->offset += offset;
  79. return 0;
  80. case SEEK_END:
  81. io->offset = io->size - offset;
  82. return 0;
  83. }
  84. return -1;
  85. }
  86. #else
  87. typedef int partition_t;
  88. static int flash_open(netio_t *io, const char *path)
  89. {
  90. return 0;
  91. }
  92. static int flash_close(netio_t *io)
  93. {
  94. return 0;
  95. }
  96. static int flash_read(netio_t *io, uint8_t *buffer, int length, int timeoutms)
  97. {
  98. return 0;
  99. }
  100. static int flash_write(netio_t *io, uint8_t *buffer, int length, int timeoutms)
  101. {
  102. return 0;
  103. }
  104. static int flash_seek(netio_t *io, size_t offset, int whence)
  105. {
  106. // partition_t handle = (partition_t)io->private;
  107. switch (whence) {
  108. case SEEK_SET:
  109. io->offset = offset;
  110. return 0;
  111. case SEEK_CUR:
  112. io->offset += offset;
  113. return 0;
  114. case SEEK_END:
  115. io->offset = io->size - offset;
  116. return 0;
  117. }
  118. return -1;
  119. }
  120. #endif /* __linux__ */
  121. const netio_cls_t flash = {
  122. .name = "flash",
  123. .open = flash_open,
  124. .close = flash_close,
  125. .write = flash_write,
  126. .read = flash_read,
  127. .seek = flash_seek,
  128. };
  129. int netio_register_flash(void)
  130. {
  131. return netio_register(&flash);
  132. }