Switch from GPLv2 to GPLv2+
[fw/altos] / ao-tools / lib / ao-dfu.c
1 /*
2  * Copyright © 2016 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 <stdarg.h>
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdint.h>
26 #include <errno.h>
27 #include "ao-hex.h"
28 #include "ao-dfu.h"
29
30 static uint32_t dfu_crc;
31 static FILE     *dfu_file;
32 static int      dfu_failed;
33 static int      dfu_error;
34
35 static uint32_t update_crc(uint32_t crc, uint8_t byte)
36 {
37         int j;
38         uint32_t mask;
39
40         crc = crc ^ byte;
41         for (j = 0; j < 8; j++) {
42                 mask = -(crc & 1);
43                 crc = (crc >> 1) ^ (0xEDB88320 & mask);
44         }
45         return crc;
46 }
47
48 static void dfu_init(FILE *file)
49 {
50         dfu_crc = 0xffffffff;
51         dfu_file = file;
52         dfu_failed = 0;
53         dfu_error = 0;
54 }
55
56 static int dfu_fini(void)
57 {
58         if (fflush(dfu_file) == EOF) {
59                 if (!dfu_failed) {
60                         dfu_failed = 1;
61                         dfu_error = errno;
62                 }
63         }
64         if (dfu_failed)
65                 errno = dfu_error;
66         return !dfu_failed;
67 }
68
69 static void dfu_8(uint8_t byte) {
70         if (putc(byte, dfu_file) == EOF) {
71                 if (!dfu_failed) {
72                         dfu_failed = 1;
73                         dfu_error = errno;
74                 }
75         }
76         dfu_crc = update_crc(dfu_crc, byte);
77 }
78
79 static void dfu_pad(int len) {
80         while (len--)
81                 dfu_8(0);
82 }
83
84 static void dfu_string(char *string) {
85         char    c;
86
87         while ((c = *string++))
88                 dfu_8((uint8_t) c);
89 }
90
91 static void dfu_string_pad(char *string, int len) {
92         char    c;
93
94         while ((c = *string++)) {
95                 dfu_8((uint8_t) c);
96                 len--;
97         }
98         dfu_pad(len);
99 }
100
101 static void dfu_block(uint8_t *bytes, int len) {
102         while (len--)
103                 dfu_8(*bytes++);
104 }
105
106 static void dfu_lsb16(uint16_t value) {
107         dfu_8(value);
108         dfu_8(value>>8);
109 }
110
111 static void dfu_lsb32(uint32_t value) {
112         dfu_8(value);
113         dfu_8(value >> 8);
114         dfu_8(value >> 16);
115         dfu_8(value >> 24);
116 }
117
118 static uint32_t dfu_image_size(struct ao_hex_image *image) {
119         return 8 + image->length;
120 }
121
122 static uint32_t dfu_images_size(int num_image, struct ao_hex_image images[])
123 {
124         uint32_t        size = 0;
125         int             i;
126
127         for (i = 0; i < num_image; i++)
128                 size += dfu_image_size(&images[i]);
129         return size;
130 }
131
132 static void dfu_image(struct ao_hex_image *image)
133 {
134         dfu_lsb32(image->address);
135         dfu_lsb32(image->length);
136         dfu_block(image->data, image->length);
137 }
138
139 static void dfu_target(char *name, int num_image, struct ao_hex_image images[])
140 {
141         uint32_t        images_size = dfu_images_size(num_image, images);
142         int             i;
143
144         dfu_string("Target");
145         dfu_8(0);
146         if (name) {
147                 dfu_8(1);
148                 dfu_pad(3);
149                 dfu_string_pad(name, 255);
150         } else {
151                 dfu_8(0);
152                 dfu_pad(3);
153                 dfu_pad(255);
154         }
155         dfu_lsb32(images_size);
156         dfu_lsb32(num_image);
157         for (i = 0; i < num_image; i++)
158                 dfu_image(&images[i]);
159 }
160
161 static uint32_t dfu_target_size(int num_image, struct ao_hex_image images[])
162 {
163         return 274 + dfu_images_size(num_image, images);
164 }
165
166 static uint32_t
167 dfu_size(int num_image, struct ao_hex_image images[])
168 {
169         uint32_t        size = 0;
170         size += 11;     /* DFU Prefix */
171
172         size += dfu_target_size(num_image, images);
173
174         return size;
175 }
176
177 int
178 ao_dfu_write(FILE *file, struct ao_dfu_info *info, int num_image, struct ao_hex_image images[])
179 {
180         uint32_t        total_size;
181
182         total_size = dfu_size(num_image, images);
183
184         dfu_init(file);
185         /* DFU Prefix */
186         dfu_string(DFU_SIGNATURE);
187         dfu_8(0x01);
188         dfu_lsb32(total_size);
189         dfu_8(0x01);
190
191         dfu_target("ST...", num_image, images);
192
193         /* DFU Suffix */
194         dfu_lsb16(info->bcdDevice);
195         dfu_lsb16(info->idProduct);
196         dfu_lsb16(info->idVendor);
197         dfu_lsb16(DFU_SPEC_VERSION);
198         dfu_string("UFD");
199         dfu_8(16);
200         dfu_lsb32(dfu_crc);
201         return dfu_fini();
202 }
203