ao-usbload: Check target device name to avoid mis-flashing
[fw/altos] / ao-tools / lib / ao-editaltos.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 <string.h>
20 #include <stdlib.h>
21 #include "ao-editaltos.h"
22
23 struct ao_sym ao_symbols[] = {
24         [AO_ROMCONFIG_VERSION_INDEX] = {
25                 .name = "ao_romconfig_version",
26                 .required = 1
27         },
28         [AO_ROMCONFIG_CHECK_INDEX] = {
29                 .name = "ao_romconfig_check",
30                 .required = 1
31         },
32         [AO_SERIAL_NUMBER_INDEX] = {
33                 .name = "ao_serial_number",
34                 .required = 1
35         },
36         [AO_RADIO_CAL_INDEX] = {
37                 .name = "ao_radio_cal",
38                 .required = 0
39         },
40         [AO_USB_DESCRIPTORS_INDEX] = {
41                 .name = "ao_usb_descriptors",
42                 .required = 0
43         },
44 };
45
46 #define NUM_SYMBOLS             5
47
48 int ao_num_symbols = NUM_SYMBOLS;
49
50 /*
51  * Edit the to-be-written memory block
52  */
53 static bool
54 rewrite(struct ao_hex_image *load, unsigned address, uint8_t *data, int length)
55 {
56         int             i;
57
58         if (address < load->address || load->address + load->length < address + length)
59                 return false;
60
61         memcpy(load->data + address - load->address, data, length);
62         return true;
63 }
64
65 /*
66  * Find the symbols needed to correctly load the program
67  */
68
69 bool
70 ao_editaltos_find_symbols(struct ao_sym *file_symbols, int num_file_symbols,
71                           struct ao_sym *symbols, int num_symbols)
72 {
73         int     f, s;
74
75         for (f = 0; f < num_file_symbols; f++) {
76                 for (s = 0; s < num_symbols; s++) {
77                         if (strcmp(symbols[s].name, file_symbols[f].name) == 0) {
78                                 symbols[s].addr = file_symbols[f].addr;
79                                 symbols[s].found = true;
80                         }
81                 }
82         }
83         for (s = 0; s < num_symbols; s++)
84                 if (!symbols[s].found && symbols[s].required)
85                         return false;
86         return true;
87 }
88
89 bool
90 ao_editaltos(struct ao_hex_image *image,
91              uint16_t serial,
92              uint32_t cal)
93 {
94         uint8_t                 *serial_ucs2;
95         int                     serial_ucs2_len;
96         uint8_t                 serial_int[2];
97         unsigned int            s;
98         int                     i;
99         int                     string_num;
100         uint8_t                 cal_int[4];
101
102         /* Write the config values into the flash image
103          */
104
105         serial_int[0] = serial & 0xff;
106         serial_int[1] = (serial >> 8) & 0xff;
107
108         if (!rewrite(image, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
109                 fprintf(stderr, "Cannot rewrite serial integer at %08x\n",
110                         AO_SERIAL_NUMBER);
111                 return false;
112         }
113
114         if (AO_USB_DESCRIPTORS) {
115                 uint32_t        usb_descriptors = AO_USB_DESCRIPTORS - image->address;
116                 string_num = 0;
117
118                 while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) {
119                         if (image->data[usb_descriptors+1] == AO_USB_DESC_STRING) {
120                                 ++string_num;
121                                 if (string_num == 4)
122                                         break;
123                         }
124                         usb_descriptors += image->data[usb_descriptors];
125                 }
126                 if (usb_descriptors >= image->length || image->data[usb_descriptors] == 0 ) {
127                         fprintf(stderr, "Cannot rewrite serial string at %08x\n", AO_USB_DESCRIPTORS);
128                         return false;
129                 }
130
131                 serial_ucs2_len = image->data[usb_descriptors] - 2;
132                 serial_ucs2 = malloc(serial_ucs2_len);
133                 if (!serial_ucs2) {
134                         fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
135                         return false;
136                 }
137                 s = serial;
138                 for (i = serial_ucs2_len / 2; i; i--) {
139                         serial_ucs2[i * 2 - 1] = 0;
140                         serial_ucs2[i * 2 - 2] = (s % 10) + '0';
141                         s /= 10;
142                 }
143                 if (!rewrite(image, usb_descriptors + 2 + image->address, serial_ucs2, serial_ucs2_len)) {
144                         fprintf (stderr, "Cannot rewrite USB descriptor at %08x\n", AO_USB_DESCRIPTORS);
145                         return false;
146                 }
147         }
148
149         if (cal && AO_RADIO_CAL) {
150                 cal_int[0] = cal & 0xff;
151                 cal_int[1] = (cal >> 8) & 0xff;
152                 cal_int[2] = (cal >> 16) & 0xff;
153                 cal_int[3] = (cal >> 24) & 0xff;
154
155                 if (!rewrite(image, AO_RADIO_CAL, cal_int, sizeof (cal_int))) {
156                         fprintf(stderr, "Cannot rewrite radio calibration at %08x\n", AO_RADIO_CAL);
157                         return false;
158                 }
159         }
160         return true;
161 }
162
163 static uint16_t
164 read_le16(uint8_t *src)
165 {
166         return (uint16_t) src[0] | ((uint16_t) src[1] << 8);
167 }
168
169 bool
170 ao_heximage_usb_id(struct ao_hex_image *image, struct ao_usb_id *id)
171 {
172         uint32_t        usb_descriptors;
173
174         if (!AO_USB_DESCRIPTORS)
175                 return false;
176         usb_descriptors = AO_USB_DESCRIPTORS - image->address;
177
178         while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) {
179                 if (image->data[usb_descriptors+1] == AO_USB_DESC_DEVICE) {
180                         break;
181                 }
182                 usb_descriptors += image->data[usb_descriptors];
183         }
184
185         /*
186          * check to make sure there's at least 0x12 (size of a USB
187          * device descriptor) available
188          */
189         if (usb_descriptors >= image->length || image->data[usb_descriptors] != 0x12)
190                 return false;
191
192         id->vid = read_le16(image->data + usb_descriptors + 8);
193         id->pid = read_le16(image->data + usb_descriptors + 10);
194
195         return true;
196 }
197
198 uint16_t *
199 ao_heximage_usb_product(struct ao_hex_image *image)
200 {
201         uint32_t        usb_descriptors;
202         int             string_num;
203         uint16_t        *product;
204         uint8_t         product_len;
205
206         if (!AO_USB_DESCRIPTORS)
207                 return NULL;
208         usb_descriptors = AO_USB_DESCRIPTORS - image->address;
209
210         string_num = 0;
211         while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) {
212                 if (image->data[usb_descriptors+1] == AO_USB_DESC_STRING) {
213                         ++string_num;
214                         if (string_num == 3)
215                                 break;
216                 }
217                 usb_descriptors += image->data[usb_descriptors];
218         }
219
220         /*
221          * check to make sure there's at least 0x12 (size of a USB
222          * device descriptor) available
223          */
224         if (usb_descriptors >= image->length || image->data[usb_descriptors] == 0)
225                 return NULL;
226
227         product_len = image->data[usb_descriptors] - 2;
228
229         if (usb_descriptors < product_len + 2)
230                 return NULL;
231
232         product = malloc (product_len + 2);
233         if (!product)
234                 return NULL;
235
236         memcpy(product, image->data + usb_descriptors + 2, product_len);
237         product[product_len/2] = 0;
238         return product;
239 }