dummyflasher.c 14.1 KB
Newer Older
1 2 3
/*
 * This file is part of the flashrom project.
 *
4
 * Copyright (C) 2009,2010 Carl-Daniel Hailfinger
5 6 7
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; version 2 of the License.
9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <string.h>
#include <stdlib.h>
#include "flash.h"
23
#include "chipdrivers.h"
24
#include "programmer.h"
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/* Remove the #define below if you don't want SPI flash chip emulation. */
#define EMULATE_SPI_CHIP 1

#if EMULATE_SPI_CHIP
#define EMULATE_CHIP 1
#include "spi.h"
#endif

#if EMULATE_CHIP
#include <sys/types.h>
#include <sys/stat.h>
#endif

#if EMULATE_CHIP
static uint8_t *flashchip_contents = NULL;
enum emu_chip {
	EMULATE_NONE,
	EMULATE_ST_M25P10_RES,
	EMULATE_SST_SST25VF040_REMS,
	EMULATE_SST_SST25VF032B,
};
static enum emu_chip emu_chip = EMULATE_NONE;
static char *emu_persistent_image = NULL;
static int emu_chip_size = 0;
#if EMULATE_SPI_CHIP
static int emu_max_byteprogram_size = 0;
static int emu_max_aai_size = 0;
static int emu_jedec_se_size = 0;
static int emu_jedec_be_52_size = 0;
static int emu_jedec_be_d8_size = 0;
static int emu_jedec_ce_60_size = 0;
static int emu_jedec_ce_c7_size = 0;
#endif
#endif

static int spi_write_256_chunksize = 256;

63 64 65 66 67 68 69 70 71 72 73 74 75
static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
		      const unsigned char *writearr, unsigned char *readarr);
static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len);

static const struct spi_programmer spi_programmer_dummyflasher = {
	.type = SPI_CONTROLLER_DUMMY,
	.max_data_read = MAX_DATA_READ_UNLIMITED,
	.max_data_write = MAX_DATA_UNSPECIFIED,
	.command = dummy_spi_send_command,
	.multicommand = default_spi_send_multicommand,
	.read = default_spi_read,
	.write_256 = dummy_spi_write_256,
};
76 77
int dummy_init(void)
{
78
	char *bustext = NULL;
79 80 81 82
	char *tmp = NULL;
#if EMULATE_CHIP
	struct stat image_stat;
#endif
83

84
	msg_pspew("%s\n", __func__);
85

86
	bustext = extract_programmer_param("bus");
87 88 89
	msg_pdbg("Requested buses are: %s\n", bustext ? bustext : "default");
	if (!bustext)
		bustext = strdup("parallel+lpc+fwh+spi");
90
	/* Convert the parameters to lowercase. */
91
	tolower_string(bustext);
92 93

	buses_supported = CHIP_BUSTYPE_NONE;
94
	if (strstr(bustext, "parallel")) {
95
		buses_supported |= CHIP_BUSTYPE_PARALLEL;
96
		msg_pdbg("Enabling support for %s flash.\n", "parallel");
97
	}
98
	if (strstr(bustext, "lpc")) {
99
		buses_supported |= CHIP_BUSTYPE_LPC;
100
		msg_pdbg("Enabling support for %s flash.\n", "LPC");
101
	}
102
	if (strstr(bustext, "fwh")) {
103
		buses_supported |= CHIP_BUSTYPE_FWH;
104
		msg_pdbg("Enabling support for %s flash.\n", "FWH");
105
	}
106
	if (strstr(bustext, "spi")) {
107
		register_spi_programmer(&spi_programmer_dummyflasher);
108
		msg_pdbg("Enabling support for %s flash.\n", "SPI");
109 110
	}
	if (buses_supported == CHIP_BUSTYPE_NONE)
111
		msg_pdbg("Support for all flash bus types disabled.\n");
112
	free(bustext);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

	tmp = extract_programmer_param("spi_write_256_chunksize");
	if (tmp) {
		spi_write_256_chunksize = atoi(tmp);
		free(tmp);
		if (spi_write_256_chunksize < 1) {
			msg_perr("invalid spi_write_256_chunksize\n");
			return 1;
		}
	}

#if EMULATE_CHIP
	tmp = extract_programmer_param("emulate");
	if (!tmp) {
		msg_pdbg("Not emulating any flash chip.\n");
		/* Nothing else to do. */
		return 0;
	}
#if EMULATE_SPI_CHIP
	if (!strcmp(tmp, "M25P10.RES")) {
		emu_chip = EMULATE_ST_M25P10_RES;
		emu_chip_size = 128 * 1024;
		emu_max_byteprogram_size = 128;
		emu_max_aai_size = 0;
		emu_jedec_se_size = 0;
		emu_jedec_be_52_size = 0;
		emu_jedec_be_d8_size = 32 * 1024;
		emu_jedec_ce_60_size = 0;
		emu_jedec_ce_c7_size = emu_chip_size;
		msg_pdbg("Emulating ST M25P10.RES SPI flash chip (RES, page "
			 "write)\n");
	}
	if (!strcmp(tmp, "SST25VF040.REMS")) {
		emu_chip = EMULATE_SST_SST25VF040_REMS;
		emu_chip_size = 512 * 1024;
		emu_max_byteprogram_size = 1;
		emu_max_aai_size = 0;
		emu_jedec_se_size = 4 * 1024;
		emu_jedec_be_52_size = 32 * 1024;
		emu_jedec_be_d8_size = 0;
		emu_jedec_ce_60_size = emu_chip_size;
		emu_jedec_ce_c7_size = 0;
		msg_pdbg("Emulating SST SST25VF040.REMS SPI flash chip (REMS, "
			 "byte write)\n");
	}
	if (!strcmp(tmp, "SST25VF032B")) {
		emu_chip = EMULATE_SST_SST25VF032B;
		emu_chip_size = 4 * 1024 * 1024;
		emu_max_byteprogram_size = 1;
		emu_max_aai_size = 2;
		emu_jedec_se_size = 4 * 1024;
		emu_jedec_be_52_size = 32 * 1024;
		emu_jedec_be_d8_size = 64 * 1024;
		emu_jedec_ce_60_size = emu_chip_size;
		emu_jedec_ce_c7_size = emu_chip_size;
		msg_pdbg("Emulating SST SST25VF032B SPI flash chip (RDID, AAI "
			 "write)\n");
	}
#endif
	if (emu_chip == EMULATE_NONE) {
		msg_perr("Invalid chip specified for emulation: %s\n", tmp);
		free(tmp);
		return 1;
	}
	free(tmp);
	flashchip_contents = malloc(emu_chip_size);
	if (!flashchip_contents) {
		msg_perr("Out of memory!\n");
		return 1;
	}
	msg_pdbg("Filling fake flash chip with 0xff, size %i\n", emu_chip_size);
	memset(flashchip_contents, 0xff, emu_chip_size);

	emu_persistent_image = extract_programmer_param("image");
	if (!emu_persistent_image) {
		/* Nothing else to do. */
		return 0;
	}
	if (!stat(emu_persistent_image, &image_stat)) {
		msg_pdbg("Found persistent image %s, size %li ",
			 emu_persistent_image, (long)image_stat.st_size);
		if (image_stat.st_size == emu_chip_size) {
			msg_pdbg("matches.\n");
			msg_pdbg("Reading %s\n", emu_persistent_image);
			read_buf_from_file(flashchip_contents, emu_chip_size,
					   emu_persistent_image);
		} else {
			msg_pdbg("doesn't match.\n");
		}
	}
#endif
204
	return 0;
205 206 207 208
}

int dummy_shutdown(void)
{
209
	msg_pspew("%s\n", __func__);
210 211 212 213 214 215 216 217 218 219
#if EMULATE_CHIP
	if (emu_chip != EMULATE_NONE) {
		if (emu_persistent_image) {
			msg_pdbg("Writing %s\n", emu_persistent_image);
			write_buf_to_file(flashchip_contents, emu_chip_size,
					  emu_persistent_image);
		}
		free(flashchip_contents);
	}
#endif
220 221 222
	return 0;
}

223 224
void *dummy_map(const char *descr, unsigned long phys_addr, size_t len)
{
225 226
	msg_pspew("%s: Mapping %s, 0x%lx bytes at 0x%08lx\n",
		  __func__, descr, (unsigned long)len, phys_addr);
227 228 229 230 231
	return (void *)phys_addr;
}

void dummy_unmap(void *virt_addr, size_t len)
{
232 233
	msg_pspew("%s: Unmapping 0x%lx bytes at %p\n",
		  __func__, (unsigned long)len, virt_addr);
234 235
}

236
void dummy_chip_writeb(uint8_t val, chipaddr addr)
237
{
238
	msg_pspew("%s: addr=0x%lx, val=0x%02x\n", __func__, addr, val);
239 240
}

241
void dummy_chip_writew(uint16_t val, chipaddr addr)
242
{
243
	msg_pspew("%s: addr=0x%lx, val=0x%04x\n", __func__, addr, val);
244 245
}

246
void dummy_chip_writel(uint32_t val, chipaddr addr)
247
{
248
	msg_pspew("%s: addr=0x%lx, val=0x%08x\n", __func__, addr, val);
249 250
}

251 252 253
void dummy_chip_writen(uint8_t *buf, chipaddr addr, size_t len)
{
	size_t i;
254 255
	msg_pspew("%s: addr=0x%lx, len=0x%08lx, writing data (hex):",
		  __func__, addr, (unsigned long)len);
256 257
	for (i = 0; i < len; i++) {
		if ((i % 16) == 0)
258 259
			msg_pspew("\n");
		msg_pspew("%02x ", buf[i]);
260 261 262
	}
}

263
uint8_t dummy_chip_readb(const chipaddr addr)
264
{
265
	msg_pspew("%s:  addr=0x%lx, returning 0xff\n", __func__, addr);
266 267 268
	return 0xff;
}

269
uint16_t dummy_chip_readw(const chipaddr addr)
270
{
271
	msg_pspew("%s:  addr=0x%lx, returning 0xffff\n", __func__, addr);
272 273 274
	return 0xffff;
}

275
uint32_t dummy_chip_readl(const chipaddr addr)
276
{
277
	msg_pspew("%s:  addr=0x%lx, returning 0xffffffff\n", __func__, addr);
278 279 280
	return 0xffffffff;
}

281 282
void dummy_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
{
283 284
	msg_pspew("%s:  addr=0x%lx, len=0x%lx, returning array of 0xff\n",
		  __func__, addr, (unsigned long)len);
285 286 287 288
	memset(buf, 0xff, len);
	return;
}

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
#if EMULATE_SPI_CHIP
static int emulate_spi_chip_response(unsigned int writecnt, unsigned int readcnt,
		      const unsigned char *writearr, unsigned char *readarr)
{
	int offs;
	static int aai_offs;
	static int aai_active = 0;

	if (writecnt == 0) {
		msg_perr("No command sent to the chip!\n");
		return 1;
	}
	/* TODO: Implement command blacklists here. */
	switch (writearr[0]) {
	case JEDEC_RES:
		if (emu_chip != EMULATE_ST_M25P10_RES)
			break;
		/* Respond with ST_M25P10_RES. */
		if (readcnt > 0)
			readarr[0] = 0x10;
		break;
	case JEDEC_REMS:
		if (emu_chip != EMULATE_SST_SST25VF040_REMS)
			break;
		/* Respond with SST_SST25VF040_REMS. */
		if (readcnt > 0)
			readarr[0] = 0xbf;
		if (readcnt > 1)
			readarr[1] = 0x44;
		break;
	case JEDEC_RDID:
		if (emu_chip != EMULATE_SST_SST25VF032B)
			break;
		/* Respond with SST_SST25VF032B. */
		if (readcnt > 0)
			readarr[0] = 0xbf;
		if (readcnt > 1)
			readarr[1] = 0x25;
		if (readcnt > 2)
			readarr[2] = 0x4a;
		break;
	case JEDEC_RDSR:
		memset(readarr, 0, readcnt);
		if (aai_active)
			memset(readarr, 1 << 6, readcnt);
		break;
	case JEDEC_READ:
		offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
		/* Truncate to emu_chip_size. */
		offs %= emu_chip_size;
		if (readcnt > 0)
			memcpy(readarr, flashchip_contents + offs, readcnt);
		break;
	case JEDEC_BYTE_PROGRAM:
		offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
		/* Truncate to emu_chip_size. */
		offs %= emu_chip_size;
		if (writecnt < 5) {
			msg_perr("BYTE PROGRAM size too short!\n");
			return 1;
		}
		if (writecnt - 4 > emu_max_byteprogram_size) {
			msg_perr("Max BYTE PROGRAM size exceeded!\n");
			return 1;
		}
		memcpy(flashchip_contents + offs, writearr + 4, writecnt - 4);
		break;
	case JEDEC_AAI_WORD_PROGRAM:
		if (!emu_max_aai_size)
			break;
		if (!aai_active) {
			if (writecnt < JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
				msg_perr("Initial AAI WORD PROGRAM size too "
					 "short!\n");
				return 1;
			}
			if (writecnt > JEDEC_AAI_WORD_PROGRAM_OUTSIZE) {
				msg_perr("Initial AAI WORD PROGRAM size too "
					 "long!\n");
				return 1;
			}
			aai_active = 1;
			aai_offs = writearr[1] << 16 | writearr[2] << 8 |
				   writearr[3];
			/* Truncate to emu_chip_size. */
			aai_offs %= emu_chip_size;
			memcpy(flashchip_contents + aai_offs, writearr + 4, 2);
			aai_offs += 2;
		} else {
			if (writecnt < JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
				msg_perr("Continuation AAI WORD PROGRAM size "
					 "too short!\n");
				return 1;
			}
			if (writecnt > JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE) {
				msg_perr("Continuation AAI WORD PROGRAM size "
					 "too long!\n");
				return 1;
			}
			memcpy(flashchip_contents + aai_offs, writearr + 1, 2);
			aai_offs += 2;
		}
		break;
	case JEDEC_WRDI:
		if (!emu_max_aai_size)
			break;
		aai_active = 0;
		break;
	case JEDEC_SE:
		if (!emu_jedec_se_size)
			break;
		if (writecnt != JEDEC_SE_OUTSIZE) {
			msg_perr("SECTOR ERASE 0x20 outsize invalid!\n");
			return 1;
		}
		if (readcnt != JEDEC_SE_INSIZE) {
			msg_perr("SECTOR ERASE 0x20 insize invalid!\n");
			return 1;
		}
		offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
		if (offs & (emu_jedec_se_size - 1))
410
			msg_pdbg("Unaligned SECTOR ERASE 0x20: 0x%x\n", offs);
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
		offs &= ~(emu_jedec_se_size - 1);
		memset(flashchip_contents + offs, 0xff, emu_jedec_se_size);
		break;
	case JEDEC_BE_52:
		if (!emu_jedec_be_52_size)
			break;
		if (writecnt != JEDEC_BE_52_OUTSIZE) {
			msg_perr("BLOCK ERASE 0x52 outsize invalid!\n");
			return 1;
		}
		if (readcnt != JEDEC_BE_52_INSIZE) {
			msg_perr("BLOCK ERASE 0x52 insize invalid!\n");
			return 1;
		}
		offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
		if (offs & (emu_jedec_be_52_size - 1))
427
			msg_pdbg("Unaligned BLOCK ERASE 0x52: 0x%x\n", offs);
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
		offs &= ~(emu_jedec_be_52_size - 1);
		memset(flashchip_contents + offs, 0xff, emu_jedec_be_52_size);
		break;
	case JEDEC_BE_D8:
		if (!emu_jedec_be_d8_size)
			break;
		if (writecnt != JEDEC_BE_D8_OUTSIZE) {
			msg_perr("BLOCK ERASE 0xd8 outsize invalid!\n");
			return 1;
		}
		if (readcnt != JEDEC_BE_D8_INSIZE) {
			msg_perr("BLOCK ERASE 0xd8 insize invalid!\n");
			return 1;
		}
		offs = writearr[1] << 16 | writearr[2] << 8 | writearr[3];
		if (offs & (emu_jedec_be_d8_size - 1))
444
			msg_pdbg("Unaligned BLOCK ERASE 0xd8: 0x%x\n", offs);
445 446 447 448 449 450 451 452 453 454 455 456 457 458
		offs &= ~(emu_jedec_be_d8_size - 1);
		memset(flashchip_contents + offs, 0xff, emu_jedec_be_d8_size);
		break;
	case JEDEC_CE_60:
		if (!emu_jedec_ce_60_size)
			break;
		if (writecnt != JEDEC_CE_60_OUTSIZE) {
			msg_perr("CHIP ERASE 0x60 outsize invalid!\n");
			return 1;
		}
		if (readcnt != JEDEC_CE_60_INSIZE) {
			msg_perr("CHIP ERASE 0x60 insize invalid!\n");
			return 1;
		}
459
		/* JEDEC_CE_60_OUTSIZE is 1 (no address) -> no offset. */
460
		/* emu_jedec_ce_60_size is emu_chip_size. */
461
		memset(flashchip_contents, 0xff, emu_jedec_ce_60_size);
462 463 464 465 466 467 468 469 470 471 472 473
		break;
	case JEDEC_CE_C7:
		if (!emu_jedec_ce_c7_size)
			break;
		if (writecnt != JEDEC_CE_C7_OUTSIZE) {
			msg_perr("CHIP ERASE 0xc7 outsize invalid!\n");
			return 1;
		}
		if (readcnt != JEDEC_CE_C7_INSIZE) {
			msg_perr("CHIP ERASE 0xc7 insize invalid!\n");
			return 1;
		}
474
		/* JEDEC_CE_C7_OUTSIZE is 1 (no address) -> no offset. */
475 476 477 478 479 480 481 482 483 484 485
		/* emu_jedec_ce_c7_size is emu_chip_size. */
		memset(flashchip_contents, 0xff, emu_jedec_ce_c7_size);
		break;
	default:
		/* No special response. */
		break;
	}
	return 0;
}
#endif

486
static int dummy_spi_send_command(unsigned int writecnt, unsigned int readcnt,
487 488 489 490
		      const unsigned char *writearr, unsigned char *readarr)
{
	int i;

491
	msg_pspew("%s:", __func__);
492

493
	msg_pspew(" writing %u bytes:", writecnt);
494
	for (i = 0; i < writecnt; i++)
495
		msg_pspew(" 0x%02x", writearr[i]);
496

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	/* Response for unknown commands and missing chip is 0xff. */
	memset(readarr, 0xff, readcnt);
#if EMULATE_SPI_CHIP
	switch (emu_chip) {
	case EMULATE_ST_M25P10_RES:
	case EMULATE_SST_SST25VF040_REMS:
	case EMULATE_SST_SST25VF032B:
		if (emulate_spi_chip_response(writecnt, readcnt, writearr,
					      readarr)) {
			msg_perr("Invalid command sent to flash chip!\n");
			return 1;
		}
		break;
	default:
		break;
	}
#endif
514
	msg_pspew(" reading %u bytes:", readcnt);
515
	for (i = 0; i < readcnt; i++) {
516
		msg_pspew(" 0x%02x", readarr[i]);
517
	}
518
	msg_pspew("\n");
519 520
	return 0;
}
521

522
static int dummy_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
523
{
524 525
	return spi_write_chunked(flash, buf, start, len,
				 spi_write_256_chunksize);
526
}