1 /*********************************************************************
3 * LPC ISP Commands for flash tool
6 * Copyright (C) 2012 Nathael Pajani <nathael.pajani@nathael.net>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *********************************************************************/
23 #include <stdlib.h> /* strtoul */
24 #include <stdio.h> /* printf, snprintf */
27 #include <string.h> /* strncmp, memset */
31 #include <unistd.h> /* for open, close */
33 #include <sys/types.h>
36 #include "isp_utils.h"
37 #include "isp_commands.h"
40 #define REP_BUFSIZE 40
51 isp_cmd_boot_version();
57 int dump_to_file(struct part_desc* part, char* filename)
63 data = malloc(part->flash_size);
65 printf("Unable to allocate read buffer, asked %u.\n", part->flash_size);
70 len = isp_read_memory(data, part->flash_base, part->flash_size, part->uuencode);
71 if (len != (int)(part->flash_size)) {
72 printf("Read returned %d bytes instead of %u.\n", len, part->flash_size);
75 /* Write data to file */
76 ret = isp_buff_to_file(data, part->flash_size, filename);
85 int erase_flash(struct part_desc* part)
91 ret = isp_cmd_unlock(1);
93 printf("Unable to unlock device, aborting.\n");
97 for (i=0; i<(int)(part->flash_nb_sectors); i++) {
98 ret = isp_send_cmd_sectors("blank-check", 'I', i, i, 1);
99 if (ret == CMD_SUCCESS) {
100 /* sector already blank, preserve the flash, skip to next one :) */
105 printf("Initial blank check error (%d) at sector %d!\n", ret, i);
108 /* Controller replyed with first non blank offset and data, remove it from buffer */
109 char buf[REP_BUFSIZE];
110 usleep( 5000 ); /* Some devices are slow to scan flash, give them some time */
111 isp_serial_read(buf, REP_BUFSIZE, 3);
113 /* Sector not blank, perform erase */
114 ret = isp_send_cmd_sectors("prepare-for-write", 'P', i, i, 1);
116 printf("Error (%d) when trying to prepare sector %d for erase operation!\n", ret, i);
119 ret = isp_send_cmd_sectors("erase", 'E', i, i, 1);
121 printf("Error (%d) when trying to erase sector %d!\n", ret, i);
125 printf("Flash now all blank.\n");
130 int start_prog(struct part_desc* part)
132 int ret = 0, len = 0;
135 len = isp_read_memory((char*)&addr, (part->flash_base + part->reset_vector_offset), sizeof(addr), part->uuencode);
136 if (len != sizeof(addr)) {
137 printf("Unable to read reset address from flash.\n");
140 /* FIXME : the following value (0x200) may be LPC111x specific */
142 printf("Actual reset address is 0x%08x, which is under the lowest allowed address of 0x200.\n", addr);
145 ret = isp_cmd_unlock(1);
147 printf("Unable to unlock device, aborting.\n");
150 /* Read address in thumb or arm mode ? */
153 ret = isp_send_cmd_go(addr, 'T');
155 ret = isp_send_cmd_go(addr, 'A');
158 /* FIXME : start terminal */
164 static unsigned int calc_write_size(unsigned int sector_size, unsigned int ram_buff_size)
166 unsigned int write_size = 0;
168 write_size = ((sector_size < ram_buff_size) ? sector_size : ram_buff_size);
169 /* According to section 21.5.7 of LPC11xx user's manual (UM10398), number of bytes
170 * written should be 256 | 512 | 1024 | 4096 */
171 if (write_size >= 4096) {
173 } else if (write_size >= 1024) {
175 } else if (write_size >= 512) {
177 } else if (write_size >= 256) {
179 } else if (write_size >= 64) {
187 int flash_target(struct part_desc* part, char* filename, int calc_user_code)
192 int i = 0, blocks = 0;
193 unsigned int write_size = 0;
194 unsigned int sector_size = (part->flash_size / part->flash_nb_sectors);
195 uint32_t ram_addr = (part->ram_base + part->ram_buff_offset);
196 uint32_t uuencode = part->uuencode;
197 uint32_t* v = NULL; /* Used for checksum computing */
201 /** Sanity checks *********************************/
202 /* RAM buffer address within RAM */
203 if (ram_addr > (part->ram_base + part->ram_size)) {
204 printf("Invalid configuration, asked to use buffer out of RAM, aborting.\n");
207 /* Calc write block size */
208 write_size = calc_write_size(sector_size, part->ram_buff_size);
209 if (write_size == 0) {
210 printf("Config error, I cannot flash using blocks of nul size !\nAborted.\n");
214 /* Just make sure flash is erased */
215 ret = erase_flash(part);
217 printf("Unable to erase device, aborting.\n");
221 /* Allocate a buffer as big as the flash */
222 data = malloc(part->flash_size);
224 printf("Unable to get a buffer to load the image!");
227 /* And fill the buffer with the image */
228 size = isp_file_to_buff(data, part->flash_size, filename);
233 /* Fill unused buffer with 0's so we can flash blocks of data of "write_size" */
234 memset(&data[size], 0, (part->flash_size - size));
235 /* And check checksum of first 7 vectors if asked, according to section 21.3.3 of
236 * LPC11xx user's manual (UM10398) */
237 v = (uint32_t *)data;
238 cksum = 0 - v[0] - v[1] - v[2] - v[3] - v[4] - v[5] - v[6];
239 if (calc_user_code == 1) {
241 } else if (cksum != v[7]) {
242 printf("Checksum is 0x%08x, should be 0x%08x\n", v[7], cksum);
246 printf("Checksum check OK\n");
248 crp = v[(CRP_OFFSET / 4)];
249 if ((crp == CRP_NO_ISP) || (crp == CRP_CRP1) || (crp == CRP_CRP2) || (crp == CRP_CRP3)) {
250 printf("CRP : 0x%08x\n", crp);
251 printf("The binary has CRP protection ativated, which violates GPLv3.\n");
252 printf("Check the licence for the software you are using, and if this is allowed,\n");
253 printf(" then modify this software to allow flashing of code with CRP protection\n");
254 printf(" activated. (Or use another software).\n");
258 blocks = (size / write_size) + ((size % write_size) ? 1 : 0);
259 /* Gonna write out of flash ? */
260 if ((blocks * write_size) > part->flash_size) {
261 printf("Config error, I cannot flash beyond end of flash !\n");
262 printf("Flash size : %d, trying to flash %d blocks of %d bytes : %d\n",
263 part->flash_size, blocks, write_size, (blocks * write_size));
267 printf("Flash size : %d, trying to flash %d blocks of %d bytes : %d\n",
268 part->flash_size, blocks, write_size, (blocks * write_size));
270 /* Now flash the device */
271 printf("Writing started, %d blocks of %d bytes ...\n", blocks, write_size);
272 for (i=0; i<blocks; i++) {
273 unsigned int current_sector = (i * write_size) / sector_size;
274 uint32_t flash_addr = part->flash_base + (i * write_size);
275 /* Prepare sector for writting (must be done before each write) */
276 ret = isp_send_cmd_sectors("prepare-for-write", 'P', current_sector, current_sector, 1);
278 printf("Error (%d) when trying to prepare sector %d for erase operation!\n", ret, i);
282 /* Send data to RAM */
283 ret = isp_send_buf_to_ram(&data[i * write_size], ram_addr, write_size, uuencode);
285 printf("Unable to perform write-to-ram operation for block %d (block size: %d)\n",
290 /* Copy from RAM to FLASH */
291 ret = isp_send_cmd_address('C', flash_addr, ram_addr, write_size, "write_to_ram");
293 printf("Unable to copy data to flash for block %d (block size: %d)\n", i, write_size);