Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 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 | /* |
| 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 | |
| 33 | const struct 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 | .erase = NULL, |
| 40 | }; |
| 41 | |
| 42 | const struct opaque_programmer *opaque_programmer = &opaque_programmer_none; |
| 43 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 44 | int probe_opaque(struct flashctx *flash) |
Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 45 | { |
| 46 | if (!opaque_programmer->probe) { |
| 47 | msg_perr("%s called before register_opaque_programmer. " |
| 48 | "Please report a bug at flashrom@flashrom.org\n", |
| 49 | __func__); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | return opaque_programmer->probe(flash); |
| 54 | } |
| 55 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 56 | int read_opaque(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 57 | { |
| 58 | if (!opaque_programmer->read) { |
| 59 | msg_perr("%s called before register_opaque_programmer. " |
| 60 | "Please report a bug at flashrom@flashrom.org\n", |
| 61 | __func__); |
| 62 | return 1; |
| 63 | } |
| 64 | return opaque_programmer->read(flash, buf, start, len); |
| 65 | } |
| 66 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 67 | int write_opaque(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 68 | { |
| 69 | if (!opaque_programmer->write) { |
| 70 | msg_perr("%s called before register_opaque_programmer. " |
| 71 | "Please report a bug at flashrom@flashrom.org\n", |
| 72 | __func__); |
| 73 | return 1; |
| 74 | } |
| 75 | return opaque_programmer->write(flash, buf, start, len); |
| 76 | } |
| 77 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 78 | int erase_opaque(struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen) |
Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 79 | { |
| 80 | if (!opaque_programmer->erase) { |
| 81 | msg_perr("%s called before register_opaque_programmer. " |
| 82 | "Please report a bug at flashrom@flashrom.org\n", |
| 83 | __func__); |
| 84 | return 1; |
| 85 | } |
| 86 | return opaque_programmer->erase(flash, blockaddr, blocklen); |
| 87 | } |
| 88 | |
| 89 | void register_opaque_programmer(const struct opaque_programmer *pgm) |
| 90 | { |
| 91 | if (!pgm->probe || !pgm->read || !pgm->write || !pgm->erase) { |
| 92 | msg_perr("%s called with one of probe/read/write/erase being " |
| 93 | "NULL. Please report a bug at flashrom@flashrom.org\n", |
| 94 | __func__); |
| 95 | return; |
| 96 | } |
| 97 | opaque_programmer = pgm; |
| 98 | buses_supported |= BUS_PROG; |
| 99 | } |