@@ -1032,7 +1032,7 @@ static int fastboot_open_usb(int index, int flags)
1032
1032
return - errno ;
1033
1033
}
1034
1034
1035
- static int fastboot_usbdev_initialize (void )
1035
+ static int fastboot_usbdev_initialize (FAR struct fastboot_ctx_s * ctx )
1036
1036
{
1037
1037
#ifdef CONFIG_SYSTEM_FASTBOOTD_USB_BOARDCTL
1038
1038
struct boardioc_usbdev_ctrl_s ctrl ;
@@ -1071,18 +1071,72 @@ static int fastboot_usbdev_initialize(void)
1071
1071
}
1072
1072
#endif /* SYSTEM_FASTBOOTD_USB_BOARDCTL */
1073
1073
1074
+ ctx -> usbdev_in =
1075
+ fastboot_open_usb (FASTBOOT_EP_BULKOUT_IDX , O_RDONLY | O_CLOEXEC );
1076
+ if (ctx -> usbdev_in < 0 )
1077
+ {
1078
+ return ctx -> usbdev_in ;
1079
+ }
1080
+
1081
+ ctx -> usbdev_out =
1082
+ fastboot_open_usb (FASTBOOT_EP_BULKIN_IDX , O_WRONLY | O_CLOEXEC );
1083
+ if (ctx -> usbdev_out < 0 )
1084
+ {
1085
+ close (ctx -> usbdev_in );
1086
+ ctx -> usbdev_in = -1 ;
1087
+ return ctx -> usbdev_out ;
1088
+ }
1089
+
1090
+ return 0 ;
1091
+ }
1092
+
1093
+ static void fastboot_usbdev_deinit (FAR struct fastboot_ctx_s * ctx )
1094
+ {
1095
+ close (ctx -> usbdev_out );
1096
+ ctx -> usbdev_out = -1 ;
1097
+ close (ctx -> usbdev_in );
1098
+ ctx -> usbdev_in = -1 ;
1099
+ }
1100
+
1101
+ static int fastboot_context_initialize (FAR struct fastboot_ctx_s * ctx )
1102
+ {
1103
+ ctx -> download_max = CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX ;
1104
+ ctx -> download_offset = 0 ;
1105
+ ctx -> download_size = 0 ;
1106
+ ctx -> flash_fd = -1 ;
1107
+ ctx -> total_imgsize = 0 ;
1108
+ ctx -> varlist = NULL ;
1109
+ ctx -> wait_ms = 0 ;
1110
+
1111
+ ctx -> download_buffer = malloc (CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX );
1112
+ if (ctx -> download_buffer == NULL )
1113
+ {
1114
+ fb_err ("ERROR: Could not allocate the memory.\n" );
1115
+ return - errno ;
1116
+ }
1117
+
1074
1118
return 0 ;
1075
1119
}
1076
1120
1121
+ static void fastboot_context_deinit (FAR struct fastboot_ctx_s * ctx )
1122
+ {
1123
+ free (ctx -> download_buffer );
1124
+ }
1125
+
1077
1126
/****************************************************************************
1078
1127
* Public Functions
1079
1128
****************************************************************************/
1080
1129
1081
1130
int main (int argc , FAR char * * argv )
1082
1131
{
1083
1132
struct fastboot_ctx_s context ;
1084
- FAR void * buffer = NULL ;
1085
- int ret = OK ;
1133
+ int ret ;
1134
+
1135
+ ret = fastboot_context_initialize (& context );
1136
+ if (ret < 0 )
1137
+ {
1138
+ return ret ;
1139
+ }
1086
1140
1087
1141
if (argc > 1 )
1088
1142
{
@@ -1096,60 +1150,18 @@ int main(int argc, FAR char **argv)
1096
1150
1097
1151
context .wait_ms = atoi (argv [1 ]);
1098
1152
}
1099
- else
1100
- {
1101
- context .wait_ms = 0 ;
1102
- }
1103
1153
1104
- ret = fastboot_usbdev_initialize ();
1154
+ ret = fastboot_usbdev_initialize (& context );
1105
1155
if (ret < 0 )
1106
1156
{
1107
1157
return ret ;
1108
1158
}
1109
1159
1110
- buffer = malloc (CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX );
1111
- if (buffer == NULL )
1112
- {
1113
- fb_err ("ERROR: Could not allocate the memory.\n" );
1114
- return - ENOMEM ;
1115
- }
1116
-
1117
- context .usbdev_in =
1118
- fastboot_open_usb (FASTBOOT_EP_BULKOUT_IDX , O_RDONLY | O_CLOEXEC );
1119
- if (context .usbdev_in < 0 )
1120
- {
1121
- ret = - errno ;
1122
- goto err_with_mem ;
1123
- }
1124
-
1125
- context .usbdev_out =
1126
- fastboot_open_usb (FASTBOOT_EP_BULKIN_IDX , O_WRONLY | O_CLOEXEC );
1127
- if (context .usbdev_out < 0 )
1128
- {
1129
- ret = - errno ;
1130
- goto err_with_in ;
1131
- }
1132
-
1133
- context .varlist = NULL ;
1134
- context .flash_fd = -1 ;
1135
- context .download_buffer = buffer ;
1136
- context .download_size = 0 ;
1137
- context .download_offset = 0 ;
1138
- context .download_max = CONFIG_SYSTEM_FASTBOOTD_DOWNLOAD_MAX ;
1139
- context .total_imgsize = 0 ;
1140
-
1141
1160
fastboot_create_publish (& context );
1142
1161
fastboot_command_loop (& context );
1143
1162
fastboot_free_publish (& context );
1163
+ fastboot_usbdev_deinit (& context );
1164
+ fastboot_context_deinit (& context );
1144
1165
1145
- close (context .usbdev_out );
1146
- context .usbdev_out = -1 ;
1147
-
1148
- err_with_in :
1149
- close (context .usbdev_in );
1150
- context .usbdev_in = -1 ;
1151
-
1152
- err_with_mem :
1153
- free (buffer );
1154
1166
return ret ;
1155
1167
}
0 commit comments