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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_12;
23 public class AltosSelfFlash extends AltosProgrammer {
25 FileInputStream input;
29 AltosFlashListener listener;
30 AltosRomconfig rom_config;
32 void action(String s, int percent) {
33 if (listener != null && !aborted)
34 listener.position(s, percent);
37 void action(int part, int total) {
38 int percent = 100 * part / total;
39 action(String.format("%d/%d (%d%%)",
40 part, total, percent),
44 byte[] read_memory(long addr, int len) throws InterruptedException, IOException {
46 byte[] data = new byte[len];
48 for (int offset = 0; offset < len; offset += 0x100) {
49 link.printf("R %x\n", addr + offset);
50 byte[] reply = link.get_binary_reply(5000, 0x100);
53 throw new IOException("Read device memory timeout");
54 for (b = 0; b < len; b++)
55 data[b+offset] = reply[b];
60 void write_memory(long addr, byte[] data, int start, int len) {
62 link.printf("W %x\n", addr);
64 for (b = 0; b < len; b++)
65 link.putchar(data[start + b]);
66 for (; b < 0x100; b++)
67 link.putchar((byte) 0xff);
77 if (!check_rom_config())
78 throw new IOException("Invalid rom config settings");
81 * Store desired config values into image
83 rom_config.write(image);
85 int remain = image.data.length;
86 long flash_addr = image.address;
90 action(0, image.data.length);
91 while (remain > 0 && !aborted) {
92 int this_time = remain;
93 if (this_time > 0x100)
98 write_memory(flash_addr, image.data, image_start, this_time);
100 byte[] check = read_memory(flash_addr, this_time);
101 for (int i = 0; i < this_time; i++)
102 if (check[i] != image.data[image_start + i])
103 throw new IOException(String.format("Flash write failed at 0x%x (%02x != %02x)",
104 image.address + image_start + i,
105 check[i], image.data[image_start + i]));
111 flash_addr += this_time;
112 image_start += this_time;
114 action(image.data.length - remain, image.data.length);
120 } catch (IOException ie) {
121 action(ie.getMessage(), -1);
123 } catch (InterruptedException ie) {
128 public void close() {
133 } catch (InterruptedException ie) {
139 synchronized public void abort() {
144 private AltosHexfile get_rom() throws InterruptedException {
146 int base = AltosRomconfig.fetch_base(image);
147 int bounds = AltosRomconfig.fetch_bounds(image);
148 byte[] data = read_memory(base, bounds - base);
149 AltosHexfile hexfile = new AltosHexfile(data, base);
150 hexfile.add_symbols(image);
152 } catch (AltosNoSymbol none) {
154 } catch (IOException ie) {
160 public boolean check_rom_config() throws InterruptedException {
164 if (rom_config == null) {
165 AltosHexfile hexfile = get_rom();
167 rom_config = new AltosRomconfig(hexfile);
169 return rom_config != null && rom_config.valid();
172 public void set_romconfig (AltosRomconfig romconfig) {
173 rom_config = romconfig;
176 public AltosRomconfig romconfig() throws InterruptedException {
177 if (!check_rom_config())
182 public AltosSelfFlash(File file, AltosLink link, AltosFlashListener listener)
183 throws IOException, FileNotFoundException, InterruptedException {
186 this.listener = listener;
187 input = new FileInputStream(file);
188 image = new AltosHexfile(input);