@@ -231,10 +231,11 @@ static ssize_t fastboot_usbdev_read(FAR struct fastboot_ctx_s *ctx,
231
231
FAR void * buf , size_t len );
232
232
static int fastboot_usbdev_write (FAR struct fastboot_ctx_s * ctx ,
233
233
FAR const void * buf , size_t len );
234
- #elif defined( CONFIG_NET_TCP )
234
+ #endif
235
235
236
236
/* TCP transport */
237
237
238
+ #ifdef CONFIG_NET_TCP
238
239
static int fastboot_tcp_initialize (FAR struct fastboot_ctx_s * ctx );
239
240
static void fastboot_tcp_deinit (FAR struct fastboot_ctx_s * ctx );
240
241
static ssize_t fastboot_tcp_read (FAR struct fastboot_ctx_s * ctx ,
@@ -275,23 +276,25 @@ static const struct memory_region_s g_memory_region[] =
275
276
};
276
277
#endif
277
278
278
- #ifdef CONFIG_USBFASTBOOT
279
- static const struct fastboot_transport_ops_s g_tran_ops_usb =
279
+ static const struct fastboot_transport_ops_s g_tran_ops [] =
280
280
{
281
- .init = fastboot_usbdev_initialize ,
282
- .deinit = fastboot_usbdev_deinit ,
283
- .read = fastboot_usbdev_read ,
284
- .write = fastboot_usbdev_write ,
285
- };
286
- #elif defined(CONFIG_NET_TCP )
287
- static const struct fastboot_transport_ops_s g_tran_ops_tcp =
288
- {
289
- .init = fastboot_tcp_initialize ,
290
- .deinit = fastboot_tcp_deinit ,
291
- .read = fastboot_tcp_read ,
292
- .write = fastboot_tcp_write ,
293
- };
281
+ #ifdef CONFIG_USBFASTBOOT
282
+ {
283
+ .init = fastboot_usbdev_initialize ,
284
+ .deinit = fastboot_usbdev_deinit ,
285
+ .read = fastboot_usbdev_read ,
286
+ .write = fastboot_usbdev_write ,
287
+ },
288
+ #endif
289
+ #ifdef CONFIG_NET_TCP
290
+ {
291
+ .init = fastboot_tcp_initialize ,
292
+ .deinit = fastboot_tcp_deinit ,
293
+ .read = fastboot_tcp_read ,
294
+ .write = fastboot_tcp_write ,
295
+ },
294
296
#endif
297
+ };
295
298
296
299
/****************************************************************************
297
300
* Private Functions
@@ -660,6 +663,11 @@ static void fastboot_download(FAR struct fastboot_ctx_s *ctx,
660
663
ssize_t r = ctx -> ops -> read (ctx , download , len );
661
664
if (r < 0 )
662
665
{
666
+ if (errno == EAGAIN )
667
+ {
668
+ continue ;
669
+ }
670
+
663
671
ctx -> download_size = 0 ;
664
672
fb_err ("fastboot_download usb read error\n" );
665
673
return ;
@@ -978,28 +986,34 @@ static void fastboot_oem(FAR struct fastboot_ctx_s *ctx, FAR const char *arg)
978
986
}
979
987
}
980
988
981
- static void fastboot_command_loop (FAR struct fastboot_ctx_s * ctx )
989
+ static void fastboot_command_loop (FAR struct fastboot_ctx_s * ctx ,
990
+ size_t nctx )
982
991
{
983
- struct epoll_event ev [1 ];
992
+ FAR struct fastboot_ctx_s * c ;
993
+ struct epoll_event ev [nctx ];
984
994
int epfd ;
995
+ int n ;
985
996
986
- if (ctx -> left > 0 )
997
+ epfd = epoll_create (1 );
998
+ if (epfd < 0 )
987
999
{
988
- epfd = epoll_create (1 );
989
- if (epfd < 0 )
990
- {
991
- fb_err ("open epoll failed %d" , errno );
992
- return ;
993
- }
1000
+ fb_err ("open epoll failed %d" , errno );
1001
+ return ;
1002
+ }
994
1003
995
- ev [0 ].events = EPOLLIN ;
996
- ev [0 ].data .ptr = ctx ;
997
- if (epoll_ctl (epfd , EPOLL_CTL_ADD , ctx -> tran_fd [0 ], & ev [0 ]) < 0 )
1004
+ for (c = ctx , n = nctx ; n -- > 0 ; c ++ )
1005
+ {
1006
+ ev [n ].events = EPOLLIN ;
1007
+ ev [n ].data .ptr = c ;
1008
+ if (epoll_ctl (epfd , EPOLL_CTL_ADD , c -> tran_fd [0 ], & ev [n ]) < 0 )
998
1009
{
999
- fb_err ("err add poll %d" , ctx -> tran_fd [0 ]);
1010
+ fb_err ("err add poll %d" , c -> tran_fd [0 ]);
1000
1011
return ;
1001
1012
}
1013
+ }
1002
1014
1015
+ if (ctx -> left > 0 )
1016
+ {
1003
1017
if (epoll_wait (epfd , ev , nitems (ev ), ctx -> left ) <= 0 )
1004
1018
{
1005
1019
return ;
@@ -1008,35 +1022,43 @@ static void fastboot_command_loop(FAR struct fastboot_ctx_s *ctx)
1008
1022
1009
1023
/* Reinitialize for storing TCP transport remaining data size. */
1010
1024
1011
- ctx -> left = 0 ;
1025
+ for (c = ctx , n = nctx ; n -- > 0 ; c ++ )
1026
+ {
1027
+ c -> left = 0 ;
1028
+ }
1012
1029
1013
1030
while (1 )
1014
1031
{
1015
1032
char buffer [FASTBOOT_MSG_LEN + 1 ];
1016
1033
size_t ncmds = nitems (g_fast_cmd );
1017
1034
size_t index ;
1018
1035
1019
- ssize_t r = ctx -> ops -> read ( ctx , buffer , FASTBOOT_MSG_LEN );
1020
- if ( r < 0 )
1036
+ n = epoll_wait ( epfd , ev , nitems ( ev ), -1 );
1037
+ for ( n -- ; n >= 0 ; )
1021
1038
{
1022
- fb_err ("fastboot transport read() %zd\n" , r );
1023
- continue ;
1024
- }
1039
+ c = (FAR struct fastboot_ctx_s * )ev [n ].data .ptr ;
1040
+ ssize_t r = c -> ops -> read (c , buffer , FASTBOOT_MSG_LEN );
1041
+ if (r <= 0 )
1042
+ {
1043
+ n -- ;
1044
+ continue ;
1045
+ }
1025
1046
1026
- buffer [r ] = '\0' ;
1027
- for (index = 0 ; index < ncmds ; index ++ )
1028
- {
1029
- size_t len = strlen (g_fast_cmd [index ].prefix );
1030
- if (memcmp (buffer , g_fast_cmd [index ].prefix , len ) == 0 )
1047
+ buffer [r ] = '\0' ;
1048
+ for (index = 0 ; index < ncmds ; index ++ )
1031
1049
{
1032
- g_fast_cmd [index ].handle (ctx , buffer + len );
1033
- break ;
1050
+ size_t len = strlen (g_fast_cmd [index ].prefix );
1051
+ if (memcmp (buffer , g_fast_cmd [index ].prefix , len ) == 0 )
1052
+ {
1053
+ g_fast_cmd [index ].handle (c , buffer + len );
1054
+ break ;
1055
+ }
1034
1056
}
1035
- }
1036
1057
1037
- if (index == ncmds )
1038
- {
1039
- fastboot_fail (ctx , "Unknown command" );
1058
+ if (index == ncmds )
1059
+ {
1060
+ fastboot_fail (c , "Unknown command" );
1061
+ }
1040
1062
}
1041
1063
}
1042
1064
}
@@ -1062,25 +1084,33 @@ static void fastboot_publish(FAR struct fastboot_ctx_s *ctx,
1062
1084
ctx -> varlist = var ;
1063
1085
}
1064
1086
1065
- static void fastboot_create_publish (FAR struct fastboot_ctx_s * ctx )
1087
+ static void fastboot_create_publish (FAR struct fastboot_ctx_s * ctx ,
1088
+ size_t nctx )
1066
1089
{
1067
- fastboot_publish (ctx , "product" , "NuttX" , 0 );
1068
- fastboot_publish (ctx , "kernel" , "NuttX" , 0 );
1069
- fastboot_publish (ctx , "version" , CONFIG_VERSION_STRING , 0 );
1070
- fastboot_publish (ctx , "slot-count" , "1" , 0 );
1071
- fastboot_publish (ctx , "max-download-size" , NULL ,
1072
- CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX );
1090
+ for (; nctx -- > 0 ; ctx ++ )
1091
+ {
1092
+ fastboot_publish (ctx , "product" , "NuttX" , 0 );
1093
+ fastboot_publish (ctx , "kernel" , "NuttX" , 0 );
1094
+ fastboot_publish (ctx , "version" , CONFIG_VERSION_STRING , 0 );
1095
+ fastboot_publish (ctx , "slot-count" , "1" , 0 );
1096
+ fastboot_publish (ctx , "max-download-size" , NULL ,
1097
+ CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX );
1098
+ }
1073
1099
}
1074
1100
1075
- static void fastboot_free_publish (FAR struct fastboot_ctx_s * ctx )
1101
+ static void fastboot_free_publish (FAR struct fastboot_ctx_s * ctx ,
1102
+ size_t nctx )
1076
1103
{
1077
1104
FAR struct fastboot_var_s * next ;
1078
1105
1079
- while ( ctx -> varlist )
1106
+ for (; nctx -- > 0 ; ctx ++ )
1080
1107
{
1081
- next = ctx -> varlist -> next ;
1082
- free (ctx -> varlist );
1083
- ctx -> varlist = next ;
1108
+ while (ctx -> varlist )
1109
+ {
1110
+ next = ctx -> varlist -> next ;
1111
+ free (ctx -> varlist );
1112
+ ctx -> varlist = next ;
1113
+ }
1084
1114
}
1085
1115
}
1086
1116
@@ -1149,15 +1179,15 @@ static int fastboot_usbdev_initialize(FAR struct fastboot_ctx_s *ctx)
1149
1179
}
1150
1180
#endif /* SYSTEM_FASTBOOTD_USB_BOARDCTL */
1151
1181
1152
- ctx -> tran_fd [0 ] =
1153
- fastboot_open_usb ( FASTBOOT_EP_BULKOUT_IDX , O_RDONLY | O_CLOEXEC );
1182
+ ctx -> tran_fd [0 ] = fastboot_open_usb ( FASTBOOT_EP_BULKOUT_IDX ,
1183
+ O_RDONLY | O_CLOEXEC | O_NONBLOCK );
1154
1184
if (ctx -> tran_fd [0 ] < 0 )
1155
1185
{
1156
1186
return ctx -> tran_fd [0 ];
1157
1187
}
1158
1188
1159
- ctx -> tran_fd [1 ] =
1160
- fastboot_open_usb ( FASTBOOT_EP_BULKIN_IDX , O_WRONLY | O_CLOEXEC );
1189
+ ctx -> tran_fd [1 ] = fastboot_open_usb ( FASTBOOT_EP_BULKIN_IDX ,
1190
+ O_WRONLY | O_CLOEXEC | O_NONBLOCK );
1161
1191
if (ctx -> tran_fd [1 ] < 0 )
1162
1192
{
1163
1193
close (ctx -> tran_fd [0 ]);
@@ -1190,13 +1220,15 @@ static int fastboot_usbdev_write(FAR struct fastboot_ctx_s *ctx,
1190
1220
{
1191
1221
return fastboot_write (ctx -> tran_fd [1 ], buf , len );
1192
1222
}
1223
+ #endif
1193
1224
1194
- #elif defined( CONFIG_NET_TCP )
1225
+ #ifdef CONFIG_NET_TCP
1195
1226
static int fastboot_tcp_initialize (FAR struct fastboot_ctx_s * ctx )
1196
1227
{
1197
1228
struct sockaddr_in addr ;
1198
1229
1199
- ctx -> tran_fd [0 ] = socket (AF_INET , SOCK_STREAM , 0 );
1230
+ ctx -> tran_fd [0 ] = socket (AF_INET , SOCK_STREAM ,
1231
+ SOCK_CLOEXEC | SOCK_NONBLOCK );
1200
1232
if (ctx -> tran_fd [0 ] < 0 )
1201
1233
{
1202
1234
fb_err ("create socket failed %d" , errno );
@@ -1357,36 +1389,54 @@ static int fastboot_tcp_write(FAR struct fastboot_ctx_s *ctx,
1357
1389
}
1358
1390
#endif
1359
1391
1360
- static int fastboot_context_initialize (FAR struct fastboot_ctx_s * ctx )
1392
+ static int fastboot_context_initialize (FAR struct fastboot_ctx_s * ctx ,
1393
+ size_t nctx )
1361
1394
{
1362
- ctx -> download_max = CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX ;
1363
- ctx -> download_offset = 0 ;
1364
- ctx -> download_size = 0 ;
1365
- ctx -> flash_fd = -1 ;
1366
- ctx -> total_imgsize = 0 ;
1367
- ctx -> varlist = NULL ;
1368
- ctx -> left = 0 ;
1369
- #ifdef CONFIG_USBFASTBOOT
1370
- ctx -> ops = & g_tran_ops_usb ;
1371
- #elif defined(CONFIG_NET_TCP )
1372
- ctx -> ops = & g_tran_ops_tcp ;
1373
- #endif
1374
- ctx -> tran_fd [0 ] = -1 ;
1375
- ctx -> tran_fd [1 ] = -1 ;
1395
+ int ret ;
1376
1396
1377
- ctx -> download_buffer = malloc (CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX );
1378
- if (ctx -> download_buffer == NULL )
1379
- {
1380
- fb_err ("ERROR: Could not allocate the memory.\n" );
1381
- return - errno ;
1397
+ for (; nctx -- > 0 ; ctx ++ )
1398
+ {
1399
+ ctx -> download_max = CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX ;
1400
+ ctx -> download_offset = 0 ;
1401
+ ctx -> download_size = 0 ;
1402
+ ctx -> flash_fd = -1 ;
1403
+ ctx -> total_imgsize = 0 ;
1404
+ ctx -> varlist = NULL ;
1405
+ ctx -> left = ctx [0 ].left ;
1406
+ ctx -> ops = & g_tran_ops [nctx ];
1407
+ ctx -> tran_fd [0 ] = -1 ;
1408
+ ctx -> tran_fd [1 ] = -1 ;
1409
+
1410
+ ctx -> download_buffer = malloc (CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX );
1411
+ if (ctx -> download_buffer == NULL )
1412
+ {
1413
+ fb_err ("ERROR: Could not allocate the memory.\n" );
1414
+ continue ;
1415
+ }
1416
+
1417
+ ret = ctx -> ops -> init (ctx );
1418
+ if (ret < 0 )
1419
+ {
1420
+ free (ctx -> download_buffer );
1421
+ ctx -> download_buffer = NULL ;
1422
+ ctx -> ops -> deinit (ctx );
1423
+ }
1382
1424
}
1383
1425
1384
1426
return 0 ;
1385
1427
}
1386
1428
1387
- static void fastboot_context_deinit (FAR struct fastboot_ctx_s * ctx )
1429
+ static void fastboot_context_deinit (FAR struct fastboot_ctx_s * ctx ,
1430
+ size_t nctx )
1388
1431
{
1389
- free (ctx -> download_buffer );
1432
+ for (; nctx -- > 0 ; ctx ++ )
1433
+ {
1434
+ if (ctx -> download_buffer )
1435
+ {
1436
+ ctx -> ops -> deinit (ctx );
1437
+ free (ctx -> download_buffer );
1438
+ }
1439
+ }
1390
1440
}
1391
1441
1392
1442
/****************************************************************************
@@ -1395,15 +1445,9 @@ static void fastboot_context_deinit(FAR struct fastboot_ctx_s *ctx)
1395
1445
1396
1446
int main (int argc , FAR char * * argv )
1397
1447
{
1398
- struct fastboot_ctx_s context ;
1448
+ struct fastboot_ctx_s context [ nitems ( g_tran_ops )] ;
1399
1449
int ret ;
1400
1450
1401
- ret = fastboot_context_initialize (& context );
1402
- if (ret < 0 )
1403
- {
1404
- return ret ;
1405
- }
1406
-
1407
1451
if (argc > 1 )
1408
1452
{
1409
1453
if (strcmp (argv [1 ], "-h" ) == 0 )
@@ -1414,23 +1458,22 @@ int main(int argc, FAR char **argv)
1414
1458
return 0 ;
1415
1459
}
1416
1460
1417
- if (sscanf (argv [1 ], "%" SCNu64 , & context .left ) != 1 )
1461
+ if (sscanf (argv [1 ], "%" SCNu64 , & context [ 0 ] .left ) != 1 )
1418
1462
{
1419
1463
return - EINVAL ;
1420
1464
}
1421
1465
}
1422
1466
1423
- ret = context . ops -> init ( & context );
1467
+ ret = fastboot_context_initialize ( context , nitems ( context ) );
1424
1468
if (ret < 0 )
1425
1469
{
1426
1470
return ret ;
1427
1471
}
1428
1472
1429
- fastboot_create_publish (& context );
1430
- fastboot_command_loop (& context );
1431
- fastboot_free_publish (& context );
1432
- context .ops -> deinit (& context );
1433
- fastboot_context_deinit (& context );
1473
+ fastboot_create_publish (context , nitems (context ));
1474
+ fastboot_command_loop (context , nitems (context ));
1475
+ fastboot_free_publish (context , nitems (context ));
1476
+ fastboot_context_deinit (context , nitems (context ));
1434
1477
1435
1478
return ret ;
1436
1479
}
0 commit comments