OpenOCD
virtual.c
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2010 by Spencer Oliver *
3  * spen@spen-soft.co.uk *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
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, see <http://www.gnu.org/licenses/>. *
17  ***************************************************************************/
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "imp.h"
24 
25 static struct flash_bank *virtual_get_master_bank(struct flash_bank *bank)
26 {
27  struct flash_bank *master_bank;
28 
29  master_bank = get_flash_bank_by_name_noprobe(bank->driver_priv);
30  if (master_bank == NULL)
31  LOG_ERROR("master flash bank '%s' does not exist", (char *)bank->driver_priv);
32 
33  return master_bank;
34 }
35 
36 static void virtual_update_bank_info(struct flash_bank *bank)
37 {
38  struct flash_bank *master_bank = virtual_get_master_bank(bank);
39 
40  if (master_bank == NULL)
41  return;
42 
43  /* update the info we do not have */
44  bank->size = master_bank->size;
45  bank->chip_width = master_bank->chip_width;
46  bank->bus_width = master_bank->bus_width;
47  bank->erased_value = master_bank->erased_value;
48  bank->default_padded_value = master_bank->default_padded_value;
49  bank->num_sectors = master_bank->num_sectors;
50  bank->sectors = master_bank->sectors;
51 }
52 
53 FLASH_BANK_COMMAND_HANDLER(virtual_flash_bank_command)
54 {
55  if (CMD_ARGC < 7)
57 
58  /* get the master flash bank */
59  const char *bank_name = CMD_ARGV[6];
60  struct flash_bank *master_bank = get_flash_bank_by_name_noprobe(bank_name);
61 
62  if (master_bank == NULL) {
63  LOG_ERROR("master flash bank '%s' does not exist", bank_name);
65  }
66 
67  /* save master bank name - use this to get settings later */
68  bank->driver_priv = strdup(bank_name);
69 
70  return ERROR_OK;
71 }
72 
73 static int virtual_protect(struct flash_bank *bank, int set, int first, int last)
74 {
75  struct flash_bank *master_bank = virtual_get_master_bank(bank);
76  int retval;
77 
78  if (master_bank == NULL)
80 
81  /* call master handler */
82  retval = master_bank->driver->protect(master_bank, set, first, last);
83  if (retval != ERROR_OK)
84  return retval;
85 
86  return ERROR_OK;
87 }
88 
89 static int virtual_protect_check(struct flash_bank *bank)
90 {
91  struct flash_bank *master_bank = virtual_get_master_bank(bank);
92  int retval;
93 
94  if (master_bank == NULL)
96 
97  /* call master handler */
98  retval = master_bank->driver->protect_check(master_bank);
99  if (retval != ERROR_OK)
100  return retval;
101 
102  return ERROR_OK;
103 }
104 
105 static int virtual_erase(struct flash_bank *bank, int first, int last)
106 {
107  struct flash_bank *master_bank = virtual_get_master_bank(bank);
108  int retval;
109 
110  if (master_bank == NULL)
112 
113  /* call master handler */
114  retval = master_bank->driver->erase(master_bank, first, last);
115  if (retval != ERROR_OK)
116  return retval;
117 
118  return ERROR_OK;
119 }
120 
121 static int virtual_write(struct flash_bank *bank, const uint8_t *buffer,
122  uint32_t offset, uint32_t count)
123 {
124  struct flash_bank *master_bank = virtual_get_master_bank(bank);
125  int retval;
126 
127  if (master_bank == NULL)
129 
130  /* call master handler */
131  retval = master_bank->driver->write(master_bank, buffer, offset, count);
132  if (retval != ERROR_OK)
133  return retval;
134 
135  return ERROR_OK;
136 }
137 
138 static int virtual_probe(struct flash_bank *bank)
139 {
140  struct flash_bank *master_bank = virtual_get_master_bank(bank);
141  int retval;
142 
143  if (master_bank == NULL)
145 
146  /* call master handler */
147  retval = master_bank->driver->probe(master_bank);
148  if (retval != ERROR_OK)
149  return retval;
150 
151  /* update the info we do not have */
153 
154  return ERROR_OK;
155 }
156 
157 static int virtual_auto_probe(struct flash_bank *bank)
158 {
159  struct flash_bank *master_bank = virtual_get_master_bank(bank);
160  int retval;
161 
162  if (master_bank == NULL)
164 
165  /* call master handler */
166  retval = master_bank->driver->auto_probe(master_bank);
167  if (retval != ERROR_OK)
168  return retval;
169 
170  /* update the info we do not have */
172 
173  return ERROR_OK;
174 }
175 
176 static int virtual_info(struct flash_bank *bank, char *buf, int buf_size)
177 {
178  struct flash_bank *master_bank = virtual_get_master_bank(bank);
179 
180  if (master_bank == NULL)
182 
183  snprintf(buf, buf_size, "%s driver for flash bank %s at 0x%8.8" PRIx32 "",
184  bank->driver->name, master_bank->name, master_bank->base);
185 
186  return ERROR_OK;
187 }
188 
189 static int virtual_blank_check(struct flash_bank *bank)
190 {
191  struct flash_bank *master_bank = virtual_get_master_bank(bank);
192  int retval;
193 
194  if (master_bank == NULL)
196 
197  /* call master handler */
198  retval = master_bank->driver->erase_check(master_bank);
199  if (retval != ERROR_OK)
200  return retval;
201 
202  return ERROR_OK;
203 }
204 
205 static int virtual_flash_read(struct flash_bank *bank,
206  uint8_t *buffer, uint32_t offset, uint32_t count)
207 {
208  struct flash_bank *master_bank = virtual_get_master_bank(bank);
209  int retval;
210 
211  if (master_bank == NULL)
213 
214  /* call master handler */
215  retval = master_bank->driver->read(master_bank, buffer, offset, count);
216  if (retval != ERROR_OK)
217  return retval;
218 
219  return ERROR_OK;
220 }
221 
223  .name = "virtual",
224  .flash_bank_command = virtual_flash_bank_command,
225  .erase = virtual_erase,
226  .protect = virtual_protect,
227  .write = virtual_write,
228  .read = virtual_flash_read,
229  .probe = virtual_probe,
230  .auto_probe = virtual_auto_probe,
231  .erase_check = virtual_blank_check,
232  .protect_check = virtual_protect_check,
233  .info = virtual_info,
234 };
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:40
int(* probe)(struct flash_bank *bank)
Probe to determine what kind of flash is present.
struct flash_bank * get_flash_bank_by_name_noprobe(const char *name)
Returns the flash bank specified by name, which matches the driver name and a suffix (option) specify...
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
uint32_t size
The size of this chip bank, in bytes.
Definition: nor/core.h:88
int(* erase)(struct flash_bank *bank, int first, int last)
Bank/sector erase routine (target-specific).
static int virtual_probe(struct flash_bank *bank)
Definition: virtual.c:138
static int virtual_protect(struct flash_bank *bank, int set, int first, int last)
Definition: virtual.c:73
void * driver_priv
Private driver storage pointer.
Definition: nor/core.h:84
struct flash_sector * sectors
Array of sectors, allocated and initialized by the flash driver.
Definition: nor/core.h:107
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:126
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:121
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
struct flash_driver virtual_flash
Definition: virtual.c:222
int(* read)(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Read data from the flash.
static int virtual_blank_check(struct flash_bank *bank)
Definition: virtual.c:189
int chip_width
Width of the chip in bytes (1,2,4 bytes)
Definition: nor/core.h:90
static struct flash_bank * virtual_get_master_bank(struct flash_bank *bank)
Definition: virtual.c:25
#define LOG_ERROR(expr...)
Definition: log.h:129
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:338
static int virtual_protect_check(struct flash_bank *bank)
Definition: virtual.c:89
uint8_t erased_value
Erased value.
Definition: nor/core.h:94
static void virtual_update_bank_info(struct flash_bank *bank)
Definition: virtual.c:36
uint8_t default_padded_value
Default padded value used, normally this matches the flash erased value.
Definition: nor/core.h:98
int bus_width
Maximum bus width, in bytes (1,2,4 bytes)
Definition: nor/core.h:91
int(* protect_check)(struct flash_bank *bank)
Determine if the specific bank is "protected" or not.
int(* auto_probe)(struct flash_bank *bank)
A more gentle flavor of filash_driver_s::probe, performing setup with less noise. ...
static int virtual_erase(struct flash_bank *bank, int first, int last)
Definition: virtual.c:105
static int virtual_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: virtual.c:121
static int virtual_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: virtual.c:205
int num_sectors
The number of sectors on this chip.
Definition: nor/core.h:105
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:78
FLASH_BANK_COMMAND_HANDLER(virtual_flash_bank_command)
Definition: virtual.c:53
int(* protect)(struct flash_bank *bank, int set, int first, int last)
Bank/sector protection routine (target-specific).
#define ERROR_OK
Definition: log.h:144
static int virtual_info(struct flash_bank *bank, char *buf, int buf_size)
Definition: virtual.c:176
static int count
Definition: helper/log.c:62
static int virtual_auto_probe(struct flash_bank *bank)
Definition: virtual.c:157
struct flash_driver * driver
Driver for this bank.
Definition: nor/core.h:83
#define NULL
Definition: usb.h:27
int(* write)(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Program data into the flash.
int(* erase_check)(struct flash_bank *bank)
Check the erasure status of a flash bank.
const char * name
Definition: nor/core.h:79
uint32_t base
The base address of this bank.
Definition: nor/core.h:87