General reformat commit to make Sora happy again
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
c67ec7ffbe
commit
e9ad8ad656
|
@ -40,35 +40,35 @@
|
|||
#include <fcntl.h>
|
||||
|
||||
|
||||
int main(int argc, char** argv){
|
||||
int main(int argc, char **argv) {
|
||||
int fdBAR4;
|
||||
FILE* fdOutput;
|
||||
FILE *fdOutput;
|
||||
|
||||
if(argc < 2){
|
||||
if (argc < 2) {
|
||||
printf("Usage: %s <output.bin> [forceSizeInMegabits, default 8]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fdOutput = fopen(argv[1], "wb");
|
||||
|
||||
if(fdOutput == NULL){
|
||||
if (fdOutput == NULL) {
|
||||
printf("Could not create %s\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
FM10K_PCI_DEVICE device = fm10k_pci_findDevice();
|
||||
|
||||
if(device.vendor == 0){
|
||||
if (device.vendor == 0) {
|
||||
printf("Unable to find FM10K device with BAR4 port access. Check bifurcation settings?\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fdBAR4 = open(device.bar4Path, O_RDWR);
|
||||
if(fdBAR4 <= 0){
|
||||
if (fdBAR4 <= 0) {
|
||||
printf("Unable to open BAR4 resource %s to read NVM\n", device.bar4Path);
|
||||
return 1;
|
||||
}
|
||||
void* memmapAddr;
|
||||
void *memmapAddr;
|
||||
memmapAddr = mmap(NULL, FM10K_BAR4_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fdBAR4, 0);
|
||||
if (memmapAddr == MAP_FAILED) {
|
||||
printf("Unable to map BAR4 resource %s to read NVM\n", device.bar4Path);
|
||||
|
@ -77,50 +77,53 @@ int main(int argc, char** argv){
|
|||
}
|
||||
|
||||
SPI_COMMAND_READ_MANUFACTURER_RESULT manufacturerInfo = fm10k_mem_spi_ReadManufacturerData((uintptr_t) memmapAddr);
|
||||
if(manufacturerInfo.value == 0){
|
||||
if (manufacturerInfo.value == 0) {
|
||||
printf("Error getting manufacturer information\n");
|
||||
return 1;
|
||||
}
|
||||
printf("Manufacturer Info 0x%08x :: JEDEC Manufacturer ID 0x%02x family 0x%02x density 0x%02x %02x:%02x\n", manufacturerInfo.value, manufacturerInfo.fields.manufacturerId, manufacturerInfo.fields.familyCode, manufacturerInfo.fields.densityCode, manufacturerInfo.fields.subCode, manufacturerInfo.fields.productVariant);
|
||||
printf("Manufacturer Info 0x%08x :: JEDEC Manufacturer ID 0x%02x family 0x%02x density 0x%02x %02x:%02x\n",
|
||||
manufacturerInfo.value, manufacturerInfo.fields.manufacturerId, manufacturerInfo.fields.familyCode,
|
||||
manufacturerInfo.fields.densityCode, manufacturerInfo.fields.subCode,
|
||||
manufacturerInfo.fields.productVariant);
|
||||
|
||||
unsigned int mbitsToRead = 8;
|
||||
|
||||
const KNOWN_FLASH_DEVICE* knownFlashDevice = getKnownFlashFromManufacturerData(manufacturerInfo);
|
||||
const KNOWN_FLASH_DEVICE *knownFlashDevice = getKnownFlashFromManufacturerData(manufacturerInfo);
|
||||
|
||||
if(knownFlashDevice != NULL){
|
||||
if (knownFlashDevice != NULL) {
|
||||
printf("Device: %s\n", knownFlashDevice->name);
|
||||
mbitsToRead = knownFlashDevice->sizeInMbit;
|
||||
}else{
|
||||
} else {
|
||||
printf("Device: Unknown, defaulting %uMbit size\n", mbitsToRead);
|
||||
}
|
||||
|
||||
if(argc > 2){
|
||||
if(mbitsToRead != 8){
|
||||
if (argc > 2) {
|
||||
if (mbitsToRead != 8) {
|
||||
printf("WARNING: %uMbit size was already auto-detected\n", mbitsToRead);
|
||||
}
|
||||
mbitsToRead = strtoul(argv[2], NULL, 10);
|
||||
printf("Forcing %uMbit size\n", mbitsToRead);
|
||||
}
|
||||
|
||||
if(mbitsToRead > 1024){
|
||||
if (mbitsToRead > 1024) {
|
||||
printf("Too many MBit: %u\n", mbitsToRead);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(mbitsToRead < 8){
|
||||
if (mbitsToRead < 8) {
|
||||
printf("Too few MBit: %u\n", mbitsToRead);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint32_t bootImageSize = (mbitsToRead * 1024 * 1024) / 8;
|
||||
|
||||
uint8_t* value = malloc(bootImageSize);
|
||||
uint8_t *value = malloc(bootImageSize);
|
||||
|
||||
uint32_t strideSize = 512;
|
||||
|
||||
for(uint32_t addr = 0; addr < bootImageSize; addr += strideSize){
|
||||
for (uint32_t addr = 0; addr < bootImageSize; addr += strideSize) {
|
||||
printf("\rread @ 0x%08x / 0x%08x %d bytes", addr, bootImageSize, strideSize);
|
||||
if(fm10k_mem_spi_ReadFlash((uintptr_t) memmapAddr, addr, value + addr, strideSize)){
|
||||
if (fm10k_mem_spi_ReadFlash((uintptr_t) memmapAddr, addr, value + addr, strideSize)) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,57 +41,56 @@
|
|||
#include <fcntl.h>
|
||||
|
||||
|
||||
int main(int argc, char** argv){
|
||||
int main(int argc, char **argv) {
|
||||
int fdBAR4;
|
||||
FILE* fdBackup;
|
||||
FILE* fdInput;
|
||||
FILE *fdBackup;
|
||||
FILE *fdInput;
|
||||
|
||||
|
||||
|
||||
if(argc < 3){
|
||||
if (argc < 3) {
|
||||
printf("Usage: %s <input.bin> <backup.bin>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fdInput = fopen(argv[1], "rb");
|
||||
|
||||
if(fdInput == NULL){
|
||||
if (fdInput == NULL) {
|
||||
printf("Could not open %s\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fdBackup = fopen(argv[2], "rb");
|
||||
if(fdBackup == NULL){
|
||||
if (fdBackup == NULL) {
|
||||
printf("Could not open existing backup %s\n", argv[2]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fseek(fdInput , 0, SEEK_END);
|
||||
fseek(fdInput, 0, SEEK_END);
|
||||
uint32_t fileSize = ftell(fdInput);
|
||||
rewind(fdInput);
|
||||
|
||||
fseek(fdBackup , 0, SEEK_END);
|
||||
fseek(fdBackup, 0, SEEK_END);
|
||||
uint32_t backupFileSize = ftell(fdBackup);
|
||||
rewind(fdBackup);
|
||||
|
||||
if(backupFileSize < fileSize){
|
||||
if (backupFileSize < fileSize) {
|
||||
printf("Backup %d is smaller than new image %d\n", backupFileSize, fileSize);
|
||||
return 1;
|
||||
}
|
||||
|
||||
FM10K_PCI_DEVICE device = fm10k_pci_findDevice();
|
||||
|
||||
if(device.vendor == 0){
|
||||
if (device.vendor == 0) {
|
||||
printf("Unable to find FM10K device with BAR4 port access. Check bifurcation settings?\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fdBAR4 = open(device.bar4Path, O_RDWR);
|
||||
if(fdBAR4 <= 0){
|
||||
if (fdBAR4 <= 0) {
|
||||
printf("Unable to open BAR4 resource %s to read NVM\n", device.bar4Path);
|
||||
return 1;
|
||||
}
|
||||
void* memmapAddr;
|
||||
void *memmapAddr;
|
||||
memmapAddr = mmap(NULL, FM10K_BAR4_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fdBAR4, 0);
|
||||
if (memmapAddr == MAP_FAILED) {
|
||||
printf("Unable to map BAR4 resource %s to read NVM\n", device.bar4Path);
|
||||
|
@ -100,29 +99,32 @@ int main(int argc, char** argv){
|
|||
}
|
||||
|
||||
SPI_COMMAND_READ_MANUFACTURER_RESULT manufacturerInfo = fm10k_mem_spi_ReadManufacturerData((uintptr_t) memmapAddr);
|
||||
if(manufacturerInfo.value == 0){
|
||||
if (manufacturerInfo.value == 0) {
|
||||
printf("Error getting manufacturer information\n");
|
||||
return 1;
|
||||
}
|
||||
printf("Manufacturer Info 0x%08x :: JEDEC Manufacturer ID 0x%02x family 0x%02x density 0x%02x %02x:%02x\n", manufacturerInfo.value, manufacturerInfo.fields.manufacturerId, manufacturerInfo.fields.familyCode, manufacturerInfo.fields.densityCode, manufacturerInfo.fields.subCode, manufacturerInfo.fields.productVariant);
|
||||
printf("Manufacturer Info 0x%08x :: JEDEC Manufacturer ID 0x%02x family 0x%02x density 0x%02x %02x:%02x\n",
|
||||
manufacturerInfo.value, manufacturerInfo.fields.manufacturerId, manufacturerInfo.fields.familyCode,
|
||||
manufacturerInfo.fields.densityCode, manufacturerInfo.fields.subCode,
|
||||
manufacturerInfo.fields.productVariant);
|
||||
|
||||
unsigned int mbitsToReadMax = 8;
|
||||
|
||||
const KNOWN_FLASH_DEVICE* knownFlashDevice = getKnownFlashFromManufacturerData(manufacturerInfo);
|
||||
const KNOWN_FLASH_DEVICE *knownFlashDevice = getKnownFlashFromManufacturerData(manufacturerInfo);
|
||||
|
||||
if(knownFlashDevice != NULL){
|
||||
if (knownFlashDevice != NULL) {
|
||||
printf("Device: %s\n", knownFlashDevice->name);
|
||||
mbitsToReadMax = knownFlashDevice->sizeInMbit;
|
||||
}else{
|
||||
} else {
|
||||
printf("Device: Unknown, defaulting max %uMbit size\n", mbitsToReadMax);
|
||||
}
|
||||
|
||||
if(mbitsToReadMax > 1024){
|
||||
if (mbitsToReadMax > 1024) {
|
||||
printf("Too many MBit: %u\n", mbitsToReadMax);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(mbitsToReadMax == 0){
|
||||
if (mbitsToReadMax == 0) {
|
||||
printf("Too few MBit: %u\n", mbitsToReadMax);
|
||||
return 1;
|
||||
}
|
||||
|
@ -130,47 +132,47 @@ int main(int argc, char** argv){
|
|||
uint32_t strideSize = 512;
|
||||
uint32_t bootImageSizeMax = (mbitsToReadMax * 1024 * 1024) / 8;
|
||||
|
||||
if(fileSize > bootImageSizeMax){
|
||||
if (fileSize > bootImageSizeMax) {
|
||||
printf("Image too large: have %d, can write max %d bytes.\n", fileSize, bootImageSizeMax);
|
||||
return 1;
|
||||
}else if(fileSize < bootImageSizeMax){
|
||||
do{
|
||||
} else if (fileSize < bootImageSizeMax) {
|
||||
do {
|
||||
bootImageSizeMax -= strideSize;
|
||||
} while (fileSize < bootImageSizeMax);
|
||||
}
|
||||
|
||||
uint8_t* value = calloc(bootImageSizeMax, sizeof(uint8_t));
|
||||
uint8_t* valueBackup = calloc(bootImageSizeMax, sizeof(uint8_t));
|
||||
uint8_t* valueCheck = calloc(bootImageSizeMax, sizeof(uint8_t));
|
||||
uint8_t *value = calloc(bootImageSizeMax, sizeof(uint8_t));
|
||||
uint8_t *valueBackup = calloc(bootImageSizeMax, sizeof(uint8_t));
|
||||
uint8_t *valueCheck = calloc(bootImageSizeMax, sizeof(uint8_t));
|
||||
|
||||
if(fread(value, 1, bootImageSizeMax, fdInput) != bootImageSizeMax){
|
||||
if (fread(value, 1, bootImageSizeMax, fdInput) != bootImageSizeMax) {
|
||||
printf("Error reading image file.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(fread(valueBackup, 1, bootImageSizeMax, fdBackup) != bootImageSizeMax){
|
||||
if (fread(valueBackup, 1, bootImageSizeMax, fdBackup) != bootImageSizeMax) {
|
||||
printf("Error reading backup image file.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(memcmp(value, valueBackup, bootImageSizeMax) == 0){
|
||||
if (memcmp(value, valueBackup, bootImageSizeMax) == 0) {
|
||||
printf("NOTE: %s and %s have the same bytes.\n", argv[2], argv[3]);
|
||||
}
|
||||
|
||||
|
||||
for(uint32_t addr = 0; addr < bootImageSizeMax; addr += strideSize){
|
||||
for (uint32_t addr = 0; addr < bootImageSizeMax; addr += strideSize) {
|
||||
printf("\rbackup check @ 0x%08x / 0x%08x %d bytes", addr, bootImageSizeMax, strideSize);
|
||||
if(fm10k_mem_spi_ReadFlash((uintptr_t) memmapAddr, addr, valueCheck + addr, strideSize)){
|
||||
if (fm10k_mem_spi_ReadFlash((uintptr_t) memmapAddr, addr, valueCheck + addr, strideSize)) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
if(memcmp(valueBackup, valueCheck, bootImageSizeMax) != 0){
|
||||
if (memcmp(valueBackup, valueCheck, bootImageSizeMax) != 0) {
|
||||
printf("Backup image mismatch! Dump image again.\n");
|
||||
return 1;
|
||||
}else{
|
||||
} else {
|
||||
printf("Backup matches.\n");
|
||||
}
|
||||
|
||||
|
@ -182,10 +184,10 @@ int main(int argc, char** argv){
|
|||
sleep(1);
|
||||
|
||||
|
||||
for(uint32_t addr = 0; addr < bootImageSizeMax; addr += strideSize){
|
||||
if(memcmp(valueBackup + addr, value + addr, strideSize) != 0){
|
||||
for (uint32_t addr = 0; addr < bootImageSizeMax; addr += strideSize) {
|
||||
if (memcmp(valueBackup + addr, value + addr, strideSize) != 0) {
|
||||
printf("write @ 0x%08x\n", addr);
|
||||
if(fm10k_mem_spi_WriteFlash((uintptr_t) memmapAddr, addr, value + addr, strideSize)){
|
||||
if (fm10k_mem_spi_WriteFlash((uintptr_t) memmapAddr, addr, value + addr, strideSize)) {
|
||||
return 2;
|
||||
}
|
||||
sleep(1);
|
||||
|
@ -197,44 +199,43 @@ int main(int argc, char** argv){
|
|||
sleep(1);
|
||||
|
||||
|
||||
for(uint32_t addr = 0; addr < bootImageSizeMax; addr += strideSize){
|
||||
for (uint32_t addr = 0; addr < bootImageSizeMax; addr += strideSize) {
|
||||
printf("\rverify check @ 0x%08x / 0x%08x %d bytes", addr, bootImageSizeMax, strideSize);
|
||||
if(fm10k_mem_spi_ReadFlash((uintptr_t) memmapAddr, addr, valueCheck + addr, strideSize)){
|
||||
if (fm10k_mem_spi_ReadFlash((uintptr_t) memmapAddr, addr, valueCheck + addr, strideSize)) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for(uint32_t addr = 0; addr < bootImageSizeMax; addr += 4){
|
||||
if(*(uint32_t*)(value + addr) != *(uint32_t*)(valueCheck + addr)){
|
||||
printf("@0x%08x: 0x%08x != 0x%08x\n", addr, *(uint32_t*)(value + addr), *(uint32_t*)(valueCheck + addr));
|
||||
for (uint32_t addr = 0; addr < bootImageSizeMax; addr += 4) {
|
||||
if (*(uint32_t *) (value + addr) != *(uint32_t *) (valueCheck + addr)) {
|
||||
printf("@0x%08x: 0x%08x != 0x%08x\n", addr, *(uint32_t *) (value + addr),
|
||||
*(uint32_t *) (valueCheck + addr));
|
||||
}
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
|
||||
if(memcmp(value, valueCheck, bootImageSizeMax) != 0){
|
||||
if (memcmp(value, valueCheck, bootImageSizeMax) != 0) {
|
||||
printf("Data written mismatch! Replacing with backup.\n");
|
||||
|
||||
for(uint32_t addr = 0; addr < bootImageSizeMax; addr += strideSize){
|
||||
if(memcmp(valueBackup + addr, valueCheck + addr, strideSize) != 0){
|
||||
for (uint32_t addr = 0; addr < bootImageSizeMax; addr += strideSize) {
|
||||
if (memcmp(valueBackup + addr, valueCheck + addr, strideSize) != 0) {
|
||||
printf("write backup @ 0x%08x\n", addr);
|
||||
if(fm10k_mem_spi_WriteFlash((uintptr_t) memmapAddr, addr, valueBackup + addr, strideSize)){
|
||||
if (fm10k_mem_spi_WriteFlash((uintptr_t) memmapAddr, addr, valueBackup + addr, strideSize)) {
|
||||
return 2;
|
||||
}
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
printf("Data written verified.\n");
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
|
||||
|
||||
|
||||
|
||||
free(value);
|
||||
free(valueBackup);
|
||||
free(valueCheck);
|
||||
|
|
142
src/fm10k.c
142
src/fm10k.c
|
@ -39,22 +39,22 @@
|
|||
|
||||
KNOWN_FLASH_DEVICE KNOWN_FLASH_DEVICE_LIST[] = {
|
||||
{{0x0101271f}, "Adesto AT45DB321E 32-Mbit", 32}, // Present on Silicom PE3100G2DQiRM-QX4
|
||||
{{0x001540ef}, "Winbond W25Q16JV 16-Mbit", 16}, // Present on Intel FM10420-100GbE-QDA2
|
||||
{{0}, "", 0}
|
||||
{{0x001540ef}, "Winbond W25Q16JV 16-Mbit", 16}, // Present on Intel FM10420-100GbE-QDA2
|
||||
{{0}, "", 0}
|
||||
};
|
||||
|
||||
void ReadRegister32(uintptr_t mem, uint32_t addr, uint32_t* value) {
|
||||
*value = *(((uint32_t*)mem) + addr);
|
||||
void ReadRegister32(uintptr_t mem, uint32_t addr, uint32_t *value) {
|
||||
*value = *(((uint32_t *) mem) + addr);
|
||||
}
|
||||
|
||||
void WriteRegister32(uintptr_t mem, uint32_t addr, uint32_t value) {
|
||||
*(uint32_t volatile*)(((uint32_t*)mem) + addr) = value;
|
||||
*(uint32_t volatile *) (((uint32_t *) mem) + addr) = value;
|
||||
|
||||
//If returning instantly this will caused missed writes. nanosleep wait of 1 nsec was too much. Busy wait works
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
|
||||
volatile uint32_t dummy;
|
||||
for(uint32_t i = 0; i < 100; ++i){
|
||||
for (uint32_t i = 0; i < 100; ++i) {
|
||||
dummy = i;
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
|
@ -71,13 +71,13 @@ uint32_t get_interval_diff(struct timeval *begin, struct timeval *end) {
|
|||
endT.tv_usec = end->tv_usec;
|
||||
}
|
||||
|
||||
diff.tv_sec = endT.tv_sec - begin->tv_sec;
|
||||
diff.tv_sec = endT.tv_sec - begin->tv_sec;
|
||||
diff.tv_usec = endT.tv_usec - begin->tv_usec;
|
||||
|
||||
return (diff.tv_sec * 1000 + diff.tv_usec / 1000);
|
||||
}
|
||||
|
||||
uint32_t fm10k_mem_spi_SetCtrlReg(uintptr_t mem, SPI_CTRL value){
|
||||
uint32_t fm10k_mem_spi_SetCtrlReg(uintptr_t mem, SPI_CTRL value) {
|
||||
struct timeval startTime;
|
||||
uint8_t isTimeout = 0;
|
||||
SPI_CTRL spiCtrl = {0};
|
||||
|
@ -85,13 +85,13 @@ uint32_t fm10k_mem_spi_SetCtrlReg(uintptr_t mem, SPI_CTRL value){
|
|||
WriteRegister32(mem, FM10K_REGISTER_SPI_CTRL, value.value);
|
||||
|
||||
gettimeofday(&startTime, NULL);
|
||||
do{
|
||||
if(isTimeout){
|
||||
do {
|
||||
if (isTimeout) {
|
||||
printf("Timeout waiting for SPI_CTRL.Busy 0x%02x\n", spiCtrl.value);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(get_interval_diff(&startTime, NULL) > 50){
|
||||
if (get_interval_diff(&startTime, NULL) > 50) {
|
||||
isTimeout = 1;
|
||||
}
|
||||
|
||||
|
@ -106,7 +106,7 @@ uint32_t fm10k_mem_spi_SetCtrlReg(uintptr_t mem, SPI_CTRL value){
|
|||
return 0;
|
||||
}
|
||||
|
||||
void fm10k_mem_spi_Enable(uintptr_t mem){
|
||||
void fm10k_mem_spi_Enable(uintptr_t mem) {
|
||||
//ENABLE SPI
|
||||
SPI_CTRL spiCtrl;
|
||||
ReadRegister32(mem, FM10K_REGISTER_SPI_CTRL, &spiCtrl.value);
|
||||
|
@ -118,7 +118,7 @@ void fm10k_mem_spi_Enable(uintptr_t mem){
|
|||
WriteRegister32(mem, FM10K_REGISTER_SPI_CTRL, newSpiCtrl.value);
|
||||
}
|
||||
|
||||
void fm10k_mem_spi_Disable(uintptr_t mem){
|
||||
void fm10k_mem_spi_Disable(uintptr_t mem) {
|
||||
//DISABLE SPI
|
||||
SPI_CTRL spiCtrl;
|
||||
ReadRegister32(mem, FM10K_REGISTER_SPI_CTRL, &spiCtrl.value);
|
||||
|
@ -131,7 +131,7 @@ void fm10k_mem_spi_Disable(uintptr_t mem){
|
|||
|
||||
|
||||
//Error if result.value is 0x00
|
||||
SPI_COMMAND_READ_MANUFACTURER_RESULT fm10k_mem_spi_ReadManufacturerData(uintptr_t mem){
|
||||
SPI_COMMAND_READ_MANUFACTURER_RESULT fm10k_mem_spi_ReadManufacturerData(uintptr_t mem) {
|
||||
|
||||
SPI_COMMAND_READ_MANUFACTURER_RESULT result = {0};
|
||||
SPI_CTRL currentSpiCtrl;
|
||||
|
@ -145,9 +145,9 @@ SPI_COMMAND_READ_MANUFACTURER_RESULT fm10k_mem_spi_ReadManufacturerData(uintptr_
|
|||
|
||||
ReadRegister32(mem, FM10K_REGISTER_SPI_CTRL, ¤tSpiCtrl.value);
|
||||
|
||||
if(FM10K_SPI_FREQ_KHZ > 0){
|
||||
freq = ((100000 / (int)FM10K_SPI_FREQ_KHZ) / 2) - 1;
|
||||
if (freq < 0){
|
||||
if (FM10K_SPI_FREQ_KHZ > 0) {
|
||||
freq = ((100000 / (int) FM10K_SPI_FREQ_KHZ) / 2) - 1;
|
||||
if (freq < 0) {
|
||||
freq = 0;
|
||||
}
|
||||
spiCtrl.fields.Freq = freq;
|
||||
|
@ -165,7 +165,7 @@ SPI_COMMAND_READ_MANUFACTURER_RESULT fm10k_mem_spi_ReadManufacturerData(uintptr_
|
|||
spiCtrl.fields.HeaderSize = 1;
|
||||
spiCtrl.fields.DataSize = 4 & 0b11;
|
||||
|
||||
if(fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)){
|
||||
if (fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -173,12 +173,13 @@ SPI_COMMAND_READ_MANUFACTURER_RESULT fm10k_mem_spi_ReadManufacturerData(uintptr_
|
|||
/* get data */
|
||||
ReadRegister32(mem, FM10K_REGISTER_SPI_RX_DATA, &rxData);
|
||||
|
||||
result.value = ((rxData>>24)&0xff) | ((rxData<<8)&0xff0000) | ((rxData>>8)&0xff00) | ((rxData<<24)&0xff000000);
|
||||
result.value = ((rxData >> 24) & 0xff) | ((rxData << 8) & 0xff0000) | ((rxData >> 8) & 0xff00) |
|
||||
((rxData << 24) & 0xff000000);
|
||||
|
||||
|
||||
/* release CS */
|
||||
spiCtrl.fields.Command = 0b1000;
|
||||
if(fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)){
|
||||
if (fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)) {
|
||||
result.value = 0;
|
||||
return result;
|
||||
}
|
||||
|
@ -189,7 +190,7 @@ SPI_COMMAND_READ_MANUFACTURER_RESULT fm10k_mem_spi_ReadManufacturerData(uintptr_
|
|||
return result;
|
||||
}
|
||||
|
||||
uint32_t fm10k_mem_spi_ReadFlash(uintptr_t mem, uint32_t address, uint8_t* data, uint32_t len){
|
||||
uint32_t fm10k_mem_spi_ReadFlash(uintptr_t mem, uint32_t address, uint8_t *data, uint32_t len) {
|
||||
|
||||
SPI_CTRL currentSpiCtrl;
|
||||
SPI_CTRL spiCtrl = {0};
|
||||
|
@ -205,9 +206,9 @@ uint32_t fm10k_mem_spi_ReadFlash(uintptr_t mem, uint32_t address, uint8_t* data,
|
|||
|
||||
ReadRegister32(mem, FM10K_REGISTER_SPI_CTRL, ¤tSpiCtrl.value);
|
||||
|
||||
if(FM10K_SPI_FREQ_KHZ > 0){
|
||||
freq = ((100000 / (int)FM10K_SPI_FREQ_KHZ) / 2) - 1;
|
||||
if (freq < 0){
|
||||
if (FM10K_SPI_FREQ_KHZ > 0) {
|
||||
freq = ((100000 / (int) FM10K_SPI_FREQ_KHZ) / 2) - 1;
|
||||
if (freq < 0) {
|
||||
freq = 0;
|
||||
}
|
||||
spiCtrl.fields.Freq = freq;
|
||||
|
@ -223,14 +224,14 @@ uint32_t fm10k_mem_spi_ReadFlash(uintptr_t mem, uint32_t address, uint8_t* data,
|
|||
spiCtrl.fields.Command |= 0b0001;
|
||||
|
||||
cnt = 0;
|
||||
while (cnt < len){
|
||||
while (cnt < len) {
|
||||
/* determine the number of data bytes to read [1..4] */
|
||||
numRead = (len - cnt) > 3 ? 4 : (len - cnt);
|
||||
/* set 'shift data' flag and number of data bytes */
|
||||
spiCtrl.fields.Command |= 0b0100;
|
||||
spiCtrl.fields.DataSize = numRead & 0b11;
|
||||
|
||||
if(fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)){
|
||||
if (fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -249,7 +250,7 @@ uint32_t fm10k_mem_spi_ReadFlash(uintptr_t mem, uint32_t address, uint8_t* data,
|
|||
|
||||
/* release CS */
|
||||
spiCtrl.fields.Command = 0b1000;
|
||||
if(fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)){
|
||||
if (fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -259,7 +260,7 @@ uint32_t fm10k_mem_spi_ReadFlash(uintptr_t mem, uint32_t address, uint8_t* data,
|
|||
return 0;
|
||||
}
|
||||
|
||||
uint32_t fm10k_mem_spi_EnableSectorProtection(uintptr_t mem){
|
||||
uint32_t fm10k_mem_spi_EnableSectorProtection(uintptr_t mem) {
|
||||
SPI_CTRL currentSpiCtrl;
|
||||
SPI_CTRL spiCtrl = {0};
|
||||
|
||||
|
@ -271,9 +272,9 @@ uint32_t fm10k_mem_spi_EnableSectorProtection(uintptr_t mem){
|
|||
|
||||
ReadRegister32(mem, FM10K_REGISTER_SPI_CTRL, ¤tSpiCtrl.value);
|
||||
|
||||
if(FM10K_SPI_FREQ_KHZ > 0){
|
||||
freq = ((100000 / (int)FM10K_SPI_FREQ_KHZ) / 2) - 1;
|
||||
if (freq < 0){
|
||||
if (FM10K_SPI_FREQ_KHZ > 0) {
|
||||
freq = ((100000 / (int) FM10K_SPI_FREQ_KHZ) / 2) - 1;
|
||||
if (freq < 0) {
|
||||
freq = 0;
|
||||
}
|
||||
spiCtrl.fields.Freq = freq;
|
||||
|
@ -288,13 +289,13 @@ uint32_t fm10k_mem_spi_EnableSectorProtection(uintptr_t mem){
|
|||
spiCtrl.fields.Command |= 0b0001;
|
||||
|
||||
/* send command to the flash */
|
||||
if(fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)){
|
||||
if (fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* release CS */
|
||||
spiCtrl.fields.Command = 0b1000;
|
||||
if(fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)){
|
||||
if (fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -303,7 +304,7 @@ uint32_t fm10k_mem_spi_EnableSectorProtection(uintptr_t mem){
|
|||
return 0;
|
||||
}
|
||||
|
||||
uint32_t fm10k_mem_spi_DisableSectorProtection(uintptr_t mem){
|
||||
uint32_t fm10k_mem_spi_DisableSectorProtection(uintptr_t mem) {
|
||||
SPI_CTRL currentSpiCtrl;
|
||||
SPI_CTRL spiCtrl = {0};
|
||||
|
||||
|
@ -315,9 +316,9 @@ uint32_t fm10k_mem_spi_DisableSectorProtection(uintptr_t mem){
|
|||
|
||||
ReadRegister32(mem, FM10K_REGISTER_SPI_CTRL, ¤tSpiCtrl.value);
|
||||
|
||||
if(FM10K_SPI_FREQ_KHZ > 0){
|
||||
freq = ((100000 / (int)FM10K_SPI_FREQ_KHZ) / 2) - 1;
|
||||
if (freq < 0){
|
||||
if (FM10K_SPI_FREQ_KHZ > 0) {
|
||||
freq = ((100000 / (int) FM10K_SPI_FREQ_KHZ) / 2) - 1;
|
||||
if (freq < 0) {
|
||||
freq = 0;
|
||||
}
|
||||
spiCtrl.fields.Freq = freq;
|
||||
|
@ -332,13 +333,13 @@ uint32_t fm10k_mem_spi_DisableSectorProtection(uintptr_t mem){
|
|||
spiCtrl.fields.Command |= 0b0001;
|
||||
|
||||
/* send command to the flash */
|
||||
if(fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)){
|
||||
if (fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* release CS */
|
||||
spiCtrl.fields.Command = 0b1000;
|
||||
if(fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)){
|
||||
if (fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -347,7 +348,7 @@ uint32_t fm10k_mem_spi_DisableSectorProtection(uintptr_t mem){
|
|||
return 0;
|
||||
}
|
||||
|
||||
uint32_t fm10k_mem_spi_WriteFlash(uintptr_t mem, uint32_t address, const uint8_t* data, uint32_t len){
|
||||
uint32_t fm10k_mem_spi_WriteFlash(uintptr_t mem, uint32_t address, const uint8_t *data, uint32_t len) {
|
||||
|
||||
SPI_CTRL currentSpiCtrl;
|
||||
SPI_CTRL spiCtrl = {0};
|
||||
|
@ -363,9 +364,9 @@ uint32_t fm10k_mem_spi_WriteFlash(uintptr_t mem, uint32_t address, const uint8_t
|
|||
|
||||
ReadRegister32(mem, FM10K_REGISTER_SPI_CTRL, ¤tSpiCtrl.value);
|
||||
|
||||
if(FM10K_SPI_FREQ_KHZ > 0){
|
||||
freq = ((100000 / (int)FM10K_SPI_FREQ_KHZ) / 2) - 1;
|
||||
if (freq < 0){
|
||||
if (FM10K_SPI_FREQ_KHZ > 0) {
|
||||
freq = ((100000 / (int) FM10K_SPI_FREQ_KHZ) / 2) - 1;
|
||||
if (freq < 0) {
|
||||
freq = 0;
|
||||
}
|
||||
spiCtrl.fields.Freq = freq;
|
||||
|
@ -381,7 +382,7 @@ uint32_t fm10k_mem_spi_WriteFlash(uintptr_t mem, uint32_t address, const uint8_t
|
|||
spiCtrl.fields.Command |= 0b0001;
|
||||
|
||||
cnt = 0;
|
||||
while (cnt < len){
|
||||
while (cnt < len) {
|
||||
/* determine the number of data bytes to read [1..4] */
|
||||
numWrite = (len - cnt) > 3 ? 4 : (len - cnt);
|
||||
/* set 'shift data' flag and number of data bytes */
|
||||
|
@ -396,7 +397,7 @@ uint32_t fm10k_mem_spi_WriteFlash(uintptr_t mem, uint32_t address, const uint8_t
|
|||
WriteRegister32(mem, FM10K_REGISTER_SPI_TX_DATA, txData);
|
||||
|
||||
/* send command to the flash */
|
||||
if(fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)){
|
||||
if (fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -406,7 +407,7 @@ uint32_t fm10k_mem_spi_WriteFlash(uintptr_t mem, uint32_t address, const uint8_t
|
|||
|
||||
/* release CS */
|
||||
spiCtrl.fields.Command = 0b1000;
|
||||
if(fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)){
|
||||
if (fm10k_mem_spi_SetCtrlReg(mem, spiCtrl)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -416,10 +417,10 @@ uint32_t fm10k_mem_spi_WriteFlash(uintptr_t mem, uint32_t address, const uint8_t
|
|||
return 0;
|
||||
}
|
||||
|
||||
const KNOWN_FLASH_DEVICE* getKnownFlashFromManufacturerData(SPI_COMMAND_READ_MANUFACTURER_RESULT data){
|
||||
const KNOWN_FLASH_DEVICE *getKnownFlashFromManufacturerData(SPI_COMMAND_READ_MANUFACTURER_RESULT data) {
|
||||
uint32_t deviceCount = (sizeof(KNOWN_FLASH_DEVICE_LIST) / sizeof(KNOWN_FLASH_DEVICE)) - 1;
|
||||
for(uint32_t i = 0; i < deviceCount; ++i){
|
||||
if(KNOWN_FLASH_DEVICE_LIST[i].data.value == data.value){
|
||||
for (uint32_t i = 0; i < deviceCount; ++i) {
|
||||
if (KNOWN_FLASH_DEVICE_LIST[i].data.value == data.value) {
|
||||
return &KNOWN_FLASH_DEVICE_LIST[i];
|
||||
}
|
||||
}
|
||||
|
@ -427,43 +428,44 @@ const KNOWN_FLASH_DEVICE* getKnownFlashFromManufacturerData(SPI_COMMAND_READ_MAN
|
|||
return NULL;
|
||||
}
|
||||
|
||||
FM10K_PCI_DEVICE fm10k_pci_findDevice(){
|
||||
FM10K_PCI_DEVICE fm10k_pci_findDevice() {
|
||||
DIR *folder;
|
||||
char fileName[PATH_MAX];
|
||||
FM10K_PCI_DEVICE device = {0, 0, ""};
|
||||
|
||||
folder = opendir("/sys/bus/pci/devices");
|
||||
if(folder == NULL){
|
||||
if (folder == NULL) {
|
||||
return device;
|
||||
}
|
||||
|
||||
struct dirent *entry;
|
||||
while((entry = readdir(folder))){
|
||||
if(entry->d_type == DT_DIR || entry->d_type == DT_LNK){
|
||||
while ((entry = readdir(folder))) {
|
||||
if (entry->d_type == DT_DIR || entry->d_type == DT_LNK) {
|
||||
sprintf(fileName, "/sys/bus/pci/devices/%s/vendor", entry->d_name);
|
||||
|
||||
FILE* vendorBytesPointer = fopen(fileName, "rb");
|
||||
if(vendorBytesPointer != NULL){
|
||||
char* vendorBytesBuffer = NULL;
|
||||
FILE *vendorBytesPointer = fopen(fileName, "rb");
|
||||
if (vendorBytesPointer != NULL) {
|
||||
char *vendorBytesBuffer = NULL;
|
||||
size_t vendorBytesLen;
|
||||
size_t vendorBytesRead = getdelim(&vendorBytesBuffer, &vendorBytesLen, '\0', vendorBytesPointer);
|
||||
if(vendorBytesRead != -1){
|
||||
int vendor = (int)strtol(vendorBytesBuffer, NULL, 0);
|
||||
if (vendorBytesRead != -1) {
|
||||
int vendor = (int) strtol(vendorBytesBuffer, NULL, 0);
|
||||
|
||||
if(vendor == 0x8086){ //Intel Corporation
|
||||
if (vendor == 0x8086) { //Intel Corporation
|
||||
|
||||
sprintf(fileName, "/sys/bus/pci/devices/%s/device", entry->d_name);
|
||||
FILE* classBytesPointer = fopen(fileName, "rb");
|
||||
if(classBytesPointer != NULL){
|
||||
char* classBytesBuffer = NULL;
|
||||
FILE *classBytesPointer = fopen(fileName, "rb");
|
||||
if (classBytesPointer != NULL) {
|
||||
char *classBytesBuffer = NULL;
|
||||
size_t classBytesLen;
|
||||
size_t classBytesRead = getdelim(&classBytesBuffer, &classBytesLen, '\0', classBytesPointer);
|
||||
if(classBytesRead != -1){
|
||||
int class = (int)strtol(classBytesBuffer, NULL, 0);
|
||||
size_t classBytesRead = getdelim(&classBytesBuffer, &classBytesLen, '\0',
|
||||
classBytesPointer);
|
||||
if (classBytesRead != -1) {
|
||||
int class = (int) strtol(classBytesBuffer, NULL, 0);
|
||||
|
||||
if(class == 0x15a4 || class == 0x15d0 || class == 0x15d5){ //FM10000 devices
|
||||
if (class == 0x15a4 || class == 0x15d0 || class == 0x15d5) { //FM10000 devices
|
||||
sprintf(fileName, "/sys/bus/pci/devices/%s/resource4", entry->d_name);
|
||||
if(access(fileName, F_OK ) == 0 ) {
|
||||
if (access(fileName, F_OK) == 0) {
|
||||
device.vendor = vendor;
|
||||
device.class = class;
|
||||
|
||||
|
@ -472,7 +474,7 @@ FM10K_PCI_DEVICE fm10k_pci_findDevice(){
|
|||
}
|
||||
}
|
||||
|
||||
if(classBytesBuffer != NULL){
|
||||
if (classBytesBuffer != NULL) {
|
||||
free(classBytesBuffer);
|
||||
}
|
||||
fclose(classBytesPointer);
|
||||
|
@ -480,13 +482,13 @@ FM10K_PCI_DEVICE fm10k_pci_findDevice(){
|
|||
}
|
||||
}
|
||||
|
||||
if(vendorBytesBuffer != NULL){
|
||||
if (vendorBytesBuffer != NULL) {
|
||||
free(vendorBytesBuffer);
|
||||
}
|
||||
fclose(vendorBytesPointer);
|
||||
}
|
||||
|
||||
if(device.vendor != 0){
|
||||
if (device.vendor != 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
53
src/fm10k.h
53
src/fm10k.h
|
@ -59,31 +59,31 @@
|
|||
typedef union {
|
||||
uint32_t value;
|
||||
struct {
|
||||
uint16_t Freq : 10; // Actual speed is PCIE_REFCLK/(2*(1+Freq))
|
||||
uint8_t Enable : 1;
|
||||
uint8_t Command : 4; // 0b00 = reset
|
||||
uint8_t HeaderSize : 2; // 0b00 = 4 bytes
|
||||
uint8_t DataSize : 2; // 0b00 = 4 bytes
|
||||
uint8_t DataShiftMethod : 2; // 0b00 = SINGLE, 0b01 = DUAL, 0b10 = QUAD, 0b11 = Reserved
|
||||
uint8_t Busy : 1; // volatile ReadOnly
|
||||
uint8_t Selected : 1; // volatile ReadOnly
|
||||
uint8_t DirIO2 : 1; // 0b0 = Input, 0b1 = Output
|
||||
uint8_t DirIO3 : 1; // 0b0 = Input, 0b1 = Output
|
||||
uint8_t PinIO2 : 1; // volatile ReadWrite
|
||||
uint8_t PinIO3 : 1; // volatile ReadWrite
|
||||
uint8_t Reserved : 5;
|
||||
uint16_t Freq: 10; // Actual speed is PCIE_REFCLK/(2*(1+Freq))
|
||||
uint8_t Enable: 1;
|
||||
uint8_t Command: 4; // 0b00 = reset
|
||||
uint8_t HeaderSize: 2; // 0b00 = 4 bytes
|
||||
uint8_t DataSize: 2; // 0b00 = 4 bytes
|
||||
uint8_t DataShiftMethod: 2; // 0b00 = SINGLE, 0b01 = DUAL, 0b10 = QUAD, 0b11 = Reserved
|
||||
uint8_t Busy: 1; // volatile ReadOnly
|
||||
uint8_t Selected: 1; // volatile ReadOnly
|
||||
uint8_t DirIO2: 1; // 0b0 = Input, 0b1 = Output
|
||||
uint8_t DirIO3: 1; // 0b0 = Input, 0b1 = Output
|
||||
uint8_t PinIO2: 1; // volatile ReadWrite
|
||||
uint8_t PinIO3: 1; // volatile ReadWrite
|
||||
uint8_t Reserved: 5;
|
||||
} __attribute__((packed)) fields;
|
||||
} SPI_CTRL;
|
||||
|
||||
typedef union {
|
||||
uint32_t value;
|
||||
struct {
|
||||
uint8_t manufacturerId : 8;
|
||||
uint8_t densityCode : 5;
|
||||
uint8_t familyCode : 3;
|
||||
uint8_t productVariant : 5;
|
||||
uint8_t subCode : 3;
|
||||
uint8_t RESERVED : 8;
|
||||
uint8_t manufacturerId: 8;
|
||||
uint8_t densityCode: 5;
|
||||
uint8_t familyCode: 3;
|
||||
uint8_t productVariant: 5;
|
||||
uint8_t subCode: 3;
|
||||
uint8_t RESERVED: 8;
|
||||
} __attribute__((packed)) fields;
|
||||
} SPI_COMMAND_READ_MANUFACTURER_RESULT;
|
||||
|
||||
|
@ -97,12 +97,11 @@ typedef struct {
|
|||
#error "Packed bit-fields of type char were not properly bit-packed on many targets prior to GCC 4.4, upgrade to GCC >= 4.4."
|
||||
#endif
|
||||
|
||||
void ReadRegister32(uintptr_t mem, uint32_t addr, uint32_t* value);
|
||||
void ReadRegister32(uintptr_t mem, uint32_t addr, uint32_t *value);
|
||||
|
||||
void WriteRegister32(uintptr_t mem, uint32_t addr, uint32_t value);
|
||||
|
||||
|
||||
|
||||
uint32_t get_interval_diff(struct timeval *begin, struct timeval *end);
|
||||
|
||||
uint32_t fm10k_mem_spi_SetCtrlReg(uintptr_t mem, SPI_CTRL value);
|
||||
|
@ -112,18 +111,20 @@ void fm10k_mem_spi_Enable(uintptr_t mem);
|
|||
void fm10k_mem_spi_Disable(uintptr_t mem);
|
||||
|
||||
|
||||
|
||||
//Error if result.value is 0x00
|
||||
SPI_COMMAND_READ_MANUFACTURER_RESULT fm10k_mem_spi_ReadManufacturerData(uintptr_t mem);
|
||||
const KNOWN_FLASH_DEVICE* getKnownFlashFromManufacturerData(SPI_COMMAND_READ_MANUFACTURER_RESULT data);
|
||||
|
||||
const KNOWN_FLASH_DEVICE *getKnownFlashFromManufacturerData(SPI_COMMAND_READ_MANUFACTURER_RESULT data);
|
||||
|
||||
uint32_t fm10k_mem_spi_EnableSectorProtection(uintptr_t mem);
|
||||
|
||||
uint32_t fm10k_mem_spi_DisableSectorProtection(uintptr_t mem);
|
||||
|
||||
uint32_t fm10k_mem_spi_ReadFlash(uintptr_t mem, uint32_t address, uint8_t* data, uint32_t len);
|
||||
uint32_t fm10k_mem_spi_WriteFlash(uintptr_t mem, uint32_t address, const uint8_t* data, uint32_t len);
|
||||
uint32_t fm10k_mem_spi_ReadFlash(uintptr_t mem, uint32_t address, uint8_t *data, uint32_t len);
|
||||
|
||||
typedef struct{
|
||||
uint32_t fm10k_mem_spi_WriteFlash(uintptr_t mem, uint32_t address, const uint8_t *data, uint32_t len);
|
||||
|
||||
typedef struct {
|
||||
uint16_t vendor;
|
||||
uint16_t class;
|
||||
char bar4Path[PATH_MAX];
|
||||
|
|
Loading…
Reference in a new issue