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.

143 lines
4.3KB

  1. /***************************************************************************
  2. * Copyright (C) 2009 √ėyvind Harboe *
  3. * oyvind.harboe@zylin.com *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "imp.h"
  24. #include <target/image.h>
  25. #include "hello.h"
  26. struct faux_flash_bank {
  27. struct target *target;
  28. uint8_t *memory;
  29. uint32_t start_address;
  30. };
  31. static const int sectorSize = 0x10000;
  32. /* flash bank faux <base> <size> <chip_width> <bus_width> <target#> <driverPath>
  33. */
  34. FLASH_BANK_COMMAND_HANDLER(faux_flash_bank_command)
  35. {
  36. struct faux_flash_bank *info;
  37. if (CMD_ARGC < 6)
  38. return ERROR_COMMAND_SYNTAX_ERROR;
  39. info = malloc(sizeof(struct faux_flash_bank));
  40. if (info == NULL) {
  41. LOG_ERROR("no memory for flash bank info");
  42. return ERROR_FAIL;
  43. }
  44. info->memory = malloc(bank->size);
  45. if (info == NULL) {
  46. free(info);
  47. LOG_ERROR("no memory for flash bank info");
  48. return ERROR_FAIL;
  49. }
  50. bank->driver_priv = info;
  51. /* Use 0x10000 as a fixed sector size. */
  52. int i = 0;
  53. uint32_t offset = 0;
  54. bank->num_sectors = bank->size/sectorSize;
  55. bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
  56. for (i = 0; i < bank->num_sectors; i++) {
  57. bank->sectors[i].offset = offset;
  58. bank->sectors[i].size = sectorSize;
  59. offset += bank->sectors[i].size;
  60. bank->sectors[i].is_erased = -1;
  61. bank->sectors[i].is_protected = 0;
  62. }
  63. info->target = get_target(CMD_ARGV[5]);
  64. if (info->target == NULL) {
  65. LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
  66. free(info->memory);
  67. free(info);
  68. return ERROR_FAIL;
  69. }
  70. return ERROR_OK;
  71. }
  72. static int faux_erase(struct flash_bank *bank, int first, int last)
  73. {
  74. struct faux_flash_bank *info = bank->driver_priv;
  75. memset(info->memory + first*sectorSize, 0xff, sectorSize*(last-first + 1));
  76. return ERROR_OK;
  77. }
  78. static int faux_protect(struct flash_bank *bank, int set, int first, int last)
  79. {
  80. LOG_USER("set protection sector %d to %d to %s", first, last, set ? "on" : "off");
  81. return ERROR_OK;
  82. }
  83. static int faux_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
  84. {
  85. struct faux_flash_bank *info = bank->driver_priv;
  86. memcpy(info->memory + offset, buffer, count);
  87. return ERROR_OK;
  88. }
  89. static int faux_protect_check(struct flash_bank *bank)
  90. {
  91. return ERROR_OK;
  92. }
  93. static int faux_info(struct flash_bank *bank, char *buf, int buf_size)
  94. {
  95. snprintf(buf, buf_size, "faux flash driver");
  96. return ERROR_OK;
  97. }
  98. static int faux_probe(struct flash_bank *bank)
  99. {
  100. return ERROR_OK;
  101. }
  102. static const struct command_registration faux_command_handlers[] = {
  103. {
  104. .name = "faux",
  105. .mode = COMMAND_ANY,
  106. .help = "faux flash command group",
  107. .chain = hello_command_handlers,
  108. },
  109. COMMAND_REGISTRATION_DONE
  110. };
  111. struct flash_driver faux_flash = {
  112. .name = "faux",
  113. .commands = faux_command_handlers,
  114. .flash_bank_command = faux_flash_bank_command,
  115. .erase = faux_erase,
  116. .protect = faux_protect,
  117. .write = faux_write,
  118. .read = default_flash_read,
  119. .probe = faux_probe,
  120. .auto_probe = faux_probe,
  121. .erase_check = default_flash_blank_check,
  122. .protect_check = faux_protect_check,
  123. .info = faux_info
  124. };