2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.altoslib_2;
22 public class AltosSelfFlash {
24 FileInputStream input;
28 AltosFlashListener listener;
29 byte[] read_block, write_block;
31 void action(String s, int percent) {
32 if (listener != null && !aborted)
33 listener.position(s, percent);
36 void action(int part, int total) {
37 int percent = 100 * part / total;
38 action(String.format("%d/%d (%d%%)",
39 part, total, percent),
43 void read_block(long addr) {
44 link.printf("R %x\n", addr);
48 void read_memory(long addr, int len) {
51 void write_memory(long addr, byte[] data, int start, int len) {
60 int remain = image.data.length;
61 long flash_addr = image.address;
65 action(0, image.data.length);
66 while (remain > 0 && !aborted) {
67 int this_time = remain;
68 if (this_time > 0x100)
73 write_memory(flash_addr, image.data, image_start, this_time);
75 byte[] check = read_memory(flash_addr, this_time);
76 for (int i = 0; i < this_time; i++)
77 if (check[i] != image.data[image_start + i])
78 throw new IOException(String.format("Flash write failed at 0x%x (%02x != %02x)",
79 image.address + image_start + i,
80 check[i], image.data[image_start + i]));
86 flash_addr += this_time;
87 image_start += this_time;
89 action(image.data.length - remain, image.data.length);
99 } catch (IOException ie) {
100 action(ie.getMessage(), -1);
102 } catch (InterruptedException ie) {
107 public void close() {
112 synchronized public void abort() {
117 public boolean check_rom_config() {
120 if (rom_config == null)
121 rom_config = debug.romconfig();
122 return rom_config != null && rom_config.valid();
125 public void set_romconfig (AltosRomconfig romconfig) {
126 rom_config = romconfig;
129 public AltosRomconfig romconfig() {
130 if (!check_rom_config())
135 public AltosFlash(File file, AltosLink link, AltosFlashListener listener)
136 throws IOException, FileNotFoundException, InterruptedException {
139 this.listener = listener;
140 this.read_block = new byte[256];
141 this.write_block = new byte[256];
142 input = new FileInputStream(file);
143 image = new AltosHexfile(input);
146 throw new IOException("Debug port not connected");