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