1
1
// SPDX-License-Identifier: GPL-2.0-only
2
2
3
3
use std:: fs:: read_dir;
4
- use std:: os:: unix:: ffi:: OsStrExt ;
5
4
use std:: os:: unix:: fs:: symlink;
6
- use std:: { thread, time} ;
5
+ use std:: thread;
6
+ use std:: time:: Duration ;
7
7
8
8
use log:: debug;
9
9
@@ -12,15 +12,21 @@ use crate::mount::mount_apivfs;
12
12
use crate :: util:: { mkdir, write_file} ;
13
13
use crate :: Result ;
14
14
15
- fn setup_9pfs_gadget ( device : & String ) -> Result < ( ) > {
15
+ fn setup_9pfs_gadget ( device : Option < & str > ) -> Result < ( ) > {
16
16
debug ! ( "Initializing USB 9pfs gadget ..." ) ;
17
17
18
- let udc = read_dir ( "/sys/class/udc" )
19
- . map_err ( |e| format ! ( "Failed to list /sys/class/udc: {e}" ) ) ?
20
- . next ( )
21
- . ok_or ( "No UDC found to attach the 9pfs gadget" . to_string ( ) ) ?
22
- . map_err ( |e| format ! ( "Failed to inspect the first entry in /sys/class/udc: {e}" ) ) ?
23
- . file_name ( ) ;
18
+ let udc = if let Some ( device) = device {
19
+ device. to_owned ( )
20
+ } else {
21
+ read_dir ( "/sys/class/udc" )
22
+ . map_err ( |e| format ! ( "Failed to list /sys/class/udc: {e}" ) ) ?
23
+ . next ( )
24
+ . ok_or ( "No UDC found to attach the 9pfs gadget" . to_string ( ) ) ?
25
+ . map_err ( |e| format ! ( "Failed to inspect the first entry in /sys/class/udc: {e}" ) ) ?
26
+ . file_name ( )
27
+ . into_string ( )
28
+ . map_err ( |e| format ! ( "invalid utf-8 in file name: {e:?}" ) ) ?
29
+ } ;
24
30
25
31
mount_apivfs ( "/sys/kernel/config" , "configfs" ) ?;
26
32
@@ -46,22 +52,17 @@ fn setup_9pfs_gadget(device: &String) -> Result<()> {
46
52
mkdir ( "/sys/kernel/config/usb_gadget/9pfs/configs/c.1" ) ?;
47
53
mkdir ( "/sys/kernel/config/usb_gadget/9pfs/configs/c.1/strings/0x409" ) ?;
48
54
49
- let function = format ! ( "/sys/kernel/config/usb_gadget/9pfs/functions/usb9pfs.{device} " ) ;
50
- let link = format ! ( "/sys/kernel/config/usb_gadget/9pfs/configs/c.1/usb9pfs.{device} " ) ;
55
+ let function = format ! ( "/sys/kernel/config/usb_gadget/9pfs/functions/usb9pfs.rootfs " ) ;
56
+ let link = format ! ( "/sys/kernel/config/usb_gadget/9pfs/configs/c.1/usb9pfs.rootfs " ) ;
51
57
mkdir ( & function) ?;
52
58
symlink ( & function, & link) ?;
53
59
54
- debug ! (
55
- "Attaching 9pfs gatget to UDC {}" ,
56
- udc. as_bytes( ) . escape_ascii( )
57
- ) ;
58
- write_file (
59
- "/sys/kernel/config/usb_gadget/9pfs/UDC" ,
60
- udc. as_encoded_bytes ( ) ,
61
- ) ?;
60
+ let udc = udc. as_bytes ( ) . escape_ascii ( ) . to_string ( ) ;
61
+
62
+ debug ! ( "Attaching 9pfs gatget to UDC {udc}" , ) ;
63
+ write_file ( "/sys/kernel/config/usb_gadget/9pfs/UDC" , udc) ?;
62
64
63
- let d = time:: Duration :: new ( 1 , 0 ) ;
64
- thread:: sleep ( d) ;
65
+ thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
65
66
Ok ( ( ) )
66
67
}
67
68
@@ -72,12 +73,8 @@ pub fn prepare_9pfs_gadget(options: &CmdlineOptions) -> Result<bool> {
72
73
. as_deref ( )
73
74
. is_some_and ( |flags| flags. contains ( "trans=usbg" ) )
74
75
{
75
- if let Some ( root) = & options. root {
76
- setup_9pfs_gadget ( root) ?;
77
- Ok ( true )
78
- } else {
79
- Err ( "Missing root= for 9p!" . into ( ) )
80
- }
76
+ setup_9pfs_gadget ( options. root . as_deref ( ) ) ?;
77
+ Ok ( true )
81
78
} else {
82
79
Ok ( false )
83
80
}
0 commit comments