Skip to content

Commit 859407e

Browse files
authored
Merge pull request #223 from nielsmh/sd-sendfile-fixes
Toolbox fixes for file upload
2 parents da7df4b + bc07c93 commit 859407e

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

src/BlueSCSI_Toolbox.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,12 @@ void onGetFile10(char * dir_name) {
251251
void onSendFilePrep(char * dir_name)
252252
{
253253
char file_name[32+1];
254-
memset(file_name, '\0', 32+1);
254+
255255
scsiEnterPhase(DATA_OUT);
256-
for (int i = 0; i < 32+1; ++i)
257-
{
258-
file_name[i] = scsiReadByte();
259-
}
256+
scsiRead(static_cast<uint8_t *>(static_cast<void *>(file_name)), 32+1, NULL);
257+
file_name[32] = '\0';
258+
259+
debuglog("TOOLBOX OPEN FILE FOR WRITE: '", file_name, "'");
260260
SD.chdir(dir_name);
261261
gFile.open(file_name, FILE_WRITE);
262262
SD.chdir("/");
@@ -296,19 +296,22 @@ void onSendFile10(void)
296296
uint16_t bytes_sent = ((uint16_t)scsiDev.cdb[1] << 8) | scsiDev.cdb[2];
297297
// 512 byte offset of where to put these bytes.
298298
uint32_t offset = ((uint32_t)scsiDev.cdb[3] << 16) | ((uint32_t)scsiDev.cdb[4] << 8) | scsiDev.cdb[5];
299-
uint16_t buf_size = 512;
300-
uint8_t buf[512];
299+
const uint16_t BUFSIZE = 512;
300+
uint8_t buf[BUFSIZE];
301301

302-
// Check if last block of file, and not the only bock in file.
303-
if(bytes_sent < buf_size)
302+
// Do not allow buffer overrun
303+
if (bytes_sent > BUFSIZE)
304304
{
305-
buf_size = bytes_sent;
305+
debuglog("TOOLBOX SEND FILE 10 ILLEGAL DATA SIZE");
306+
gFile.close();
307+
scsiDev.status = CHECK_CONDITION;
308+
scsiDev.target->sense.code = ILLEGAL_REQUEST;
306309
}
307310

308311
scsiEnterPhase(DATA_OUT);
309312
scsiRead(buf, bytes_sent, NULL);
310313
gFile.seekCur(offset * 512);
311-
gFile.write(buf, buf_size);
314+
gFile.write(buf, bytes_sent);
312315
if(gFile.getWriteError())
313316
{
314317
gFile.clearWriteError();

0 commit comments

Comments
 (0)