Added a feature where the firmware can be auto updated from a USB drive.

This commit is contained in:
Stephen White 2018-12-29 17:48:18 +11:00
parent 6db79af58a
commit a25f1963c0
3 changed files with 111 additions and 5 deletions

View File

@ -43,7 +43,7 @@ extern Options options;
extern void GlobalSetDeviceID(u8 id); extern void GlobalSetDeviceID(u8 id);
extern void CheckAutoMountImage(EXIT_TYPE reset_reason , FileBrowser* fileBrowser); extern void CheckAutoMountImage(EXIT_TYPE reset_reason , FileBrowser* fileBrowser);
extern void SwitchDrive(const char* drive); extern bool SwitchDrive(const char* drive);
extern int numberOfUSBMassStorageDevices; extern int numberOfUSBMassStorageDevices;
unsigned char FileBrowser::LSTBuffer[FileBrowser::LSTBuffer_size]; unsigned char FileBrowser::LSTBuffer[FileBrowser::LSTBuffer_size];
@ -1373,7 +1373,7 @@ void FileBrowser::DisplayStatusBar()
u32 y = screenMain->ScaleY(STATUS_BAR_POSITION_Y); u32 y = screenMain->ScaleY(STATUS_BAR_POSITION_Y);
char bufferOut[128]; char bufferOut[128];
snprintf(bufferOut, 256, "LED 0 Motor 0 Track 18.0 ATN 0 DAT 0 CLK 0"); snprintf(bufferOut, 128, "LED 0 Motor 0 Track 18.0 ATN 0 DAT 0 CLK 0");
screenMain->PrintText(false, x, y, bufferOut, RGBA(0, 0, 0, 0xff), RGBA(0xff, 0xff, 0xff, 0xff)); screenMain->PrintText(false, x, y, bufferOut, RGBA(0, 0, 0, 0xff), RGBA(0xff, 0xff, 0xff, 0xff));
} }

View File

@ -452,6 +452,9 @@ IEC_Commands::UpdateAction IEC_Commands::SimulateIECUpdate(void)
} }
updateAction = NONE; updateAction = NONE;
if (selectedImageName[0] != 0) updateAction = IMAGE_SELECTED;
switch (atnSequence) switch (atnSequence)
{ {
case ATN_SEQUENCE_IDLE: case ATN_SEQUENCE_IDLE:

View File

@ -81,7 +81,7 @@ enum EmulatingMode
EMULATING_1581 EMULATING_1581
}; };
EmulatingMode emulating; volatile EmulatingMode emulating;
typedef void(*func_ptr)(); typedef void(*func_ptr)();
@ -1538,12 +1538,112 @@ void Reboot_Pi()
reboot_now(); reboot_now();
} }
void SwitchDrive(const char* drive) bool SwitchDrive(const char* drive)
{ {
FRESULT res; FRESULT res;
res = f_chdrive(drive); res = f_chdrive(drive);
DEBUG_LOG("chdrive %s res %d\r\n", drive, res); DEBUG_LOG("chdrive %s res %d\r\n", drive, res);
return res == FR_OK;
}
void UpdateFirmwareToSD()
{
const char* firmwareName = "kernel.img";
DIR dir;
FILINFO filInfo;
FRESULT res;
u32 widthText, heightText;
u32 widthScreen = screen.Width();
u32 heightScreen = screen.Height();
u32 xpos, ypos;
if (SwitchDrive("USB01:"))
{
char cwd[1024];
if (f_getcwd(cwd, 1024) == FR_OK)
{
f_chdir("\\");
bool found = f_findfirst(&dir, &filInfo, ".", firmwareName) == FR_OK;
if (found)
{
char* mem = (char*)malloc((u32)filInfo.fsize);
if (mem)
{
FIL fp;
u32 bytes;
res = f_open(&fp, firmwareName, FA_READ);
if (res == FR_OK)
{
screen.Clear(COLOUR_BLACK);
snprintf(tempBuffer, tempBufferSize, "Checking firmware on USB.\r\n");
screen.MeasureText(false, tempBuffer, &widthText, &heightText);
xpos = (widthScreen - widthText) >> 1;
ypos = (heightScreen - heightText) >> 1;
screen.PrintText(false, xpos, ypos, tempBuffer, COLOUR_WHITE, COLOUR_RED);
res = f_read(&fp, mem, (u32)filInfo.fsize, &bytes);
f_close(&fp);
if ((res == FR_OK) && ((u32)filInfo.fsize == bytes))
{
if (SwitchDrive("SD:"))
{
if (f_chdir("\\") == FR_OK)
{
bool same = true;
if (FR_OK == f_open(&fp, firmwareName, FA_READ))
{
char* ptr = mem;
char buffer[256];
unsigned bufferIndex = 0;
unsigned bytesRead;
do
{
f_read(&fp, buffer, 256, &bytesRead);
for (unsigned index = 0; index < bytesRead; ++index)
{
if (buffer[index] != mem[bufferIndex + index])
{
same = false;
break;
}
}
bufferIndex += bytesRead;
} while (same && (bufferIndex < (u32)filInfo.fsize));
f_close(&fp);
}
screen.Clear(COLOUR_BLACK);
if (!same && (FR_OK == f_open(&fp, firmwareName, FA_CREATE_ALWAYS | FA_WRITE)))
{
snprintf(tempBuffer, tempBufferSize, "Updating firmware.\r\n");
screen.MeasureText(false, tempBuffer, &widthText, &heightText);
xpos = (widthScreen - widthText) >> 1;
ypos = (heightScreen - heightText) >> 1;
screen.PrintText(false, xpos, ypos, tempBuffer, COLOUR_WHITE, COLOUR_RED);
res = f_write(&fp, mem, (u32)filInfo.fsize, &bytes);
f_close(&fp);
}
}
}
}
}
else
{
DEBUG_LOG("failed to open file %s %d\r\n", firmwareName, (int)res);
}
free(mem);
}
}
SwitchDrive("USB01:");
f_chdir(cwd);
}
}
} }
extern "C" extern "C"
@ -1642,7 +1742,10 @@ extern "C"
res = f_mount(&fileSystemUSB[USBDriveIndex], USBDriveId, 1); res = f_mount(&fileSystemUSB[USBDriveIndex], USBDriveId, 1);
} }
if (numberOfUSBMassStorageDevices > 0) if (numberOfUSBMassStorageDevices > 0)
SwitchDrive("USB01:"); {
if (SwitchDrive("USB01:"))
UpdateFirmwareToSD();
}
f_chdir("/1541"); f_chdir("/1541");