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.
27 #include "ao-stmload.h"
31 #define TRACE(...) if (ao_self_verbose) printf (__VA_ARGS__)
34 ao_self_block_read(struct cc_usb *cc, uint32_t address, uint8_t block[256])
38 cc_usb_printf(cc, "R %x\n", address);
39 for (byte = 0; byte < 0x100; byte++) {
40 block[byte] = cc_usb_getchar(cc);
42 TRACE ("\nread %08x\n", address);
43 for (byte = 0; byte < 0x100; byte++) {
44 TRACE (" %02x", block[byte]);
45 if ((byte & 0xf) == 0xf)
51 ao_self_block_write(struct cc_usb *cc, uint32_t address, uint8_t block[256])
55 cc_usb_printf(cc, "W %x\n", address);
56 TRACE ("write %08x\n", address);
57 for (byte = 0; byte < 0x100; byte++) {
58 TRACE (" %02x", block[byte]);
59 if ((byte & 0xf) == 0xf)
62 for (byte = 0; byte < 0x100; byte++) {
63 cc_usb_printf(cc, "%c", block[byte]);
68 ao_self_read(struct cc_usb *cc, uint32_t address, uint32_t length)
70 struct hex_image *image;
73 uint32_t base = address & ~0xff;
74 uint32_t bound = (address + length + 0xff) & ~0xff;
76 image = calloc(sizeof (struct hex_image) + (bound - base), 1);
77 image->address = base;
78 image->length = bound - base;
79 pages = image->length / 0x100;
80 for (page = 0; page < pages; page++)
81 ao_self_block_read(cc, image->address + page * 0x100, image->data + page * 0x100);
86 ao_self_write(struct cc_usb *cc, struct hex_image *image)
90 uint32_t base, bound, length, address;
94 base = image->address & ~0xff;
95 bound = (image->address + image->length + 0xff) & ~0xff;
98 length = bound - base;
100 pages = length / 0x100;
101 printf ("Write %08x %d pages: ", address, length/0x100); fflush(stdout);
102 for (page = 0; page < pages; page++) {
103 uint32_t start, stop;
104 address = base + page * 0x100;
106 if (address < image->address || address + 0x100 > image->address + image->length) {
107 ao_self_block_read(cc, address, block);
110 stop = address + 0x100;
111 if (start < image->address)
112 start = image->address;
113 if (stop > image->address + image->length)
114 stop = image->address + image->length;
115 memcpy(block + start - address, image->data + start - image->address, stop - start);
116 ao_self_block_write(cc, address, block);
117 ao_self_block_read(cc, address, check);
118 if (memcmp(block, check, 0x100) != 0) {
119 fprintf(stderr, "Block at 0x%08x doesn't match\n", address);
122 putchar('.'); fflush(stdout);
125 cc_usb_printf(cc,"a\n");