1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2009 Øyvind Harboe *
5 * oyvind.harboe@zylin.com *
6 ***************************************************************************/
13 #include <target/image.h>
16 struct faux_flash_bank
{
17 struct target
*target
;
19 uint32_t start_address
;
22 static const int sector_size
= 0x10000;
25 /* flash bank faux <base> <size> <chip_width> <bus_width> <target#> <driverPath>
27 FLASH_BANK_COMMAND_HANDLER(faux_flash_bank_command
)
29 struct faux_flash_bank
*info
;
32 return ERROR_COMMAND_SYNTAX_ERROR
;
34 info
= malloc(sizeof(struct faux_flash_bank
));
36 LOG_ERROR("no memory for flash bank info");
39 info
->memory
= malloc(bank
->size
);
42 LOG_ERROR("no memory for flash bank info");
45 bank
->driver_priv
= info
;
47 /* Use 0x10000 as a fixed sector size. */
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;
59 info
->target
= get_target(CMD_ARGV
[5]);
61 LOG_ERROR("target '%s' not defined", CMD_ARGV
[5]);
69 static int faux_erase(struct flash_bank
*bank
, unsigned int first
,
72 struct faux_flash_bank
*info
= bank
->driver_priv
;
73 memset(info
->memory
+ first
*sector_size
, 0xff, sector_size
*(last
-first
+ 1));
77 static int faux_write(struct flash_bank
*bank
, const uint8_t *buffer
, uint32_t offset
, uint32_t count
)
79 struct faux_flash_bank
*info
= bank
->driver_priv
;
80 memcpy(info
->memory
+ offset
, buffer
, count
);
84 static int faux_info(struct flash_bank
*bank
, struct command_invocation
*cmd
)
86 command_print_sameline(cmd
, "faux flash driver");
90 static int faux_probe(struct flash_bank
*bank
)
95 static const struct command_registration faux_command_handlers
[] = {
99 .help
= "faux flash command group",
100 .chain
= hello_command_handlers
,
103 COMMAND_REGISTRATION_DONE
106 const struct flash_driver faux_flash
= {
108 .commands
= faux_command_handlers
,
109 .flash_bank_command
= faux_flash_bank_command
,
112 .read
= default_flash_read
,
114 .auto_probe
= faux_probe
,
115 .erase_check
= default_flash_blank_check
,
117 .free_driver_priv
= default_flash_free_driver_priv
,