flash_bank_t -> struct flash_bank
[fw/openocd] / src / flash / faux.c
1 /***************************************************************************
2  *   Copyright (C) 2009 Ã˜yvind Harboe                                      *
3  *   oyvind.harboe@zylin.com                                               *
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, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "flash.h"
25 #include "image.h"
26
27
28 struct faux_flash_bank
29 {
30         struct target *target;
31         uint8_t *memory;
32         uint32_t start_address;
33 };
34
35 static const int sectorSize = 0x10000;
36
37
38 /* flash bank faux <base> <size> <chip_width> <bus_width> <target#> <driverPath>
39  */
40 FLASH_BANK_COMMAND_HANDLER(faux_flash_bank_command)
41 {
42         struct faux_flash_bank *info;
43
44         if (argc < 6)
45         {
46                 LOG_WARNING("incomplete flash_bank faux configuration");
47                 return ERROR_FLASH_BANK_INVALID;
48         }
49
50         info = malloc(sizeof(struct faux_flash_bank));
51         if (info == NULL)
52         {
53                 LOG_ERROR("no memory for flash bank info");
54                 return ERROR_FAIL;
55         }
56         info->memory = malloc(bank->size);
57         if (info == NULL)
58         {
59                 free(info);
60                 LOG_ERROR("no memory for flash bank info");
61                 return ERROR_FAIL;
62         }
63         bank->driver_priv = info;
64
65         /* Use 0x10000 as a fixed sector size. */
66         int i = 0;
67         uint32_t offset = 0;
68         bank->num_sectors = bank->size/sectorSize;
69         bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
70         for (i = 0; i < bank->num_sectors; i++)
71         {
72                 bank->sectors[i].offset = offset;
73                 bank->sectors[i].size = sectorSize;
74                 offset += bank->sectors[i].size;
75                 bank->sectors[i].is_erased = -1;
76                 bank->sectors[i].is_protected = 0;
77         }
78
79         info->target = get_target(args[5]);
80         if (info->target == NULL)
81         {
82                 LOG_ERROR("target '%s' not defined", args[5]);
83                 free(info->memory);
84                 free(info);
85                 return ERROR_FAIL;
86         }
87         return ERROR_OK;
88 }
89
90 static int faux_register_commands(struct command_context_s *cmd_ctx)
91 {
92         return ERROR_OK;
93 }
94
95 static int faux_erase(struct flash_bank *bank, int first, int last)
96 {
97         struct faux_flash_bank *info = bank->driver_priv;
98         memset(info->memory + first*sectorSize, 0xff, sectorSize*(last-first + 1));
99         return ERROR_OK;
100 }
101
102 static int faux_protect(struct flash_bank *bank, int set, int first, int last)
103 {
104         LOG_USER("set protection sector %d to %d to %s", first, last, set?"on":"off");
105         return ERROR_OK;
106 }
107
108 static int faux_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
109 {
110         struct faux_flash_bank *info = bank->driver_priv;
111         memcpy(info->memory + offset, buffer, count);
112         return ERROR_OK;
113 }
114
115 static int faux_protect_check(struct flash_bank *bank)
116 {
117         return ERROR_OK;
118 }
119
120 static int faux_info(struct flash_bank *bank, char *buf, int buf_size)
121 {
122         snprintf(buf, buf_size, "faux flash driver");
123         return ERROR_OK;
124 }
125
126 static int faux_probe(struct flash_bank *bank)
127 {
128         return ERROR_OK;
129 }
130
131 struct flash_driver faux_flash = {
132                 .name = "faux",
133                 .register_commands = &faux_register_commands,
134                 .flash_bank_command = &faux_flash_bank_command,
135                 .erase = &faux_erase,
136                 .protect = &faux_protect,
137                 .write = &faux_write,
138                 .probe = &faux_probe,
139                 .auto_probe = &faux_probe,
140                 .erase_check = &default_flash_blank_check,
141                 .protect_check = &faux_protect_check,
142                 .info = &faux_info
143         };