X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altoslib%2FAltosSelfFlash.java;h=0250cce741a6d1f3c64713418f5a953b12f04412;hp=07917d5d09fff07eb1f4e90f6e34ccd9d9551534;hb=a8f4af38d7abbb1da922b5e4d84af218475f1752;hpb=7ec1b97d278c7aec3199fb7270f0dcf9484c879f diff --git a/altoslib/AltosSelfFlash.java b/altoslib/AltosSelfFlash.java index 07917d5d..0250cce7 100644 --- a/altoslib/AltosSelfFlash.java +++ b/altoslib/AltosSelfFlash.java @@ -3,7 +3,8 @@ * * 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; version 2 of the License. + * 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 @@ -15,18 +16,18 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package org.altusmetrum.altoslib_1; +package org.altusmetrum.altoslib_13; import java.io.*; -public class AltosSelfFlash { +public class AltosSelfFlash extends AltosProgrammer { File file; FileInputStream input; AltosHexfile image; AltosLink link; boolean aborted; AltosFlashListener listener; - byte[] read_block, write_block; + AltosRomconfig rom_config; void action(String s, int percent) { if (listener != null && !aborted) @@ -40,28 +41,66 @@ public class AltosSelfFlash { percent); } - void read_block(long addr) { - link.printf("R %x\n", addr); - + byte[] read_memory(long addr, int len) throws InterruptedException, IOException { + int b; + byte[] data = new byte[len]; + + for (int offset = 0; offset < len; offset += 0x100) { + link.printf("R %x\n", addr + offset); + byte[] reply = link.get_binary_reply(5000, 0x100); + + if (reply == null) + throw new IOException("Read device memory timeout"); + for (b = 0; b < 0x100 && b + offset < len; b++) + data[b+offset] = reply[b]; + } + return data; } - void read_memory(long addr, int len) { + AltosHexfile read_hexfile(long addr, int len) throws InterruptedException { + try { + byte[] mem = read_memory(addr, len); + + AltosHexfile hexfile = new AltosHexfile(mem, addr); + + if (image != null) + hexfile.add_symbols(image); + return hexfile; + } catch (IOException ie) { + return null; + } } - + void write_memory(long addr, byte[] data, int start, int len) { - + int b; + link.printf("W %x\n", addr); + link.flush_output(); + for (b = 0; b < len; b++) + link.putchar(data[start + b]); + for (; b < 0x100; b++) + link.putchar((byte) 0xff); } void reboot() { + link.printf("a\n"); + link.flush_output(); } public void flash() { try { + if (!check_rom_config()) + throw new IOException("Invalid rom config settings"); + + /* + * Store desired config values into image + */ + rom_config.write(image); + int remain = image.data.length; long flash_addr = image.address; int image_start = 0; - action("start", 0); + action(AltosFlashListener.flash_start, 0); action(0, image.data.length); while (remain > 0 && !aborted) { int this_time = remain; @@ -89,13 +128,9 @@ public class AltosSelfFlash { action(image.data.length - remain, image.data.length); } if (!aborted) { - action("done", 100); - if (link != null) { - reboot(); - } + action(AltosFlashListener.flash_done, 100); } - if (link != null) - link.close(); + close(); } catch (IOException ie) { action(ie.getMessage(), -1); abort(); @@ -105,8 +140,14 @@ public class AltosSelfFlash { } public void close() { - if (link != null) - link.close(); + if (link != null) { + reboot(); + try { + link.close(); + } catch (InterruptedException ie) { + } + link = null; + } } synchronized public void abort() { @@ -114,11 +155,28 @@ public class AltosSelfFlash { close(); } - public boolean check_rom_config() { - if (link == null) + private AltosHexfile get_rom() throws InterruptedException { + try { + long base = AltosRomconfig.fetch_base(image); + long bounds = AltosRomconfig.fetch_bounds(image); + + if (link.debug) + System.out.printf("rom base %x bounds %x\n", base, bounds); + return read_hexfile(base, (int) (bounds - base)); + } catch (AltosNoSymbol ns) { + return null; + } + } + + public boolean check_rom_config() throws InterruptedException { + if (link == null) { return true; - if (rom_config == null) - rom_config = debug.romconfig(); + } + if (rom_config == null) { + AltosHexfile hexfile = get_rom(); + if (hexfile != null) + rom_config = new AltosRomconfig(hexfile); + } return rom_config != null && rom_config.valid(); } @@ -126,24 +184,22 @@ public class AltosSelfFlash { rom_config = romconfig; } - public AltosRomconfig romconfig() { + public AltosRomconfig target_romconfig() throws InterruptedException { if (!check_rom_config()) return null; return rom_config; } - public AltosFlash(File file, AltosLink link, AltosFlashListener listener) + public AltosRomconfig image_romconfig() { + return new AltosRomconfig(image); + } + + public AltosSelfFlash(File file, AltosLink link, AltosFlashListener listener) throws IOException, FileNotFoundException, InterruptedException { this.file = file; this.link = link; this.listener = listener; - this.read_block = new byte[256]; - this.write_block = new byte[256]; input = new FileInputStream(file); image = new AltosHexfile(input); - if (link != null) { - debug.close(); - throw new IOException("Debug port not connected"); - } } -} \ No newline at end of file +}