]> git.sur5r.net Git - openocd/blob - src/flash/nor/faux.c
flash/kinetis: Fix bug in odd byte count padding
[openocd] / src / flash / nor / 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  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
19  ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "imp.h"
26 #include <target/image.h>
27 #include "hello.h"
28
29 struct faux_flash_bank {
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 (CMD_ARGC < 6)
45                 return ERROR_COMMAND_SYNTAX_ERROR;
46
47         info = malloc(sizeof(struct faux_flash_bank));
48         if (info == NULL) {
49                 LOG_ERROR("no memory for flash bank info");
50                 return ERROR_FAIL;
51         }
52         info->memory = malloc(bank->size);
53         if (info == NULL) {
54                 free(info);
55                 LOG_ERROR("no memory for flash bank info");
56                 return ERROR_FAIL;
57         }
58         bank->driver_priv = info;
59
60         /* Use 0x10000 as a fixed sector size. */
61         int i = 0;
62         uint32_t offset = 0;
63         bank->num_sectors = bank->size/sectorSize;
64         bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
65         for (i = 0; i < bank->num_sectors; i++) {
66                 bank->sectors[i].offset = offset;
67                 bank->sectors[i].size = sectorSize;
68                 offset += bank->sectors[i].size;
69                 bank->sectors[i].is_erased = -1;
70                 bank->sectors[i].is_protected = 0;
71         }
72
73         info->target = get_target(CMD_ARGV[5]);
74         if (info->target == NULL) {
75                 LOG_ERROR("target '%s' not defined", CMD_ARGV[5]);
76                 free(info->memory);
77                 free(info);
78                 return ERROR_FAIL;
79         }
80         return ERROR_OK;
81 }
82
83 static int faux_erase(struct flash_bank *bank, int first, int last)
84 {
85         struct faux_flash_bank *info = bank->driver_priv;
86         memset(info->memory + first*sectorSize, 0xff, sectorSize*(last-first + 1));
87         return ERROR_OK;
88 }
89
90 static int faux_protect(struct flash_bank *bank, int set, int first, int last)
91 {
92         LOG_USER("set protection sector %d to %d to %s", first, last, set ? "on" : "off");
93         return ERROR_OK;
94 }
95
96 static int faux_write(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
97 {
98         struct faux_flash_bank *info = bank->driver_priv;
99         memcpy(info->memory + offset, buffer, count);
100         return ERROR_OK;
101 }
102
103 static int faux_protect_check(struct flash_bank *bank)
104 {
105         return ERROR_OK;
106 }
107
108 static int faux_info(struct flash_bank *bank, char *buf, int buf_size)
109 {
110         snprintf(buf, buf_size, "faux flash driver");
111         return ERROR_OK;
112 }
113
114 static int faux_probe(struct flash_bank *bank)
115 {
116         return ERROR_OK;
117 }
118
119 static const struct command_registration faux_command_handlers[] = {
120         {
121                 .name = "faux",
122                 .mode = COMMAND_ANY,
123                 .help = "faux flash command group",
124                 .chain = hello_command_handlers,
125         },
126         COMMAND_REGISTRATION_DONE
127 };
128
129 struct flash_driver faux_flash = {
130         .name = "faux",
131         .commands = faux_command_handlers,
132         .flash_bank_command = faux_flash_bank_command,
133         .erase = faux_erase,
134         .protect = faux_protect,
135         .write = faux_write,
136         .read = default_flash_read,
137         .probe = faux_probe,
138         .auto_probe = faux_probe,
139         .erase_check = default_flash_blank_check,
140         .protect_check = faux_protect_check,
141         .info = faux_info
142 };