Example #1
0
int cmd_mw(char **arg)
{
	char *off_text = get_arg(arg);
	char *byte_text;
	address_t offset = 0;
	address_t length = 0;
	uint8_t buf[1024];

	if (!off_text) {
		printc_err("md: offset must be specified\n");
		return -1;
	}

	if (expr_eval(off_text, &offset) < 0) {
		printc_err("md: can't parse offset: %s\n", off_text);
		return -1;
	}

	while ((byte_text = get_arg(arg))) {
		if (length >= sizeof(buf)) {
			printc_err("md: maximum length exceeded\n");
			return -1;
		}

		buf[length++] = strtoul(byte_text, NULL, 16);
	}

	if (!length)
		return 0;

	if (device_writemem(offset, buf, length) < 0)
		return -1;

	return 0;
}
Example #2
0
int prog_flush(struct prog_data *prog)
{
	if (!prog->len)
		return 0;

	if (!prog->have_erased && (prog->flags & PROG_WANT_ERASE)) {
		printc("Erasing...\n");
		if (device_erase(DEVICE_ERASE_MAIN, 0) < 0)
			return -1;

		printc("Programming...\n");
		prog->have_erased = 1;
	}

	printc_dbg("%s %4d bytes at %04x",
		   (prog->flags & PROG_VERIFY) ? "Verifying" : "Writing",
		   prog->len, prog->addr);
	if (prog->section[0])
		printc_dbg(" [section: %s]", prog->section);
	printc_dbg("...\n");

	if (prog->flags & PROG_VERIFY) {
		uint8_t cmp_buf[PROG_BUFSIZE];
		int i;

		if (device_readmem(prog->addr, cmp_buf, prog->len) < 0)
			return -1;

		for (i = 0; i < prog->len; i++)
			if (cmp_buf[i] != prog->buf[i]) {
				printc("\x1b[1mERROR:\x1b[0m "
				       "mismatch at %04x (read %02x, "
				       "expected %02x)\n",
				       prog->addr + i,
				       cmp_buf[i], prog->buf[i]);
				return -1;
			}
	} else {
		if (device_writemem(prog->addr, prog->buf, prog->len) < 0)
			return -1;
	}

	prog->total_written += prog->len;
	prog->addr += prog->len;
	prog->len = 0;
	return 0;
}
Example #3
0
static int write_memory(struct gdb_data *data, char *text)
{
	char *data_text = strchr(text, ':');
	char *length_text = strchr(text, ',');
	address_t length, addr;
	uint8_t buf[GDB_MAX_XFER];
	int buflen = 0;

	if (!(data_text && length_text)) {
		printc_err("gdb: malformed memory write request\n");
		return gdb_send(data, "E00");
	}

	*(data_text++) = 0;
	*(length_text++) = 0;

	length = strtoul(length_text, NULL, 16);
	addr = strtoul(text, NULL, 16);

	while (buflen < sizeof(buf) && *data_text && data_text[1]) {
		buf[buflen++] = (hexval(data_text[0]) << 4) |
			hexval(data_text[1]);
		data_text += 2;
	}

	if (buflen != length) {
		printc_err("gdb: length mismatch\n");
		return gdb_send(data, "E00");
	}

	printc("Writing %4d bytes to 0x%04x\n", length, addr);

	if (device_writemem(addr, buf, buflen) < 0)
		return gdb_send(data, "E00");

	return gdb_send(data, "OK");
}