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 extends AltosProgrammer {
24 FileInputStream input;
28 AltosFlashListener listener;
29 AltosRomconfig rom_config;
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 byte[] read_memory(long addr, int len) throws InterruptedException, IOException {
45 byte[] data = new byte[len];
47 for (int offset = 0; offset < len; offset += 0x100) {
48 link.printf("R %x\n", addr + offset);
49 byte[] reply = link.get_binary_reply(5000, 0x100);
52 throw new IOException("Read device memory timeout");
53 for (b = 0; b < len; b++)
54 data[b+offset] = reply[b];
59 void write_memory(long addr, byte[] data, int start, int len) {
61 System.out.printf ("write_memory %x %d\n", addr, 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);
71 System.out.printf("reboot\n");
78 if (!check_rom_config())
79 throw new IOException("Invalid rom config settings");
82 * Store desired config values into image
84 rom_config.write(image);
86 int remain = image.data.length;
87 long flash_addr = image.address;
91 action(0, image.data.length);
92 while (remain > 0 && !aborted) {
93 int this_time = remain;
94 if (this_time > 0x100)
99 write_memory(flash_addr, image.data, image_start, this_time);
101 byte[] check = read_memory(flash_addr, this_time);
102 for (int i = 0; i < this_time; i++)
103 if (check[i] != image.data[image_start + i])
104 throw new IOException(String.format("Flash write failed at 0x%x (%02x != %02x)",
105 image.address + image_start + i,
106 check[i], image.data[image_start + i]));
112 flash_addr += this_time;
113 image_start += this_time;
115 action(image.data.length - remain, image.data.length);
118 System.out.printf ("done\n");
122 } catch (IOException ie) {
123 action(ie.getMessage(), -1);
125 } catch (InterruptedException ie) {
130 public void close() {
135 } catch (InterruptedException ie) {
141 synchronized public void abort() {
146 private AltosHexfile get_rom() throws InterruptedException {
147 System.out.printf("get rom\n");
149 int base = AltosRomconfig.fetch_base(image);
150 int bounds = AltosRomconfig.fetch_bounds(image);
151 byte[] data = read_memory(base, bounds - base);
152 AltosHexfile hexfile = new AltosHexfile(data, base);
153 hexfile.add_symbols(image);
155 } catch (AltosNoSymbol none) {
156 System.out.printf("no symbol %s\n", none.getMessage());
158 } catch (IOException ie) {
164 public boolean check_rom_config() throws InterruptedException {
166 System.out.printf ("no link\n");
169 if (rom_config == null) {
170 AltosHexfile hexfile = get_rom();
172 rom_config = new AltosRomconfig(hexfile);
174 return rom_config != null && rom_config.valid();
177 public void set_romconfig (AltosRomconfig romconfig) {
178 rom_config = romconfig;
181 public AltosRomconfig romconfig() throws InterruptedException {
182 System.out.printf("fetch romconfig\n");
183 if (!check_rom_config())
188 public AltosSelfFlash(File file, AltosLink link, AltosFlashListener listener)
189 throws IOException, FileNotFoundException, InterruptedException {
192 this.listener = listener;
193 input = new FileInputStream(file);
194 image = new AltosHexfile(input);
195 System.out.printf ("AltosSelfFlash %x\n", image.address);