| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 4 | * Copyright (C) 2011,2013,2014 Carl-Daniel Hailfinger |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 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. |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | /* |
| Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 17 | * Contains the opaque master framework. |
| 18 | * An opaque master is a master which does not provide direct access |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 19 | * to the flash chip and which abstracts all flash chip properties into a |
| Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 20 | * master specific interface. |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <stdint.h> |
| Nico Huber | c3eaa95 | 2026-03-07 23:22:54 +0100 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 27 | #include "flash.h" |
| 28 | #include "flashchips.h" |
| Nico Huber | 0579029 | 2026-03-09 17:24:08 +0100 | [diff] [blame] | 29 | #include "chipdrivers/opaque.h" |
| Nico Huber | c3eaa95 | 2026-03-07 23:22:54 +0100 | [diff] [blame] | 30 | #include "chipdrivers/probing.h" |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 31 | #include "programmer.h" |
| 32 | |
| Nico Huber | c3eaa95 | 2026-03-07 23:22:54 +0100 | [diff] [blame] | 33 | struct found_id *probe_opaque(const struct bus_probe *probe, |
| 34 | const struct master_common *mst, |
| 35 | const struct flashchip *chip) |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 36 | { |
| Nico Huber | c3eaa95 | 2026-03-07 23:22:54 +0100 | [diff] [blame] | 37 | struct found_id *const found = calloc(1, sizeof(*found)); |
| 38 | if (!found) { |
| 39 | msg_cerr("Out of memory!\n"); |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | found->info.id.type = ID_OPAQUE; |
| 44 | found->info.id.manufacture = PROGMANUF_ID; |
| 45 | found->info.id.model = PROGDEV_ID; |
| 46 | |
| 47 | return found; |
| Nico Huber | 91f5152 | 2026-03-07 22:57:56 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| Nico Huber | 517d937 | 2026-03-14 21:48:36 +0100 | [diff] [blame] | 50 | int prepare_opaque(struct flashctx *flash) |
| Nico Huber | 91f5152 | 2026-03-07 22:57:56 +0100 | [diff] [blame] | 51 | { |
| Nico Huber | 91f5152 | 2026-03-07 22:57:56 +0100 | [diff] [blame] | 52 | return flash->mst.opaque->prepare(flash) ? 0 : -1; |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 55 | 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] | 56 | { |
| Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 57 | return flash->mst.opaque->read(flash, buf, start, len); |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 60 | int write_opaque(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 61 | { |
| Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 62 | return flash->mst.opaque->write(flash, buf, start, len); |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 65 | 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] | 66 | { |
| Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 67 | return flash->mst.opaque->erase(flash, blockaddr, blocklen); |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| Nico Huber | c3eaa95 | 2026-03-07 23:22:54 +0100 | [diff] [blame] | 70 | static const struct bus_probe opaque_probes[] = { |
| 71 | /* prio. type function function argument */ |
| 72 | { 0, ID_OPAQUE, probe_opaque, NULL }, |
| 73 | }; |
| 74 | |
| 75 | static bool opaque_probe_match(const struct flashchip *chip, const struct id_info_ext *found) |
| 76 | { |
| 77 | return memcmp(&chip->id, &found->id, sizeof(found->id)) == 0; |
| 78 | } |
| 79 | |
| Anastasia Klimchuk | 21b2021 | 2021-05-13 12:28:47 +1000 | [diff] [blame] | 80 | int register_opaque_master(const struct opaque_master *mst, void *data) |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 81 | { |
| Nico Huber | af9d738 | 2023-05-01 13:33:26 +0200 | [diff] [blame] | 82 | struct registered_master rmst = { 0 }; |
| Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 83 | |
| Anastasia Klimchuk | a1fed9f | 2021-08-03 14:08:02 +1000 | [diff] [blame] | 84 | if (mst->shutdown) { |
| 85 | if (register_shutdown(mst->shutdown, data)) { |
| 86 | mst->shutdown(data); /* cleanup */ |
| 87 | return 1; |
| 88 | } |
| 89 | } |
| 90 | |
| Nico Huber | 91f5152 | 2026-03-07 22:57:56 +0100 | [diff] [blame] | 91 | if (!mst->prepare || !mst->read || !mst->write || !mst->erase) { |
| Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 92 | msg_perr("%s called with incomplete master definition.\n" |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 93 | "Please report a bug at flashprog@flashprog.org\n", |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 94 | __func__); |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 95 | return ERROR_FLASHPROG_BUG; |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 96 | } |
| Nico Huber | 0d2b45e | 2026-03-23 22:42:46 +0100 | [diff] [blame] | 97 | rmst.buses_supported = BUS_OPAQUE; |
| Nico Huber | c3eaa95 | 2026-03-07 23:22:54 +0100 | [diff] [blame] | 98 | rmst.probing.probe_count = ARRAY_SIZE(opaque_probes); |
| 99 | rmst.probing.probes = opaque_probes; |
| 100 | rmst.probing.match = opaque_probe_match; |
| Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 101 | rmst.opaque = *mst; |
| Anastasia Klimchuk | 21b2021 | 2021-05-13 12:28:47 +1000 | [diff] [blame] | 102 | if (data) |
| 103 | rmst.opaque.data = data; |
| Nico Huber | 006d08d | 2026-03-10 22:33:22 +0100 | [diff] [blame] | 104 | |
| 105 | rmst.common.max_rom_decode = MAX_ROM_DECODE_UNLIMITED; |
| 106 | |
| Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 107 | return register_master(&rmst); |
| Carl-Daniel Hailfinger | 532c717 | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 108 | } |