Skip to content

Commit 25fab4e

Browse files
committed
use proper FAT_END constant, add kernel update support
1 parent dd110b2 commit 25fab4e

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

isd.cpp

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ static void usage( std::FILE *out )
1818
std::fputs("isd -R filename -- run srf file\n",out);
1919
std::fputs("isd -r filename -- download file\n",out);
2020
std::fputs("isd -s filename -- upload file\n",out);
21+
std::fputs("isd -k all.img@ku.srf -- update kernel\n",out);
2122
std::fputs("isd -v -- show version\n",out);
2223
}
2324

@@ -90,6 +91,62 @@ static int run_srf_file( n::piece::Device &d, char *fname )
9091
return 0;
9192
}
9293

94+
static int update_kernel( n::piece::Device &d, char *arg )
95+
{
96+
char *ampersand = strchr(arg, '@');
97+
if ( ampersand == NULL ){
98+
return 1;
99+
}
100+
101+
*ampersand = '\0';
102+
103+
const char *ku_path = ampersand + 1;
104+
105+
char buf[512*1024];
106+
107+
FILE *fp = fopen( arg, "rb" );
108+
109+
if ( fp == NULL ){
110+
std::perror("fopen");
111+
return 1;
112+
}
113+
114+
struct stat stat_buf;
115+
if ( stat( arg, &stat_buf ) < 0 ) {
116+
std::perror("stat");
117+
fclose( fp );
118+
return 1;
119+
}
120+
121+
size_t len = stat_buf.st_size;
122+
if ( len >= sizeof(buf) ) {
123+
std::fprintf( stderr, "kernel image too large\n" );
124+
fclose( fp );
125+
return 1;
126+
}
127+
128+
if ( fread( buf, 1, len, fp ) != len ) {
129+
std::perror("fread");
130+
fclose( fp );
131+
return 1;
132+
}
133+
134+
fclose( fp );
135+
136+
fp = fopen( ku_path, "rb" );
137+
if ( fp == NULL ){
138+
std::perror("fopen");
139+
return 1;
140+
}
141+
142+
d.setAppStat( n::piece::Device::APP_STOP );
143+
d.writeMem( 0x102c00, buf, len );
144+
d.uploadSrf( fp );
145+
d.setAppStat( n::piece::Device::APP_RUN );
146+
fclose( fp );
147+
return 0;
148+
}
149+
93150
static int fs_status( n::piece::Device &d, n::piece::Fs &fs )
94151
{
95152
size_t size = fs.getFreeBlockCount();
@@ -100,12 +157,12 @@ static int fs_status( n::piece::Device &d, n::piece::Fs &fs )
100157
int main( int argc, char **argv )
101158
{
102159
try {
103-
160+
104161
n::piece::Device d;
105162
n::piece::Fs fs( d );
106163
while ( 1 ) {
107-
int c = getopt( argc, argv, "lr:d:c?hs:fFvR:" );
108-
164+
int c = getopt( argc, argv, "lr:d:c?hs:fFvR:k:" );
165+
109166
switch ( c ) {
110167
case 'l':
111168
fs.dumpDir();
@@ -143,12 +200,15 @@ int main( int argc, char **argv )
143200
case 'R':
144201
return run_srf_file( d, optarg );
145202

203+
case 'k':
204+
return update_kernel( d, optarg );
205+
146206
default:
147207
usage( stderr );
148208
return 1;
149209
}
150210
}
151-
211+
152212
} catch ( const char *err ) {
153213
std::fprintf( stderr, "%s\n", err );
154214
return 1;

piecefat.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace {
1010
void fileNotFound( const char *fname )
1111
{
1212
static char tmp[64];
13-
sprintf( tmp, "%s: file not found", fname );
13+
snprintf( tmp, sizeof(tmp), "%s: file not found", fname );
1414

1515
throw tmp;
1616
}
@@ -123,7 +123,7 @@ void Fs::makeChain( uint16_t *pchain, int blkcnt, int next )
123123
uint16_t *fat = master_block_.fat_chain;
124124

125125
if ( blkcnt <= 0 ) {
126-
*pchain = MAXFAT+1;
126+
*pchain = FAT_END;
127127
return;
128128
}
129129

@@ -291,7 +291,7 @@ void Fs::format()
291291

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

296296
update();
297297
}

piecefat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Fs
1616
static const int MAXDIR=96;
1717
static const int MAXFAT=496;
1818
static const uint16_t FAT_FREE=0xffff;
19+
static const uint16_t FAT_END=0xeeee;
1920
static const size_t FNAME_LEN=23;
2021

2122
struct Directory {

0 commit comments

Comments
 (0)