5f7708fd2f8c93ac4b1d8da40fefc0baaa71b2ca
[fw/altos] / ao-tools / ao-rawload / ao-rawload.c
1 /*
2  * Copyright © 2008 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 "ccdbg.h"
20
21 int
22 main (int argc, char **argv)
23 {
24         struct ccdbg    *dbg;
25         uint8_t         status;
26         uint16_t        pc;
27         struct hex_file *hex;
28         struct hex_image *image;
29         char *filename;
30         FILE *file;
31
32         filename = argv[1];
33         if (filename == NULL) {
34                 fprintf(stderr, "usage: %s <filename.ihx>\n", argv[0]);
35                 exit(1);
36         }
37         file = fopen(filename, "r");
38         if (!file) {
39                 perror(filename);
40                 exit(1);
41         }
42         hex = ccdbg_hex_file_read(file, filename);
43         fclose(file);
44         if (!hex)
45                 exit (1);
46         image = ccdbg_hex_image_create(hex);
47         if (!image) {
48                 fprintf(stderr, "image create failed\n");
49                 exit (1);
50         }
51
52         ccdbg_hex_file_free(hex);
53         dbg = ccdbg_open();
54         if (!dbg)
55                 exit (1);
56
57         ccdbg_add_debug(CC_DEBUG_FLASH);
58
59         ccdbg_debug_mode(dbg);
60         ccdbg_halt(dbg);
61         if (image->address == 0xf000) {
62                 printf("Loading %d bytes to execute from RAM\n",
63                        image->length);
64                 ccdbg_write_hex_image(dbg, image, 0);
65         } else if (image->address == 0x0000) {
66                 printf("Loading %d bytes to execute from FLASH\n",
67                        image->length);
68                 ccdbg_flash_hex_image(dbg, image);
69         } else {
70                 printf("Cannot load code to 0x%04x\n",
71                        image->address);
72                 ccdbg_hex_image_free(image);
73                 ccdbg_close(dbg);
74                 exit(1);
75         }
76         ccdbg_set_pc(dbg, image->address);
77         ccdbg_resume(dbg);
78         ccdbg_close(dbg);
79         exit (0);
80 }