ao-tools: Clean up ao-sym structure an initializers
[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; 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 <string.h>
19 #include <stdlib.h>
20 #include "ao-editaltos.h"
21
22 struct ao_sym ao_symbols[] = {
23         {
24                 .name = "ao_romconfig_version",
25                 .required = 1
26         },
27         {
28                 .name = "ao_romconfig_check",
29                 .required = 1
30         },
31         {
32                 .name = "ao_serial_number",
33                 .required = 1
34         },
35         {
36                 .name = "ao_radio_cal",
37                 .required = 0
38         },
39         {
40                 .name = "ao_usb_descriptors",
41                 .required = 0
42         },
43 };
44
45 #define NUM_SYMBOLS             5
46
47 int ao_num_symbols = NUM_SYMBOLS;
48
49 /*
50  * Edit the to-be-written memory block
51  */
52 static bool
53 rewrite(struct ao_hex_image *load, unsigned address, uint8_t *data, int length)
54 {
55         int             i;
56
57         if (address < load->address || load->address + load->length < address + length)
58                 return false;
59
60         printf("rewrite %04x:", address);
61         for (i = 0; i < length; i++)
62                 printf (" %02x", load->data[address - load->address + i]);
63         printf(" ->");
64         for (i = 0; i < length; i++)
65                 printf (" %02x", data[i]);
66         printf("\n");
67         memcpy(load->data + address - load->address, data, length);
68         return true;
69 }
70
71 /*
72  * Find the symbols needed to correctly load the program
73  */
74
75 bool
76 ao_editaltos_find_symbols(struct ao_sym *file_symbols, int num_file_symbols,
77                           struct ao_sym *symbols, int num_symbols)
78 {
79         int     f, s;
80
81         for (f = 0; f < num_file_symbols; f++) {
82                 for (s = 0; s < num_symbols; s++) {
83                         if (strcmp(symbols[s].name, file_symbols[f].name) == 0) {
84                                 symbols[s].addr = file_symbols[f].addr;
85                                 symbols[s].found = true;
86                         }
87                 }
88         }
89         for (s = 0; s < num_symbols; s++)
90                 if (!symbols[s].found && symbols[s].required)
91                         return false;
92         return true;
93 }
94
95 bool
96 ao_editaltos(struct ao_hex_image *image,
97              uint16_t serial,
98              uint32_t cal)
99 {
100         uint8_t                 *serial_ucs2;
101         int                     serial_ucs2_len;
102         uint8_t                 serial_int[2];
103         unsigned int            s;
104         int                     i;
105         int                     string_num;
106         uint8_t                 cal_int[4];
107
108         /* Write the config values into the flash image
109          */
110
111         serial_int[0] = serial & 0xff;
112         serial_int[1] = (serial >> 8) & 0xff;
113
114         if (!rewrite(image, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
115                 fprintf(stderr, "Cannot rewrite serial integer at %08x\n",
116                         AO_SERIAL_NUMBER);
117                 return false;
118         }
119
120         if (AO_USB_DESCRIPTORS) {
121                 uint32_t        usb_descriptors = AO_USB_DESCRIPTORS - image->address;
122                 string_num = 0;
123
124                 while (image->data[usb_descriptors] != 0 && usb_descriptors < image->length) {
125                         if (image->data[usb_descriptors+1] == AO_USB_DESC_STRING) {
126                                 ++string_num;
127                                 if (string_num == 4)
128                                         break;
129                         }
130                         usb_descriptors += image->data[usb_descriptors];
131                 }
132                 if (usb_descriptors >= image->length || image->data[usb_descriptors] == 0 ) {
133                         fprintf(stderr, "Cannot rewrite serial string at %08x\n", AO_USB_DESCRIPTORS);
134                         return false;
135                 }
136
137                 serial_ucs2_len = image->data[usb_descriptors] - 2;
138                 serial_ucs2 = malloc(serial_ucs2_len);
139                 if (!serial_ucs2) {
140                         fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
141                         return false;
142                 }
143                 s = serial;
144                 for (i = serial_ucs2_len / 2; i; i--) {
145                         serial_ucs2[i * 2 - 1] = 0;
146                         serial_ucs2[i * 2 - 2] = (s % 10) + '0';
147                         s /= 10;
148                 }
149                 if (!rewrite(image, usb_descriptors + 2 + image->address, serial_ucs2, serial_ucs2_len)) {
150                         fprintf (stderr, "Cannot rewrite USB descriptor at %08x\n", AO_USB_DESCRIPTORS);
151                         return false;
152                 }
153         }
154
155         if (cal && AO_RADIO_CAL) {
156                 cal_int[0] = cal & 0xff;
157                 cal_int[1] = (cal >> 8) & 0xff;
158                 cal_int[2] = (cal >> 16) & 0xff;
159                 cal_int[3] = (cal >> 24) & 0xff;
160
161                 if (!rewrite(image, AO_RADIO_CAL, cal_int, sizeof (cal_int))) {
162                         fprintf(stderr, "Cannot rewrite radio calibration at %08x\n", AO_RADIO_CAL);
163                         return false;
164                 }
165         }
166         return true;
167 }