opaque.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2011 Carl-Daniel Hailfinger
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  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 Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /*
  20. * Contains the opaque programmer framework.
  21. * An opaque programmer is a programmer which does not provide direct access
  22. * to the flash chip and which abstracts all flash chip properties into a
  23. * programmer specific interface.
  24. */
  25. #include <stdint.h>
  26. #include "flash.h"
  27. #include "flashchips.h"
  28. #include "chipdrivers.h"
  29. #include "programmer.h"
  30. struct opaque_programmer opaque_programmer_none = {
  31. .max_data_read = MAX_DATA_UNSPECIFIED,
  32. .max_data_write = MAX_DATA_UNSPECIFIED,
  33. .probe = NULL,
  34. .read = NULL,
  35. .write = NULL,
  36. .read_status = NULL,
  37. .write_status = NULL,
  38. .erase = NULL,
  39. };
  40. struct opaque_programmer *opaque_programmer = &opaque_programmer_none;
  41. int probe_opaque(struct flashctx *flash)
  42. {
  43. if (!opaque_programmer->probe) {
  44. msg_perr("%s called before register_opaque_programmer. "
  45. "Please report a bug at flashrom@flashrom.org\n",
  46. __func__);
  47. return 0;
  48. }
  49. return opaque_programmer->probe(flash);
  50. }
  51. int read_opaque(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
  52. {
  53. if (!opaque_programmer->read) {
  54. msg_perr("%s called before register_opaque_programmer. "
  55. "Please report a bug at flashrom@flashrom.org\n",
  56. __func__);
  57. return 1;
  58. }
  59. return opaque_programmer->read(flash, buf, start, len);
  60. }
  61. int write_opaque(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
  62. {
  63. if (!opaque_programmer->write) {
  64. msg_perr("%s called before register_opaque_programmer. "
  65. "Please report a bug at flashrom@flashrom.org\n",
  66. __func__);
  67. return 1;
  68. }
  69. return opaque_programmer->write(flash, buf, start, len);
  70. }
  71. int erase_opaque(struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen)
  72. {
  73. if (!opaque_programmer->erase) {
  74. msg_perr("%s called before register_opaque_programmer. "
  75. "Please report a bug at flashrom@flashrom.org\n",
  76. __func__);
  77. return 1;
  78. }
  79. return opaque_programmer->erase(flash, blockaddr, blocklen);
  80. }
  81. uint8_t read_status_opaque(const struct flashctx *flash)
  82. {
  83. if (!opaque_programmer->read_status) {
  84. msg_perr("%s called before register_opaque_programmer. "
  85. "Please report a bug at flashrom@flashrom.org\n",
  86. __func__);
  87. return 1;
  88. }
  89. return opaque_programmer->read_status(flash);
  90. }
  91. int write_status_opaque(const struct flashctx *flash, int status)
  92. {
  93. if (!opaque_programmer->write_status) {
  94. msg_perr("%s called before register_opaque_programmer. "
  95. "Please report a bug at flashrom@flashrom.org\n",
  96. __func__);
  97. return 1;
  98. }
  99. return opaque_programmer->write_status(flash, status);
  100. }
  101. void register_opaque_programmer(struct opaque_programmer *pgm)
  102. {
  103. if (!pgm->probe || !pgm->read || !pgm->write || !pgm->erase) {
  104. msg_perr("%s called with one of probe/read/write/erase being "
  105. "NULL. Please report a bug at flashrom@flashrom.org\n",
  106. __func__);
  107. return;
  108. }
  109. opaque_programmer = pgm;
  110. buses_supported |= BUS_PROG;
  111. }