blob: b6c3b21209e62e3042ae10adbe4dbbfca4979127 [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 Hendricksac1d25c2016-08-09 17:00:58 -070033struct opaque_programmer opaque_programmer_none = {
34 .max_data_read = MAX_DATA_UNSPECIFIED,
35 .max_data_write = MAX_DATA_UNSPECIFIED,
36 .probe = NULL,
37 .read = NULL,
38 .write = NULL,
39 .read_status = NULL,
40 .write_status = NULL,
41 .erase = NULL,
42};
43
44struct opaque_programmer *opaque_programmer = &opaque_programmer_none;
45
Souvik Ghoshd75cd672016-06-17 14:21:39 -070046int probe_opaque(struct flashctx *flash)
hailfingerfe7cd9e2011-11-04 21:35:26 +000047{
David Hendricksac1d25c2016-08-09 17:00:58 -070048 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);
hailfingerfe7cd9e2011-11-04 21:35:26 +000056}
57
Souvik Ghoshd75cd672016-06-17 14:21:39 -070058int read_opaque(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerfe7cd9e2011-11-04 21:35:26 +000059{
David Hendricksac1d25c2016-08-09 17:00:58 -070060 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);
hailfingerfe7cd9e2011-11-04 21:35:26 +000067}
68
Souvik Ghoshd75cd672016-06-17 14:21:39 -070069int write_opaque(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
hailfingerfe7cd9e2011-11-04 21:35:26 +000070{
David Hendricksac1d25c2016-08-09 17:00:58 -070071 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);
hailfingerfe7cd9e2011-11-04 21:35:26 +000078}
79
Souvik Ghoshd75cd672016-06-17 14:21:39 -070080int erase_opaque(struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen)
hailfingerfe7cd9e2011-11-04 21:35:26 +000081{
David Hendricksac1d25c2016-08-09 17:00:58 -070082 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);
hailfingerfe7cd9e2011-11-04 21:35:26 +000089}
90
Souvik Ghoshd75cd672016-06-17 14:21:39 -070091uint8_t read_status_opaque(const struct flashctx *flash)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +053092{
David Hendricksac1d25c2016-08-09 17:00:58 -070093 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);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530100}
101
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700102int write_status_opaque(const struct flashctx *flash, int status)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530103{
David Hendricksac1d25c2016-08-09 17:00:58 -0700104 if (!opaque_programmer->write_status) {
105 msg_perr("%s called before register_opaque_programmer. "
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700106 "Please report a bug at flashrom@flashrom.org\n",
hailfingerfe7cd9e2011-11-04 21:35:26 +0000107 __func__);
David Hendricksac1d25c2016-08-09 17:00:58 -0700108 return 1;
hailfingerfe7cd9e2011-11-04 21:35:26 +0000109 }
David Hendricksac1d25c2016-08-09 17:00:58 -0700110 return opaque_programmer->write_status(flash, status);
111}
112
113void register_opaque_programmer(struct opaque_programmer *pgm)
114{
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;
hailfingerfe7cd9e2011-11-04 21:35:26 +0000123}