blob: 7a11b90c278d39e9afed9c8f8a83e3df50417eb4 [file] [log] [blame]
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00004 * Copyright (C) 2011,2013,2014 Carl-Daniel Hailfinger
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +00005 *
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 Hailfinger532c7172011-11-04 21:35:26 +000014 */
15
16/*
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000017 * Contains the opaque master framework.
18 * An opaque master is a master which does not provide direct access
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000019 * to the flash chip and which abstracts all flash chip properties into a
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000020 * master specific interface.
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000021 */
22
23#include <stdint.h>
Nico Huberc3eaa952026-03-07 23:22:54 +010024#include <stdlib.h>
25#include <string.h>
26
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000027#include "flash.h"
28#include "flashchips.h"
Nico Huber05790292026-03-09 17:24:08 +010029#include "chipdrivers/opaque.h"
Nico Huberc3eaa952026-03-07 23:22:54 +010030#include "chipdrivers/probing.h"
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000031#include "programmer.h"
32
Nico Huberc3eaa952026-03-07 23:22:54 +010033struct found_id *probe_opaque(const struct bus_probe *probe,
34 const struct master_common *mst,
35 const struct flashchip *chip)
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000036{
Nico Huberc3eaa952026-03-07 23:22:54 +010037 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 Huber91f51522026-03-07 22:57:56 +010048}
49
50int prepare_opaque(struct flashctx *flash, enum preparation_steps step)
51{
52 if (step != PREPARE_POST_PROBE)
53 return 0;
54 return flash->mst.opaque->prepare(flash) ? 0 : -1;
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000055}
56
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000057int read_opaque(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000058{
Nico Huber9a11cbf2023-01-13 01:19:07 +010059 return flash->mst.opaque->read(flash, buf, start, len);
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000060}
61
Mark Marshallf20b7be2014-05-09 21:16:21 +000062int write_opaque(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000063{
Nico Huber9a11cbf2023-01-13 01:19:07 +010064 return flash->mst.opaque->write(flash, buf, start, len);
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000065}
66
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000067int erase_opaque(struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen)
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000068{
Nico Huber9a11cbf2023-01-13 01:19:07 +010069 return flash->mst.opaque->erase(flash, blockaddr, blocklen);
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000070}
71
Nico Huberc3eaa952026-03-07 23:22:54 +010072static const struct bus_probe opaque_probes[] = {
73 /* prio. type function function argument */
74 { 0, ID_OPAQUE, probe_opaque, NULL },
75};
76
77static bool opaque_probe_match(const struct flashchip *chip, const struct id_info_ext *found)
78{
79 return memcmp(&chip->id, &found->id, sizeof(found->id)) == 0;
80}
81
Anastasia Klimchuk21b20212021-05-13 12:28:47 +100082int register_opaque_master(const struct opaque_master *mst, void *data)
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000083{
Nico Huberaf9d7382023-05-01 13:33:26 +020084 struct registered_master rmst = { 0 };
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +000085
Anastasia Klimchuka1fed9f2021-08-03 14:08:02 +100086 if (mst->shutdown) {
87 if (register_shutdown(mst->shutdown, data)) {
88 mst->shutdown(data); /* cleanup */
89 return 1;
90 }
91 }
92
Nico Huber91f51522026-03-07 22:57:56 +010093 if (!mst->prepare || !mst->read || !mst->write || !mst->erase) {
Nico Huberac90af62022-12-18 00:22:47 +000094 msg_perr("%s called with incomplete master definition.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +020095 "Please report a bug at flashprog@flashprog.org\n",
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000096 __func__);
Nico Huberc3b02dc2023-08-12 01:13:45 +020097 return ERROR_FLASHPROG_BUG;
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +000098 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +000099 rmst.buses_supported = BUS_PROG;
Nico Huberc3eaa952026-03-07 23:22:54 +0100100 rmst.probing.probe_count = ARRAY_SIZE(opaque_probes);
101 rmst.probing.probes = opaque_probes;
102 rmst.probing.match = opaque_probe_match;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000103 rmst.opaque = *mst;
Anastasia Klimchuk21b20212021-05-13 12:28:47 +1000104 if (data)
105 rmst.opaque.data = data;
Nico Huber006d08d2026-03-10 22:33:22 +0100106
107 rmst.common.max_rom_decode = MAX_ROM_DECODE_UNLIMITED;
108
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000109 return register_master(&rmst);
Carl-Daniel Hailfinger532c7172011-11-04 21:35:26 +0000110}