예제 #1
0
int main(int argc, char **argv)
{
	int can_write_bl = 0;
	int num_files = 0;
	int res;
	flash_file_t files[MAX_FILES];

	memset(files, 0, sizeof(files));

	if (argc < 2) {
		usage(argv[0]);
		return -1;
	}

	for (int i = 1; i < argc; i++) {
		if (argv[i][0] == '-') {
			if (!strcmp(argv[i], "-b")) {
				can_write_bl = 1;
			} else {
				usage(argv[0]);
				return -1;
			}
		} else {
			res = flash_load(&files[num_files], argv[i], can_write_bl);
			if (res < 0) {
				fprintf(stderr, "Error while loading %s\n", argv[i]);
				return -1;
			}
			fprintf(stderr, "\n");
			num_files++;
		}
	}

	usb_init();

	fprintf(stderr, "Waiting for Proxmark to appear on USB...");
	while (!OpenProxmark(0)) {
		sleep(1);
		fprintf(stderr, ".");
	}
	fprintf(stderr, " Found.\n");

	res = flash_start_flashing(can_write_bl);
	if (res < 0)
		return -1;

	fprintf(stderr, "\nFlashing...\n");

	for (int i = 0; i < num_files; i++) {
		res = flash_write(&files[i]);
		if (res < 0)
			return -1;
		flash_free(&files[i]);
		fprintf(stderr, "\n");
	}

	fprintf(stderr, "Resetting hardware...\n");

	res = flash_stop_flashing();
	if (res < 0)
		return -1;

	CloseProxmark();

	fprintf(stderr, "All done.\n\n");
	fprintf(stderr, "Have a nice day!\n");

	return 0;
}
예제 #2
0
파일: drumfish.c 프로젝트: cardoe/drumfish
int
main(int argc, char *argv[])
{
    const char *argv0 = argv[0];
    int exit_state = EXIT_FAILURE;
    char *env;
    struct drumfish_cfg config;
    struct sigaction act;
    int state = cpu_Limbo;
    int opt;
    char **flash_file = NULL;
    size_t flash_file_len = 0;
    long  port;

    config.mac = NULL;
    config.pflash = NULL;
    config.foreground = 1;
    config.verbose = 0;
    config.gdb = 0;
    config.erase_pflash = 0;
    config.peripherals[DF_PERIPHERAL_UART0] = strdup("off");
    config.peripherals[DF_PERIPHERAL_UART1] = strdup("on");

    while ((opt = getopt(argc, argv, "ef:p:m:vg:s:h")) != -1) {
        switch (opt) {
            case 'e':
                config.erase_pflash = 1;
                break;
            case 'f':
                /* Increment how many file names we need to keep track of */
                flash_file_len++;

                /* Set a reasonable max which is less than 2^30 to avoid
                 * overflowing realloc().
                 */
                if (flash_file_len > MAX_FLASH_FILES) {
                    fprintf(stderr, "Unable to load more than %d "
                            "firmware images at once.\n", MAX_FLASH_FILES);
                    exit(EXIT_FAILURE);
                }

                /* Make room for our next pointer */
                flash_file = realloc(flash_file,
                        sizeof(void *) * flash_file_len);
                if (!flash_file) {
                    fprintf(stderr, "Failed to allocate memory for "
                            "flash file list.\n");
                    exit(EXIT_FAILURE);
                }

                /* Now let's store our file name */
                flash_file[flash_file_len - 1] = strdup(optarg);
                if (!flash_file[flash_file_len - 1]) {
                    fprintf(stderr, "Failed to allocate memory for "
                            "flash file.\n");
                    exit(EXIT_FAILURE);
                }
                break;
            case 's':
                /* path to storage for flash */
                config.pflash = strdup(optarg);
                if (!config.pflash) {
                    fprintf(stderr, "Failed to allocate memory for "
                            "programmable flashh.\n");
                    exit(EXIT_FAILURE);
                }
                break;
            case 'm':
                config.mac = strdup(optarg);
                if (!config.mac) {
                    fprintf(stderr, "Failed to allocate memory for MAC.\n");
                    exit(EXIT_FAILURE);
                }
                break;
            case 'v':
               config.verbose++;
               break;
            case 'g':
               errno = 0;
               port = strtol(optarg, NULL, 10);
               if (errno != 0) {
                   fprintf(stderr, "Invalid supplied GDB port '%s': %s\n",
                           optarg, strerror(errno));
                   exit(EXIT_FAILURE);
               }

               if (port <= 1024 && port > UINT16_MAX) {
                   fprintf(stderr, "Invalid supplied GDB port %ld. "
                           "Must be 1024 < port <= %d\n", port, UINT16_MAX);
                   exit(EXIT_FAILURE);
               }

               config.gdb = port;
               break;
            case 'p':
               /* store requested port (UART) path */
               df_peripheral_parse(&config, optarg);
               break;
            case 'V':
               /* print version */
               break;
            case 'h':
               usage(argv0);
               exit(EXIT_SUCCESS);
               break;
            default: /* '?' */
                usage(argv0);
                exit(EXIT_FAILURE);
        }
    }

    /* Initialize our logging support */
    df_log_init(&config);

    /* If the user did not override the default location of the
     * programmable flash storage, then set the default
     */
    env = getenv("HOME");
    if (!env || !env[0]) {
        fprintf(stderr, "Unable to determine your HOME.\n");
        exit(EXIT_FAILURE);
    }

    if (asprintf(&config.pflash, "%s%s", env, DEFAULT_PFLASH_PATH) < 0) {
        fprintf(stderr, "Failed to allocate memory for pflash filename.\n");
        exit(EXIT_FAILURE);
    }

    printf("Programmable Flash Storage: %s\n", config.pflash);

    /* Handle the bare minimum signals */
    /* Yes I should use sigset_t here and use sigemptyset() */
    memset(&act, 0, sizeof(act));

    act.sa_handler = handler;
    if (sigaction(SIGHUP, &act, NULL) < 0) {
        fprintf(stderr, "Failed to install SIGHUP handler\n");
        exit(EXIT_FAILURE);
    }
    if (sigaction(SIGINT, &act, NULL) < 0) {
        fprintf(stderr, "Failed to install SIGINT handler\n");
        exit(EXIT_FAILURE);
    }
    if (sigaction(SIGTERM, &act, NULL) < 0) {
        fprintf(stderr, "Failed to install SIGTERM handler\n");
        exit(EXIT_FAILURE);
    }

    avr = m128rfa1_create(&config);
    if (!avr) {
        fprintf(stderr, "Unable to initialize requested board.\n");
        exit(EXIT_FAILURE);
    }

    /* Flash in any requested firmware, while cleaning up our memory */
    for (size_t i = 0; i < flash_file_len; i++) {
        if (flash_load(flash_file[i], avr->flash, avr->flashend + 1)) {
            fprintf(stderr, "Failed to load '%s' into flash.\n", flash_file[i]);
            exit(EXIT_FAILURE);
        }
        /* Don't need this memory anymore */
        free(flash_file[i]);
    }
    free(flash_file);

    /* Ensure the instruction we're about to execute is legit */
    if (avr->flash[avr->pc] == 0xff) {
        fprintf(stderr, "No firmware loaded in programmable flash, unable "
                "to boot.\n");
        fprintf(stderr, "Try using '-f firmware.hex' to supply one.\n");
        exit(EXIT_FAILURE);
    }

    /* If the user wants to run the core with GDB server enabled,
     * set that up.
     */
    if (config.gdb) {
        avr->gdb_port = config.gdb;
        /* Normally starting the CPU should be in limbo, but the
         * GDB code of simavr wants it to be stopped.
         */
        state = cpu_Stopped;

        avr_gdb_init(avr);
    }

    /* Capture the current time to be used as when our CPU started */
    df_log_start_time();

    df_log_msg(DF_LOG_INFO, "Booting CPU from 0x%x.\n", avr->pc);

    /* Our main event loop */
    for (;;) {
        state = avr_run(avr);

        if (state == cpu_Done) {
            exit_state = EXIT_SUCCESS;
            break;
        } else if (state == cpu_Crashed) {
            /* many firmwares disable interrupts and enable the watchdog
             * to cause the MCU to reboot. simavr treats that state as
             * cpu_Crashed
             */
            df_log_msg(DF_LOG_INFO, "CPU rebooted\n");
            avr_reset(avr);
        }
    }

    avr_terminate(avr);

    df_log_msg(DF_LOG_INFO, "Terminated.\n");

    free(config.pflash);

    return exit_state;
}