You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

118 lines
3.9 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2020 Loci Controls Inc
  4. * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * Neither the name of Texas Instruments Incorporated nor the names of
  19. * its contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************/
  35. #include <stdint.h>
  36. #include <stdbool.h>
  37. #include "flashloader.h"
  38. #include "flash.h"
  39. /* Array holding erased state of the flash sectors. */
  40. static bool g_is_erased[FLASH_MAX_SECTOR_COUNT];
  41. extern uint8_t g_retain_buf[];
  42. uint32_t flashloader_init(struct flash_params *params, uint8_t *buf1,
  43. uint8_t *buf2)
  44. {
  45. /* Initialize params buffers */
  46. memset((void *)params, 0, 2 * sizeof(struct flash_params));
  47. params[0].buf_addr = (uint32_t)buf1;
  48. params[1].buf_addr = (uint32_t)buf2;
  49. /* Mark all sectors at "not erased" */
  50. memset(g_is_erased, false, sizeof(g_is_erased));
  51. return STATUS_OK;
  52. }
  53. uint32_t flashloader_erase_all(void)
  54. {
  55. if (flash_bank_erase() != 0)
  56. return STATUS_FAILED_ERASE_ALL;
  57. memset(g_is_erased, true, sizeof(g_is_erased));
  58. return STATUS_OK;
  59. }
  60. uint32_t flashloader_erase_sectors(uint32_t address, uint32_t byte_count)
  61. {
  62. uint32_t first_sector_idx;
  63. uint32_t last_sector_idx;
  64. uint32_t status;
  65. uint32_t idx;
  66. if (address < FLASHMEM_BASE ||
  67. (address + byte_count) > (FLASHMEM_BASE + FLASH_MAX_BYTES))
  68. return STATUS_FAILED_INVALID_ARGUMENTS;
  69. /* Convert address and count to range of sectors */
  70. first_sector_idx = flash_address_to_sector(address);
  71. last_sector_idx = flash_address_to_sector(address + byte_count - 1);
  72. /* Erase given sector(s) */
  73. for (idx = first_sector_idx; idx <= last_sector_idx; idx++) {
  74. /* Only erase sectors that haven't already been erased */
  75. if (g_is_erased[idx] == false) {
  76. status = flash_sector_erase(idx);
  77. if (status != 0) {
  78. status = (STATUS_FAILED_SECTOR_ERASE |
  79. ((idx << STATUS_EXT_INFO_S) & STATUS_EXT_INFO_M) |
  80. ((status << STATUS_ROM_CODE_S) & STATUS_ROM_CODE_M));
  81. return status;
  82. }
  83. g_is_erased[idx] = true;
  84. }
  85. }
  86. return STATUS_OK;
  87. }
  88. uint32_t flashloader_program(uint8_t *src, uint32_t address, uint32_t byte_count)
  89. {
  90. if (address < FLASHMEM_BASE ||
  91. (address + byte_count) > (FLASHMEM_BASE + FLASH_MAX_BYTES))
  92. return STATUS_FAILED_INVALID_ARGUMENTS;
  93. uint32_t status = flash_program(src, address, byte_count);
  94. if (status != 0) {
  95. status = (STATUS_FAILED_PROGRAM |
  96. ((status << STATUS_ROM_CODE_S) & STATUS_ROM_CODE_M));
  97. }
  98. return STATUS_OK;
  99. }