ao-usbload: Check target device name to avoid mis-flashing
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <sysexits.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include "ao-hex.h"
26 #include "ao-selfload.h"
27 #include "ao-verbose.h"
28
29 #define TRACE(...) ao_printf(AO_VERBOSE_SELF, __VA_ARGS__)
30
31 static void
32 ao_self_block_read(struct cc_usb *cc, uint32_t address, uint8_t block[256])
33 {
34         int                     byte;
35         cc_usb_sync(cc);
36         cc_usb_printf(cc, "R %x\n", address);
37         for (byte = 0; byte < 0x100; byte++) {
38                 block[byte] = cc_usb_getchar(cc);
39         }
40         TRACE ("\nread %08x\n", address);
41         for (byte = 0; byte < 0x100; byte++) {
42                 TRACE (" %02x", block[byte]);
43                 if ((byte & 0xf) == 0xf)
44                         TRACE ("\n");
45         }
46 }
47
48 static void
49 ao_self_block_write(struct cc_usb *cc, uint32_t address, uint8_t block[256])
50 {
51         int                     byte;
52         cc_usb_sync(cc);
53         cc_usb_printf(cc, "W %x\n", address);
54         TRACE ("write %08x\n", address);
55         for (byte = 0; byte < 0x100; byte++) {
56                 TRACE (" %02x", block[byte]);
57                 if ((byte & 0xf) == 0xf)
58                         TRACE ("\n");
59         }
60         for (byte = 0; byte < 0x100; byte++) {
61                 cc_usb_printf(cc, "%c", block[byte]);
62         }
63 }
64
65 struct ao_hex_image *
66 ao_self_read(struct cc_usb *cc, uint32_t address, uint32_t length)
67 {
68         struct ao_hex_image     *image;
69         int                     pages;
70         int                     page;
71         uint32_t                base = address & ~0xff;
72         uint32_t                bound = (address + length + 0xff) & ~0xff;
73
74         image = calloc(sizeof (struct ao_hex_image) + (bound - base), 1);
75         image->address = base;
76         image->length = bound - base;
77         pages = image->length / 0x100;
78         for (page = 0; page < pages; page++)
79                 ao_self_block_read(cc, image->address + page * 0x100, image->data + page * 0x100);
80         return image;
81 }
82
83 bool
84 ao_self_write(struct cc_usb *cc, struct ao_hex_image *image)
85 {
86         uint8_t         block[256];
87         uint8_t         check[256];
88         uint32_t        base, bound, length, address;
89         uint32_t        pages;
90         uint32_t        page;
91
92         base = image->address & ~0xff;
93         bound = (image->address + image->length + 0xff) & ~0xff;
94
95         address = base;
96         length = bound - base;
97
98         pages = length / 0x100;
99         printf ("Write %08x %d pages: ", address, length/0x100); fflush(stdout);
100         for (page = 0; page < pages; page++) {
101                 uint32_t        start, stop;
102                 address = base + page * 0x100;
103
104                 if (address < image->address || address + 0x100 > image->address + image->length) {
105                         ao_self_block_read(cc, address, block);
106                 }
107                 start = address;
108                 stop = address + 0x100;
109                 if (start < image->address)
110                         start = image->address;
111                 if (stop > image->address + image->length)
112                         stop = image->address + image->length;
113                 memcpy(block + start - address, image->data + start - image->address, stop - start);
114                 ao_self_block_write(cc, address, block);
115                 ao_self_block_read(cc, address, check);
116                 if (memcmp(block, check, 0x100) != 0) {
117                         fprintf(stderr, "Block at 0x%08x doesn't match\n", address);
118                         return 0;
119                 }
120                 putchar('.'); fflush(stdout);
121         }
122         printf("done\n");
123         cc_usb_printf(cc,"a\n");
124         return 1;
125 }
126
127 /*
128  * Read a 16-bit value from the USB target
129  */
130
131 uint16_t
132 ao_self_get_uint16(struct cc_usb *cc, uint32_t addr)
133 {
134         struct ao_hex_image     *hex = ao_self_read(cc, addr, 2);
135         uint16_t                v;
136         uint8_t                 *data;
137
138         if (!hex)
139                 return 0;
140         data = hex->data + addr - hex->address;
141         v = data[0] | (data[1] << 8);
142         free(hex);
143         return v;
144 }
145
146 uint32_t
147 ao_self_get_uint32(struct cc_usb *cc, uint32_t addr)
148 {
149         struct ao_hex_image     *hex = ao_self_read(cc, addr, 4);
150         uint32_t                v;
151         uint8_t                 *data;
152
153         if (!hex)
154                 return 0;
155         data = hex->data + addr - hex->address;
156         v = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
157         free(hex);
158         return v;
159 }
160
161 bool
162 ao_self_get_usb_id(struct cc_usb *cc, struct ao_usb_id *id)
163 {
164         struct ao_hex_image     *hex;
165         bool                    ret;
166
167         if (!AO_USB_DESCRIPTORS)
168                 return false;
169
170         hex = ao_self_read(cc, AO_USB_DESCRIPTORS, 512);
171         if (!hex)
172                 return false;
173
174         ret = ao_heximage_usb_id(hex, id);
175         free(hex);
176         return ret;
177 }
178
179 uint16_t *
180 ao_self_get_usb_product(struct cc_usb *cc)
181 {
182         struct ao_hex_image     *hex;
183         uint16_t                *ret;
184
185         if (!AO_USB_DESCRIPTORS)
186                 return NULL;
187
188         hex = ao_self_read(cc, AO_USB_DESCRIPTORS, 512);
189         if (!hex)
190                 return NULL;
191
192         ret = ao_heximage_usb_product(hex);
193         free(hex);
194         return ret;
195 }
196