ao-tools: Clean up ao-sym structure an initializers
[fw/altos] / ao-tools / lib / ao-selfload.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <sysexits.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include "ao-hex.h"
25 #include "ao-selfload.h"
26 #include "ao-verbose.h"
27
28 #define TRACE(...) ao_printf(AO_VERBOSE_SELF, __VA_ARGS__)
29
30 static void
31 ao_self_block_read(struct cc_usb *cc, uint32_t address, uint8_t block[256])
32 {
33         int                     byte;
34         cc_usb_sync(cc);
35         cc_usb_printf(cc, "R %x\n", address);
36         for (byte = 0; byte < 0x100; byte++) {
37                 block[byte] = cc_usb_getchar(cc);
38         }
39         TRACE ("\nread %08x\n", address);
40         for (byte = 0; byte < 0x100; byte++) {
41                 TRACE (" %02x", block[byte]);
42                 if ((byte & 0xf) == 0xf)
43                         TRACE ("\n");
44         }
45 }
46
47 static void
48 ao_self_block_write(struct cc_usb *cc, uint32_t address, uint8_t block[256])
49 {
50         int                     byte;
51         cc_usb_sync(cc);
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)
57                         TRACE ("\n");
58         }
59         for (byte = 0; byte < 0x100; byte++) {
60                 cc_usb_printf(cc, "%c", block[byte]);
61         }
62 }
63
64 struct ao_hex_image *
65 ao_self_read(struct cc_usb *cc, uint32_t address, uint32_t length)
66 {
67         struct ao_hex_image     *image;
68         int                     pages;
69         int                     page;
70         uint32_t                base = address & ~0xff;
71         uint32_t                bound = (address + length + 0xff) & ~0xff;
72
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);
79         return image;
80 }
81
82 bool
83 ao_self_write(struct cc_usb *cc, struct ao_hex_image *image)
84 {
85         uint8_t         block[256];
86         uint8_t         check[256];
87         uint32_t        base, bound, length, address;
88         uint32_t        pages;
89         uint32_t        page;
90
91         base = image->address & ~0xff;
92         bound = (image->address + image->length + 0xff) & ~0xff;
93
94         address = base;
95         length = bound - base;
96
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;
102
103                 if (address < image->address || address + 0x100 > image->address + image->length) {
104                         ao_self_block_read(cc, address, block);
105                 }
106                 start = address;
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);
117                         return 0;
118                 }
119                 putchar('.'); fflush(stdout);
120         }
121         printf("done\n");
122         cc_usb_printf(cc,"a\n");
123         return 1;
124 }
125
126 /*
127  * Read a 16-bit value from the USB target
128  */
129
130 uint16_t
131 ao_self_get_uint16(struct cc_usb *cc, uint32_t addr)
132 {
133         struct ao_hex_image     *hex = ao_self_read(cc, addr, 2);
134         uint16_t                v;
135         uint8_t                 *data;
136
137         if (!hex)
138                 return 0;
139         data = hex->data + addr - hex->address;
140         v = data[0] | (data[1] << 8);
141         free(hex);
142         return v;
143 }
144
145 uint32_t
146 ao_self_get_uint32(struct cc_usb *cc, uint32_t addr)
147 {
148         struct ao_hex_image     *hex = ao_self_read(cc, addr, 4);
149         uint32_t                v;
150         uint8_t                 *data;
151
152         if (!hex)
153                 return 0;
154         data = hex->data + addr - hex->address;
155         v = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
156         free(hex);
157         return v;
158 }