Ejemplo n.º 1
0
/*
 *  Log the specified message.
 *
 *  @param char *message The message to log.
 *  @return void
 */
void put_to_log(char *message) {

  // If the logging type is "0", we just print to STDOUT.
  if (logging_type == 0) {
    print_to_stdout(message);
  }

  // If it's "1", we write to a file.
  else if (logging_type == 1) {
    print_to_file(message, log_file_path);
  }

}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
	int read, width, fd;
	unsigned long addr, val, count;
	off_t mofs;
	size_t mlen;
	void *maddr;
	int i;

	if (argc < 4) {
	printf(	"mem: Access memory/register space using /dev/mem\n"\
		"usage: mem [r|w|R|W] [b|w|l] [addr] [rcount/wvalue] [wcount]\n"\
		"        r: Read memory. rcount is optional (defaults to 0x40)\n"\
		"        w: Write memory. wcount is optional (defaults to 1)\n"\
		"        R: Read memory and ouput raw data to stdout\n"\
		"        W: Write to memory, but get raw data from stdin\n"\
		);
		return -1;
	}

	if (strcmp(argv[1], "r") == 0) {
		read = 1;
	} else if (strcmp(argv[1], "w") == 0) {
		read = 0;
	} else if (strcmp(argv[1], "R") == 0) {
		read = 3;
	} else if (strcmp(argv[1], "W") == 0) {
		read = 2;
	} else {
		printf("Invalid command: %s\n", argv[1]);
		return -1;
	}

	if (strcmp(argv[2], "b") == 0) {
		width = 1;
	} else if (strcmp(argv[2], "w") == 0) {
		width = 2;
	} else if (strcmp(argv[2], "l") == 0) {
		width = 4;
	} else {
		printf("Invalid width: %s\n", argv[2]);
		return -1;
	}

	sscanf(argv[3], "%lx", &addr);


	if (addr % width != 0) {
		printf("Address not aligned on %d-bit boundary\n",
				width * 8);
		return -1;
	}

	if (read & 1) {	/* 'r' and 'R' */
		if (argc == 5)
			sscanf(argv[4], "%lx", &count);
		else
			count = 0x40;
	}
	if (read == 0 ) { /* 'w' */
		if (argc < 5) {
			printf("ERROR: Missing write value.\n");
			return -1;
		}
		sscanf(argv[4], "%lx", &val);

		if (argc == 6)
			sscanf(argv[5], "%lx", &count);
		else
			count = 1;
	}

	fd = open("/dev/mem", O_RDWR);
	if (fd == -1) {
		perror("/dev/mem");
		return -1;
	}

	/* offset must be on page boundary */
	mofs = addr & ~(PAGE_SIZE - 1);

	/* length must be a multiple of page size */
	mlen = (addr - mofs + count + PAGE_SIZE - 1) &
			~(PAGE_SIZE - 1);

	/* 'r' */
	if (read == 1) {
		maddr = mmap(NULL, mlen, PROT_READ, MAP_SHARED, fd, mofs);
		if (maddr == MAP_FAILED)
			goto done;
		print_buffer((unsigned long)(maddr + (addr & (PAGE_SIZE - 1))),
				count, width, addr);
	}
	/* 'w' */
	else if (read == 0) {
		maddr = mmap(NULL, mlen, PROT_WRITE, MAP_SHARED, fd, mofs);
		if (maddr == MAP_FAILED)
			goto done;
		write_buffer((unsigned long)(maddr + (addr & (PAGE_SIZE - 1))),
				val, count, width);
	}
	/* 'R' */
	else if (read == 3) {
		maddr = mmap(NULL, mlen, PROT_READ, MAP_SHARED, fd, mofs);
		if (maddr == MAP_FAILED)
			goto done;
		/* output to sdtio */
		print_to_stdout((unsigned long)(maddr + (addr & (PAGE_SIZE - 1))),
				count, width);
	}
	/* 'W' */
	else if (read == 0) {
		maddr = mmap(NULL, mlen, PROT_WRITE, MAP_SHARED, fd, mofs);
		if (maddr == MAP_FAILED)
			goto done;
		write_from_stdin((unsigned long)(maddr + (addr & (PAGE_SIZE - 1))),
				count, width);
	}

	munmap(maddr, mlen);
done:
	close(fd);
}