Skip to content

Commit 52151ab

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

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

isd.cpp

Lines changed: 65 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,63 @@ 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 *at_sign = strchr(arg, '@');
97+
if ( at_sign == NULL ){
98+
std::fprintf( stderr, "missing '@'\n" );
99+
return 1;
100+
}
101+
102+
*at_sign = '\0';
103+
104+
const char *ku_path = at_sign + 1;
105+
106+
char buf[512*1024];
107+
108+
FILE *fp = fopen( arg, "rb" );
109+
110+
if ( fp == NULL ){
111+
std::perror("fopen");
112+
return 1;
113+
}
114+
115+
struct stat stat_buf;
116+
if ( stat( arg, &stat_buf ) < 0 ) {
117+
std::perror("stat");
118+
fclose( fp );
119+
return 1;
120+
}
121+
122+
size_t len = stat_buf.st_size;
123+
if ( len >= sizeof(buf) ) {
124+
std::fprintf( stderr, "kernel image too large\n" );
125+
fclose( fp );
126+
return 1;
127+
}
128+
129+
if ( fread( buf, 1, len, fp ) != len ) {
130+
std::perror("fread");
131+
fclose( fp );
132+
return 1;
133+
}
134+
135+
fclose( fp );
136+
137+
fp = fopen( ku_path, "rb" );
138+
if ( fp == NULL ){
139+
std::perror("fopen");
140+
return 1;
141+
}
142+
143+
d.setAppStat( n::piece::Device::APP_STOP );
144+
d.writeMem( 0x102c00, buf, len );
145+
d.uploadSrf( fp );
146+
d.setAppStat( n::piece::Device::APP_RUN );
147+
fclose( fp );
148+
return 0;
149+
}
150+
93151
static int fs_status( n::piece::Device &d, n::piece::Fs &fs )
94152
{
95153
size_t size = fs.getFreeBlockCount();
@@ -100,12 +158,12 @@ static int fs_status( n::piece::Device &d, n::piece::Fs &fs )
100158
int main( int argc, char **argv )
101159
{
102160
try {
103-
161+
104162
n::piece::Device d;
105163
n::piece::Fs fs( d );
106164
while ( 1 ) {
107-
int c = getopt( argc, argv, "lr:d:c?hs:fFvR:" );
108-
165+
int c = getopt( argc, argv, "lr:d:c?hs:fFvR:k:" );
166+
109167
switch ( c ) {
110168
case 'l':
111169
fs.dumpDir();
@@ -143,12 +201,15 @@ int main( int argc, char **argv )
143201
case 'R':
144202
return run_srf_file( d, optarg );
145203

204+
case 'k':
205+
return update_kernel( d, optarg );
206+
146207
default:
147208
usage( stderr );
148209
return 1;
149210
}
150211
}
151-
212+
152213
} catch ( const char *err ) {
153214
std::fprintf( stderr, "%s\n", err );
154215
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)