ao-tools/ao-stmload: Fix ELF, add IHX, add self-flashing
[fw/altos] / ao-tools / ao-stmload / ao-selfload.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 <stdio.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <sysexits.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include "cc.h"
25 #include "cc-usb.h"
26 #include "ccdbg.h"
27 #include "ao-stmload.h"
28
29 void
30 ao_self_block_read(struct cc_usb *cc, uint32_t address, uint8_t block[256])
31 {
32         int                     byte;
33         cc_usb_sync(cc);
34         cc_usb_printf(cc, "R %x\n", address);
35 //      printf ("read %08x\n", address);
36         for (byte = 0; byte < 0x100; byte++) {
37                 block[byte] = cc_usb_getchar(cc);
38 //              printf (" %02x", block[byte]);
39 //              if ((byte & 0xf) == 0xf)
40 //                      printf ("\n");
41         }
42 }
43
44 void
45 ao_self_block_write(struct cc_usb *cc, uint32_t address, uint8_t block[256])
46 {
47         int                     byte;
48         cc_usb_sync(cc);
49         cc_usb_printf(cc, "W %x\n", address);
50 //      printf ("write %08x\n", address);
51         for (byte = 0; byte < 0x100; byte++) {
52                 cc_usb_printf(cc, "%c", block[byte]);
53 //              printf (" %02x", block[byte]);
54 //              if ((byte & 0xf) == 0xf)
55 //                      printf ("\n");
56         }
57 }
58
59 struct hex_image *
60 ao_self_read(struct cc_usb *cc, uint32_t address, uint32_t length)
61 {
62         struct hex_image        *image;
63         int                     pages;
64         int                     page;
65         uint32_t                base = address & ~0xff;
66         uint32_t                bound = (address + length + 0xff) & ~0xff;
67
68         image = calloc(sizeof (struct hex_image) + (bound - base), 1);
69         image->address = base;
70         image->length = bound - base;
71         pages = image->length / 0x100;
72         for (page = 0; page < pages; page++)
73                 ao_self_block_read(cc, image->address + page * 0x100, image->data + page * 0x100);
74         return image;
75 }
76
77 int
78 ao_self_write(struct cc_usb *cc, struct hex_image *image)
79 {
80         uint8_t         block[256];
81         uint8_t         check[256];
82         uint32_t        base, bound, length, address;
83         uint32_t        pages;
84         uint32_t        page;
85
86         base = image->address & ~0xff;
87         bound = (image->address + image->length + 0xff) & ~0xff;
88
89         address = base;
90         length = bound - base;
91
92         pages = length / 0x100;
93         printf ("Write %08x %d pages: ", address, length/0x100); fflush(stdout);
94         for (page = 0; page < pages; page++) {
95                 uint32_t        start, stop;
96                 address = base + page * 0x100;
97
98                 if (address < image->address || address + 0x100 > image->address + image->length) {
99                         ao_self_block_read(cc, address, block);
100                 }
101                 start = address;
102                 stop = address + 0x100;
103                 if (start < image->address)
104                         start = image->address;
105                 if (stop > image->address + image->length)
106                         stop = image->address + image->length;
107                 memcpy(block + start - address, image->data + start - image->address, stop - start);
108                 ao_self_block_write(cc, address, block);
109                 ao_self_block_read(cc, address, check);
110                 if (memcmp(block, check, 0x100) != 0) {
111                         fprintf(stderr, "Block at 0x%08x doesn't match\n", address);
112                         return 0;
113                 }
114                 putchar('.'); fflush(stdout);
115         }
116         printf("done\n");
117         return 1;
118 }