lpc178x_fcg.c 3.07 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 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 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
/*
 * Copyright (C) 2011 by NXP Semiconductors
 * All rights reserved.
 *
 * @Author: Kevin Wells
 * @Descr: Inserts checksum into FLASH image for LPC17xx devices
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * This LPC1788 requires a checksum of the first 7 words in FLASH to boot
 * a user program. All this program does is compute the 2's complement
 * checksum on the first 7 32-bit words and inserts it into the 8th 32-bit
 * word - overwriting what is there.
 *
 * Note that some LPC1788 FLASH burners may already generate the checksum
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/*
 * Works for LPC1787and LPC1788, but adjust for other parts
 */
#define MAX_FLASH_SIZE (512 * 1024)

int main(int argc, char *argv[])
{
	FILE *fp = NULL;
	char *filename, *nfilename;
	unsigned int cksum, *buff;
	size_t fsize;
	int i, ret = 0;

	if (argc != 3)
	{
		printf("Usage: %s infile outfile\n", argv[0]);
		return 1;
	}

	/* Enough for a 512K image to burn */
	buff = (unsigned int *) malloc(MAX_FLASH_SIZE);
	if (!buff) {
		printf("Error allocating memory for buffer\n");
		return 1;
	}

	filename = (char *) argv[1];
	fp = fopen((const char *) filename, "r");
	if (!fp) {
		printf("Error opening file %s\n", filename);
		ret = 1;
		goto free_buff;
	}

	/* Load file and save file size */
	fsize = fread((void *) buff, 1, MAX_FLASH_SIZE, fp);

	if (ferror(fp)) {
		printf("Error loading file %s\n", filename);
		ret = 1;
		goto close_file;
	}


	if (!feof(fp)) {
		printf("File %s is too large. The maximum size is "
			"limited by FLASH (%d bytes)\n", filename,
			MAX_FLASH_SIZE);
		ret = 1;
		goto close_file;
	}

	printf("File %s loaded with size %d bytes\n", filename, (int) fsize);

	fclose(fp);

	nfilename = (char *) argv[2];

	/* Open new file for writing */
	fp = fopen(nfilename, "w");
	if (!fp) {
		printf("Error creating new file %s\n", nfilename);
		ret = 1;
		goto free_buff;
	}

	/* Compute 2's complement checksum */
	cksum = 0;
	for (i = 0; i < 7; i++) {
		cksum += buff[i];
		printf("Word %d: 0x%08x\n", i, buff[i]);
	}

	buff[7] = 0xFFFFFFFF - cksum + 1;
	printf("Word 7: 0x%08x (cksum total: 0x%08x)\n", buff[7],
		(cksum + buff[7]));

	/* Write data back to file */
	if (fwrite(buff, 1, fsize, fp) != fsize) {
		printf("Error writing new file %s\n", nfilename);
		ret = 1;
		goto close_file;
	}
	printf("File %s created with size %d bytes\n", nfilename,
		(int) fsize);

close_file:
	fclose(fp);

free_buff:
	free(buff);

	return ret;
}