1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2011,2013,2014 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
16 /*
17 * Contains the opaque master framework.
18 * An opaque master is a master which does not provide direct access
19 * to the flash chip and which abstracts all flash chip properties into a
20 * master specific interface.
21 */
22
23 #include <stdint.h>
24 #include "flash.h"
25 #include "flashchips.h"
26 #include "chipdrivers.h"
27 #include "programmer.h"
28
probe_opaque(struct flashctx * flash)29 int probe_opaque(struct flashctx *flash)
30 {
31 return flash->mst->opaque.probe(flash);
32 }
33
read_opaque(struct flashctx * flash,uint8_t * buf,unsigned int start,unsigned int len)34 int read_opaque(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
35 {
36 return flash->mst->opaque.read(flash, buf, start, len);
37 }
38
write_opaque(struct flashctx * flash,const uint8_t * buf,unsigned int start,unsigned int len)39 int write_opaque(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
40 {
41 return flash->mst->opaque.write(flash, buf, start, len);
42 }
43
erase_opaque(struct flashctx * flash,unsigned int blockaddr,unsigned int blocklen)44 int erase_opaque(struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen)
45 {
46 return flash->mst->opaque.erase(flash, blockaddr, blocklen);
47 }
48
register_opaque_master(const struct opaque_master * mst,void * data)49 int register_opaque_master(const struct opaque_master *mst, void *data)
50 {
51 struct registered_master rmst = {0};
52
53 if (mst->shutdown) {
54 if (register_shutdown(mst->shutdown, data)) {
55 mst->shutdown(data); /* cleanup */
56 return 1;
57 }
58 }
59
60 if (!mst->probe || !mst->read || !mst->write || !mst->erase) {
61 msg_perr("%s called with incomplete master definition. "
62 "Please report a bug at [email protected]\n",
63 __func__);
64 return ERROR_FLASHROM_BUG;
65 }
66 rmst.buses_supported = BUS_PROG;
67 rmst.opaque = *mst;
68 if (data)
69 rmst.opaque.data = data;
70 return register_master(&rmst);
71 }
72