Skip to content

Instantly share code, notes, and snippets.

@ihewitt
Created October 7, 2020 16:34
Show Gist options
  • Save ihewitt/5969b7d427fc7248306cb894ec20cace to your computer and use it in GitHub Desktop.
Save ihewitt/5969b7d427fc7248306cb894ec20cace to your computer and use it in GitHub Desktop.
A9G HST dump
//
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
static int serial_port;
void initSerial() {
struct termios tty;
cfsetispeed(&tty, B921600);
cfsetospeed(&tty, B921600);
// Coolhost flags:
// cflag 18b7, iflag 1401, lflag 0ab0, oflag 0004
tty.c_cflag &= ~PARENB; // no parity
tty.c_cflag &= ~CSTOPB; // 1 stop
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8bits
tty.c_cflag &= ~CRTSCTS; // no flow ctrl
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;
tty.c_cc[VTIME] = 5;
tty.c_cc[VMIN] = 0;
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
printf("Error %i\n", errno);
}
}
uint8_t count = 1;
// read data from address addr at offset off, into buffer for size bytes.
//
bool getData(uint32_t addr, int off, uint8_t *buffer, int size) {
uint8_t buf[8];
uint8_t req[11];
// Send a block of read requests for the size asked for.
for (int num = 0; num < size; num += 4) // word at a time
{
req[0] = 0xad;
req[1] = 0x00;
req[2] = 0x07;
req[3] = 0xff;
req[4] = 0x02;
*(uint32_t *)(&req[5]) = addr + off + num;
req[9] = count++; // Request count
if (count == 0xff)
count = 1;
uint8_t ck = 0;
for (int i = 0; i < 7; ++i)
ck ^= req[i + 3];
req[10] = ck; // xor checksum
tcdrain(serial_port); // Wait to send
write(serial_port, req, sizeof(req));
}
tcdrain(serial_port); // Wait to send
int n = 0;
uint8_t block[4096];
// usleep((8 + 25) * 100);
while (int nread = read(serial_port, block + n, 256)) {
// usleep((8 + 25) * 100);
n += nread;
}
printf("Read %d\n", n);
uint8_t read_buf[256];
int p = 0;
do {
if (*(uint32_t *)(block + p) == 0xff0600ad) {
/* count
* | / bytes \
* | | | /chksum
* AD 00 06 FF 67 60 F1 E0 99 70
*/
uint8_t *req = (block + p);
// check count. check checksum.
uint8_t ck = 0;
for (int i = 0; i < 6; ++i)
ck ^= req[i + 3];
if (ck != req[9]) {
printf("Check mismatch: %02x %02x\n", ck, req[9]);
printf("Data: ");
for (int i = 0; i < 10; ++i) {
printf("%02x", req[i]);
}
printf("\n");
}
*(uint32_t *)(buffer) = *(uint32_t *)(req + 5);
buffer += 4;
p += 10;
} else {
p++;
}
} while (p < n);
return true;
}
int main() {
serial_port = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_SYNC);
if (serial_port < 0) {
fprintf(stderr, "Unable to open ttyUSB\n");
_exit(-1);
}
initSerial();
// Clear any startup info waiting.
tcflush(serial_port, TCIOFLUSH);
uint32_t addr = 0xa8240000;
int size = 1024; // get 1k
printf("\nData from 0x%08x to 0x%08x\n", addr, addr + size);
uint8_t buffer[size];
// Run through memory, get 16 words a time
for (int word = 0; word < size / 256; word++) {
getData(addr, 0, buffer + word * 256, 256);
}
printf("Buffer:\n");
for (int i = 0; i < size; i += 32) {
for (int c = 0; c < 32; c++) {
printf("%02x", buffer[i + c]);
}
printf(" ");
for (int c = 0; c < 32; c++) {
if (buffer[i + c] > 0x30 && buffer[i + c] < 0x7f)
printf("%c", buffer[i + c]);
else
printf(".");
}
printf("\n");
}
close(serial_port);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment