OpenOCD
faux.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2009 Øyvind Harboe *
5  * oyvind.harboe@zylin.com *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "imp.h"
13 #include <target/image.h>
14 #include "hello.h"
15 
17  struct target *target;
18  uint8_t *memory;
19  uint32_t start_address;
20 };
21 
22 static const int sector_size = 0x10000;
23 
24 
25 /* flash bank faux <base> <size> <chip_width> <bus_width> <target#> <driverPath>
26  */
27 FLASH_BANK_COMMAND_HANDLER(faux_flash_bank_command)
28 {
29  struct faux_flash_bank *info;
30 
31  if (CMD_ARGC < 6)
33 
34  info = malloc(sizeof(struct faux_flash_bank));
35  if (!info) {
36  LOG_ERROR("no memory for flash bank info");
37  return ERROR_FAIL;
38  }
39  info->memory = malloc(bank->size);
40  if (!info->memory) {
41  free(info);
42  LOG_ERROR("no memory for flash bank info");
43  return ERROR_FAIL;
44  }
45  bank->driver_priv = info;
46 
47  /* Use 0x10000 as a fixed sector size. */
48  uint32_t offset = 0;
49  bank->num_sectors = bank->size/sector_size;
50  bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
51  for (unsigned int i = 0; i < bank->num_sectors; i++) {
52  bank->sectors[i].offset = offset;
53  bank->sectors[i].size = sector_size;
54  offset += bank->sectors[i].size;
55  bank->sectors[i].is_erased = -1;
56  bank->sectors[i].is_protected = 0;
57  }
58 
59  info->target = get_target(CMD_ARGV[5]);
60  if (!info->target) {
61  LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
62  free(info->memory);
63  free(info);
64  return ERROR_FAIL;
65  }
66  return ERROR_OK;
67 }
68 
69 static int faux_erase(struct flash_bank *bank, unsigned int first,
70  unsigned int last)
71 {
72  struct faux_flash_bank *info = bank->driver_priv;
73  memset(info->memory + first*sector_size, 0xff, sector_size*(last-first + 1));
74  return ERROR_OK;
75 }
76 
77 static int faux_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
78 {
79  struct faux_flash_bank *info = bank->driver_priv;
80  memcpy(info->memory + offset, buffer, count);
81  return ERROR_OK;
82 }
83 
84 static int faux_info(struct flash_bank *bank, struct command_invocation *cmd)
85 {
86  command_print_sameline(cmd, "faux flash driver");
87  return ERROR_OK;
88 }
89 
90 static int faux_probe(struct flash_bank *bank)
91 {
92  return ERROR_OK;
93 }
94 
95 static const struct command_registration faux_command_handlers[] = {
96  {
97  .name = "faux",
98  .mode = COMMAND_ANY,
99  .help = "faux flash command group",
100  .chain = hello_command_handlers,
101  .usage = "",
102  },
104 };
105 
106 const struct flash_driver faux_flash = {
107  .name = "faux",
108  .commands = faux_command_handlers,
109  .flash_bank_command = faux_flash_bank_command,
110  .erase = faux_erase,
111  .write = faux_write,
112  .read = default_flash_read,
113  .probe = faux_probe,
114  .auto_probe = faux_probe,
115  .erase_check = default_flash_blank_check,
116  .info = faux_info,
117  .free_driver_priv = default_flash_free_driver_priv,
118 };
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:420
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ COMMAND_ANY
Definition: command.h:42
uint8_t bank
Definition: esirisc.c:135
static const int sector_size
Definition: faux.c:22
static int faux_probe(struct flash_bank *bank)
Definition: faux.c:90
static int faux_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: faux.c:84
static int faux_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: faux.c:77
const struct flash_driver faux_flash
Definition: faux.c:106
static const struct command_registration faux_command_handlers[]
Definition: faux.c:95
static int faux_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: faux.c:69
FLASH_BANK_COMMAND_HANDLER(faux_flash_bank_command)
Definition: faux.c:27
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
const struct command_registration hello_command_handlers[]
Export the registration for the hello command group, so it can be embedded in example drivers.
Definition: hello.c:86
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define ERROR_OK
Definition: log.h:164
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:235
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:241
uint8_t * memory
Definition: faux.c:18
struct target * target
Definition: faux.c:17
uint32_t start_address
Definition: faux.c:19
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
Definition: nor/driver.h:39
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Definition: nor/driver.h:44
Describes the geometry and status of a single flash sector within a flash bank.
Definition: nor/core.h:28
Definition: target.h:116
struct target * get_target(const char *id)
Definition: target.c:433
static struct ublast_lowlevel_priv info
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22