Skip to content

Commit

Permalink
use proper FAT_END constant, add kernel update support
Browse files Browse the repository at this point in the history
  • Loading branch information
UNDEF2 committed Jul 14, 2024
1 parent dd110b2 commit 52151ab
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
69 changes: 65 additions & 4 deletions isd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static void usage( std::FILE *out )
std::fputs("isd -R filename -- run srf file\n",out);
std::fputs("isd -r filename -- download file\n",out);
std::fputs("isd -s filename -- upload file\n",out);
std::fputs("isd -k all.img@ku.srf -- update kernel\n",out);
std::fputs("isd -v -- show version\n",out);
}

Expand Down Expand Up @@ -90,6 +91,63 @@ static int run_srf_file( n::piece::Device &d, char *fname )
return 0;
}

static int update_kernel( n::piece::Device &d, char *arg )
{
char *at_sign = strchr(arg, '@');
if ( at_sign == NULL ){
std::fprintf( stderr, "missing '@'\n" );
return 1;
}

*at_sign = '\0';

const char *ku_path = at_sign + 1;

char buf[512*1024];

FILE *fp = fopen( arg, "rb" );

if ( fp == NULL ){
std::perror("fopen");
return 1;
}

struct stat stat_buf;
if ( stat( arg, &stat_buf ) < 0 ) {
std::perror("stat");
fclose( fp );
return 1;
}

size_t len = stat_buf.st_size;
if ( len >= sizeof(buf) ) {
std::fprintf( stderr, "kernel image too large\n" );
fclose( fp );
return 1;
}

if ( fread( buf, 1, len, fp ) != len ) {
std::perror("fread");
fclose( fp );
return 1;
}

fclose( fp );

fp = fopen( ku_path, "rb" );
if ( fp == NULL ){
std::perror("fopen");
return 1;
}

d.setAppStat( n::piece::Device::APP_STOP );
d.writeMem( 0x102c00, buf, len );
d.uploadSrf( fp );
d.setAppStat( n::piece::Device::APP_RUN );
fclose( fp );
return 0;
}

static int fs_status( n::piece::Device &d, n::piece::Fs &fs )
{
size_t size = fs.getFreeBlockCount();
Expand All @@ -100,12 +158,12 @@ static int fs_status( n::piece::Device &d, n::piece::Fs &fs )
int main( int argc, char **argv )
{
try {

n::piece::Device d;
n::piece::Fs fs( d );
while ( 1 ) {
int c = getopt( argc, argv, "lr:d:c?hs:fFvR:" );
int c = getopt( argc, argv, "lr:d:c?hs:fFvR:k:" );

switch ( c ) {
case 'l':
fs.dumpDir();
Expand Down Expand Up @@ -143,12 +201,15 @@ int main( int argc, char **argv )
case 'R':
return run_srf_file( d, optarg );

case 'k':
return update_kernel( d, optarg );

default:
usage( stderr );
return 1;
}
}

} catch ( const char *err ) {
std::fprintf( stderr, "%s\n", err );
return 1;
Expand Down
6 changes: 3 additions & 3 deletions piecefat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace {
void fileNotFound( const char *fname )
{
static char tmp[64];
sprintf( tmp, "%s: file not found", fname );
snprintf( tmp, sizeof(tmp), "%s: file not found", fname );

throw tmp;
}
Expand Down Expand Up @@ -123,7 +123,7 @@ void Fs::makeChain( uint16_t *pchain, int blkcnt, int next )
uint16_t *fat = master_block_.fat_chain;

if ( blkcnt <= 0 ) {
*pchain = MAXFAT+1;
*pchain = FAT_END;
return;
}

Expand Down Expand Up @@ -291,7 +291,7 @@ void Fs::format()

memset( m->directory, 0, sizeof(m->directory) );
memset( m->fat_chain, 0xff, sizeof(m->fat_chain) );
m->fat_chain[0] = MAXFAT+1;
m->fat_chain[0] = FAT_END;

update();
}
Expand Down
1 change: 1 addition & 0 deletions piecefat.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Fs
static const int MAXDIR=96;
static const int MAXFAT=496;
static const uint16_t FAT_FREE=0xffff;
static const uint16_t FAT_END=0xeeee;
static const size_t FNAME_LEN=23;

struct Directory {
Expand Down

0 comments on commit 52151ab

Please sign in to comment.