@@ -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+
93150static 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 )
100157int 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 ;
0 commit comments