- /* Verify that the loaded image fits entirely within device flash
- */
- if (load->address < sl->flash_base ||
- sl->flash_base + sl->flash_size < load->address + load->length) {
- fprintf (stderr, "\%s\": Invalid memory range 0x%08x - 0x%08x\n", filename,
- load->address, load->address + load->length);
- done(sl, NULL, 1);
- }
-
- /* Enter debugging mode
- */
- if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
- stlink_exit_dfu_mode(sl);
-
- if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
- stlink_enter_swd_mode(sl);
- } else {
- int is_loader;
- int tries;
-
- for (tries = 0; tries < 3; tries++) {
- char *this_tty = tty;
- if (!this_tty)
- this_tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
- if (!this_tty)
- this_tty = cc_usbdevs_find_by_arg(device, "MegaMetrum");
- if (!this_tty)
- this_tty = getenv("ALTOS_TTY");
- if (!this_tty)
- this_tty="/dev/ttyACM0";
-
- cc = cc_usb_open(this_tty);
-
- if (!cc)
- exit(1);
- cc_usb_printf(cc, "v\n");
- is_loader = 0;
- for (;;) {
- char line[256];
- cc_usb_getline(cc, line, sizeof(line));
- if (!strncmp(line, "altos-loader", 12))
- is_loader = 1;
- if (!strncmp(line, "software-version", 16))
- break;
- }
- if (is_loader)
- break;
- printf ("rebooting to loader\n");
- cc_usb_printf(cc, "X\n");
- cc_usb_close(cc);
- sleep(1);
- cc = NULL;
- }
- if (!is_loader) {
- fprintf(stderr, "Cannot switch to boot loader\n");
- exit(1);
- }
-#if 0
- {
- uint8_t check[256];
- int i = 0;
-
- ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, check);
- for (;;) {
- uint8_t block[256];
- putchar ('.');
- if (++i == 40) {
- putchar('\n');
- i = 0;
- }
- fflush(stdout);
- ao_self_block_write(cc, AO_BOOT_APPLICATION_BASE, block);
- ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, block);
- if (memcmp(block, check, 256) != 0) {
- fprintf (stderr, "read differed\n");
- exit(1);
- }
- }
- }
-#endif