e76dc493fe085d50cc7eb7e9928f9beaea5699ff
[fw/openocd] / src / flash / nor / faux.c
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
16 struct faux_flash_bank {
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)
32                 return ERROR_COMMAND_SYNTAX_ERROR;
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         },
103         COMMAND_REGISTRATION_DONE
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 };