Commit dd3a5120 authored by Theodor-Adrian Stana's avatar Theodor-Adrian Stana Committed by Grzegorz Daniluk

Added flash-read tool

How-to guide for the tool (together with flash-write tool) can be found in
sdbfs.README.
Signed-off-by: Theodor-Adrian Stana's avatarTheodor Stana <t.stana@cern.ch>

SQUASH remove flash-read binary: 598d cdf68
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent c3f90208
...@@ -4,7 +4,7 @@ SDBFS ?= no ...@@ -4,7 +4,7 @@ SDBFS ?= no
CFLAGS = -Wall -ggdb -I../include CFLAGS = -Wall -ggdb -I../include
LDFLAGS = -lutil LDFLAGS = -lutil
ALL = genraminit genramvhd genrammif wrpc-uart-sw ALL = genraminit genramvhd genrammif wrpc-uart-sw
ALL += wrpc-w1-read wrpc-w1-write flash-write ALL += wrpc-w1-read wrpc-w1-write flash-write flash-read
ifneq ($(EB),no) ifneq ($(EB),no)
ALL += eb-w1-write ALL += eb-w1-write
...@@ -43,10 +43,12 @@ sdb-wrpc.bin: sdbfs ...@@ -43,10 +43,12 @@ sdb-wrpc.bin: sdbfs
flash-write: flash-write.c flash-host/libflash.a flash-write: flash-write.c flash-host/libflash.a
$(CC) $(CFLAGS) -Iflash-host $^ -o $@ $(CC) $(CFLAGS) -Iflash-host $^ -o $@
flash-read: flash-read.c flash-host/libflash.a
$(CC) $(CFLAGS) -Iflash-host $^ -o $@
flash-host/libflash.a: flash-host/libflash.a:
$(MAKE) -C flash-host $(MAKE) -C flash-host
clean: clean:
rm -f $(ALL) *.o *~ rm -f $(ALL) *.o *~
$(MAKE) -C flash-host clean $(MAKE) -C flash-host clean
/*
*==============================================================================
* CERN (BE-CO-HT)
* Source file for M25P flash readout tool
*==============================================================================
*
* author: Theodor Stana (t.stana@cern.ch)
*
* date of creation: 2013-10-24
*
* version: 1.0
*
* description:
*
* dependencies:
*
* references:
*
*==============================================================================
* GNU LESSER GENERAL PUBLIC LICENSE
*==============================================================================
* This source file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version. This source is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details. You should have
* received a copy of the GNU Lesser General Public License along with this
* source; if not, download it from http://www.gnu.org/licenses/lgpl-2.1.html
*==============================================================================
* last changes:
* 2013-10-24 Theodor Stana t.stana@cern.ch File created
*==============================================================================
* TODO: -
*==============================================================================
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include "flash.h"
#define SPEC_SYSCON_OFFSET 0x20400 /* from "sdb" on the shell, current gateware */
#define MAX_DEVICES 8 /* no alloc, me lazy */
static struct spec_pci_id {
unsigned pci_v;
unsigned pci_d;
} spec_devices[] = {
{ 0x10dc /* CERN */, 0x018d /* SPEC */ },
{ 0x1a39 /* Gennum */, 0x0004 /* GN4124 */ },
{ 0, },
};
struct spec_device {
void *mapaddr;
int mapsize;
int busid;
};
static struct spec_device devs[MAX_DEVICES];
char *prgname;
int verbose;
extern void *BASE_SYSCON;
/*
* Read the flash chip
*/
static int spec_read_flash(struct spec_device *spec, int addr, int len)
{
int i;
int startlen = len;
int startaddr = addr;
/* Initializations */
uint8_t *buf = malloc(len);
if (buf == NULL) {
fprintf(stderr, "Memory not available for read buffer!");
return -1;
}
BASE_SYSCON = spec->mapaddr + SPEC_SYSCON_OFFSET;
flash_init();
if (verbose) {
fprintf(stderr, "Reading device on bus %i: "
"offset %i (0x%X), len %i\n", spec->busid,
addr, addr, len);
}
/* Read data and put it in buffer */
while (len) {
i = len;
if (i > 256) i = 256;
flash_read(addr, buf, i);
len -= i;
addr += i;
buf += i;
}
/* Pull buffer pointer back to start of buffer */
buf -= startlen;
/* Print read values */
if (verbose) {
for (i = 0; i < startlen; i++) {
fprintf(stderr, "offset %4i (0x%03x): %3i (0x%02x)\n",
startaddr + i, startaddr + i, buf[i], buf[i]);
}
} else {
fwrite(buf, 1, startlen, stdout);
}
/* Free buffer and exit */
free(buf);
return 0;
}
/*
* What follows is mostly generic, should be librarized in a way
*/
/* Access a PCI device, mmap and so on */
static void *spec_access_pci(char *name, int index)
{
struct spec_device *dev = devs + index;
char path[PATH_MAX];
struct stat stbuf;
int fd;
memset(dev, 0, sizeof(*dev));
sprintf(path, "/sys/bus/pci/devices/%s/resource0", name);
if ((fd = open(path, O_RDWR | O_SYNC)) < 0) {
fprintf(stderr, "%s: %s: %s\n", prgname, path,
strerror(errno));
return NULL;
}
fstat(fd, &stbuf);
dev->mapsize = stbuf.st_size;
dev->mapaddr = mmap(0, stbuf.st_size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if (dev->mapaddr == MAP_FAILED) {
fprintf(stderr, "%s: mmap(%s): %s\n", prgname, path,
strerror(errno));
return NULL;
}
if (sscanf(name, "%*x:%x", &dev->busid) != 1)
return NULL;
return dev->mapaddr;
}
/* Scan PCI space for vendor and device; return number of successes */
static int spec_scan_pci(struct spec_pci_id *id, struct spec_device *arr,
int alen)
{
char path[PATH_MAX];
FILE *f;
struct dirent **namelist;
int i, j, n, ndevs;
unsigned v, d;
n = scandir("/sys/bus/pci/devices", &namelist, 0, 0);
if (n < 0) {
fprintf(stderr, "%s: /sys/bus/pci/devices: %s\n", prgname,
strerror(errno));
return -1;
}
for (i = ndevs = 0; i < n; i++) {
if (namelist[i]->d_name[0] == '.')
continue;
/* check vendor */
sprintf(path, "/sys/bus/pci/devices/%s/vendor",
namelist[i]->d_name);
f = fopen(path, "r");
if (!f) {
fprintf(stderr, "%s: %s: %s\n", prgname, path,
strerror(errno));
continue;
}
if (fscanf(f, "%i", &v) != 1)
continue;
fclose(f);
/* check device */
sprintf(path, "/sys/bus/pci/devices/%s/device",
namelist[i]->d_name);
f = fopen(path, "r");
if (!f) {
fprintf(stderr, "%s: %s: %s\n", prgname, path,
strerror(errno));
continue;
}
if (fscanf(f, "%i", &d) != 1)
continue;
fclose(f);
for (j = 0; id[j].pci_v; j++)
if (id[j].pci_v == v && id[j].pci_d == d)
break;
if (!spec_devices[j].pci_v)
continue; /* not found in whole array */
/* Ok, so this is ours. Celebrate, and open it */
if (verbose)
fprintf(stderr, "%s: found device %04x:%04x: %s\n",
prgname, v, d, namelist[i]->d_name);
if (ndevs == alen) {
fprintf(stderr, "%s: array overflow, ignoring card\n",
prgname);
continue;
}
if (spec_access_pci(namelist[i]->d_name, ndevs) == NULL)
continue;
ndevs++;
}
return ndevs;
}
static int help(void)
{
fprintf(stderr, "%s: Use: \"%s [-v] [-b <bus>] <addr> <len>\n",
prgname, prgname);
return 1;
}
int main(int argc, char **argv)
{
int ndev, i, c, bus = -1;
struct spec_device *spec = NULL;
prgname = argv[0];
while ((c = getopt(argc, argv, "b:v")) != -1) {
switch(c) {
case 'b':
sscanf(optarg, "%i", &bus);
break;
case 'v':
verbose++;
break;
default:
exit(help());
}
}
if (optind != argc - 2)
exit(help());
/* find which one to use */
ndev = spec_scan_pci(spec_devices, devs, MAX_DEVICES);
if (ndev < 1) {
fprintf(stderr, "%s: no suitable PCI devices\n", prgname);
exit(1);
}
if (bus == -1 && ndev == 1) {
spec = devs;
} else if (bus == -1) {
fprintf(stderr, "%s: several devices found, please choose:\n",
prgname);
for (i = 0; i < ndev; i++) {
fprintf(stderr, " -b %i\n", devs[i].busid);
}
exit(1);
} else {
for (i = 0; i < ndev; i++)
if (bus == devs[i].busid)
break;
if (i == ndev) {
fprintf(stderr, "%s: no device on bus %i\n", prgname,
bus);
exit(1);
}
spec = devs + i;
}
i = spec_read_flash(spec, atoi(argv[optind]), atoi(argv[optind + 1]));
return i;
}
...@@ -43,6 +43,9 @@ This states where the various files are. ...@@ -43,6 +43,9 @@ This states where the various files are.
To write to w1-eeprom: "tools/wrpc-w1-write 0 320 < /tmp/sdb-wrpc.bin" To write to w1-eeprom: "tools/wrpc-w1-write 0 320 < /tmp/sdb-wrpc.bin"
(this assumes that the size is 320 bytes. (this assumes that the size is 320 bytes.
To write to the flash: "tools/flash-write 0 320 < /tmp/sdb-wrpc.bin"
(again, assuming a 320-byte-sized image file).
The next boot of lm32 will show it found the files: The next boot of lm32 will show it found the files:
sdbfs: found at 0 in W1 sdbfs: found at 0 in W1
...@@ -51,3 +54,6 @@ The next boot of lm32 will show it found the files: ...@@ -51,3 +54,6 @@ The next boot of lm32 will show it found the files:
file 0x6d61632d @ 576, name mac-address file 0x6d61632d @ 576, name mac-address
file 0x7366702d @ 640, name sfp-database file 0x7366702d @ 640, name sfp-database
file 0x63616c69 @ 768, name calibration file 0x63616c69 @ 768, name calibration
To read the flash: "tools/flash-read 0 320 > /tmp/flash.bin",
and then check the flash image file: "sdb-read -l /tmp/flash.bin".
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment