blob: a8a9a493a308a663fcbe6d95aec17695b40d30bc [file] [log] [blame]
hailfingerfe7cd9e2011-11-04 21:35:26 +00001/*
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/*
21 * Contains the opaque programmer framework.
22 * An opaque programmer is a programmer which does not provide direct access
23 * to the flash chip and which abstracts all flash chip properties into a
24 * programmer specific interface.
25 */
26
27#include <stdint.h>
28#include "flash.h"
29#include "flashchips.h"
30#include "chipdrivers.h"
31#include "programmer.h"
32
David Hendricks292edf02013-07-11 16:12:58 -070033struct opaque_programmer opaque_programmer_none = {
hailfingerfe7cd9e2011-11-04 21:35:26 +000034 .max_data_read = MAX_DATA_UNSPECIFIED,
35 .max_data_write = MAX_DATA_UNSPECIFIED,
36 .probe = NULL,
37 .read = NULL,
38 .write = NULL,
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +053039 .read_status = NULL,
40 .write_status = NULL,
hailfingerfe7cd9e2011-11-04 21:35:26 +000041 .erase = NULL,
42};
43
David Hendricks292edf02013-07-11 16:12:58 -070044struct opaque_programmer *opaque_programmer = &opaque_programmer_none;
hailfingerfe7cd9e2011-11-04 21:35:26 +000045
46int probe_opaque(struct flashchip *flash)
47{
48 if (!opaque_programmer->probe) {
49 msg_perr("%s called before register_opaque_programmer. "
50 "Please report a bug at flashrom@flashrom.org\n",
51 __func__);
52 return 0;
53 }
54
55 return opaque_programmer->probe(flash);
56}
57
stefanctc5eb8a92011-11-23 09:13:48 +000058int read_opaque(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerfe7cd9e2011-11-04 21:35:26 +000059{
60 if (!opaque_programmer->read) {
61 msg_perr("%s called before register_opaque_programmer. "
62 "Please report a bug at flashrom@flashrom.org\n",
63 __func__);
64 return 1;
65 }
66 return opaque_programmer->read(flash, buf, start, len);
67}
68
stefanctc5eb8a92011-11-23 09:13:48 +000069int write_opaque(struct flashchip *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerfe7cd9e2011-11-04 21:35:26 +000070{
71 if (!opaque_programmer->write) {
72 msg_perr("%s called before register_opaque_programmer. "
73 "Please report a bug at flashrom@flashrom.org\n",
74 __func__);
75 return 1;
76 }
77 return opaque_programmer->write(flash, buf, start, len);
78}
79
80int erase_opaque(struct flashchip *flash, unsigned int blockaddr, unsigned int blocklen)
81{
82 if (!opaque_programmer->erase) {
83 msg_perr("%s called before register_opaque_programmer. "
84 "Please report a bug at flashrom@flashrom.org\n",
85 __func__);
86 return 1;
87 }
88 return opaque_programmer->erase(flash, blockaddr, blocklen);
89}
90
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +053091uint8_t read_status_opaque(const struct flashchip *flash)
92{
93 if (!opaque_programmer->read_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->read_status(flash);
100}
101
102int write_status_opaque(const struct flashchip *flash, int status)
103{
104 if (!opaque_programmer->write_status) {
105 msg_perr("%s called before register_opaque_programmer. "
106 "Please report a bug at flashrom@flashrom.org\n",
107 __func__);
108 return 1;
109 }
110 return opaque_programmer->write_status(flash, status);
111}
112
David Hendricks292edf02013-07-11 16:12:58 -0700113void register_opaque_programmer(struct opaque_programmer *pgm)
hailfingerfe7cd9e2011-11-04 21:35:26 +0000114{
115 if (!pgm->probe || !pgm->read || !pgm->write || !pgm->erase) {
116 msg_perr("%s called with one of probe/read/write/erase being "
117 "NULL. Please report a bug at flashrom@flashrom.org\n",
118 __func__);
119 return;
120 }
121 opaque_programmer = pgm;
122 buses_supported |= BUS_PROG;
123}