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