2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
22 #define YNORSIM_FNAME "emfile-nor"
24 /* Set YNORSIM_BIT_CHANGES to a a value from 1..30 to
25 *simulate bit flipping as the programming happens.
26 * A low value results in faster simulation with less chance of encountering a partially programmed
30 //#define YNORSIM_BIT_CHANGES 15
31 #define YNORSIM_BIT_CHANGES 2
34 /* Simulate 32MB of flash in 256k byte blocks.
38 #define YNORSIM_BLOCK_SIZE_U32 (256*1024/4)
39 #define YNORSIM_DEV_SIZE_U32 (32*1024 * 1024/4)
41 /* Simulate 8MB of flash in 256k byte blocks.
45 #define YNORSIM_BLOCK_SIZE_U32 (256*1024/4)
46 #define YNORSIM_DEV_SIZE_U32 (8*1024 * 1024/4)
60 int ops_multiplier = 500;
61 extern int random_seed;
62 extern int simulate_power_failure;
64 static void NorError(struct nor_sim *sim)
66 printf("Nor error on device %s\n", sim->fname);
71 static void ynorsim_save_image(struct nor_sim *sim)
75 h = open(sim->fname, O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
76 write(h, sim->word, sim->file_size);
80 static void ynorsim_restore_image(struct nor_sim *sim)
84 h = open(sim->fname, O_RDONLY, S_IREAD | S_IWRITE);
85 memset(sim->word, 0xFF, sim->file_size);
86 read(h, sim->word, sim->file_size);
90 static void ynorsim_power_fail(struct nor_sim *sim)
92 ynorsim_save_image(sim);
96 static void ynorsim_maybe_power_fail(struct nor_sim *sim)
100 if (simulate_power_failure && sim->remaining_ops < 1) {
101 printf("Simulated power failure after %d operations\n",
103 ynorsim_power_fail(sim);
107 static void ynorsim_ready(struct nor_sim *sim)
109 if (sim->initialised)
112 sim->remaining_ops = 1000000000;
114 (rand() % 10000) * ops_multiplier * YNORSIM_BIT_CHANGES;
115 ynorsim_restore_image(sim);
116 sim->initialised = 1;
119 /* Public functions. */
121 void ynorsim_rd32(struct nor_sim *sim, u32 * addr, u32 * buf, int nwords)
132 void ynorsim_wr_one_word32(struct nor_sim *sim, u32 * addr, u32 val)
140 /* Fail due to trying to change a zero into a 1 */
141 printf("attempt to set a zero to one (%x)->(%x)\n", tmp, val);
145 for (i = 0; i < YNORSIM_BIT_CHANGES; i++) {
146 m = 1 << (rand() & 31);
150 ynorsim_maybe_power_fail(sim);
156 ynorsim_maybe_power_fail(sim);
159 void ynorsim_wr32(struct nor_sim *sim, u32 * addr, u32 * buf, int nwords)
162 ynorsim_wr_one_word32(sim, addr, *buf);
169 void ynorsim_erase(struct nor_sim *sim, u32 * addr)
171 /* Todo... bit flipping */
172 memset(addr, 0xFF, sim->block_size_bytes);
175 struct nor_sim *ynorsim_initialise(char *name, int n_blocks,
176 int block_size_bytes)
180 sim = malloc(sizeof(*sim));
184 memset(sim, 0, sizeof(*sim));
185 sim->n_blocks = n_blocks;
186 sim->block_size_bytes = block_size_bytes;
187 sim->file_size = n_blocks * block_size_bytes;
188 sim->word = malloc(sim->file_size);
189 sim->fname = strdup(name);
198 void ynorsim_shutdown(struct nor_sim *sim)
200 ynorsim_save_image(sim);
201 sim->initialised = 0;
204 u32 *ynorsim_get_base(struct nor_sim *sim)