ao-tools: Create general elf and hex library routines
[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 int     ao_self_verbose;
30
31 #define TRACE(...) if (ao_self_verbose) printf (__VA_ARGS__)
32
33 void
34 ao_self_block_read(struct cc_usb *cc, uint32_t address, uint8_t block[256])
35 {
36         int                     byte;
37         cc_usb_sync(cc);
38         cc_usb_printf(cc, "R %x\n", address);
39         for (byte = 0; byte < 0x100; byte++) {
40                 block[byte] = cc_usb_getchar(cc);
41         }
42         TRACE ("\nread %08x\n", address);
43         for (byte = 0; byte < 0x100; byte++) {
44                 TRACE (" %02x", block[byte]);
45                 if ((byte & 0xf) == 0xf)
46                         TRACE ("\n");
47         }
48 }
49
50 void
51 ao_self_block_write(struct cc_usb *cc, uint32_t address, uint8_t block[256])
52 {
53         int                     byte;
54         cc_usb_sync(cc);
55         cc_usb_printf(cc, "W %x\n", address);
56         TRACE ("write %08x\n", address);
57         for (byte = 0; byte < 0x100; byte++) {
58                 TRACE (" %02x", block[byte]);
59                 if ((byte & 0xf) == 0xf)
60                         TRACE ("\n");
61         }
62         for (byte = 0; byte < 0x100; byte++) {
63                 cc_usb_printf(cc, "%c", block[byte]);
64         }
65 }
66
67 struct ao_hex_image *
68 ao_self_read(struct cc_usb *cc, uint32_t address, uint32_t length)
69 {
70         struct ao_hex_image     *image;
71         int                     pages;
72         int                     page;
73         uint32_t                base = address & ~0xff;
74         uint32_t                bound = (address + length + 0xff) & ~0xff;
75
76         image = calloc(sizeof (struct ao_hex_image) + (bound - base), 1);
77         image->address = base;
78         image->length = bound - base;
79         pages = image->length / 0x100;
80         for (page = 0; page < pages; page++)
81                 ao_self_block_read(cc, image->address + page * 0x100, image->data + page * 0x100);
82         return image;
83 }
84
85 int
86 ao_self_write(struct cc_usb *cc, struct ao_hex_image *image)
87 {
88         uint8_t         block[256];
89         uint8_t         check[256];
90         uint32_t        base, bound, length, address;
91         uint32_t        pages;
92         uint32_t        page;
93
94         base = image->address & ~0xff;
95         bound = (image->address + image->length + 0xff) & ~0xff;
96
97         address = base;
98         length = bound - base;
99
100         pages = length / 0x100;
101         printf ("Write %08x %d pages: ", address, length/0x100); fflush(stdout);
102         for (page = 0; page < pages; page++) {
103                 uint32_t        start, stop;
104                 address = base + page * 0x100;
105
106                 if (address < image->address || address + 0x100 > image->address + image->length) {
107                         ao_self_block_read(cc, address, block);
108                 }
109                 start = address;
110                 stop = address + 0x100;
111                 if (start < image->address)
112                         start = image->address;
113                 if (stop > image->address + image->length)
114                         stop = image->address + image->length;
115                 memcpy(block + start - address, image->data + start - image->address, stop - start);
116                 ao_self_block_write(cc, address, block);
117                 ao_self_block_read(cc, address, check);
118                 if (memcmp(block, check, 0x100) != 0) {
119                         fprintf(stderr, "Block at 0x%08x doesn't match\n", address);
120                         return 0;
121                 }
122                 putchar('.'); fflush(stdout);
123         }
124         printf("done\n");
125         cc_usb_printf(cc,"a\n");
126         return 1;
127 }