Skip to content

Commit 75033b7

Browse files
JianyuWang0623xiaoxiang781216
authored andcommitted
system/fastboot: add func for context and usbdev
fastboot_context_initialize : initialize fastboot context fastboot_context_deinit : deinitialize fastboot context fastboot_usbdev_initialize : initialize usbdev and open USB endpoint fastboot_usbdev_deinit : close USB endpoint Signed-off-by: wangjianyu3 <[email protected]>
1 parent 88fea63 commit 75033b7

File tree

1 file changed

+60
-48
lines changed

1 file changed

+60
-48
lines changed

system/fastboot/fastboot.c

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ static int fastboot_open_usb(int index, int flags)
10321032
return -errno;
10331033
}
10341034

1035-
static int fastboot_usbdev_initialize(void)
1035+
static int fastboot_usbdev_initialize(FAR struct fastboot_ctx_s *ctx)
10361036
{
10371037
#ifdef CONFIG_SYSTEM_FASTBOOTD_USB_BOARDCTL
10381038
struct boardioc_usbdev_ctrl_s ctrl;
@@ -1071,18 +1071,72 @@ static int fastboot_usbdev_initialize(void)
10711071
}
10721072
#endif /* SYSTEM_FASTBOOTD_USB_BOARDCTL */
10731073

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+
10741118
return 0;
10751119
}
10761120

1121+
static void fastboot_context_deinit(FAR struct fastboot_ctx_s *ctx)
1122+
{
1123+
free(ctx->download_buffer);
1124+
}
1125+
10771126
/****************************************************************************
10781127
* Public Functions
10791128
****************************************************************************/
10801129

10811130
int main(int argc, FAR char **argv)
10821131
{
10831132
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+
}
10861140

10871141
if (argc > 1)
10881142
{
@@ -1096,60 +1150,18 @@ int main(int argc, FAR char **argv)
10961150

10971151
context.wait_ms = atoi(argv[1]);
10981152
}
1099-
else
1100-
{
1101-
context.wait_ms = 0;
1102-
}
11031153

1104-
ret = fastboot_usbdev_initialize();
1154+
ret = fastboot_usbdev_initialize(&context);
11051155
if (ret < 0)
11061156
{
11071157
return ret;
11081158
}
11091159

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-
11411160
fastboot_create_publish(&context);
11421161
fastboot_command_loop(&context);
11431162
fastboot_free_publish(&context);
1163+
fastboot_usbdev_deinit(&context);
1164+
fastboot_context_deinit(&context);
11441165

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);
11541166
return ret;
11551167
}

0 commit comments

Comments
 (0)