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.
25 #include "ao-selfload.h"
26 #include "ao-verbose.h"
28 #define TRACE(...) ao_printf(AO_VERBOSE_SELF, __VA_ARGS__)
31 ao_self_block_read(struct cc_usb *cc, uint32_t address, uint8_t block[256])
35 cc_usb_printf(cc, "R %x\n", address);
36 for (byte = 0; byte < 0x100; byte++) {
37 block[byte] = cc_usb_getchar(cc);
39 TRACE ("\nread %08x\n", address);
40 for (byte = 0; byte < 0x100; byte++) {
41 TRACE (" %02x", block[byte]);
42 if ((byte & 0xf) == 0xf)
48 ao_self_block_write(struct cc_usb *cc, uint32_t address, uint8_t block[256])
52 cc_usb_printf(cc, "W %x\n", address);
53 TRACE ("write %08x\n", address);
54 for (byte = 0; byte < 0x100; byte++) {
55 TRACE (" %02x", block[byte]);
56 if ((byte & 0xf) == 0xf)
59 for (byte = 0; byte < 0x100; byte++) {
60 cc_usb_printf(cc, "%c", block[byte]);
65 ao_self_read(struct cc_usb *cc, uint32_t address, uint32_t length)
67 struct ao_hex_image *image;
70 uint32_t base = address & ~0xff;
71 uint32_t bound = (address + length + 0xff) & ~0xff;
73 image = calloc(sizeof (struct ao_hex_image) + (bound - base), 1);
74 image->address = base;
75 image->length = bound - base;
76 pages = image->length / 0x100;
77 for (page = 0; page < pages; page++)
78 ao_self_block_read(cc, image->address + page * 0x100, image->data + page * 0x100);
83 ao_self_write(struct cc_usb *cc, struct ao_hex_image *image)
87 uint32_t base, bound, length, address;
91 base = image->address & ~0xff;
92 bound = (image->address + image->length + 0xff) & ~0xff;
95 length = bound - base;
97 pages = length / 0x100;
98 printf ("Write %08x %d pages: ", address, length/0x100); fflush(stdout);
99 for (page = 0; page < pages; page++) {
100 uint32_t start, stop;
101 address = base + page * 0x100;
103 if (address < image->address || address + 0x100 > image->address + image->length) {
104 ao_self_block_read(cc, address, block);
107 stop = address + 0x100;
108 if (start < image->address)
109 start = image->address;
110 if (stop > image->address + image->length)
111 stop = image->address + image->length;
112 memcpy(block + start - address, image->data + start - image->address, stop - start);
113 ao_self_block_write(cc, address, block);
114 ao_self_block_read(cc, address, check);
115 if (memcmp(block, check, 0x100) != 0) {
116 fprintf(stderr, "Block at 0x%08x doesn't match\n", address);
119 putchar('.'); fflush(stdout);
122 cc_usb_printf(cc,"a\n");
127 * Read a 16-bit value from the USB target
131 ao_self_get_uint16(struct cc_usb *cc, uint32_t addr)
133 struct ao_hex_image *hex = ao_self_read(cc, addr, 2);
139 data = hex->data + addr - hex->address;
140 v = data[0] | (data[1] << 8);
146 ao_self_get_uint32(struct cc_usb *cc, uint32_t addr)
148 struct ao_hex_image *hex = ao_self_read(cc, addr, 4);
154 data = hex->data + addr - hex->address;
155 v = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);