lpc2900.c 51.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/***************************************************************************
 *   Copyright (C) 2009 by                                                 *
 *   Rolf Meeser <rolfm_9dq@yahoo.de>                                      *
 *                                                                         *
 *   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  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif


Zachary T Welch's avatar
Zachary T Welch committed
26
#include "imp.h"
27
#include <helper/binarybuffer.h>
28
#include <target/algorithm.h>
29
#include <target/arm.h>
Zachary T Welch's avatar
Zachary T Welch committed
30
#include <target/image.h>
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131


/* 1024 bytes */
#define KiB                 1024

/* Some flash constants */
#define FLASH_PAGE_SIZE     512     /* bytes */
#define FLASH_ERASE_TIME    100000  /* microseconds */
#define FLASH_PROGRAM_TIME  1000    /* microseconds */

/* Chip ID / Feature Registers */
#define CHIPID          0xE0000000  /* Chip ID */
#define FEAT0           0xE0000100  /* Chip feature 0 */
#define FEAT1           0xE0000104  /* Chip feature 1 */
#define FEAT2           0xE0000108  /* Chip feature 2 (contains flash size indicator) */
#define FEAT3           0xE000010C  /* Chip feature 3 */

#define EXPECTED_CHIPID 0x209CE02B  /* Chip ID of all LPC2900 devices */

/* Flash/EEPROM Control Registers */
#define FCTR            0x20200000  /* Flash control */
#define FPTR            0x20200008  /* Flash program-time */
#define FTCTR           0x2020000C  /* Flash test control */
#define FBWST           0x20200010  /* Flash bridge wait-state */
#define FCRA            0x2020001C  /* Flash clock divider */
#define FMSSTART        0x20200020  /* Flash Built-In Selft Test start address */
#define FMSSTOP         0x20200024  /* Flash Built-In Selft Test stop address */
#define FMS16           0x20200028  /* Flash 16-bit signature */
#define FMSW0           0x2020002C  /* Flash 128-bit signature Word 0 */
#define FMSW1           0x20200030  /* Flash 128-bit signature Word 1 */
#define FMSW2           0x20200034  /* Flash 128-bit signature Word 2 */
#define FMSW3           0x20200038  /* Flash 128-bit signature Word 3 */

#define EECMD           0x20200080  /* EEPROM command */
#define EEADDR          0x20200084  /* EEPROM address */
#define EEWDATA         0x20200088  /* EEPROM write data */
#define EERDATA         0x2020008C  /* EEPROM read data */
#define EEWSTATE        0x20200090  /* EEPROM wait state */
#define EECLKDIV        0x20200094  /* EEPROM clock divider */
#define EEPWRDWN        0x20200098  /* EEPROM power-down/start */
#define EEMSSTART       0x2020009C  /* EEPROM BIST start address */
#define EEMSSTOP        0x202000A0  /* EEPROM BIST stop address */
#define EEMSSIG         0x202000A4  /* EEPROM 24-bit BIST signature */

#define INT_CLR_ENABLE  0x20200FD8  /* Flash/EEPROM interrupt clear enable */
#define INT_SET_ENABLE  0x20200FDC  /* Flash/EEPROM interrupt set enable */
#define INT_STATUS      0x20200FE0  /* Flash/EEPROM interrupt status */
#define INT_ENABLE      0x20200FE4  /* Flash/EEPROM interrupt enable */
#define INT_CLR_STATUS  0x20200FE8  /* Flash/EEPROM interrupt clear status */
#define INT_SET_STATUS  0x20200FEC  /* Flash/EEPROM interrupt set status */

/* Interrupt sources */
#define INTSRC_END_OF_PROG    (1 << 28)
#define INTSRC_END_OF_BIST    (1 << 27)
#define INTSRC_END_OF_RDWR    (1 << 26)
#define INTSRC_END_OF_MISR    (1 << 2)
#define INTSRC_END_OF_BURN    (1 << 1)
#define INTSRC_END_OF_ERASE   (1 << 0)


/* FCTR bits */
#define FCTR_FS_LOADREQ       (1 << 15)
#define FCTR_FS_CACHECLR      (1 << 14)
#define FCTR_FS_CACHEBYP      (1 << 13)
#define FCTR_FS_PROGREQ       (1 << 12)
#define FCTR_FS_RLS           (1 << 11)
#define FCTR_FS_PDL           (1 << 10)
#define FCTR_FS_PD            (1 << 9)
#define FCTR_FS_WPB           (1 << 7)
#define FCTR_FS_ISS           (1 << 6)
#define FCTR_FS_RLD           (1 << 5)
#define FCTR_FS_DCR           (1 << 4)
#define FCTR_FS_WEB           (1 << 2)
#define FCTR_FS_WRE           (1 << 1)
#define FCTR_FS_CS            (1 << 0)
/* FPTR bits */
#define FPTR_EN_T             (1 << 15)
/* FTCTR bits */
#define FTCTR_FS_BYPASS_R     (1 << 29)
#define FTCTR_FS_BYPASS_W     (1 << 28)
/* FMSSTOP bits */
#define FMSSTOP_MISR_START    (1 << 17)
/* EEMSSTOP bits */
#define EEMSSTOP_STRTBIST     (1 << 31)

/* Index sector */
#define ISS_CUSTOMER_START1   (0x830)
#define ISS_CUSTOMER_END1     (0xA00)
#define ISS_CUSTOMER_SIZE1    (ISS_CUSTOMER_END1 - ISS_CUSTOMER_START1)
#define ISS_CUSTOMER_NWORDS1  (ISS_CUSTOMER_SIZE1 / 4)
#define ISS_CUSTOMER_START2   (0xA40)
#define ISS_CUSTOMER_END2     (0xC00)
#define ISS_CUSTOMER_SIZE2    (ISS_CUSTOMER_END2 - ISS_CUSTOMER_START2)
#define ISS_CUSTOMER_NWORDS2  (ISS_CUSTOMER_SIZE2 / 4)
#define ISS_CUSTOMER_SIZE     (ISS_CUSTOMER_SIZE1 + ISS_CUSTOMER_SIZE2)



/**
 * Private data for \c lpc2900 flash driver.
 */
132
struct lpc2900_flash_bank
133
{
134
135
136
137
138
	/**
	 * This flag is set when the device has been successfully probed.
	 */
	bool is_probed;

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
	/**
	 * Holds the value read from CHIPID register.
	 * The driver will not load if the chipid doesn't match the expected
	 * value of 0x209CE02B of the LPC2900 family. A probe will only be done
	 * if the chipid does not yet contain the expected value.
	 */
	uint32_t chipid;

	/**
	 * String holding device name.
	 * This string is set by the probe function to the type number of the
	 * device. It takes the form "LPC29xx".
	 */
	char * target_name;

	/**
	 * System clock frequency.
	 * Holds the clock frequency in Hz, as passed by the configuration file
	 * to the <tt>flash bank</tt> command.
	 */
	uint32_t clk_sys_fmc;

	/**
	 * Flag to indicate that dangerous operations are possible.
	 * This flag can be set by passing the correct password to the
	 * <tt>lpc2900 password</tt> command. If set, other dangerous commands,
	 * which operate on the index sector, can be executed.
	 */
	uint32_t risky;

	/**
	 * Maximum contiguous block of internal SRAM (bytes).
	 * Autodetected by the driver. Not the total amount of SRAM, only the
	 * the largest \em contiguous block!
	 */
	uint32_t max_ram_block;

176
};
177
178


179
180
181
182
183
static uint32_t lpc2900_wait_status(struct flash_bank *bank, uint32_t mask, int timeout);
static void lpc2900_setup(struct flash_bank *bank);
static uint32_t lpc2900_is_ready(struct flash_bank *bank);
static uint32_t lpc2900_read_security_status(struct flash_bank *bank);
static uint32_t lpc2900_run_bist128(struct flash_bank *bank,
184
185
                                    uint32_t addr_from, uint32_t addr_to,
                                    uint32_t (*signature)[4] );
186
static uint32_t lpc2900_address2sector(struct flash_bank *bank, uint32_t offset);
187
static uint32_t lpc2900_calc_tr(uint32_t clock_var, uint32_t time_var);
188
189
190
191
192
193
194
195
196
197
198
199
200
201


/***********************  Helper functions  **************************/


/**
 * Wait for an event in mask to occur in INT_STATUS.
 *
 * Return when an event occurs, or after a timeout.
 *
 * @param[in] bank Pointer to the flash bank descriptor
 * @param[in] mask Mask to be used for INT_STATUS
 * @param[in] timeout Timeout in ms
 */
202
static uint32_t lpc2900_wait_status( struct flash_bank *bank,
203
204
205
206
                                     uint32_t mask,
                                     int timeout )
{
	uint32_t int_status;
Zachary T Welch's avatar
Zachary T Welch committed
207
	struct target *target = bank->target;
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235


	do
	{
		alive_sleep(1);
		timeout--;
		target_read_u32(target, INT_STATUS, &int_status);
	}
	while( ((int_status & mask) == 0) && (timeout != 0) );

	if (timeout == 0)
	{
		LOG_DEBUG("Timeout!");
		return ERROR_FLASH_OPERATION_FAILED;
	}

	return ERROR_OK;
}



/**
 * Set up the flash for erase/program operations.
 *
 * Enable the flash, and set the correct CRA clock of 66 kHz.
 *
 * @param bank Pointer to the flash bank descriptor
 */
236
static void lpc2900_setup( struct flash_bank *bank )
237
238
{
	uint32_t fcra;
239
	struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258


	/* Power up the flash block */
	target_write_u32( bank->target, FCTR, FCTR_FS_WEB | FCTR_FS_CS );


	fcra = (lpc2900_info->clk_sys_fmc / (3 * 66000)) - 1;
	target_write_u32( bank->target, FCRA, fcra );
}



/**
 * Check if device is ready.
 *
 * Check if device is ready for flash operation:
 * Must have been successfully probed.
 * Must be halted.
 */
259
static uint32_t lpc2900_is_ready( struct flash_bank *bank )
260
{
261
	struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
262

263
	if( !lpc2900_info->is_probed )
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
	{
		return ERROR_FLASH_BANK_NOT_PROBED;
	}

	if( bank->target->state != TARGET_HALTED )
	{
		LOG_ERROR( "Target not halted" );
		return ERROR_TARGET_NOT_HALTED;
	}

	return ERROR_OK;
}


/**
 * Read the status of sector security from the index sector.
 *
 * @param bank Pointer to the flash bank descriptor
 */
283
static uint32_t lpc2900_read_security_status( struct flash_bank *bank )
284
285
286
287
288
289
290
{
	uint32_t status;
	if( (status = lpc2900_is_ready( bank )) != ERROR_OK )
	{
		return status;
	}

Zachary T Welch's avatar
Zachary T Welch committed
291
	struct target *target = bank->target;
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313

	/* Enable ISS access */
	target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB | FCTR_FS_ISS);

	/* Read the relevant block of memory from the ISS sector */
	uint32_t iss_secured_field[ 0x230/16 ][ 4 ];
	target_read_memory(target, bank->base + 0xC00, 4, 0x230/4,
	                           (uint8_t *)iss_secured_field);

	/* Disable ISS access */
	target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);

	/* Check status of each sector. Note that the sector numbering in the LPC2900
	 * is different from the logical sector numbers used in OpenOCD!
	 * Refer to the user manual for details.
	 *
	 * All zeros (16x 0x00) are treated as a secured sector (is_protected = 1)
	 * All ones (16x 0xFF) are treated as a non-secured sector (is_protected = 0)
	 * Anything else is undefined (is_protected = -1). This is treated as
	 * a protected sector!
	 */
	int sector;
314
	int index_t;
315
316
317
318
319
	for( sector = 0; sector < bank->num_sectors; sector++ )
	{
		/* Convert logical sector number to physical sector number */
		if( sector <= 4 )
		{
320
			index_t = sector + 11;
321
322
323
		}
		else if( sector <= 7 )
		{
324
			index_t = sector + 27;
325
326
327
		}
		else
		{
328
			index_t = sector - 8;
329
330
331
332
333
		}

		bank->sectors[sector].is_protected = -1;

		if (
334
335
336
337
		    (iss_secured_field[index_t][0] == 0x00000000) &&
		    (iss_secured_field[index_t][1] == 0x00000000) &&
		    (iss_secured_field[index_t][2] == 0x00000000) &&
		    (iss_secured_field[index_t][3] == 0x00000000) )
338
339
340
341
342
		{
			bank->sectors[sector].is_protected = 1;
		}

		if (
343
344
345
346
		    (iss_secured_field[index_t][0] == 0xFFFFFFFF) &&
		    (iss_secured_field[index_t][1] == 0xFFFFFFFF) &&
		    (iss_secured_field[index_t][2] == 0xFFFFFFFF) &&
		    (iss_secured_field[index_t][3] == 0xFFFFFFFF) )
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
		{
			bank->sectors[sector].is_protected = 0;
		}
	}

	return ERROR_OK;
}


/**
 * Use BIST to calculate a 128-bit hash value over a range of flash.
 *
 * @param bank Pointer to the flash bank descriptor
 * @param addr_from
 * @param addr_to
 * @param signature
 */
364
static uint32_t lpc2900_run_bist128(struct flash_bank *bank,
365
366
367
368
                                    uint32_t addr_from,
                                    uint32_t addr_to,
                                    uint32_t (*signature)[4] )
{
Zachary T Welch's avatar
Zachary T Welch committed
369
	struct target *target = bank->target;
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

	/* Clear END_OF_MISR interrupt status */
	target_write_u32( target, INT_CLR_STATUS, INTSRC_END_OF_MISR );

	/* Start address */
	target_write_u32( target, FMSSTART, addr_from >> 4);
	/* End address, and issue start command */
	target_write_u32( target, FMSSTOP, (addr_to >> 4) | FMSSTOP_MISR_START );

	/* Poll for end of operation. Calculate a reasonable timeout. */
	if( lpc2900_wait_status( bank, INTSRC_END_OF_MISR, 1000 ) != ERROR_OK )
	{
		return ERROR_FLASH_OPERATION_FAILED;
	}

	/* Return the signature */
	target_read_memory( target, FMSW0, 4, 4, (uint8_t *)signature );

	return ERROR_OK;
}


/**
 * Return sector number for given address.
 *
 * Return the (logical) sector number for a given relative address.
 * No sanity check is done. It assumed that the address is valid.
 *
 * @param bank Pointer to the flash bank descriptor
 * @param offset Offset address relative to bank start
 */
401
static uint32_t lpc2900_address2sector( struct flash_bank *bank,
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
                                        uint32_t offset )
{
	uint32_t address = bank->base + offset;


	/* Run through all sectors of this bank */
	int sector;
	for( sector = 0; sector < bank->num_sectors; sector++ )
	{
		/* Return immediately if address is within the current sector */
		if( address < (bank->sectors[sector].offset + bank->sectors[sector].size) )
		{
			return sector;
		}
	}

	/* We should never come here. If we do, return an arbitrary sector number. */
	return 0;
}




/**
 * Write one page to the index sector.
 *
 * @param bank Pointer to the flash bank descriptor
 * @param pagenum Page number (0...7)
 * @param page Page array (FLASH_PAGE_SIZE bytes)
 */
432
static int lpc2900_write_index_page( struct flash_bank *bank,
433
434
435
436
                                     int pagenum,
                                     uint8_t (*page)[FLASH_PAGE_SIZE] )
{
	/* Only pages 4...7 are user writable */
David Brownell's avatar
David Brownell committed
437
	if ((pagenum < 4) || (pagenum > 7))
438
	{
David Brownell's avatar
David Brownell committed
439
		LOG_ERROR("Refuse to burn index sector page %d", pagenum);
440
441
442
443
		return ERROR_COMMAND_ARGUMENT_INVALID;
	}

	/* Get target, and check if it's halted */
Zachary T Welch's avatar
Zachary T Welch committed
444
	struct target *target = bank->target;
445
446
447
448
449
450
451
	if( target->state != TARGET_HALTED )
	{
		LOG_ERROR( "Target not halted" );
		return ERROR_TARGET_NOT_HALTED;
	}

	/* Private info */
452
	struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471

	/* Enable flash block and set the correct CRA clock of 66 kHz */
	lpc2900_setup( bank );

	/* Un-protect the index sector */
	target_write_u32( target, bank->base, 0 );
	target_write_u32( target, FCTR,
	                  FCTR_FS_LOADREQ | FCTR_FS_WPB | FCTR_FS_ISS |
	                  FCTR_FS_WEB | FCTR_FS_WRE | FCTR_FS_CS );

	/* Set latch load mode */
	target_write_u32( target, FCTR,
	                  FCTR_FS_ISS | FCTR_FS_WEB | FCTR_FS_WRE | FCTR_FS_CS );

	/* Write whole page to flash data latches */
	if( target_write_memory( target,
	                         bank->base + pagenum * FLASH_PAGE_SIZE,
	                         4, FLASH_PAGE_SIZE / 4, (uint8_t *)page) != ERROR_OK )
	{
David Brownell's avatar
David Brownell committed
472
		LOG_ERROR("Index sector write failed @ page %d", pagenum);
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
		target_write_u32( target, FCTR, FCTR_FS_CS | FCTR_FS_WEB );

		return ERROR_FLASH_OPERATION_FAILED;
	}

	/* Clear END_OF_BURN interrupt status */
	target_write_u32( target, INT_CLR_STATUS, INTSRC_END_OF_BURN );

	/* Set the program/erase time to FLASH_PROGRAM_TIME */
	target_write_u32(target, FPTR,
	                 FPTR_EN_T | lpc2900_calc_tr( lpc2900_info->clk_sys_fmc,
	                                              FLASH_PROGRAM_TIME ));

	/* Trigger flash write */
	target_write_u32( target, FCTR,
	                  FCTR_FS_PROGREQ | FCTR_FS_ISS |
	                  FCTR_FS_WPB | FCTR_FS_WRE | FCTR_FS_CS );

	/* Wait for the end of the write operation. If it's not over after one
	 * second, something went dreadfully wrong... :-(
	 */
David Brownell's avatar
David Brownell committed
494
	if (lpc2900_wait_status(bank, INTSRC_END_OF_BURN, 1000) != ERROR_OK)
495
	{
David Brownell's avatar
David Brownell committed
496
497
		LOG_ERROR("Index sector write failed @ page %d", pagenum);
		target_write_u32(target, FCTR, FCTR_FS_CS | FCTR_FS_WEB);
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514

		return ERROR_FLASH_OPERATION_FAILED;
	}

	target_write_u32( target, FCTR, FCTR_FS_CS | FCTR_FS_WEB );

	return ERROR_OK;
}



/**
 * Calculate FPTR.TR register value for desired program/erase time.
 *
 * @param clock System clock in Hz
 * @param time Program/erase time in µs
 */
515
static uint32_t lpc2900_calc_tr( uint32_t clock_var, uint32_t time_var )
516
517
518
519
520
521
{
	/*           ((time[µs]/1e6) * f[Hz]) + 511
	 * FPTR.TR = -------------------------------
	 *                         512
	 */

522
	uint32_t tr_val = (uint32_t)((((time_var / 1e6) * clock_var) + 511.0) / 512.0);
523
524
525
526
527
528
529
530
531
532
533
534
535
536

	return tr_val;
}


/***********************  Private flash commands  **************************/


/**
 * Command to determine the signature of the whole flash.
 *
 * Uses the Built-In-Self-Test (BIST) to generate a 128-bit hash value
 * of the flash content.
 */
537
COMMAND_HANDLER(lpc2900_handle_signature_command)
538
539
540
541
542
{
	uint32_t status;
	uint32_t signature[4];


543
	if( CMD_ARGC < 1 )
544
	{
Mathias K's avatar
Mathias K committed
545
		return ERROR_COMMAND_SYNTAX_ERROR;
546
547
	}

548
	struct flash_bank *bank;
549
	int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
550
551
	if (ERROR_OK != retval)
		return retval;
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568

	if( bank->target->state != TARGET_HALTED )
	{
		LOG_ERROR( "Target not halted" );
		return ERROR_TARGET_NOT_HALTED;
	}

	/* Run BIST over whole flash range */
	if( (status = lpc2900_run_bist128( bank,
	                                   bank->base,
	                                   bank->base + (bank->size - 1),
	                                   &signature)
	                                 ) != ERROR_OK )
	{
		return status;
	}

569
	command_print( CMD_CTX, "signature: 0x%8.8" PRIx32
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
	                                  ":0x%8.8" PRIx32
	                                  ":0x%8.8" PRIx32
	                                  ":0x%8.8" PRIx32,
	              signature[3], signature[2], signature[1], signature[0] );

	return ERROR_OK;
}



/**
 * Store customer info in file.
 *
 * Read customer info from index sector, and store that block of data into
 * a disk file. The format is binary.
 */
586
COMMAND_HANDLER(lpc2900_handle_read_custom_command)
587
{
588
	if( CMD_ARGC < 2 )
589
590
591
592
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

593
	struct flash_bank *bank;
594
	int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
595
596
597
	if (ERROR_OK != retval)
		return retval;

598
	struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
599
600
601
	lpc2900_info->risky = 0;

	/* Get target, and check if it's halted */
Zachary T Welch's avatar
Zachary T Welch committed
602
	struct target *target = bank->target;
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
	if( target->state != TARGET_HALTED )
	{
		LOG_ERROR( "Target not halted" );
		return ERROR_TARGET_NOT_HALTED;
	}

	/* Storage for customer info. Read in two parts */
	uint32_t customer[ ISS_CUSTOMER_NWORDS1 + ISS_CUSTOMER_NWORDS2 ];

	/* Enable access to index sector */
	target_write_u32( target, FCTR, FCTR_FS_CS | FCTR_FS_WEB | FCTR_FS_ISS );

	/* Read two parts */
	target_read_memory( target, bank->base+ISS_CUSTOMER_START1, 4,
	                            ISS_CUSTOMER_NWORDS1,
	                            (uint8_t *)&customer[0] );
	target_read_memory( target, bank->base+ISS_CUSTOMER_START2, 4,
	                            ISS_CUSTOMER_NWORDS2,
	                            (uint8_t *)&customer[ISS_CUSTOMER_NWORDS1] );

	/* Deactivate access to index sector */
	target_write_u32( target, FCTR, FCTR_FS_CS | FCTR_FS_WEB );

	/* Try and open the file */
Zachary T Welch's avatar
Zachary T Welch committed
627
	struct fileio fileio;
628
	const char *filename = CMD_ARGV[1];
629
630
631
632
633
634
635
	int ret = fileio_open( &fileio, filename, FILEIO_WRITE, FILEIO_BINARY );
	if( ret != ERROR_OK )
	{
		LOG_WARNING( "Could not open file %s", filename );
		return ret;
	}

Zachary T Welch's avatar
Zachary T Welch committed
636
	size_t nwritten;
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
	ret = fileio_write( &fileio, sizeof(customer),
                        (const uint8_t *)customer, &nwritten );
	if( ret != ERROR_OK )
	{
		LOG_ERROR( "Write operation to file %s failed", filename );
		fileio_close( &fileio );
		return ret;
	}

	fileio_close( &fileio );

	return ERROR_OK;
}




/**
 * Enter password to enable potentially dangerous options.
 */
657
COMMAND_HANDLER(lpc2900_handle_password_command)
658
{
659
	if (CMD_ARGC < 2)
660
661
662
663
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

664
	struct flash_bank *bank;
665
	int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
666
667
668
	if (ERROR_OK != retval)
		return retval;

669
	struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
670
671
672

#define ISS_PASSWORD "I_know_what_I_am_doing"

673
	lpc2900_info->risky = !strcmp( CMD_ARGV[1], ISS_PASSWORD );
674
675
676

	if( !lpc2900_info->risky )
	{
677
		command_print(CMD_CTX, "Wrong password (use '%s')", ISS_PASSWORD);
678
679
680
		return ERROR_COMMAND_ARGUMENT_INVALID;
	}

681
	command_print(CMD_CTX,
682
683
684
685
686
687
688
689
690
691
                  "Potentially dangerous operation allowed in next command!");

	return ERROR_OK;
}



/**
 * Write customer info from file to the index sector.
 */
692
COMMAND_HANDLER(lpc2900_handle_write_custom_command)
693
{
694
	if (CMD_ARGC < 2)
695
696
697
698
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

699
	struct flash_bank *bank;
700
	int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
701
702
703
	if (ERROR_OK != retval)
		return retval;

704
	struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
705
706
707
708

	/* Check if command execution is allowed. */
	if( !lpc2900_info->risky )
	{
709
		command_print( CMD_CTX, "Command execution not allowed!" );
710
711
712
713
714
		return ERROR_COMMAND_ARGUMENT_INVALID;
	}
	lpc2900_info->risky = 0;

	/* Get target, and check if it's halted */
Zachary T Welch's avatar
Zachary T Welch committed
715
	struct target *target = bank->target;
716
717
718
719
720
721
722
	if (target->state != TARGET_HALTED)
	{
		LOG_ERROR("Target not halted");
		return ERROR_TARGET_NOT_HALTED;
	}

	/* The image will always start at offset 0 */
Zachary T Welch's avatar
Zachary T Welch committed
723
	struct image image;
724
725
726
727
	image.base_address_set = 1;
	image.base_address = 0;
	image.start_address_set = 0;

728
729
	const char *filename = CMD_ARGV[1];
	const char *type = (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL;
730
	retval = image_open(&image, filename, type);
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
	if (retval != ERROR_OK)
	{
		return retval;
	}

	/* Do a sanity check: The image must be exactly the size of the customer
	   programmable area. Any other size is rejected. */
	if( image.num_sections != 1 )
	{
		LOG_ERROR("Only one section allowed in image file.");
		return ERROR_COMMAND_SYNTAX_ERROR;
	}
	if( (image.sections[0].base_address != 0) ||
        (image.sections[0].size != ISS_CUSTOMER_SIZE) )
	{
David Brownell's avatar
David Brownell committed
746
747
		LOG_ERROR("Incorrect image file size. Expected %d, "
			"got %" PRIu32,
748
749
750
751
752
753
754
755
756
757
758
759
                   ISS_CUSTOMER_SIZE, image.sections[0].size);
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

	/* Well boys, I reckon this is it... */

	/* Customer info is split into two blocks in pages 4 and 5. */
	uint8_t page[FLASH_PAGE_SIZE];

	/* Page 4 */
	uint32_t offset = ISS_CUSTOMER_START1 % FLASH_PAGE_SIZE;
	memset( page, 0xff, FLASH_PAGE_SIZE );
Zachary T Welch's avatar
Zachary T Welch committed
760
	size_t size_read;
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
	retval = image_read_section( &image, 0, 0,
	                             ISS_CUSTOMER_SIZE1, &page[offset], &size_read);
	if( retval != ERROR_OK )
	{
		LOG_ERROR("couldn't read from file '%s'", filename);
		image_close(&image);
		return retval;
	}
	if( (retval = lpc2900_write_index_page( bank, 4, &page )) != ERROR_OK )
	{
		image_close(&image);
		return retval;
	}

	/* Page 5 */
	offset = ISS_CUSTOMER_START2 % FLASH_PAGE_SIZE;
	memset( page, 0xff, FLASH_PAGE_SIZE );
	retval = image_read_section( &image, 0, ISS_CUSTOMER_SIZE1,
	                             ISS_CUSTOMER_SIZE2, &page[offset], &size_read);
	if( retval != ERROR_OK )
	{
		LOG_ERROR("couldn't read from file '%s'", filename);
		image_close(&image);
		return retval;
	}
	if( (retval = lpc2900_write_index_page( bank, 5, &page )) != ERROR_OK )
	{
		image_close(&image);
		return retval;
	}

	image_close(&image);

	return ERROR_OK;
}



/**
 * Activate 'sector security' for a range of sectors.
 */
802
COMMAND_HANDLER(lpc2900_handle_secure_sector_command)
803
{
804
	if (CMD_ARGC < 3)
805
806
807
808
809
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

	/* Get the bank descriptor */
810
	struct flash_bank *bank;
811
	int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
812
813
814
	if (ERROR_OK != retval)
		return retval;

815
	struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
816
817
818
819

	/* Check if command execution is allowed. */
	if( !lpc2900_info->risky )
	{
820
		command_print( CMD_CTX, "Command execution not allowed! "
821
822
823
824
825
826
		"(use 'password' command first)");
		return ERROR_COMMAND_ARGUMENT_INVALID;
	}
	lpc2900_info->risky = 0;

	/* Read sector range, and do a sanity check. */
827
	int first, last;
828
829
	COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], first);
	COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], last);
830
831
832
833
	if( (first >= bank->num_sectors) ||
	    (last >= bank->num_sectors) ||
	    (first > last) )
	{
834
		command_print( CMD_CTX, "Illegal sector range" );
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
		return ERROR_COMMAND_ARGUMENT_INVALID;
	}

	uint8_t page[FLASH_PAGE_SIZE];
	int sector;

	/* Sectors in page 6 */
	if( (first <= 4) || (last >= 8) )
	{
		memset( &page, 0xff, FLASH_PAGE_SIZE );
		for( sector = first; sector <= last; sector++ )
		{
			if( sector <= 4 )
			{
				memset( &page[0xB0 + 16*sector], 0, 16 );
			}
			else if( sector >= 8 )
			{
				memset( &page[0x00 + 16*(sector - 8)], 0, 16 );
			}
		}

		if( (retval = lpc2900_write_index_page( bank, 6, &page )) != ERROR_OK )
		{
			LOG_ERROR("failed to update index sector page 6");
			return retval;
		}
	}

	/* Sectors in page 7 */
	if( (first <= 7) && (last >= 5) )
	{
		memset( &page, 0xff, FLASH_PAGE_SIZE );
		for( sector = first; sector <= last; sector++ )
		{
			if( (sector >= 5) && (sector <= 7) )
			{
				memset( &page[0x00 + 16*(sector - 5)], 0, 16 );
			}
		}

		if( (retval = lpc2900_write_index_page( bank, 7, &page )) != ERROR_OK )
		{
			LOG_ERROR("failed to update index sector page 7");
			return retval;
		}
	}

883
	command_print( CMD_CTX,
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
	        "Sectors security will become effective after next power cycle");

	/* Update the sector security status */
	if ( lpc2900_read_security_status(bank) != ERROR_OK )
	{
		LOG_ERROR( "Cannot determine sector security status" );
		return ERROR_FLASH_OPERATION_FAILED;
	}

	return ERROR_OK;
}



/**
 * Activate JTAG protection.
 */
901
COMMAND_HANDLER(lpc2900_handle_secure_jtag_command)
902
{
903
	if (CMD_ARGC < 1)
904
905
906
907
908
	{
		return ERROR_COMMAND_SYNTAX_ERROR;
	}

	/* Get the bank descriptor */
909
	struct flash_bank *bank;
910
	int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
911
912
913
	if (ERROR_OK != retval)
		return retval;

914
	struct lpc2900_flash_bank *lpc2900_info = bank->driver_priv;
915
916
917
918

	/* Check if command execution is allowed. */
	if( !lpc2900_info->risky )
	{
919
		command_print( CMD_CTX, "Command execution not allowed! "
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
		                        "(use 'password' command first)");
		return ERROR_COMMAND_ARGUMENT_INVALID;
	}
	lpc2900_info->risky = 0;

	/* Prepare page */
	uint8_t page[FLASH_PAGE_SIZE];
	memset( &page, 0xff, FLASH_PAGE_SIZE );


	/* Insert "soft" protection word */
	page[0x30 + 15] = 0x7F;
	page[0x30 + 11] = 0x7F;
	page[0x30 +  7] = 0x7F;
	page[0x30 +  3] = 0x7F;

	/* Write to page 5 */
	if( (retval = lpc2900_write_index_page( bank, 5, &page ))
			!= ERROR_OK )
	{
		LOG_ERROR("failed to update index sector page 5");
		return retval;
	}

	LOG_INFO("JTAG security set. Good bye!");

	return ERROR_OK;
}



/***********************  Flash interface functions  **************************/

953
954
955
static const struct command_registration lpc2900_exec_command_handlers[] = {
	{
		.name = "signature",
Mathias K's avatar
Mathias K committed
956
		.usage = "<bank>",
David Brownell's avatar
David Brownell committed
957
		.handler = lpc2900_handle_signature_command,
958
		.mode = COMMAND_EXEC,
959
		.help = "Calculate and display signature of flash bank.",
960
961
962
	},
	{
		.name = "read_custom",
David Brownell's avatar
David Brownell committed
963
		.handler = lpc2900_handle_read_custom_command,
964
		.mode = COMMAND_EXEC,
965
966
967
		.usage = "bank_id filename",
		.help = "Copies 912 bytes of customer information "
			"from index sector into file.",
968
969
970
	},
	{
		.name = "password",
David Brownell's avatar
David Brownell committed
971
		.handler = lpc2900_handle_password_command,
972
		.mode = COMMAND_EXEC,
973
974
		.usage = "bank_id password",
		.help = "Enter fixed password to enable 'dangerous' options.",
975
976
977
	},
	{
		.name = "write_custom",
David Brownell's avatar
David Brownell committed
978
		.handler = lpc2900_handle_write_custom_command,
979
		.mode = COMMAND_EXEC,
980
981
982
		.usage = "bank_id filename ('bin'|'ihex'|'elf'|'s19')",
		.help = "Copies 912 bytes of customer info from file "
			"to index sector.",
983
984
985
	},
	{
		.name = "secure_sector",
David Brownell's avatar
David Brownell committed
986
		.handler = lpc2900_handle_secure_sector_command,
987
		.mode = COMMAND_EXEC,
988
989
990
		.usage = "bank_id first_sector last_sector",
		.help = "Activate sector security for a range of sectors.  "
			"It will be effective after a power cycle.",
991
992
993
	},
	{
		.name = "secure_jtag",
David Brownell's avatar
David Brownell committed
994
		.handler = lpc2900_handle_secure_jtag_command,
995
		.mode = COMMAND_EXEC,
996
997
998
		.usage = "bank_id",
		.help = "Disable the JTAG port.  "
			"It will be effective after a power cycle.",
999
1000
	},
	COMMAND_REGISTRATION_DONE
For faster browsing, not all history is shown. View entire blame